diff options
Diffstat (limited to 'nacl/crypto_secretbox/try.c')
-rw-r--r-- | nacl/crypto_secretbox/try.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/nacl/crypto_secretbox/try.c b/nacl/crypto_secretbox/try.c new file mode 100644 index 00000000..eda091e9 --- /dev/null +++ b/nacl/crypto_secretbox/try.c | |||
@@ -0,0 +1,129 @@ | |||
1 | /* | ||
2 | * crypto_secretbox/try.c version 20090118 | ||
3 | * D. J. Bernstein | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include "crypto_secretbox.h" | ||
8 | |||
9 | extern unsigned char *alignedcalloc(unsigned long long); | ||
10 | |||
11 | const char *primitiveimplementation = crypto_secretbox_IMPLEMENTATION; | ||
12 | |||
13 | #define MAXTEST_BYTES 10000 | ||
14 | #define CHECKSUM_BYTES 4096 | ||
15 | #define TUNE_BYTES 1536 | ||
16 | |||
17 | static unsigned char *k; | ||
18 | static unsigned char *n; | ||
19 | static unsigned char *m; | ||
20 | static unsigned char *c; | ||
21 | static unsigned char *t; | ||
22 | static unsigned char *k2; | ||
23 | static unsigned char *n2; | ||
24 | static unsigned char *m2; | ||
25 | static unsigned char *c2; | ||
26 | static unsigned char *t2; | ||
27 | |||
28 | #define klen crypto_secretbox_KEYBYTES | ||
29 | #define nlen crypto_secretbox_NONCEBYTES | ||
30 | |||
31 | void preallocate(void) | ||
32 | { | ||
33 | } | ||
34 | |||
35 | void allocate(void) | ||
36 | { | ||
37 | k = alignedcalloc(klen); | ||
38 | n = alignedcalloc(nlen); | ||
39 | m = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES); | ||
40 | c = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES); | ||
41 | t = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES); | ||
42 | k2 = alignedcalloc(klen); | ||
43 | n2 = alignedcalloc(nlen); | ||
44 | m2 = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES); | ||
45 | c2 = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES); | ||
46 | t2 = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES); | ||
47 | } | ||
48 | |||
49 | void predoit(void) | ||
50 | { | ||
51 | } | ||
52 | |||
53 | void doit(void) | ||
54 | { | ||
55 | crypto_secretbox(c,m,TUNE_BYTES + crypto_secretbox_ZEROBYTES,n,k); | ||
56 | crypto_secretbox_open(t,c,TUNE_BYTES + crypto_secretbox_ZEROBYTES,n,k); | ||
57 | } | ||
58 | |||
59 | char checksum[klen * 2 + 1]; | ||
60 | |||
61 | const char *checksum_compute(void) | ||
62 | { | ||
63 | long long i; | ||
64 | long long j; | ||
65 | |||
66 | for (j = 0;j < crypto_secretbox_ZEROBYTES;++j) m[j] = 0; | ||
67 | |||
68 | for (i = 0;i < CHECKSUM_BYTES;++i) { | ||
69 | long long mlen = i + crypto_secretbox_ZEROBYTES; | ||
70 | long long tlen = i + crypto_secretbox_ZEROBYTES; | ||
71 | long long clen = i + crypto_secretbox_ZEROBYTES; | ||
72 | |||
73 | for (j = -16;j < 0;++j) k[j] = random(); | ||
74 | for (j = -16;j < 0;++j) n[j] = random(); | ||
75 | for (j = -16;j < 0;++j) m[j] = random(); | ||
76 | for (j = klen;j < klen + 16;++j) k[j] = random(); | ||
77 | for (j = nlen;j < nlen + 16;++j) n[j] = random(); | ||
78 | for (j = mlen;j < mlen + 16;++j) m[j] = random(); | ||
79 | for (j = -16;j < klen + 16;++j) k2[j] = k[j]; | ||
80 | for (j = -16;j < nlen + 16;++j) n2[j] = n[j]; | ||
81 | for (j = -16;j < mlen + 16;++j) m2[j] = m[j]; | ||
82 | for (j = -16;j < clen + 16;++j) c2[j] = c[j] = random(); | ||
83 | |||
84 | if (crypto_secretbox(c,m,mlen,n,k) != 0) return "crypto_secretbox returns nonzero"; | ||
85 | |||
86 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_secretbox overwrites m"; | ||
87 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_secretbox overwrites n"; | ||
88 | for (j = -16;j < klen + 16;++j) if (k2[j] != k[j]) return "crypto_secretbox overwrites k"; | ||
89 | for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_secretbox writes before output"; | ||
90 | for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_secretbox writes after output"; | ||
91 | for (j = 0;j < crypto_secretbox_BOXZEROBYTES;++j) | ||
92 | if (c[j] != 0) return "crypto_secretbox does not clear extra bytes"; | ||
93 | |||
94 | for (j = -16;j < 0;++j) c[j] = random(); | ||
95 | for (j = clen;j < clen + 16;++j) c[j] = random(); | ||
96 | for (j = -16;j < clen + 16;++j) c2[j] = c[j]; | ||
97 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
98 | |||
99 | if (crypto_secretbox_open(t,c,clen,n,k) != 0) return "crypto_secretbox_open returns nonzero"; | ||
100 | |||
101 | for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_secretbox_open overwrites c"; | ||
102 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_secretbox_open overwrites n"; | ||
103 | for (j = -16;j < klen + 16;++j) if (k2[j] != k[j]) return "crypto_secretbox_open overwrites k"; | ||
104 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_secretbox_open writes before output"; | ||
105 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_secretbox_open writes after output"; | ||
106 | for (j = 0;j < crypto_secretbox_ZEROBYTES;++j) | ||
107 | if (t[j] != 0) return "crypto_secretbox_open does not clear extra bytes"; | ||
108 | |||
109 | for (j = 0;j < i;++j) if (t[j] != m[j]) return "plaintext does not match"; | ||
110 | |||
111 | for (j = 0;j < i;++j) | ||
112 | k[j % klen] ^= c[j + crypto_secretbox_BOXZEROBYTES]; | ||
113 | crypto_secretbox(c,m,mlen,n,k); | ||
114 | for (j = 0;j < i;++j) | ||
115 | n[j % nlen] ^= c[j + crypto_secretbox_BOXZEROBYTES]; | ||
116 | crypto_secretbox(c,m,mlen,n,k); | ||
117 | if (i == 0) m[crypto_secretbox_ZEROBYTES + 0] = 0; | ||
118 | m[crypto_secretbox_ZEROBYTES + i] = m[crypto_secretbox_ZEROBYTES + 0]; | ||
119 | for (j = 0;j < i;++j) | ||
120 | m[j + crypto_secretbox_ZEROBYTES] ^= c[j + crypto_secretbox_BOXZEROBYTES]; | ||
121 | } | ||
122 | |||
123 | for (i = 0;i < klen;++i) { | ||
124 | checksum[2 * i] = "0123456789abcdef"[15 & (k[i] >> 4)]; | ||
125 | checksum[2 * i + 1] = "0123456789abcdef"[15 & k[i]]; | ||
126 | } | ||
127 | checksum[2 * i] = 0; | ||
128 | return 0; | ||
129 | } | ||