diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-02 09:53:34 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-02 09:53:34 -0400 |
commit | e2967396ac73cb7410787886cdaf072a184ffc49 (patch) | |
tree | 527a74d25a4a0705fc641994fd35bfab22662034 /nacl/crypto_hash/try.c | |
parent | 8928c817df345f29aa0b194743595aa11bd6a8ba (diff) |
Added NaCl crypto library.
Diffstat (limited to 'nacl/crypto_hash/try.c')
-rw-r--r-- | nacl/crypto_hash/try.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/nacl/crypto_hash/try.c b/nacl/crypto_hash/try.c new file mode 100644 index 00000000..fab49c99 --- /dev/null +++ b/nacl/crypto_hash/try.c | |||
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | * crypto_hash/try.c version 20090118 | ||
3 | * D. J. Bernstein | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <stdlib.h> | ||
8 | #include "crypto_hash.h" | ||
9 | |||
10 | extern unsigned char *alignedcalloc(unsigned long long); | ||
11 | |||
12 | const char *primitiveimplementation = crypto_hash_IMPLEMENTATION; | ||
13 | |||
14 | #define MAXTEST_BYTES (10000 + crypto_hash_BYTES) | ||
15 | #define CHECKSUM_BYTES 4096 | ||
16 | #define TUNE_BYTES 1536 | ||
17 | |||
18 | static unsigned char *h; | ||
19 | static unsigned char *h2; | ||
20 | static unsigned char *m; | ||
21 | static unsigned char *m2; | ||
22 | |||
23 | void preallocate(void) | ||
24 | { | ||
25 | } | ||
26 | |||
27 | void allocate(void) | ||
28 | { | ||
29 | h = alignedcalloc(crypto_hash_BYTES); | ||
30 | h2 = alignedcalloc(crypto_hash_BYTES); | ||
31 | m = alignedcalloc(MAXTEST_BYTES); | ||
32 | m2 = alignedcalloc(MAXTEST_BYTES); | ||
33 | } | ||
34 | |||
35 | void predoit(void) | ||
36 | { | ||
37 | } | ||
38 | |||
39 | void doit(void) | ||
40 | { | ||
41 | crypto_hash(h,m,TUNE_BYTES); | ||
42 | } | ||
43 | |||
44 | char checksum[crypto_hash_BYTES * 2 + 1]; | ||
45 | |||
46 | const 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_hash_BYTES; | ||
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_hash(h,m,mlen) != 0) return "crypto_hash returns nonzero"; | ||
61 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hash writes to input"; | ||
62 | for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hash writes before output"; | ||
63 | for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hash writes after output"; | ||
64 | if (crypto_hash(m2,m2,mlen) != 0) return "crypto_hash returns nonzero"; | ||
65 | for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_hash does not handle overlap"; | ||
66 | for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen]; | ||
67 | m[mlen] = h[0]; | ||
68 | } | ||
69 | if (crypto_hash(h,m,CHECKSUM_BYTES) != 0) return "crypto_hash returns nonzero"; | ||
70 | |||
71 | for (i = 0;i < crypto_hash_BYTES;++i) { | ||
72 | checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)]; | ||
73 | checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]]; | ||
74 | } | ||
75 | checksum[2 * i] = 0; | ||
76 | return 0; | ||
77 | } | ||