forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathec_key.c
611 lines (502 loc) · 17.4 KB
/
ec_key.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "ec_key.h"
#include "sig_algs.h"
#include "../curves/curves.h"
/*
* Check if given private key 'A' has been initialized. Returns 0 on success,
* -1 on error
*/
int priv_key_check_initialized(const ec_priv_key *A)
{
int ret = 0;
MUST_HAVE(((A != NULL) && (A->magic == PRIV_KEY_MAGIC) && (A->params != NULL)), ret, err);
err:
return ret;
}
/*
* Same as previous but also verifies that the signature algorithm type does
* match the one passed using 'alg_type'. Returns 0 on success, -1 on error.
*/
int priv_key_check_initialized_and_type(const ec_priv_key *A,
ec_alg_type alg_type)
{
int ret = 0;
MUST_HAVE(((A != NULL) && (A->magic == PRIV_KEY_MAGIC) &&
(A->params != NULL) && (A->key_type == alg_type)), ret, err);
err:
return ret;
}
/*
* Import a private key from a buffer with known EC parameters and algorithm
* Note that no sanity check is performed by the function to verify key
* is valid for params. Also note that no deep copy of pointed params is
* performed. The function returns 0 on success, -1 on error.
*/
int ec_priv_key_import_from_buf(ec_priv_key *priv_key,
const ec_params *params,
const u8 *priv_key_buf, u8 priv_key_buf_len,
ec_alg_type ec_key_alg)
{
int ret;
MUST_HAVE((priv_key != NULL), ret, err);
ret = nn_init_from_buf(&(priv_key->x), priv_key_buf, priv_key_buf_len); EG(ret, err);
/* Set key type and pointer to EC params */
priv_key->key_type = ec_key_alg;
priv_key->params = (const ec_params *)params;
priv_key->magic = PRIV_KEY_MAGIC;
err:
return ret;
}
/*
* Export a private key 'priv_key' to a buffer 'priv_key_buf' of length
* 'priv_key_buf_len'. The function returns 0 on sucess, -1 on error.
*/
int ec_priv_key_export_to_buf(const ec_priv_key *priv_key, u8 *priv_key_buf,
u8 priv_key_buf_len)
{
int ret;
bitcnt_t blen;
ret = priv_key_check_initialized(priv_key); EG(ret, err);
/*
* Check that there is enough room to export our private key without
* losing information.
*/
ret = nn_bitlen(&(priv_key->x), &blen); EG(ret, err);
MUST_HAVE(((8 * (u32)priv_key_buf_len) >= (u32)blen), ret, err);
/* Export our private key */
ret = nn_export_to_buf(priv_key_buf, priv_key_buf_len, &(priv_key->x));
err:
return ret;
}
/*
* Check if given public key 'A' has been initialized. Returns 0 on success,
* -1 on error
*/
int pub_key_check_initialized(const ec_pub_key *A)
{
int ret = 0;
MUST_HAVE(((A != NULL) && (A->magic == PUB_KEY_MAGIC) && (A->params != NULL)), ret, err);
err:
return ret;
}
/*
* Same as previous but also verifies that the signature algorithm type does
* match the one passed using 'alg_type'. Returns 0 on success, -1 on error.
*/
int pub_key_check_initialized_and_type(const ec_pub_key *A,
ec_alg_type alg_type)
{
int ret = 0;
MUST_HAVE(((A != NULL) && (A->magic == PUB_KEY_MAGIC) &&
(A->params != NULL) && (A->key_type == alg_type)), ret, err);
err:
return ret;
}
/*
* Import a public key from a buffer with known EC parameters and algorithm
* Note that no sanity check is performed by the function to verify key
* is valid for params. Also note that no deep copy of pointed params is
* performed. The buffer contains projective point coordinates. The function
* returns 0 on success, -1 on error.
*/
int ec_pub_key_import_from_buf(ec_pub_key *pub_key, const ec_params *params,
const u8 *pub_key_buf, u8 pub_key_buf_len,
ec_alg_type ec_key_alg)
{
int ret, isone, check;
MUST_HAVE(((pub_key != NULL) && (pub_key_buf != NULL) && (params != NULL)), ret, err);
/* Import the projective point */
ret = prj_pt_import_from_buf(&(pub_key->y),
pub_key_buf, pub_key_buf_len,
(ec_shortw_crv_src_t)&(params->ec_curve)); EG(ret, err);
/* If the cofactor of the curve is not 1, we check that
* our public key is indeed in the sub-group generated by
* our generator.
* NOTE: this is indeed a 'costly' operation, but it is necessary
* when we do not trust the public key that is provided, which can
* be the case in some protocols.
*/
ret = nn_isone(&(params->ec_gen_cofactor), &isone); EG(ret, err);
if (!isone) {
ret = check_prj_pt_order(&(pub_key->y), &(params->ec_gen_order), PUBLIC_PT, &check); EG(ret, err);
MUST_HAVE(check, ret, err);
}
/* Set key type and pointer to EC params */
pub_key->key_type = ec_key_alg;
pub_key->params = (const ec_params *)params;
pub_key->magic = PUB_KEY_MAGIC;
err:
return ret;
}
/*
* Import a public key from a buffer with known EC parameters and algorithm
* Note that no sanity check is performed by the function to verify key
* is valid for params. Also note that no deep copy of pointed params is
* performed. The buffer contains affine point coordinates. The function
* returns 0 on success, -1 on error.
*/
int ec_pub_key_import_from_aff_buf(ec_pub_key *pub_key, const ec_params *params,
const u8 *pub_key_buf, u8 pub_key_buf_len,
ec_alg_type ec_key_alg)
{
int ret, isone, check;
MUST_HAVE(((pub_key != NULL) && (pub_key_buf != NULL) && (params != NULL)), ret, err);
/* Import the projective point */
ret = prj_pt_import_from_aff_buf(&(pub_key->y),
pub_key_buf, pub_key_buf_len,
(ec_shortw_crv_src_t)&(params->ec_curve)); EG(ret, err);
/* If the cofactor of the curve is not 1, we check that
* our public key is indeed in the sub-group generated by
* our generator.
* NOTE: this is indeed a 'costly' operation, but it is necessary
* when we do not trust the public key that is provided, which can
* be the case in some protocols.
*/
ret = nn_isone(&(params->ec_gen_cofactor), &isone); EG(ret, err);
if (!isone){
ret = check_prj_pt_order(&(pub_key->y), &(params->ec_gen_order), PUBLIC_PT, &check); EG(ret, err);
MUST_HAVE(check, ret, err);
}
/* Set key type and pointer to EC params */
pub_key->key_type = ec_key_alg;
pub_key->params = (const ec_params *)params;
pub_key->magic = PUB_KEY_MAGIC;
err:
return ret;
}
/*
* Export a public key to a projective point buffer. The function returns 0 on
* success, -1 on error.
*/
int ec_pub_key_export_to_buf(const ec_pub_key *pub_key, u8 *pub_key_buf,
u8 pub_key_buf_len)
{
int ret;
ret = pub_key_check_initialized(pub_key); EG(ret, err);
ret = prj_pt_export_to_buf(&(pub_key->y), pub_key_buf, pub_key_buf_len);
err:
return ret;
}
/*
* Export a public key to an affine point buffer. The function returns 0 on
* success, -1 on error.
*/
int ec_pub_key_export_to_aff_buf(const ec_pub_key *pub_key, u8 *pub_key_buf,
u8 pub_key_buf_len)
{
int ret;
ret = pub_key_check_initialized(pub_key); EG(ret, err);
ret = prj_pt_export_to_aff_buf(&(pub_key->y), pub_key_buf,
pub_key_buf_len);
err:
return ret;
}
/*
* Check if given key pair 'A' has been initialized. Returns 0 on success,
* -1 on error
*/
int key_pair_check_initialized(const ec_key_pair *A)
{
int ret;
MUST_HAVE((A != NULL), ret, err);
ret = priv_key_check_initialized(&A->priv_key); EG(ret, err);
ret = pub_key_check_initialized(&A->pub_key);
err:
return ret;
}
/*
* Same as previous but also verifies that the signature algorithm type does
* match the one passed using 'alg_type'. Returns 0 on success, -1 on error.
*/
int key_pair_check_initialized_and_type(const ec_key_pair *A,
ec_alg_type alg_type)
{
int ret;
MUST_HAVE((A != NULL), ret, err);
ret = priv_key_check_initialized_and_type(&A->priv_key, alg_type); EG(ret, err);
ret = pub_key_check_initialized_and_type(&A->pub_key, alg_type);
err:
return ret;
}
/*
* Import a key pair from a buffer representing the private key. The associated
* public key is computed from the private key. The function returns 0 on
* success, -1 on error.
*/
int ec_key_pair_import_from_priv_key_buf(ec_key_pair *kp,
const ec_params *params,
const u8 *priv_key, u8 priv_key_len,
ec_alg_type ec_key_alg)
{
int ret;
MUST_HAVE((kp != NULL), ret, err);
/* Import private key */
ret = ec_priv_key_import_from_buf(&(kp->priv_key), params, priv_key,
priv_key_len, ec_key_alg); EG(ret, err);
/* Generate associated public key. */
ret = init_pubkey_from_privkey(&(kp->pub_key), &(kp->priv_key));
err:
return ret;
}
/*
* Import a structured private key to buffer. The structure allows some sanity
* checks. The function returns 0 on success, -1 on error.
*/
int ec_structured_priv_key_import_from_buf(ec_priv_key *priv_key,
const ec_params *params,
const u8 *priv_key_buf,
u8 priv_key_buf_len,
ec_alg_type ec_key_alg)
{
u8 metadata_len = (3 * sizeof(u8));
u8 crv_name_len;
u32 len;
int ret;
/* We first pull the metadata, consisting of:
* - One byte = the key type (public or private)
* - One byte = the algorithm type (ECDSA, ECKCDSA, ...)
* - One byte = the curve type (FRP256V1, ...)
*/
MUST_HAVE((priv_key != NULL) && (priv_key_buf != NULL) && (priv_key_buf_len > metadata_len), ret, err);
MUST_HAVE((params != NULL) && (params->curve_name != NULL), ret, err);
/* Pull and check the key type */
MUST_HAVE((EC_PRIVKEY == priv_key_buf[0]), ret, err);
/* Pull and check the algorithm type */
MUST_HAVE((ec_key_alg == priv_key_buf[1]), ret, err);
/* Pull and check the curve type */
ret = local_strlen((const char *)params->curve_name, &len); EG(ret, err);
len += 1;
MUST_HAVE((len < 256), ret, err);
crv_name_len = (u8)len;
ret = ec_check_curve_type_and_name((ec_curve_type) (priv_key_buf[2]),
params->curve_name, crv_name_len); EG(ret, err);
ret = ec_priv_key_import_from_buf(priv_key, params,
priv_key_buf + metadata_len,
(u8)(priv_key_buf_len - metadata_len),
ec_key_alg);
err:
return ret;
}
/*
* Export a structured private key to buffer. The structure allows some sanity
* checks. The function returns 0 on success, -1 on error.
*/
int ec_structured_priv_key_export_to_buf(const ec_priv_key *priv_key,
u8 *priv_key_buf, u8 priv_key_buf_len)
{
u8 metadata_len = (3 * sizeof(u8));
const u8 *curve_name;
u8 curve_name_len;
u32 len;
ec_curve_type curve_type;
int ret;
ret = priv_key_check_initialized(priv_key); EG(ret, err);
MUST_HAVE((priv_key_buf != NULL) && (priv_key_buf_len > metadata_len) && (priv_key->params->curve_name != NULL), ret, err);
/*
* We first put the metadata, consisting on:
* - One byte = the key type (public or private)
* - One byte = the algorithm type (ECDSA, ECKCDSA, ...)
* - One byte = the curve type (FRP256V1, ...)
*/
/* Push the key type */
priv_key_buf[0] = (u8)EC_PRIVKEY;
/* Push the algorithm type */
priv_key_buf[1] = (u8)priv_key->key_type;
/* Push the curve type */
curve_name = priv_key->params->curve_name;
ret = local_strlen((const char *)curve_name, &len); EG(ret, err);
len += 1;
MUST_HAVE((len < 256), ret, err);
curve_name_len = (u8)len;
ret = ec_get_curve_type_by_name(curve_name, curve_name_len, &curve_type); EG(ret, err);
priv_key_buf[2] = (u8)curve_type;
/* Push the raw private key buffer */
ret = ec_priv_key_export_to_buf(priv_key, priv_key_buf + metadata_len,
(u8)(priv_key_buf_len - metadata_len));
err:
return ret;
}
/*
* Import a structured pub key from buffer. The structure allows some sanity
* checks. The function returns 0 on success, -1 on error.
*/
int ec_structured_pub_key_import_from_buf(ec_pub_key *pub_key,
const ec_params *params,
const u8 *pub_key_buf,
u8 pub_key_buf_len,
ec_alg_type ec_key_alg)
{
u8 metadata_len = (3 * sizeof(u8));
u8 crv_name_len;
u32 len;
int ret;
MUST_HAVE((pub_key_buf != NULL) && (pub_key_buf_len > metadata_len), ret, err);
MUST_HAVE((params != NULL) && (params->curve_name != NULL), ret, err);
/*
* We first pull the metadata, consisting of:
* - One byte = the key type (public or private)
* - One byte = the algorithm type (ECDSA, ECKCDSA, ...)
* - One byte = the curve type (FRP256V1, ...)
*/
/* Pull and check the key type */
MUST_HAVE((EC_PUBKEY == pub_key_buf[0]), ret, err);
/* Pull and check the algorithm type */
MUST_HAVE((ec_key_alg == pub_key_buf[1]), ret, err);
/* Pull and check the curve type */
ret = local_strlen((const char *)params->curve_name, &len); EG(ret, err);
len += 1;
MUST_HAVE((len < 256), ret, err);
crv_name_len = (u8)len;
ret = ec_check_curve_type_and_name((ec_curve_type) (pub_key_buf[2]),
params->curve_name, crv_name_len); EG(ret, err);
ret = ec_pub_key_import_from_buf(pub_key, params,
pub_key_buf + metadata_len,
(u8)(pub_key_buf_len - metadata_len),
ec_key_alg);
err:
return ret;
}
/*
* Export a structured pubate key to buffer. The structure allows some sanity
* checks. The function returns 0 on success, -1 on error.
*/
int ec_structured_pub_key_export_to_buf(const ec_pub_key *pub_key,
u8 *pub_key_buf, u8 pub_key_buf_len)
{
u8 metadata_len = (3 * sizeof(u8));
const u8 *curve_name;
u8 curve_name_len;
u32 len;
ec_curve_type curve_type;
int ret;
ret = pub_key_check_initialized(pub_key); EG(ret, err);
MUST_HAVE((pub_key_buf != NULL) && (pub_key_buf_len > metadata_len), ret, err);
MUST_HAVE((pub_key->params->curve_name != NULL), ret, err);
/*
* We first put the metadata, consisting of:
* - One byte = the key type (public or private)
* - One byte = the algorithm type (ECDSA, ECKCDSA, ...)
* - One byte = the curve type (FRP256V1, ...)
*/
/* Push the key type */
pub_key_buf[0] = (u8)EC_PUBKEY;
/* Push the algorithm type */
pub_key_buf[1] = (u8)pub_key->key_type;
/* Push the curve type */
curve_name = pub_key->params->curve_name;
ret = local_strlen((const char *)curve_name, &len); EG(ret, err);
len += 1;
MUST_HAVE((len < 256), ret, err);
curve_name_len = (u8)len;
ret = ec_get_curve_type_by_name(curve_name, curve_name_len, &curve_type); EG(ret, err);
pub_key_buf[2] = (u8)curve_type;
/* Push the raw pub key buffer */
ret = ec_pub_key_export_to_buf(pub_key, pub_key_buf + metadata_len,
(u8)(pub_key_buf_len - metadata_len));
err:
return ret;
}
/*
* Import a key pair from a structured private key buffer. The structure allows
* some sanity checks. The function returns 0 on success, -1 on error.
*/
int ec_structured_key_pair_import_from_priv_key_buf(ec_key_pair *kp,
const ec_params *params,
const u8 *priv_key_buf,
u8 priv_key_buf_len,
ec_alg_type ec_key_alg)
{
u8 metadata_len = (3 * sizeof(u8));
u8 crv_name_len;
u32 len;
int ret;
MUST_HAVE((kp != NULL) && (priv_key_buf != NULL) && (priv_key_buf_len > metadata_len), ret, err);
MUST_HAVE((params != NULL) && (params->curve_name != NULL), ret, err);
/* We first pull the metadata, consisting on:
* - One byte = the key type (public or private)
* - One byte = the algorithm type (ECDSA, ECKCDSA, ...)
* - One byte = the curve type (FRP256V1, ...)
*/
/* Pull and check the key type */
MUST_HAVE((EC_PRIVKEY == priv_key_buf[0]), ret, err);
/* Pull and check the algorithm type */
MUST_HAVE((ec_key_alg == priv_key_buf[1]), ret, err);
/* Pull and check the curve type */
ret = local_strlen((const char *)params->curve_name, &len); EG(ret, err);
len += 1;
MUST_HAVE((len < 256), ret, err);
crv_name_len = (u8)len;
ret = ec_check_curve_type_and_name((ec_curve_type) (priv_key_buf[2]),
params->curve_name, crv_name_len); EG(ret, err);
ret = ec_key_pair_import_from_priv_key_buf(kp, params,
priv_key_buf + metadata_len,
(u8)(priv_key_buf_len - metadata_len),
ec_key_alg);
err:
return ret;
}
/*
* Import a key pair from a two structured key buffer (private and public one)
* The function does not verify the coherency between private and public parts.
* The function returns 0 on success, -1 on error.
*/
int ec_structured_key_pair_import_from_buf(ec_key_pair *kp,
const ec_params *params,
const u8 *priv_key_buf,
u8 priv_key_buf_len,
const u8 *pub_key_buf,
u8 pub_key_buf_len,
ec_alg_type ec_key_alg)
{
int ret;
MUST_HAVE((kp != NULL), ret, err);
ret = ec_structured_pub_key_import_from_buf(&kp->pub_key, params,
pub_key_buf,
pub_key_buf_len,
ec_key_alg); EG(ret, err);
ret = ec_structured_priv_key_import_from_buf(&kp->priv_key, params,
priv_key_buf,
priv_key_buf_len,
ec_key_alg);
err:
return ret;
}
/*
* Generate a public/private key pair for given signature algorithm, using
* given EC params. The function returns 0 on success, -1 on error.
*/
int ec_key_pair_gen(ec_key_pair *kp, const ec_params *params,
ec_alg_type ec_key_alg)
{
int ret;
MUST_HAVE((kp != NULL) && (params != NULL), ret, err);
/* Get a random value in ]0,q[ */
ret = nn_get_random_mod(&(kp->priv_key.x), &(params->ec_gen_order)); EG(ret, err);
/* Set key type and pointer to EC params for private key */
kp->priv_key.key_type = ec_key_alg;
kp->priv_key.params = (const ec_params *)params;
kp->priv_key.magic = PRIV_KEY_MAGIC;
/* Call our private key generation function */
ret = gen_priv_key(&(kp->priv_key)); EG(ret, err);
/* Generate associated public key. */
ret = init_pubkey_from_privkey(&(kp->pub_key), &(kp->priv_key));
err:
if (ret && (kp != NULL)) {
IGNORE_RET_VAL(local_memset(kp, 0, sizeof(ec_key_pair)));
}
return ret;
}