diff options
Diffstat (limited to 'nacl/crypto_verify/try.c')
-rw-r--r-- | nacl/crypto_verify/try.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/nacl/crypto_verify/try.c b/nacl/crypto_verify/try.c new file mode 100644 index 00000000..f555cb4e --- /dev/null +++ b/nacl/crypto_verify/try.c | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * crypto_verify/try.c version 20090118 | ||
3 | * D. J. Bernstein | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <stdlib.h> | ||
8 | #include "crypto_verify.h" | ||
9 | |||
10 | extern unsigned char *alignedcalloc(unsigned long long); | ||
11 | |||
12 | const char *primitiveimplementation = crypto_verify_IMPLEMENTATION; | ||
13 | |||
14 | static unsigned char *x; | ||
15 | static unsigned char *y; | ||
16 | |||
17 | void preallocate(void) | ||
18 | { | ||
19 | } | ||
20 | |||
21 | void allocate(void) | ||
22 | { | ||
23 | x = alignedcalloc(crypto_verify_BYTES); | ||
24 | y = alignedcalloc(crypto_verify_BYTES); | ||
25 | } | ||
26 | |||
27 | void predoit(void) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | void doit(void) | ||
32 | { | ||
33 | crypto_verify(x,y); | ||
34 | } | ||
35 | |||
36 | static const char *check(void) | ||
37 | { | ||
38 | int r = crypto_verify(x,y); | ||
39 | if (r == 0) { | ||
40 | if (memcmp(x,y,crypto_verify_BYTES)) return "different strings pass verify"; | ||
41 | } else if (r == -1) { | ||
42 | if (!memcmp(x,y,crypto_verify_BYTES)) return "equal strings fail verify"; | ||
43 | } else { | ||
44 | return "weird return value from verify"; | ||
45 | } | ||
46 | return 0; | ||
47 | } | ||
48 | |||
49 | char checksum[2]; | ||
50 | |||
51 | const char *checksum_compute(void) | ||
52 | { | ||
53 | long long tests; | ||
54 | long long i; | ||
55 | long long j; | ||
56 | const char *c; | ||
57 | |||
58 | for (tests = 0;tests < 100000;++tests) { | ||
59 | for (i = 0;i < crypto_verify_BYTES;++i) x[i] = random(); | ||
60 | for (i = 0;i < crypto_verify_BYTES;++i) y[i] = random(); | ||
61 | c = check(); if (c) return c; | ||
62 | for (i = 0;i < crypto_verify_BYTES;++i) y[i] = x[i]; | ||
63 | c = check(); if (c) return c; | ||
64 | y[random() % crypto_verify_BYTES] = random(); | ||
65 | c = check(); if (c) return c; | ||
66 | y[random() % crypto_verify_BYTES] = random(); | ||
67 | c = check(); if (c) return c; | ||
68 | y[random() % crypto_verify_BYTES] = random(); | ||
69 | c = check(); if (c) return c; | ||
70 | } | ||
71 | |||
72 | checksum[0] = '0'; | ||
73 | checksum[1] = 0; | ||
74 | return 0; | ||
75 | } | ||