This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push: new 1a5351edac libc:add timingsafe_bcmp to libc 1a5351edac is described below commit 1a5351edaccc14e75c317481e14d843990db342d Author: anjiahao <anjia...@xiaomi.com> AuthorDate: Sat Aug 27 12:22:29 2022 +0800 libc:add timingsafe_bcmp to libc Signed-off-by: anjiahao <anjia...@xiaomi.com> --- include/string.h | 1 + libs/libc/string/Make.defs | 2 +- libs/libc/string/lib_timingsafe_bcmp.c | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/include/string.h b/include/string.h index 8d82b6b7a0..7e449fe6ed 100644 --- a/include/string.h +++ b/include/string.h @@ -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) diff --git a/libs/libc/string/Make.defs b/libs/libc/string/Make.defs index 6284d1fe74..1208246e97 100644 --- a/libs/libc/string/Make.defs +++ b/libs/libc/string/Make.defs @@ -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 diff --git a/libs/libc/string/lib_timingsafe_bcmp.c b/libs/libc/string/lib_timingsafe_bcmp.c new file mode 100644 index 0000000000..8473b61249 --- /dev/null +++ b/libs/libc/string/lib_timingsafe_bcmp.c @@ -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); +}