forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpk_ec.c
More file actions
5589 lines (4915 loc) · 159 KB
/
pk_ec.c
File metadata and controls
5589 lines (4915 loc) · 159 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
/* pk_ec.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>
#include <wolfssl/internal.h>
#ifndef WC_NO_RNG
#include <wolfssl/wolfcrypt/random.h>
#endif
#ifdef HAVE_ECC
#include <wolfssl/wolfcrypt/ecc.h>
#ifdef HAVE_SELFTEST
/* point compression types. */
#define ECC_POINT_COMP_EVEN 0x02
#define ECC_POINT_COMP_ODD 0x03
#define ECC_POINT_UNCOMP 0x04
#endif
#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
#if !defined(WOLFSSL_PK_EC_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning pk_ec.c does not need to be compiled separately from ssl.c
#endif
#else
/*******************************************************************************
* START OF EC API
******************************************************************************/
#ifdef HAVE_ECC
#if defined(OPENSSL_EXTRA)
/* Start EC_curve */
/* Get the NIST name for the numeric ID.
*
* @param [in] nid Numeric ID of an EC curve.
* @return String representing NIST name of EC curve on success.
* @return NULL on error.
*/
const char* wolfSSL_EC_curve_nid2nist(int nid)
{
const char* name = NULL;
const WOLF_EC_NIST_NAME* nist_name;
/* Attempt to find the curve info matching the NID passed in. */
for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
if (nist_name->nid == nid) {
/* NID found - return name. */
name = nist_name->name;
break;
}
}
return name;
}
/* Get the numeric ID for the NIST name.
*
* @param [in] name NIST name of EC curve.
* @return NID matching NIST name on success.
* @return 0 on error.
*/
int wolfSSL_EC_curve_nist2nid(const char* name)
{
int nid = 0;
const WOLF_EC_NIST_NAME* nist_name;
/* Attempt to find the curve info matching the NIST name passed in. */
for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
if (XSTRCMP(nist_name->name, name) == 0) {
/* Name found - return NID. */
nid = nist_name->nid;
break;
}
}
return nid;
}
#endif /* OPENSSL_EXTRA */
/* End EC_curve */
/* Start EC_METHOD */
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
/* Get the EC method of the EC group object.
*
* wolfSSL doesn't use method tables. Implementation used is dependent upon
* the NID.
*
* @param [in] group EC group object.
* @return EC method.
*/
const WOLFSSL_EC_METHOD* wolfSSL_EC_GROUP_method_of(
const WOLFSSL_EC_GROUP *group)
{
/* No method table used so just return the same object. */
return group;
}
/* Get field type for method.
*
* Only prime fields are supported.
*
* @param [in] meth EC method.
* @return X9.63 prime field NID on success.
* @return 0 on error.
*/
int wolfSSL_EC_METHOD_get_field_type(const WOLFSSL_EC_METHOD *meth)
{
int nid = 0;
if (meth != NULL) {
/* Only field type supported by code base. */
nid = WC_NID_X9_62_prime_field;
}
return nid;
}
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
/* End EC_METHOD */
/* Start EC_GROUP */
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
/* Converts ECC curve enum values in ecc_curve_id to the associated OpenSSL NID
* value.
*
* @param [in] n ECC curve id.
* @return ECC curve NID (OpenSSL compatible value).
*/
int EccEnumToNID(int n)
{
WOLFSSL_ENTER("EccEnumToNID");
switch(n) {
case ECC_SECP192R1:
return WC_NID_X9_62_prime192v1;
case ECC_PRIME192V2:
return WC_NID_X9_62_prime192v2;
case ECC_PRIME192V3:
return WC_NID_X9_62_prime192v3;
case ECC_PRIME239V1:
return WC_NID_X9_62_prime239v1;
case ECC_PRIME239V2:
return WC_NID_X9_62_prime239v2;
case ECC_PRIME239V3:
return WC_NID_X9_62_prime239v3;
case ECC_SECP256R1:
return WC_NID_X9_62_prime256v1;
case ECC_SECP112R1:
return WC_NID_secp112r1;
case ECC_SECP112R2:
return WC_NID_secp112r2;
case ECC_SECP128R1:
return WC_NID_secp128r1;
case ECC_SECP128R2:
return WC_NID_secp128r2;
case ECC_SECP160R1:
return WC_NID_secp160r1;
case ECC_SECP160R2:
return WC_NID_secp160r2;
case ECC_SECP224R1:
return WC_NID_secp224r1;
case ECC_SECP384R1:
return WC_NID_secp384r1;
case ECC_SECP521R1:
return WC_NID_secp521r1;
case ECC_SECP160K1:
return WC_NID_secp160k1;
case ECC_SECP192K1:
return WC_NID_secp192k1;
case ECC_SECP224K1:
return WC_NID_secp224k1;
case ECC_SECP256K1:
return WC_NID_secp256k1;
case ECC_BRAINPOOLP160R1:
return WC_NID_brainpoolP160r1;
case ECC_BRAINPOOLP192R1:
return WC_NID_brainpoolP192r1;
case ECC_BRAINPOOLP224R1:
return WC_NID_brainpoolP224r1;
case ECC_BRAINPOOLP256R1:
return WC_NID_brainpoolP256r1;
case ECC_BRAINPOOLP320R1:
return WC_NID_brainpoolP320r1;
case ECC_BRAINPOOLP384R1:
return WC_NID_brainpoolP384r1;
case ECC_BRAINPOOLP512R1:
return WC_NID_brainpoolP512r1;
#ifdef WOLFSSL_SM2
case ECC_SM2P256V1:
return WC_NID_sm2;
#endif
default:
WOLFSSL_MSG("NID not found");
return WOLFSSL_FATAL_ERROR;
}
}
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
/* Converts OpenSSL NID of EC curve to the enum value in ecc_curve_id
*
* Used by ecc_sets[].
*
* @param [in] n OpenSSL NID of EC curve.
* @return wolfCrypt EC curve id.
* @return -1 on error.
*/
int NIDToEccEnum(int nid)
{
int id;
WOLFSSL_ENTER("NIDToEccEnum");
switch (nid) {
case WC_NID_X9_62_prime192v1:
id = ECC_SECP192R1;
break;
case WC_NID_X9_62_prime192v2:
id = ECC_PRIME192V2;
break;
case WC_NID_X9_62_prime192v3:
id = ECC_PRIME192V3;
break;
case WC_NID_X9_62_prime239v1:
id = ECC_PRIME239V1;
break;
case WC_NID_X9_62_prime239v2:
id = ECC_PRIME239V2;
break;
case WC_NID_X9_62_prime239v3:
id = ECC_PRIME239V3;
break;
case WC_NID_X9_62_prime256v1:
id = ECC_SECP256R1;
break;
case WC_NID_secp112r1:
id = ECC_SECP112R1;
break;
case WC_NID_secp112r2:
id = ECC_SECP112R2;
break;
case WC_NID_secp128r1:
id = ECC_SECP128R1;
break;
case WC_NID_secp128r2:
id = ECC_SECP128R2;
break;
case WC_NID_secp160r1:
id = ECC_SECP160R1;
break;
case WC_NID_secp160r2:
id = ECC_SECP160R2;
break;
case WC_NID_secp224r1:
id = ECC_SECP224R1;
break;
case WC_NID_secp384r1:
id = ECC_SECP384R1;
break;
case WC_NID_secp521r1:
id = ECC_SECP521R1;
break;
case WC_NID_secp160k1:
id = ECC_SECP160K1;
break;
case WC_NID_secp192k1:
id = ECC_SECP192K1;
break;
case WC_NID_secp224k1:
id = ECC_SECP224K1;
break;
case WC_NID_secp256k1:
id = ECC_SECP256K1;
break;
case WC_NID_brainpoolP160r1:
id = ECC_BRAINPOOLP160R1;
break;
case WC_NID_brainpoolP192r1:
id = ECC_BRAINPOOLP192R1;
break;
case WC_NID_brainpoolP224r1:
id = ECC_BRAINPOOLP224R1;
break;
case WC_NID_brainpoolP256r1:
id = ECC_BRAINPOOLP256R1;
break;
case WC_NID_brainpoolP320r1:
id = ECC_BRAINPOOLP320R1;
break;
case WC_NID_brainpoolP384r1:
id = ECC_BRAINPOOLP384R1;
break;
case WC_NID_brainpoolP512r1:
id = ECC_BRAINPOOLP512R1;
break;
default:
WOLFSSL_MSG("NID not found");
/* -1 on error. */
id = WOLFSSL_FATAL_ERROR;
}
return id;
}
/* Set the fields of the EC group based on numeric ID.
*
* @param [in, out] group EC group.
* @param [in] nid Numeric ID of an EC curve.
*/
static void ec_group_set_nid(WOLFSSL_EC_GROUP* group, int nid)
{
int eccEnum;
int realNid;
/* Convert ecc_curve_id enum to NID. */
if ((realNid = EccEnumToNID(nid)) != -1) {
/* ecc_curve_id enum passed in - have real NID value set. */
eccEnum = nid;
}
else {
/* NID passed in is OpenSSL type. */
realNid = nid;
/* Convert NID to ecc_curve_id enum. */
eccEnum = NIDToEccEnum(nid);
}
/* Set the numeric ID of the curve */
group->curve_nid = realNid;
/* Initialize index to -1 (i.e. wolfCrypt doesn't support curve). */
group->curve_idx = -1;
/* Find index and OID sum for curve if wolfCrypt supports it. */
if (eccEnum != -1) {
int i;
/* Find id and set the internal curve idx and OID sum. */
for (i = 0; ecc_sets[i].size != 0; i++) {
if (ecc_sets[i].id == eccEnum) {
/* Found id in wolfCrypt supported EC curves. */
group->curve_idx = i;
group->curve_oid = (int)ecc_sets[i].oidSum;
break;
}
}
}
}
/* Create a new EC group with the numeric ID for an EC curve.
*
* @param [in] nid Numeric ID of an EC curve.
* @return New, allocated EC group on success.
* @return NULL on error.
*/
WOLFSSL_EC_GROUP* wolfSSL_EC_GROUP_new_by_curve_name(int nid)
{
int err = 0;
WOLFSSL_EC_GROUP* group;
WOLFSSL_ENTER("wolfSSL_EC_GROUP_new_by_curve_name");
/* Allocate EC group. */
group = (WOLFSSL_EC_GROUP*)XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
DYNAMIC_TYPE_ECC);
if (group == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_new_by_curve_name malloc failure");
err = 1;
}
if (!err) {
/* Reset all fields. */
XMEMSET(group, 0, sizeof(WOLFSSL_EC_GROUP));
/* Set the fields of group based on the numeric ID. */
ec_group_set_nid(group, nid);
}
return group;
}
#endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
/* Dispose of the EC group.
*
* Cannot use group after this call.
*
* @param [in] group EC group to free.
*/
void wolfSSL_EC_GROUP_free(WOLFSSL_EC_GROUP *group)
{
WOLFSSL_ENTER("wolfSSL_EC_GROUP_free");
/* Dispose of EC group. */
XFREE(group, NULL, DYNAMIC_TYPE_ECC);
}
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
#ifdef OPENSSL_EXTRA
#ifndef NO_BIO
/* Creates an EC group from the DER encoding.
*
* Only named curves supported.
*
* @param [out] group Reference to EC group object.
* @param [in] in Buffer holding DER encoding of curve.
* @param [in] inSz Length of data in buffer.
* @return EC group on success.
* @return NULL on error.
*/
static WOLFSSL_EC_GROUP* wolfssl_ec_group_d2i(WOLFSSL_EC_GROUP** group,
const unsigned char** in_pp, long inSz)
{
int err = 0;
WOLFSSL_EC_GROUP* ret = NULL;
word32 idx = 0;
word32 oid = 0;
int id = 0;
const unsigned char* in;
if (in_pp == NULL || *in_pp == NULL)
return NULL;
in = *in_pp;
/* Use the group passed in. */
if ((group != NULL) && (*group != NULL)) {
ret = *group;
}
/* Only support named curves. */
if (in[0] != ASN_OBJECT_ID) {
WOLFSSL_ERROR_MSG("Invalid or unsupported encoding");
err = 1;
}
/* Decode the OBJECT ID - expecting an EC curve OID. */
if ((!err) && (GetObjectId(in, &idx, &oid, oidCurveType, (word32)inSz) !=
0)) {
err = 1;
}
if (!err) {
/* Get the internal ID for OID. */
id = wc_ecc_get_oid(oid, NULL, NULL);
if (id < 0) {
err = 1;
}
}
if (!err) {
/* Get the NID for the internal ID. */
int nid = EccEnumToNID(id);
if (ret == NULL) {
/* Create a new EC group with the numeric ID. */
ret = wolfSSL_EC_GROUP_new_by_curve_name(nid);
if (ret == NULL) {
err = 1;
}
}
else {
ec_group_set_nid(ret, nid);
}
}
if ((!err) && (group != NULL)) {
/* Return the EC group through reference. */
*group = ret;
}
if (err) {
if ((ret != NULL) && (ret != *group)) {
wolfSSL_EC_GROUP_free(ret);
}
ret = NULL;
}
else {
*in_pp += idx;
}
return ret;
}
/* Creates a new EC group from the PEM encoding in the BIO.
*
* @param [in] bio BIO to read PEM encoding from.
* @param [out] group Reference to EC group object.
* @param [in] cb Password callback when PEM encrypted.
* @param [in] pass NUL terminated string for passphrase when PEM encrypted.
* @return EC group on success.
* @return NULL on error.
*/
WOLFSSL_EC_GROUP* wolfSSL_PEM_read_bio_ECPKParameters(WOLFSSL_BIO* bio,
WOLFSSL_EC_GROUP** group, wc_pem_password_cb* cb, void* pass)
{
int err = 0;
WOLFSSL_EC_GROUP* ret = NULL;
DerBuffer* der = NULL;
int keyFormat = 0;
if (bio == NULL) {
err = 1;
}
/* Read parameters from BIO and convert PEM to DER. */
if ((!err) && (pem_read_bio_key(bio, cb, pass, ECC_PARAM_TYPE,
&keyFormat, &der) < 0)) {
err = 1;
}
if (!err) {
/* Create EC group from DER encoding. */
const byte** p = (const byte**)&der->buffer;
ret = wolfssl_ec_group_d2i(group, p, der->length);
if (ret == NULL) {
WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_EC_GROUP");
}
}
/* Dispose of any allocated data. */
FreeDer(&der);
return ret;
}
WOLFSSL_EC_GROUP *wolfSSL_d2i_ECPKParameters(WOLFSSL_EC_GROUP **out,
const unsigned char **in, long len)
{
return wolfssl_ec_group_d2i(out, in, len);
}
int wolfSSL_i2d_ECPKParameters(const WOLFSSL_EC_GROUP* grp, unsigned char** pp)
{
unsigned char* out = NULL;
int len = 0;
int idx;
const byte* oid = NULL;
word32 oidSz = 0;
if (grp == NULL || !wc_ecc_is_valid_idx(grp->curve_idx) ||
grp->curve_idx < 0)
return WOLFSSL_FATAL_ERROR;
/* Get the actual DER encoding of the OID. ecc_sets[grp->curve_idx].oid
* is just the numerical representation. */
if (wc_ecc_get_oid((word32)grp->curve_oid, &oid, &oidSz) < 0)
return WOLFSSL_FATAL_ERROR;
len = SetObjectId((int)oidSz, NULL) + (int)oidSz;
if (pp == NULL)
return len;
if (*pp == NULL) {
out = (unsigned char*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_ASN1);
if (out == NULL)
return WOLFSSL_FATAL_ERROR;
}
else {
out = *pp;
}
idx = SetObjectId((int)oidSz, out);
XMEMCPY(out + idx, oid, oidSz);
if (*pp == NULL)
*pp = out;
else
*pp += len;
return len;
}
#endif /* !NO_BIO */
#if defined(OPENSSL_ALL) && !defined(NO_CERTS)
/* Copy an EC group.
*
* Only used by wolfSSL_EC_KEY_dup at this time.
*
* @param [in, out] dst Destination EC group.
* @param [in] src Source EC group.
* @return 0 on success.
*/
static int wolfssl_ec_group_copy(WOLFSSL_EC_GROUP* dst,
const WOLFSSL_EC_GROUP* src)
{
/* Copy the fields. */
dst->curve_idx = src->curve_idx;
dst->curve_nid = src->curve_nid;
dst->curve_oid = src->curve_oid;
return 0;
}
#endif /* OPENSSL_ALL && !NO_CERTS */
/* Copies ecc_key into new WOLFSSL_EC_GROUP object
*
* @param [in] src EC group to duplicate.
*
* @return EC group on success.
* @return NULL on error.
*/
WOLFSSL_EC_GROUP* wolfSSL_EC_GROUP_dup(const WOLFSSL_EC_GROUP *src)
{
WOLFSSL_EC_GROUP* newGroup = NULL;
if (src != NULL) {
/* Create new group base on NID in original EC group. */
newGroup = wolfSSL_EC_GROUP_new_by_curve_name(src->curve_nid);
}
return newGroup;
}
/* Compare two EC groups.
*
* Return code compliant with OpenSSL.
*
* @param [in] a First EC group.
* @param [in] b Second EC group.
* @param [in] ctx Big number context to use when comparing fields. Unused.
*
* @return 0 if equal.
* @return 1 if not equal.
* @return -1 on error.
*/
int wolfSSL_EC_GROUP_cmp(const WOLFSSL_EC_GROUP *a, const WOLFSSL_EC_GROUP *b,
WOLFSSL_BN_CTX *ctx)
{
int ret;
/* No BN operations performed. */
(void)ctx;
WOLFSSL_ENTER("wolfSSL_EC_GROUP_cmp");
/* Validate parameters. */
if ((a == NULL) || (b == NULL)) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_cmp Bad arguments");
/* Return error value. */
ret = WOLFSSL_FATAL_ERROR;
}
/* Compare NID and wolfSSL curve index. */
else {
/* 0 when same, 1 when not. */
ret = ((a->curve_nid == b->curve_nid) &&
(a->curve_idx == b->curve_idx)) ? 0 : 1;
}
return ret;
}
#ifndef NO_WOLFSSL_STUB
/* Set the ASN.1 flag that indicate encoding of curve.
*
* Stub function - flag not used elsewhere.
* Always encoded as named curve.
*
* @param [in] group EC group to modify.
* @param [in] flag ASN.1 flag to set. Valid values:
* OPENSSL_EC_EXPLICIT_CURVE, OPENSSL_EC_NAMED_CURVE
*/
void wolfSSL_EC_GROUP_set_asn1_flag(WOLFSSL_EC_GROUP *group, int flag)
{
(void)group;
(void)flag;
WOLFSSL_ENTER("wolfSSL_EC_GROUP_set_asn1_flag");
WOLFSSL_STUB("EC_GROUP_set_asn1_flag");
}
#endif
/* Get the curve NID of the group.
*
* Return code compliant with OpenSSL.
*
* @param [in] group EC group.
* @return Curve NID on success.
* @return 0 on error.
*/
int wolfSSL_EC_GROUP_get_curve_name(const WOLFSSL_EC_GROUP *group)
{
int nid = 0;
WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_curve_name");
if (group == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_curve_name Bad arguments");
}
else {
nid = group->curve_nid;
}
return nid;
}
/* Get the degree (curve size in bits) of the EC group.
*
* Return code compliant with OpenSSL.
*
* @return Degree of the curve on success.
* @return 0 on error.
*/
int wolfSSL_EC_GROUP_get_degree(const WOLFSSL_EC_GROUP *group)
{
int degree = 0;
WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_degree");
if (group == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_degree Bad arguments");
}
else {
switch (group->curve_nid) {
case WC_NID_secp112r1:
case WC_NID_secp112r2:
degree = 112;
break;
case WC_NID_secp128r1:
case WC_NID_secp128r2:
degree = 128;
break;
case WC_NID_secp160k1:
case WC_NID_secp160r1:
case WC_NID_secp160r2:
case WC_NID_brainpoolP160r1:
degree = 160;
break;
case WC_NID_secp192k1:
case WC_NID_brainpoolP192r1:
case WC_NID_X9_62_prime192v1:
case WC_NID_X9_62_prime192v2:
case WC_NID_X9_62_prime192v3:
degree = 192;
break;
case WC_NID_secp224k1:
case WC_NID_secp224r1:
case WC_NID_brainpoolP224r1:
degree = 224;
break;
case WC_NID_X9_62_prime239v1:
case WC_NID_X9_62_prime239v2:
case WC_NID_X9_62_prime239v3:
degree = 239;
break;
case WC_NID_secp256k1:
case WC_NID_brainpoolP256r1:
case WC_NID_X9_62_prime256v1:
degree = 256;
break;
case WC_NID_brainpoolP320r1:
degree = 320;
break;
case WC_NID_secp384r1:
case WC_NID_brainpoolP384r1:
degree = 384;
break;
case WC_NID_brainpoolP512r1:
degree = 512;
break;
case WC_NID_secp521r1:
degree = 521;
break;
}
}
return degree;
}
#endif /* OPENSSL_EXTRA */
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
/* Get the length of the order in bits of the EC group.
*
* TODO: consider switch statement or calculating directly from hex string
* array instead of using mp_int.
*
* @param [in] group EC group.
* @return Length of order in bits on success.
* @return 0 on error.
*/
int wolfSSL_EC_GROUP_order_bits(const WOLFSSL_EC_GROUP *group)
{
int ret = 0;
WC_DECLARE_VAR(order, mp_int, 1, 0);
/* Validate parameter. */
if ((group == NULL) || (group->curve_idx < 0)) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_order_bits NULL error");
ret = WOLFSSL_FATAL_ERROR;
}
#ifdef WOLFSSL_SMALL_STACK
if (ret == 0) {
/* Allocate memory for mp_int that will hold order value. */
order = (mp_int *)XMALLOC(sizeof(*order), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (order == NULL) {
ret = WOLFSSL_FATAL_ERROR;
}
}
#endif
if (ret == 0) {
/* Initialize mp_int. */
ret = mp_init(order);
}
if (ret == 0) {
/* Read hex string of order from wolfCrypt array of curves. */
ret = mp_read_radix(order, ecc_sets[group->curve_idx].order,
MP_RADIX_HEX);
if (ret == 0) {
/* Get bits of order. */
ret = mp_count_bits(order);
}
/* Clear and free mp_int. */
mp_clear(order);
}
WC_FREE_VAR_EX(order, NULL, DYNAMIC_TYPE_TMP_BUFFER);
/* Convert error code to length of 0. */
if (ret < 0) {
ret = 0;
}
return ret;
}
#endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
#if defined(OPENSSL_EXTRA)
/* Get the order of the group as a BN.
*
* Return code compliant with OpenSSL.
*
* @param [in] group EC group.
* @param [in, out] order BN to hold order value.
* @param [in] ctx Context to use for BN operations. Unused.
* @return 1 on success.
* @return 0 on error.
*/
int wolfSSL_EC_GROUP_get_order(const WOLFSSL_EC_GROUP *group,
WOLFSSL_BIGNUM *order, WOLFSSL_BN_CTX *ctx)
{
int ret = 1;
mp_int* mp = NULL;
/* No BN operations performed - done with mp_int in BN. */
(void)ctx;
/* Validate parameters. */
if ((group == NULL) || (order == NULL) || (order->internal == NULL)) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order NULL error");
ret = 0;
}
if (ret == 1 &&
(group->curve_idx < 0 || !wc_ecc_is_valid_idx(group->curve_idx))) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order Bad group idx");
ret = 0;
}
if (ret == 1) {
mp = (mp_int*)order->internal;
}
/* Initialize */
if ((ret == 1) && (mp_init(mp) != MP_OKAY)) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_init failure");
ret = 0;
}
/* Read hex string of order from wolfCrypt array of curves. */
if ((ret == 1) && (mp_read_radix(mp, ecc_sets[group->curve_idx].order,
MP_RADIX_HEX) != MP_OKAY)) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_read order failure");
/* Zero out any partial value but don't free. */
mp_zero(mp);
ret = 0;
}
return ret;
}
#endif /* OPENSSL_EXTRA */
/* End EC_GROUP */
/* Start EC_POINT */
#if defined(OPENSSL_EXTRA)
/* Set data of EC point into internal, wolfCrypt EC point object.
*
* EC_POINT Openssl -> WolfSSL
*
* @param [in, out] p EC point to update.
* @return 1 on success.
* @return -1 on failure.
*/
static int ec_point_internal_set(WOLFSSL_EC_POINT *p)
{
int ret = 1;
WOLFSSL_ENTER("ec_point_internal_set");
/* Validate parameter. */
if ((p == NULL) || (p->internal == NULL)) {
WOLFSSL_MSG("ECPoint NULL error");
ret = WOLFSSL_FATAL_ERROR;
}
else {
/* Get internal point as a wolfCrypt EC point. */
ecc_point* point = (ecc_point*)p->internal;
/* Set X ordinate if available. */
if ((p->X != NULL) && (wolfssl_bn_get_value(p->X, point->x) != 1)) {
WOLFSSL_MSG("ecc point X error");
ret = WOLFSSL_FATAL_ERROR;
}
/* Set Y ordinate if available. */
if ((ret == 1) && (p->Y != NULL) && (wolfssl_bn_get_value(p->Y,
point->y) != 1)) {
WOLFSSL_MSG("ecc point Y error");
ret = WOLFSSL_FATAL_ERROR;
}
/* Set Z ordinate if available. */
if ((ret == 1) && (p->Z != NULL) && (wolfssl_bn_get_value(p->Z,
point->z) != 1)) {
WOLFSSL_MSG("ecc point Z error");
ret = WOLFSSL_FATAL_ERROR;
}
/* Internal values set when operations succeeded. */
p->inSet = (ret == 1);
}
return ret;
}
/* Set data of internal, wolfCrypt EC point object into EC point.
*
* EC_POINT WolfSSL -> OpenSSL
*
* @param [in, out] p EC point to update.
* @return 1 on success.
* @return -1 on failure.
*/
static int ec_point_external_set(WOLFSSL_EC_POINT *p)
{
int ret = 1;
WOLFSSL_ENTER("ec_point_external_set");
/* Validate parameter. */
if ((p == NULL) || (p->internal == NULL)) {
WOLFSSL_MSG("ECPoint NULL error");
ret = WOLFSSL_FATAL_ERROR;
}
else {
/* Get internal point as a wolfCrypt EC point. */
ecc_point* point = (ecc_point*)p->internal;
/* Set X ordinate. */
if (wolfssl_bn_set_value(&p->X, point->x) != 1) {
WOLFSSL_MSG("ecc point X error");
ret = WOLFSSL_FATAL_ERROR;
}
/* Set Y ordinate. */
if ((ret == 1) && (wolfssl_bn_set_value(&p->Y, point->y) != 1)) {
WOLFSSL_MSG("ecc point Y error");
ret = WOLFSSL_FATAL_ERROR;
}
/* Set Z ordinate. */
if ((ret == 1) && (wolfssl_bn_set_value(&p->Z, point->z) != 1)) {
WOLFSSL_MSG("ecc point Z error");
ret = WOLFSSL_FATAL_ERROR;
}
/* External values set when operations succeeded. */
p->exSet = (ret == 1);
}