This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtry.c
140 lines (125 loc) · 4.22 KB
/
try.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* crypto_encrypt/try.c version 20140423
* D. J. Bernstein
* Public domain.
* Auto-generated by trygen.py; do not edit.
*/
#include "crypto_encrypt.h"
#include "try.h"
#include "randombytes.h"
const char *primitiveimplementation = crypto_encrypt_IMPLEMENTATION;
#define TUNE_BYTES 1536
#ifdef SMALL
#define MAXTEST_BYTES 128
#else
#define MAXTEST_BYTES 4096
#endif
#ifdef SMALL
#define LOOPS 8
#else
#define LOOPS 64
#endif
static unsigned char *p;
static unsigned char *s;
static unsigned char *m;
static unsigned char *c;
static unsigned char *t;
static unsigned char *p2;
static unsigned char *s2;
static unsigned char *m2;
static unsigned char *c2;
static unsigned char *t2;
#define plen crypto_encrypt_PUBLICKEYBYTES
#define slen crypto_encrypt_SECRETKEYBYTES
unsigned long long mlen;
unsigned long long clen;
unsigned long long tlen;
void preallocate(void)
{
#ifdef RAND_R_PRNG_NOT_SEEDED
RAND_status();
#endif
}
void allocate(void)
{
unsigned long long alloclen = 0;
if (alloclen < TUNE_BYTES) alloclen = TUNE_BYTES;
if (alloclen < MAXTEST_BYTES + crypto_encrypt_BYTES) alloclen = MAXTEST_BYTES + crypto_encrypt_BYTES;
if (alloclen < crypto_encrypt_PUBLICKEYBYTES) alloclen = crypto_encrypt_PUBLICKEYBYTES;
if (alloclen < crypto_encrypt_SECRETKEYBYTES) alloclen = crypto_encrypt_SECRETKEYBYTES;
p = alignedcalloc(alloclen);
s = alignedcalloc(alloclen);
m = alignedcalloc(alloclen);
c = alignedcalloc(alloclen);
t = alignedcalloc(alloclen);
p2 = alignedcalloc(alloclen);
s2 = alignedcalloc(alloclen);
m2 = alignedcalloc(alloclen);
c2 = alignedcalloc(alloclen);
t2 = alignedcalloc(alloclen);
}
void predoit(void)
{
crypto_encrypt_keypair(p,s);
mlen = TUNE_BYTES;
clen = 0;
randombytes(m,mlen);
}
void doit(void)
{
crypto_encrypt(c,&clen,m,mlen,p);
crypto_encrypt_open(t,&tlen,c,clen,s);
}
void test(void)
{
unsigned long long loop;
for (loop = 0;loop < LOOPS;++loop) {
mlen = myrandom() % (MAXTEST_BYTES + 1);
output_prepare(p2,p,plen);
output_prepare(s2,s,slen);
if (crypto_encrypt_keypair(p,s) != 0) fail("crypto_encrypt_keypair returns nonzero");
checksum(p,plen);
checksum(s,slen);
output_compare(p2,p,plen,"crypto_encrypt_keypair");
output_compare(s2,s,slen,"crypto_encrypt_keypair");
clen = mlen + crypto_encrypt_BYTES;
output_prepare(c2,c,clen);
input_prepare(m2,m,mlen);
memcpy(p2,p,plen);
double_canary(p2,p,plen);
if (crypto_encrypt(c,&clen,m,mlen,p) != 0) fail("crypto_encrypt returns nonzero");
if (clen < mlen) fail("crypto_encrypt returns smaller output than input");
if (clen > mlen + crypto_encrypt_BYTES) fail("crypto_encrypt returns more than crypto_encrypt_BYTES extra bytes");
checksum(c,clen);
output_compare(c2,c,clen,"crypto_encrypt");
input_compare(m2,m,mlen,"crypto_encrypt");
input_compare(p2,p,plen,"crypto_encrypt");
tlen = clen;
output_prepare(t2,t,tlen);
memcpy(c2,c,clen);
double_canary(c2,c,clen);
memcpy(s2,s,slen);
double_canary(s2,s,slen);
if (crypto_encrypt_open(t,&tlen,c,clen,s) != 0) fail("crypto_encrypt_open returns nonzero");
if (tlen != mlen) fail("crypto_encrypt_open does not match mlen");
if (memcmp(t,m,mlen) != 0) fail("crypto_encrypt_open does not match m");
checksum(t,tlen);
output_compare(t2,t,tlen,"crypto_encrypt_open");
input_compare(c2,c,clen,"crypto_encrypt_open");
input_compare(s2,s,slen,"crypto_encrypt_open");
double_canary(t2,t,tlen);
double_canary(c2,c,clen);
double_canary(s2,s,slen);
if (crypto_encrypt_open(t2,&tlen,c2,clen,s2) != 0) fail("crypto_encrypt_open returns nonzero");
if (memcmp(t2,t,tlen) != 0) fail("crypto_encrypt_open is nondeterministic");
double_canary(t2,t,tlen);
double_canary(c2,c,clen);
double_canary(s2,s,slen);
if (crypto_encrypt_open(c2,&tlen,c2,clen,s) != 0) fail("crypto_encrypt_open with c=t overlap returns nonzero");
if (memcmp(c2,t,tlen) != 0) fail("crypto_encrypt_open does not handle c=t overlap");
memcpy(c2,c,clen);
if (crypto_encrypt_open(s2,&tlen,c,clen,s2) != 0) fail("crypto_encrypt_open with s=t overlap returns nonzero");
if (memcmp(s2,t,tlen) != 0) fail("crypto_encrypt_open does not handle s=t overlap");
memcpy(s2,s,slen);
}
}