-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
146 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,84 @@ | ||
#![no_main] | ||
// rustup default nightly | ||
// head -c 3200 </dev/urandom > seed1 | ||
// cargo fuzz run fuzz_all -j 4 | ||
|
||
use fips203::ml_kem_512; | ||
use fips203::traits::{Decaps, Encaps, SerDes}; | ||
use fips203::traits::{Decaps, Encaps, KeyGen, SerDes}; | ||
use libfuzzer_sys::fuzz_target; | ||
use rand_core::{CryptoRng, RngCore}; | ||
|
||
fuzz_target!(|data: [u8; 1632+800+768]| { // dk_len + ek+len + ct_len = 3200 | ||
const RND_SIZE: usize = 32; | ||
|
||
|
||
struct TestRng { | ||
data: Vec<Vec<u8>>, | ||
} | ||
|
||
impl RngCore for TestRng { | ||
fn next_u32(&mut self) -> u32 { unimplemented!() } | ||
|
||
fn next_u64(&mut self) -> u64 { unimplemented!() } | ||
|
||
fn fill_bytes(&mut self, out: &mut [u8]) { | ||
let x = self.data.pop().expect("TestRng problem"); | ||
out.copy_from_slice(&x) | ||
} | ||
|
||
fn try_fill_bytes(&mut self, out: &mut [u8]) -> Result<(), rand_core::Error> { | ||
self.fill_bytes(out); | ||
Ok(()) // panic on probs is OK | ||
} | ||
} | ||
|
||
impl CryptoRng for TestRng {} | ||
|
||
impl TestRng { | ||
fn new() -> Self { TestRng { data: Vec::new() } } | ||
|
||
fn push(&mut self, new_data: &[u8]) { | ||
let x = new_data.to_vec(); | ||
self.data.push(x); | ||
} | ||
} | ||
|
||
fuzz_target!(|data: [u8; 3328]| { | ||
|
||
let mut rng = TestRng::new(); | ||
let mut start = 0; | ||
rng.push(&data[start..start+RND_SIZE]); | ||
start += RND_SIZE; | ||
rng.push(&data[start..start+RND_SIZE]); | ||
start += RND_SIZE; | ||
let keypair = ml_kem_512::KG::try_keygen_with_rng_vt(&mut rng); // consumes 2 rng values | ||
let (ek1, dk1) = keypair.unwrap(); // only rng can fail, which it won't | ||
|
||
let ek2_bytes = &data[start..start+ml_kem_512::EK_LEN]; | ||
start += ml_kem_512::EK_LEN; | ||
let ek2 = ml_kem_512::EncapsKey::try_from_bytes(ek2_bytes.try_into().unwrap()); | ||
|
||
rng.push(&data[start..start+RND_SIZE]); | ||
start += RND_SIZE; | ||
rng.push(&data[start..start+RND_SIZE]); | ||
start += RND_SIZE; | ||
|
||
if ek2.is_ok() { | ||
let _res = ek2.unwrap().try_encaps_with_rng_vt(&mut rng); | ||
} | ||
let _res = ek1.try_encaps_with_rng_vt(&mut rng); | ||
|
||
|
||
let dk2_bytes = &data[start..start+ml_kem_512::DK_LEN]; | ||
start += ml_kem_512::DK_LEN; | ||
let dk2 = ml_kem_512::DecapsKey::try_from_bytes(dk2_bytes.try_into().unwrap()); | ||
|
||
let ct_bytes = &data[start..start+ml_kem_512::CT_LEN]; | ||
start += ml_kem_512::CT_LEN; | ||
let ct = ml_kem_512::CipherText::try_from_bytes(ct_bytes.try_into().unwrap()).unwrap(); // always good | ||
|
||
if dk2.is_ok() { | ||
let _res = dk2.unwrap().try_decaps_vt(&ct); | ||
} | ||
let _res = dk1.try_decaps_vt(&ct); | ||
|
||
assert_eq!(start, data.len()); // this doesn't appear to trigger (even when wrong) | ||
|
||
let ek = ml_kem_512::EncapsKey::try_from_bytes(data[0..800].try_into().unwrap()).unwrap(); | ||
let dk = ml_kem_512::DecapsKey::try_from_bytes(data[800..800+1632].try_into().unwrap()).unwrap(); | ||
let ct = ml_kem_512::CipherText::try_from_bytes(data[800+1632..800+1632+768].try_into().unwrap()).unwrap(); | ||
|
||
let _result = ek.try_encaps_vt(); | ||
let _result = dk.try_decaps_vt(&ct); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.