@@ -693,7 +693,7 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
693
693
}
694
694
695
695
696
- #[ cfg( test) ]
696
+ #[ cfg( all ( test, not ( feature = "fuzztarget" ) ) ) ]
697
697
mod tests {
698
698
use rand:: { RngCore , thread_rng} ;
699
699
use std:: str:: FromStr ;
@@ -1084,3 +1084,66 @@ mod benches {
1084
1084
} ) ;
1085
1085
}
1086
1086
}
1087
+
1088
+ #[ cfg( all( test, feature = "fuzztarget" ) ) ]
1089
+ mod test_fuzz {
1090
+ use super :: * ;
1091
+ use std:: str:: FromStr ;
1092
+ use std:: panic:: { self , UnwindSafe } ;
1093
+
1094
+
1095
+ pub fn crashed_for_fuzz < F : FnOnce ( ) -> R + UnwindSafe , R > ( f : F ) -> bool {
1096
+ if let Err ( e) = panic:: catch_unwind ( f) {
1097
+ if let Some ( st) = e. downcast_ref :: < & str > ( ) {
1098
+ return st. contains ( ffi:: UNSAFE_CRYPTO_WARNING ) ;
1099
+ }
1100
+ }
1101
+ false
1102
+ }
1103
+
1104
+ #[ test]
1105
+ fn fuzz_not_set_var ( ) {
1106
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = false } ;
1107
+ assert ! ( crashed_for_fuzz( || SecretKey :: from_slice( & [ 2 ; 32 ] ) ) ) ;
1108
+ assert ! ( crashed_for_fuzz( || PublicKey :: from_slice( & [ 2 ; 33 ] ) ) ) ;
1109
+ assert ! ( crashed_for_fuzz( || Signature :: from_compact( & [ 3 ; 64 ] ) ) ) ;
1110
+ assert ! ( crashed_for_fuzz( || Secp256k1 :: new( ) ) ) ;
1111
+ assert ! ( crashed_for_fuzz( || {
1112
+ let mut sec = SecretKey :: from_str( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap( ) ;
1113
+ let _ = sec. add_assign( & [ 2u8 ; 32 ] ) ;
1114
+ } ) ) ;
1115
+ assert ! ( crashed_for_fuzz( || {
1116
+ let mut sec = SecretKey :: from_str( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap( ) ;
1117
+ let _ = sec. mul_assign( & [ 2u8 ; 32 ] ) ;
1118
+ } ) ) ;
1119
+ assert ! ( crashed_for_fuzz( || {
1120
+ let sig: Signature = unsafe { std:: mem:: transmute:: <_, ffi:: Signature >( [ 3u8 ; 64 ] ) . into( ) } ;
1121
+ let _ = sig. serialize_compact( ) ;
1122
+ } ) ) ;
1123
+
1124
+ assert ! ( crashed_for_fuzz( || {
1125
+ let pubkey1: PublicKey = unsafe { std:: mem:: transmute:: <_, ffi:: PublicKey >( [ 3u8 ; 64 ] ) . into( ) } ;
1126
+ let pubkey2: PublicKey = unsafe { std:: mem:: transmute:: <_, ffi:: PublicKey >( [ 5u8 ; 64 ] ) . into( ) } ;
1127
+ let _ = pubkey1. combine( & pubkey2) ;
1128
+ } ) ) ;
1129
+ }
1130
+
1131
+ #[ test]
1132
+ fn fuzz_set_var_not_crash ( ) {
1133
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = true ; }
1134
+ let _ = SecretKey :: from_slice ( & [ 2 ; 32 ] ) ;
1135
+ let _ = PublicKey :: from_slice ( & [ 2 ; 33 ] ) ;
1136
+ let _ = Signature :: from_compact ( & [ 3 ; 64 ] ) ;
1137
+ let _ = Secp256k1 :: new ( ) ;
1138
+ let mut sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1139
+ let _ = sec. add_assign ( & [ 2u8 ; 32 ] ) ;
1140
+ let mut sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1141
+ let _ = sec. mul_assign ( & [ 2u8 ; 32 ] ) ;
1142
+ let sig: Signature = unsafe { std:: mem:: transmute :: < _ , ffi:: Signature > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1143
+ let _ = sig. serialize_compact ( ) ;
1144
+ let pubkey1: PublicKey = unsafe { std:: mem:: transmute :: < _ , ffi:: PublicKey > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1145
+ let pubkey2: PublicKey = unsafe { std:: mem:: transmute :: < _ , ffi:: PublicKey > ( [ 5u8 ; 64 ] ) . into ( ) } ;
1146
+ let _ = pubkey1. combine ( & pubkey2) ;
1147
+ }
1148
+ }
1149
+
0 commit comments