qistoph / ArduinoAES256

Ilya's byte-oriented AES-256 implementation implemented for Arduino
62 stars 22 forks source link

Encrypts/decrypts only first 16 bytes of data #2

Closed spinningarrow closed 10 years ago

spinningarrow commented 10 years ago

If I change the data in the examples to 32 bytes, for instance:

uint8_t data[] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};

the output on the serial monitor is:

Key: 
0001020304050607000102030405060700010203040506070001020304050607
Unencrypted data: 
000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F
Encrypted data: 
7EE7EF984D51B1E3E1A31F8559988CF2000102030405060708090A0B0C0D0E0F
Back decrypted data: 
000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F

As you can see, only the first 16 bytes are being encrypted (7EE7EF984D51B1E3E1A31F8559988CF2); the rest is still cleartext. Why is that?

qistoph commented 10 years ago

"AES is a variant of Rijndael which has a fixed block size of 128 bits, and a key size of 128, 192, or 256 bits." (http://en.wikipedia.org/wiki/Advanced_Encryption_Standard)

You will have to update the data in blocks of 128 bits.

Try something like this:

// data[32]
aes256_encrypt_ecb(&ctxt, data); // encrypt first block of 128 bits in place
aes256_encrypt_ecb(&ctxt, data+16); // encrypt second block of 128 bits in place
DUMP("Encrypted data: ", i, data, sizeof(data));

aes256_decrypt_ecb(&ctxt, data); // decrypt first block of 128 bits in place
aes256_decrypt_ecb(&ctxt, data+16); // decrypt second block of 128 bits in place
DUMP("Back decrypted data: ", i, data, sizeof(data));
spinningarrow commented 10 years ago

That helps; thank you!

qistoph commented 10 years ago

Please be aware though that the described method performs ECB mode encryption.

Have a look at http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_.28ECB.29 for more details.