EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Trying used AES CBC 256-bit encryption mode having problems.

Started by FINETECH November 23, 2011
#use AES_CRYPT.LIB
#define AES_CBC_BLOCK_SIZE 16

/*
////////////////////////////////////////////////////////////////////
// AES CBC 256-bit encryption mode with RCM5600W
// For tester: http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors#aes-cbc-256
////////////////////////////////////////////////////////////////////

Key:
0X603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4
InitialVector:
0X000102030405060708090a0b0c0d0e0f (initial vector)
PlainText:
0X6bc1bee22e409f96e93d7e117393172a (data input)
CipherText Expected:
0xf58c4c04d6e5f1ba779eabfb5f7bfbd6 (data output expected)
CipherText Result:
0Xd7bbc90f60d24ab41ae6f3eed7c9c2a1 (data output error in here ??)

*/

const char key[AES_CBC_BLOCK_SIZE * 2] = {
'\x60', '\x3d', '\xeb', '\x10', '\x15', '\xca', '\x71', '\xbe',
'\x2b', '\x73', '\xae', '\xf0', '\x85', '\x7d', '\x77', '\x81',
'\x1f', '\x35', '\x2c', '\x07', '\x3b', '\x61', '\x08', '\xd7',
'\x2d', '\x98', '\x10', '\xa3', '\x09', '\x14', '\xdf', '\xf4',
};

const char initial_vector[AES_CBC_BLOCK_SIZE] = {
'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
'\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
};

const char plain_text[AES_CBC_BLOCK_SIZE] = {
'\x6b', '\xc1', '\xbe', '\xe2', '\x2e', '\x40', '\x9f', '\x96',
'\xe9', '\x3d', '\x7e', '\x11', '\x73', '\x93', '\x17', '\x2a',
};

const char cipher_text_expected[AES_CBC_BLOCK_SIZE] = {
'\xf5', '\x8c', '\x4c', '\x04', '\xd6', '\xe5', '\xf1', '\xba',
'\x77', '\x9e', '\xab', '\xfb', '\x5f', '\x7b', '\xfb', '\xd6',
};
int main(void) {
auto int i;
auto char text[256];
auto AESstreamState state;

// ENCRYPT
AESinitStream256(&state, key, initial_vector);
memcpy(text, plain_text, sizeof(plain_text));
AESencryptStream_CBC(&state, text, AES_CBC_BLOCK_SIZE * 2);
printf("Encrypted text:\n");
for (i = 0; i < sizeof(plain_text); i++) {
printf("%02x ", (int) text[i]);
if (0 == ((i+1) % AES_CBC_BLOCK_SIZE )) printf("\n");
}
// Encrypted text:
// d7 bb c9 0f 60 d2 4a b4 1a e6 f3 ee d7 c9 c2 a1 (ERROR)
printf("Expected text:\n");
for (i = 0; i < sizeof(cipher_text_expected); i++) {
printf("%02x ", (int) cipher_text_expected[i]);
if (0 == ((i+1) % AES_CBC_BLOCK_SIZE )) printf("\n");
}
printf("\n");
// Expected text:
// f5 8c 4c 04 d6 e5 f1 ba 77 9e ab fb 5f 7b fb d6
/////////////////////////////////////////////////////////
// DECRYPT
AESinitStream256(&state, key, initial_vector);
memcpy(text, cipher_text_expected, sizeof(cipher_text_expected));
AESdecryptStream_CBC(&state, text, AES_CBC_BLOCK_SIZE * 2);
printf("Decrypted text:\n");
for (i = 0; i < sizeof(cipher_text_expected); i++) {
printf("%02x ", (int) text[i]);
if (0 == ((i+1) % AES_CBC_BLOCK_SIZE)) printf("\n");
}
// Decrypted text:
// 43 22 6c 48 49 87 b6 93 e6 56 fb b2 20 b9 7f 05 (ERROR)
printf("Expected text:\n");
for (i = 0; i < sizeof(plain_text); i++) {
printf("%02x ", (int) plain_text[i]);
if (0 == ((i+1) % AES_CBC_BLOCK_SIZE)) printf("\n");
}
printf("\n");
// Expected text:
// 6b c1 be e2 2e 40 9f 96 e9 3d 7e 11 73 93 17 2a
}

Above program, when I try with key: 128 bit, produce the correct output. But when I used key: 256 bit, the program produces the wrong output.

I hope there are friends who can help find the error .. Thank you
Regards,
Yudha


The 2024 Embedded Online Conference