utils: Add a constant time memeq() variant for cryptographic purposes
This commit is contained in:
@@ -109,6 +109,25 @@ void memwipe_noinline(void *ptr, size_t n)
|
||||
memwipe_inline(ptr, n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Described in header.
|
||||
*/
|
||||
bool memeq_const(const void *x, const void *y, size_t len)
|
||||
{
|
||||
const u_char *a, *b;
|
||||
u_int bad = 0;
|
||||
size_t i;
|
||||
|
||||
a = (const u_char*)x;
|
||||
b = (const u_char*)y;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
bad |= a[i] != b[i];
|
||||
}
|
||||
return !bad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Described in header.
|
||||
*/
|
||||
|
||||
@@ -184,6 +184,11 @@ static inline bool memeq(const void *x, const void *y, size_t len)
|
||||
return memcmp(x, y, len) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as memeq(), but with a constant runtime, safe for cryptographic use.
|
||||
*/
|
||||
bool memeq_const(const void *x, const void *y, size_t len);
|
||||
|
||||
/**
|
||||
* Calling memcpy() with NULL pointers, even with n == 0, results in undefined
|
||||
* behavior according to the C standard. This version is guaranteed to not
|
||||
|
||||
Reference in New Issue
Block a user