1
0
Fork 0
forked from nuttx/nuttx-update

libc:add timingsafe_bcmp to libc

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2022-08-27 12:22:29 +08:00 committed by Xiang Xiao
parent 21934122d5
commit 1a5351edac
3 changed files with 45 additions and 1 deletions

View file

@ -93,6 +93,7 @@ FAR void *memmem(FAR const void *haystack, size_t haystacklen,
FAR const void *needle, size_t needlelen);
void explicit_bzero(FAR void *s, size_t n);
int timingsafe_bcmp(FAR const void *b1, FAR const void *b2, size_t n);
#undef EXTERN
#if defined(__cplusplus)

View file

@ -29,7 +29,7 @@ CSRCS += lib_strerror.c lib_strncasecmp.c lib_strncat.c lib_strncmp.c
CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.c lib_strrchr.c
CSRCS += lib_strspn.c lib_strstr.c lib_strtok.c lib_strtokr.c
CSRCS += lib_strsep.c lib_strerrorr.c lib_explicit_bzero.c lib_strsignal.c
CSRCS += lib_index.c lib_rindex.c
CSRCS += lib_index.c lib_rindex.c lib_timingsafe_bcmp.c
ifneq ($(CONFIG_LIBC_ARCH_MEMCHR),y)
CSRCS += lib_memchr.c

View file

@ -0,0 +1,43 @@
/****************************************************************************
* libs/libc/string/lib_timingsafe_bcmp.c
* $OpenBSD: timingsafe_bcmp.c,v 1.3 2015/08/31 02:53:57 guenther Exp $
*
* Copyright (c) 2010 Damien Miller. All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <string.h>
/****************************************************************************
* Public Functions
****************************************************************************/
int timingsafe_bcmp(FAR const void *b1, FAR const void *b2, size_t n)
{
FAR const unsigned char *p1 = b1;
FAR const unsigned char *p2 = b2;
int ret = 0;
for (; n > 0; n--)
{
ret |= *p1++ ^ *p2++;
}
return (ret != 0);
}