Skip to content

Commit a238e5c

Browse files
joerchankartben
authored andcommitted
Bluetooth: audio: Fix byteorder in csip sef crypto function
Fix byteorder handling in csip sef crypto function. On big-endian system we also need to convert the k array from little-endian to big-endian. The Bluetooth protocol is always using little-endian ordering. Signed-off-by: Joakim Andersson <[email protected]>
1 parent eda1b3f commit a238e5c

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

subsys/bluetooth/audio/csip_crypto.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,8 @@ int bt_csip_sef(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE], const uint8_t sirk[BT_
171171

172172
LOG_DBG("SIRK %s", bt_hex(sirk, BT_CSIP_SIRK_SIZE));
173173

174-
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
175-
/* Swap because aes_cmac is big endian
176-
* and we are little endian
177-
*/
178-
sys_memcpy_swap(k1_tmp, k, sizeof(k1_tmp));
179-
} else {
180-
(void)memcpy(k1_tmp, k, sizeof(k1_tmp));
181-
}
174+
/* Swap because aes_cmac is big endian and k is little endian */
175+
sys_memcpy_swap(k1_tmp, k, sizeof(k1_tmp));
182176
LOG_DBG("BE: k %s", bt_hex(k1_tmp, sizeof(k1_tmp)));
183177

184178
err = s1(m, sizeof(m), s1_out);
@@ -195,10 +189,8 @@ int bt_csip_sef(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE], const uint8_t sirk[BT_
195189

196190
LOG_DBG("BE: k1 result %s", bt_hex(k1_out, sizeof(k1_out)));
197191

198-
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
199-
/* Swap result back to little endian */
200-
sys_mem_swap(k1_out, sizeof(k1_out));
201-
}
192+
/* Get result back to little endian. */
193+
sys_mem_swap(k1_out, sizeof(k1_out));
202194

203195
mem_xor_128(out_sirk, k1_out, sirk);
204196
LOG_DBG("out %s", bt_hex(out_sirk, BT_CSIP_SIRK_SIZE));

subsys/bluetooth/audio/csip_set_coordinator.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,23 +266,19 @@ static int sirk_decrypt(struct bt_conn *conn,
266266
uint8_t *out_sirk)
267267
{
268268
int err;
269-
uint8_t *k;
269+
const uint8_t *k;
270270

271271
if (IS_ENABLED(CONFIG_BT_CSIP_SET_COORDINATOR_TEST_SAMPLE_DATA)) {
272272
/* test_k is from the sample data from A.2 in the CSIS spec */
273-
static uint8_t test_k[] = {0x67, 0x6e, 0x1b, 0x9b,
274-
0xd4, 0x48, 0x69, 0x6f,
275-
0x06, 0x1e, 0xc6, 0x22,
276-
0x3c, 0xe5, 0xce, 0xd9};
277-
static bool swapped;
273+
static const uint8_t test_k[] = {
274+
/* Sample data is in big-endian, we need it in little-endian. */
275+
REVERSE_ARGS(0x67, 0x6e, 0x1b, 0x9b,
276+
0xd4, 0x48, 0x69, 0x6f,
277+
0x06, 0x1e, 0xc6, 0x22,
278+
0x3c, 0xe5, 0xce, 0xd9) };
278279

279280
LOG_DBG("Decrypting with sample data K");
280281

281-
if (!swapped && IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
282-
/* Swap test_k to little endian */
283-
sys_mem_swap(test_k, 16);
284-
swapped = true;
285-
}
286282
k = test_k;
287283
} else {
288284
k = conn->le.keys->ltk.val;

subsys/bluetooth/audio/csip_set_member.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,16 @@ static int sirk_encrypt(struct bt_conn *conn, const struct bt_csip_sirk *sirk,
145145
struct bt_csip_sirk *enc_sirk)
146146
{
147147
int err;
148-
uint8_t *k;
148+
const uint8_t *k;
149149

150150
if (IS_ENABLED(CONFIG_BT_CSIP_SET_MEMBER_TEST_SAMPLE_DATA)) {
151151
/* test_k is from the sample data from A.2 in the CSIS spec */
152-
static uint8_t test_k[] = {0x67, 0x6e, 0x1b, 0x9b,
153-
0xd4, 0x48, 0x69, 0x6f,
154-
0x06, 0x1e, 0xc6, 0x22,
155-
0x3c, 0xe5, 0xce, 0xd9};
156-
static bool swapped;
157-
158-
if (!swapped && IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
159-
/* Swap test_k to little endian */
160-
sys_mem_swap(test_k, 16);
161-
swapped = true;
162-
}
152+
static const uint8_t test_k[] = {
153+
/* Sample data is in big-endian, we need it in little-endian. */
154+
+ REVERSE_ARGS(0x67, 0x6e, 0x1b, 0x9b,
155+
0xd4, 0x48, 0x69, 0x6f,
156+
0x06, 0x1e, 0xc6, 0x22,
157+
0x3c, 0xe5, 0xce, 0xd9) };
163158
LOG_DBG("Encrypting test SIRK");
164159
k = test_k;
165160
} else {

0 commit comments

Comments
 (0)