summaryrefslogtreecommitdiff
path: root/nacl/crypto_hashblocks/try.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/crypto_hashblocks/try.c')
-rw-r--r--nacl/crypto_hashblocks/try.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/nacl/crypto_hashblocks/try.c b/nacl/crypto_hashblocks/try.c
new file mode 100644
index 00000000..720d2fb3
--- /dev/null
+++ b/nacl/crypto_hashblocks/try.c
@@ -0,0 +1,79 @@
1/*
2 * crypto_hashblocks/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
6
7#include <stdlib.h>
8#include "crypto_hashblocks.h"
9
10extern unsigned char *alignedcalloc(unsigned long long);
11
12const char *primitiveimplementation = crypto_hashblocks_IMPLEMENTATION;
13
14#define MAXTEST_BYTES (10000 + crypto_hashblocks_STATEBYTES)
15#define CHECKSUM_BYTES 4096
16#define TUNE_BYTES 1536
17
18static unsigned char *h;
19static unsigned char *h2;
20static unsigned char *m;
21static unsigned char *m2;
22
23void preallocate(void)
24{
25}
26
27void allocate(void)
28{
29 h = alignedcalloc(crypto_hashblocks_STATEBYTES);
30 h2 = alignedcalloc(crypto_hashblocks_STATEBYTES);
31 m = alignedcalloc(MAXTEST_BYTES);
32 m2 = alignedcalloc(MAXTEST_BYTES);
33}
34
35void predoit(void)
36{
37}
38
39void doit(void)
40{
41 crypto_hashblocks(h,m,TUNE_BYTES);
42}
43
44char checksum[crypto_hashblocks_STATEBYTES * 2 + 1];
45
46const char *checksum_compute(void)
47{
48 long long i;
49 long long j;
50
51 for (i = 0;i < CHECKSUM_BYTES;++i) {
52 long long hlen = crypto_hashblocks_STATEBYTES;
53 long long mlen = i;
54 for (j = -16;j < 0;++j) h[j] = random();
55 for (j = hlen;j < hlen + 16;++j) h[j] = random();
56 for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
57 for (j = -16;j < 0;++j) m[j] = random();
58 for (j = mlen;j < mlen + 16;++j) m[j] = random();
59 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
60 if (crypto_hashblocks(h,m,mlen) != 0) return "crypto_hashblocks returns nonzero";
61 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hashblocks writes to input";
62 for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hashblocks writes before output";
63 for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hashblocks writes after output";
64 for (j = 0;j < hlen;++j) m2[j] = h2[j];
65 if (crypto_hashblocks(h2,m2,mlen) != 0) return "crypto_hashblocks returns nonzero";
66 if (crypto_hashblocks(m2,m2,mlen) != 0) return "crypto_hashblocks returns nonzero";
67 for (j = 0;j < hlen;++j) if (m2[j] != h2[j]) return "crypto_hashblocks does not handle overlap";
68 for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen];
69 m[mlen] = h[0];
70 }
71 if (crypto_hashblocks(h,m,CHECKSUM_BYTES) != 0) return "crypto_hashblocks returns nonzero";
72
73 for (i = 0;i < crypto_hashblocks_STATEBYTES;++i) {
74 checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
75 checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
76 }
77 checksum[2 * i] = 0;
78 return 0;
79}