libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_hashblocks / try.c
blob78bf40af306b17694c19d337daeb3c44ed979190
1 /*
2 * crypto_hashblocks/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_hashblocks.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_hashblocks_IMPLEMENTATION;
15 #define MAXTEST_BYTES (10000 + crypto_hashblocks_STATEBYTES)
16 #define CHECKSUM_BYTES 4096
17 #define TUNE_BYTES 1536
19 static unsigned char *h;
20 static unsigned char *h2;
21 static unsigned char *m;
22 static unsigned char *m2;
24 void preallocate(void)
28 void allocate(void)
30 h = alignedcalloc(crypto_hashblocks_STATEBYTES);
31 h2 = alignedcalloc(crypto_hashblocks_STATEBYTES);
32 m = alignedcalloc(MAXTEST_BYTES);
33 m2 = alignedcalloc(MAXTEST_BYTES);
36 void predoit(void)
40 void doit(void)
42 crypto_hashblocks(h,m,TUNE_BYTES);
45 char checksum[crypto_hashblocks_STATEBYTES * 2 + 1];
47 const char *checksum_compute(void)
49 long long i;
50 long long j;
52 for (i = 0;i < CHECKSUM_BYTES;++i) {
53 long long hlen = crypto_hashblocks_STATEBYTES;
54 long long mlen = i;
55 for (j = -16;j < 0;++j) h[j] = rand();
56 for (j = hlen;j < hlen + 16;++j) h[j] = rand();
57 for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
58 for (j = -16;j < 0;++j) m[j] = rand();
59 for (j = mlen;j < mlen + 16;++j) m[j] = rand();
60 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
61 if (crypto_hashblocks(h,m,mlen) != 0) return "crypto_hashblocks returns nonzero";
62 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hashblocks writes to input";
63 for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hashblocks writes before output";
64 for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hashblocks writes after output";
65 for (j = 0;j < hlen;++j) m2[j] = h2[j];
66 if (crypto_hashblocks(h2,m2,mlen) != 0) return "crypto_hashblocks returns nonzero";
67 if (crypto_hashblocks(m2,m2,mlen) != 0) return "crypto_hashblocks returns nonzero";
68 for (j = 0;j < hlen;++j) if (m2[j] != h2[j]) return "crypto_hashblocks does not handle overlap";
69 for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen];
70 m[mlen] = h[0];
72 if (crypto_hashblocks(h,m,CHECKSUM_BYTES) != 0) return "crypto_hashblocks returns nonzero";
74 for (i = 0;i < crypto_hashblocks_STATEBYTES;++i) {
75 checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
76 checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
78 checksum[2 * i] = 0;
79 return 0;