From c3d8041fe7d5dd189ab0bd7bf96d8261b062a238 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 22 Nov 2022 15:01:39 +0000 Subject: [PATCH] Introduce mbedtls_xor Signed-off-by: Dave Rodgman --- library/common.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/library/common.h b/library/common.h index a630fcc456..d1af48eb28 100644 --- a/library/common.h +++ b/library/common.h @@ -26,6 +26,7 @@ #include "mbedtls/build_info.h" #include +#include /** Helper to define a function as static except when building invasive tests. * @@ -390,6 +391,32 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c } #endif +/** + * Perform a fast block XOR operation, such that + * r[i] = a[i] ^ b[i] where 0 <= i < n + * + * \param r Pointer to result (buffer of at least \p n bytes). \p r + * may be equal to either \p a or \p b, but behaviour when + * it overlaps in other ways is undefined. + * \param a Pointer to input (buffer of at least \p n bytes) + * \param b Pointer to input (buffer of at least \p n bytes) + * \param n Number of bytes to process. + */ +static inline void mbedtls_xor( unsigned char* r, unsigned char const *a, unsigned char const *b, size_t n ) +{ + uint32_t *a32 = (uint32_t*)a; + uint32_t *b32 = (uint32_t*)b; + uint32_t *r32 = (uint32_t*)r; + for ( size_t i = 0; i < (n >> 2); i++ ) + { + r32[i] = a32[i] ^ b32[i]; + } + for ( size_t i = n - (n % 4) ; i < n; i++ ) + { + r[i] = a[i] ^ b[i]; + } +} + /* Fix MSVC C99 compatible issue * MSVC support __func__ from visual studio 2015( 1900 ) * Use MSVC predefine macro to avoid name check fail.