diff options
author | 2014-06-13 01:55:02 +0000 | |
---|---|---|
committer | 2014-06-13 01:55:02 +0000 | |
commit | dbb5ab15d432a0884c0a5ed14043ab1ecaf7dd25 (patch) | |
tree | a504824bda38713ddd16b6b67d501c8752c51448 /regress/lib/libc | |
parent | Add support for MAP_INHERIT_ZERO. (diff) | |
download | wireguard-openbsd-dbb5ab15d432a0884c0a5ed14043ab1ecaf7dd25.tar.xz wireguard-openbsd-dbb5ab15d432a0884c0a5ed14043ab1ecaf7dd25.zip |
Add regress tests for timingsafe_bcmp and timingsafe_memcmp.
timingsafe_memcmp tests are disabled for now, pending its addition to
libc.
Diffstat (limited to 'regress/lib/libc')
-rw-r--r-- | regress/lib/libc/Makefile | 4 | ||||
-rw-r--r-- | regress/lib/libc/timingsafe/Makefile | 5 | ||||
-rw-r--r-- | regress/lib/libc/timingsafe/timingsafe.c | 79 |
3 files changed, 86 insertions, 2 deletions
diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile index 71eb6398a81..e7a96f1eceb 100644 --- a/regress/lib/libc/Makefile +++ b/regress/lib/libc/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.40 2014/06/12 22:01:55 matthew Exp $ +# $OpenBSD: Makefile,v 1.41 2014/06/13 01:55:02 matthew Exp $ SUBDIR+= _setjmp alloca atexit basename cephes cxa-atexit db dirname env SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob @@ -6,7 +6,7 @@ SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream SUBDIR+= orientation popen printf SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf SUBDIR+= stdio_threading stpncpy strerror strtod strtol strtonum -SUBDIR+= telldir time vis +SUBDIR+= telldir time timingsafe vis .if defined(REGRESS_FULL) SUBDIR+= getaddrinfo diff --git a/regress/lib/libc/timingsafe/Makefile b/regress/lib/libc/timingsafe/Makefile new file mode 100644 index 00000000000..9a679b61f84 --- /dev/null +++ b/regress/lib/libc/timingsafe/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2014/06/13 01:55:02 matthew Exp $ + +PROG= timingsafe + +.include <bsd.regress.mk> diff --git a/regress/lib/libc/timingsafe/timingsafe.c b/regress/lib/libc/timingsafe/timingsafe.c new file mode 100644 index 00000000000..9ecac93c097 --- /dev/null +++ b/regress/lib/libc/timingsafe/timingsafe.c @@ -0,0 +1,79 @@ +/* $OpenBSD: timingsafe.c,v 1.1 2014/06/13 01:55:02 matthew 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 <assert.h> +#include <stdlib.h> +#include <string.h> + +#define ASSERT_EQ(a, b) assert((a) == (b)) + +enum { + N = 8 +}; + +static unsigned char bufone[N], buftwo[N]; + +void +check() +{ + int cmp = memcmp(bufone, buftwo, N); + + /* + * timingsafe_memcmp is specified to return -1, 0, or 1, + * but memcmp only specifies <0, 0, or >0. + */ + if (cmp < 0) cmp = -1; + if (cmp > 0) cmp = 1; + + /* Check for reflexivity. */ + ASSERT_EQ(0, timingsafe_bcmp(bufone, bufone, N)); + ASSERT_EQ(0, timingsafe_bcmp(buftwo, buftwo, N)); +#if notyet + ASSERT_EQ(0, timingsafe_memcmp(bufone, bufone, N)); + ASSERT_EQ(0, timingsafe_memcmp(buftwo, buftwo, N)); +#endif + + /* Check that timingsafe_bcmp returns 0 iff memcmp returns 0. */ + ASSERT_EQ(cmp == 0, timingsafe_bcmp(bufone, buftwo, N) == 0); + +#if notyet + /* Check that timingsafe_memcmp returns cmp... */ + ASSERT_EQ(cmp, timingsafe_memcmp(bufone, buftwo, N)); + + /* ... or -cmp if the argument order is swapped. */ + ASSERT_EQ(-cmp, timingsafe_memcmp(buftwo, bufone, N)); +#endif +} + +int +main() +{ + int i, j; + + for (i = 0; i < 10000; i++) { + arc4random_buf(bufone, N); + arc4random_buf(buftwo, N); + + check(); + for (j = 0; j < N; j++) { + buftwo[j] = bufone[j]; + check(); + } + } + + return (0); +} |