mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2026-01-09 00:51:29 +00:00
util: add constant-time memcmp
Import from vboot_reference the constant-time memcmp implementation for safer usage in cryptography code. Signed-off-by: Vincent Palatin <vpalatin@chromium.org> BRANCH=none BUG=chrome-os-partner:62991 TEST=run curve25519 test Change-Id: I9c4c61e15912c978e13b6cc002af879c8ae8f630 Reviewed-on: https://chromium-review.googlesource.com/446098 Commit-Ready: Vincent Palatin <vpalatin@chromium.org> Tested-by: Vincent Palatin <vpalatin@chromium.org> Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
committed by
chrome-bot
parent
b8c4f23f75
commit
bff020fc6b
@@ -188,6 +188,25 @@ int memcmp(const void *s1, const void *s2, size_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant-time memory comparison */
|
||||
int safe_memcmp(const void *s1, const void *s2, size_t size)
|
||||
{
|
||||
const uint8_t *us1 = s1;
|
||||
const uint8_t *us2 = s2;
|
||||
int result = 0;
|
||||
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Code snippet without data-dependent branch due to Nate Lawson
|
||||
* (nate@root.org) of Root Labs.
|
||||
*/
|
||||
while (size--)
|
||||
result |= *us1++ ^ *us2++;
|
||||
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
void *memcpy(void *dest, const void *src, size_t len)
|
||||
{
|
||||
|
||||
@@ -66,6 +66,7 @@ int isspace(int c);
|
||||
int isalpha(int c);
|
||||
int isprint(int c);
|
||||
int memcmp(const void *s1, const void *s2, size_t len);
|
||||
int safe_memcmp(const void *s1, const void *s2, size_t len);
|
||||
void *memcpy(void *dest, const void *src, size_t len);
|
||||
__visible void *memset(void *dest, int c, size_t len);
|
||||
void *memmove(void *dest, const void *src, size_t len);
|
||||
|
||||
Reference in New Issue
Block a user