This nonce:
938e465cecefcb55ea32c69c485eb0af69dccd8c0f1244fd
you used for a successful decryption was not the one received during pairing… It is the one received within the message returned from the Nuki with the keyturner states.
(I deducted that the nonce (number only used once) also should not be reused
?)
But still it works on your side still not on mine…
updated the code:
unsigned char encrData[encrMsgLen + crypto_secretbox_MACBYTES];
unsigned char padding[crypto_secretbox_MACBYTES] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
memcpy(encrData, &padding, sizeof(padding));
memcpy(&encrData[crypto_secretbox_MACBYTES], &recData[30], encrMsgLen);
maybe not completely the correct/best way but it does generate this buffer to be decoded:
Rec encrypted data: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 65 2f bb e2 c3 82 fd 43 a1 df 4e 81 fa e6 08 96 72 58 4b 59 3b e4 4d 7a e4 c4 07 2f f9 85 0e cf 7d 66 c4 00 3c 45 ef 79 08 f1 29 75 f1
I used the nonce I received as header of the encrypted msg:
received nonce: 22 ad 2d b9 85 96 a6 27 e6 b9 d2 40 f3 39 24 08 0a 34 98 ef 24 95 78 fb
But still decryption failed. Are you still able to decrypt this?
I tried to change the size I give to the decode to the encrypted msg size + the 16 padding bytes but that also did not work:
decode(encrData, decrData, encrMsgLen + crypto_secretbox_MACBYTES, recNonce, secretKeyK);
What should the size be for the decrypted msg?
it’s now:
unsigned char decrData[encrMsgLen - crypto_secretbox_MACBYTES];
thx!
full code as the above snippets do not provide an overview:
//handle encrypted msg
unsigned char recNonce[crypto_secretbox_NONCEBYTES];
unsigned char recAuthorizationId[4];
unsigned char recMsgLen[2];
memcpy(recNonce, &recData[0], 24);
memcpy(recAuthorizationId, &recData[24], 4);
memcpy(recMsgLen, &recData[28], 2);
uint16_t encrMsgLen = 0;
memcpy(&encrMsgLen, recMsgLen, 2);
unsigned char encrData[encrMsgLen + crypto_secretbox_MACBYTES];
unsigned char padding[crypto_secretbox_MACBYTES] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
memcpy(encrData, &padding, sizeof(padding));
memcpy(&encrData[crypto_secretbox_MACBYTES], &recData[30], encrMsgLen);
#ifdef DEBUG_NUKI
log_d("Received encrypted msg...");
printBuffer(recNonce, sizeof(recNonce), false, "received nonce");
printBuffer(recAuthorizationId, sizeof(recAuthorizationId), false, "Received AuthorizationId");
log_d("len encr msg: %d", encrMsgLen);
printBuffer(encrData, sizeof(encrData), false, "Rec encrypted data");
#endif
unsigned char decrData[encrMsgLen - crypto_secretbox_MACBYTES];
decode(encrData, decrData, encrMsgLen + crypto_secretbox_MACBYTES, recNonce, secretKeyK);