Skip to content

Commit e13351c

Browse files
committed
Add a static mut bool to prevent accidentally using fuzz functions
1 parent 4e69dcc commit e13351c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ffi.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub type EcdhHashFn = unsafe extern "C" fn(
6363
#[cfg(feature = "fuzztarget")]
6464
impl Context {
6565
pub fn flags(&self) -> u32 {
66+
unsafe {assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING); }
6667
self.0 as u32
6768
}
6869
}
@@ -399,6 +400,9 @@ mod fuzz_dummy {
399400
use self::std::{ptr, mem};
400401
use self::std::boxed::Box;
401402

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+
402406
extern "C" {
403407
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn;
404408
pub static secp256k1_nonce_function_rfc6979: NonceFn;
@@ -408,36 +412,43 @@ mod fuzz_dummy {
408412
// Contexts
409413
/// Creates a dummy context, tracking flags to ensure proper calling semantics
410414
pub unsafe fn secp256k1_context_preallocated_create(_ptr: *mut c_void, flags: c_uint) -> *mut Context {
415+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
411416
let b = Box::new(Context(flags as i32));
412417
Box::into_raw(b)
413418
}
414419

415420
/// Return dummy size of context struct.
416421
pub unsafe fn secp256k1_context_preallocated_size(_flags: c_uint) -> usize {
422+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
417423
mem::size_of::<Context>()
418424
}
419425

420426
/// Return dummy size of context struct.
421427
pub unsafe fn secp256k1_context_preallocated_clone_size(cx: *mut Context) -> usize {
428+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
422429
mem::size_of::<Context>()
423430
}
424431

425432
/// Copies a dummy context
426433
pub unsafe fn secp256k1_context_preallocated_clone(cx: *const Context, prealloc: *mut c_void) -> *mut Context {
434+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
427435
let ret = prealloc as *mut Context;
428436
*ret = (*cx).clone();
429437
ret
430438
}
431439

432440
/// "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);
434444
(*cx).0 = 0;
435445
}
436446

437447
/// Asserts that cx is properly initialized
438448
pub unsafe fn secp256k1_context_randomize(cx: *mut Context,
439449
_seed32: *const c_uchar)
440450
-> c_int {
451+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
441452
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
442453
1
443454
}
@@ -454,6 +465,7 @@ mod fuzz_dummy {
454465
pub unsafe fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey,
455466
input: *const c_uchar, in_len: usize)
456467
-> c_int {
468+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
457469
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
458470
match in_len {
459471
33 => {
@@ -482,6 +494,7 @@ mod fuzz_dummy {
482494
out_len: *mut usize, pk: *const PublicKey,
483495
compressed: c_uint)
484496
-> c_int {
497+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
485498
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
486499
if test_pk_validate(cx, pk) != 1 { return 0; }
487500
if compressed == SECP256K1_SER_COMPRESSED {
@@ -513,6 +526,7 @@ mod fuzz_dummy {
513526
pub unsafe fn secp256k1_ecdsa_signature_parse_compact(cx: *const Context, sig: *mut Signature,
514527
input64: *const c_uchar)
515528
-> c_int {
529+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
516530
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
517531
if secp256k1_ec_seckey_verify(cx, input64.offset(32)) != 1 { return 0; } // sig should be msg32||sk
518532
ptr::copy(input64, (*sig).0[..].as_mut_ptr(), 64);
@@ -529,6 +543,7 @@ mod fuzz_dummy {
529543
pub unsafe fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar,
530544
out_len: *mut usize, sig: *const Signature)
531545
-> c_int {
546+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
532547
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
533548

534549
let mut len_r = 33;
@@ -567,6 +582,7 @@ mod fuzz_dummy {
567582
pub unsafe fn secp256k1_ecdsa_signature_serialize_compact(cx: *const Context, output64: *mut c_uchar,
568583
sig: *const Signature)
569584
-> c_int {
585+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
570586
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
571587
ptr::copy((*sig).0[..].as_ptr(), output64, 64);
572588
1
@@ -585,6 +601,7 @@ mod fuzz_dummy {
585601
msg32: *const c_uchar,
586602
pk: *const PublicKey)
587603
-> c_int {
604+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
588605
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
589606
assert!((*cx).0 as u32 & SECP256K1_START_VERIFY == SECP256K1_START_VERIFY);
590607
if test_pk_validate(cx, pk) != 1 { return 0; }
@@ -608,6 +625,7 @@ mod fuzz_dummy {
608625
_noncefn: NonceFn,
609626
_noncedata: *const c_void)
610627
-> c_int {
628+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
611629
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
612630
assert!((*cx).0 as u32 & SECP256K1_START_SIGN == SECP256K1_START_SIGN);
613631
if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }
@@ -620,6 +638,7 @@ mod fuzz_dummy {
620638
/// Checks that pk != 0xffff...ffff and pk[0..32] == pk[32..64]
621639
pub unsafe fn test_pk_validate(cx: *const Context,
622640
pk: *const PublicKey) -> c_int {
641+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
623642
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
624643
if (*pk).0[0..32] != (*pk).0[32..64] || secp256k1_ec_seckey_verify(cx, (*pk).0[0..32].as_ptr()) == 0 {
625644
0
@@ -631,6 +650,7 @@ mod fuzz_dummy {
631650
/// Checks that sk != 0xffff...ffff
632651
pub unsafe fn secp256k1_ec_seckey_verify(cx: *const Context,
633652
sk: *const c_uchar) -> c_int {
653+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
634654
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
635655
let mut res = 0;
636656
for i in 0..32 {
@@ -642,6 +662,7 @@ mod fuzz_dummy {
642662
/// Sets pk to sk||sk
643663
pub unsafe fn secp256k1_ec_pubkey_create(cx: *const Context, pk: *mut PublicKey,
644664
sk: *const c_uchar) -> c_int {
665+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
645666
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
646667
if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }
647668
ptr::copy(sk, (*pk).0[0..32].as_mut_ptr(), 32);
@@ -657,6 +678,7 @@ mod fuzz_dummy {
657678
sk: *mut c_uchar,
658679
tweak: *const c_uchar)
659680
-> c_int {
681+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
660682
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
661683
if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }
662684
ptr::copy(tweak.offset(16), sk.offset(16), 16);
@@ -669,6 +691,7 @@ mod fuzz_dummy {
669691
pk: *mut PublicKey,
670692
tweak: *const c_uchar)
671693
-> c_int {
694+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
672695
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
673696
if test_pk_validate(cx, pk) != 1 { return 0; }
674697
ptr::copy(tweak.offset(16), (*pk).0[16..32].as_mut_ptr(), 16);
@@ -683,6 +706,7 @@ mod fuzz_dummy {
683706
sk: *mut c_uchar,
684707
tweak: *const c_uchar)
685708
-> c_int {
709+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
686710
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
687711
if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }
688712
ptr::copy(tweak.offset(16), sk.offset(16), 16);
@@ -695,6 +719,7 @@ mod fuzz_dummy {
695719
pk: *mut PublicKey,
696720
tweak: *const c_uchar)
697721
-> c_int {
722+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
698723
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
699724
if test_pk_validate(cx, pk) != 1 { return 0; }
700725
ptr::copy(tweak.offset(16), (*pk).0[16..32].as_mut_ptr(), 16);
@@ -709,6 +734,7 @@ mod fuzz_dummy {
709734
ins: *const *const PublicKey,
710735
n: c_int)
711736
-> c_int {
737+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
712738
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
713739
assert!(n <= 32 && n >= 0); //TODO: Remove this restriction?
714740
for i in 0..n {
@@ -730,6 +756,7 @@ mod fuzz_dummy {
730756
_hashfp: EcdhHashFn,
731757
_data: *mut c_void,
732758
) -> c_int {
759+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
733760
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
734761
if secp256k1_ec_seckey_verify(cx, scalar) != 1 { return 0; }
735762

src/recovery/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ mod fuzz_dummy {
102102
_noncefn: NonceFn,
103103
_noncedata: *const c_void)
104104
-> c_int {
105+
assert!(UNSAFE_CRYPTO_FUZZING, UNSAFE_CRYPTO_WARNING);
105106
assert!(!cx.is_null() && (*cx).flags() & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
106107
assert!((*cx).flags() & SECP256K1_START_SIGN == SECP256K1_START_SIGN);
107108
if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }

0 commit comments

Comments
 (0)