forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheccsi.c
More file actions
2293 lines (2061 loc) · 67.7 KB
/
eccsi.c
File metadata and controls
2293 lines (2061 loc) · 67.7 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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eccsi.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#ifdef WOLFCRYPT_HAVE_ECCSI
#include <wolfssl/wolfcrypt/eccsi.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#ifdef WOLFSSL_HAVE_SP_ECC
#include <wolfssl/wolfcrypt/sp.h>
#endif
#if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && !defined(WOLFSSL_SP_ASM)
/* force off unneeded vector register save/restore. */
#undef SAVE_VECTOR_REGISTERS
#define SAVE_VECTOR_REGISTERS(fail_clause) SAVE_NO_VECTOR_REGISTERS(fail_clause)
#undef RESTORE_VECTOR_REGISTERS
#define RESTORE_VECTOR_REGISTERS() RESTORE_NO_VECTOR_REGISTERS()
#endif
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
/* FIPS build has replaced ecc.h. */
#define wc_ecc_key_get_priv(key) (&((key)->k))
#define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
#endif
/**
* Initialize the components of the ECCSI key and use the specified curve.
*
* Must be called before performing any operations.
* Free the ECCSI key with wc_FreeEccsiKey() when no longer needed.
*
* @param [in] key ECCSI key to initialize.
* @param [in] heap Heap hint.
* @param [in] devId Device identifier.
* Use INVALID_DEVID when no device used.
* @return 0 on success.
* @return BAD_FUNC_ARG when key is NULL.
* @return MEMORY_E when dynamic memory allocation fails.
*/
int wc_InitEccsiKey_ex(EccsiKey* key, int keySz, int curveId, void* heap,
int devId)
{
int err = 0;
EccsiKeyParams* params = NULL;
if (key == NULL) {
err = BAD_FUNC_ARG;
}
if (err == 0) {
XMEMSET(key, 0, sizeof(*key));
key->heap = heap;
params = &key->params;
err = wc_ecc_init_ex(&key->ecc, heap, devId);
}
if (err == 0) {
err = wc_ecc_init_ex(&key->pubkey, heap, devId);
}
if (err == 0) {
key->pvt = wc_ecc_new_point_h(heap);
if (key->pvt == NULL) {
err = MEMORY_E;
}
}
if (err == 0) {
err = mp_init_multi(¶ms->order,
#ifdef WOLFCRYPT_ECCSI_CLIENT
¶ms->a, ¶ms->b, ¶ms->prime, &key->tmp, &key->ssk
#else
NULL, NULL, NULL, NULL, NULL
#endif
);
}
if (err == 0) {
err = wc_ecc_set_curve(&key->ecc, keySz, curveId);
}
if (err == 0) {
err = wc_ecc_set_curve(&key->pubkey, keySz, curveId);
}
if (err != 0) {
wc_FreeEccsiKey(key);
}
return err;
}
/**
* Initialize the components of the ECCSI key.
* Default curve used: NIST_P256 (ECC_SECP256R1)
*
* Must be called before performing any operations.
* Free the ECCSI key with wc_FreeEccsiKey() when no longer needed.
*
* @param [in] key ECCSI key to initialize.
* @param [in] heap Heap hint.
* @param [in] devId Device identifier.
* Use INVALID_DEVID when no device used.
* @return 0 on success.
* @return BAD_FUNC_ARG when key is NULL.
* @return MEMORY_E when dynamic memory allocation fails.
*/
int wc_InitEccsiKey(EccsiKey* key, void* heap, int devId)
{
return wc_InitEccsiKey_ex(key, 32, ECC_SECP256R1, heap, devId);
}
/**
* Frees memory associated with components of the ECCIS key.
*
* Must be called when finished with the ECCIS key.
*
* @param [in] key ECCIS key.
*/
void wc_FreeEccsiKey(EccsiKey* key)
{
if (key != NULL) {
EccsiKeyParams* params = &key->params;
wc_ecc_del_point_h(params->base, key->heap);
#ifdef WOLFCRYPT_ECCSI_CLIENT
mp_free(&key->ssk);
mp_free(&key->tmp);
mp_free(¶ms->prime);
mp_free(¶ms->b);
mp_free(¶ms->a);
#endif
mp_free(¶ms->order);
wc_ecc_del_point_h(key->pvt, key->heap);
wc_ecc_free(&key->pubkey);
wc_ecc_free(&key->ecc);
XMEMSET(key, 0, sizeof(*key));
}
}
/*
* Order, as a hex string in the ECC object, loaded into mp_int in key.
* Flags that the order is available so it isn't loaded multiple times.
*
* @param [in] key ECCSI key.
* @return 0 on success.
* @return MEMORY_E when dynamic memory allocation fails.
*/
static int eccsi_load_order(EccsiKey* key)
{
int err = 0;
if (!key->params.haveOrder) {
err = mp_read_radix(&key->params.order, key->ecc.dp->order,
MP_RADIX_HEX);
if (err == 0) {
key->params.haveOrder = 1;
}
}
return err;
}
#ifdef WOLFCRYPT_ECCSI_CLIENT
/*
* Parameters, as a hex strings in the ECC object, loaded into mp_ints in key.
*
* Parameters loaded: order, A, B, prime.
* Flags that each parameter is available so they aren't loaded multiple times.
*
* @param [in] key ECCSI key.
* @return 0 on success.
* @return MEMORY_E when dynamic memory allocation fails.
*/
static int eccsi_load_ecc_params(EccsiKey* key)
{
int err = 0;
EccsiKeyParams* params = &key->params;
err = eccsi_load_order(key);
if ((err == 0) && (!params->haveA)) {
err = mp_read_radix(¶ms->a, key->ecc.dp->Af, MP_RADIX_HEX);
if (err == 0) {
params->haveA = 1;
}
}
if ((err == 0) && (!params->haveB)) {
err = mp_read_radix(¶ms->b, key->ecc.dp->Bf, MP_RADIX_HEX);
if (err == 0) {
params->haveB = 1;
}
}
if ((err == 0) && (!params->havePrime)) {
err = mp_read_radix(¶ms->prime, key->ecc.dp->prime, MP_RADIX_HEX);
if (err == 0) {
params->havePrime = 1;
}
}
return err;
}
#endif /* WOLFCRYPT_ECCSI_CLIENT */
/*
* Get the base point, hex encoded in the ECC object, as an ecc_point.
*
* Flags that base is available so it isn't loaded multiple times.
* @param [in] key ECCSI key.
* @param [out] base Base point of curve.
* @return 0 on success.
* @return MEMORY_E when dynamic memory allocation fails.
*/
static int eccsi_load_base(EccsiKey* key)
{
int err = 0;
EccsiKeyParams* params = &key->params;
if (!params->haveBase) {
if (params->base == NULL) {
params->base = wc_ecc_new_point_h(key->heap);
if (params->base == NULL) {
err = MEMORY_E;
}
}
if (err == 0) {
err = mp_read_radix(params->base->x, key->ecc.dp->Gx, MP_RADIX_HEX);
}
if (err == 0) {
err = mp_read_radix(params->base->y, key->ecc.dp->Gy, MP_RADIX_HEX);
}
if (err == 0) {
err = mp_set(params->base->z, 1);
}
if (err == 0) {
params->haveBase = 1;
}
}
return err;
}
/*
* Encode the base point of the curve.
*
* Base point is hex encoded in the ECC object or cached as an ECC point from
* previous load calls.
*
* @param [in] key ECCSI key.
* @param [out] data Buffer to encode base point into.
* @param [out] dataSz Length of base point in bytes.
* @return 0 on success.
* @return MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
static int eccsi_encode_base(EccsiKey* key, byte* data, word32* dataSz)
{
int err;
int idx = wc_ecc_get_curve_idx(key->ecc.dp->id);
err = eccsi_load_base(key);
if (err == 0) {
err = wc_ecc_export_point_der(idx, key->params.base, data, dataSz);
}
return err;
}
#ifndef WOLFSSL_HAVE_SP_ECC
/*
* Convert the KPAK to montgomery form.
*
* The KPAK is needed in Montgomery form for verification.
*
* @param [in] key ECCSI key.
* @return 0 on success.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
static int eccsi_kpak_to_mont(EccsiKey* key)
{
int err = 0;
ecc_point* kpak = &key->ecc.pubkey;
mp_int* mu = &key->tmp;
mp_int* prime = &key->params.prime;
if (!key->kpakMont) {
err = mp_montgomery_calc_normalization(mu, prime);
if (err == 0) {
err = mp_mulmod(kpak->x, mu, prime, kpak->x);
}
if (err == 0) {
err = mp_mulmod(kpak->y, mu, prime, kpak->y);
}
if (err == 0) {
err = mp_mulmod(kpak->z, mu, prime, kpak->z);
}
if (err == 0) {
key->kpakMont = 1;
}
}
return err;
}
#endif
/*
* Convert the KPAK from montgomery form.
*
* The KPAK is needed in Montgomery form for verification.
*
* @param [in] key ECCSI key.
* @return 0 on success.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
static int eccsi_kpak_from_mont(EccsiKey* key)
{
int err = 0;
ecc_point* kpak = &key->ecc.pubkey;
mp_digit mp;
mp_int* prime = &key->params.prime;
if (key->kpakMont) {
err = mp_montgomery_setup(prime, &mp);
if (err == 0) {
err = mp_montgomery_reduce(kpak->x, prime, mp);
}
if (err == 0) {
err = mp_montgomery_reduce(kpak->y, prime, mp);
}
if (err == 0) {
err = mp_montgomery_reduce(kpak->z, prime, mp);
}
if (err == 0) {
key->kpakMont = 0;
}
}
return err;
}
/*
* Compute HS = hash( G | KPAK | ID | PVT )
*
* Use when making a (SSK,PVT) pair, signing and verifying.
*
* @param [in] key ECCSI key.
* @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
* @param [in] id Identity to create hash from.
* @param [in] idSz Length of identity in bytes.
* @param [in] pvt Public Validation Token (PVT) as an ECC point.
* @param [out] hash Buffer to hold hash data.
* @param [out] hashSz Length of hash data in bytes.
* @return 0 on success.
* @return MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
static int eccsi_compute_hs(EccsiKey* key, enum wc_HashType hashType,
const byte* id, word32 idSz, ecc_point* pvt, byte* hash, byte* hashSz)
{
int err;
word32 dataSz = 0;
int idx = wc_ecc_get_curve_idx(key->ecc.dp->id);
ecc_point* kpak = &key->ecc.pubkey;
int hash_inited = 0;
/* HS = hash( G | KPAK | ID | PVT ) */
err = wc_HashInit_ex(&key->hash, hashType, key->heap, INVALID_DEVID);
if (err == 0) {
hash_inited = 1;
/* Base Point - G */
dataSz = sizeof(key->data);
err = eccsi_encode_base(key, key->data, &dataSz);
}
if (err == 0) {
err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
}
if (err == 0) {
err = eccsi_kpak_from_mont(key);
}
if (err == 0) {
dataSz = sizeof(key->data);
/* KPAK - public key */
err = wc_ecc_export_point_der(idx, kpak, key->data, &dataSz);
}
if (err == 0) {
err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
}
if (err == 0) {
/* Id - Signer's ID */
err = wc_HashUpdate(&key->hash, hashType, id, idSz);
}
if (err == 0) {
dataSz = sizeof(key->data);
/* PVT - Public Validation Token */
err = wc_ecc_export_point_der(idx, pvt, key->data, &dataSz);
}
if (err == 0) {
/* PVT - Public Validation Token */
err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
}
if (err == 0) {
err = wc_HashFinal(&key->hash, hashType, hash);
}
if (err == 0) {
*hashSz = (byte)wc_HashGetDigestSize(hashType);
}
if (hash_inited) {
(void)wc_HashFree(&key->hash, hashType);
}
return err;
}
#ifdef WOLFCRYPT_ECCSI_KMS
/**
* Generate KMS Secret Auth Key (KSAK) and KMS Public Auth Key (KPAK).
*
* RFC 6507, Section 4.2
*
* Called when establishing a new KMS.\n
* KSAK must be kept secret while KPAK is required by clients for signing
* and verifying.\n
* Export key using wc_ExportEccsiKey(), once generated, to reuse the key.\n
* Export KPAK using wc_ExportEccsiPublicKey(), once generate to send to
* clients.
*
* Creates a random private key and multiplies it by the base point to calculate
* the public key.
*
* @param [in] key ECCSI key.
* @param [in] rng Random number generator.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or rng is NULL.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
int wc_MakeEccsiKey(EccsiKey* key, WC_RNG* rng)
{
int err = 0;
if ((key == NULL) || (rng == NULL)) {
err = BAD_FUNC_ARG;
}
if (err == 0) {
err = wc_ecc_make_key_ex(rng, key->ecc.dp->size, &key->ecc,
key->ecc.dp->id);
}
return err;
}
/*
* Encode a point into a buffer.
*
* X and y ordinate of point concatenated. Each number is zero padded tosize.
* Descriptor byte (0x04) is prepended when not raw.
*
* @param [in] point ECC point to encode.
* @param [in] size Size of prime in bytes - maximum ordinate length.
* @param [out] data Buffer to hold encoded data.
* NULL when needing length of encoded data.
* @param [in,out] sz In, the size of the buffer in bytes.
* Out, the size of the encoded data in bytes.
* @param [in] raw On 0, prepend descriptor byte.
* On 1, only include ordinates.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or sz is NULL.
* @return LENGTH_ONLY_E when data is NULL - sz will hold the size in bytes of
* the encoded data.
* @return BUFFER_E when size of buffer is too small.
*/
static int eccsi_encode_point(ecc_point* point, word32 size, byte* data,
word32* sz, int raw)
{
int err = 0;
if (data == NULL) {
*sz = size * 2 + !raw;
err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
}
if ((err == 0) && (*sz < size * 2 + !raw)) {
err = BUFFER_E;
}
if (err == 0) {
if (!raw) {
data[0] = 0x04;
data++;
}
/* Write out the point's x ordinate into key size bytes. */
err = mp_to_unsigned_bin_len(point->x, data, (int)size);
}
if (err == 0) {
data += size;
/* Write out the point's y ordinate into key size bytes. */
err = mp_to_unsigned_bin_len(point->y, data, (int)size);
}
if (err == 0) {
*sz = size * 2 + !raw;
}
return err;
}
/*
* Decode the data into an ECC point.
*
* X and y ordinate of point concatenated. Each number is zero padded to
* key size. Supports prepended descriptor byte (0x04).
*
* @param [out] point ECC point to encode.
* @param [in] size Size of prime in bytes - maximum ordinate length.
* @param [in] data Encoded public key.
* @param [in] sz Size of the encoded public key in bytes.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or z is NULL.
* @return BUFFER_E when size of data is not equal to the expected size.
* @return ASN_PARSE_E when format byte is invalid.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
*/
static int eccsi_decode_point(ecc_point* point, word32 size, const byte* data,
word32 sz)
{
int err = 0;
if ((sz != size * 2) && (sz != size * 2 + 1)) {
err = BUFFER_E;
}
if ((err == 0) && (sz & 1)) {
if (data[0] != 0x04) {
err = ASN_PARSE_E;
}
data++;
}
if (err == 0) {
/* Read the public key point's x value from key size bytes. */
err = mp_read_unsigned_bin(point->x, data, size);
}
if (err == 0) {
data += size;
/* Read the public key point's y value from key size bytes. */
err = mp_read_unsigned_bin(point->y, data, size);
}
if (err == 0) {
err = mp_set(point->z, 1);
}
return err;
}
/*
* Encode the ECCSI key.
*
* Encodes the private key as big-endian bytes of fixed length.
* Encodes the public key x and y ordinates as big-endian bytes of fixed length.
*
* @param [in] key ECCSI key.
* @param [out] data Buffer to hold encoded ECCSI key.
* @return 0 on success.
* @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
*/
static int eccsi_encode_key(EccsiKey* key, byte* data)
{
int err;
word32 sz = (word32)key->ecc.dp->size * 2;
/* Write out the secret value into key size bytes. */
err = mp_to_unsigned_bin_len(wc_ecc_key_get_priv(&key->ecc), data,
key->ecc.dp->size);
if (err == 0) {
data += key->ecc.dp->size;
/* Write the public key. */
err = eccsi_encode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
data, &sz, 1);
}
return err;
}
/**
* Export the ECCSI key as encoded public/private ECC key.
*
* Use when saving the KMS key pair.
*
* Private key, x ordinate of public key and y ordinate of public key
* concatenated. Each number is zero padded to key size.
*
* @param [in] key ECCSI key.
* @param [out] data Buffer to hold encoded ECCSI key.
* NULL when requesting required length.
* @param [in,out] sz On in, size of buffer in bytes.
* On out, size of encoded ECCSI key in bytes.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or sz is NULL
* @return BAD_STATE_E when no key to export.
* @return LENGTH_ONLY_E when data is NULL - sz is set.
* @return BUFFER_E when the buffer passed in is too small.
* @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
*/
int wc_ExportEccsiKey(EccsiKey* key, byte* data, word32* sz)
{
int err = 0;
if ((key == NULL) || (sz == NULL)) {
err = BAD_FUNC_ARG;
}
if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
err = BAD_STATE_E;
}
if (err == 0) {
if (data == NULL) {
*sz = (word32)(key->ecc.dp->size * 3);
err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
}
else if (*sz < (word32)key->ecc.dp->size * 3) {
err = BUFFER_E;
}
else {
*sz = (word32)(key->ecc.dp->size * 3);
}
}
if (err == 0) {
err = eccsi_kpak_from_mont(key);
}
if (err == 0) {
/* Encode key */
err = eccsi_encode_key(key, data);
}
return err;
}
/*
* Import the ECCSI key as encoded public/private ECC key.
*
* Decodes the private key as big-endian bytes of fixed length.
* Decodes the public key x and y ordinates as big-endian bytes of fixed length.
*
* @param [in] key ECCSI key.
* @param [in] data Buffer holding encoded ECCSI key.
* @return 0 on success.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
*/
static int eccsi_decode_key(EccsiKey* key, const byte* data)
{
int err;
/* Read the secret value from key size bytes. */
err = mp_read_unsigned_bin(wc_ecc_key_get_priv(&key->ecc), data,
(word32)key->ecc.dp->size);
if (err == 0) {
data += key->ecc.dp->size;
/* Read public key. */
err = eccsi_decode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
data, (word32)(key->ecc.dp->size * 2));
}
return err;
}
/**
* Import the ECCSI key as encoded public/private ECC key.
*
* Use when restoring the KMS key pair.
*
* Private key, x ordinate of public key and y ordinate of public key
* concatenated. Each number is zero padded to key size.
*
* @param [in] key ECCSI key.
* @param [in] data Buffer holding encoded ECCSI key.
* @param [in] sz Size of encoded ECCSI key in bytes.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or data is NULL.
* @return BUFFER_E when size of data is not equal to the expected size.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
*/
int wc_ImportEccsiKey(EccsiKey* key, const byte* data, word32 sz)
{
int err = 0;
if ((key == NULL) || (data == NULL)) {
err = BAD_FUNC_ARG;
}
if ((err == 0) && (sz != (word32)key->ecc.dp->size * 3)) {
err = BUFFER_E;
}
if (err == 0) {
key->kpakMont = 0;
/* Decode key */
err = eccsi_decode_key(key, data);
}
if (err == 0) {
key->ecc.type = ECC_PRIVATEKEY;
}
return err;
}
/**
* Export the ECCSI private key.
*
* Use when saving the KMS key.
*
* Private key is zero padded to key size.
*
* @param [in] key ECCSI key.
* @param [out] data Buffer to hold encoded ECCSI private key.
* NULL when requesting required length.
* @param [in,out] sz On in, size of buffer in bytes.
* On out, size of encoded ECCSI private key in bytes.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or sz is NULL
* @return BAD_STATE_E when no key to export.
* @return LENGTH_ONLY_E when data is NULL - sz is set.
* @return BUFFER_E when the buffer passed in is too small.
* @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
*/
int wc_ExportEccsiPrivateKey(EccsiKey* key, byte* data, word32* sz)
{
int err = 0;
if ((key == NULL) || (sz == NULL)) {
err = BAD_FUNC_ARG;
}
if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
err = BAD_STATE_E;
}
if (err == 0) {
if (data == NULL) {
*sz = (word32)key->ecc.dp->size;
err = WC_NO_ERR_TRACE(LENGTH_ONLY_E);
}
else if (*sz < (word32)key->ecc.dp->size) {
err = BUFFER_E;
}
else {
*sz = (word32)key->ecc.dp->size;
}
}
if (err == 0) {
err = mp_to_unsigned_bin_len(wc_ecc_key_get_priv(&key->ecc), data,
key->ecc.dp->size);
}
return err;
}
/**
* Import the ECCSI private key.
*
* Use when restoring the KMS key pair.
*
* Private key is zero padded to key size.
*
* @param [in] key ECCSI key.
* @param [in] data Buffer holding encoded ECCSI private key.
* @param [in] sz Size of encoded ECCSI private key in bytes.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or data is NULL.
* @return BUFFER_E when size of data is not equal to the expected size.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
*/
int wc_ImportEccsiPrivateKey(EccsiKey* key, const byte* data, word32 sz)
{
int err = 0;
if ((key == NULL) || (data == NULL)) {
err = BAD_FUNC_ARG;
}
if ((err == 0) && (sz != (word32)key->ecc.dp->size)) {
err = BUFFER_E;
}
if (err == 0) {
err = mp_read_unsigned_bin(wc_ecc_key_get_priv(&key->ecc), data,
(word32)key->ecc.dp->size);
}
return err;
}
/**
* Export the KMS Public Auth Key (KPAK) from the ECCSI object.
*
* KPAK is required by all clients in order to perform cryptographic operations.
*
* X and y ordinate of public key concatenated. Each number is zero padded to
* key size.
* Descriptor byte (0x04) is prepended when not raw.
*
* @param [in] key ECCSI key.
* @param [out] data Buffer to hold the encoded public key.
* @param [in,out] sz On in, size of buffer in bytes.
* On out, length of encoded public key in bytes.
* @param [in] raw On 0, prepend descriptor byte.
* On 1, only include ordinates.
* @return 0 on success.
* @return BAD_FUNC_ARG when key or sz is NULL.
* @return LENGTH_ONLY_E when data is NULL - sz is set.
* @return BUFFER_E when the buffer passed in is too small.
*/
int wc_ExportEccsiPublicKey(EccsiKey* key, byte* data, word32* sz, int raw)
{
int err = 0;
if ((key == NULL) || (sz == NULL)) {
err = BAD_FUNC_ARG;
}
if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
(key->ecc.type != ECC_PUBLICKEY)) {
err = BAD_STATE_E;
}
if ((err == 0) && (data != NULL)) {
err = eccsi_kpak_from_mont(key);
}
if (err == 0) {
/* Write out public key. */
err = eccsi_encode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
data, sz, raw);
}
return err;
}
/*
* Generates an (SSK, PVT) Pair - signing key pair.
*
* RFC 6507, Section 5.1.1
*
* @param [in] key ECCSI key.
* @param [in] rng Random number generator.
* @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
* @param [in] id Identity to create hash from.
* @param [in] idSz Length of identity in bytes.
* @param [out] ssk Secret Signing Key as an MP integer.
* @param [out] pvt Public Validation Token (PVT) as an ECC point.
* @return 0 on success.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
static int eccsi_make_pair(EccsiKey* key, WC_RNG* rng,
enum wc_HashType hashType, const byte* id, word32 idSz, mp_int* ssk,
ecc_point* pvt)
{
int err = 0;
byte hashSz = 0;
int genTryCnt = 0;
do {
/* Don't infinitely make pairs when random number generator fails. */
if ((++genTryCnt) > ECCSI_MAX_GEN_COUNT) {
err = RNG_FAILURE_E;
}
if (err == 0) {
wc_ecc_free(&key->pubkey);
/* Step 1 and 2: Generate ephemeral key - v, PVT = [v]G */
err = wc_ecc_make_key_ex(rng, key->ecc.dp->size, &key->pubkey,
key->ecc.dp->id);
}
if (err == 0) {
err = wc_ecc_copy_point(&key->pubkey.pubkey, pvt);
}
/* Step 3: Compute HS */
if (err == 0) {
hashSz = (byte)sizeof(key->data);
err = eccsi_compute_hs(key, hashType, id, idSz, pvt, key->data,
&hashSz);
}
/* Step 4: Compute SSK = ( KSAK + HS * v ) modulo q */
if (err == 0) {
err = mp_read_unsigned_bin(ssk, key->data, hashSz);
}
if (err == 0) {
err = mp_mulmod(ssk, wc_ecc_key_get_priv(&key->pubkey),
&key->params.order, ssk);
}
if (err == 0) {
err = mp_addmod(ssk, wc_ecc_key_get_priv(&key->ecc),
&key->params.order, ssk);
}
}
while ((err == 0) && (mp_iszero(ssk) ||
(mp_cmp(ssk, wc_ecc_key_get_priv(&key->ecc)) == MP_EQ)));
/* Step 5: ensure SSK and HS are non-zero (code lines above) */
/* Step 6: Copy out SSK (done during calc) and PVT. Erase v */
mp_forcezero(wc_ecc_key_get_priv(&key->pubkey));
return err;
}
/**
* Generates an (SSK, PVT) Pair - signing key pair.
*
* RFC 6507, Section 5.1.1
*
* ID should include information to indicate a revocation date.\n
* SSK must be zeroized after sending to client.\n
* SSK is sent to signing client only.\n
* PVT is sent to all client types.
*
* @param [in] key ECCSI key.
* @param [in] rng Random number generator.
* @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
* @param [in] id Identity to create hash from.
* @param [in] idSz Length of identity in bytes.
* @param [out] ssk Secret Signing Key as an MP integer.
* @param [out] pvt Public Validation Token (PVT) as an ECC point.
* @return 0 on success.
* @return BAD_FUNC_ARG when key, rng, id, ssk or pvt is NULL.
* @return BAD_STATE_E when curve not set (key not set).
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
* @return Other -ve value when an internal operation fails.
*/
int wc_MakeEccsiPair(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
const byte* id, word32 idSz, mp_int* ssk, ecc_point* pvt)
{
int err = 0;
if ((key == NULL) || (rng == NULL) || (id == NULL) || (ssk == NULL) ||
(pvt == NULL)) {
err = BAD_FUNC_ARG;
}
if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
err = BAD_STATE_E;
}
if (err == 0) {
err = eccsi_load_order(key);
}
if (err == 0) {
err = eccsi_make_pair(key, rng, hashType, id, idSz, ssk, pvt);
}
return err;
}
/**
* Encode the SSK and PVT into a buffer.
*
* SSK and PVT required by client signing messages.
*
* @param [in] key ECCSI key.
* @param [in] ssk Secret Signing Key as an MP integer.
* @param [in] pvt Public Validation Token (PVT) as an ECC point.
* @param [out] data Buffer to encode key pair into.
* @param [in,out] sz In, size of buffer in bytes.
* Out, size of encoded pair data in bytes.
* @return 0 on success.
* @return BAD_FUNC_ARG when key, ssk, pvt or sz is NULL.
* @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
* @return LENGTH_ONLY_E when data is NULL - sz is set.
*/
int wc_EncodeEccsiPair(const EccsiKey* key, mp_int* ssk, ecc_point* pvt,
byte* data, word32* sz)
{
int err = 0;