summaryrefslogtreecommitdiff
path: root/nacl/crypto_sign/try.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/crypto_sign/try.c')
-rw-r--r--nacl/crypto_sign/try.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/nacl/crypto_sign/try.c b/nacl/crypto_sign/try.c
new file mode 100644
index 00000000..fc553416
--- /dev/null
+++ b/nacl/crypto_sign/try.c
@@ -0,0 +1,86 @@
1/*
2 * crypto_sign/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
6
7#include <stdlib.h>
8#include "randombytes.h"
9#include "crypto_sign.h"
10
11#define MAXTEST_BYTES 10000
12#define TUNE_BYTES 1536
13
14extern unsigned char *alignedcalloc(unsigned long long);
15
16const char *primitiveimplementation = crypto_sign_IMPLEMENTATION;
17
18static unsigned char *pk;
19static unsigned char *sk;
20static unsigned char *m; unsigned long long mlen;
21static unsigned char *sm; unsigned long long smlen;
22static unsigned char *t; unsigned long long tlen;
23
24void preallocate(void)
25{
26#ifdef RAND_R_PRNG_NOT_SEEDED
27 RAND_status();
28#endif
29}
30
31void allocate(void)
32{
33 pk = alignedcalloc(crypto_sign_PUBLICKEYBYTES);
34 sk = alignedcalloc(crypto_sign_SECRETKEYBYTES);
35 m = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
36 sm = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
37 t = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
38}
39
40void predoit(void)
41{
42 crypto_sign_keypair(pk,sk);
43 mlen = TUNE_BYTES;
44 smlen = 0;
45 randombytes(m,mlen);
46 crypto_sign(sm,&smlen,m,mlen,sk);
47}
48
49void doit(void)
50{
51 crypto_sign_open(t,&tlen,sm,smlen,pk);
52}
53
54char checksum[crypto_sign_BYTES * 2 + 1];
55
56const char *checksum_compute(void)
57{
58 long long mlen;
59 long long i;
60 long long j;
61
62 if (crypto_sign_keypair(pk,sk) != 0) return "crypto_sign_keypair returns nonzero";
63 for (mlen = 0;mlen < MAXTEST_BYTES;mlen += 1 + (mlen / 16)) {
64 if (crypto_sign(sm,&smlen,m,mlen,sk) != 0) return "crypto_sign returns nonzero";
65 if (crypto_sign_open(t,&tlen,sm,smlen,pk) != 0) return "crypto_sign_open returns nonzero";
66 if (tlen != mlen) return "crypto_sign_open does not match length";
67 for (i = 0;i < tlen;++i)
68 if (t[i] != m[i])
69 return "crypto_sign_open does not match contents";
70
71 j = random() % smlen;
72 sm[j] ^= 1;
73 if (crypto_sign_open(t,&tlen,sm,smlen,pk) == 0) {
74 if (tlen != mlen) return "crypto_sign_open allows trivial forgery of length";
75 for (i = 0;i < tlen;++i)
76 if (t[i] != m[i])
77 return "crypto_sign_open allows trivial forgery of contents";
78 }
79 sm[j] ^= 1;
80
81 }
82
83 /* do some long-term checksum */
84 checksum[0] = 0;
85 return 0;
86}