@@ -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,69 @@ 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
+
1170
+
1171
+ pub fn crashed_for_fuzz < F : FnOnce ( ) -> R + UnwindSafe , R > ( f : F ) -> bool {
1172
+ if let Err ( e) = panic:: catch_unwind ( f) {
1173
+ if let Some ( st) = e. downcast_ref :: < & str > ( ) {
1174
+ return st. contains ( ffi:: UNSAFE_CRYPTO_WARNING ) ;
1175
+ }
1176
+ }
1177
+ false
1178
+ }
1179
+
1180
+ fn fuzz_not_set_var ( ) {
1181
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = false } ;
1182
+ assert ! ( crashed_for_fuzz( || SecretKey :: from_slice( & [ 2 ; 32 ] ) ) ) ;
1183
+ assert ! ( crashed_for_fuzz( || PublicKey :: from_slice( & [ 2 ; 33 ] ) ) ) ;
1184
+ assert ! ( crashed_for_fuzz( || Signature :: from_compact( & [ 3 ; 64 ] ) ) ) ;
1185
+ assert ! ( crashed_for_fuzz( || Secp256k1 :: new( ) ) ) ;
1186
+ assert ! ( crashed_for_fuzz( || {
1187
+ let mut sec = SecretKey :: from_str( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap( ) ;
1188
+ let _ = sec. add_assign( & [ 2u8 ; 32 ] ) ;
1189
+ } ) ) ;
1190
+ assert ! ( crashed_for_fuzz( || {
1191
+ let mut sec = SecretKey :: from_str( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap( ) ;
1192
+ let _ = sec. mul_assign( & [ 2u8 ; 32 ] ) ;
1193
+ } ) ) ;
1194
+ assert ! ( crashed_for_fuzz( || {
1195
+ let sig: Signature = unsafe { std:: mem:: transmute:: <_, ffi:: Signature >( [ 3u8 ; 64 ] ) . into( ) } ;
1196
+ let _ = sig. serialize_compact( ) ;
1197
+ } ) ) ;
1198
+
1199
+ assert ! ( crashed_for_fuzz( || {
1200
+ let pubkey1: PublicKey = unsafe { std:: mem:: transmute:: <_, ffi:: PublicKey >( [ 3u8 ; 64 ] ) . into( ) } ;
1201
+ let pubkey2: PublicKey = unsafe { std:: mem:: transmute:: <_, ffi:: PublicKey >( [ 5u8 ; 64 ] ) . into( ) } ;
1202
+ let _ = pubkey1. combine( & pubkey2) ;
1203
+ } ) ) ;
1204
+ }
1205
+
1206
+ fn fuzz_set_var_not_crash ( ) {
1207
+ unsafe { ffi:: UNSAFE_CRYPTO_FUZZING = true ; }
1208
+ let _ = SecretKey :: from_slice ( & [ 2 ; 32 ] ) ;
1209
+ let _ = PublicKey :: from_slice ( & [ 2 ; 33 ] ) ;
1210
+ let _ = Signature :: from_compact ( & [ 3 ; 64 ] ) ;
1211
+ let _ = Secp256k1 :: new ( ) ;
1212
+ let mut sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1213
+ let _ = sec. add_assign ( & [ 2u8 ; 32 ] ) ;
1214
+ let mut sec = SecretKey :: from_str ( "01010101010101010001020304050607ffff0000ffff00006363636363636363" ) . unwrap ( ) ;
1215
+ let _ = sec. mul_assign ( & [ 2u8 ; 32 ] ) ;
1216
+ let sig: Signature = unsafe { std:: mem:: transmute :: < _ , ffi:: Signature > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1217
+ let _ = sig. serialize_compact ( ) ;
1218
+ let pubkey1: PublicKey = unsafe { std:: mem:: transmute :: < _ , ffi:: PublicKey > ( [ 3u8 ; 64 ] ) . into ( ) } ;
1219
+ let pubkey2: PublicKey = unsafe { std:: mem:: transmute :: < _ , ffi:: PublicKey > ( [ 5u8 ; 64 ] ) . into ( ) } ;
1220
+ let _ = pubkey1. combine ( & pubkey2) ;
1221
+ }
1222
+
1223
+ #[ test]
1224
+ fn test_fuzz_setting_var ( ) {
1225
+ fuzz_not_set_var ( ) ;
1226
+ fuzz_set_var_not_crash ( ) ;
1227
+ }
1228
+ }
0 commit comments