Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypts/decrypts only first 16 bytes of data #2

Closed
spinningarrow opened this issue Mar 18, 2014 · 3 comments
Closed

Encrypts/decrypts only first 16 bytes of data #2

spinningarrow opened this issue Mar 18, 2014 · 3 comments

Comments

@spinningarrow
Copy link

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
Copy link
Owner

qistoph commented Mar 18, 2014

"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
Copy link
Author

That helps; thank you!

@qistoph
Copy link
Owner

qistoph commented Mar 19, 2014

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.

@qistoph qistoph closed this as completed Mar 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants