@@ -63,6 +63,7 @@ pub type EcdhHashFn = unsafe extern "C" fn(
63
63
#[ cfg( feature = "fuzztarget" ) ]
64
64
impl Context {
65
65
pub fn flags ( & self ) -> u32 {
66
+ unsafe { assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ; }
66
67
self . 0 as u32
67
68
}
68
69
}
@@ -399,6 +400,8 @@ mod fuzz_dummy {
399
400
use self :: std:: { ptr, mem} ;
400
401
use self :: std:: boxed:: Box ;
401
402
403
+ pub static mut UNSAFE_CRYPTO_FUZZING : bool = false ;
404
+
402
405
extern "C" {
403
406
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn ;
404
407
pub static secp256k1_nonce_function_rfc6979: NonceFn ;
@@ -408,36 +411,43 @@ mod fuzz_dummy {
408
411
// Contexts
409
412
/// Creates a dummy context, tracking flags to ensure proper calling semantics
410
413
pub unsafe fn secp256k1_context_preallocated_create ( _ptr : * mut c_void , flags : c_uint ) -> * mut Context {
414
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
411
415
let b = Box :: new ( Context ( flags as i32 ) ) ;
412
416
Box :: into_raw ( b)
413
417
}
414
418
415
419
/// Return dummy size of context struct.
416
420
pub unsafe fn secp256k1_context_preallocated_size ( _flags : c_uint ) -> usize {
421
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
417
422
mem:: size_of :: < Context > ( )
418
423
}
419
424
420
425
/// Return dummy size of context struct.
421
426
pub unsafe fn secp256k1_context_preallocated_clone_size ( cx : * mut Context ) -> usize {
427
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
422
428
mem:: size_of :: < Context > ( )
423
429
}
424
430
425
431
/// Copies a dummy context
426
432
pub unsafe fn secp256k1_context_preallocated_clone ( cx : * const Context , prealloc : * mut c_void ) -> * mut Context {
433
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
427
434
let ret = prealloc as * mut Context ;
428
435
* ret = ( * cx) . clone ( ) ;
429
436
ret
430
437
}
431
438
432
439
/// "Destroys" a dummy context
433
- pub unsafe fn secp256k1_context_preallocated_destroy ( cx : * mut Context ) {
440
+ pub unsafe fn secp256k1_context_preallocated_destroy ( cx : * mut Context )
441
+ {
442
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
434
443
( * cx) . 0 = 0 ;
435
444
}
436
445
437
446
/// Asserts that cx is properly initialized
438
447
pub unsafe fn secp256k1_context_randomize ( cx : * mut Context ,
439
448
_seed32 : * const c_uchar )
440
449
-> c_int {
450
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
441
451
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
442
452
1
443
453
}
@@ -454,6 +464,7 @@ mod fuzz_dummy {
454
464
pub unsafe fn secp256k1_ec_pubkey_parse ( cx : * const Context , pk : * mut PublicKey ,
455
465
input : * const c_uchar , in_len : usize )
456
466
-> c_int {
467
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
457
468
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
458
469
match in_len {
459
470
33 => {
@@ -482,6 +493,7 @@ mod fuzz_dummy {
482
493
out_len : * mut usize , pk : * const PublicKey ,
483
494
compressed : c_uint )
484
495
-> c_int {
496
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
485
497
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
486
498
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
487
499
if compressed == SECP256K1_SER_COMPRESSED {
@@ -513,6 +525,7 @@ mod fuzz_dummy {
513
525
pub unsafe fn secp256k1_ecdsa_signature_parse_compact ( cx : * const Context , sig : * mut Signature ,
514
526
input64 : * const c_uchar )
515
527
-> c_int {
528
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
516
529
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
517
530
if secp256k1_ec_seckey_verify ( cx, input64. offset ( 32 ) ) != 1 { return 0 ; } // sig should be msg32||sk
518
531
ptr:: copy ( input64, ( * sig) . 0 [ ..] . as_mut_ptr ( ) , 64 ) ;
@@ -529,6 +542,7 @@ mod fuzz_dummy {
529
542
pub unsafe fn secp256k1_ecdsa_signature_serialize_der ( cx : * const Context , output : * mut c_uchar ,
530
543
out_len : * mut usize , sig : * const Signature )
531
544
-> c_int {
545
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
532
546
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
533
547
534
548
let mut len_r = 33 ;
@@ -567,6 +581,7 @@ mod fuzz_dummy {
567
581
pub unsafe fn secp256k1_ecdsa_signature_serialize_compact ( cx : * const Context , output64 : * mut c_uchar ,
568
582
sig : * const Signature )
569
583
-> c_int {
584
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
570
585
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
571
586
ptr:: copy ( ( * sig) . 0 [ ..] . as_ptr ( ) , output64, 64 ) ;
572
587
1
@@ -585,6 +600,7 @@ mod fuzz_dummy {
585
600
msg32 : * const c_uchar ,
586
601
pk : * const PublicKey )
587
602
-> c_int {
603
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
588
604
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
589
605
assert ! ( ( * cx) . 0 as u32 & SECP256K1_START_VERIFY == SECP256K1_START_VERIFY ) ;
590
606
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
@@ -608,6 +624,7 @@ mod fuzz_dummy {
608
624
_noncefn : NonceFn ,
609
625
_noncedata : * const c_void )
610
626
-> c_int {
627
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
611
628
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
612
629
assert ! ( ( * cx) . 0 as u32 & SECP256K1_START_SIGN == SECP256K1_START_SIGN ) ;
613
630
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
@@ -620,6 +637,7 @@ mod fuzz_dummy {
620
637
/// Checks that pk != 0xffff...ffff and pk[0..32] == pk[32..64]
621
638
pub unsafe fn test_pk_validate ( cx : * const Context ,
622
639
pk : * const PublicKey ) -> c_int {
640
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
623
641
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
624
642
if ( * pk) . 0 [ 0 ..32 ] != ( * pk) . 0 [ 32 ..64 ] || secp256k1_ec_seckey_verify ( cx, ( * pk) . 0 [ 0 ..32 ] . as_ptr ( ) ) == 0 {
625
643
0
@@ -631,6 +649,7 @@ mod fuzz_dummy {
631
649
/// Checks that sk != 0xffff...ffff
632
650
pub unsafe fn secp256k1_ec_seckey_verify ( cx : * const Context ,
633
651
sk : * const c_uchar ) -> c_int {
652
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
634
653
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
635
654
let mut res = 0 ;
636
655
for i in 0 ..32 {
@@ -642,6 +661,7 @@ mod fuzz_dummy {
642
661
/// Sets pk to sk||sk
643
662
pub unsafe fn secp256k1_ec_pubkey_create ( cx : * const Context , pk : * mut PublicKey ,
644
663
sk : * const c_uchar ) -> c_int {
664
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
645
665
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
646
666
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
647
667
ptr:: copy ( sk, ( * pk) . 0 [ 0 ..32 ] . as_mut_ptr ( ) , 32 ) ;
@@ -657,6 +677,7 @@ mod fuzz_dummy {
657
677
sk : * mut c_uchar ,
658
678
tweak : * const c_uchar )
659
679
-> c_int {
680
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
660
681
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
661
682
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
662
683
ptr:: copy ( tweak. offset ( 16 ) , sk. offset ( 16 ) , 16 ) ;
@@ -669,6 +690,7 @@ mod fuzz_dummy {
669
690
pk : * mut PublicKey ,
670
691
tweak : * const c_uchar )
671
692
-> c_int {
693
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
672
694
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
673
695
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
674
696
ptr:: copy ( tweak. offset ( 16 ) , ( * pk) . 0 [ 16 ..32 ] . as_mut_ptr ( ) , 16 ) ;
@@ -683,6 +705,7 @@ mod fuzz_dummy {
683
705
sk : * mut c_uchar ,
684
706
tweak : * const c_uchar )
685
707
-> c_int {
708
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
686
709
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
687
710
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
688
711
ptr:: copy ( tweak. offset ( 16 ) , sk. offset ( 16 ) , 16 ) ;
@@ -695,6 +718,7 @@ mod fuzz_dummy {
695
718
pk : * mut PublicKey ,
696
719
tweak : * const c_uchar )
697
720
-> c_int {
721
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
698
722
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
699
723
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
700
724
ptr:: copy ( tweak. offset ( 16 ) , ( * pk) . 0 [ 16 ..32 ] . as_mut_ptr ( ) , 16 ) ;
@@ -709,6 +733,7 @@ mod fuzz_dummy {
709
733
ins : * const * const PublicKey ,
710
734
n : c_int )
711
735
-> c_int {
736
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
712
737
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
713
738
assert ! ( n <= 32 && n >= 0 ) ; //TODO: Remove this restriction?
714
739
for i in 0 ..n {
@@ -730,6 +755,7 @@ mod fuzz_dummy {
730
755
_hashfp : EcdhHashFn ,
731
756
_data : * mut c_void ,
732
757
) -> c_int {
758
+ assert ! ( UNSAFE_CRYPTO_FUZZING , "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ) ;
733
759
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
734
760
if secp256k1_ec_seckey_verify ( cx, scalar) != 1 { return 0 ; }
735
761
0 commit comments