Skip to content

Commit 87c8b89

Browse files
Thalleycarlescufi
authored andcommitted
include: util: Add mem_xor functions
Add functions to do XOR on arrays of memory, with one that takes arbitrary sizes and one for 32 bits and 128 bits as those are common sizes for this functionality. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 7c53fa8 commit 87c8b89

File tree

9 files changed

+117
-42
lines changed

9 files changed

+117
-42
lines changed

include/zephyr/sys/util.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,45 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
657657
(((buflen) != 0) && \
658658
((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
659659

660+
/**
661+
* @brief XOR n bytes
662+
*
663+
* @param dst Destination of where to store result. Shall be @p len bytes.
664+
* @param src1 First source. Shall be @p len bytes.
665+
* @param src2 Second source. Shall be @p len bytes.
666+
* @param len Number of bytes to XOR.
667+
*/
668+
static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
669+
{
670+
while (len--) {
671+
*dst++ = *src1++ ^ *src2++;
672+
}
673+
}
674+
675+
/**
676+
* @brief XOR 32 bits
677+
*
678+
* @param dst Destination of where to store result. Shall be 32 bits.
679+
* @param src1 First source. Shall be 32 bits.
680+
* @param src2 Second source. Shall be 32 bits.
681+
*/
682+
static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
683+
{
684+
mem_xor_n(dst, src1, src2, 4U);
685+
}
686+
687+
/**
688+
* @brief XOR 128 bits
689+
*
690+
* @param dst Destination of where to store result. Shall be 128 bits.
691+
* @param src1 First source. Shall be 128 bits.
692+
* @param src2 Second source. Shall be 128 bits.
693+
*/
694+
static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
695+
{
696+
mem_xor_n(dst, src1, src2, 16);
697+
}
698+
660699
#ifdef __cplusplus
661700
}
662701
#endif

subsys/bluetooth/audio/csip_crypto.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <tinycrypt/cmac_mode.h>
1818
#include <tinycrypt/ccm_mode.h>
1919
#include <zephyr/sys/byteorder.h>
20+
#include <zephyr/sys/util.h>
2021

2122
#include "common/bt_str.h"
2223

@@ -51,16 +52,6 @@ static int aes_cmac(const uint8_t key[BT_CSIP_CRYPTO_KEY_SIZE],
5152
return 0;
5253
}
5354

54-
static void xor_128(const uint8_t a[16], const uint8_t b[16], uint8_t out[16])
55-
{
56-
size_t len = 16;
57-
/* TODO: Identical to the xor_128 from smp.c: Move to util */
58-
59-
while (len--) {
60-
*out++ = *a++ ^ *b++;
61-
}
62-
}
63-
6455
int bt_csip_sih(const uint8_t sirk[BT_CSIP_SET_SIRK_SIZE], uint8_t r[BT_CSIP_CRYPTO_PRAND_SIZE],
6556
uint8_t out[BT_CSIP_CRYPTO_HASH_SIZE])
6657
{
@@ -229,7 +220,7 @@ int bt_csip_sef(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE],
229220
sys_mem_swap(k1_out, sizeof(k1_out));
230221
}
231222

232-
xor_128(k1_out, sirk, out_sirk);
223+
mem_xor_128(out_sirk, k1_out, sirk);
233224
LOG_DBG("out %s", bt_hex(out_sirk, BT_CSIP_SET_SIRK_SIZE));
234225

235226
return 0;

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_adv_iso.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <soc.h>
1111
#include <zephyr/sys/byteorder.h>
12+
#include <zephyr/sys/util.h>
1213

1314
#include "hal/cpu.h"
1415
#include "hal/ccm.h"

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_sync_iso.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <soc.h>
1111
#include <zephyr/sys/byteorder.h>
12+
#include <zephyr/sys/util.h>
1213

1314
#include "hal/cpu.h"
1415
#include "hal/ccm.h"

subsys/bluetooth/controller/ll_sw/ull_conn_iso.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <zephyr/kernel.h>
88
#include <zephyr/sys/byteorder.h>
9+
#include <zephyr/sys/util.h>
910
#include <zephyr/bluetooth/hci_types.h>
1011

1112
#include "util/util.h"

subsys/bluetooth/controller/util/mem.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,6 @@ uint8_t mem_nz(uint8_t *src, uint16_t len)
134134
return 0;
135135
}
136136

137-
/**
138-
* @brief XOR bytes
139-
*/
140-
inline void mem_xor_n(uint8_t *dst, uint8_t *src1, uint8_t *src2, uint16_t len)
141-
{
142-
while (len--) {
143-
*dst++ = *src1++ ^ *src2++;
144-
}
145-
}
146-
147-
/**
148-
* @brief XOR 32-bits
149-
*/
150-
void mem_xor_32(uint8_t *dst, uint8_t *src1, uint8_t *src2)
151-
{
152-
mem_xor_n(dst, src1, src2, 4U);
153-
}
154-
155137
/**
156138
* @brief Unit test
157139
*/

subsys/bluetooth/controller/util/mem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,5 @@ uint16_t mem_index_get(const void *mem, const void *mem_pool, uint16_t mem_size)
6363

6464
void mem_rcopy(uint8_t *dst, uint8_t const *src, uint16_t len);
6565
uint8_t mem_nz(uint8_t *src, uint16_t len);
66-
void mem_xor_n(uint8_t *dst, uint8_t *src1, uint8_t *src2, uint16_t len);
67-
void mem_xor_32(uint8_t *dst, uint8_t *src1, uint8_t *src2);
6866

6967
uint32_t mem_ut(void);

subsys/bluetooth/host/smp.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,15 +1829,6 @@ static uint8_t smp_send_pairing_random(struct bt_smp *smp)
18291829
}
18301830

