-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtest_ecc_whitebox.c
More file actions
3894 lines (3533 loc) · 153 KB
/
Copy pathtest_ecc_whitebox.c
File metadata and controls
3894 lines (3533 loc) · 153 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_ecc_whitebox.c
*
* White-box MC/DC supplement for wolfcrypt/src/ecc.c.
*
* The tests/api ECC suite (test_ecc.c, including its MC/DC-focused
* test_wc_EccDecisionCoverage) drives ecc.c through its *public* API. A
* handful of decision conditions live in file-static helpers whose "bad"
* operand combinations are rejected by every public caller *before* the
* helper runs (the caller hard-codes a value, or pre-validates the same
* condition), so those combinations can never be exercised from the API
* without modifying library source. This translation unit reaches them by
* compiling ecc.c directly (#include) and calling the helpers with both
* halves of each MC/DC independence pair.
*
* Coverage from this binary is unioned with the tests/api variant coverage by
* source line:col in the per-module campaign (iso26262/mcdc-per-module):
* llvm-cov computes MC/DC independence PER BINARY, and the campaign's
* aggregate.sh ORs the "independence shown" bit across binaries by key. That
* is why every pair below is completed *within this file* rather than
* relying on the API tests to supply the other half.
*
* Build: compiled by run-mcdc.sh's white-box step with the SAME MC/DC CFLAGS,
* -DHAVE_CONFIG_H and -I<workspace> as the instrumented library, then linked
* against that variant's libwolfssl.a with its ecc.o removed (this TU
* supplies the instrumented ecc.c). NOT part of the wolfSSL build; not
* registered in tests/api. See tests/unit-mcdc/README.md.
*
* Targeted residuals (ecc.c), by class:
* Class 1 wc_ecc_curve_load() dp/pCurve NULL guard ............. 2 conditions
* Class 2 _ecc_import_private_key_ex() key/priv NULL guard ...... 2 conditions
* Class 3 ecc_ctx_set_salt() ctx/flags==0 guard ................. 2 conditions
* Class 4 wc_ecc_ctx_get_own_salt() ctx->protocol==0 half ....... 1 condition
* Class 5 wc_ecc_ctx_set_peer_salt() ctx->protocol==0 half ...... 1 condition
* Class 6 wc_ecc_ctx_set_own_salt() ctx->protocol==0 half ....... 1 condition
* Classes 1-3 are confirmed structurally unreachable through any public
* wrapper (every wrapper hard-codes the "safe" side of the static helper's
* own re-check). Classes 4-6 became API-reachable when
* wc_ecc_ctx_new()/wc_ecc_ctx_new_ex() started accepting flags==0 -
* tests/api/test_ecc.c (test_wc_ecc_ctx_set_peer_salt) now drives them
* through the public API - and are kept here so this binary still completes
* both halves of each pair with a directly-built ctx (see the note above on
* per-binary MC/DC independence). See RESIDUALS.md for everything else.
*/
/* ecc.c refuses the AES-GCM ECIES DEM in the default IV mode unless one of
* WOLFSSL_ECIES_OLD / _GEN_IV / _STATIC_GCM_NONCE is set, so without an opt-in
* every GCM call returns NOT_COMPILED_IN before reaching ecc_is_gcm's callers
* (15809 / 16157) and their operands stay unreachable in every variant. This
* TU compiles its OWN instrumented copy of ecc.c and links against the library
* with ecc.o removed, so the opt-in is local to the white-box binary: it does
* not change the library under test, and it touches nothing outside ecc.c
* (the macro appears in no header, so no shared type or layout moves). The
* campaign unions MC/DC per source line, which is exactly how a white-box is
* meant to add rows the native variants cannot produce. */
#ifndef WOLFSSL_ECIES_STATIC_GCM_NONCE
#define WOLFSSL_ECIES_STATIC_GCM_NONCE
#endif
/* Pull ecc.c in verbatim so the file-static helpers below are in scope and
* instrumented in THIS binary. ecc.c includes settings.h (which picks up
* user_settings.h via -DWOLFSSL_USER_SETTINGS) and ecc.h itself. */
#include <wolfcrypt/src/ecc.c>
#include <stdio.h>
#ifndef INVALID_DEVID
#define INVALID_DEVID (-2)
#endif
static int wb_fail = 0;
#define WB_NOTE(msg) do { printf(" [wb] %s\n", (msg)); } while (0)
#if defined(HAVE_ECC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
/* ------------------------------------------------------------------------- *
* Class 1: wc_ecc_curve_load() dp/pCurve NULL guard (line ~1772).
*
* if (dp == NULL || pCurve == NULL)
*
* Every public caller (wc_ecc_set_curve, wc_ecc_make_key_ex, ...) looks up a
* non-NULL ecc_set_type* from the static ecc_sets[] table and always passes
* the address of a live ecc_curve_spec* local, so neither operand's TRUE
* side is reachable from the API.
* ------------------------------------------------------------------------- */
static void wb_curve_load(void)
{
/* Without ECC_CACHE_CURVE (our variants don't define it), wc_ecc_
* curve_load()'s *pCurve is NOT an out-only "give me a fresh one"
* slot -- the DECLARE_CURVE_SPECS() macro (used by every real caller)
* pre-allocates a real ecc_curve_spec on the caller's stack and passes
* its address; wc_ecc_curve_load() only fills it in. A bare NULL
* ecc_curve_spec* (valid ONLY under ECC_CACHE_CURVE, where the second
* branch of DECLARE_CURVE_SPECS lets the callee allocate/cache it) hits
* "curve = *pCurve; curve->dp != dp" on a NULL curve and crashes. Match
* the real DECLARE_CURVE_SPECS(1) shape here for the all-false call.
*/
ecc_curve_spec* curveNull = NULL;
int ret;
ret = wc_ecc_curve_load(NULL, &curveNull, ECC_CURVE_FIELD_ALL);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_curve_load(dp=NULL) unexpected return");
wb_fail = 1;
}
ret = wc_ecc_curve_load(&ecc_sets[0], NULL, ECC_CURVE_FIELD_ALL);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_curve_load(pCurve=NULL) unexpected return");
wb_fail = 1;
}
/* all-false: real dp + a properly pre-allocated pCurve slot (already
* exercised by every public caller via DECLARE_CURVE_SPECS(), repeated
* here so the independence PAIR -- not just each TRUE half -- lives in
* this binary too). */
{
/* ECC_CURVE_FIELD_ALL loads all six fields (prime/Af/Bf/order/Gx/
* Gy), matching DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT) -- an
* undersized spec_ints (e.g. count 1) makes wc_ecc_curve_load()
* overrun it while loading the later fields. */
DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT);
int allocErr = MP_OKAY;
ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, allocErr);
if (allocErr == MP_OKAY) {
ret = wc_ecc_curve_load(&ecc_sets[0], &curve, ECC_CURVE_FIELD_ALL);
if (ret != 0) {
WB_NOTE("wc_ecc_curve_load(all-false) unexpected return");
wb_fail = 1;
}
wc_ecc_curve_free(curve);
}
else {
WB_NOTE("ALLOC_CURVE_SPECS failed (all-false case skipped)");
}
FREE_CURVE_SPECS();
}
WB_NOTE("wc_ecc_curve_load dp/pCurve NULL guard pairs exercised");
}
/* ------------------------------------------------------------------------- *
* Class 2: _ecc_import_private_key_ex() key/priv NULL guard (line ~11671).
*
* if (key == NULL || priv == NULL)
*
* wc_ecc_import_private_key_ex() (the public wrapper) performs the identical
* check itself before ever reaching the static, so both operands' TRUE side
* inside the static are white-box only. The all-false ("both valid") side
* is exercised elsewhere by tests/api's test_wc_ecc_import_private_key --
* but that is a DIFFERENT binary, and llvm-cov computes MC/DC independence
* per binary, so this function also drives one real (all-false) call
* itself to complete both operands' pairs within this binary.
* ------------------------------------------------------------------------- */
static void wb_import_private_key_ex(void)
{
byte priv[32];
ecc_key key;
int ret;
XMEMSET(priv, 0x5A, sizeof(priv));
XMEMSET(&key, 0, sizeof(key));
ret = _ecc_import_private_key_ex(priv, sizeof(priv), NULL, 0, NULL,
ECC_CURVE_DEF);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("_ecc_import_private_key_ex(key=NULL) unexpected return");
wb_fail = 1;
}
ret = _ecc_import_private_key_ex(NULL, 0, NULL, 0, &key, ECC_CURVE_DEF);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("_ecc_import_private_key_ex(priv=NULL) unexpected return");
wb_fail = 1;
}
/* all-false: real key + real priv (pub left NULL -- private-only
* import), a small but nonzero/in-range scalar (k=1) so wc_ecc_
* set_curve()'s size check and the rest of the import succeed. */
if (wc_ecc_init(&key) == 0) {
byte k1[32];
XMEMSET(k1, 0, sizeof(k1));
k1[sizeof(k1) - 1] = 1;
ret = _ecc_import_private_key_ex(k1, sizeof(k1), NULL, 0, &key,
ECC_SECP256R1);
if (ret != 0) {
WB_NOTE("_ecc_import_private_key_ex(all-false) unexpected return");
wb_fail = 1;
}
wc_ecc_free(&key);
}
WB_NOTE("_ecc_import_private_key_ex key/priv NULL guard pairs exercised");
}
#ifdef HAVE_ECC_ENCRYPT
/* ------------------------------------------------------------------------- *
* Class 3: ecc_ctx_set_salt() ctx/flags==0 guard (line ~14665).
*
* if (ctx == NULL || flags == 0)
*
* Both public callers (wc_ecc_ctx_set_own_salt via REQ_RESP_CLIENT/SERVER,
* ecc_ctx_init) always pass a live ctx and a hard-coded nonzero flags
* (REQ_RESP_CLIENT/REQ_RESP_SERVER); wc_ecc_ctx_reset() skips the call
* entirely when protocol==0, so flags==0 can never be observed from the API.
* ctx==NULL is likewise never forwarded by any caller (they all either
* early-return on their own NULL check or pass &localCtx).
*
* Classes 4-6: ecEncCtx.protocol == 0 halves of the get_own_salt /
* set_peer_salt / set_own_salt guards (lines ~14506, ~14554, ~14646). The
* public constructor wc_ecc_ctx_new()/wc_ecc_ctx_new_ex() does accept
* flags==0 - such a context carries the algorithms and the key type but takes
* no part in the REQ/RESP salt exchange - and these three functions are the
* ones that reject it. Build the ctx directly here since ecEncCtx's full
* definition is only visible inside this TU (it is an opaque
* forward-declared type in ecc.h).
* ------------------------------------------------------------------------- */
static void wb_ctx_set_salt(void)
{
ecEncCtx ctx;
WC_RNG rng;
int ret;
XMEMSET(&ctx, 0, sizeof(ctx));
if (wc_InitRng(&rng) != 0) {
WB_NOTE("wc_InitRng failed (ecc_ctx_set_salt skipped)");
wb_fail = 1;
return;
}
ctx.rng = &rng;
ret = ecc_ctx_set_salt(NULL, REQ_RESP_CLIENT);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("ecc_ctx_set_salt(ctx=NULL) unexpected return");
wb_fail = 1;
}
ret = ecc_ctx_set_salt(&ctx, 0);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("ecc_ctx_set_salt(flags=0) unexpected return");
wb_fail = 1;
}
/* all-false: real ctx + real flags value. */
ret = ecc_ctx_set_salt(&ctx, REQ_RESP_CLIENT);
if (ret != 0) {
WB_NOTE("ecc_ctx_set_salt(all-false) unexpected return");
wb_fail = 1;
}
(void)wc_FreeRng(&rng);
WB_NOTE("ecc_ctx_set_salt ctx/flags==0 guard pairs exercised");
}
/* llvm-cov computes MC/DC independence PER BINARY: this whitebox TU is the
* ONLY place wc_ecc_ctx_set_own_salt() is ever called at all (the tests/api
* suite only exercises wc_ecc_ctx_set_peer_salt()), so every operand of
* both functions' 3-operand OR guard --
* if (ctx == NULL || ctx->protocol == 0 || salt == NULL)
* -- must get its full independence pair (toggle one operand, hold the
* other two fixed at their "continue" value) from calls made HERE; a TRUE
* shown in one binary and a FALSE shown in another do not combine. */
static void wb_ctx_protocol_zero(void)
{
ecEncCtx zeroCtx; /* protocol left at 0 by XMEMSET */
ecEncCtx liveCtx;
WC_RNG rng;
byte salt[EXCHANGE_SALT_SZ];
XMEMSET(&zeroCtx, 0, sizeof(zeroCtx));
XMEMSET(salt, 0x11, sizeof(salt));
/* wc_ecc_ctx_get_own_salt: protocol==0 TRUE half (line ~14506). The
* NULL/valid halves are already shown by the tests/api suite. */
if (wc_ecc_ctx_get_own_salt(&zeroCtx) != NULL) {
WB_NOTE("wc_ecc_ctx_get_own_salt(protocol=0) unexpected non-NULL");
wb_fail = 1;
}
if (wc_InitRng(&rng) != 0) {
WB_NOTE("wc_InitRng failed (protocol guard pairs skipped)");
return;
}
ecc_ctx_init(&liveCtx, REQ_RESP_CLIENT, &rng);
/* wc_ecc_ctx_get_own_salt: protocol==0 FALSE half (live ctx, real
* protocol) -- completes the independence pair for that operand
* within this binary (the TRUE half was zeroCtx above). */
if (wc_ecc_ctx_get_own_salt(&liveCtx) == NULL) {
WB_NOTE("wc_ecc_ctx_get_own_salt(live) unexpected NULL");
wb_fail = 1;
}
/* ---- wc_ecc_ctx_set_peer_salt (line ~14554): ctx==NULL and salt==NULL
* halves are already shown by tests/api's test_wc_ecc_ctx_set_peer_salt
* (same pattern, different binary -- doesn't count here); supply ALL
* THREE operands' pairs in this one binary too so this binary's own
* MC/DC is complete independent of what the API binary shows. */
if (wc_ecc_ctx_set_peer_salt(NULL, salt) != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_ctx_set_peer_salt(ctx=NULL) unexpected return");
wb_fail = 1;
}
if (wc_ecc_ctx_set_peer_salt(&zeroCtx, salt) !=
WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_ctx_set_peer_salt(protocol=0) unexpected return");
wb_fail = 1;
}
if (wc_ecc_ctx_set_peer_salt(&liveCtx, NULL) !=
WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_ctx_set_peer_salt(salt=NULL) unexpected return");
wb_fail = 1;
}
/* all-false companion: real ctx (protocol != 0), real salt. */
(void)wc_ecc_ctx_set_peer_salt(&liveCtx, salt);
/* ---- wc_ecc_ctx_set_own_salt (line ~14646): never called anywhere
* else, so needs its complete independence set here. */
if (wc_ecc_ctx_set_own_salt(NULL, salt, sizeof(salt)) !=
WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_ctx_set_own_salt(ctx=NULL) unexpected return");
wb_fail = 1;
}
if (wc_ecc_ctx_set_own_salt(&zeroCtx, salt, sizeof(salt)) !=
WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_ctx_set_own_salt(protocol=0) unexpected return");
wb_fail = 1;
}
if (wc_ecc_ctx_set_own_salt(&liveCtx, NULL, 0) !=
WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_ctx_set_own_salt(salt=NULL) unexpected return");
wb_fail = 1;
}
/* all-false companion: real ctx (protocol != 0), real salt. */
(void)wc_ecc_ctx_set_own_salt(&liveCtx, salt, sizeof(salt));
(void)wc_FreeRng(&rng);
WB_NOTE("ecEncCtx protocol==0 guard halves exercised");
}
#else
static void wb_ctx_set_salt(void) { WB_NOTE("HAVE_ECC_ENCRYPT off; skipped"); }
static void wb_ctx_protocol_zero(void)
{
WB_NOTE("HAVE_ECC_ENCRYPT off; skipped");
}
#endif /* HAVE_ECC_ENCRYPT */
/* ------------------------------------------------------------------------- *
* Class 7: wc_ecc_is_valid_idx() "n < x" independence (line ~4335).
*
* if ((n >= ECC_CUSTOM_IDX) && (n < x)) { return 1; }
*
* Every public caller passes either a real ecc_sets[] index (n < x, always
* true) or a value already rejected by the earlier "n >= ECC_SET_COUNT"
* guard, so the "n < x" operand's FALSE half (n >= ECC_CUSTOM_IDX true, but
* n not less than the live table size x) is never observed. n == x (the
* table's own terminator slot) is the smallest value that is still
* ECC_CUSTOM_IDX and still under ECC_SET_COUNT.
* ------------------------------------------------------------------------- */
static void wb_is_valid_idx_n_lt_x(void)
{
int x;
for (x = 0; ecc_sets[x].size != 0; x++) { }
if (wc_ecc_is_valid_idx(x) != 0) {
WB_NOTE("wc_ecc_is_valid_idx(terminator idx) unexpected valid");
wb_fail = 1;
}
WB_NOTE("wc_ecc_is_valid_idx n<x independence exercised");
}
/* ------------------------------------------------------------------------- *
* Class 8: wc_ecc_cmp_param() param/curveParam NULL guard (line ~4460).
*
* if (param == NULL || curveParam == NULL) return BAD_FUNC_ARG;
*
* Both public callers always pass live ecc_sets[] hex strings and a live
* caller-supplied buffer, so neither NULL half is reachable from the API.
* File-static: only callable because this TU #includes ecc.c.
* ------------------------------------------------------------------------- */
static void wb_cmp_param_null(void)
{
byte param[4] = { 1, 2, 3, 4 };
int ret;
ret = wc_ecc_cmp_param(NULL, param, sizeof(param), WC_TYPE_UNSIGNED_BIN);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_cmp_param(curveParam=NULL) unexpected return");
wb_fail = 1;
}
ret = wc_ecc_cmp_param("01020304", NULL, 0, WC_TYPE_UNSIGNED_BIN);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
WB_NOTE("wc_ecc_cmp_param(param=NULL) unexpected return");
wb_fail = 1;
}
/* all-false companion, in this binary. */
(void)wc_ecc_cmp_param("01020304", param, sizeof(param),
WC_TYPE_UNSIGNED_BIN);
WB_NOTE("wc_ecc_cmp_param NULL guard pairs exercised");
}
/* Convert a curve hex-string field to its raw big-endian bytes, using the
* same mp_read_radix()/mp_to_unsigned_bin() the library itself uses -- so
* the bytes compare equal (via wc_ecc_cmp_param) to the source hex string
* regardless of any leading-zero-nibble width difference. */
#define WB_MAXFIELD 100 /* > any ecc_sets[] field at P-521 (66 bytes) */
static int wb_hex_to_bin(const char* hex, byte* out, word32* outLen)
{
mp_int t;
int err;
int sz;
if (mp_init(&t) != MP_OKAY)
return -1;
err = mp_read_radix(&t, hex, MP_RADIX_HEX);
if (err == MP_OKAY) {
sz = mp_unsigned_bin_size(&t);
if (sz < 0 || (word32)sz > WB_MAXFIELD) {
err = -1;
}
else {
*outLen = (word32)sz;
err = mp_to_unsigned_bin(&t, out);
}
}
mp_clear(&t);
return err;
}
/* ------------------------------------------------------------------------- *
* Class 9/10: wc_ecc_get_curve_id_from_params() (lines ~4537, ~4545).
*
* NULL OR guard: prime==NULL || Af==NULL || Bf==NULL || order==NULL ||
* Gx==NULL || Gy==NULL
* AND match chain: prime match && Af match && Bf match && order match &&
* Gx match && Gy match && cofactor match
*
* No tests/api caller ever supplies a real matching parameter set (every
* caller either passes a deliberately-wrong set to prove ECC_CURVE_INVALID,
* or does not call this function at all), so neither the NULL guard's non-
* prime operands nor any operand of the match chain past "prime" has its
* independence pair shown anywhere. Build a byte-exact copy of a real
* curve's fields (SECP256R1) to drive both.
* ------------------------------------------------------------------------- */
static void wb_get_curve_id_from_params(void)
{
int idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
const ecc_set_type* cs;
byte prime[WB_MAXFIELD], Af[WB_MAXFIELD], Bf[WB_MAXFIELD];
byte order[WB_MAXFIELD], Gx[WB_MAXFIELD], Gy[WB_MAXFIELD];
byte bad[WB_MAXFIELD];
word32 primeSz, AfSz, BfSz, orderSz, GxSz, GySz;
int fieldSize, ret;
if (idx == ECC_CURVE_INVALID) {
WB_NOTE("SECP256R1 not in ecc_sets[]; skipped");
wb_fail = 1;
return;
}
cs = wc_ecc_get_curve_params(idx);
fieldSize = cs->size * 8;
if (wb_hex_to_bin(cs->prime, prime, &primeSz) != 0 ||
wb_hex_to_bin(cs->Af, Af, &AfSz) != 0 ||
wb_hex_to_bin(cs->Bf, Bf, &BfSz) != 0 ||
wb_hex_to_bin(cs->order, order, &orderSz) != 0 ||
wb_hex_to_bin(cs->Gx, Gx, &GxSz) != 0 ||
wb_hex_to_bin(cs->Gy, Gy, &GySz) != 0) {
WB_NOTE("wb_hex_to_bin failed; wb_get_curve_id_from_params skipped");
wb_fail = 1;
return;
}
/* ---- NULL OR guard: isolate each non-prime operand, rest valid ---- */
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, NULL,
AfSz, Bf, BfSz, order, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, NULL, BfSz, order, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, NULL, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, order, orderSz, NULL, GxSz, Gy, GySz, cs->cofactor);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, order, orderSz, Gx, GxSz, NULL, GySz, cs->cofactor);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
if (wb_fail) {
WB_NOTE("wc_ecc_get_curve_id_from_params NULL guard unexpected");
}
/* ---- all-valid baseline: full real match, needed in THIS binary ---- */
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, order, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != cs->id) {
WB_NOTE("wc_ecc_get_curve_id_from_params(all-match) unexpected id");
wb_fail = 1;
}
/* ---- AND-chain independence: corrupt exactly one field, keep the
* rest matching, expect ECC_CURVE_INVALID (no ecc_sets[] entry can
* coincidentally match a flipped cryptographic field of the same
* length). ---- */
#define WB_CORRUPT1(buf, len) do { \
XMEMCPY(bad, (buf), (len)); \
bad[(len) - 1] = (byte)(bad[(len) - 1] ^ 0xFFu); \
} while (0)
WB_CORRUPT1(Af, AfSz);
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, bad,
AfSz, Bf, BfSz, order, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
WB_CORRUPT1(Bf, BfSz);
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, bad, BfSz, order, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
WB_CORRUPT1(order, orderSz);
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, bad, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
WB_CORRUPT1(Gx, GxSz);
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, order, orderSz, bad, GxSz, Gy, GySz, cs->cofactor);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
WB_CORRUPT1(Gy, GySz);
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, order, orderSz, Gx, GxSz, bad, GySz, cs->cofactor);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
ret = wc_ecc_get_curve_id_from_params(fieldSize, prime, primeSz, Af,
AfSz, Bf, BfSz, order, orderSz, Gx, GxSz, Gy, GySz, cs->cofactor + 1);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
#undef WB_CORRUPT1
WB_NOTE("wc_ecc_get_curve_id_from_params NULL+match-chain pairs done");
}
/* ------------------------------------------------------------------------- *
* Class 11: wc_ecc_get_curve_id_from_dp_params() (lines ~4580, ~4591).
*
* 7-operand OR guard: dp==NULL || dp->prime==NULL || dp->Af==NULL ||
* dp->Bf==NULL || dp->order==NULL || dp->Gx==NULL || dp->Gy==NULL
* AND match chain: same 6 hex-string fields (WC_TYPE_HEX_STR) + cofactor.
*
* No public caller ever builds an ecc_set_type with a live curve's own hex
* strings copied in field-by-field (real callers pass a whole, pre-existing
* dp), so every operand past "dp itself" is unreached both in the OR guard
* and the match chain.
* ------------------------------------------------------------------------- */
static void wb_get_curve_id_from_dp_params(void)
{
int idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
const ecc_set_type* cs;
ecc_set_type dp;
char bad[WB_MAXFIELD * 2 + 4];
int ret;
if (idx == ECC_CURVE_INVALID) {
WB_NOTE("SECP256R1 not in ecc_sets[]; skipped");
wb_fail = 1;
return;
}
cs = wc_ecc_get_curve_params(idx);
ret = wc_ecc_get_curve_id_from_dp_params(NULL);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
XMEMSET(&dp, 0, sizeof(dp));
dp.size = cs->size;
dp.cofactor = cs->cofactor;
dp.Af = cs->Af; dp.Bf = cs->Bf; dp.order = cs->order;
dp.Gx = cs->Gx; dp.Gy = cs->Gy;
dp.prime = NULL;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
dp.prime = cs->prime;
dp.Af = NULL;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
dp.Af = cs->Af;
dp.Bf = NULL;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
dp.Bf = cs->Bf;
dp.order = NULL;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
dp.order = cs->order;
dp.Gx = NULL;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
dp.Gx = cs->Gx;
dp.Gy = NULL;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
dp.Gy = cs->Gy;
if (wb_fail) {
WB_NOTE("wc_ecc_get_curve_id_from_dp_params NULL guard unexpected");
}
/* all-valid baseline: full real match. */
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != cs->id) {
WB_NOTE("wc_ecc_get_curve_id_from_dp_params(all-match) unexpected");
wb_fail = 1;
}
/* AND-chain independence: corrupt one hex digit at a time (same
* strlen, so the "strlen mismatch" fast-reject in wc_ecc_cmp_param
* does not short-circuit before the byte compare runs), keep every
* other field matching. */
#define WB_CORRUPT_HEX(field) do { \
size_t wb_n = XSTRLEN((field)); \
XSTRNCPY(bad, (field), sizeof(bad) - 1); \
bad[sizeof(bad) - 1] = '\0'; \
bad[wb_n - 1] = (bad[wb_n - 1] == '0') ? '1' : '0'; \
} while (0)
WB_CORRUPT_HEX(cs->Af); dp.Af = bad;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
dp.Af = cs->Af;
WB_CORRUPT_HEX(cs->Bf); dp.Bf = bad;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
dp.Bf = cs->Bf;
WB_CORRUPT_HEX(cs->order); dp.order = bad;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
dp.order = cs->order;
WB_CORRUPT_HEX(cs->Gx); dp.Gx = bad;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
dp.Gx = cs->Gx;
WB_CORRUPT_HEX(cs->Gy); dp.Gy = bad;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
dp.Gy = cs->Gy;
#undef WB_CORRUPT_HEX
dp.cofactor = cs->cofactor + 1;
ret = wc_ecc_get_curve_id_from_dp_params(&dp);
if (ret != ECC_CURVE_INVALID) { wb_fail = 1; }
WB_NOTE("wc_ecc_get_curve_id_from_dp_params NULL+match-chain pairs done");
}
/* ------------------------------------------------------------------------- *
* Class 12: wc_ecc_mulmod_ex2() 4-operand NULL guard, generic (!SP_MATH)
* path (line ~4009): k==NULL || G==NULL || R==NULL || modulus==NULL.
*
* No variant here defines bare WOLFSSL_SP_MATH, so this is the branch every
* variant compiles. No current test calls this entry point directly (real
* traffic goes through wc_ecc_mulmod_ex/wc_ecc_mulmod, or the FP_ECC/SP
* layers above it), so not even the all-valid baseline is shown elsewhere
* in this binary; supply the full independence set plus one real call.
* ------------------------------------------------------------------------- */
static void wb_mulmod_ex2_null_guard(void)
{
ecc_point *G = NULL, *R = NULL;
mp_int k, a, modulus, order;
int ret;
if (mp_init_multi(&k, &a, &modulus, &order, NULL, NULL) != MP_OKAY) {
WB_NOTE("mp_init_multi failed; wb_mulmod_ex2_null_guard skipped");
wb_fail = 1;
return;
}
(void)mp_set(&k, 3);
(void)mp_set(&a, 2);
(void)mp_set_int(&modulus, 1000000007uL);
(void)mp_set_int(&order, 1000000007uL);
G = wc_ecc_new_point();
R = wc_ecc_new_point();
if (G == NULL || R == NULL) {
WB_NOTE("wc_ecc_new_point failed; wb_mulmod_ex2_null_guard skipped");
wb_fail = 1;
goto out;
}
(void)mp_set(G->x, 5);
(void)mp_set(G->y, 7);
(void)mp_set(G->z, 1);
ret = wc_ecc_mulmod_ex2(NULL, G, R, &a, &modulus, &order, NULL, 1, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = wc_ecc_mulmod_ex2(&k, NULL, R, &a, &modulus, &order, NULL, 1, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = wc_ecc_mulmod_ex2(&k, G, NULL, &a, &modulus, &order, NULL, 1, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = wc_ecc_mulmod_ex2(&k, G, R, &a, NULL, &order, NULL, 1, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
/* all-false baseline: real (if not curve-accurate) arithmetic inputs;
* the generic point-math does not require the operands to satisfy a
* real curve equation, only that modulus is odd (for montgomery). */
(void)wc_ecc_mulmod_ex2(&k, G, R, &a, &modulus, &order, NULL, 1, NULL);
if (wb_fail) {
WB_NOTE("wc_ecc_mulmod_ex2 NULL guard unexpected return");
}
WB_NOTE("wc_ecc_mulmod_ex2 4-operand NULL guard pairs exercised");
out:
wc_ecc_del_point(G);
wc_ecc_del_point(R);
mp_clear(&order);
mp_clear(&modulus);
mp_clear(&a);
mp_clear(&k);
}
/* ------------------------------------------------------------------------- *
* Class 13: ecc_map_ex() P/modulus NULL guard (line ~2791).
*
* if (P == NULL || modulus == NULL) return ECC_BAD_ARG_E;
*
* Every caller of ecc_map()/ecc_map_ex() passes a live point off the stack
* and a live curve modulus, so neither NULL half is reachable via the API.
* ------------------------------------------------------------------------- */
static void wb_ecc_map_ex_null(void)
{
ecc_point* P;
mp_int modulus;
int ret;
if (mp_init(&modulus) != MP_OKAY) {
wb_fail = 1;
return;
}
(void)mp_set_int(&modulus, 1000000007uL);
P = wc_ecc_new_point();
if (P == NULL) {
mp_clear(&modulus);
wb_fail = 1;
return;
}
(void)mp_set(P->x, 3);
(void)mp_set(P->y, 5);
(void)mp_set(P->z, 1);
ret = ecc_map_ex(NULL, &modulus, 0, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = ecc_map_ex(P, NULL, 0, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
wc_ecc_del_point(P);
mp_clear(&modulus);
WB_NOTE("ecc_map_ex P/modulus NULL guard pairs exercised");
}
/* ------------------------------------------------------------------------- *
* Class 14: ecc_projective_add_point()/ecc_projective_dbl_point() public
* wrappers (lines ~2393/2397, ~2761/2764): NULL guard + coordinate range
* check ("mp_cmp(..) != MP_LT" over x/y/z of both operands).
*
* These wrappers are compiled unconditionally in ecc.c (always in scope for
* a same-TU #include), but only PROTOTYPED under WOLFSSL_PUBLIC_ECC_ADD_DBL
* -- no in-tree caller (all of which use the _safe() variants) reaches them
* at all, so no operand of either guard has any coverage.
* ------------------------------------------------------------------------- */
static void wb_projective_wrappers(void)
{
ecc_point *P, *Q, *R;
mp_int a, modulus;
int ret;
if (mp_init_multi(&a, &modulus, NULL, NULL, NULL, NULL) != MP_OKAY) {
wb_fail = 1;
return;
}
(void)mp_set(&a, 2);
(void)mp_set_int(&modulus, 1000000007uL);
P = wc_ecc_new_point();
Q = wc_ecc_new_point();
R = wc_ecc_new_point();
if (P == NULL || Q == NULL || R == NULL) {
wb_fail = 1;
goto out;
}
(void)mp_set(P->x, 3); (void)mp_set(P->y, 5); (void)mp_set(P->z, 1);
(void)mp_set(Q->x, 11); (void)mp_set(Q->y, 13); (void)mp_set(Q->z, 1);
/* ---- ecc_projective_add_point: NULL guard, each operand isolated ---- */
ret = ecc_projective_add_point(NULL, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = ecc_projective_add_point(P, NULL, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = ecc_projective_add_point(P, Q, NULL, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = ecc_projective_add_point(P, Q, R, &a, NULL, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
/* ---- range check: each of the 6 coordinate comparisons isolated,
* one coordinate at a time set >= modulus, rest in range. ---- */
(void)mp_set_int(P->x, 1000000007uL); /* == modulus: not MP_LT */
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(P->x, 3);
(void)mp_set_int(P->y, 1000000007uL);
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(P->y, 5);
(void)mp_set_int(P->z, 1000000007uL);
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(P->z, 1);
(void)mp_set_int(Q->x, 1000000007uL);
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(Q->x, 11);
(void)mp_set_int(Q->y, 1000000007uL);
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(Q->y, 13);
(void)mp_set_int(Q->z, 1000000007uL);
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(Q->z, 1);
/* all-in-range baseline (drives the real _ecc_projective_add_point). */
ret = ecc_projective_add_point(P, Q, R, &a, &modulus, 0);
if (ret != MP_OKAY) { wb_fail = 1; }
/* ---- ecc_projective_dbl_point: NULL guard + range check, same idea
* with 3 operands (P/R/modulus; no Q). ---- */
ret = ecc_projective_dbl_point(NULL, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = ecc_projective_dbl_point(P, NULL, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = ecc_projective_dbl_point(P, R, &a, NULL, 0);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
(void)mp_set_int(P->x, 1000000007uL);
ret = ecc_projective_dbl_point(P, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(P->x, 3);
(void)mp_set_int(P->y, 1000000007uL);
ret = ecc_projective_dbl_point(P, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(P->y, 5);
(void)mp_set_int(P->z, 1000000007uL);
ret = ecc_projective_dbl_point(P, R, &a, &modulus, 0);
if (ret != WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) { wb_fail = 1; }
(void)mp_set(P->z, 1);
ret = ecc_projective_dbl_point(P, R, &a, &modulus, 0);
if (ret != MP_OKAY) { wb_fail = 1; }
if (wb_fail) {
WB_NOTE("ecc_projective_add/dbl_point wrapper unexpected return");
}
WB_NOTE("ecc_projective_add/dbl_point wrapper NULL+range pairs done");
out:
wc_ecc_del_point(P);
wc_ecc_del_point(Q);
wc_ecc_del_point(R);
mp_clear(&modulus);
mp_clear(&a);
}
/* ECC_SHAMIR is unconditional in the base config (see modules.json "ecc"
* notes): under it, ecc_mul2add() is `static normal_ecc_mul2add()` (FP_ECC
* on, the default) or the public `ecc_mul2add()` itself (no_fp_shamir, FP_ECC
* off) -- select the same symbol the source itself would use. */
#ifdef ECC_SHAMIR
#ifdef FP_ECC
#define WB_MUL2ADD_FN normal_ecc_mul2add
#else
#define WB_MUL2ADD_FN ecc_mul2add
#endif
/* ------------------------------------------------------------------------- *
* Class 15: ecc_mul2add()/normal_ecc_mul2add() (line ~8774): 6-operand NULL
* guard (A/kA/B/kB/C/modulus -- "a" is not checked) and the ECC_BUFSIZE
* scalar-length sanity check (line ~8861).
*
* No caller passes a NULL operand (every public path -- verify_hash's
* Shamir's-trick optimization -- already validated key/hash/sig upstream),
* and no caller's scalar ever exceeds ECC_BUFSIZE (257) bytes (they are all
* bounded by a curve order), so neither guard's TRUE half is reachable via
* the API. Drives real SECP256R1 generator-point arithmetic (kA=3, kB=5) so
* the all-false baseline is also completed within this binary.
* ------------------------------------------------------------------------- */
static void wb_mul2add_and_bufsize(void)
{
int idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
const ecc_set_type* cs;
mp_int a, modulus, kA, kB, kBig;
ecc_point *A = NULL, *B = NULL, *C = NULL;
byte fbuf[WB_MAXFIELD];
byte bigbuf[300];
word32 sz;
int ret;
if (idx == ECC_CURVE_INVALID) {
wb_fail = 1;
return;
}
cs = wc_ecc_get_curve_params(idx);
if (mp_init_multi(&a, &modulus, &kA, &kB, &kBig, NULL) != MP_OKAY) {
wb_fail = 1;
return;
}
if (wb_hex_to_bin(cs->Af, fbuf, &sz) != 0 ||
mp_read_unsigned_bin(&a, fbuf, (int)sz) != MP_OKAY) {
wb_fail = 1; goto out;
}
if (wb_hex_to_bin(cs->prime, fbuf, &sz) != 0 ||
mp_read_unsigned_bin(&modulus, fbuf, (int)sz) != MP_OKAY) {
wb_fail = 1; goto out;
}
(void)mp_set(&kA, 3);
(void)mp_set(&kB, 5);
XMEMSET(bigbuf, 0xFF, sizeof(bigbuf)); /* > ECC_BUFSIZE (257) bytes */
if (mp_read_unsigned_bin(&kBig, bigbuf, (int)sizeof(bigbuf)) != MP_OKAY) {
wb_fail = 1; goto out;
}
A = wc_ecc_new_point();
B = wc_ecc_new_point();
C = wc_ecc_new_point();
if (A == NULL || B == NULL || C == NULL) {
wb_fail = 1; goto out;
}
if (wb_hex_to_bin(cs->Gx, fbuf, &sz) != 0 ||
mp_read_unsigned_bin(A->x, fbuf, (int)sz) != MP_OKAY) {
wb_fail = 1; goto out;
}
if (wb_hex_to_bin(cs->Gy, fbuf, &sz) != 0 ||
mp_read_unsigned_bin(A->y, fbuf, (int)sz) != MP_OKAY) {
wb_fail = 1; goto out;
}
(void)mp_set(A->z, 1);
(void)mp_copy(A->x, B->x);
(void)mp_copy(A->y, B->y);
(void)mp_copy(A->z, B->z);
/* ---- 6-operand NULL guard, each isolated ---- */
ret = WB_MUL2ADD_FN(NULL, &kA, B, &kB, C, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = WB_MUL2ADD_FN(A, NULL, B, &kB, C, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = WB_MUL2ADD_FN(A, &kA, NULL, &kB, C, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = WB_MUL2ADD_FN(A, &kA, B, NULL, C, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = WB_MUL2ADD_FN(A, &kA, B, &kB, NULL, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
ret = WB_MUL2ADD_FN(A, &kA, B, &kB, C, &a, NULL, NULL);
if (ret != WC_NO_ERR_TRACE(ECC_BAD_ARG_E)) { wb_fail = 1; }
/* ---- ECC_BUFSIZE check: lenA/lenB isolated ---- */
ret = WB_MUL2ADD_FN(A, &kBig, B, &kB, C, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
ret = WB_MUL2ADD_FN(A, &kA, B, &kBig, C, &a, &modulus, NULL);
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) { wb_fail = 1; }
/* ---- all-false baseline: real kA*G + kB*G Shamir computation ---- */
ret = WB_MUL2ADD_FN(A, &kA, B, &kB, C, &a, &modulus, NULL);
if (ret != MP_OKAY) { wb_fail = 1; }
if (wb_fail) {
WB_NOTE("ecc_mul2add NULL/ECC_BUFSIZE guard unexpected return");
}
WB_NOTE("ecc_mul2add NULL guard + ECC_BUFSIZE pairs done");
out:
wc_ecc_del_point(A);
wc_ecc_del_point(B);
wc_ecc_del_point(C);
mp_clear(&kBig);
mp_clear(&kB);
mp_clear(&kA);
mp_clear(&modulus);
mp_clear(&a);
}