libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_sign / try.c
blob8ea81b657b6ffd1258a49cff24a5d80389c54934
1 /*
2 * crypto_sign/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "randombytes.h"
9 #include "crypto_sign.h"
10 #include "windows/windows-quirks.h"
12 #define MAXTEST_BYTES 10000
13 #define TUNE_BYTES 1536
15 extern unsigned char *alignedcalloc(unsigned long long);
17 const char *primitiveimplementation = crypto_sign_IMPLEMENTATION;
19 static unsigned char *pk;
20 static unsigned char *sk;
21 static unsigned char *m; unsigned long long mlen;
22 static unsigned char *sm; unsigned long long smlen;
23 static unsigned char *t; unsigned long long tlen;
25 void preallocate(void)
27 #ifdef RAND_R_PRNG_NOT_SEEDED
28 RAND_status();
29 #endif
32 void allocate(void)
34 pk = alignedcalloc(crypto_sign_PUBLICKEYBYTES);
35 sk = alignedcalloc(crypto_sign_SECRETKEYBYTES);
36 m = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
37 sm = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
38 t = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
41 void predoit(void)
43 crypto_sign_keypair(pk,sk);
44 mlen = TUNE_BYTES;
45 smlen = 0;
46 randombytes(m,mlen);
47 crypto_sign(sm,&smlen,m,mlen,sk);
50 void doit(void)
52 crypto_sign_open(t,&tlen,sm,smlen,pk);
55 char checksum[crypto_sign_BYTES * 2 + 1];
57 const char *checksum_compute(void)
59 long long mlen;
60 long long i;
61 long long j;
63 if (crypto_sign_keypair(pk,sk) != 0) return "crypto_sign_keypair returns nonzero";
64 for (mlen = 0;mlen < MAXTEST_BYTES;mlen += 1 + (mlen / 16)) {
65 if (crypto_sign(sm,&smlen,m,mlen,sk) != 0) return "crypto_sign returns nonzero";
66 if (crypto_sign_open(t,&tlen,sm,smlen,pk) != 0) return "crypto_sign_open returns nonzero";
67 if (tlen != mlen) return "crypto_sign_open does not match length";
68 for (i = 0;i < tlen;++i)
69 if (t[i] != m[i])
70 return "crypto_sign_open does not match contents";
72 j = rand() % smlen;
73 sm[j] ^= 1;
74 if (crypto_sign_open(t,&tlen,sm,smlen,pk) == 0) {
75 if (tlen != mlen) return "crypto_sign_open allows trivial forgery of length";
76 for (i = 0;i < tlen;++i)
77 if (t[i] != m[i])
78 return "crypto_sign_open allows trivial forgery of contents";
80 sm[j] ^= 1;
84 /* do some long-term checksum */
85 checksum[0] = 0;
86 return 0;