Skip to content

Commit 08e057f

Browse files
committed
Fuzz fixes WIP
1 parent adf07cb commit 08e057f

File tree

5 files changed

+165
-77
lines changed

5 files changed

+165
-77
lines changed

fuzz/Cargo.lock

Lines changed: 33 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/fuzz_targets/key_parsing_fuzz.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,35 @@ use libfuzzer_sys::fuzz_target;
66
const NUM_ALGORITHMS: u8 = 4; // SECP256K1_SCHNORR, FN_DSA_512, ML_DSA_44, SLH_DSA_128S
77

88
fuzz_target!(|data: &[u8]| {
9-
if data.is_empty() {
10-
return; // Need at least one byte for algorithm selection
9+
if data.len() < 2 {
10+
// Need at least 2 bytes: 1 for algorithm, 1+ for key data
11+
return;
1112
}
1213

13-
// Use first byte to select an algorithm
14+
// First byte selects algorithm
1415
let alg_byte = data[0];
1516
let algorithm = algorithm_from_index(alg_byte);
1617

17-
// Use remaining bytes as potential key data
18+
// Rest of the data is treated as a potential key
1819
let key_data = &data[1..];
1920

20-
// Attempt to parse as PublicKey
21-
let _ = PublicKey::try_from_slice(algorithm, key_data);
22-
23-
// Attempt to parse as SecretKey
24-
let secret_key_result = SecretKey::try_from_slice(algorithm, key_data);
25-
26-
assert!(
27-
secret_key_result.is_ok(),
28-
"Secret key parsing failed! Algorithm: {algorithm:?}",
29-
);
21+
// Try to interpret this as a secret key
22+
// The key_parsing should correctly validate this without crashing
23+
let sk_result = SecretKey::try_from_slice(algorithm, key_data);
24+
if key_data.len() == bitcoinpqc::secret_key_size(algorithm) {
25+
// If length matches, it should parse correctly
26+
// (assuming bytewise validation passes)
27+
let _ = sk_result.unwrap_or_else(|_| {
28+
panic!(
29+
"Secret key parsing failed! Algorithm: {}",
30+
algorithm.debug_name()
31+
)
32+
});
33+
} else {
34+
// Otherwise it should return an error
35+
assert!(
36+
sk_result.is_err(),
37+
"Parsing should fail for invalid key length!"
38+
);
39+
}
3040
});

fuzz/fuzz_targets/keypair_generation_fuzz.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ use bitcoinpqc::{algorithm_from_index, generate_keypair};
44
use libfuzzer_sys::fuzz_target;
55

66
fuzz_target!(|data: &[u8]| {
7-
if data.len() < 129 {
8-
// Need at least 129 bytes: 1 for algorithm selection and 128 for random data
7+
if data.len() < 130 {
8+
// Need at least 1 byte for algorithm + 129 bytes for key seed
99
return;
1010
}
1111

12-
// Use first byte to select an algorithm
12+
// First byte selects algorithm
1313
let alg_byte = data[0];
1414
let algorithm = algorithm_from_index(alg_byte);
1515

16-
// Use remaining bytes as random data
17-
let random_data = &data[1..];
18-
19-
// Try to generate a keypair with this data
20-
let keypair = generate_keypair(algorithm, random_data);
16+
// Rest is key generation data
17+
let key_data = &data[1..]; // Should be 129+ bytes
2118

19+
// Try to generate a keypair
20+
let keypair_result = generate_keypair(algorithm, key_data);
2221
assert!(
23-
keypair.is_ok(),
24-
"Keypair generation failed! Algorithm: {algorithm:?}",
22+
keypair_result.is_ok(),
23+
"Keypair generation failed! Algorithm: {}",
24+
algorithm.debug_name()
2525
);
26+
let _keypair = keypair_result.unwrap();
27+
// Success!
2628
});

fuzz/fuzz_targets/sign_verify_fuzz.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,59 @@ fuzz_target!(|data: &[u8]| {
1717
let key_data = &data[1..129];
1818

1919
// Try to generate a keypair
20-
let keypair = match generate_keypair(algorithm, key_data) {
21-
Ok(kp) => kp,
22-
Err(_) => return, // Skip if key generation fails
23-
};
20+
let keypair_result = generate_keypair(algorithm, key_data);
21+
if let Err(err) = &keypair_result {
22+
panic!(
23+
"Key generation failed for algorithm: {}, error: {:?}",
24+
algorithm.debug_name(),
25+
err
26+
);
27+
}
28+
let keypair = keypair_result.unwrap();
2429

2530
// Use remaining bytes as message to sign
2631
let message = &data[129..];
2732

2833
// Try to sign the message
29-
let signature = match sign(&keypair.secret_key, message) {
30-
Ok(sig) => sig,
31-
Err(_) => return, // Skip if signing fails
32-
};
34+
let signature_result = sign(&keypair.secret_key, message);
35+
if let Err(err) = &signature_result {
36+
panic!(
37+
"Signing failed for algorithm: {}, error: {:?}",
38+
algorithm.debug_name(),
39+
err
40+
);
41+
}
42+
let signature = signature_result.unwrap();
3343

3444
// Try to verify the signature with the correct public key
3545
let verify_result = verify(&keypair.public_key, message, &signature);
36-
37-
assert!(
38-
verify_result.is_ok(),
39-
"Verification failed for a signature generated with the corresponding private key! Algorithm: {algorithm:?}",
40-
);
46+
if let Err(err) = &verify_result {
47+
panic!("Verification failed for a signature generated with the corresponding private key! Algorithm: {}, error: {:?}",
48+
algorithm.debug_name(), err);
49+
}
4150

4251
// Also try some invalid cases (if we have a valid signature)
4352
if message.len() > 1 {
4453
// Try with modified message
4554
let mut modified_msg = message.to_vec();
4655
modified_msg[0] ^= 0xFF; // Flip bits in first byte
47-
let _verify_result_bad_msg = verify(&keypair.public_key, &modified_msg, &signature);
56+
let verify_result_bad_msg = verify(&keypair.public_key, &modified_msg, &signature);
57+
assert!(
58+
verify_result_bad_msg.is_err(),
59+
"Verification should fail with modified message! Algorithm: {}",
60+
algorithm.debug_name()
61+
);
4862
}
4963

5064
if signature.bytes.len() > 1 {
5165
// Try with modified signature
5266
let mut modified_sig = signature.clone();
5367
modified_sig.bytes[0] ^= 0xFF; // Flip bits in first byte
54-
let _verify_result_bad_sig = verify(&keypair.public_key, message, &modified_sig);
68+
let verify_result_bad_sig = verify(&keypair.public_key, message, &modified_sig);
69+
assert!(
70+
verify_result_bad_sig.is_err(),
71+
"Verification should fail with modified signature! Algorithm: {}",
72+
algorithm.debug_name()
73+
);
5574
}
5675
});

0 commit comments

Comments
 (0)