From 51fb0f4c6484c45ffe4e75655e05538f4294c870 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 23 Apr 2013 11:56:10 -0700 Subject: [PATCH] Add sodium_memcmp() --- src/libsodium/include/sodium/utils.h | 3 +++ src/libsodium/sodium/utils.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h index ff6cdf9d..149a5cdf 100644 --- a/src/libsodium/include/sodium/utils.h +++ b/src/libsodium/include/sodium/utils.h @@ -16,6 +16,9 @@ unsigned char *_sodium_alignedcalloc(unsigned char ** const unaligned_p, SODIUM_EXPORT void sodium_memzero(void * const pnt, const size_t len); +SODIUM_EXPORT +int sodium_memcmp(const void * const b1_, const void * const b2_, size_t size); + #ifdef __cplusplus } #endif diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 70ff1136..784a3a4f 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -27,6 +27,20 @@ sodium_memzero(void * const pnt, const size_t len) #endif } +int +sodium_memcmp(const void * const b1_, const void * const b2_, size_t size) +{ + const unsigned char *b1 = b1_; + const unsigned char *b2 = b2_; + size_t i; + unsigned char d = (unsigned char) 0U; + + for (i = 0U; i < size; i++) { + d |= b1[i] ^ b2[i]; + } + return (int) d; +} + unsigned char * _sodium_alignedcalloc(unsigned char ** const unaligned_p, const size_t len) {