Skip to content

Commit cc1f937

Browse files
cvinayakaescolar
authored andcommitted
Bluetooth: Fix endianness of cryptographic toolbox function h8
Convert endianness of supplied parameters before calculating the AES-CMAC. Bluetooth stores values in little-endian and crypto traditionally operates on big-endian storage. Relates to commit e9c542a ("Bluetooth: Add the cryptographic toolbox function h8"). Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent 069afbe commit cc1f937

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

subsys/bluetooth/crypto/bt_crypto.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,34 @@ int bt_crypto_h7(const uint8_t salt[16], const uint8_t w[16], uint8_t res[16])
261261

262262
int bt_crypto_h8(const uint8_t k[16], const uint8_t s[16], const uint8_t key_id[4], uint8_t res[16])
263263
{
264+
uint8_t key_id_s[4];
265+
uint8_t iks[16];
266+
uint8_t ks[16];
267+
uint8_t ss[16];
264268
int err;
265-
uint8_t ik[16];
266269

267-
err = bt_crypto_aes_cmac(s, k, 16, ik);
270+
LOG_DBG("k %s", bt_hex(k, 16));
271+
LOG_DBG("s %s", bt_hex(s, 16));
272+
LOG_DBG("key_id %s", bt_hex(key_id, 4));
273+
274+
sys_memcpy_swap(ks, k, 16);
275+
sys_memcpy_swap(ss, s, 16);
276+
277+
err = bt_crypto_aes_cmac(ss, ks, 16, iks);
268278
if (err) {
269279
return err;
270280
}
271281

272-
err = bt_crypto_aes_cmac(ik, key_id, 4, res);
282+
sys_memcpy_swap(key_id_s, key_id, 4);
283+
284+
err = bt_crypto_aes_cmac(iks, key_id_s, 4, res);
273285
if (err) {
274286
return err;
275287
}
276288

289+
LOG_DBG("res %s", bt_hex(res, 16));
290+
291+
sys_mem_swap(res, 16);
292+
277293
return 0;
278294
}

tests/bluetooth/bt_crypto/src/test_bt_crypto.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ ZTEST(bt_crypto, test_result_h7)
162162

163163
ZTEST(bt_crypto, test_result_h8)
164164
{
165-
uint8_t k[16] = {0xec, 0x02, 0x34, 0xa3, 0x57, 0xc8, 0xad, 0x05,
166-
0x34, 0x10, 0x10, 0xa6, 0x0a, 0x39, 0x7d, 0x9b};
167-
uint8_t s[16] = {0x15, 0x36, 0xd1, 0x8d, 0xe3, 0xd2, 0x0d, 0xf9,
168-
0x9b, 0x70, 0x44, 0xc1, 0x2f, 0x9e, 0xd5, 0xba};
169-
uint8_t key_id[4] = {0xcc, 0x03, 0x01, 0x48};
170-
171-
uint8_t exp_res[16] = {0xe5, 0xe5, 0xbe, 0xba, 0xae, 0x72, 0x28, 0xe7,
172-
0x22, 0xa3, 0x89, 0x04, 0xed, 0x35, 0x0f, 0x6d};
165+
uint8_t k[16] = {0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
166+
0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec};
167+
uint8_t s[16] = {0xba, 0xd5, 0x9e, 0x2f, 0xc1, 0x44, 0x70, 0x9b,
168+
0xf9, 0x0d, 0xd2, 0xe3, 0x8d, 0xd1, 0x36, 0x15};
169+
uint8_t key_id[4] = {0x48, 0x01, 0x03, 0xcc};
170+
171+
uint8_t exp_res[16] = {0x6d, 0x0f, 0x35, 0xed, 0x04, 0x89, 0xa3, 0x22,
172+
0xe7, 0x28, 0x72, 0xae, 0xba, 0xbe, 0xe5, 0xe5};
173173
uint8_t res[16];
174174

175175
bt_crypto_h8(k, s, key_id, res);

0 commit comments

Comments
 (0)