From 1ca05f6b165e225e91da75da06f5dbdf9631dedd Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 30 Oct 2013 06:47:13 -0700 Subject: [PATCH] Have sodium_memcmp(a,b,s) return -1 if a != b, document return value. --- README.markdown | 5 +++++ src/libsodium/sodium/utils.c | 2 +- test/default/sodium_utils.exp | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index a1868b9c..8970711f 100644 --- a/README.markdown +++ b/README.markdown @@ -173,6 +173,11 @@ In order to compare memory zones in constant time, Sodium provides: int sodium_memcmp(const void * const b1_, const void * const b2_, size_t size); +`sodium_memcmp()` returns `0` if `size` bytes at `b1_` and `b2_` are +equal, another value if they are not. Unlike `memcmp()`, +`sodium_memcmp()` cannot be used to put `b1_` and `b2_` into a defined +order. + And a convenience function for converting a binary buffer to a hexadecimal string: diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 6263dfc1..cc2dfbd4 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -38,7 +38,7 @@ sodium_memcmp(const void * const b1_, const void * const b2_, size_t size) for (i = 0U; i < size; i++) { d |= b1[i] ^ b2[i]; } - return (int) d; + return (int) ((1 & ((d - 1) >> 8)) - 1); } unsigned char * diff --git a/test/default/sodium_utils.exp b/test/default/sodium_utils.exp index 03cb2179..713a1d9b 100644 --- a/test/default/sodium_utils.exp +++ b/test/default/sodium_utils.exp @@ -1,6 +1,6 @@ 0 0 -255 +-1 0 0 30313233343536373839414243444546