Add timingsafe_bcmp and timingsafe_memcmp.

Obtained from:	OpenBSD
Reviewed by:	trasz
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D7280
This commit is contained in:
delphij 2016-08-14 23:38:50 +00:00
parent c2131431d1
commit 408f4a1ab4
8 changed files with 201 additions and 5 deletions

View File

@ -134,6 +134,8 @@ typedef __ssize_t ssize_t;
void swab(const void * __restrict, void * __restrict, ssize_t);
#endif /* _SWAB_DECLARED */
int timingsafe_bcmp(const void *, const void *, size_t);
int timingsafe_memcmp(const void *, const void *, size_t);
#endif /* __BSD_VISIBLE */
#if __POSIX_VISIBLE >= 200809 || defined(_XLOCALE_H_)

View File

@ -16,7 +16,10 @@ MISRCS+=bcmp.c bcopy.c bzero.c explicit_bzero.c \
strcspn.c strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c \
strncat.c strncmp.c strncpy.c strndup.c strnlen.c strnstr.c \
strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \
strxfrm.c swab.c wcpcpy.c wcpncpy.c wcscasecmp.c wcscat.c \
strxfrm.c swab.c \
timingsafe_bcmp.c \
timingsafe_memcmp.c \
wcpcpy.c wcpncpy.c wcscasecmp.c wcscat.c \
wcschr.c wcscmp.c wcscoll.c wcscpy.c wcscspn.c wcsdup.c \
wcslcat.c wcslcpy.c wcslen.c wcsncasecmp.c wcsncat.c wcsncmp.c \
wcsncpy.c wcsnlen.c wcspbrk.c \
@ -34,7 +37,9 @@ MAN+= bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \
memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \
strchr.3 strcmp.3 strcoll.3 strcpy.3 strdup.3 strerror.3 \
string.3 strlcpy.3 strlen.3 strmode.3 strpbrk.3 strsep.3 \
strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 wcscoll.3 wcstok.3 \
strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 \
timingsafe_bcmp.3 \
wcscoll.3 wcstok.3 \
wcswidth.3 wcsxfrm.3 wmemchr.3
MLINKS+=bzero.3 explicit_bzero.3
@ -69,6 +74,7 @@ MLINKS+=strstr.3 strcasestr.3 \
strstr.3 strcasestr_l.3
MLINKS+=strtok.3 strtok_r.3
MLINKS+=strxfrm.3 strxfrm_l.3
MLINKS+=timingsafe_bcmp.3 timingsafe_memcmp.3
MLINKS+=wmemchr.3 wcpcpy.3 \
wmemchr.3 wcpncpy.3 \
wmemchr.3 wcscasecmp.3 \

View File

@ -104,6 +104,11 @@ FBSD_1.4 {
explicit_bzero;
};
FBSD_1.5 {
timingsafe_bcmp;
timingsafe_memcmp;
};
FBSDprivate_1.0 {
__strtok_r;
};

View File

@ -30,7 +30,7 @@
.\" @(#)bcmp.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd June 4, 1993
.Dd August 15, 2016
.Dt BCMP 3
.Os
.Sh NAME
@ -62,7 +62,8 @@ The strings may overlap.
.Xr strcasecmp 3 ,
.Xr strcmp 3 ,
.Xr strcoll 3 ,
.Xr strxfrm 3
.Xr strxfrm 3 ,
.Xr timingsafe_bcmp 3
.Sh HISTORY
A
.Fn bcmp

View File

@ -32,7 +32,7 @@
.\" @(#)memcmp.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd June 4, 1993
.Dd August 15, 2016
.Dt MEMCMP 3
.Os
.Sh NAME
@ -75,6 +75,7 @@ Zero-length strings are always identical.
.Xr strcmp 3 ,
.Xr strcoll 3 ,
.Xr strxfrm 3 ,
.Xr timingsafe_memcmp 3 ,
.Xr wmemcmp 3
.Sh STANDARDS
The

View File

@ -0,0 +1,92 @@
.\" $OpenBSD: timingsafe_bcmp.3,v 1.2 2014/06/21 20:22:15 tedu Exp $
.\"
.\" Copyright (c) 2014 Google Inc.
.\"
.\" 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.
.\"
.\" $FreeBSD$
.Dd August 15, 2016
.Dt TIMINGSAFE_BCMP 3
.Os
.Sh NAME
.Nm timingsafe_bcmp ,
.Nm timingsafe_memcmp
.Nd timing-safe byte sequence comparisons
.Sh SYNOPSIS
.In string.h
.Ft int
.Fn timingsafe_bcmp "const void *b1" "const void *b2" "size_t len"
.Ft int
.Fn timingsafe_memcmp "const void *b1" "const void *b2" "size_t len"
.Sh DESCRIPTION
The
.Fn timingsafe_bcmp
and
.Fn timingsafe_memcmp
functions lexicographically compare the first
.Fa len
bytes (each interpreted as an
.Vt unsigned char )
pointed to by
.Fa b1
and
.Fa b2 .
.Pp
Additionally, their running times are independent of the byte sequences compared,
making them safe to use for comparing secret values such as cryptographic MACs.
In contrast,
.Xr bcmp 3
and
.Xr memcmp 3
may short-circuit after finding the first differing byte.
.Sh RETURN VALUES
The
.Fn timingsafe_bcmp
function returns 0 or not zero if the byte sequence pointed to by
.Fa b1
compares equal to or not equal to (respectively)
the byte sequence pointed to by
.Fa b2 .
.Pp
The
.Fn timingsafe_memcmp
function returns a negative value, 0, or positive value if the byte sequence
pointed to by
.Fa b1
compares less than, equal to, or greater than (respectively)
the byte sequence pointed to by
.Fa b2 .
.Sh SEE ALSO
.Xr bcmp 3 ,
.Xr memcmp 3
.Sh STANDARDS
The
.Fn timingsafe_bcmp
and
.Fn timingsafe_memcmp
functions are
.Fx
extensions.
.Sh HISTORY
The
.Fn timingsafe_bcmp
function first appeared in
.Ox 4.9 .
.Pp
The
.Fn timingsafe_memcmp
function first appeared in
.Ox 5.6 .
.Pp
Both functions first appeared in
.Fx 12.0 .

View File

@ -0,0 +1,36 @@
/* $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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <string.h>
int __timingsafe_bcmp(const void *, const void *, size_t);
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);
}
__weak_reference(__timingsafe_bcmp, timingsafe_bcmp);

View File

@ -0,0 +1,53 @@
/* $OpenBSD: timingsafe_memcmp.c,v 1.2 2015/08/31 02:53:57 guenther Exp $ */
/*
* Copyright (c) 2014 Google Inc.
*
* 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <limits.h>
#include <string.h>
int __timingsafe_memcmp(const void *, const void *, size_t);
int
__timingsafe_memcmp(const void *b1, const void *b2, size_t len)
{
const unsigned char *p1 = b1, *p2 = b2;
size_t i;
int res = 0, done = 0;
for (i = 0; i < len; i++) {
/* lt is -1 if p1[i] < p2[i]; else 0. */
int lt = (p1[i] - p2[i]) >> CHAR_BIT;
/* gt is -1 if p1[i] > p2[i]; else 0. */
int gt = (p2[i] - p1[i]) >> CHAR_BIT;
/* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */
int cmp = lt - gt;
/* set res = cmp if !done. */
res |= cmp & ~done;
/* set done if p1[i] != p2[i]. */
done |= lt | gt;
}
return (res);
}
__weak_reference(__timingsafe_memcmp, timingsafe_memcmp);