summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatthew <>2014-06-13 01:55:02 +0000
committermatthew <>2014-06-13 01:55:02 +0000
commit5a1fd238ffb5612e4dd1965f694b7f04ab0d5ecb (patch)
tree49cbc0c1c73c9c75d3f8fe2de5493265c836adae
parent6b58c306f2b5b2dcef66386167df6c8703c397b2 (diff)
downloadopenbsd-5a1fd238ffb5612e4dd1965f694b7f04ab0d5ecb.tar.gz
openbsd-5a1fd238ffb5612e4dd1965f694b7f04ab0d5ecb.tar.bz2
openbsd-5a1fd238ffb5612e4dd1965f694b7f04ab0d5ecb.zip
Add regress tests for timingsafe_bcmp and timingsafe_memcmp.
timingsafe_memcmp tests are disabled for now, pending its addition to libc.
-rw-r--r--src/regress/lib/libc/Makefile4
-rw-r--r--src/regress/lib/libc/timingsafe/Makefile5
-rw-r--r--src/regress/lib/libc/timingsafe/timingsafe.c79
3 files changed, 86 insertions, 2 deletions
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 @@
1# $OpenBSD: Makefile,v 1.40 2014/06/12 22:01:55 matthew Exp $ 1# $OpenBSD: Makefile,v 1.41 2014/06/13 01:55:02 matthew Exp $
2 2
3SUBDIR+= _setjmp alloca atexit basename cephes cxa-atexit db dirname env 3SUBDIR+= _setjmp alloca atexit basename cephes cxa-atexit db dirname env
4SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob 4SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob
@@ -6,7 +6,7 @@ SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream
6SUBDIR+= orientation popen printf 6SUBDIR+= orientation popen printf
7SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf 7SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf
8SUBDIR+= stdio_threading stpncpy strerror strtod strtol strtonum 8SUBDIR+= stdio_threading stpncpy strerror strtod strtol strtonum
9SUBDIR+= telldir time vis 9SUBDIR+= telldir time timingsafe vis
10 10
11.if defined(REGRESS_FULL) 11.if defined(REGRESS_FULL)
12SUBDIR+= getaddrinfo 12SUBDIR+= 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 @@
1# $OpenBSD: Makefile,v 1.1 2014/06/13 01:55:02 matthew Exp $
2
3PROG= timingsafe
4
5.include <bsd.regress.mk>
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 @@
1/* $OpenBSD: timingsafe.c,v 1.1 2014/06/13 01:55:02 matthew Exp $ */
2/*
3 * Copyright (c) 2014 Google Inc.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <assert.h>
19#include <stdlib.h>
20#include <string.h>
21
22#define ASSERT_EQ(a, b) assert((a) == (b))
23
24enum {
25 N = 8
26};
27
28static unsigned char bufone[N], buftwo[N];
29
30void
31check()
32{
33 int cmp = memcmp(bufone, buftwo, N);
34
35 /*
36 * timingsafe_memcmp is specified to return -1, 0, or 1,
37 * but memcmp only specifies <0, 0, or >0.
38 */
39 if (cmp < 0) cmp = -1;
40 if (cmp > 0) cmp = 1;
41
42 /* Check for reflexivity. */
43 ASSERT_EQ(0, timingsafe_bcmp(bufone, bufone, N));
44 ASSERT_EQ(0, timingsafe_bcmp(buftwo, buftwo, N));
45#if notyet
46 ASSERT_EQ(0, timingsafe_memcmp(bufone, bufone, N));
47 ASSERT_EQ(0, timingsafe_memcmp(buftwo, buftwo, N));
48#endif
49
50 /* Check that timingsafe_bcmp returns 0 iff memcmp returns 0. */
51 ASSERT_EQ(cmp == 0, timingsafe_bcmp(bufone, buftwo, N) == 0);
52
53#if notyet
54 /* Check that timingsafe_memcmp returns cmp... */
55 ASSERT_EQ(cmp, timingsafe_memcmp(bufone, buftwo, N));
56
57 /* ... or -cmp if the argument order is swapped. */
58 ASSERT_EQ(-cmp, timingsafe_memcmp(buftwo, bufone, N));
59#endif
60}
61
62int
63main()
64{
65 int i, j;
66
67 for (i = 0; i < 10000; i++) {
68 arc4random_buf(bufone, N);
69 arc4random_buf(buftwo, N);
70
71 check();
72 for (j = 0; j < N; j++) {
73 buftwo[j] = bufone[j];
74 check();
75 }
76 }
77
78 return (0);
79}