-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathtest_ossl_ec.c
More file actions
1625 lines (1437 loc) · 58.6 KB
/
test_ossl_ec.c
File metadata and controls
1625 lines (1437 loc) · 58.6 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
/* test_ossl_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 <tests/unit.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/openssl/ec.h>
#include <wolfssl/openssl/ecdh.h>
#include <wolfssl/openssl/pem.h>
#include <wolfssl/wolfcrypt/types.h>
#include <tests/api/api.h>
#include <tests/api/test_ossl_ec.h>
/*******************************************************************************
* EC OpenSSL compatibility API Testing
******************************************************************************/
#if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
int test_wolfSSL_EC_GROUP(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_EXTRA
EC_GROUP *group = NULL;
EC_GROUP *group2 = NULL;
EC_GROUP *group3 = NULL;
#ifndef HAVE_ECC_BRAINPOOL
EC_GROUP *group4 = NULL;
#endif
WOLFSSL_BIGNUM* order = NULL;
int group_bits;
int i;
static const int knownEccNids[] = {
NID_X9_62_prime192v1,
NID_X9_62_prime192v2,
NID_X9_62_prime192v3,
NID_X9_62_prime239v1,
NID_X9_62_prime239v2,
NID_X9_62_prime239v3,
NID_X9_62_prime256v1,
NID_secp112r1,
NID_secp112r2,
NID_secp128r1,
NID_secp128r2,
NID_secp160r1,
NID_secp160r2,
NID_secp224r1,
NID_secp384r1,
NID_secp521r1,
NID_secp160k1,
NID_secp192k1,
NID_secp224k1,
NID_secp256k1,
NID_brainpoolP160r1,
NID_brainpoolP192r1,
NID_brainpoolP224r1,
NID_brainpoolP256r1,
NID_brainpoolP320r1,
NID_brainpoolP384r1,
NID_brainpoolP512r1,
};
int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
static const int knownEccEnums[] = {
ECC_SECP192R1,
ECC_PRIME192V2,
ECC_PRIME192V3,
ECC_PRIME239V1,
ECC_PRIME239V2,
ECC_PRIME239V3,
ECC_SECP256R1,
ECC_SECP112R1,
ECC_SECP112R2,
ECC_SECP128R1,
ECC_SECP128R2,
ECC_SECP160R1,
ECC_SECP160R2,
ECC_SECP224R1,
ECC_SECP384R1,
ECC_SECP521R1,
ECC_SECP160K1,
ECC_SECP192K1,
ECC_SECP224K1,
ECC_SECP256K1,
ECC_BRAINPOOLP160R1,
ECC_BRAINPOOLP192R1,
ECC_BRAINPOOLP224R1,
ECC_BRAINPOOLP256R1,
ECC_BRAINPOOLP320R1,
ECC_BRAINPOOLP384R1,
ECC_BRAINPOOLP512R1,
};
int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
ExpectNotNull(group2 = EC_GROUP_dup(group));
ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
#ifndef HAVE_ECC_BRAINPOOL
ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
NID_brainpoolP256r1));
#endif
ExpectNull(EC_GROUP_dup(NULL));
ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
#ifndef HAVE_ECC_BRAINPOOL
ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
#endif
ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
ExpectNotNull(order = BN_new());
ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
wolfSSL_BN_free(order);
ExpectNotNull(EC_GROUP_method_of(group));
ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
NID_X9_62_prime_field);
ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
#ifndef NO_WOLFSSL_STUB
wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
#endif
#ifndef HAVE_ECC_BRAINPOOL
EC_GROUP_free(group4);
#endif
EC_GROUP_free(group3);
EC_GROUP_free(group2);
EC_GROUP_free(group);
for (i = 0; i < knowEccNidsLen; i++) {
group = NULL;
ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
EC_GROUP_free(group);
}
for (i = 0; i < knowEccEnumsLen; i++) {
group = NULL;
ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
EC_GROUP_free(group);
}
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
{
EXPECT_DECLS;
#if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
EC_GROUP *group = NULL;
BIO* bio = NULL;
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
EC_GROUP *ret = NULL;
static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
"BgUrgQQAIg==\n"
"-----END EC PARAMETERS-----";
#endif
static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
"MAA=\n"
"-----END EC PARAMETERS-----";
static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
"BgA=\n"
"-----END EC PARAMETERS-----";
static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
"BgE=\n"
"-----END EC PARAMETERS-----";
static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
"BgE*\n"
"-----END EC PARAMETERS-----";
/* Test that first parameter, bio, being NULL fails. */
ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
/* Test that reading named parameters works. */
ExpectNotNull(bio = BIO_new(BIO_s_file()));
ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
BIO_free(bio);
bio = NULL;
EC_GROUP_free(group);
group = NULL;
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
/* Test that reusing group works. */
ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
sizeof(ec_nc_p384)));
ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
BIO_free(bio);
bio = NULL;
EC_GROUP_free(group);
group = NULL;
/* Test that returning through group works. */
ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
sizeof(ec_nc_p384)));
ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
ExpectIntEQ(ret == group, 1);
ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
BIO_free(bio);
bio = NULL;
EC_GROUP_free(group);
group = NULL;
#endif
/* Test 0x30, 0x00 (not and object id) fails. */
ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
sizeof(ec_nc_bad_1)));
ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
BIO_free(bio);
bio = NULL;
/* Test 0x06, 0x00 (empty object id) fails. */
ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
sizeof(ec_nc_bad_2)));
ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
BIO_free(bio);
bio = NULL;
/* Test 0x06, 0x01 (badly formed object id) fails. */
ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
sizeof(ec_nc_bad_3)));
ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
BIO_free(bio);
bio = NULL;
/* Test invalid PEM encoding - invalid character. */
ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
sizeof(ec_nc_bad_4)));
ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
BIO_free(bio);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_i2d_ECPKParameters(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA)
EC_GROUP* grp = NULL;
unsigned char p256_oid[] = {
0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
};
unsigned char *der = p256_oid;
unsigned char out_der[sizeof(p256_oid)];
XMEMSET(out_der, 0, sizeof(out_der));
ExpectNotNull(d2i_ECPKParameters(&grp, (const unsigned char **)&der,
sizeof(p256_oid)));
der = out_der;
ExpectIntEQ(i2d_ECPKParameters(grp, &der), sizeof(p256_oid));
ExpectBufEQ(p256_oid, out_der, sizeof(p256_oid));
EC_GROUP_free(grp);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EC_POINT(void)
{
EXPECT_DECLS;
#if !defined(WOLFSSL_SP_MATH) && \
(!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
#ifdef OPENSSL_EXTRA
BN_CTX* ctx = NULL;
EC_GROUP* group = NULL;
#ifndef HAVE_ECC_BRAINPOOL
EC_GROUP* group2 = NULL;
#endif
EC_POINT* Gxy = NULL;
EC_POINT* new_point = NULL;
EC_POINT* set_point = NULL;
EC_POINT* get_point = NULL;
EC_POINT* infinity = NULL;
EC_POINT* dup_point = NULL;
BIGNUM* k = NULL;
BIGNUM* Gx = NULL;
BIGNUM* Gy = NULL;
BIGNUM* Gz = NULL;
BIGNUM* X = NULL;
BIGNUM* Y = NULL;
BIGNUM* set_point_bn = NULL;
char* hexStr = NULL;
const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
"17AF381913FF7A96314EA47055EA0FD0";
/* NISTP256R1 Gx/Gy */
const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
"77037D812DEB33A0F4A13945D898C296";
const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
"2BCE33576B315ECECBB6406837BF51F5";
const char* uncompG
= "046B17D1F2E12C4247F8BCE6E563A440F2"
"77037D812DEB33A0F4A13945D898C296"
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
"2BCE33576B315ECECBB6406837BF51F5";
const char* compG
= "036B17D1F2E12C4247F8BCE6E563A440F2"
"77037D812DEB33A0F4A13945D898C296";
#ifndef HAVE_SELFTEST
EC_POINT *tmp = NULL;
size_t bin_len;
unsigned int blen = 0;
unsigned char* buf = NULL;
unsigned char bufInf[1] = { 0x00 };
const unsigned char binUncompG[] = {
0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
};
const unsigned char binUncompGBad[] = {
0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
};
#ifdef HAVE_COMP_KEY
const unsigned char binCompG[] = {
0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
};
#endif
#endif
ExpectNotNull(ctx = BN_CTX_new());
ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
#ifndef HAVE_ECC_BRAINPOOL
/* Used to make groups curve_idx == -1. */
ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
#endif
ExpectNull(EC_POINT_new(NULL));
ExpectNotNull(Gxy = EC_POINT_new(group));
ExpectNotNull(new_point = EC_POINT_new(group));
ExpectNotNull(set_point = EC_POINT_new(group));
ExpectNotNull(X = BN_new());
ExpectNotNull(Y = BN_new());
ExpectNotNull(set_point_bn = BN_new());
ExpectNotNull(infinity = EC_POINT_new(group));
/* load test values */
ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
/* populate coordinates for input point */
if (Gxy != NULL) {
Gxy->X = Gx;
Gxy->Y = Gy;
Gxy->Z = Gz;
}
/* Test handling of NULL point. */
EC_POINT_clear_free(NULL);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
NULL, NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
NULL, NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
NULL, NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
X, NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
NULL, Y, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
X, Y, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
X, Y, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
NULL, Y, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
X, NULL, ctx), 0);
/* Getting point at infinity returns an error. */
ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
X, Y, ctx), 0);
#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
!defined(WOLFSSL_MICROCHIP_TA100) && \
!defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
/* perform point multiplication */
ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
ExpectIntEQ(BN_is_zero(new_point->X), 0);
ExpectIntEQ(BN_is_zero(new_point->Y), 0);
ExpectIntEQ(BN_is_zero(new_point->Z), 0);
ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
ExpectIntEQ(BN_is_zero(new_point->X), 0);
ExpectIntEQ(BN_is_zero(new_point->Y), 0);
ExpectIntEQ(BN_is_zero(new_point->Z), 0);
ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
ExpectIntEQ(BN_is_zero(new_point->X), 0);
ExpectIntEQ(BN_is_zero(new_point->Y), 0);
ExpectIntEQ(BN_is_zero(new_point->Z), 0);
ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
ExpectIntEQ(BN_is_zero(new_point->X), 1);
ExpectIntEQ(BN_is_zero(new_point->Y), 1);
ExpectIntEQ(BN_is_zero(new_point->Z), 1);
/* Set point to something. */
ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
#else
ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
ctx), 1);
ExpectIntEQ(BN_is_zero(new_point->X), 0);
ExpectIntEQ(BN_is_zero(new_point->Y), 0);
ExpectIntEQ(BN_is_zero(new_point->Z), 0);
#endif
/* check if point X coordinate is zero */
ExpectIntEQ(BN_is_zero(new_point->X), 0);
#if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
(!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
#endif
/* extract the coordinates from point */
ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
ctx), WOLFSSL_SUCCESS);
/* check if point X coordinate is zero */
ExpectIntEQ(BN_is_zero(X), WC_NO_ERR_TRACE(WOLFSSL_FAILURE));
/* set the same X and Y points in another object */
ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
ctx), WOLFSSL_SUCCESS);
/* compare points as they should be the same */
ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
/* Test copying */
ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
/* Test duplicating */
ExpectNull(EC_POINT_dup(NULL, group));
ExpectNull(EC_POINT_dup(set_point, NULL));
ExpectNotNull(dup_point = EC_POINT_dup(set_point, group));
ExpectIntEQ(EC_POINT_cmp(group, dup_point, set_point, ctx), 0);
/* Test inverting */
ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
!defined(WOLFSSL_MICROCHIP_TA100) && \
!defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
{
EC_POINT* orig_point = NULL;
ExpectNotNull(orig_point = EC_POINT_new(group));
ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
1);
/* new_point should be set_point inverted so adding it will revert
* the point back to set_point */
ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
NULL), 1);
ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
/* dup_point equals set_point so let's test with that too */
ExpectIntEQ(EC_POINT_add(group, orig_point, dup_point, dup_point, NULL),
1);
ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
NULL), 1);
ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
EC_POINT_free(orig_point);
}
#endif
/* Test getting affine converts from projective. */
ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
/* Force non-affine coordinates */
ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
(WOLFSSL_BIGNUM*)BN_value_one()), 1);
if (new_point != NULL) {
new_point->inSet = 0;
}
/* extract the coordinates from point */
ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
ctx), WOLFSSL_SUCCESS);
/* check if point ordinates have changed. */
ExpectIntNE(BN_cmp(X, set_point->X), 0);
ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
/* Test check for infinity */
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
#else
ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
#endif
ExpectPtrEq(EC_POINT_point2bn(group, set_point,
POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
/* check bn2hex */
hexStr = BN_bn2hex(k);
ExpectStrEQ(hexStr, kTest);
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
defined(XFPRINTF)
BN_print_fp(stderr, k);
fprintf(stderr, "\n");
#endif
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
hexStr = BN_bn2hex(Gx);
ExpectStrEQ(hexStr, kGx);
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
defined(XFPRINTF)
BN_print_fp(stderr, Gx);
fprintf(stderr, "\n");
#endif
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
hexStr = BN_bn2hex(Gy);
ExpectStrEQ(hexStr, kGy);
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
defined(XFPRINTF)
BN_print_fp(stderr, Gy);
fprintf(stderr, "\n");
#endif
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
/* Test point to hex */
ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
ctx));
ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
ctx));
ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
ctx));
#ifndef HAVE_ECC_BRAINPOOL
/* Group not supported in wolfCrypt. */
ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
ctx));
#endif
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
ExpectNotNull(hexStr);
ExpectStrEQ(hexStr, uncompG);
ExpectNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
ExpectIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
ExpectNotNull(hexStr);
ExpectStrEQ(hexStr, compG);
#if defined(HAVE_COMP_KEY) && !defined(HAVE_SELFTEST)
ExpectNotNull(get_point = EC_POINT_hex2point
(group, hexStr, get_point, ctx));
ExpectIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
#endif
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
EC_POINT_free(get_point);
#ifndef HAVE_SELFTEST
/* Test point to oct */
ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
NULL, 0, ctx), 0);
ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
NULL, 0, ctx), 0);
ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
NULL, 0, ctx), 0);
bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
NULL, 0, ctx);
ExpectIntEQ(bin_len, sizeof(binUncompG));
ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
DYNAMIC_TYPE_ECC));
ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
buf, bin_len, ctx), bin_len);
ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
/* Infinity (x=0, y=0) encodes as '0x00'. */
ExpectIntEQ(EC_POINT_point2oct(group, infinity,
POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
ExpectIntEQ(EC_POINT_point2oct(group, infinity,
POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
ExpectIntEQ(EC_POINT_point2oct(group, infinity,
POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
ExpectIntEQ(bufInf[0], 0);
wolfSSL_EC_POINT_dump(NULL, NULL);
/* Test point i2d */
ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
ExpectIntEQ(blen, sizeof(binUncompG));
ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
blen--;
ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
blen++;
ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
#ifdef HAVE_COMP_KEY
/* Test point to oct compressed */
bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
0, ctx);
ExpectIntEQ(bin_len, sizeof(binCompG));
ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
DYNAMIC_TYPE_ECC));
ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
bin_len, ctx), bin_len);
ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
#endif
/* Test point BN */
ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
/* Test oct to point */
ExpectNotNull(tmp = EC_POINT_new(group));
ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
ctx), 0);
ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
ctx), 0);
ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
ctx), 0);
ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
sizeof(binUncompGBad), ctx), 0);
ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
ctx), 1);
ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
EC_POINT_free(tmp);
tmp = NULL;
/* Test setting BN ordinates. */
ExpectNotNull(tmp = EC_POINT_new(group));
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
Gy, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
Gy, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
NULL, ctx), 0);
ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
ctx), 1);
EC_POINT_free(tmp);
tmp = NULL;
/* Test point d2i */
ExpectNotNull(tmp = EC_POINT_new(group));
ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
EC_POINT_free(tmp);
tmp = NULL;
#ifdef HAVE_COMP_KEY
/* Test oct compressed to point */
ExpectNotNull(tmp = EC_POINT_new(group));
ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
1);
ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
EC_POINT_free(tmp);
tmp = NULL;
/* Test point d2i - compressed */
ExpectNotNull(tmp = EC_POINT_new(group));
ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
EC_POINT_free(tmp);
tmp = NULL;
#endif
#endif
/* test BN_mod_add */
ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
(WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
1);
ExpectIntEQ(BN_is_zero(new_point->Z), 1);
/* cleanup */
BN_free(X);
BN_free(Y);
BN_free(k);
BN_free(set_point_bn);
EC_POINT_free(infinity);
EC_POINT_free(dup_point);
EC_POINT_free(new_point);
EC_POINT_free(set_point);
EC_POINT_clear_free(Gxy);
#ifndef HAVE_ECC_BRAINPOOL
EC_GROUP_free(group2);
#endif
EC_GROUP_free(group);
BN_CTX_free(ctx);
#endif
#endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
return EXPECT_RESULT();
}
int test_wolfSSL_SPAKE(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
&& !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
!defined(WOLFSSL_MICROCHIP_TA100) && \
!defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
BIGNUM* x = NULL; /* kdc priv */
BIGNUM* y = NULL; /* client priv */
BIGNUM* w = NULL; /* shared value */
byte M_bytes[] = {
/* uncompressed */
0x04,
/* x */
0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
/* y */
0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
};
EC_POINT* M = NULL; /* shared value */
byte N_bytes[] = {
/* uncompressed */
0x04,
/* x */
0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
/* y */
0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
};
EC_POINT* N = NULL; /* shared value */
EC_POINT* T = NULL; /* kdc pub */
EC_POINT* tmp1 = NULL; /* kdc pub */
EC_POINT* tmp2 = NULL; /* kdc pub */
EC_POINT* S = NULL; /* client pub */
EC_POINT* client_secret = NULL;
EC_POINT* kdc_secret = NULL;
EC_GROUP* group = NULL;
BN_CTX* bn_ctx = NULL;
/* Values taken from a test run of Kerberos 5 */
ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
ExpectNotNull(bn_ctx = BN_CTX_new());
ExpectNotNull(M = EC_POINT_new(group));
ExpectNotNull(N = EC_POINT_new(group));
ExpectNotNull(T = EC_POINT_new(group));
ExpectNotNull(tmp1 = EC_POINT_new(group));
ExpectNotNull(tmp2 = EC_POINT_new(group));
ExpectNotNull(S = EC_POINT_new(group));
ExpectNotNull(client_secret = EC_POINT_new(group));
ExpectNotNull(kdc_secret = EC_POINT_new(group));
ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
"E5D8768A725EAEEA6FC68EC239A17C0"), 1);
ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
"90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
"506AB395478F0AAB647752CF117B36250"), 1);
ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
1);
ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
1);
/* Function pattern similar to ossl_keygen and ossl_result in krb5 */
/* kdc */
/* T=x*P+w*M */
/* All in one function call */
ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
/* Spread into separate calls */
ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
1);
ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
/* client */
/* S=y*P+w*N */
/* All in one function call */
ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
/* Spread into separate calls */
ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
1);
ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
/* K=y*(T-w*M) */
ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
1);
ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
bn_ctx), 1);
/* kdc */
/* K=x*(S-w*N) */
ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
1);
ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
1);
/* kdc_secret == client_secret */
ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
BN_free(x);
BN_free(y);
BN_free(w);
EC_POINT_free(M);
EC_POINT_free(N);
EC_POINT_free(T);
EC_POINT_free(tmp1);
EC_POINT_free(tmp2);
EC_POINT_free(S);
EC_POINT_free(client_secret);
EC_POINT_free(kdc_secret);
EC_GROUP_free(group);
BN_CTX_free(bn_ctx);
#endif
return EXPECT_RESULT();
}
int test_wolfSSL_EC_KEY_generate(void)
{
EXPECT_DECLS;
#ifdef OPENSSL_EXTRA
WOLFSSL_EC_KEY* key = NULL;
#ifndef HAVE_ECC_BRAINPOOL
WOLFSSL_EC_GROUP* group = NULL;
#endif
ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
wolfSSL_EC_KEY_free(key);
key = NULL;
#ifndef HAVE_ECC_BRAINPOOL
ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
NID_brainpoolP256r1));
ExpectNotNull(key = wolfSSL_EC_KEY_new());
ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
wolfSSL_EC_KEY_free(key);
wolfSSL_EC_GROUP_free(group);
#endif
#endif
return EXPECT_RESULT();
}
int test_EC_i2d(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
EC_KEY *key = NULL;
EC_KEY *copy = NULL;
int len = 0;
unsigned char *buf = NULL;
unsigned char *p = NULL;
const unsigned char *tmp = NULL;
const unsigned char octBad[] = {
0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
};
ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
ExpectIntEQ(EC_KEY_generate_key(key), 1);
ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
DYNAMIC_TYPE_TMP_BUFFER));
p = buf;
ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
ExpectNull(o2i_ECPublicKey(©, NULL, -1));
ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
tmp = buf;
ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
ExpectNull(o2i_ECPublicKey(©, &tmp, 0));
ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);