-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rudraksh.c
More file actions
340 lines (292 loc) · 10.3 KB
/
Copy pathtest_rudraksh.c
File metadata and controls
340 lines (292 loc) · 10.3 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "api.h"
#include "cbd.h"
#include "kem.h"
#include "params.h"
#include "poly.h"
#include "polyvec.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define KEM_ROUNDTRIP_ITERS 512
enum {
ASSERT_PK_BYTES = 1 / (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_PUBLICKEYBYTES == RUDRAKSH_PUBLICKEYBYTES),
ASSERT_SK_BYTES = 1 / (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_SECRETKEYBYTES == RUDRAKSH_SECRETKEYBYTES),
ASSERT_CT_BYTES = 1 / (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_CIPHERTEXTBYTES == RUDRAKSH_CIPHERTEXTBYTES),
ASSERT_SS_BYTES = 1 / (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_BYTES == RUDRAKSH_SSBYTES)
};
static uint32_t rng_state = 0x12345678u;
static uint8_t test_random_byte(void) {
rng_state ^= rng_state << 13;
rng_state ^= rng_state >> 17;
rng_state ^= rng_state << 5;
return (uint8_t)(rng_state >> 24);
}
static void test_random_bytes(uint8_t *out, size_t outlen) {
size_t i;
for (i = 0; i < outlen; i++) {
out[i] = test_random_byte();
}
}
static uint16_t test_random_modq(void) {
uint16_t v = (uint16_t)test_random_byte();
v = (uint16_t)(v | ((uint16_t)test_random_byte() << 8));
return (uint16_t)(v % RUDRAKSH_Q);
}
static void test_random_poly(poly *a) {
unsigned int i;
for (i = 0; i < RUDRAKSH_N; i++) {
a->coeffs[i] = (int16_t)test_random_modq();
}
}
static int poly_equal_modq(const poly *a, const poly *b) {
unsigned int i;
for (i = 0; i < RUDRAKSH_N; i++) {
int16_t x = a->coeffs[i];
int16_t y = b->coeffs[i];
while (x < 0) {
x = (int16_t)(x + RUDRAKSH_Q);
}
while (y < 0) {
y = (int16_t)(y + RUDRAKSH_Q);
}
x = (int16_t)(x % RUDRAKSH_Q);
y = (int16_t)(y % RUDRAKSH_Q);
if (x != y) {
return 0;
}
}
return 1;
}
static int test_poly_serialization(void) {
poly a;
poly b;
polyvec av;
polyvec bv;
uint8_t buf[RUDRAKSH_POLYBYTES];
uint8_t vbuf[RUDRAKSH_POLYVECBYTES];
unsigned int i;
for (i = 0; i < RUDRAKSH_N; i++) {
a.coeffs[i] = (int16_t)((i * 127u + 19u) % RUDRAKSH_Q);
}
a.coeffs[0] = 0;
a.coeffs[1] = RUDRAKSH_Q - 1;
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_tobytes(buf, &a);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_frombytes(&b, buf);
if (!poly_equal_modq(&a, &b)) {
fprintf(stderr, "poly serialization roundtrip failed\n");
return 1;
}
for (i = 0; i < RUDRAKSH_L; i++) {
test_random_poly(&av.vec[i]);
}
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_polyvec_tobytes(vbuf, &av);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_polyvec_frombytes(&bv, vbuf);
for (i = 0; i < RUDRAKSH_L; i++) {
if (!poly_equal_modq(&av.vec[i], &bv.vec[i])) {
fprintf(stderr, "polyvec serialization roundtrip failed at component %u\n", i);
return 1;
}
}
return 0;
}
static int test_compression_normalization(void) {
poly a;
poly b;
polyvec av;
polyvec bv;
uint8_t u0[RUDRAKSH_POLYCOMPRESSEDBYTES];
uint8_t u1[RUDRAKSH_POLYCOMPRESSEDBYTES];
uint8_t vv0[RUDRAKSH_POLYVCOMPRESSEDBYTES];
uint8_t vv1[RUDRAKSH_POLYVCOMPRESSEDBYTES];
uint8_t vu0[RUDRAKSH_POLYVECCOMPRESSEDBYTES];
uint8_t vu1[RUDRAKSH_POLYVECCOMPRESSEDBYTES];
unsigned int i;
for (i = 0; i < 128u; i++) {
test_random_poly(&a);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_compress_u(u0, &a);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_decompress_u(&b, u0);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_compress_u(u1, &b);
if (memcmp(u0, u1, sizeof u0) != 0) {
fprintf(stderr, "u compression normalization failed at iteration %u\n", i);
return 1;
}
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_compress_v(vv0, &a);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_decompress_v(&b, vv0);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_compress_v(vv1, &b);
if (memcmp(vv0, vv1, sizeof vv0) != 0) {
fprintf(stderr, "v compression normalization failed at iteration %u\n", i);
return 1;
}
}
for (i = 0; i < RUDRAKSH_L; i++) {
test_random_poly(&av.vec[i]);
}
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_polyvec_compress(vu0, &av);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_polyvec_decompress(&bv, vu0);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_polyvec_compress(vu1, &bv);
if (memcmp(vu0, vu1, sizeof vu0) != 0) {
fprintf(stderr, "polyvec compression normalization failed\n");
return 1;
}
return 0;
}
static int test_ntt_roundtrip(void) {
poly a;
poly b;
unsigned int i;
for (i = 0; i < 128u; i++) {
test_random_poly(&a);
b = a;
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_ntt(&b);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_invntt(&b);
if (!poly_equal_modq(&a, &b)) {
fprintf(stderr, "NTT roundtrip failed at iteration %u\n", i);
return 1;
}
}
return 0;
}
static int test_message_roundtrip(void) {
uint8_t msg[RUDRAKSH_MSGBYTES];
uint8_t out[RUDRAKSH_MSGBYTES];
poly a;
unsigned int i;
for (i = 0; i < 256u; i++) {
test_random_bytes(msg, sizeof msg);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_frommsg(&a, msg);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_tomsg(out, &a);
if (memcmp(msg, out, sizeof msg) != 0) {
fprintf(stderr, "message encode/decode failed at iteration %u\n", i);
return 1;
}
}
return 0;
}
static int test_cbd_range(void) {
uint8_t buf[RUDRAKSH_N * 4 / 8];
poly a;
unsigned int i;
unsigned int j;
for (i = 0; i < 256u; i++) {
test_random_bytes(buf, sizeof buf);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_poly_cbd_eta2(&a, buf);
for (j = 0; j < RUDRAKSH_N; j++) {
if (a.coeffs[j] < -2 || a.coeffs[j] > 2) {
fprintf(stderr, "CBD coefficient out of range at iteration %u coefficient %u\n", i, j);
return 1;
}
}
}
return 0;
}
static int test_derand_determinism(void) {
uint8_t coins[3u * RUDRAKSH_SYMBYTES];
uint8_t enc_coins[RUDRAKSH_SYMBYTES];
uint8_t pk0[RUDRAKSH_PUBLICKEYBYTES];
uint8_t pk1[RUDRAKSH_PUBLICKEYBYTES];
uint8_t sk0[RUDRAKSH_SECRETKEYBYTES];
uint8_t sk1[RUDRAKSH_SECRETKEYBYTES];
uint8_t ct0[RUDRAKSH_CIPHERTEXTBYTES];
uint8_t ct1[RUDRAKSH_CIPHERTEXTBYTES];
uint8_t ss0[RUDRAKSH_SSBYTES];
uint8_t ss1[RUDRAKSH_SSBYTES];
test_random_bytes(coins, sizeof coins);
test_random_bytes(enc_coins, sizeof enc_coins);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_keypair_derand(pk0, sk0, coins);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_keypair_derand(pk1, sk1, coins);
if (memcmp(pk0, pk1, sizeof pk0) != 0 || memcmp(sk0, sk1, sizeof sk0) != 0) {
fprintf(stderr, "keypair_derand is not deterministic\n");
return 1;
}
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_enc_derand(ct0, ss0, pk0, enc_coins);
PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_enc_derand(ct1, ss1, pk0, enc_coins);
if (memcmp(ct0, ct1, sizeof ct0) != 0 || memcmp(ss0, ss1, sizeof ss0) != 0) {
fprintf(stderr, "enc_derand is not deterministic\n");
return 1;
}
return 0;
}
static int test_kem_roundtrips(void) {
uint8_t pk[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_PUBLICKEYBYTES];
uint8_t sk[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_SECRETKEYBYTES];
uint8_t ct[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_CIPHERTEXTBYTES];
uint8_t badct[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_CIPHERTEXTBYTES];
uint8_t ss1[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_BYTES];
uint8_t ss2[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_BYTES];
uint8_t ss3[PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_CRYPTO_BYTES];
int i;
for (i = 0; i < KEM_ROUNDTRIP_ITERS; i++) {
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_keypair(pk, sk) != 0) {
return 1;
}
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_enc(ct, ss1, pk) != 0) {
return 1;
}
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_dec(ss2, ct, sk) != 0) {
return 1;
}
if (memcmp(ss1, ss2, sizeof ss1) != 0) {
fprintf(stderr, "KEM mismatch at iteration %d\n", i);
return 1;
}
memcpy(badct, ct, sizeof badct);
badct[(unsigned int)i % sizeof badct] ^= (uint8_t)(1u << ((unsigned int)i & 7u));
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_dec(ss3, badct, sk) != 0) {
return 1;
}
if (memcmp(ss1, ss3, sizeof ss1) == 0) {
fprintf(stderr, "tampered ciphertext accepted at iteration %d\n", i);
return 1;
}
}
return 0;
}
static int test_ciphertext_tamper_sweep(void) {
uint8_t pk[RUDRAKSH_PUBLICKEYBYTES];
uint8_t sk[RUDRAKSH_SECRETKEYBYTES];
uint8_t ct[RUDRAKSH_CIPHERTEXTBYTES];
uint8_t badct[RUDRAKSH_CIPHERTEXTBYTES];
uint8_t ss[RUDRAKSH_SSBYTES];
uint8_t reject0[RUDRAKSH_SSBYTES];
uint8_t reject1[RUDRAKSH_SSBYTES];
size_t i;
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_keypair(pk, sk) != 0) {
return 1;
}
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_enc(ct, ss, pk) != 0) {
return 1;
}
for (i = 0; i < sizeof ct; i++) {
memcpy(badct, ct, sizeof badct);
badct[i] ^= (uint8_t)(1u << (i & 7u));
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_dec(reject0, badct, sk) != 0) {
return 1;
}
if (PQCLEAN_RUDRAKSHKEMPOLY64_CLEAN_crypto_kem_dec(reject1, badct, sk) != 0) {
return 1;
}
if (memcmp(reject0, reject1, sizeof reject0) != 0) {
fprintf(stderr, "tampered ciphertext rejection is not deterministic at byte %zu\n", i);
return 1;
}
if (memcmp(ss, reject0, sizeof ss) == 0) {
fprintf(stderr, "tampered ciphertext accepted at byte %zu\n", i);
return 1;
}
}
return 0;
}
int main(void) {
if (test_poly_serialization() != 0 ||
test_compression_normalization() != 0 ||
test_ntt_roundtrip() != 0 ||
test_message_roundtrip() != 0 ||
test_cbd_range() != 0 ||
test_derand_determinism() != 0 ||
test_kem_roundtrips() != 0 ||
test_ciphertext_tamper_sweep() != 0) {
return 1;
}
puts("Rudraksh KEM-poly64 deep self-test passed");
return 0;
}