Last active
March 2, 2018 22:40
-
-
Save jessicacarneiro/614bf64cf3be0abcf3c9ffe0730ae9b5 to your computer and use it in GitHub Desktop.
Example of use of RELIC's AES CBC mode on Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern "C" { | |
| #include "../include/relic.h" | |
| } | |
| #define KEYSZ 16 | |
| #define CIPHER_TEXT_SZ(PLAINTEXT_SZ) (PLAINTEXT_SZ + BLOCK_SZ - (PLAINTEXT_SZ % BLOCK_SZ)) | |
| #include <string.h> | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| while (!Serial) | |
| ; // wait for serial port to connect | |
| core_init(); | |
| pc_param_set_any(); | |
| } | |
| void loop() | |
| { | |
| uint8_t key[KEYSZ] = { 1 }; | |
| uint8_t iv[BC_LEN] = { 0 }; | |
| uint8_t input[5] = {'A','l','i','c','e'}; | |
| uint8_t output[CIPHER_TEXT_SZ(5)]; | |
| int output_sz = CIPHER_TEXT_SZ(5); | |
| Serial.print("1.Input: "); | |
| for (int i = 0; i < 5; i++) { | |
| Serial.print(input[i], HEX); | |
| Serial.print(" "); | |
| } | |
| Serial.println("\n"); | |
| if (bc_aes_cbc_enc(output, &output_sz, input, 5, key, KEYSZ*8, iv) == STS_OK) | |
| Serial.print("2.Success enc\n"); | |
| else | |
| Serial.print("3.Error enc\n"); | |
| Serial.print("2.Output size in enc is "); | |
| Serial.println(output_sz); | |
| uint8_t dec[CIPHER_TEXT_SZ(5)]; | |
| memset(dec, 0, CIPHER_TEXT_SZ(5)); | |
| Serial.println(); | |
| int dec_sz = KEYSZ; | |
| if (bc_aes_cbc_dec(dec, &dec_sz, output, output_sz, key, KEYSZ*8, iv) == STS_OK) | |
| Serial.print("4.Success dec\n"); | |
| else | |
| Serial.print("4.Error dec\n"); | |
| Serial.print("5.Output size in dec is "); | |
| Serial.println(dec_sz); | |
| Serial.print("6.Output: "); | |
| for (int i = 0; i < dec_sz; i++) { | |
| Serial.print(dec[i], HEX); | |
| Serial.print(" "); | |
| } | |
| Serial.println("\n"); | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment