diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2016-03-18 13:49:26 +0300 |
---|---|---|
committer | Corinna Vinschen <corinna@vinschen.de> | 2016-03-18 14:33:40 +0300 |
commit | 559fd77ddac907da598202c23f103fc940f15ff8 (patch) | |
tree | 502f0f2236526840efda91ce2038da5d381966af /newlib | |
parent | 8740fa7fd0f1eba18059b2794c9cdfe29ef564d3 (diff) |
Add timingsafe_bcmp()
This function is used by LibreSSL and OpenSSH and is provided by the
OpenBSD libc.
* libc/include/string.h (timingsafe_bcmp): Declare.
* libc/string/timingsafe_bcmp.c: New file.
* libc/string/Makefile.am: Add new file.
* libc/string/Makefile.in: Regenerate.
Diffstat (limited to 'newlib')
-rw-r--r-- | newlib/libc/include/string.h | 1 | ||||
-rw-r--r-- | newlib/libc/string/Makefile.am | 1 | ||||
-rw-r--r-- | newlib/libc/string/Makefile.in | 49 | ||||
-rw-r--r-- | newlib/libc/string/timingsafe_bcmp.c | 29 |
4 files changed, 59 insertions, 21 deletions
diff --git a/newlib/libc/include/string.h b/newlib/libc/include/string.h index c43106b33..2bda8f952 100644 --- a/newlib/libc/include/string.h +++ b/newlib/libc/include/string.h @@ -53,6 +53,7 @@ void _EXFUN(bzero,(void *, size_t)); #endif #if __BSD_VISIBLE void _EXFUN(explicit_bzero,(void *, size_t)); +int _EXFUN(timingsafe_bcmp,(const void *, const void *, size_t)); #endif #if __MISC_VISIBLE || __POSIX_VISIBLE >= 200809 int _EXFUN(ffs,(int)); diff --git a/newlib/libc/string/Makefile.am b/newlib/libc/string/Makefile.am index 9b8e6b183..c227bbda9 100644 --- a/newlib/libc/string/Makefile.am +++ b/newlib/libc/string/Makefile.am @@ -46,6 +46,7 @@ GENERAL_SOURCES = \ strxfrm.c \ strstr.c \ swab.c \ + timingsafe_bcmp.c \ u_strerr.c \ wcscat.c \ wcschr.c \ diff --git a/newlib/libc/string/Makefile.in b/newlib/libc/string/Makefile.in index 910d30b8d..1db7f0e62 100644 --- a/newlib/libc/string/Makefile.in +++ b/newlib/libc/string/Makefile.in @@ -92,21 +92,21 @@ am__objects_1 = lib_a-bcopy.$(OBJEXT) lib_a-bzero.$(OBJEXT) \ lib_a-strspn.$(OBJEXT) lib_a-strtok.$(OBJEXT) \ lib_a-strtok_r.$(OBJEXT) lib_a-strupr.$(OBJEXT) \ lib_a-strxfrm.$(OBJEXT) lib_a-strstr.$(OBJEXT) \ - lib_a-swab.$(OBJEXT) lib_a-u_strerr.$(OBJEXT) \ - lib_a-wcscat.$(OBJEXT) lib_a-wcschr.$(OBJEXT) \ - lib_a-wcscmp.$(OBJEXT) lib_a-wcscoll.$(OBJEXT) \ - lib_a-wcscpy.$(OBJEXT) lib_a-wcscspn.$(OBJEXT) \ - lib_a-wcslcat.$(OBJEXT) lib_a-wcslcpy.$(OBJEXT) \ - lib_a-wcslen.$(OBJEXT) lib_a-wcsncat.$(OBJEXT) \ - lib_a-wcsncmp.$(OBJEXT) lib_a-wcsncpy.$(OBJEXT) \ - lib_a-wcsnlen.$(OBJEXT) lib_a-wcspbrk.$(OBJEXT) \ - lib_a-wcsrchr.$(OBJEXT) lib_a-wcsspn.$(OBJEXT) \ - lib_a-wcsstr.$(OBJEXT) lib_a-wcstok.$(OBJEXT) \ - lib_a-wcswidth.$(OBJEXT) lib_a-wcsxfrm.$(OBJEXT) \ - lib_a-wcwidth.$(OBJEXT) lib_a-wmemchr.$(OBJEXT) \ - lib_a-wmemcmp.$(OBJEXT) lib_a-wmemcpy.$(OBJEXT) \ - lib_a-wmemmove.$(OBJEXT) lib_a-wmemset.$(OBJEXT) \ - lib_a-xpg_strerror_r.$(OBJEXT) + lib_a-swab.$(OBJEXT) lib_a-timingsafe_bcmp.$(OBJEXT) \ + lib_a-u_strerr.$(OBJEXT) lib_a-wcscat.$(OBJEXT) \ + lib_a-wcschr.$(OBJEXT) lib_a-wcscmp.$(OBJEXT) \ + lib_a-wcscoll.$(OBJEXT) lib_a-wcscpy.$(OBJEXT) \ + lib_a-wcscspn.$(OBJEXT) lib_a-wcslcat.$(OBJEXT) \ + lib_a-wcslcpy.$(OBJEXT) lib_a-wcslen.$(OBJEXT) \ + lib_a-wcsncat.$(OBJEXT) lib_a-wcsncmp.$(OBJEXT) \ + lib_a-wcsncpy.$(OBJEXT) lib_a-wcsnlen.$(OBJEXT) \ + lib_a-wcspbrk.$(OBJEXT) lib_a-wcsrchr.$(OBJEXT) \ + lib_a-wcsspn.$(OBJEXT) lib_a-wcsstr.$(OBJEXT) \ + lib_a-wcstok.$(OBJEXT) lib_a-wcswidth.$(OBJEXT) \ + lib_a-wcsxfrm.$(OBJEXT) lib_a-wcwidth.$(OBJEXT) \ + lib_a-wmemchr.$(OBJEXT) lib_a-wmemcmp.$(OBJEXT) \ + lib_a-wmemcpy.$(OBJEXT) lib_a-wmemmove.$(OBJEXT) \ + lib_a-wmemset.$(OBJEXT) lib_a-xpg_strerror_r.$(OBJEXT) @ELIX_LEVEL_1_FALSE@am__objects_2 = lib_a-bcmp.$(OBJEXT) \ @ELIX_LEVEL_1_FALSE@ lib_a-memccpy.$(OBJEXT) \ @ELIX_LEVEL_1_FALSE@ lib_a-mempcpy.$(OBJEXT) \ @@ -138,12 +138,12 @@ am__objects_4 = bcopy.lo bzero.lo explicit_bzero.lo index.lo memchr.lo \ strncasecmp.lo strncat.lo strncmp.lo strncpy.lo strnlen.lo \ strpbrk.lo strrchr.lo strsep.lo strsignal.lo strspn.lo \ strtok.lo strtok_r.lo strupr.lo strxfrm.lo strstr.lo swab.lo \ - u_strerr.lo wcscat.lo wcschr.lo wcscmp.lo wcscoll.lo wcscpy.lo \ - wcscspn.lo wcslcat.lo wcslcpy.lo wcslen.lo wcsncat.lo \ - wcsncmp.lo wcsncpy.lo wcsnlen.lo wcspbrk.lo wcsrchr.lo \ - wcsspn.lo wcsstr.lo wcstok.lo wcswidth.lo wcsxfrm.lo \ - wcwidth.lo wmemchr.lo wmemcmp.lo wmemcpy.lo wmemmove.lo \ - wmemset.lo xpg_strerror_r.lo + timingsafe_bcmp.lo u_strerr.lo wcscat.lo wcschr.lo wcscmp.lo \ + wcscoll.lo wcscpy.lo wcscspn.lo wcslcat.lo wcslcpy.lo \ + wcslen.lo wcsncat.lo wcsncmp.lo wcsncpy.lo wcsnlen.lo \ + wcspbrk.lo wcsrchr.lo wcsspn.lo wcsstr.lo wcstok.lo \ + wcswidth.lo wcsxfrm.lo wcwidth.lo wmemchr.lo wmemcmp.lo \ + wmemcpy.lo wmemmove.lo wmemset.lo xpg_strerror_r.lo @ELIX_LEVEL_1_FALSE@am__objects_5 = bcmp.lo memccpy.lo mempcpy.lo \ @ELIX_LEVEL_1_FALSE@ stpcpy.lo stpncpy.lo strndup.lo \ @ELIX_LEVEL_1_FALSE@ strcasestr.lo strchrnul.lo strndup_r.lo \ @@ -370,6 +370,7 @@ GENERAL_SOURCES = \ strxfrm.c \ strstr.c \ swab.c \ + timingsafe_bcmp.c \ u_strerr.c \ wcscat.c \ wcschr.c \ @@ -774,6 +775,12 @@ lib_a-swab.o: swab.c lib_a-swab.obj: swab.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-swab.obj `if test -f 'swab.c'; then $(CYGPATH_W) 'swab.c'; else $(CYGPATH_W) '$(srcdir)/swab.c'; fi` +lib_a-timingsafe_bcmp.o: timingsafe_bcmp.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-timingsafe_bcmp.o `test -f 'timingsafe_bcmp.c' || echo '$(srcdir)/'`timingsafe_bcmp.c + +lib_a-timingsafe_bcmp.obj: timingsafe_bcmp.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-timingsafe_bcmp.obj `if test -f 'timingsafe_bcmp.c'; then $(CYGPATH_W) 'timingsafe_bcmp.c'; else $(CYGPATH_W) '$(srcdir)/timingsafe_bcmp.c'; fi` + lib_a-u_strerr.o: u_strerr.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-u_strerr.o `test -f 'u_strerr.c' || echo '$(srcdir)/'`u_strerr.c diff --git a/newlib/libc/string/timingsafe_bcmp.c b/newlib/libc/string/timingsafe_bcmp.c new file mode 100644 index 000000000..0b736154c --- /dev/null +++ b/newlib/libc/string/timingsafe_bcmp.c @@ -0,0 +1,29 @@ +/* $OpenBSD: timingsafe_bcmp.c,v 1.2 2014/06/10 04:17:37 deraadt 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. + */ + +#include <string.h> + +int +timingsafe_bcmp(const void *b1, const void *b2, size_t n) +{ + const unsigned char *p1 = b1, *p2 = b2; + int ret = 0; + + for (; n > 0; n--) + ret |= *p1++ ^ *p2++; + return (ret != 0); +} |