From 825629c04bbee96f47bb74caab52b3ecc533cedf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mate=CC=8Cj=20Sychra?= Date: Thu, 11 Oct 2018 12:13:27 +0200 Subject: [PATCH] revised simple example; optional debug logging --- examples/simple/simple.ino | 60 +++++++++++++++++++++++--------------- src/AESLib.cpp | 33 ++++++++++++++++++++- src/AESLib.h | 2 ++ 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/examples/simple/simple.ino b/examples/simple/simple.ino index 595c506..8834b9f 100644 --- a/examples/simple/simple.ino +++ b/examples/simple/simple.ino @@ -4,8 +4,7 @@ AESLib aesLib; -String plaintext = "AAAAAAA"; -int loopcount = 0; +String plaintext = "HELLO WORLD!"; char cleartext[256]; char ciphertext[512]; @@ -18,15 +17,19 @@ byte aes_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Generate IV (once) void aes_init() { + Serial.println("gen_iv()"); aesLib.gen_iv(aes_iv); // workaround for incorrect B64 functionality on first run... - encrypt("HELLO WORLD!", aes_iv); + Serial.println("encrypt()"); + Serial.println(encrypt(strdup(plaintext.c_str()), aes_iv)); } -String encrypt(char * msg, byte iv[]) { +String encrypt(char * msg, byte iv[]) { int msgLen = strlen(msg); + Serial.print("msglen = "); Serial.println(msgLen); char encrypted[4 * msgLen]; // AHA! needs to be large, 2x is not enough aesLib.encrypt64(msg, encrypted, aes_key, iv); + Serial.print("encrypted = "); Serial.println(encrypted); return String(encrypted); } @@ -39,28 +42,39 @@ String decrypt(char * msg, byte iv[]) { } void setup() { - Serial.begin(115200); + Serial.begin(230400); + while (!Serial); // wait for serial port + delay(2000); + Serial.println("aes_init()"); aes_init(); } -void loop() { - - loopcount++; - - 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... - String encrypted = encrypt(cleartext, enc_iv); - sprintf(ciphertext, "%s", encrypted.c_str()); - Serial.print("Ciphertext: "); - Serial.println(encrypted); +/* non-blocking wait function */ +void wait(unsigned long milliseconds) { + unsigned long timeout = millis() + milliseconds; + while (millis() < timeout) { + yield(); + } +} - // Decrypt - byte dec_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... - String decrypted = decrypt(ciphertext, dec_iv); - Serial.print("Cleartext: "); - Serial.println(decrypted); +unsigned long loopcount = 0; - delay(500); +void loop() { + + if (Serial.available() > 0) { + + loopcount++; Serial.println(loopcount); // entry counter + + String readBuffer = Serial.readStringUntil('\n'); + Serial.println("INPUT:" + readBuffer); + + sprintf(cleartext, "%s", readBuffer.c_str()); // must not exceed 255 bytes; may contain a newline + + // 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... + String encrypted = encrypt(cleartext, enc_iv); + sprintf(ciphertext, "%s", encrypted.c_str()); + Serial.print("Ciphertext: "); + Serial.println(encrypted); + } } diff --git a/src/AESLib.cpp b/src/AESLib.cpp index 06d12c2..9601bda 100755 --- a/src/AESLib.cpp +++ b/src/AESLib.cpp @@ -30,7 +30,7 @@ String AESLib::decrypt(String msg, byte key[], byte my_iv[]) { int baseLen = base64_decode(message, (char *)out, outDataLen); message[baseLen] = '\0'; // ensure trailing zero after cstring - + return String(message); } @@ -101,24 +101,55 @@ String AESLib::encrypt(String msg, byte key[], byte my_iv[]) { /* Returns message encrypted and base64 encoded to be used as string. */ void AESLib::encrypt64(char * msg, char * output, byte key[], byte my_iv[]) { +#ifdef AES_DEBUG + Serial.print("incoming msg: "); Serial.println(msg); + Serial.print("incoming k-size: "); Serial.println(sizeof(key)); + Serial.print("incoming v-size: "); Serial.println(sizeof(my_iv)); +#endif + aes.set_key(key, sizeof(key)); int msgLen = strlen(msg); +#ifdef AES_DEBUG + Serial.println("- msgLen"); +#endif char b64data[base64_enc_len(msgLen)]; +#ifdef AES_DEBUG + Serial.println("- b64data"); +#endif + int b64len = base64_encode(b64data, (char*)msg, msgLen); +#ifdef AES_DEBUG + Serial.println("- b64len"); +#endif int paddedLen = b64len + (N_BLOCK - (b64len % N_BLOCK)) + 1; +#ifdef AES_DEBUG + Serial.println("- paddedLen"); +#endif + byte padded[paddedLen]; aes.padPlaintext(b64data, padded); +#ifdef AES_DEBUG + Serial.println("- padPlaintext"); +#endif byte cipher[2*b64len]; aes.do_aes_encrypt((byte *)padded, paddedLen, cipher, key, 128, my_iv); +#ifdef AES_DEBUG + Serial.println("- do_aes_encrypt"); +#endif char out2[4*b64len]; base64_encode(out2, (char *)cipher, aes.get_size() ); +#ifdef AES_DEBUG + Serial.println("- base64_encode"); +#endif strcpy(output, (char*)out2); + Serial.println("- strcpy"); +#endif } /* Returns message encrypted only to be used as byte array. */ diff --git a/src/AESLib.h b/src/AESLib.h index 776b3a4..150eee2 100755 --- a/src/AESLib.h +++ b/src/AESLib.h @@ -5,6 +5,8 @@ #include "AES.h" #include "base64.h" +#define AES_DEBUG + class AESLib { public: