@@ -707,7 +707,7 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
707
707
}
708
708
709
709
710
- #[ cfg( test) ]
710
+ #[ cfg( all ( test, not ( feature = "fuzztarget" ) ) ) ]
711
711
mod tests {
712
712
use rand:: { RngCore , thread_rng} ;
713
713
use std:: str:: FromStr ;
@@ -1160,3 +1160,82 @@ mod benches {
1160
1160
} ) ;
1161
1161
}
1162
1162
}
1163
+
1164
+ #[ cfg( all( test, feature = "fuzztarget" ) ) ]
1165
+ mod test_fuzz {
1166
+ use super :: * ;
1167
+ use std:: str:: FromStr ;
1168
+ use std:: panic:: { self , UnwindSafe } ;
1169
+ use std:: mem;
1170
+
1171
+
1172
+ pub fn crashed_for_fuzz < F : FnOnce ( ) -> R + UnwindSafe , R > ( f : F ) -> bool {
1173
+ if let Err ( e) = panic:: catch_unwind ( f) {
1174
+ if let Some ( st) = e. downcast_ref :: < & str > ( ) {
1175
+ return st. contains ( ffi:: UNSAFE_CRYPTO_WARNING ) ;
1176
+ }
1177
+ }
1178
+ false
1179
+ }
1180
+
1181
+ fn fuzz_not_set_var ( ) {
1182
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = false } ;
1183
+ let mut raw_fake_context: ffi:: Context = unsafe { mem:: transmute ( VerifyOnly :: FLAGS | SignOnly :: FLAGS ) } ;
1184
+ let fake_context: Secp256k1 < All > = Secp256k1 {
1185
+ ctx : & mut raw_fake_context,
1186
+ phantom : PhantomData ,
1187
+ buf : ptr:: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ]
1188
+ } ;
1189
+ let msg = Message :: from_slice ( & [ 1u8 ; 32 ] ) . unwrap ( ) ;
1190
+ let sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1191
+ let sig: Signature = unsafe { std:: mem:: transmute :: < _ , ffi:: Signature > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1192
+
1193
+ assert ! ( crashed_for_fuzz( || SecretKey :: from_slice( & [ 2 ; 32 ] ) ) ) ;
1194
+ assert ! ( crashed_for_fuzz( || PublicKey :: from_slice( & [ 2 ; 33 ] ) ) ) ;
1195
+ assert ! ( crashed_for_fuzz( || Signature :: from_compact( & [ 3 ; 64 ] ) ) ) ;
1196
+ assert ! ( crashed_for_fuzz( || Secp256k1 :: new( ) ) ) ;
1197
+ assert ! ( crashed_for_fuzz( || sec. clone( ) . add_assign( & [ 2u8 ; 32 ] ) ) ) ;
1198
+ assert ! ( crashed_for_fuzz( || sec. clone( ) . mul_assign( & [ 2u8 ; 32 ] ) ) ) ;
1199
+ assert ! ( crashed_for_fuzz( || sig. serialize_compact( ) ) ) ;
1200
+ assert ! ( crashed_for_fuzz( || fake_context. sign( & msg, & sec) ) ) ;
1201
+ #[ cfg( feature = "recovery" ) ]
1202
+ {
1203
+ assert ! ( crashed_for_fuzz( ||fake_context. sign_recoverable( & msg, & sec) ) ) ;
1204
+ }
1205
+ assert ! ( crashed_for_fuzz( || drop( fake_context) ) ) ;
1206
+ }
1207
+
1208
+ fn fuzz_set_var_not_crash ( ) {
1209
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = true ; }
1210
+ let context = Secp256k1 :: new ( ) ;
1211
+ let msg = Message :: from_slice ( & [ 1u8 ; 32 ] ) . unwrap ( ) ;
1212
+ let _ = SecretKey :: from_slice ( & [ 2 ; 32 ] ) ;
1213
+ let _ = PublicKey :: from_slice ( & [ 2 ; 33 ] ) ;
1214
+ let _ = Signature :: from_compact ( & [ 3 ; 64 ] ) ;
1215
+ let _ = Secp256k1 :: new ( ) ;
1216
+ let mut sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1217
+ let _ = sec. add_assign ( & [ 2u8 ; 32 ] ) ;
1218
+ let mut sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1219
+ let _ = sec. mul_assign ( & [ 2u8 ; 32 ] ) ;
1220
+ let sig: Signature = unsafe { std:: mem:: transmute :: < _ , ffi:: Signature > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1221
+ let _ = sig. serialize_compact ( ) ;
1222
+ let pubkey1: PublicKey = unsafe { mem:: transmute :: < _ , ffi:: PublicKey > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1223
+ let pubkey2: PublicKey = unsafe { mem:: transmute :: < _ , ffi:: PublicKey > ( [ 5u8 ; 64 ] ) . into ( ) } ;
1224
+ let _ = pubkey1. combine ( & pubkey2) ;
1225
+ let sig = context. sign ( & msg, & sec) ;
1226
+ assert_eq ! ( context. verify( & msg, & sig, & pubkey1) . unwrap_err( ) , Error :: IncorrectSignature ) ;
1227
+
1228
+ #[ cfg( feature = "recovery" ) ]
1229
+ {
1230
+ let _ = context. sign_recoverable ( & msg, & sec) ;
1231
+ }
1232
+
1233
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = false ; }
1234
+ }
1235
+
1236
+ #[ test]
1237
+ fn test_fuzz_setting_var ( ) {
1238
+ fuzz_not_set_var ( ) ;
1239
+ fuzz_set_var_not_crash ( ) ;
1240
+ }
1241
+ }
0 commit comments