-
Notifications
You must be signed in to change notification settings - Fork 26
/
picnic.c
464 lines (395 loc) · 13.4 KB
/
picnic.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "picnic.h"
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "io.h"
#include "lowmc.h"
#include "picnic_instances.h"
#if defined(WITH_ZKBPP)
#include "picnic_impl.h"
#endif
#if defined(WITH_KKW)
#include "picnic3_impl.h"
#endif
#include "randomness.h"
// Public and private keys are serialized as follows:
// - public key: instance || C || p
// - secret key: instance || sk || C || p
#define SK_SK(sk) &(sk)->data[1]
#define SK_C(sk, instance) &(sk)->data[1 + instance->input_output_size]
#define SK_PT(sk, instance) &(sk)->data[1 + 2 * instance->input_output_size]
#define PK_C(pk) &(pk)->data[1]
#define PK_PT(pk, instance) &(pk)->data[1 + instance->input_output_size]
size_t PICNIC_CALLING_CONVENTION picnic_get_lowmc_block_size(picnic_params_t param) {
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return 0;
}
return instance->input_output_size;
}
size_t PICNIC_CALLING_CONVENTION picnic_signature_size(picnic_params_t param) {
if (!picnic_instance_get(param)) {
return 0;
}
switch (param) {
case Picnic_L1_FS:
return PICNIC_SIGNATURE_SIZE_Picnic_L1_FS;
case Picnic_L1_UR:
return PICNIC_SIGNATURE_SIZE_Picnic_L1_UR;
case Picnic_L1_full:
return PICNIC_SIGNATURE_SIZE_Picnic_L1_full;
case Picnic3_L1:
return PICNIC_SIGNATURE_SIZE_Picnic3_L1;
case Picnic_L3_FS:
return PICNIC_SIGNATURE_SIZE_Picnic_L3_FS;
case Picnic_L3_UR:
return PICNIC_SIGNATURE_SIZE_Picnic_L3_UR;
case Picnic_L3_full:
return PICNIC_SIGNATURE_SIZE_Picnic_L3_full;
case Picnic3_L3:
return PICNIC_SIGNATURE_SIZE_Picnic3_L3;
case Picnic_L5_FS:
return PICNIC_SIGNATURE_SIZE_Picnic_L5_FS;
case Picnic_L5_UR:
return PICNIC_SIGNATURE_SIZE_Picnic_L5_UR;
case Picnic_L5_full:
return PICNIC_SIGNATURE_SIZE_Picnic_L5_full;
case Picnic3_L5:
return PICNIC_SIGNATURE_SIZE_Picnic3_L5;
default:
/* this should never happen */
return 0;
}
}
size_t PICNIC_CALLING_CONVENTION picnic_get_private_key_size(picnic_params_t param) {
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return 0;
}
return 1 + 3 * instance->input_output_size;
}
size_t PICNIC_CALLING_CONVENTION picnic_get_public_key_size(picnic_params_t param) {
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return 0;
}
return 1 + 2 * instance->input_output_size;
}
int PICNIC_CALLING_CONVENTION picnic_keygen(picnic_params_t param, picnic_publickey_t* pk,
picnic_privatekey_t* sk) {
if (!pk || !sk) {
return -1;
}
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
uint8_t* sk_sk = SK_SK(sk);
uint8_t* sk_pt = SK_PT(sk, instance);
uint8_t* sk_c = SK_C(sk, instance);
// generate private key
sk->data[0] = param;
// random secret key
if (rand_bits(sk_sk, instance->lowmc.n)) {
return -1;
}
// random plain text
if (rand_bits(sk_pt, instance->lowmc.n)) {
return -1;
}
// encrypt plaintext under secret key
if (picnic_sk_to_pk(sk, pk)) {
return -1;
}
// copy ciphertext to secret key
memcpy(sk_c, PK_C(pk), instance->input_output_size);
return 0;
}
int PICNIC_CALLING_CONVENTION picnic_sk_to_pk(const picnic_privatekey_t* sk,
picnic_publickey_t* pk) {
if (!sk || !pk) {
return -1;
}
const picnic_params_t param = sk->data[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const uint8_t* sk_sk = SK_SK(sk);
uint8_t* pk_c = PK_C(pk);
uint8_t* pk_pt = PK_PT(pk, instance);
const uint8_t* sk_pt = SK_PT(sk, instance);
mzd_local_t plaintext[(MAX_LOWMC_BLOCK_SIZE_BITS + 255) / 256];
mzd_local_t privkey[(MAX_LOWMC_BLOCK_SIZE_BITS + 255) / 256];
mzd_local_t ciphertext[(MAX_LOWMC_BLOCK_SIZE_BITS + 255) / 256];
mzd_from_char_array(plaintext, sk_pt, instance->input_output_size);
mzd_from_char_array(privkey, sk_sk, instance->input_output_size);
// compute public key
lowmc_compute(&instance->lowmc, privkey, plaintext, ciphertext);
pk->data[0] = param;
memcpy(pk_pt, sk_pt, instance->input_output_size);
mzd_to_char_array(pk_c, ciphertext, instance->input_output_size);
return 0;
}
int PICNIC_CALLING_CONVENTION picnic_validate_keypair(const picnic_privatekey_t* sk,
const picnic_publickey_t* pk) {
if (!sk || !pk) {
return -1;
}
const picnic_params_t param = sk->data[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const uint8_t* sk_sk = SK_SK(sk);
const uint8_t* sk_pt = SK_PT(sk, instance);
const uint8_t* sk_c = SK_C(sk, instance);
const uint8_t* pk_pt = PK_PT(pk, instance);
const uint8_t* pk_c = PK_C(pk);
// check param and plaintext
if (param != pk->data[0] || memcmp(sk_pt, pk_pt, instance->input_output_size) != 0 ||
memcmp(sk_c, pk_c, instance->input_output_size) != 0) {
return -1;
}
mzd_local_t plaintext[(MAX_LOWMC_BLOCK_SIZE_BITS + 255) / 256];
mzd_local_t privkey[(MAX_LOWMC_BLOCK_SIZE_BITS + 255) / 256];
mzd_local_t ciphertext[(MAX_LOWMC_BLOCK_SIZE_BITS + 255) / 256];
mzd_from_char_array(plaintext, sk_pt, instance->input_output_size);
mzd_from_char_array(privkey, sk_sk, instance->input_output_size);
// compute public key
lowmc_compute(&instance->lowmc, privkey, plaintext, ciphertext);
uint8_t buffer[MAX_LOWMC_BLOCK_SIZE];
mzd_to_char_array(buffer, ciphertext, instance->input_output_size);
return memcmp(buffer, pk_c, instance->input_output_size);
}
int PICNIC_CALLING_CONVENTION picnic_sign(const picnic_privatekey_t* sk, const uint8_t* message,
size_t message_len, uint8_t* signature,
size_t* signature_len) {
if (!sk || !signature || !signature_len) {
return -1;
}
const picnic_params_t param = sk->data[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const uint8_t* sk_sk = SK_SK(sk);
const uint8_t* sk_c = SK_C(sk, instance);
const uint8_t* sk_pt = SK_PT(sk, instance);
if (param == Picnic3_L1 || param == Picnic3_L3 || param == Picnic3_L5) {
#if defined(WITH_KKW)
return impl_sign_picnic3(instance, sk_pt, sk_sk, sk_c, message, message_len, signature,
signature_len);
#else
return -1;
#endif
} else {
#if defined(WITH_ZKBPP)
picnic_context_t context;
mzd_from_char_array(context.m_plaintext, sk_pt, instance->input_output_size);
mzd_from_char_array(context.m_key, sk_sk, instance->input_output_size);
context.plaintext = sk_pt;
context.private_key = sk_sk;
context.public_key = sk_c;
context.msg = message;
context.msglen = message_len;
#if defined(WITH_UNRUH)
context.unruh = picnic_instance_is_unruh(param);
#endif
int ret = picnic_impl_sign(instance, &context, signature, signature_len);
picnic_explicit_bzero(context.m_key, sizeof(context.m_key));
return ret;
#else
return -1;
#endif
}
}
int PICNIC_CALLING_CONVENTION picnic_verify(const picnic_publickey_t* pk, const uint8_t* message,
size_t message_len, const uint8_t* signature,
size_t signature_len) {
if (!pk || !signature || !signature_len) {
return -1;
}
const picnic_params_t param = pk->data[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const uint8_t* pk_c = PK_C(pk);
const uint8_t* pk_pt = PK_PT(pk, instance);
if (param == Picnic3_L1 || param == Picnic3_L3 || param == Picnic3_L5) {
#if defined(WITH_KKW)
return impl_verify_picnic3(instance, pk_pt, pk_c, message, message_len, signature,
signature_len);
#else
return -1;
#endif
} else {
#if defined(WITH_ZKBPP)
picnic_context_t context;
mzd_from_char_array(context.m_plaintext, pk_pt, instance->input_output_size);
mzd_from_char_array(context.m_key, pk_c, instance->input_output_size);
context.plaintext = pk_pt;
context.private_key = NULL;
context.public_key = pk_c;
context.msg = message;
context.msglen = message_len;
#if defined(WITH_UNRUH)
context.unruh = picnic_instance_is_unruh(param);
#endif
return picnic_impl_verify(instance, &context, signature, signature_len);
#else
return -1;
#endif
}
}
const char* PICNIC_CALLING_CONVENTION picnic_get_param_name(picnic_params_t parameters) {
switch (parameters) {
case Picnic_L1_FS:
return "Picnic_L1_FS";
case Picnic_L1_UR:
return "Picnic_L1_UR";
case Picnic_L3_FS:
return "Picnic_L3_FS";
case Picnic_L3_UR:
return "Picnic_L3_UR";
case Picnic_L5_FS:
return "Picnic_L5_FS";
case Picnic_L5_UR:
return "Picnic_L5_UR";
case Picnic3_L1:
return "Picnic3_L1";
case Picnic3_L3:
return "Picnic3_L3";
case Picnic3_L5:
return "Picnic3_L5";
case Picnic_L1_full:
return "Picnic_L1_full";
case Picnic_L3_full:
return "Picnic_L3_full";
case Picnic_L5_full:
return "Picnic_L5_full";
default:
return "Unknown parameter set";
}
}
int PICNIC_CALLING_CONVENTION picnic_write_public_key(const picnic_publickey_t* key, uint8_t* buf,
size_t buflen) {
if (!key || !buf) {
return -1;
}
const picnic_params_t param = key->data[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const size_t bytes_required = 1 + 2 * instance->input_output_size;
if (buflen < bytes_required) {
return -1;
}
memcpy(buf, key->data, bytes_required);
return (int)bytes_required;
}
int PICNIC_CALLING_CONVENTION picnic_read_public_key(picnic_publickey_t* key, const uint8_t* buf,
size_t buflen) {
if (!key || !buf || buflen < 1) {
return -1;
}
const picnic_params_t param = buf[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const size_t bytes_required = 1 + 2 * instance->input_output_size;
if (buflen < bytes_required) {
return -1;
}
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_255_255_4)
if (instance->lowmc.n & 0x7) {
const unsigned int diff = instance->input_output_size * 8 - instance->lowmc.n;
if (check_padding_bits(buf[1 + instance->input_output_size - 1], diff) ||
check_padding_bits(buf[1 + 2 * instance->input_output_size - 1], diff)) {
return -1;
}
}
#endif
memcpy(key->data, buf, bytes_required);
return 0;
}
int PICNIC_CALLING_CONVENTION picnic_write_private_key(const picnic_privatekey_t* key, uint8_t* buf,
size_t buflen) {
if (!key || !buf) {
return -1;
}
const picnic_params_t param = key->data[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const size_t bytes_required = 1 + 3 * instance->input_output_size;
if (buflen < bytes_required) {
return -1;
}
memcpy(buf, &key->data, bytes_required);
return (int)bytes_required;
}
int PICNIC_CALLING_CONVENTION picnic_read_private_key(picnic_privatekey_t* key, const uint8_t* buf,
size_t buflen) {
if (!key || !buf || buflen < 1) {
return -1;
}
const picnic_params_t param = buf[0];
const picnic_instance_t* instance = picnic_instance_get(param);
if (!instance) {
return -1;
}
const size_t bytes_required = 1 + 3 * instance->input_output_size;
if (buflen < bytes_required) {
return -1;
}
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_255_255_4)
if (instance->lowmc.n & 0x7) {
const unsigned int diff = instance->input_output_size * 8 - instance->lowmc.n;
/* sanity check of public data: padding bits need to be 0 */
const int check = check_padding_bits(buf[1 + instance->input_output_size - 1], diff) |
check_padding_bits(buf[1 + 2 * instance->input_output_size - 1], diff) |
check_padding_bits(buf[1 + 3 * instance->input_output_size - 1], diff);
picnic_declassify(&check, sizeof(check));
if (check) {
return -1;
}
}
#endif
memcpy(key->data, buf, bytes_required);
return 0;
}
void PICNIC_CALLING_CONVENTION picnic_clear_private_key(picnic_privatekey_t* key) {
picnic_explicit_bzero(key, sizeof(picnic_privatekey_t));
}
picnic_params_t PICNIC_CALLING_CONVENTION
picnic_get_private_key_param(const picnic_privatekey_t* privatekey) {
if (!privatekey) {
return PARAMETER_SET_INVALID;
}
const picnic_params_t param = privatekey->data[0];
return picnic_instance_get(param) ? param : PARAMETER_SET_INVALID;
}
picnic_params_t PICNIC_CALLING_CONVENTION
picnic_get_public_key_param(const picnic_publickey_t* publickey) {
if (!publickey) {
return PARAMETER_SET_INVALID;
}
const picnic_params_t param = publickey->data[0];
return picnic_instance_get(param) ? param : PARAMETER_SET_INVALID;
}