EEPROM SHA256+AES256 Password Storage Issue

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){}
  
}

replace

Serial.print(message[x]);   ==>  Serial.print(message[x], DEC);

might make the data human readable...

update - added code tags

line looked unreadable without: see below

Serial.print(message[x]); ==> Serial.print(message[x], DEC);

After working on this issue for several days, I ended up finding the problem about an hour after I posted in the forum. Wow you guys are great! :slight_smile:

As it turns out, the AES256 library operates on a 32 byte payload. So, in order to get good data in and out of it, you have to pad your data all the way out to 32 bytes before sending to be encrypted or decrypted. Once I set my data array to 32 bytes and assigned it zeros in the elements I was not using, it started working like a charm.

By writing only 12 bytes to EEPROM and reading back only 12 bytes, the remaining elements of the array that the AES256 library was looking at (out to a full 32 bytes) were beyond the size of the array and ended up being random data elements stored in memory. So, it ended up not being an EEPROM related issue after all.

did not know that, learned something today !