This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new d06ed2f8db3 libc/string: Add timingsafe_memcmp().
d06ed2f8db3 is described below
commit d06ed2f8db31689a7599a64264166d1fcd959059
Author: anpeiyun <[email protected]>
AuthorDate: Tue Oct 13 11:33:03 2026 +0800
libc/string: Add timingsafe_memcmp().
timingsafe_bcmp() only tells whether two buffers are equal. Code that
compares MACs, tags, hashes or nonces without revealing where the
buffers first differ may also need to know which one is greater, so
add the ordering form next to timingsafe_bcmp().
The difference of two bytes is tested on the bit just above the value
range of unsigned char, so unlike the OpenBSD original no negative
value is shifted right. The function is also added to libc.csv.
Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
---
include/string.h | 1 +
libs/libc/libc.csv | 1 +
libs/libc/string/CMakeLists.txt | 1 +
libs/libc/string/Make.defs | 1 +
libs/libc/string/lib_timingsafe_memcmp.c | 77 ++++++++++++++++++++++++++++++++
5 files changed, 81 insertions(+)
diff --git a/include/string.h b/include/string.h
index 3509dad1d61..e6997e21547 100644
--- a/include/string.h
+++ b/include/string.h
@@ -111,6 +111,7 @@ FAR void *memchr(FAR const void *s, int c, size_t n);
FAR void *memrchr(FAR const void *s, int c, size_t n);
FAR void *rawmemchr(FAR const void *s, int c);
FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, size_t n);
+int timingsafe_memcmp(FAR const void *, FAR const void *, size_t);
int memcmp(FAR const void *s1, FAR const void *s2, size_t n);
FAR void *memcpy(FAR void *dest, FAR const void *src, size_t n) used_code;
FAR void *mempcpy(FAR void *dest, FAR const void *src, size_t n);
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index 91a4fccca88..59bb3135572 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -405,6 +405,7 @@
"tfind","search.h","","FAR void *","FAR const void *","FAR void * const
*","int(*)(FAR const void *,FAR const void *)"
"thrd_exit","threads.h","!defined(CONFIG_DISABLE_PTHREAD)","noreturn","int"
"time","time.h","","time_t","FAR time_t *"
+"timingsafe_memcmp","string.h","","int","FAR const void *","FAR const void
*","size_t"
"tolower","ctype.h","","int","int"
"toupper","ctype.h","","int","int"
"towlower","wchar.h","","wint_t","wint_t"
diff --git a/libs/libc/string/CMakeLists.txt b/libs/libc/string/CMakeLists.txt
index a9c83e70953..e5dc6f2c4ae 100644
--- a/libs/libc/string/CMakeLists.txt
+++ b/libs/libc/string/CMakeLists.txt
@@ -58,6 +58,7 @@ set(SRCS
lib_index.c
lib_rindex.c
lib_timingsafe_bcmp.c
+ lib_timingsafe_memcmp.c
lib_strverscmp.c
lib_mempcpy.c
lib_rawmemchr.c
diff --git a/libs/libc/string/Make.defs b/libs/libc/string/Make.defs
index 790cc0b33d3..e916f88e88e 100644
--- a/libs/libc/string/Make.defs
+++ b/libs/libc/string/Make.defs
@@ -32,6 +32,7 @@ CSRCS += lib_strerror.c lib_strncasecmp.c lib_strncat.c
CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.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_timingsafe_memcmp.c
CSRCS += lib_index.c lib_rindex.c lib_timingsafe_bcmp.c lib_strverscmp.c
CSRCS += lib_mempcpy.c lib_rawmemchr.c
diff --git a/libs/libc/string/lib_timingsafe_memcmp.c
b/libs/libc/string/lib_timingsafe_memcmp.c
new file mode 100644
index 00000000000..3fee04d3a0e
--- /dev/null
+++ b/libs/libc/string/lib_timingsafe_memcmp.c
@@ -0,0 +1,77 @@
+/****************************************************************************
+ * libs/libc/string/lib_timingsafe_memcmp.c
+ *
+ * SPDX-License-Identifier: ISC
+ * SPDX-FileCopyrightText: 2019 Linaro Limited
+ * SPDX-FileCopyrightText: 2004-2007, 2011, 2012 Internet Systems Consortium
+ * SPDX-FileCopyrightText: 1999-2001, 2003 Internet Software Consortium
+ * SPDX-FileCopyrightText: 2015 J. Perlinger
+ *
+ * Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 <limits.h>
+#include <string.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: timingsafe_memcmp
+ *
+ * Description:
+ * Compare two memory areas of n bytes without leaking the offset of the
+ * first differing byte: no early-out and no data dependent branch, one
+ * pass over all n bytes. It is the ordering counterpart of
+ * timingsafe_bcmp(), which only tells equal from not equal.
+ *
+ * The result comes from the first difference: mask is cleared as soon as
+ * one is found, so the bytes that follow cannot change it. mask is
+ * volatile so that the compiler cannot skip those bytes either.
+ *
+ * Input Parameters:
+ * b1 - First memory area
+ * b2 - Second memory area
+ * n - Number of bytes to compare
+ *
+ * Returned Value:
+ * A negative value if b1 is less than b2, zero if they are equal and a
+ * positive value otherwise.
+ *
+ ****************************************************************************/
+
+int timingsafe_memcmp(FAR const void *b1, FAR const void *b2, size_t n)
+{
+ FAR const unsigned char *p1 = b1;
+ FAR const unsigned char *p2 = b2;
+ volatile int mask = 1u << CHAR_BIT;
+ int is_lt = 0;
+ int is_gt = 0;
+
+ for (; n > 0; n--)
+ {
+ is_lt |= mask & (*p1 - *p2);
+ is_gt |= mask & (*p2 - *p1);
+ mask &= ~(is_lt | is_gt);
+ p1++;
+ p2++;
+ }
+
+ return (is_gt >> CHAR_BIT) - (is_lt >> CHAR_BIT);
+}