@@ -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,43 @@ 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
- pub unsafe fn secp256k1_context_preallocated_destroy ( cx : * mut Context ) {
490
+ pub unsafe fn secp256k1_context_preallocated_destroy ( cx : * mut Context )
491
+ {
492
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
483
493
( * cx) . 0 = 0 ;
484
494
}
485
495
486
496
/// Asserts that cx is properly initialized
487
497
pub unsafe fn secp256k1_context_randomize ( cx : * mut Context ,
488
498
_seed32 : * const c_uchar )
489
499
-> c_int {
500
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
490
501
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
491
502
1
492
503
}
@@ -496,6 +507,7 @@ mod fuzz_dummy {
496
507
pub unsafe fn secp256k1_ec_pubkey_parse ( cx : * const Context , pk : * mut PublicKey ,
497
508
input : * const c_uchar , in_len : size_t )
498
509
-> c_int {
510
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
499
511
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
500
512
match in_len {
501
513
33 => {
@@ -524,6 +536,7 @@ mod fuzz_dummy {
524
536
out_len : * mut size_t , pk : * const PublicKey ,
525
537
compressed : c_uint )
526
538
-> c_int {
539
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
527
540
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
528
541
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
529
542
if compressed == SECP256K1_SER_COMPRESSED {
@@ -555,6 +568,7 @@ mod fuzz_dummy {
555
568
pub unsafe fn secp256k1_ecdsa_signature_parse_compact ( cx : * const Context , sig : * mut Signature ,
556
569
input64 : * const c_uchar )
557
570
-> c_int {
571
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
558
572
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
559
573
if secp256k1_ec_seckey_verify ( cx, input64. offset ( 32 ) ) != 1 { return 0 ; } // sig should be msg32||sk
560
574
ptr:: copy ( input64, ( * sig) . 0 [ ..] . as_mut_ptr ( ) , 64 ) ;
@@ -571,6 +585,7 @@ mod fuzz_dummy {
571
585
pub unsafe fn secp256k1_ecdsa_signature_serialize_der ( cx : * const Context , output : * mut c_uchar ,
572
586
out_len : * mut size_t , sig : * const Signature )
573
587
-> c_int {
588
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
574
589
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
575
590
576
591
let mut len_r = 33 ;
@@ -609,6 +624,7 @@ mod fuzz_dummy {
609
624
pub unsafe fn secp256k1_ecdsa_signature_serialize_compact ( cx : * const Context , output64 : * mut c_uchar ,
610
625
sig : * const Signature )
611
626
-> c_int {
627
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
612
628
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
613
629
ptr:: copy ( ( * sig) . 0 [ ..] . as_ptr ( ) , output64, 64 ) ;
614
630
1
@@ -627,6 +643,7 @@ mod fuzz_dummy {
627
643
msg32 : * const c_uchar ,
628
644
pk : * const PublicKey )
629
645
-> c_int {
646
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
630
647
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
631
648
assert ! ( ( * cx) . 0 as u32 & SECP256K1_START_VERIFY == SECP256K1_START_VERIFY ) ;
632
649
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
@@ -650,6 +667,7 @@ mod fuzz_dummy {
650
667
_noncefn : NonceFn ,
651
668
_noncedata : * const c_void )
652
669
-> c_int {
670
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
653
671
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
654
672
assert ! ( ( * cx) . 0 as u32 & SECP256K1_START_SIGN == SECP256K1_START_SIGN ) ;
655
673
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
@@ -662,6 +680,7 @@ mod fuzz_dummy {
662
680
/// Checks that pk != 0xffff...ffff and pk[0..32] == pk[32..64]
663
681
pub unsafe fn test_pk_validate ( cx : * const Context ,
664
682
pk : * const PublicKey ) -> c_int {
683
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
665
684
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
666
685
if ( * pk) . 0 [ 0 ..32 ] != ( * pk) . 0 [ 32 ..64 ] || secp256k1_ec_seckey_verify ( cx, ( * pk) . 0 [ 0 ..32 ] . as_ptr ( ) ) == 0 {
667
686
0
@@ -673,6 +692,7 @@ mod fuzz_dummy {
673
692
/// Checks that sk != 0xffff...ffff
674
693
pub unsafe fn secp256k1_ec_seckey_verify ( cx : * const Context ,
675
694
sk : * const c_uchar ) -> c_int {
695
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
676
696
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
677
697
let mut res = 0 ;
678
698
for i in 0 ..32 {
@@ -684,6 +704,7 @@ mod fuzz_dummy {
684
704
/// Sets pk to sk||sk
685
705
pub unsafe fn secp256k1_ec_pubkey_create ( cx : * const Context , pk : * mut PublicKey ,
686
706
sk : * const c_uchar ) -> c_int {
707
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
687
708
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
688
709
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
689
710
ptr:: copy ( sk, ( * pk) . 0 [ 0 ..32 ] . as_mut_ptr ( ) , 32 ) ;
@@ -699,6 +720,7 @@ mod fuzz_dummy {
699
720
sk : * mut c_uchar ,
700
721
tweak : * const c_uchar )
701
722
-> c_int {
723
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
702
724
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
703
725
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
704
726
ptr:: copy ( tweak. offset ( 16 ) , sk. offset ( 16 ) , 16 ) ;
@@ -711,6 +733,7 @@ mod fuzz_dummy {
711
733
pk : * mut PublicKey ,
712
734
tweak : * const c_uchar )
713
735
-> c_int {
736
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
714
737
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
715
738
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
716
739
ptr:: copy ( tweak. offset ( 16 ) , ( * pk) . 0 [ 16 ..32 ] . as_mut_ptr ( ) , 16 ) ;
@@ -725,6 +748,7 @@ mod fuzz_dummy {
725
748
sk : * mut c_uchar ,
726
749
tweak : * const c_uchar )
727
750
-> c_int {
751
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
728
752
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
729
753
if secp256k1_ec_seckey_verify ( cx, sk) != 1 { return 0 ; }
730
754
ptr:: copy ( tweak. offset ( 16 ) , sk. offset ( 16 ) , 16 ) ;
@@ -737,6 +761,7 @@ mod fuzz_dummy {
737
761
pk : * mut PublicKey ,
738
762
tweak : * const c_uchar )
739
763
-> c_int {
764
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
740
765
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
741
766
if test_pk_validate ( cx, pk) != 1 { return 0 ; }
742
767
ptr:: copy ( tweak. offset ( 16 ) , ( * pk) . 0 [ 16 ..32 ] . as_mut_ptr ( ) , 16 ) ;
@@ -751,6 +776,7 @@ mod fuzz_dummy {
751
776
ins : * const * const PublicKey ,
752
777
n : c_int )
753
778
-> c_int {
779
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
754
780
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
755
781
assert ! ( n <= 32 && n >= 0 ) ; //TODO: Remove this restriction?
756
782
for i in 0 ..n {
@@ -772,6 +798,7 @@ mod fuzz_dummy {
772
798
_hashfp : EcdhHashFn ,
773
799
_data : * mut c_void ,
774
800
) -> c_int {
801
+ assert ! ( UNSAFE_CRYPTO_FUZZING , UNSAFE_CRYPTO_WARNING ) ;
775
802
assert ! ( !cx. is_null( ) && ( * cx) . 0 as u32 & !( SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN ) == 0 ) ;
776
803
if secp256k1_ec_seckey_verify ( cx, scalar) != 1 { return 0 ; }
777
804
0 commit comments