@@ -84,6 +84,7 @@ pub type EcdhHashFn = unsafe extern "C" fn(
84
84
#[ cfg( feature = "fuzztarget" ) ]
85
85
impl Context {
86
86
pub fn flags ( & self ) -> u32 {
87
+ unsafe { assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ; }
87
88
self . 0 as u32
88
89
}
89
90
}
@@ -405,7 +406,7 @@ unsafe fn strlen(mut str_ptr: *const c_char) -> usize {
405
406
/// A trait for producing pointers that will always be valid in C. (assuming NULL pointer is a valid no-op)
406
407
/// Rust doesn't promise what pointers does it give to ZST (https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
407
408
/// In case the type is empty this trait will give a NULL pointer, which should be handled in C.
408
- ///
409
+ ///
409
410
pub trait CPtr {
410
411
type Target ;
411
412
fn as_c_ptr ( & self ) -> * const Self :: Target ;
@@ -447,6 +448,9 @@ mod fuzz_dummy {
447
448
#[ allow( non_upper_case_globals) ]
448
449
pub static secp256k1_context_no_precomp: & Context = & Context ( 0 ) ;
449
450
451
+ pub static mut UNSAFE_CRYPTO_FUZZING : bool = false ;
452
+ pub const UNSAFE_CRYPTO_WARNING : & str = "Tried fuzzing without setting the UNSAFE_CRYPTO_FUZZING variable" ;
453
+
450
454
extern "C" {
451
455
#[ cfg_attr( not( feature = "external-symbols" ) , link_name = "rustsecp256k1_v0_1_1_ecdh_hash_function_default" ) ]
452
456
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn ;
@@ -457,36 +461,42 @@ mod fuzz_dummy {
457
461
// Contexts
458
462
/// Creates a dummy context, tracking flags to ensure proper calling semantics
459
463
pub unsafe fn secp256k1_context_preallocated_create ( _ptr : * mut c_void , flags : c_uint ) -> * mut Context {
464
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
460
465
let b = Box :: new ( Context ( flags as i32 ) ) ;
461
466
Box :: into_raw ( b)
462
467
}
463
468
464
469
/// Return dummy size of context struct.
465
470
pub unsafe fn secp256k1_context_preallocated_size ( _flags : c_uint ) -> size_t {
471
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
466
472
mem:: size_of :: < Context > ( )
467
473
}
468
474
469
475
/// Return dummy size of context struct.
470
476
pub unsafe fn secp256k1_context_preallocated_clone_size ( _cx : * mut Context ) -> size_t {
477
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
471
478
mem:: size_of :: < Context > ( )
472
479
}
473
480
474
481
/// Copies a dummy context
475
482
pub unsafe fn secp256k1_context_preallocated_clone ( cx : * const Context , prealloc : * mut c_void ) -> * mut Context {
483
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
476
484
let ret = prealloc as * mut Context ;
477
485
* ret = ( * cx) . clone ( ) ;
478
486
ret
479
487
}
480
488
481
489
/// "Destroys" a dummy context
482
490
pub unsafe fn secp256k1_context_preallocated_destroy ( cx : * mut Context ) {
491
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
483
492
( * cx) . 0 = 0 ;
484
493
}
485
494
486
495
/// Asserts that cx is properly initialized
487
496
pub unsafe fn secp256k1_context_randomize ( cx : * mut Context ,
488
497
_seed32 : * const c_uchar )
489
498
-> c_int {
499
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
490
500
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
491
501
1
492
502
}
@@ -496,6 +506,7 @@ mod fuzz_dummy {
496
506
pub unsafe fn secp256k1_ec_pubkey_parse ( cx : * const Context , pk : * mut PublicKey ,
497
507
input : * const c_uchar , in_len : size_t )
498
508
-> c_int {
509
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
499
510
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
500
511
match in_len {
501
512
33 => {
@@ -524,6 +535,7 @@ mod fuzz_dummy {
524
535
out_len : * mut size_t , pk : * const PublicKey ,
525
536
compressed : c_uint )
526
537
-> c_int {
538
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
527
539
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
528
540
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
529
541
if compressed == SECP256K1_SER_COMPRESSED {
@@ -555,6 +567,7 @@ mod fuzz_dummy {
555
567
pub unsafe fn secp256k1_ecdsa_signature_parse_compact ( cx : * const Context , sig : * mut Signature ,
556
568
input64 : * const c_uchar )
557
569
-> c_int {
570
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
558
571
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
559
572
if secp256k1_ec_seckey_verify ( cx, input64. offset ( 32 ) ) != 1 { return 0 ; } // sig should be msg32||sk
560
573
ptr:: copy ( input64, ( * sig) . 0 [ ..] . as_mut_ptr ( ) , 64 ) ;
@@ -571,6 +584,7 @@ mod fuzz_dummy {
571
584
pub unsafe fn secp256k1_ecdsa_signature_serialize_der ( cx : * const Context , output : * mut c_uchar ,
572
585
out_len : * mut size_t , sig : * const Signature )
573
586
-> c_int {
587
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
574
588
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
575
589
576
590
let mut len_r = 33 ;
@@ -609,6 +623,7 @@ mod fuzz_dummy {
609
623
pub unsafe fn secp256k1_ecdsa_signature_serialize_compact ( cx : * const Context , output64 : * mut c_uchar ,
610
624
sig : * const Signature )
611
625
-> c_int {
626
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
612
627
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
613
628
ptr:: copy ( ( * sig) . 0 [ ..] . as_ptr ( ) , output64, 64 ) ;
614
629
1
@@ -627,6 +642,7 @@ mod fuzz_dummy {
627
642
msg32 : * const c_uchar ,
628
643
pk : * const PublicKey )
629
644
-> c_int {
645
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
630
646
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
631
647
assert ! ( ( * cx) . 0 as u32 & SECP256K1_START_VERIFY == SECP256K1_START_VERIFY ) ;
632
648
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
@@ -650,6 +666,7 @@ mod fuzz_dummy {
650
666
_noncefn : NonceFn ,
651
667
_noncedata : * const c_void )
652
668
-> c_int {
669
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
653
670
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
654
671
assert ! ( ( * cx) . 0 as u32 & SECP256K1_START_SIGN == SECP256K1_START_SIGN ) ;
655
672
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
@@ -662,6 +679,7 @@ mod fuzz_dummy {
662
679
/// Checks that pk != 0xffff...ffff and pk[0..32] == pk[32..64]
663
680
pub unsafe fn test_pk_validate ( cx : * const Context ,
664
681
pk : * const PublicKey ) -> c_int {
682
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
665
683
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
666
684
if ( * pk) . 0 [ 0 ..32 ] != ( * pk) . 0 [ 32 ..64 ] || secp256k1_ec_seckey_verify ( cx, ( * pk) . 0 [ 0 ..32 ] . as_ptr ( ) ) == 0 {
667
685
0
@@ -673,6 +691,7 @@ mod fuzz_dummy {
673
691
/// Checks that sk != 0xffff...ffff
674
692
pub unsafe fn secp256k1_ec_seckey_verify ( cx : * const Context ,
675
693
sk : * const c_uchar ) -> c_int {
694
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
676
695
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
677
696
let mut res = 0 ;
678
697
for i in 0 ..32 {
@@ -684,6 +703,7 @@ mod fuzz_dummy {
684
703
/// Sets pk to sk||sk
685
704
pub unsafe fn secp256k1_ec_pubkey_create ( cx : * const Context , pk : * mut PublicKey ,
686
705
sk : * const c_uchar ) -> c_int {
706
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
687
707
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
688
708
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
689
709
ptr:: copy ( sk, ( * pk) . 0 [ 0 ..32 ] . as_mut_ptr ( ) , 32 ) ;
@@ -699,6 +719,7 @@ mod fuzz_dummy {
699
719
sk : * mut c_uchar ,
700
720
tweak : * const c_uchar )
701
721
-> c_int {
722
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
702
723
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
703
724
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
704
725
ptr:: copy ( tweak. offset ( 16 ) , sk. offset ( 16 ) , 16 ) ;
@@ -711,6 +732,7 @@ mod fuzz_dummy {
711
732
pk : * mut PublicKey ,
712
733
tweak : * const c_uchar )
713
734
-> c_int {
735
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
714
736
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
715
737
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
716
738
ptr:: copy ( tweak. offset ( 16 ) , ( * pk) . 0 [ 16 ..32 ] . as_mut_ptr ( ) , 16 ) ;
@@ -725,6 +747,7 @@ mod fuzz_dummy {
725
747
sk : * mut c_uchar ,
726
748
tweak : * const c_uchar )
727
749
-> c_int {
750
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
728
751
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
729
752
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
730
753
ptr:: copy ( tweak. offset ( 16 ) , sk. offset ( 16 ) , 16 ) ;
@@ -737,6 +760,7 @@ mod fuzz_dummy {
737
760
pk : * mut PublicKey ,
738
761
tweak : * const c_uchar )
739
762
-> c_int {
763
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
740
764
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
741
765
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
742
766
ptr:: copy ( tweak. offset ( 16 ) , ( * pk) . 0 [ 16 ..32 ] . as_mut_ptr ( ) , 16 ) ;
@@ -751,6 +775,7 @@ mod fuzz_dummy {
751
775
ins : * const * const PublicKey ,
752
776
n : c_int )
753
777
-> c_int {
778
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
754
779
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
755
780
assert ! ( n <= 32 && n >= 0 ) ; //TODO: Remove this restriction?
756
781
for i in 0 ..n {
@@ -772,6 +797,7 @@ mod fuzz_dummy {
772
797
_hashfp : EcdhHashFn ,
773
798
_data : * mut c_void ,
774
799
) -> c_int {
800
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
775
801
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
776
802
if secp256k1_ec_seckey_verify ( cx, scalar) != 1 { return 0 ; }
777
803
0 commit comments