libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_auth / try.c
blobd6e55607b3a43a8af732782c3f5660ef8ecdbac6
1 /*
2 * crypto_auth/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_hash_sha256.h"
9 #include "crypto_auth.h"
10 #include "windows/windows-quirks.h"
12 extern unsigned char *alignedcalloc(unsigned long long);
14 const char *primitiveimplementation = crypto_auth_IMPLEMENTATION;
16 #define MAXTEST_BYTES 10000
17 #define CHECKSUM_BYTES 4096
18 #define TUNE_BYTES 1536
20 static unsigned char *h;
21 static unsigned char *m;
22 static unsigned char *k;
23 static unsigned char *h2;
24 static unsigned char *m2;
25 static unsigned char *k2;
27 void preallocate(void)
31 void allocate(void)
33 h = alignedcalloc(crypto_auth_BYTES);
34 m = alignedcalloc(MAXTEST_BYTES);
35 k = alignedcalloc(crypto_auth_KEYBYTES);
36 h2 = alignedcalloc(crypto_auth_BYTES);
37 m2 = alignedcalloc(MAXTEST_BYTES + crypto_auth_BYTES);
38 k2 = alignedcalloc(crypto_auth_KEYBYTES + crypto_auth_BYTES);
41 void predoit(void)
45 void doit(void)
47 crypto_auth(h,m,TUNE_BYTES,k);
48 crypto_auth_verify(h,m,TUNE_BYTES,k);
51 char checksum[crypto_auth_BYTES * 2 + 1];
53 const char *checksum_compute(void)
55 long long i;
56 long long j;
58 for (i = 0;i < CHECKSUM_BYTES;++i) {
59 long long mlen = i;
60 long long klen = crypto_auth_KEYBYTES;
61 long long hlen = crypto_auth_BYTES;
63 for (j = -16;j < 0;++j) h[j] = rand();
64 for (j = -16;j < 0;++j) k[j] = rand();
65 for (j = -16;j < 0;++j) m[j] = rand();
66 for (j = hlen;j < hlen + 16;++j) h[j] = rand();
67 for (j = klen;j < klen + 16;++j) k[j] = rand();
68 for (j = mlen;j < mlen + 16;++j) m[j] = rand();
69 for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
70 for (j = -16;j < klen + 16;++j) k2[j] = k[j];
71 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
73 if (crypto_auth(h,m,mlen,k) != 0) return "crypto_auth returns nonzero";
75 for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_auth overwrites k";
76 for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_auth overwrites m";
77 for (j = -16;j < 0;++j) if (h[j] != h2[j]) return "crypto_auth writes before output";
78 for (j = hlen;j < hlen + 16;++j) if (h[j] != h2[j]) return "crypto_auth writes after output";
80 for (j = -16;j < 0;++j) h[j] = rand();
81 for (j = -16;j < 0;++j) k[j] = rand();
82 for (j = -16;j < 0;++j) m[j] = rand();
83 for (j = hlen;j < hlen + 16;++j) h[j] = rand();
84 for (j = klen;j < klen + 16;++j) k[j] = rand();
85 for (j = mlen;j < mlen + 16;++j) m[j] = rand();
86 for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
87 for (j = -16;j < klen + 16;++j) k2[j] = k[j];
88 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
90 if (crypto_auth(m2,m2,mlen,k) != 0) return "crypto_auth returns nonzero";
91 for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_auth does not handle m overlap";
92 for (j = 0;j < hlen;++j) m2[j] = m[j];
93 if (crypto_auth(k2,m2,mlen,k2) != 0) return "crypto_auth returns nonzero";
94 for (j = 0;j < hlen;++j) if (k2[j] != h[j]) return "crypto_auth does not handle k overlap";
95 for (j = 0;j < hlen;++j) k2[j] = k[j];
97 if (crypto_auth_verify(h,m,mlen,k) != 0) return "crypto_auth_verify returns nonzero";
99 for (j = -16;j < hlen + 16;++j) if (h[j] != h2[j]) return "crypto_auth overwrites h";
100 for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_auth overwrites k";
101 for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_auth overwrites m";
103 crypto_hash_sha256(h2,h,hlen);
104 for (j = 0;j < klen;++j) k[j] ^= h2[j % 32];
105 if (crypto_auth(h,m,mlen,k) != 0) return "crypto_auth returns nonzero";
106 if (crypto_auth_verify(h,m,mlen,k) != 0) return "crypto_auth_verify returns nonzero";
108 crypto_hash_sha256(h2,h,hlen);
109 for (j = 0;j < mlen;++j) m[j] ^= h2[j % 32];
110 m[mlen] = h2[0];
112 if (crypto_auth(h,m,CHECKSUM_BYTES,k) != 0) return "crypto_auth returns nonzero";
113 if (crypto_auth_verify(h,m,CHECKSUM_BYTES,k) != 0) return "crypto_auth_verify returns nonzero";
115 for (i = 0;i < crypto_auth_BYTES;++i) {
116 checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
117 checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
119 checksum[2 * i] = 0;
120 return 0;