I'm attempting to store AES256 encrypted data to the EEPROM area that can be retrieved and decoded. I can successfully encrypt and decrypt with SHA256+AES256 in SRAM, however, once I save the data to EEPROM and retrieve it, I can no longer decrypt the data again. Am I doing something wrong in the saving and retrieving? I have tried using a variety of EEPROM read/write methods including the standard Arduino version, AVR version and now I'm using the EEPROMAnything.h, but all have similar results.
I have included the code sketch below to help illustrate the issue. In theory, the top section generates some data, encrypts it and writes it to the EEPROM area. The bottom section reads the EEPROM data and attempts to decrypt it. When all of the code is uncommented, it works properly. If I comment out the top section at the markers I have indicated, the bottom section should still be able to retrieve the data from EEPROM and decrypt, with the same results, but that does not happen.
How can I save my AES256 encrypted data to EEPROM so that it can be retrieved and used just like it is in SRAM? Is there a data conversion happening causing the EEPROM read data from being usable by AES256?
Thanks.
-John
#include "sha256.h"
#include "aes256.h"
#include "EEPROM.h"
#include "EEPROMAnything.h"
void setup() {
Serial.begin(38400);
}
void enc(uint8_t* key, uint8_t* message){
aes256_context ctx;
aes256_init(&ctx, key);
aes256_encrypt_ecb(&ctx, message);
aes256_done(&ctx);
}
void dec(uint8_t* key, uint8_t* message){
aes256_context ctx;
aes256_init(&ctx, key);
aes256_decrypt_ecb(&ctx, message);
aes256_done(&ctx);
}
void loop() {
delay(2000);
Serial.println("==============================================");
uint8_t message[12];
int x;
// <--comment block starts here
//message to encrypt
Serial.println("message:");
uint8_t yt;
for(yt=0;yt<12;yt++){
message[yt] = yt*2;
Serial.print(message[yt]);
Serial.print(" ");
}
Serial.println("");
Sha256.init();
Sha256.print("this is my key");
enc(Sha256.result(), &message[0]);
//print the resulting encrypted data
Serial.println("Resulting encrypted code");
for(x=0;x<11;x++){
Serial.print(message[x]);
Serial.print(" ");
}
Serial.println("");
//save the resulting key to memory
Serial.println("Encryption done and saved");
EEPROM_writeAnything(12, message);
delay(500);
// <--comment block ends here
/////////////////////// Decryption Section Below//////////////////////////////////
//read the encrypted password from memory
Serial.println("Reading encrypted password from memory");
EEPROM_readAnything(12, message);
Sha256.init();
Sha256.print("this is my key");
Serial.println("Decrypting password");
dec(Sha256.result(), &message[0]);
Serial.println("Dedcrypted message");
for(x=0;x<12;x++){
Serial.print(message[x]);
Serial.print(" ");
}
Serial.println("");
while(1){}
}