Introduce mbedtls_xor
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
diff --git a/library/common.h b/library/common.h
index a630fcc..d1af48e 100644
--- a/library/common.h
+++ b/library/common.h
@@ -26,6 +26,7 @@
#include "mbedtls/build_info.h"
#include <stdint.h>
+#include <stddef.h>
/** Helper to define a function as static except when building invasive tests.
*
@@ -390,6 +391,32 @@
}
#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.