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.
This commit is contained in:
Sebastian Huber 2016-03-18 11:49:26 +01:00 committed by Corinna Vinschen
parent 8740fa7fd0
commit 559fd77dda
4 changed files with 59 additions and 21 deletions

View File

@ -53,6 +53,7 @@ void _EXFUN(bzero,(void *, size_t));
#endif #endif
#if __BSD_VISIBLE #if __BSD_VISIBLE
void _EXFUN(explicit_bzero,(void *, size_t)); void _EXFUN(explicit_bzero,(void *, size_t));
int _EXFUN(timingsafe_bcmp,(const void *, const void *, size_t));
#endif #endif
#if __MISC_VISIBLE || __POSIX_VISIBLE >= 200809 #if __MISC_VISIBLE || __POSIX_VISIBLE >= 200809
int _EXFUN(ffs,(int)); int _EXFUN(ffs,(int));

View File

@ -46,6 +46,7 @@ GENERAL_SOURCES = \
strxfrm.c \ strxfrm.c \
strstr.c \ strstr.c \
swab.c \ swab.c \
timingsafe_bcmp.c \
u_strerr.c \ u_strerr.c \
wcscat.c \ wcscat.c \
wcschr.c \ wcschr.c \

View File

@ -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-strspn.$(OBJEXT) lib_a-strtok.$(OBJEXT) \
lib_a-strtok_r.$(OBJEXT) lib_a-strupr.$(OBJEXT) \ lib_a-strtok_r.$(OBJEXT) lib_a-strupr.$(OBJEXT) \
lib_a-strxfrm.$(OBJEXT) lib_a-strstr.$(OBJEXT) \ lib_a-strxfrm.$(OBJEXT) lib_a-strstr.$(OBJEXT) \
lib_a-swab.$(OBJEXT) lib_a-u_strerr.$(OBJEXT) \ lib_a-swab.$(OBJEXT) lib_a-timingsafe_bcmp.$(OBJEXT) \
lib_a-wcscat.$(OBJEXT) lib_a-wcschr.$(OBJEXT) \ lib_a-u_strerr.$(OBJEXT) lib_a-wcscat.$(OBJEXT) \
lib_a-wcscmp.$(OBJEXT) lib_a-wcscoll.$(OBJEXT) \ lib_a-wcschr.$(OBJEXT) lib_a-wcscmp.$(OBJEXT) \
lib_a-wcscpy.$(OBJEXT) lib_a-wcscspn.$(OBJEXT) \ lib_a-wcscoll.$(OBJEXT) lib_a-wcscpy.$(OBJEXT) \
lib_a-wcslcat.$(OBJEXT) lib_a-wcslcpy.$(OBJEXT) \ lib_a-wcscspn.$(OBJEXT) lib_a-wcslcat.$(OBJEXT) \
lib_a-wcslen.$(OBJEXT) lib_a-wcsncat.$(OBJEXT) \ lib_a-wcslcpy.$(OBJEXT) lib_a-wcslen.$(OBJEXT) \
lib_a-wcsncmp.$(OBJEXT) lib_a-wcsncpy.$(OBJEXT) \ lib_a-wcsncat.$(OBJEXT) lib_a-wcsncmp.$(OBJEXT) \
lib_a-wcsnlen.$(OBJEXT) lib_a-wcspbrk.$(OBJEXT) \ lib_a-wcsncpy.$(OBJEXT) lib_a-wcsnlen.$(OBJEXT) \
lib_a-wcsrchr.$(OBJEXT) lib_a-wcsspn.$(OBJEXT) \ lib_a-wcspbrk.$(OBJEXT) lib_a-wcsrchr.$(OBJEXT) \
lib_a-wcsstr.$(OBJEXT) lib_a-wcstok.$(OBJEXT) \ lib_a-wcsspn.$(OBJEXT) lib_a-wcsstr.$(OBJEXT) \
lib_a-wcswidth.$(OBJEXT) lib_a-wcsxfrm.$(OBJEXT) \ lib_a-wcstok.$(OBJEXT) lib_a-wcswidth.$(OBJEXT) \
lib_a-wcwidth.$(OBJEXT) lib_a-wmemchr.$(OBJEXT) \ lib_a-wcsxfrm.$(OBJEXT) lib_a-wcwidth.$(OBJEXT) \
lib_a-wmemcmp.$(OBJEXT) lib_a-wmemcpy.$(OBJEXT) \ lib_a-wmemchr.$(OBJEXT) lib_a-wmemcmp.$(OBJEXT) \
lib_a-wmemmove.$(OBJEXT) lib_a-wmemset.$(OBJEXT) \ lib_a-wmemcpy.$(OBJEXT) lib_a-wmemmove.$(OBJEXT) \
lib_a-xpg_strerror_r.$(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@am__objects_2 = lib_a-bcmp.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-memccpy.$(OBJEXT) \ @ELIX_LEVEL_1_FALSE@ lib_a-memccpy.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-mempcpy.$(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 \ strncasecmp.lo strncat.lo strncmp.lo strncpy.lo strnlen.lo \
strpbrk.lo strrchr.lo strsep.lo strsignal.lo strspn.lo \ strpbrk.lo strrchr.lo strsep.lo strsignal.lo strspn.lo \
strtok.lo strtok_r.lo strupr.lo strxfrm.lo strstr.lo swab.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 \ timingsafe_bcmp.lo u_strerr.lo wcscat.lo wcschr.lo wcscmp.lo \
wcscspn.lo wcslcat.lo wcslcpy.lo wcslen.lo wcsncat.lo \ wcscoll.lo wcscpy.lo wcscspn.lo wcslcat.lo wcslcpy.lo \
wcsncmp.lo wcsncpy.lo wcsnlen.lo wcspbrk.lo wcsrchr.lo \ wcslen.lo wcsncat.lo wcsncmp.lo wcsncpy.lo wcsnlen.lo \
wcsspn.lo wcsstr.lo wcstok.lo wcswidth.lo wcsxfrm.lo \ wcspbrk.lo wcsrchr.lo wcsspn.lo wcsstr.lo wcstok.lo \
wcwidth.lo wmemchr.lo wmemcmp.lo wmemcpy.lo wmemmove.lo \ wcswidth.lo wcsxfrm.lo wcwidth.lo wmemchr.lo wmemcmp.lo \
wmemset.lo xpg_strerror_r.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@am__objects_5 = bcmp.lo memccpy.lo mempcpy.lo \
@ELIX_LEVEL_1_FALSE@ stpcpy.lo stpncpy.lo strndup.lo \ @ELIX_LEVEL_1_FALSE@ stpcpy.lo stpncpy.lo strndup.lo \
@ELIX_LEVEL_1_FALSE@ strcasestr.lo strchrnul.lo strndup_r.lo \ @ELIX_LEVEL_1_FALSE@ strcasestr.lo strchrnul.lo strndup_r.lo \
@ -370,6 +370,7 @@ GENERAL_SOURCES = \
strxfrm.c \ strxfrm.c \
strstr.c \ strstr.c \
swab.c \ swab.c \
timingsafe_bcmp.c \
u_strerr.c \ u_strerr.c \
wcscat.c \ wcscat.c \
wcschr.c \ wcschr.c \
@ -774,6 +775,12 @@ lib_a-swab.o: swab.c
lib_a-swab.obj: 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` $(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 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 $(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

View File

@ -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);
}