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