Skip to content

Commit

Permalink
revised simple example; optional debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Matěj Sychra committed Oct 11, 2018
1 parent e63fe3f commit 825629c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
60 changes: 37 additions & 23 deletions examples/simple/simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

AESLib aesLib;

String plaintext = "AAAAAAA";
int loopcount = 0;
String plaintext = "HELLO WORLD!";

char cleartext[256];
char ciphertext[512];
Expand All @@ -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);
}

Expand All @@ -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);
}
}
33 changes: 32 additions & 1 deletion src/AESLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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. */
Expand Down
2 changes: 2 additions & 0 deletions src/AESLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "AES.h"
#include "base64.h"

#define AES_DEBUG

class AESLib
{
public:
Expand Down

0 comments on commit 825629c

Please sign in to comment.