-
Notifications
You must be signed in to change notification settings - Fork 1
/
PQgenKAT_sign.c
280 lines (241 loc) · 7.19 KB
/
PQgenKAT_sign.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Implementors: EagleSign Team
* This implementation is highly inspired from Dilithium and
* Falcon Signatures' implementations
*/
//
// PQgenKAT_sign.c
//
// Created by Bassham, Lawrence E (Fed) on 8/29/17.
// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
//
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "rng.h"
#include "sign.h"
#define MAX_MARKER_LEN 50
#define KAT_SUCCESS 0
#define KAT_FILE_OPEN_ERROR -1
#define KAT_DATA_ERROR -3
#define KAT_CRYPTO_FAILURE -4
int FindMarker(FILE *infile, const char *marker);
int ReadHex(FILE *infile, unsigned char *a, int Length, char *str);
void fprintBstr(FILE *fp, char *s, unsigned char *a, unsigned long long l);
int main()
{
char fn_req[32], fn_rsp[32];
FILE *fp_req, *fp_rsp;
uint8_t seed[48];
uint8_t msg[3300];
uint8_t entropy_input[48];
uint8_t *m, *sm, *m1;
size_t mlen, smlen, mlen1;
int count;
int done;
uint8_t pk[CRYPTO_EAGLESIGN_PUBLICKEYBYTES], sk[CRYPTO_EAGLESIGN_SECRETKEYBYTES];
int ret_val;
// Create the REQUEST file
sprintf(fn_req, "PQsignKAT_%.16s.req", CRYPTO_ALGNAME);
if ((fp_req = fopen(fn_req, "w")) == NULL)
{
printf("Couldn't open <%s> for write\n", fn_req);
return KAT_FILE_OPEN_ERROR;
}
sprintf(fn_rsp, "PQsignKAT_%.16s.rsp", CRYPTO_ALGNAME);
if ((fp_rsp = fopen(fn_rsp, "w")) == NULL)
{
printf("Couldn't open <%s> for write\n", fn_rsp);
return KAT_FILE_OPEN_ERROR;
}
for (int i = 0; i < 48; i++)
entropy_input[i] = i;
randombytes_init(entropy_input, NULL, 256);
for (int i = 0; i < 100; i++)
{
fprintf(fp_req, "count = %d\n", i);
randombytes(seed, 48);
fprintBstr(fp_req, "seed = ", seed, 48);
mlen = 33 * (i + 1);
fprintf(fp_req, "mlen = %lu\n", mlen);
randombytes(msg, mlen);
fprintBstr(fp_req, "msg = ", msg, mlen);
fprintf(fp_req, "pk =\n");
fprintf(fp_req, "sk =\n");
fprintf(fp_req, "smlen =\n");
fprintf(fp_req, "sm =\n\n");
}
fclose(fp_req);
// Create the RESPONSE file based on what's in the REQUEST file
if ((fp_req = fopen(fn_req, "r")) == NULL)
{
printf("Couldn't open <%s> for read\n", fn_req);
return KAT_FILE_OPEN_ERROR;
}
fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME);
done = 0;
do
{
if (FindMarker(fp_req, "count = "))
fscanf(fp_req, "%d", &count);
else
{
done = 1;
break;
}
fprintf(fp_rsp, "count = %d\n", count);
if (!ReadHex(fp_req, seed, 48, "seed = "))
{
printf("ERROR: unable to read 'seed' from <%s>\n", fn_req);
return KAT_DATA_ERROR;
}
fprintBstr(fp_rsp, "seed = ", seed, 48);
randombytes_init(seed, NULL, 256);
if (FindMarker(fp_req, "mlen = "))
fscanf(fp_req, "%lu", &mlen);
else
{
printf("ERROR: unable to read 'mlen' from <%s>\n", fn_req);
return KAT_DATA_ERROR;
}
fprintf(fp_rsp, "mlen = %lu\n", mlen);
m = (uint8_t *)calloc(mlen, sizeof(uint8_t));
m1 = (uint8_t *)calloc(mlen + CRYPTO_EAGLESIGN_BYTES, sizeof(uint8_t));
sm = (uint8_t *)calloc(mlen + CRYPTO_EAGLESIGN_BYTES, sizeof(uint8_t));
if (!ReadHex(fp_req, m, (int)mlen, "msg = "))
{
printf("ERROR: unable to read 'msg' from <%s>\n", fn_req);
return KAT_DATA_ERROR;
}
fprintBstr(fp_rsp, "msg = ", m, mlen);
// Generate the public/private keypair
if ((ret_val = crypto_sign_keypair(pk, sk)) != 0)
{
printf("crypto_sign_keypair returned <%d>\n", ret_val);
return KAT_CRYPTO_FAILURE;
}
fprintBstr(fp_rsp, "pk = ", pk, CRYPTO_EAGLESIGN_PUBLICKEYBYTES);
fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_EAGLESIGN_SECRETKEYBYTES);
if ((ret_val = crypto_sign(sm, &smlen, m, mlen, sk)) != 0)
{
printf("crypto_sign returned <%d>\n", ret_val);
return KAT_CRYPTO_FAILURE;
}
fprintf(fp_rsp, "smlen = %lu\n", smlen);
fprintBstr(fp_rsp, "sm = ", sm, smlen);
fprintf(fp_rsp, "\n");
if ((ret_val = crypto_sign_open(m1, &mlen1, sm, smlen, pk)) != 0)
{
printf("crypto_sign_open returned <%d>\n", ret_val);
return KAT_CRYPTO_FAILURE;
}
if (mlen != mlen1)
{
printf("crypto_sign_open returned bad 'mlen': Got <%lu>, expected <%lu>\n", mlen1, mlen);
return KAT_CRYPTO_FAILURE;
}
if (memcmp(m, m1, mlen))
{
printf("crypto_sign_open returned bad 'm' value\n");
return KAT_CRYPTO_FAILURE;
}
free(m);
free(m1);
free(sm);
} while (!done);
fclose(fp_req);
fclose(fp_rsp);
return KAT_SUCCESS;
}
//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int FindMarker(FILE *infile, const char *marker)
{
char line[MAX_MARKER_LEN];
int i, len;
int curr_line;
len = (int)strlen(marker);
if (len > MAX_MARKER_LEN - 1)
len = MAX_MARKER_LEN - 1;
for (i = 0; i < len; i++)
{
curr_line = fgetc(infile);
line[i] = curr_line;
if (curr_line == EOF)
return 0;
}
line[len] = '\0';
while (1)
{
if (!strncmp(line, marker, len))
return 1;
for (i = 0; i < len - 1; i++)
line[i] = line[i + 1];
curr_line = fgetc(infile);
line[len - 1] = curr_line;
if (curr_line == EOF)
return 0;
line[len] = '\0';
}
// shouldn't get here
return 0;
}
//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int ReadHex(FILE *infile, unsigned char *a, int Length, char *str)
{
int i, ch, started;
unsigned char ich;
if (Length == 0)
{
a[0] = 0x00;
return 1;
}
memset(a, 0x00, Length);
started = 0;
if (FindMarker(infile, str))
while ((ch = fgetc(infile)) != EOF)
{
if (!isxdigit(ch))
{
if (!started)
{
if (ch == '\n')
break;
else
continue;
}
else
break;
}
started = 1;
if ((ch >= '0') && (ch <= '9'))
ich = ch - '0';
else if ((ch >= 'A') && (ch <= 'F'))
ich = ch - 'A' + 10;
else if ((ch >= 'a') && (ch <= 'f'))
ich = ch - 'a' + 10;
else // shouldn't ever get here
ich = 0;
for (i = 0; i < Length - 1; i++)
a[i] = (a[i] << 4) | (a[i + 1] >> 4);
a[Length - 1] = (a[Length - 1] << 4) | ich;
}
else
return 0;
return 1;
}
void fprintBstr(FILE *fp, char *s, unsigned char *a, unsigned long long l)
{
unsigned long long i;
fprintf(fp, "%s", s);
for (i = 0; i < l; i++)
fprintf(fp, "%02X", a[i]);
if (l == 0)
fprintf(fp, "00");
fprintf(fp, "\n");
}