From 5a1fd238ffb5612e4dd1965f694b7f04ab0d5ecb Mon Sep 17 00:00:00 2001 From: matthew <> Date: Fri, 13 Jun 2014 01:55:02 +0000 Subject: Add regress tests for timingsafe_bcmp and timingsafe_memcmp. timingsafe_memcmp tests are disabled for now, pending its addition to libc. --- src/regress/lib/libc/Makefile | 4 +- src/regress/lib/libc/timingsafe/Makefile | 5 ++ src/regress/lib/libc/timingsafe/timingsafe.c | 79 ++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/regress/lib/libc/timingsafe/Makefile create mode 100644 src/regress/lib/libc/timingsafe/timingsafe.c diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 71eb6398a8..e7a96f1ece 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/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/src/regress/lib/libc/timingsafe/Makefile b/src/regress/lib/libc/timingsafe/Makefile new file mode 100644 index 0000000000..9a679b61f8 --- /dev/null +++ b/src/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 diff --git a/src/regress/lib/libc/timingsafe/timingsafe.c b/src/regress/lib/libc/timingsafe/timingsafe.c new file mode 100644 index 0000000000..9ecac93c09 --- /dev/null +++ b/src/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 +#include +#include + +#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); +} -- cgit v1.2.3-55-g6feb