1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

util: Add memcmp_const_time()

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Andreas Schneider 2016-04-01 10:09:45 +02:00 committed by Andreas Schneider
parent fec698dbfd
commit ba6e39076b
2 changed files with 25 additions and 0 deletions

View File

@ -279,6 +279,19 @@ limited by 'n' bytes
_PUBLIC_ size_t utf16_len_n(const void *src, size_t n);
_PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags);
/**
* @brief Constant time compare to memory regions.
*
* @param[in] s1 The first memory region to compare.
*
* @param[in] s2 The second memory region to compare.
*
* @param[in] n The length of the memory to comapre.
*
* @return 0 when the memory regions are equal, 0 if not.
*/
_PUBLIC_ int memcmp_const_time(const void *s1, const void *s2, size_t n);
/**
Do a case-insensitive, whitespace-ignoring string compare.
**/

View File

@ -333,3 +333,15 @@ _PUBLIC_ size_t utf16_len_n(const void *src, size_t n)
return len;
}
_PUBLIC_ int memcmp_const_time(const void *s1, const void *s2, size_t n)
{
const uint8_t *p1 = s1, *p2 = s2;
size_t i, sum = 0;
for (i = 0; i < n; i++) {
sum |= (p1[i] ^ p2[i]);
}
return sum != 0;
}