-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathgenerate_tests.cc
2021 lines (1773 loc) · 64.4 KB
/
generate_tests.cc
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
/* Copyright 2016 Brian Smith.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
// Generate test vectors for *ring*.
#include <openssl/ec.h>
#include <assert.h>
#include <inttypes.h>
#include <vector>
#include <openssl/digest.h>
#include <openssl/ecdsa.h>
#include <openssl/mem.h>
#include <openssl/nid.h>
#include <openssl/sha.h>
#include "ecp_nistz256_precomp.h"
#include "../crypto/bn/internal.h"
#include "../crypto/ec/internal.h"
#include "internal.h"
enum ECDSASigFormat { Fixed, ASN1 };
enum Affinification { MakeAffineAllZero, MakeAffineToken, Unchanged };
extern "C" int digest_to_bn(BIGNUM *out, const uint8_t *digest,
size_t digest_len, const BIGNUM *order);
static bool format_ecdsa_sig(uint8_t *out_sig, unsigned int *out_sig_len,
const ECDSA_SIG *sig, const EC_GROUP *group,
ECDSASigFormat fmt) {
if (fmt == ASN1) {
uint8_t *temp = NULL;
size_t temp_len = 0;
if (!ECDSA_SIG_to_bytes(&temp, &temp_len, sig)) {
return false;
}
if (*out_sig_len < temp_len) {
OPENSSL_free(temp);
return false;
}
memcpy(out_sig, temp, temp_len);
*out_sig_len = (unsigned int)temp_len;
OPENSSL_free(temp);
return true;
}
assert(fmt == Fixed);
const BIGNUM *order = EC_GROUP_get0_order(group);
size_t order_bits = BN_num_bits(order);
size_t sig_len = 2 * (order_bits / 8);
if (*out_sig_len < sig_len) {
return false;
}
*out_sig_len = sig_len;
if (!BN_bn2bin_padded(out_sig, sig_len / 2, sig->r) ||
!BN_bn2bin_padded(out_sig + (sig_len / 2), sig_len / 2, sig->s)) {
return false;
}
return true;
}
static bool ecdsa_sign(uint8_t *sig, unsigned int *sig_len, EC_KEY *key,
const uint8_t *digest, size_t digest_len,
ECDSASigFormat fmt, int i) {
if (fmt == ASN1) {
if (!ECDSA_sign_ex(0, digest, digest_len, sig, sig_len, NULL, NULL,
key, i)) {
printf("failed\n");
return false;
}
} else {
assert(fmt == Fixed);
bssl::UniquePtr<ECDSA_SIG>
ecdsa_sig(ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key, i));
format_ecdsa_sig(sig, sig_len, ecdsa_sig.get(), EC_KEY_get0_group(key), fmt);
}
return true;
}
void print_hex(FILE *f, const uint8_t *data, size_t len) {
for (size_t i = 0; i < len; i++) {
fprintf(f, "%02x", data[i]);
}
}
void print_bn(const BIGNUM *b) {
if (BN_is_zero(b)) {
printf("00");
return;
}
char *hex = BN_bn2hex(b);
if (!hex) {
abort();
}
printf("%s", hex);
free(hex);
}
static bool print_ecdsa_sig(const ECDSA_SIG *sig, const EC_GROUP *group,
ECDSASigFormat fmt) {
uint8_t sig_bytes[1024];
unsigned int sig_bytes_len = sizeof(sig_bytes);
if (!format_ecdsa_sig(sig_bytes, &sig_bytes_len, sig, group, fmt)) {
return false;
}
printf("Sig = ");
print_hex(stdout, sig_bytes, sig_bytes_len);
printf("\n");
return true;
}
static bool GenerateTestsForRS(const EC_GROUP *group, const char *curve_name,
const BIGNUM *r, const BIGNUM *r_override,
const BIGNUM *s, ECDSASigFormat fmt, BN_CTX *ctx,
const char *result, const char *comment) {
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group));
if (!pub_key ||
!EC_POINT_set_compressed_coordinates_GFp(
group, pub_key.get(), (r_override ? r_override : r), 0, NULL)) {
return false;
}
bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
if (!sig ||
!BN_nnmod(sig->r, r, EC_GROUP_get0_order(group), ctx)) {
return false;
}
sig->s = BN_dup(s);
if (!sig->s) {
return false;
}
// Any message will do.
uint8_t digest[EVP_MAX_MD_SIZE];
size_t order_bits = BN_num_bits(EC_GROUP_get0_order(group));
size_t digest_len;
const char *digest_name;
switch (order_bits) {
case 384:
digest_len = SHA384_DIGEST_LENGTH;
digest_name = "SHA384";
if (SHA384((const uint8_t *)"", 0, digest) == NULL) {
return false;
}
break;
case 256:
digest_len = SHA256_DIGEST_LENGTH;
digest_name = "SHA256";
if (SHA256((const uint8_t *)"", 0, digest) == NULL) {
return false;
}
break;
default:
assert(0);
return false;
}
bssl::UniquePtr<BIGNUM> z_neg(BN_new());
if (!z_neg ||
!digest_to_bn(z_neg.get(), digest, digest_len,
EC_GROUP_get0_order(group))) {
return false;
}
BN_set_negative(z_neg.get(), true);
bssl::UniquePtr<EC_POINT> intermediate(EC_POINT_new(group));
if (!intermediate ||
!EC_POINT_mul(group, intermediate.get(), z_neg.get(), pub_key.get(),
sig->s, NULL)) {
return false;
}
bssl::UniquePtr<BIGNUM> r_inv(BN_new());
if (!r_inv ||
BN_mod_inverse(r_inv.get(), r, EC_GROUP_get0_order(group), ctx) == NULL) {
return false;
}
bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group));
if (!result ||
!EC_POINT_mul(group, point.get(), NULL, intermediate.get(), r_inv.get(),
NULL)) {
return false;
}
uint8_t pub_key_encoded[1024];
size_t pub_key_encoded_len =
EC_POINT_point2oct(group, point.get(), POINT_CONVERSION_UNCOMPRESSED,
pub_key_encoded, sizeof(pub_key_encoded), NULL);
if (pub_key_encoded_len == 0) {
return false;
}
printf("\n");
printf("%s\n", comment);
printf("Curve = %s\n", curve_name);
printf("Digest = %s\n", digest_name);
printf("Msg = \"\"\n");
printf("Q = ");
print_hex(stdout, pub_key_encoded, pub_key_encoded_len);
printf("\n");
print_ecdsa_sig(sig.get(), group, fmt);
printf("Result = %s\n", result);
return true;
}
static bool GenerateMaxwellTestsForCurve(int nid, const char *curve_name,
BN_ULONG r_word, BN_ULONG offset,
ECDSASigFormat fmt, BN_CTX *ctx) {
bssl::UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid));
bssl::UniquePtr<BIGNUM> r(BN_new());
bssl::UniquePtr<BIGNUM> q(BN_new());
bssl::UniquePtr<BIGNUM> q_minus_n(BN_new());
bssl::UniquePtr<BIGNUM> q_minus_n_ish(BN_new());
bssl::UniquePtr<BIGNUM> wrong_r(BN_new());
bssl::UniquePtr<BIGNUM> s(BN_new());
if (!group || !r || !q || !q_minus_n || !q_minus_n_ish || !wrong_r || !s ||
!EC_GROUP_get_curve_GFp(group.get(), q.get(), nullptr, nullptr, nullptr) ||
!BN_sub(q_minus_n.get(), q.get(), EC_GROUP_get0_order(group.get())) ||
!BN_copy(q_minus_n_ish.get(), q_minus_n.get()) ||
!BN_add_word(q_minus_n_ish.get(), offset) ||
!BN_mod_add(wrong_r.get(), q_minus_n_ish.get(),
EC_GROUP_get0_order(group.get()), q.get(), ctx) ||
!BN_set_word(s.get(), 4)) {
return false;
}
if (!BN_set_word(r.get(), r_word) ||
!GenerateTestsForRS(group.get(), curve_name, r.get(), nullptr, s.get(),
fmt, ctx, "P (0 )",
"# The signature has r < q - n. This is the control case for the next\n"
"# test case; this signature is the same but the public key is\n"
"# different. Notice that both public keys work for the same signature!\n"
"# This signature will validate even if the implementation doesn't\n"
"# reduce the X coordinate of the multiplication result (mod n).")) {
return false;
}
if (!BN_add(r.get(), r.get(), EC_GROUP_get0_order(group.get())) ||
!GenerateTestsForRS(group.get(), curve_name, r.get(), nullptr, s.get(),
fmt, ctx, "P (0 )",
"# The signature has r < q - n. s Since r < q - n, r + n < q. Notice\n"
"# that this signature is the same as the signature in the preceding\n"
"# test case, but the public key is different. That the signature\n"
"# validates for this case too is what's special about the case where\n"
"# r < q - n. If this test case fails it is likely that the\n"
"# implementation doesn't reduce the X coordinate of the multiplication\n"
"# result (mod n), or it is missing the second step of Gregory\n"
"# Maxwell's trick.")) {
return false;
}
if (!GenerateTestsForRS(group.get(), curve_name, q_minus_n_ish.get(),
nullptr, s.get(), fmt, ctx, "P (0 )",
"# The signature has r > q - n. The signature is for the public key\n"
"# recovered from r. r + n > q since r > q - n. This is the control\n"
"# for the next test case; this signature is the same as the signature\n"
"# in the following test case but the public key is different.")) {
return false;
}
if (!GenerateTestsForRS(group.get(), curve_name, q_minus_n_ish.get(),
wrong_r.get(), s.get(), fmt, ctx, "F",
"# The signature has r > q - n. The signature is for the public key\n"
"# recovered from r + n (mod q). r + n > q since r > q - n, and so\n"
"# r + n (mod q) < r because r + n (mod n) != r + n (mod q). Notice\n"
"# that this signature is the same as the signature in the preceding\n"
"# test case but the public key is different. Also, notice that the\n"
"# signature fails to validate in this case, unlike other related test\n"
"# cases. If this test case fails (the signature validates), it is\n"
"# likely that the implementation didn't guard the second case of\n"
"# Gregory Maxwell's trick on the condition r < q - n.")) {
return false;
}
return true;
}
static bool GenerateMaxwellTests(ECDSASigFormat fmt, BN_CTX *ctx) {
printf(
"# Test vectors for Gregory Maxwell's trick.\n"
"#\n"
"# In all cases, the `s` component of the signature was selected\n"
"# arbitrarily as 4 and then the `r` component was chosen to be the\n"
"# smallest value where the public key recovery from the signature\n"
"# works.\n");
// The numbers (6, 0) and (3, 2) were determined using the guess-and-check
// method. Using smaller/different numbers causes the public key recovery
// from the signature to fail.
if (!GenerateMaxwellTestsForCurve(NID_X9_62_prime256v1, "P-256", 6, 0, fmt, ctx) ||
!GenerateMaxwellTestsForCurve(NID_secp384r1, "P-384", 3, 2, fmt, ctx)) {
return false;
}
return true;
}
static bool GenerateShortSTestsForCurve(int nid, const char *curve_name,
ECDSASigFormat fmt, BN_CTX *ctx) {
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
if (!key ||
!EC_KEY_generate_key(key.get())) {
return false;
}
const EC_GROUP *group = EC_KEY_get0_group(key.get());
unsigned order_bits = BN_num_bits(EC_GROUP_get0_order(group));
static const uint8_t MSG[1] = {};
static const size_t MSG_LEN = 0;
uint8_t digest[EVP_MAX_MD_SIZE];
size_t digest_len;
const char *digest_name;
switch (order_bits) {
case 256:
SHA256(MSG, MSG_LEN, digest);
digest_len = SHA256_DIGEST_LENGTH;
digest_name = "SHA256";
break;
case 384:
SHA384(MSG, MSG_LEN, digest);
digest_len = SHA384_DIGEST_LENGTH;
digest_name = "SHA384";
break;
default:
abort();
}
uint8_t pub_key_encoded[1024];
size_t pub_key_encoded_len =
EC_POINT_point2oct(group, EC_KEY_get0_public_key(key.get()),
POINT_CONVERSION_UNCOMPRESSED, pub_key_encoded,
sizeof(pub_key_encoded), NULL);
if (pub_key_encoded_len == 0) {
return false;
}
uint8_t sig[1024];
unsigned sig_len = 0;
for (unsigned i = 0; i < 3; ++i) {
if (!ecdsa_sign(sig, &sig_len, key.get(), digest, digest_len, fmt, i)) {
return false;
}
printf("\n");
if (i == 0) {
printf("# S is the maximum length.\n");
} else if (i == 1) {
printf("# S is one byte shorter than the maximum length.\n");
} else {
printf("# S is %d bytes shorter than the maximum length.\n", (int)i);
}
printf("Curve = %s\n", curve_name);
printf("Digest = %s\n", digest_name);
printf("Msg = \"\"\n");
printf("Q = ");
print_hex(stdout, pub_key_encoded, pub_key_encoded_len);
printf("\n");
printf("Sig = ");
print_hex(stdout, sig, sig_len);
printf("\n");
printf("Result = P (0 )\n");
}
return true;
}
static bool GenerateShortSTests(ECDSASigFormat fmt, BN_CTX *ctx) {
if (!GenerateShortSTestsForCurve(NID_X9_62_prime256v1, "P-256", fmt, ctx) ||
!GenerateShortSTestsForCurve(NID_secp384r1, "P-384", fmt, ctx)) {
return false;
}
return true;
}
static bool GenerateECDSATestsForCurve(int nid, const char *curve_name,
BN_ULONG r_word, ECDSASigFormat fmt,
BN_CTX *ctx) {
bssl::UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid));
bssl::UniquePtr<BIGNUM> r(BN_new());
bssl::UniquePtr<BIGNUM> s(BN_new());
if (!group || !r || !s) {
return false;
}
if (!BN_set_word(r.get(), r_word)) {
return false;
}
bssl::UniquePtr<BIGNUM> one(BN_new());
if (!one ||
!BN_one(one.get())) {
return false;
}
bssl::UniquePtr<BIGNUM> zero(BN_new());
if (!zero) {
return false;
}
BN_zero(zero.get());
const BIGNUM *n = EC_GROUP_get0_order(group.get());
bssl::UniquePtr<BIGNUM> n_minus_1(BN_new());
if (!n_minus_1 ||
!BN_copy(n_minus_1.get(), n) ||
!BN_sub_word(n_minus_1.get(), 1)) {
return false;
}
if (!GenerateTestsForRS(group.get(), curve_name, r.get(), nullptr,
zero.get(), fmt, ctx, "F",
"# s == 0 (out of range)") ||
!GenerateTestsForRS(group.get(), curve_name, r.get(), nullptr,
one.get(), fmt, ctx, "P (0 )",
"# s == 1 (minimum allowed)") ||
!GenerateTestsForRS(group.get(), curve_name, r.get(), nullptr, n, fmt, ctx,
"F", "# s == n (out of range)") ||
!GenerateTestsForRS(group.get(), curve_name, r.get(), nullptr,
n_minus_1.get(), fmt, ctx,
"P (0 )", "# s == n - 1 (maximum allowed)")) {
return false;
}
return true;
}
static bool GenerateECDSATests(ECDSASigFormat fmt, BN_CTX *ctx) {
if (!GenerateMaxwellTests(fmt, ctx)) {
return false;
}
printf(
"\n\n# Generated Test vectors edge cases of signature (r, s) values.\n");
if (!GenerateECDSATestsForCurve(NID_X9_62_prime256v1, "P-256", 6, fmt, ctx) ||
!GenerateECDSATestsForCurve(NID_secp384r1, "P-384", 3, fmt, ctx)) {
return false;
}
return true;
}
static void GenerateECCPublicKeyTestEncoded(const char *curve_name,
const uint8_t *pub_key_encoded,
size_t pub_key_encoded_len,
const char *result,
const char *comment) {
printf("\n");
printf("%s\n", comment);
printf("Curve = %s\n", curve_name);
printf("Q = ");
print_hex(stdout, pub_key_encoded, pub_key_encoded_len);
printf("\n");
printf("Result = %s\n", result);
}
static bool GenerateECCPublicKeyTest(const char *curve_name,
const EC_GROUP *group,
const EC_POINT *point, const char *result,
const char *comment) {
uint8_t pub_key_encoded[1024];
size_t pub_key_encoded_len =
EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
pub_key_encoded, sizeof(pub_key_encoded), NULL);
if (pub_key_encoded_len == 0) {
return false;
}
GenerateECCPublicKeyTestEncoded(curve_name, pub_key_encoded,
pub_key_encoded_len, result, comment);
return true;
}
static bool GenerateECCPublicKeyTestWithAffineDecodedCoordinates(
const char *curve_name, const EC_GROUP *group, const BIGNUM *x,
const BIGNUM *y, const char *result, const char *comment) {
unsigned coord_len = (EC_GROUP_get_degree(group) + 7) / 8;
uint8_t pub_key_encoded[1024];
size_t pub_key_encoded_len = 1 + (2 * coord_len);
assert(pub_key_encoded_len <= sizeof(pub_key_encoded));
pub_key_encoded[0] = 0x04; // Uncompressed
if (!BN_bn2bin_padded(&pub_key_encoded[1], coord_len, x) ||
!BN_bn2bin_padded(&pub_key_encoded[1 + coord_len], coord_len, y)) {
return false;
}
GenerateECCPublicKeyTestEncoded(curve_name, pub_key_encoded,
pub_key_encoded_len, result, comment);
return true;
}
static bool GenerateECCPublicKeyTestsForCurve(int nid, const char *curve_name,
BN_CTX *ctx) {
bssl::UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid));
bssl::UniquePtr<BIGNUM> q(BN_new());
if (!group ||
!q ||
!EC_GROUP_get_curve_GFp(group.get(), q.get(), NULL, NULL, NULL)) {
return false;
}
bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group.get()));
if (!point) {
return false;
}
bssl::UniquePtr<BIGNUM> zero(BN_new());
if (!zero) {
return false;
}
BN_zero(zero.get());
bssl::UniquePtr<BIGNUM> y(BN_new());
if (!y) {
return false;
}
if (!EC_POINT_set_compressed_coordinates_GFp(group.get(), point.get(),
zero.get(), 0, ctx) ||
!GenerateECCPublicKeyTest(curve_name, group.get(), point.get(), "P",
"# X == 0, decompressed with y_bit == 0. This verifies that the\n"
"# implementation doesn't reject zero-valued field elements (they\n"
"# aren't scalars).")) {
return false;
}
if (!EC_POINT_get_affine_coordinates_GFp(group.get(), point.get(), NULL,
y.get(), ctx) ||
!GenerateECCPublicKeyTestWithAffineDecodedCoordinates(curve_name,
group.get(),
q.get(), y.get(),
"F (X is out of range)",
"# X == q. This is invalid because q isn't a valid field element. Some\n"
"# broken implementations might accept this if they reduce X mod q\n"
"# since q mod q == 0 and the Y coordinate matches the one from the\n"
"# x == 0 test case above.")) {
return false;
}
if (!EC_POINT_set_compressed_coordinates_GFp(group.get(), point.get(),
zero.get(), 1, ctx) ||
!GenerateECCPublicKeyTest(curve_name, group.get(), point.get(), "P",
"# X == 0, decompressed with y_bit == 1.")) {
return false;
}
if (!EC_POINT_get_affine_coordinates_GFp(group.get(), point.get(), NULL,
y.get(), ctx) ||
!GenerateECCPublicKeyTestWithAffineDecodedCoordinates(curve_name,
group.get(),
q.get(), y.get(),
"F (X is out of range)",
"# X == q, decompressed with y_bit == 1. See the previous X == q test\n"
"# case.")) {
return false;
}
// Find the largest valid x coordinate for the curve.
// XXX: Assumes EC_POINT_set_compressed_coordinates_GFp won't fail for any
// reason other than the X value not resulting in X**3 + a*x + b being a
// perfect square.
bssl::UniquePtr<BIGNUM> largest_x(BN_new());
if (!BN_copy(largest_x.get(), q.get())) {
return false;
}
do {
if (!BN_sub_word(largest_x.get(), 1)) {
return false;
}
} while (!EC_POINT_set_compressed_coordinates_GFp(group.get(), point.get(),
largest_x.get(), 0, ctx));
if (!GenerateECCPublicKeyTest(curve_name, group.get(), point.get(), "P",
"# The largest valid X coordinate, decompressed with y_bit == 0. This\n"
"# helps ensure that the upper bound on coordinate values is not too\n"
"# low.")) {
return false;
}
return true;
}
static bool GenerateECCPublicKeyTests(BN_CTX *ctx) {
printf(
"# Test vectors for Public Key Point Validation.\n"
"#\n"
"# These test vectors were generated by applying the patch in\n"
"# util/generate-tests.patch to BoringSSL, and then running\n"
"# `bssl generate-tests ecc-public-key`.\n"
"#\n");
if (!GenerateECCPublicKeyTestsForCurve(NID_X9_62_prime256v1, "P-256", ctx) ||
!GenerateECCPublicKeyTestsForCurve(NID_secp384r1, "P-384", ctx)) {
return false;
}
return true;
}
struct InterestingPoints {
InterestingPoints(bool *valid, int nid, const char *curve_name, BN_CTX *ctx) {
*valid = false;
group.reset(EC_GROUP_new_by_curve_name(nid));
if (!group) {
return;
}
g_inv.reset(EC_POINT_dup(g(), group.get()));
if (!g_inv ||
!EC_POINT_invert(group.get(), g_inv.get(), ctx)) {
return;
}
inf.reset(EC_POINT_new(group.get()));
if (!inf ||
!EC_POINT_set_to_infinity(group.get(), inf.get())) {
return;
}
inf_n_g.reset(EC_POINT_new(group.get()));
if (!inf_n_g ||
!EC_POINT_mul(group.get(), inf_n_g.get(),
EC_GROUP_get0_order(group.get()), NULL, NULL, ctx)) {
return;
}
bssl::UniquePtr<BIGNUM> nm1(BN_dup(EC_GROUP_get0_order(group.get())));
nm1_g.reset(EC_POINT_new(group.get()));
if (!nm1 ||
!BN_sub_word(nm1.get(), 1) ||
!nm1_g ||
!EC_POINT_mul(group.get(), nm1_g.get(), nm1.get(), NULL, NULL, ctx)) {
return;
}
nm1_g_aff.reset(EC_POINT_dup(nm1_g.get(), group.get()));
if (!nm1_g_aff ||
!EC_POINT_make_affine(group.get(), nm1_g_aff.get(), ctx)) {
return;
}
nm1_g_inv.reset(EC_POINT_dup(nm1_g.get(), group.get()));
if (!nm1_g_inv ||
!EC_POINT_invert(group.get(), nm1_g_inv.get(), ctx)) {
return;
}
nm1_g_inv_aff.reset(EC_POINT_dup(nm1_g_inv.get(), group.get()));
if (!nm1_g_inv_aff ||
!EC_POINT_make_affine(group.get(), nm1_g_inv_aff.get(), ctx)) {
return;
}
// XXX: How does BoringSSL deal with failure to allocate within
// std::string?
this->curve_name = curve_name;
*valid = true;
}
const EC_POINT *g() const {
return EC_GROUP_get0_generator(group.get());
}
bssl::UniquePtr<EC_GROUP> group;
std::string curve_name;
bssl::UniquePtr<EC_POINT> inf;
bssl::UniquePtr<EC_POINT> g_inv; // -G
bssl::UniquePtr<EC_POINT> nm1_g; // (n - 1) * G
bssl::UniquePtr<EC_POINT> nm1_g_aff; // (n - 1) * G (affine)
bssl::UniquePtr<EC_POINT> nm1_g_inv; // inverse of (n - 1) * G
bssl::UniquePtr<EC_POINT> nm1_g_inv_aff; // inverse of (n - 1) * G (affine)
bssl::UniquePtr<EC_POINT> inf_n_g; // n * (affine) G
};
static bool print_point(const EC_GROUP *group, const char *name,
const EC_POINT *p, Affinification aff, BN_CTX *ctx) {
uint8_t buf[1024];
uint8_t num_bytes = (EC_GROUP_get_degree(group) + 7) / 8;
assert(num_bytes <= sizeof(buf));
bssl::UniquePtr<EC_POINT> p_aff;
bool is_infinity = EC_POINT_is_at_infinity(group, p);
if (aff != Unchanged && !is_infinity) {
p_aff.reset(EC_POINT_dup(p, group));
if (!p_aff ||
!EC_POINT_make_affine(group, p_aff.get(), ctx)) {
return false;
}
p = p_aff.get();
}
printf("%s = ", name);
if (is_infinity && aff == MakeAffineToken) {
printf("inf");
} else if (is_infinity && aff == MakeAffineAllZero) {
BIGNUM zero;
BN_init(&zero);
BN_zero(&zero);
if (!BN_bn2bin_padded(buf, num_bytes, &zero)) {
return false;
}
print_hex(stdout, buf, num_bytes);
printf(", ");
print_hex(stdout, buf, num_bytes);
} else {
if (!BN_bn2bin_padded(buf, num_bytes, &p->X)) {
return false;
}
print_hex(stdout, buf, num_bytes);
printf(", ");
if (!BN_bn2bin_padded(buf, num_bytes, &p->Y)) {
return false;
}
print_hex(stdout, buf, num_bytes);
}
if (aff == Unchanged) {
if (!BN_bn2bin_padded(buf, num_bytes, &p->Z)) {
return false;
}
printf(", ");
print_hex(stdout, buf, num_bytes);
}
printf("\n");
return true;
}
static bool GenerateECCPointDoubleTest(const InterestingPoints &points,
size_t n, const EC_POINT *a,
BN_CTX *ctx, const char *comment) {
const EC_GROUP *group = points.group.get();
bssl::UniquePtr<EC_POINT> r(EC_POINT_dup(a, group));
if (!r) {
return false;
}
for (size_t i = 0; i < n; ++i) {
if (!EC_POINT_dbl(group, r.get(), r.get(), ctx) ||
!EC_POINT_make_affine(group, r.get(), ctx)) {
return false;
}
}
printf("\n");
printf("%s\n", comment);
if (!print_point(group, "a", a, Unchanged, ctx) ||
!print_point(group, "r", r.get(), MakeAffineToken, ctx)) {
return false;
}
return true;
}
static bool GenerateECCPointDoubleTestsForCurve(InterestingPoints &points,
BN_CTX *ctx) {
if (!GenerateECCPointDoubleTest(points, 1, points.g(), ctx,
"# G doubled once.") ||
!GenerateECCPointDoubleTest(points, 1, points.inf.get(), ctx,
"# Point at infinity doubled. This uses the (0, 0, 0) representation of\n"
"# the point at infinity instead of the classic (1, 1, 0)\n"
"# representation.") ||
!GenerateECCPointDoubleTest(points, 1, points.inf_n_g.get(), ctx,
"# Point at infinity doubled. This form is the result of multiplying\n"
"# n * G (affine), which is more interesting than the above case\n"
"# because only the Z coordinate is zero.") ||
!GenerateECCPointDoubleTest(points, 1, points.nm1_g.get(), ctx,
"# (n - 1) * G doubled.")) {
}
return true;
}
static bool GenerateECCPointAddTest(InterestingPoints &points,
const EC_POINT *a, const EC_POINT *b,
Affinification b_make_aff,
BN_CTX *ctx, const char *comment) {
if (b_make_aff != Unchanged) {
assert(b_make_aff != MakeAffineToken);
// We never try affine addition when b is the point at infinity since we'd
// never have an affine point that can't be represented in affine
// coordinates, so just skip these.
if (EC_POINT_is_at_infinity(points.group.get(), b)) {
assert(b_make_aff == MakeAffineAllZero);
}
}
const EC_GROUP *group = points.group.get();
bssl::UniquePtr<EC_POINT> r(EC_POINT_new(group));
if (!r) {
return false;
}
if (!EC_POINT_add(group, r.get(), a, b, ctx)) {
return false;
}
printf("\n");
printf("%s\n", comment);
if (!print_point(group, "a", a, Unchanged, ctx) ||
!print_point(group, "b", b, b_make_aff, ctx) ||
!print_point(group, "r", r.get(), MakeAffineToken, ctx)) {
return false;
}
return true;
}
static bool GenerateECCPointAddTestsForCurve(InterestingPoints &points,
Affinification b_aff,
BN_CTX *ctx) {
if (!GenerateECCPointAddTest(points, points.inf.get(), points.inf.get(),
b_aff, ctx,
"# inf + inf == 2 * inf == inf") ||
!GenerateECCPointAddTest(points, points.inf_n_g.get(),
points.inf_n_g.get(), b_aff, ctx,
"# inf (n*G) + inf (n*G) == 2 * inf == inf") ||
!GenerateECCPointAddTest(points, points.inf_n_g.get(), points.inf.get(),
b_aff, ctx,
"# inf (n*G) + inf == 2 * inf == inf") ||
!GenerateECCPointAddTest(points, points.inf.get(), points.inf_n_g.get(),
b_aff, ctx,
"# inf + inf (n*G) == 2 * inf == inf") ||
!GenerateECCPointAddTest(points, points.g(), points.inf.get(), b_aff, ctx,
"# G + inf == G") ||
!GenerateECCPointAddTest(points, points.g(), points.inf_n_g.get(), b_aff, ctx,
"# G + inf (n*G) == G") ||
!GenerateECCPointAddTest(points, points.inf.get(), points.g(), b_aff, ctx,
"# inf + G == G") ||
!GenerateECCPointAddTest(points, points.inf_n_g.get(), points.g(), b_aff,
ctx,
"# inf (n*G) + G == G")) {
return false;
}
if (b_aff == Affinification::Unchanged) {
if (!GenerateECCPointAddTest(points, points.g(), points.g(), b_aff, ctx,
"# G + G == 2*G") ||
!GenerateECCPointAddTest(points, points.nm1_g.get(), points.g(), b_aff, ctx,
"# (n-1)*G + G == inf; note that -G is (n-1)*G")) {
return false;
}
}
if (!GenerateECCPointAddTest(points, points.g(), points.nm1_g.get(), b_aff,
ctx,
"# G + (n-1)*G == inf; note that -G is (n-1)*G")) {
return false;
}
if (b_aff == Affinification::Unchanged) {
if (!GenerateECCPointAddTest(points, points.nm1_g.get(), points.nm1_g.get(),
b_aff, ctx,
"# (n-1)*G + (n-1)*G == 2*(n-1)*G") ||
!GenerateECCPointAddTest(points, points.nm1_g_aff.get(),
points.nm1_g.get(), b_aff, ctx,
"# (n-1)*G + (n-1)*G (affine) == 2*(n-1)*G")) {
return false;
}
}
if (!GenerateECCPointAddTest(points, points.nm1_g.get(),
points.nm1_g_inv.get(), b_aff, ctx,
"# (n-1)*G + -(n-1)*G == inf") ||
!GenerateECCPointAddTest(points, points.nm1_g_inv.get(),
points.nm1_g.get(), b_aff, ctx,
"# -(n-1)*G + (n-1)*G == inf") ||
!GenerateECCPointAddTest(points, points.nm1_g_inv.get(),
points.nm1_g.get(), b_aff, ctx,
"# -(n-1)*G (affine) + (n-1)*G == inf") ||
!GenerateECCPointAddTest(points, points.nm1_g_inv.get(),
points.g_inv.get(), b_aff, ctx,
"# -(n-1)*G + -G == inf; note that -G is (n-1)*G (affine)") ||
!GenerateECCPointAddTest(points, points.g_inv.get(),
points.nm1_g_inv.get(), b_aff, ctx,
"# -G + -(n-1)*G == inf; note that -G is (n-1)*G (affine)")) {
return false;
}
if (b_aff == Affinification::Unchanged) {
if (!GenerateECCPointAddTest(points, points.nm1_g.get(), points.g_inv.get(),
b_aff, ctx,
"# (n-1)*G + -G; == -2*G; note that -G == (n-1)*G (affine)") ||
!GenerateECCPointAddTest(points, points.g_inv.get(), points.nm1_g.get(),
b_aff, ctx,
"# -G + (n-1)*G == -2*G; note that -G is (n-1)*G (affine)") ||
!GenerateECCPointAddTest(points, points.nm1_g.get(), points.g_inv.get(),
b_aff, ctx,
"# (n-1)*G + -G == -2*G; note that -G is (n-1)*G (affine)") ||
!GenerateECCPointAddTest(points, points.g_inv.get(),
points.nm1_g.get(), b_aff, ctx,
"# -G + (n-1)*G == -2*G; note that -G = (n-1)*G")) {
return false;
}
}
if (!GenerateECCPointAddTest(points, points.g_inv.get(), points.g(), b_aff, ctx,
"# -G + G == inf; note that -G is (n-1)*G (affine)") ||
!GenerateECCPointAddTest(points, points.g(),
points.g_inv.get(), b_aff, ctx,
"# G + -G == inf; note that -G is (n-1)*G (affine)")) {
return false;
}
return true;
}
bool GeneratePointMulTest(const InterestingPoints &points,
const BIGNUM *g_scalar, const BIGNUM *p_scalar,
const EC_POINT *p, BN_CTX *ctx) {
bssl::UniquePtr<EC_POINT> result(EC_POINT_new(points.group.get()));
if (!result ||
!EC_POINT_mul(points.group.get(), result.get(), g_scalar, p, p_scalar,
ctx)) {
return false;
}
if (g_scalar != NULL) {
printf("g_scalar = "); print_bn(g_scalar); printf("\n");
}
if (p_scalar != NULL) {
printf("p_scalar = "); print_bn(p_scalar); printf("\n");
if (!print_point(points.group.get(), "p", p, MakeAffineToken, ctx)) {
return false;
}
}
if (EC_POINT_is_at_infinity(points.group.get(), result.get())) {
printf("r = inf\n");
} else {
if (!print_point(points.group.get(), "r", result.get(), MakeAffineToken, ctx)) {
return false;
}
}
return true;
}
bool GeneratePointMulTests(const InterestingPoints &points, bool generator,
bool do_p, BN_CTX *ctx) {
const int SHIFT = 7;
const BN_ULONG N = (1 << SHIFT);
const size_t NUM_SCALARS = (N + 1) + (N - 1) + N;
bssl::UniquePtr<BIGNUM> scalars[NUM_SCALARS];
size_t START_SMALL_HIGH = N + 1;
size_t START_BIG = START_SMALL_HIGH + (N - 1);
int order_bits = EC_GROUP_get_degree(points.group.get());
{
size_t i;
const BIGNUM *n = EC_GROUP_get0_order(points.group.get());
for (i = 0; i <= N; ++i) {
scalars[i].reset(BN_new());
if (!scalars[i] ||
!BN_set_word(scalars[i].get(), i)) {
return false;
}
if (i != 0 && i != N) {
scalars[START_SMALL_HIGH + i - 1].reset(BN_new());
BIGNUM *small_high = scalars[START_SMALL_HIGH + i - 1].get();
if (!small_high ||
!BN_lshift(small_high, scalars[i].get(), order_bits - SHIFT)) {
return false;
}
}
if (i != N) {
scalars[START_BIG + i].reset(BN_dup(n));
if (!scalars[START_BIG + i] ||
!BN_sub_word(scalars[START_BIG + i].get(), N - i)) {
return false;
}
}
}
}
bssl::UniquePtr<EC_POINT> p;
if (do_p) {
bssl::UniquePtr<BIGNUM> n_minus_1(BN_dup(EC_GROUP_get0_order(points.group.get())));
if (!n_minus_1 ||
!BN_sub_word(n_minus_1.get(), 1)) {
return false;
}
p.reset(EC_POINT_new(points.group.get()));
if (!p ||
!EC_POINT_mul(points.group.get(), p.get(), n_minus_1.get(), NULL, NULL,
ctx)) {
return false;
}
}