Skip to content

Commit

Permalink
cleaning code and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Matěj Sychra committed Oct 8, 2018
2 parents 0b59b31 + 8a6a983 commit e63fe3f
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 175 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# THiNX AESLib (ESP32, ESP8266, Arduino)
# THiNX AESLib (ESP32, ESP8266)

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/8dded023f3d14a69b3c38c9f5fd66a40)](https://www.codacy.com/app/suculent/thinx-aes-lib?utm_source=github.com&utm_medium=referral&utm_content=suculent/thinx-aes-lib&utm_campaign=Badge_Grade)

An Arduino/ESP32/ESP8266 library to wrap AES encryption with Base64 support. This project is originally based on [AESLib by kakopappa](https://github.com/kakopappa/arduino-esp8266-aes-lib). This fork actually works, will be maintained at least for a while, and provides optimized methods that do not require using Arduino's flawed String objects (even though those are still in examples).
An ESP32/ESP8266/Arduino library for Arduino IDE to wrap AES encryption with Base64 support. This project is originally based on [AESLib by kakopappa](https://github.com/kakopappa/arduino-esp8266-aes-lib). This fork actually works, will be maintained at least for a while, and provides optimized methods that do not require using Arduino's flawed String objects (even though those are still in examples).

AESLib provides convenience methods for encrypting data to byte arrays and Strings, with optional additional base64 encoding to return strings instead of bare data.

In future this should use AES implementation from BearSSL, when available (to save more RAM in larger projects).

# Changes

1.0.3 - fixed padding (after encoding, not before)

# Client Example

```
Expand Down Expand Up @@ -35,15 +41,15 @@ void aes_init() {
String encrypt(char * msg, byte iv[]) {
int msgLen = strlen(msg);
char encrypted[2 * msgLen];
aesLib.encrypt(msg, encrypted, aes_key, iv);
aesLib.encrypt64(msg, encrypted, aes_key, iv);
return String(encrypted);
}
String decrypt(char * msg, byte iv[]) {
unsigned long ms = micros();
int msgLen = strlen(msg);
char decrypted[msgLen]; // half may be enough
aesLib.decrypt(msg, decrypted, aes_key, iv);
aesLib.decrypt64(msg, decrypted, aes_key, iv);
return String(decrypted);
}
Expand Down
7 changes: 4 additions & 3 deletions examples/base64_iv/base64_iv.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ void aes_init() {

// reset aes_iv to server-based value
int ivLen = base64_decode((char*)server_b64iv.c_str(), (char *)aes_iv, server_b64iv.length());
Serial.printf("Decoded IV bytes: %i\n", ivLen);
Serial.print("Decoded IV bytes: ");
Serial.println(ivLen);
print_key_iv();
}

String encrypt(char * msg, byte iv[]) {
int msgLen = strlen(msg);
char encrypted[2 * msgLen];
aesLib.encrypt(msg, encrypted, aes_key, iv);
aesLib.encrypt64(msg, encrypted, aes_key, iv);
return String(encrypted);
}

String decrypt(char * msg, byte iv[]) {
unsigned long ms = micros();
int msgLen = strlen(msg);
char decrypted[msgLen]; // half may be enough
aesLib.decrypt(msg, decrypted, aes_key, iv);
aesLib.decrypt64(msg, decrypted, aes_key, iv);
return String(decrypted);
}

Expand Down
10 changes: 2 additions & 8 deletions examples/complex/complex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ AESLib aesLib;
byte aes_key[] = { 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30 };

// General initialization vector (use your own)
byte aes_iv[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte aes_iv[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

// Generate IV (once)
void aes_init() {
Expand Down Expand Up @@ -80,7 +80,6 @@ String decode() {
return String(decoded);
}

// V2
String encrypt(char * msg, byte iv[]) {
unsigned long ms = micros();
int msgLen = strlen(msg);
Expand All @@ -92,7 +91,6 @@ String encrypt(char * msg, byte iv[]) {
return String(encrypted);
}

// V2
String decrypt(char * msg, byte iv[]) {
unsigned long ms = micros();
int msgLen = strlen(msg);
Expand Down Expand Up @@ -131,25 +129,21 @@ void loop() {

loopcount++;

//sprintf(cleartext, "START; %i \n", loopcount);
sprintf(cleartext, "AAAAAAAAA");

print_key_iv();

print_iv();

// V2
Serial.println("ENCRYPTION (char*)");
byte enc_iv[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // iv_block gets written to, reqires always fresh copy.
String encrypted = encrypt(cleartext, enc_iv);
sprintf(ciphertext, "%s", encrypted.c_str());
//ciphertext = encrypted.c_str();

Serial.print("Encrypted Result: ");
Serial.println(encrypted);
Serial.println();

// V2
Serial.println("DECRYPTION (char*)");
Serial.println(ciphertext);
byte dec_iv[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // iv_block gets written to, reqires always fresh copy.
Expand All @@ -160,7 +154,7 @@ void loop() {

String plain = String(cleartext);

if (plain.indexOf(decrypted) == -1) {
if (plain.indexOf(decrypted) == -1) {
Serial.println("Decryption FAILED!");
Serial.printf("At: %i \n", plain.indexOf(decrypted));
delay(5000);
Expand Down
14 changes: 6 additions & 8 deletions examples/simple/simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

AESLib aesLib;

String plaintext = "AAAAAAAAA";
String plaintext = "AAAAAAA";
int loopcount = 0;

char cleartext[256];
char ciphertext[512];

// AES Encryption Key
byte aes_key[] = { 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30 };
byte aes_key[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };

// General initialization vector (you must use your own IV's in production for full security!!!)
byte aes_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Expand All @@ -20,15 +20,14 @@ byte aes_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
void aes_init() {
aesLib.gen_iv(aes_iv);
// workaround for incorrect B64 functionality on first run...
sprintf(cleartext, plaintext.c_str());
encrypt(cleartext, aes_iv);
encrypt("HELLO WORLD!", aes_iv);
}

String encrypt(char * msg, byte iv[]) {
int msgLen = strlen(msg);
char encrypted[2 * msgLen];
char encrypted[4 * msgLen]; // AHA! needs to be large, 2x is not enough
aesLib.encrypt64(msg, encrypted, aes_key, iv);
return String(encrypted);
return String(encrypted);
}

String decrypt(char * msg, byte iv[]) {
Expand All @@ -41,15 +40,14 @@ String decrypt(char * msg, byte iv[]) {

void setup() {
Serial.begin(115200);

aes_init();
}

void loop() {

loopcount++;

sprintf(cleartext, plaintext.c_str());
sprintf(cleartext, "%s", plaintext.c_str());

// Encrypt
byte enc_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // iv_block gets written to, provide own fresh copy...
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/suculent/thinx-aes-lib.git"
},
"version": "1.0.0",
"version": "1.0.3",
"frameworks": "arduino",
"platforms": ["esp8266", "esp32"]
"platforms": ["esp8266", "esp32", "avr"]
}
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=AESLib
version=1.0.0
version=1.0.3
author=Matej Sychra
maintainer=Matej Sychra
sentence=Arduino/ESP8266 wrapper for AES library with 128-bit CBC encryption
paragraph=Arduino/ESP8266 wrapper for AES library with 128-bit CBC encryption
category="Data Processing"
category=Data Processing
url=https://github.com/suculent/thinx-aes-lib
architectures=esp8266,esp32
architectures=*
58 changes: 29 additions & 29 deletions src/AES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
/* This version derived by Mark Tillotson 2012-01-23, tidied up, slimmed down
and tailored to 8-bit microcontroller abilities and Arduino datatypes.
The s-box and inverse s-box were retained as tables (0.5kB PROGMEM) but all
the other transformations are coded to save table space. Many efficiency
The s-box and inverse s-box were retained as tables (0.5kB PROGMEM) but all
the other transformations are coded to save table space. Many efficiency
improvments to the routines mix_sub_columns() and inv_mix_sub_columns()
(mainly common sub-expression elimination).
Only the routines with precalculated subkey schedule are retained (together
with set_key() - this does however mean each AES object takes 240 bytes of
with set_key() - this does however mean each AES object takes 240 bytes of
RAM, alas)
The CBC routines side-effect the iv argument (so that successive calls work
Expand Down Expand Up @@ -218,7 +218,7 @@ static void inv_mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK])
byte a8 = f2(a4), b8 = f2(b4), c8 = f2(c4), d8 = f2(d4) ;
byte a9 = a8 ^ a1,b9 = b8 ^ b1,c9 = c8 ^ c1,d9 = d8 ^ d1 ;
byte ac = a8 ^ a4,bc = b8 ^ b4,cc = c8 ^ c4,dc = d8 ^ d4 ;

dt[i] = is_box (ac^a2 ^ b9^b2 ^ cc^c1 ^ d9) ;
dt[(i+5)&15] = is_box (a9 ^ bc^b2 ^ c9^c2 ^ dc^d1) ;
dt[(i+10)&15] = is_box (ac^a1 ^ b9 ^ cc^c2 ^ d9^d2) ;
Expand All @@ -232,21 +232,21 @@ AES::AES(){
byte ar_iv[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 };
memcpy(iv,ar_iv,8);
memcpy(iv+8,ar_iv,8);
arr_pad[0] = 0x01;
arr_pad[1] = 0x02;
arr_pad[2] = 0x03;
arr_pad[3] = 0x04;
arr_pad[4] = 0x05;
arr_pad[5] = 0x06;
arr_pad[6] = 0x07;
arr_pad[7] = 0x08;
arr_pad[8] = 0x09;
arr_pad[9] = 0x0a;
arr_pad[10] = 0x0b;
arr_pad[11] = 0x0c;
arr_pad[12] = 0x0d;
arr_pad[13] = 0x0e;
arr_pad[14] = 0x0f;
arr_pad[0] = 0x00;
arr_pad[1] = 0x00;
arr_pad[2] = 0x00;
arr_pad[3] = 0x00;
arr_pad[4] = 0x00;
arr_pad[5] = 0x00;
arr_pad[6] = 0x00;
arr_pad[7] = 0x00;
arr_pad[8] = 0x00;
arr_pad[9] = 0x00;
arr_pad[10] = 0x00;
arr_pad[11] = 0x00;
arr_pad[12] = 0x00;
arr_pad[13] = 0x00;
arr_pad[14] = 0x00;
}

/******************************************************************************/
Expand All @@ -257,29 +257,29 @@ byte AES::set_key (byte key [], int keylen)
switch (keylen)
{
case 16:
case 128:
case 128:
keylen = 16; // 10 rounds
round = 10 ;
break;
case 24:
case 192:
case 192:
keylen = 24; // 12 rounds
round = 12 ;
break;
case 32:
case 256:
case 256:
keylen = 32; // 14 rounds
round = 14 ;
break;
default:
round = 0;
default:
round = 0;
return FAILURE;
}
hi = (round + 1) << 4 ;
copy_n_bytes (key_sched, key, keylen) ;
byte t[4] ;
byte next = keylen ;
for (byte cc = keylen, rc = 1 ; cc < hi ; cc += N_COL)
for (byte cc = keylen, rc = 1 ; cc < hi ; cc += N_COL)
{
for (byte i = 0 ; i < N_COL ; i++)
t[i] = key_sched [cc-4+i] ;
Expand Down Expand Up @@ -340,7 +340,7 @@ byte AES::encrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK])
copy_and_key (s1, plain, (byte*) (key_sched)) ;

for (r = 1 ; r < round ; r++)
{
{
byte s2 [N_BLOCK] ;
mix_sub_columns (s2, s1) ;
copy_and_key (s1, s2, (byte*) (key_sched + r * N_BLOCK)) ;
Expand Down Expand Up @@ -411,7 +411,7 @@ byte AES::decrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK])
/******************************************************************************/

byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOCK])
{
{
while (n_block--)
{
byte tmp [N_BLOCK] ;
Expand All @@ -429,7 +429,7 @@ byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOC
/******************************************************************************/

byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block)
{
{
while (n_block--)
{
byte tmp [N_BLOCK] ;
Expand Down Expand Up @@ -505,7 +505,7 @@ void AES::padPlaintext(void* in,byte* out)
/******************************************************************************/

bool AES::CheckPad(byte* in,int lsize){
if (in[lsize-1] <= 0x0f){
if (in[lsize-1] <= 0x0f){
int lpad = (int)in[lsize-1];
for (int i = lsize - 1; i >= lsize-lpad; i--){
if (arr_pad[lpad - 1] != in[i]){
Expand Down
Loading

0 comments on commit e63fe3f

Please sign in to comment.