Commit 87c8b897 authored by Emil Gydesen's avatar Emil Gydesen Committed by Carles Cufi
Browse files

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: default avatarEmil Gydesen <emil.gydesen@nordicsemi.no>
parent 7c53fa86
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -657,6 +657,45 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
	(((buflen) != 0) &&                        \
	((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))

/**
 * @brief XOR n bytes
 *
 * @param dst  Destination of where to store result. Shall be @p len bytes.
 * @param src1 First source. Shall be @p len bytes.
 * @param src2 Second source. Shall be @p len bytes.
 * @param len  Number of bytes to XOR.
 */
static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
{
	while (len--) {
		*dst++ = *src1++ ^ *src2++;
	}
}

/**
 * @brief XOR 32 bits
 *
 * @param dst  Destination of where to store result. Shall be 32 bits.
 * @param src1 First source. Shall be 32 bits.
 * @param src2 Second source. Shall be 32 bits.
 */
static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
{
	mem_xor_n(dst, src1, src2, 4U);
}

/**
 * @brief XOR 128 bits
 *
 * @param dst  Destination of where to store result. Shall be 128 bits.
 * @param src1 First source. Shall be 128 bits.
 * @param src2 Second source. Shall be 128 bits.
 */
static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
{
	mem_xor_n(dst, src1, src2, 16);
}

#ifdef __cplusplus
}
#endif
+2 −11
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include <tinycrypt/cmac_mode.h>
#include <tinycrypt/ccm_mode.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>

#include "common/bt_str.h"

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

static void xor_128(const uint8_t a[16], const uint8_t b[16], uint8_t out[16])
{
	size_t len = 16;
	/* TODO: Identical to the xor_128 from smp.c: Move to util */

	while (len--) {
		*out++ = *a++ ^ *b++;
	}
}

int bt_csip_sih(const uint8_t sirk[BT_CSIP_SET_SIRK_SIZE], uint8_t r[BT_CSIP_CRYPTO_PRAND_SIZE],
		uint8_t out[BT_CSIP_CRYPTO_HASH_SIZE])
{
@@ -229,7 +220,7 @@ int bt_csip_sef(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE],
		sys_mem_swap(k1_out, sizeof(k1_out));
	}

	xor_128(k1_out, sirk, out_sirk);
	mem_xor_128(out_sirk, k1_out, sirk);
	LOG_DBG("out %s", bt_hex(out_sirk, BT_CSIP_SET_SIRK_SIZE));

	return 0;
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@

#include <soc.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>

#include "hal/cpu.h"
#include "hal/ccm.h"
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@

#include <soc.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>

#include "hal/cpu.h"
#include "hal/ccm.h"
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include <zephyr/bluetooth/hci_types.h>

#include "util/util.h"
Loading