18311831
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
1832-
static void xor_128(const uint8_t p[16], const uint8_t q[16], uint8_t r[16])
1833-
{
1834-
size_t len = 16;
1835-
1836-
while (len--) {
1837-
*r++ = *p++ ^ *q++;
1838-
}
1839-
}
1840-
18411832
static int smp_c1(const uint8_t k[16], const uint8_t r[16],
18421833
const uint8_t preq[7], const uint8_t pres[7],
18431834
const bt_addr_le_t *ia, const bt_addr_le_t *ra,
@@ -1864,7 +1855,7 @@ static int smp_c1(const uint8_t k[16], const uint8_t r[16],
18641855
/* c1 = e(k, e(k, r XOR p1) XOR p2) */
18651856

18661857
/* Using enc_data as temporary output buffer */
1867-
xor_128(r, p1, enc_data);
1858+
mem_xor_128(enc_data, r, p1);
18681859

18691860
err = bt_encrypt_le(k, enc_data, enc_data);
18701861
if (err) {
@@ -1878,7 +1869,7 @@ static int smp_c1(const uint8_t k[16], const uint8_t r[16],
18781869

18791870
LOG_DBG("p2 %s", bt_hex(p2, 16));
18801871

1881-
xor_128(enc_data, p2, enc_data);
1872+
mem_xor_128(enc_data, p2, enc_data);
18821873

18831874
return bt_encrypt_le(k, enc_data, enc_data);
18841875
}

tests/unit/util/main.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,75 @@ ZTEST(util, test_IF_DISABLED)
646646
#undef test_IF_DISABLED_FLAG_B
647647
}
648648

649+
ZTEST(util, test_mem_xor_n)
650+
{
651+
const size_t max_len = 128;
652+
uint8_t expected_result[max_len];
653+
uint8_t src1[max_len];
654+
uint8_t src2[max_len];
655+
uint8_t dst[max_len];
656+
657+
memset(expected_result, 0, sizeof(expected_result));
658+
memset(src1, 0, sizeof(src1));
659+
memset(src2, 0, sizeof(src2));
660+
memset(dst, 0, sizeof(dst));
661+
662+
for (size_t i = 0U; i < max_len; i++) {
663+
const size_t len = i;
664+
665+
for (size_t j = 0U; j < len; j++) {
666+
src1[j] = 0x33;
667+
src2[j] = 0x0F;
668+
expected_result[j] = 0x3C;
669+
}
670+
671+
mem_xor_n(dst, src1, src2, len);
672+
zassert_mem_equal(expected_result, dst, len);
673+
}
674+
}
675+
676+
ZTEST(util, test_mem_xor_32)
677+
{
678+
uint8_t expected_result[4];
679+
uint8_t src1[4];
680+
uint8_t src2[4];
681+
uint8_t dst[4];
682+
683+
memset(expected_result, 0, sizeof(expected_result));
684+
memset(src1, 0, sizeof(src1));
685+
memset(src2, 0, sizeof(src2));
686+
memset(dst, 0, sizeof(dst));
687+
688+
for (size_t i = 0U; i < 4; i++) {
689+
src1[i] = 0x43;
690+
src2[i] = 0x0F;
691+
expected_result[i] = 0x4C;
692+
}
693+
694+
mem_xor_32(dst, src1, src2);
695+
zassert_mem_equal(expected_result, dst, 4);
696+
}
697+
698+
ZTEST(util, test_mem_xor_128)
699+
{
700+
uint8_t expected_result[16];
701+
uint8_t src1[16];
702+
uint8_t src2[16];
703+
uint8_t dst[16];
704+
705+
memset(expected_result, 0, sizeof(expected_result));
706+
memset(src1, 0, sizeof(src1));
707+
memset(src2, 0, sizeof(src2));
708+
memset(dst, 0, sizeof(dst));
709+
710+
for (size_t i = 0U; i < 16; i++) {
711+
src1[i] = 0x53;
712+
src2[i] = 0x0F;
713+
expected_result[i] = 0x5C;
714+
}
715+
716+
mem_xor_128(dst, src1, src2);
717+
zassert_mem_equal(expected_result, dst, 16);
718+
}
719+
649720
ZTEST_SUITE(util, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)