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