diff --git a/src/mesh/aes-ccm.cpp b/src/mesh/aes-ccm.cpp index a73c9473e..e1c65c7aa 100644 --- a/src/mesh/aes-ccm.cpp +++ b/src/mesh/aes-ccm.cpp @@ -16,22 +16,23 @@ * @param a First byte array to compare * @param b Second byte array to compare * @param len Number of bytes to compare - * @return 0 if arrays are equal, 1 if different or if inputs are invalid + * @return 0 if arrays are equal, -1 if different or if inputs are invalid */ static int constant_time_compare(const void *a_, const void *b_, size_t len) { - // Cast to volatile to prevent the compiler from optimizing out their comparison. + /* Cast to volatile to prevent the compiler from optimizing out their comparison. */ const volatile uint8_t *volatile a = (const volatile uint8_t *volatile) a_; const volatile uint8_t *volatile b = (const volatile uint8_t *volatile) b_; if (len == 0) return 0; if (a == NULL || b == NULL) - return 1; + return -1; size_t i; volatile uint8_t d = 0U; for (i = 0U; i < len; i++) { d |= (a[i] ^ b[i]); } + /* Constant time bit arithmetic to convert d > 0 to -1 and d = 0 to 0. */ return (1 & ((d - 1) >> 8)) - 1; }