-
Notifications
You must be signed in to change notification settings - Fork 26
/
picnic3_param.c.in
291 lines (233 loc) · 8.2 KB
/
picnic3_param.c.in
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
/*
* 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 "@PARAM_L@.h"
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "io.h"
#include "lowmc.h"
#include "@LOWMC_L@.h"
#include "picnic3_impl.h"
#include "picnic_instances.h"
#include "randomness.h"
#define LOWMC_BLOCK_BITS @LOWMC@_N
#define LOWMC_BLOCK_SZ LOWMC_BLOCK_SIZE_@PARAM@
#define PRIVATE_KEY_SIZE PICNIC_PRIVATE_KEY_SIZE_@PARAM@
#define PUBLIC_KEY_SIZE PICNIC_PUBLIC_KEY_SIZE_@PARAM@
#define SIGNATURE_SIZE PICNIC_SIGNATURE_SIZE_@PARAM@
#define PARAM @PARAM@
// 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[0]
#define SK_C(sk) &(sk)->data[LOWMC_BLOCK_SZ]
#define SK_PT(sk) &(sk)->data[2 * LOWMC_BLOCK_SZ]
#define PK_C(pk) &(pk)->data[0]
#define PK_PT(pk) &(pk)->data[LOWMC_BLOCK_SZ]
size_t PICNIC_CALLING_CONVENTION @PARAM_L@_signature_size(void) {
return SIGNATURE_SIZE;
}
size_t PICNIC_CALLING_CONVENTION @PARAM_L@_get_private_key_size(void) {
return PRIVATE_KEY_SIZE;
}
size_t PICNIC_CALLING_CONVENTION @PARAM_L@_get_public_key_size(void) {
return PUBLIC_KEY_SIZE;
}
const char* PICNIC_CALLING_CONVENTION @PARAM_L@_get_param_name(void) {
return "@PARAM_L@";
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_keygen(@PARAM_L@_publickey_t* pk,
@PARAM_L@_privatekey_t* sk) {
if (!pk || !sk) {
return -1;
}
uint8_t* sk_sk = SK_SK(sk);
uint8_t* sk_pt = SK_PT(sk);
uint8_t* sk_c = SK_C(sk);
// generate private key
// random secret key
if (rand_bits(sk_sk, LOWMC_BLOCK_BITS)) {
return -1;
}
// random plain text
if (rand_bits(sk_pt, LOWMC_BLOCK_BITS)) {
return -1;
}
// encrypt plaintext under secret key
if (@PARAM_L@_sk_to_pk(sk, pk)) {
return -1;
}
// copy ciphertext to secret key
memcpy(sk_c, PK_C(pk), LOWMC_BLOCK_SZ);
return 0;
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_sk_to_pk(const @PARAM_L@_privatekey_t* sk,
@PARAM_L@_publickey_t* pk) {
if (!sk || !pk) {
return -1;
}
const picnic_instance_t* instance = picnic_instance_get(PARAM);
const uint8_t* sk_sk = SK_SK(sk);
uint8_t* pk_c = PK_C(pk);
uint8_t* pk_pt = PK_PT(pk);
const uint8_t* sk_pt = SK_PT(sk);
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, LOWMC_BLOCK_SZ);
mzd_from_char_array(privkey, sk_sk, LOWMC_BLOCK_SZ);
// compute public key
lowmc_compute(&instance->lowmc, privkey, plaintext, ciphertext);
memcpy(pk_pt, sk_pt, LOWMC_BLOCK_SZ);
mzd_to_char_array(pk_c, ciphertext, LOWMC_BLOCK_SZ);
return 0;
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_validate_keypair(const @PARAM_L@_privatekey_t* sk,
const @PARAM_L@_publickey_t* pk) {
if (!sk || !pk) {
return -1;
}
const picnic_instance_t* instance = picnic_instance_get(PARAM);
const uint8_t* sk_sk = SK_SK(sk);
const uint8_t* sk_pt = SK_PT(sk);
const uint8_t* sk_c = SK_C(sk);
const uint8_t* pk_pt = PK_PT(pk);
const uint8_t* pk_c = PK_C(pk);
// check param and plaintext
if (memcmp(sk_pt, pk_pt, LOWMC_BLOCK_SZ) != 0 || memcmp(sk_c, pk_c, LOWMC_BLOCK_SZ) != 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, LOWMC_BLOCK_SZ);
mzd_from_char_array(privkey, sk_sk, LOWMC_BLOCK_SZ);
// compute public key
lowmc_compute(&instance->lowmc, privkey, plaintext, ciphertext);
uint8_t buffer[MAX_LOWMC_BLOCK_SIZE];
mzd_to_char_array(buffer, ciphertext, LOWMC_BLOCK_SZ);
return memcmp(buffer, pk_c, LOWMC_BLOCK_SZ);
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_sign(const @PARAM_L@_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_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);
const uint8_t* sk_pt = SK_PT(sk);
return impl_sign_picnic3(instance, sk_pt, sk_sk, sk_c, message, message_len, signature,
signature_len);
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_verify(const @PARAM_L@_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_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);
return impl_verify_picnic3(instance, pk_pt, pk_c, message, message_len, signature,
signature_len);
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_write_public_key(const @PARAM_L@_publickey_t* key,
uint8_t* buf, size_t buflen) {
if (!key || !buf) {
return -1;
}
if (buflen < PUBLIC_KEY_SIZE) {
return -1;
}
buf[0] = PARAM;
memcpy(buf + 1, key->data, PUBLIC_KEY_SIZE - 1);
return PUBLIC_KEY_SIZE;
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_read_public_key(@PARAM_L@_publickey_t* key,
const uint8_t* buf, size_t buflen) {
if (!key || !buf || buflen < 1) {
return -1;
}
const picnic_params_t param = buf[0];
if (param != PARAM) {
return -1;
}
const picnic_instance_t* instance = picnic_instance_get(PARAM);
if (!instance) {
return -1;
}
if (buflen < PUBLIC_KEY_SIZE) {
return -1;
}
#if (LOWMC_BLOCK_BITS & 0x7) != 0
static const unsigned int diff = LOWMC_BLOCK_SZ * 8 - LOWMC_BLOCK_BITS;
if (check_padding_bits(buf[1 + LOWMC_BLOCK_SZ - 1], diff) ||
check_padding_bits(buf[1 + 2 * LOWMC_BLOCK_SZ - 1], diff)) {
return -1;
}
#endif
memcpy(key->data, buf + 1, PUBLIC_KEY_SIZE - 1);
return 0;
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_write_private_key(const @PARAM_L@_privatekey_t* key,
uint8_t* buf, size_t buflen) {
if (!key || !buf) {
return -1;
}
if (buflen < PRIVATE_KEY_SIZE) {
return -1;
}
buf[0] = PARAM;
memcpy(buf + 1, &key->data, PRIVATE_KEY_SIZE - 1);
return PRIVATE_KEY_SIZE;
}
int PICNIC_CALLING_CONVENTION @PARAM_L@_read_private_key(@PARAM_L@_privatekey_t* key,
const uint8_t* buf, size_t buflen) {
if (!key || !buf || buflen < 1) {
return -1;
}
const picnic_params_t param = buf[0];
if (param != PARAM) {
return -1;
}
const picnic_instance_t* instance = picnic_instance_get(PARAM);
if (!instance) {
return -1;
}
if (buflen < PRIVATE_KEY_SIZE) {
return -1;
}
#if (LOWMC_BLOCK_BITS & 0x7) != 0
static const unsigned int diff = LOWMC_BLOCK_SZ * 8 - LOWMC_BLOCK_BITS;
/* sanity check of public data: padding bits need to be 0 */
const int check = check_padding_bits(buf[1 + LOWMC_BLOCK_SZ - 1], diff) |
check_padding_bits(buf[1 + 2 * LOWMC_BLOCK_SZ - 1], diff) |
check_padding_bits(buf[1 + 3 * LOWMC_BLOCK_SZ - 1], diff);
picnic_declassify(&check, sizeof(check));
if (check) {
return -1;
}
#endif
memcpy(key->data, buf + 1, PRIVATE_KEY_SIZE - 1);
return 0;
}
void PICNIC_CALLING_CONVENTION @PARAM_L@_clear_private_key(@PARAM_L@_privatekey_t* key) {
picnic_explicit_bzero(key, sizeof(*key));
}