Hi all,
I'm using BLE on ESP32 to run the provisioning. This uses AES CTR mode encrypt & decryption. I've got a related question. I noticed during the session establishment process that I had to increment the nonce to get the link to work. After exchanging the initial random IV (nonce). I used this to send the first message, and incremented it by 2 to account for the 32 bytes I'd just sent. I was then able to decrypt the response from the subsequent read. So, it seems that both sides are keeping track of the nonce and using a single shared key and nonce. Am I right?
I've found 2 stumbling blocks.
1. When I write repeatedly to the start scan or another scan endpoint, only the first works. I'm continuing the increase the nonce by what I believe to be the correct amount. But the second attempt always kills the connection due to this error:
Code: Select all
E (165849) proto_wifi_scan: Unable to unpack scan message
E (165859) protocomm: Request handler for prov-scan failed
E (165859) protocomm_ble: Invalid content received, killing connection
2. When I read back from the scan status endpoint, weirdly the encrypted data doesn't seem to change. So, clearly the nonce is not being incremented on the device as part of the readback... It's all very confusing...
I'm using CryptoJS on my end, and I haven't figured out how to let it handle the nonce for me, not that I'm sure it could do so given the odd things I've observed... N.B. The whole things works fine with the supplied ESP Provisioning app. So, I'm clearly not understanding something here. Or something very 'bespoke' is going on to make it more difficult to port.
/* de-cryption to get the client random number */
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, key_encrypt, 128); /* do not use mbedtls_aes_setkey_dec(&aes, key_encrypt, 128); */
offset = 0;
input[0] = 0xbb;
input[1] = 0x83;
input[2] = 0xe0;
input[3] = 0x37;
input[4] = 0x8d;
input[5] = 0x6d;
input[6] = 0x96;
input[7] = 0x13;
input[8] = 0x1f;
input[9] = 0x73;
input[10] = 0xdb;
input[11] = 0x95;
input[12] = 0xb7;
input[13] = 0xb1;
input[14] = 0x8a;
input[15] = 0x7f;
// memset(stream_block, 0x0u, 16u);
mbedtls_aes_crypt_ctr(&aes, 16u, &offset, nonce, stream_block, input, output);
mbedtls_aes_free( &aes );
https://zh.wikipedia.org/wiki/%E5%88%86 ... R%EF%BC%89