|
| 1 | +/* |
| 2 | + ArduinoCrypto AES128 Example |
| 3 | +
|
| 4 | + This sketch demonstrates how to run AES128 encryption and decryption for an input string. |
| 5 | +
|
| 6 | + Circuit: |
| 7 | + - Nano 33 IoT board |
| 8 | +
|
| 9 | + created 13 July 2020 |
| 10 | + by Luigi Gubello |
| 11 | +
|
| 12 | + This example code is in the public domain. |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <ArduinoBearSSL.h> |
| 16 | + |
| 17 | +uint8_t key[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02}; |
| 18 | +uint8_t enc_iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01}; |
| 19 | +uint8_t dec_iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01}; |
| 20 | +uint8_t input[16] = "ArduinoArduino"; // {0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x00,0x00} |
| 21 | + |
| 22 | +void setup() { |
| 23 | + Serial.begin(9600); |
| 24 | + while (!Serial); |
| 25 | +} |
| 26 | + |
| 27 | +void loop() { |
| 28 | + |
| 29 | + Serial.print("Key: "); |
| 30 | + printHex(key, 16); |
| 31 | + Serial.println(" "); |
| 32 | + Serial.print("IV: "); |
| 33 | + printHex(enc_iv, 16); |
| 34 | + Serial.println(" "); |
| 35 | + Serial.print("AES128 Encryption of '"); |
| 36 | + printHex(input, 16); |
| 37 | + Serial.print("' is 0x"); |
| 38 | + AES128.runEnc(key, 16, input, 16, enc_iv); // expect 0x65D0F7758B094114AFA6D33A5EA0716A |
| 39 | + printHex(input, 16); |
| 40 | + Serial.println(" "); |
| 41 | + Serial.println(" "); |
| 42 | + Serial.print("Key: "); |
| 43 | + printHex(key, 16); |
| 44 | + Serial.println(" "); |
| 45 | + Serial.print("IV: "); |
| 46 | + printHex(dec_iv, 16); |
| 47 | + Serial.println(" "); |
| 48 | + Serial.print("AES128 Decryption of '"); |
| 49 | + printHex(input, 16); |
| 50 | + Serial.print("' is 0x"); |
| 51 | + AES128.runDec(key, 16, input, 16, dec_iv); |
| 52 | + printHex(input, 16); |
| 53 | + Serial.println(" "); |
| 54 | + while (1); |
| 55 | +} |
| 56 | + |
| 57 | +void printHex(uint8_t *text, size_t size) { |
| 58 | + for (byte i = 0; i < size; i = i + 1) { |
| 59 | + if (text[i] < 16) { |
| 60 | + Serial.print("0"); |
| 61 | + } |
| 62 | + Serial.print(text[i], HEX); |
| 63 | + } |
| 64 | +} |
0 commit comments