Hello everyone
It seems that there is a bug in the AES Coprocessor API which occurs sometimes (in most cases the result is correct). Below you can find a code snippet which encodes a plaintext (16 bytes) and returns the encoded data in the same array. The code runs on an JN5168-M05 module. To ensure that there is no "buffer" issue I used different tsReg128 structs for the result and the input data.
static void
encrypt(uint8_t *plaintext_and_result)
{
HEXDUMP("aes_128_key", &jn516x_aes_128_key, sizeof(tsReg128));
HEXDUMP("plaintext", plaintext_and_result, sizeof(tsReg128));
memcpy(&jn516x_aes_128_plaintext, plaintext_and_result, sizeof(tsReg128));
HEXDUMP("aes_128_plaintext", &jn516x_aes_128_plaintext, sizeof(tsReg128));
if(!bACI_ECBencodeStripe(&jn516x_aes_128_key,
jn516x_aes_128_key_changed,
&jn516x_aes_128_plaintext,
&jn516x_aes_128_result)) {
ERROR("Could not encrypt...");
}
ERROR("new key: %u\n", jn516x_aes_128_key_changed);
HEXDUMP("aes_128_result", &jn516x_aes_128_result, sizeof(tsReg128));
memcpy(plaintext_and_result, &jn516x_aes_128_result, sizeof(tsReg128));
HEXDUMP("result", plaintext_and_result, sizeof(tsReg128));
}
Log output of node 1:
aes_128_key:
0000 53 d6 00 56 6e 33 25 50 a3 62 db 17 f5 b2 a3 27 S..Vn3%P.b.....'
plaintext:
0000 26 0c ab c0 9c b1 3b 71 e8 3d 47 f1 d5 22 ab 20 &.....;q.=G..".
aes_128_plaintext:
0000 26 0c ab c0 9c b1 3b 71 e8 3d 47 f1 d5 22 ab 20 &.....;q.=G..".
new key: 0
aes_128_result:
0000 cf b4 e9 86 c5 4b 9a 1f 6f b2 ff 49 5e 91 0a dc .....K..o..I^...
result:
0000 cf b4 e9 86 c5 4b 9a 1f 6f b2 ff 49 5e 91 0a dc .....K..o..I^...
Log output on node 2:
aes_128_key:
0000 53 d6 00 56 6e 33 25 50 a3 62 db 17 f5 b2 a3 27 S..Vn3%P.b.....'
plaintext:
0000 26 0c ab c0 9c b1 3b 71 e8 3d 47 f1 d5 22 ab 20 &.....;q.=G..".
result:
0000 d0 95 5a 61 cc 17 3e 64 f6 7c 2d e2 01 f3 31 58 ..Za..>d.|-...1X
Log output of another AES implementation:
Key:
0000 53 d6 00 56 6e 33 25 50 a3 62 db 17 f5 b2 a3 27 S..Vn3%P.b.....'
Data:
0000 26 0c ab c0 9c b1 3b 71 e8 3d 47 f1 d5 22 ab 20 &.....;q.=G..".
Result:
0000 d0 95 5a 61 cc 17 3e 64 f6 7c 2d e2 01 f3 31 58 ..Za..>d.|-...1X
Thanks for your help!
Marko
已解决! 转到解答。
Hi Marko,
Did you get the same result when the jn516x_aes_128_key_changed
is TRUE?
Could you also print the result of the function?
Regards,
Ovidiu
Hi Ovidiu
Thank you for your help. It indeed had something to do with jn516x_aes_128_key_changed
flag. Setting it to true all the time fixed the issue. The problem was that we used the bACI_CCMstar() function in another part of our application, which apparently uses the same key memory as bACI_ECBencodeStripe(). Even if the local key did not change, it might have changed by calling the other function.
With kind regards
Marko