libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_hash / try.c
blob80e97dbc9ae5b37f308813125aab0f76c49fc415
1 /*
2 * crypto_hash/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_hash.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_hash_IMPLEMENTATION;
15 #define MAXTEST_BYTES (10000 + crypto_hash_BYTES)
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_hash_BYTES);
31 h2 = alignedcalloc(crypto_hash_BYTES);
32 m = alignedcalloc(MAXTEST_BYTES);
33 m2 = alignedcalloc(MAXTEST_BYTES);
36 void predoit(void)
40 void doit(void)
42 crypto_hash(h,m,TUNE_BYTES);
45 char checksum[crypto_hash_BYTES * 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_hash_BYTES;
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_hash(h,m,mlen) != 0) return "crypto_hash returns nonzero";
62 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hash writes to input";
63 for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hash writes before output";
64 for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hash writes after output";
65 if (crypto_hash(m2,m2,mlen) != 0) return "crypto_hash returns nonzero";
66 for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_hash does not handle overlap";
67 for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen];
68 m[mlen] = h[0];
70 if (crypto_hash(h,m,CHECKSUM_BYTES) != 0) return "crypto_hash returns nonzero";
72 for (i = 0;i < crypto_hash_BYTES;++i) {
73 checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
74 checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
76 checksum[2 * i] = 0;
77 return 0;