]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Add constant time memory comparison function os_memcmp_const
authorJouni Malinen <j@w1.fi>
Sun, 29 Jun 2014 16:33:19 +0000 (19:33 +0300)
committerJouni Malinen <j@w1.fi>
Wed, 2 Jul 2014 09:38:47 +0000 (12:38 +0300)
This function is meant for comparing passwords or hash values where
difference in execution time could provide external observer information
about the location of the difference in the memory buffers. The return
value does not behave like os_memcmp(), i.e., os_memcmp_const() cannot
be used to sort items into a defined order. Unlike os_memcmp(),
execution time of os_memcmp_const() does not depend on the contents of
the compared memory buffers, but only on the total compared length.

Signed-off-by: Jouni Malinen <j@w1.fi>
src/utils/os.h
src/utils/os_internal.c
src/utils/os_none.c
src/utils/os_unix.c
src/utils/os_win32.c

index f019e26b13683098d6be9a7ebd8ba1015aeb2157..f196209b7a54bde14012dbff78f558031eef32c5 100644 (file)
@@ -584,6 +584,23 @@ static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size,
  */
 size_t os_strlcpy(char *dest, const char *src, size_t siz);
 
+/**
+ * os_memcmp_const - Constant time memory comparison
+ * @a: First buffer to compare
+ * @b: Second buffer to compare
+ * @len: Number of octets to compare
+ * Returns: 0 if buffers are equal, non-zero if not
+ *
+ * This function is meant for comparing passwords or hash values where
+ * difference in execution time could provide external observer information
+ * about the location of the difference in the memory buffers. The return value
+ * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to
+ * sort items into a defined order. Unlike os_memcmp(), execution time of
+ * os_memcmp_const() does not depend on the contents of the compared memory
+ * buffers, but only on the total compared length.
+ */
+int os_memcmp_const(const void *a, const void *b, size_t len);
+
 
 #ifdef OS_REJECT_C_LIB_FUNCTIONS
 #define malloc OS_DO_NOT_USE_malloc
index 2cb0d1262d20121cb1a748cf7ca7f4401b31805e..90b6688adc253f704a45d382541f9da1094dc7f1 100644 (file)
@@ -463,6 +463,20 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz)
 }
 
 
+int os_memcmp_const(const void *a, const void *b, size_t len)
+{
+       const u8 *aa = a;
+       const u8 *bb = b;
+       size_t i;
+       u8 res;
+
+       for (res = 0, i = 0; i < len; i++)
+               res |= aa[i] ^ bb[i];
+
+       return res;
+}
+
+
 char * os_strstr(const char *haystack, const char *needle)
 {
        size_t len = os_strlen(needle);
index 228c4724cdd126975c3fd3ad3711e66328eb7d8b..26491115a44264850539ab4610d71f08834eb280 100644 (file)
@@ -218,6 +218,11 @@ size_t os_strlcpy(char *dest, const char *src, size_t size)
 }
 
 
+int os_memcmp_const(const void *a, const void *b, size_t len)
+{
+       return 0;
+}
+
 char * os_strstr(const char *haystack, const char *needle)
 {
        return NULL;
index 008ec6b0846e926778eb988176d548a757e62324..d3860aec1aeb2e2a9c6d90f0abdb6af725df5180 100644 (file)
@@ -450,6 +450,20 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz)
 }
 
 
+int os_memcmp_const(const void *a, const void *b, size_t len)
+{
+       const u8 *aa = a;
+       const u8 *bb = b;
+       size_t i;
+       u8 res;
+
+       for (res = 0, i = 0; i < len; i++)
+               res |= aa[i] ^ bb[i];
+
+       return res;
+}
+
+
 #ifdef WPA_TRACE
 
 void * os_malloc(size_t size)
index 1cfa7a5f81ab7228d25be0eeacc2ba784eacf792..55937dec687f619164dcdfd64e196dc4c471fde0 100644 (file)
@@ -244,3 +244,17 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz)
 
        return s - src - 1;
 }
+
+
+int os_memcmp_const(const void *a, const void *b, size_t len)
+{
+       const u8 *aa = a;
+       const u8 *bb = b;
+       size_t i;
+       u8 res;
+
+       for (res = 0, i = 0; i < len; i++)
+               res |= aa[i] ^ bb[i];
+
+       return res;
+}