Skip to content

Commit

Permalink
Merge pull request #660 from cryspen/franziskus/ml-kem-fuzzing
Browse files Browse the repository at this point in the history
Basic ml kem fuzzing
  • Loading branch information
jschneider-bensch authored Nov 6, 2024
2 parents 5ddbf9e + d5632a5 commit f7dccd0
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 33 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/mlkem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,37 @@ jobs:
run: |
cargo clean
cargo hack test --each-feature $EXCLUDE_FEATURES --verbose $RUST_TARGET_FLAG
fuzz:
strategy:
fail-fast: false
matrix:
os:
- macos-latest # macos-14 m1
- ubuntu-latest

runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
working-directory: libcrux-ml-kem

steps:
- uses: actions/checkout@v4

- name: 🛠️ Setup Rust Nightly
run: |
rustup toolchain install nightly
cargo install cargo-fuzz
- name: 🛠️ Update dependencies
run: cargo update

- name: 🏃🏻‍♀️ Decaps
run: CARGO_PROFILE_RELEASE_LTO=false cargo +nightly fuzz run decaps -- -runs=100000

- name: 🏃🏻‍♀️ Encaps
run: CARGO_PROFILE_RELEASE_LTO=false cargo +nightly fuzz run encaps -- -runs=100000

- name: 🏃🏻‍♀️ KeyGen
run: CARGO_PROFILE_RELEASE_LTO=false cargo +nightly fuzz run keygen -- -runs=1000000
74 changes: 41 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"benchmarks",
"fuzz",
"libcrux-ml-kem",
"libcrux-ml-kem/fuzz",
"libcrux-sha3",
"libcrux-ml-dsa",
"libcrux-intrinsics",
Expand Down
4 changes: 4 additions & 0 deletions libcrux-ml-kem/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
35 changes: 35 additions & 0 deletions libcrux-ml-kem/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "libcrux-ml-kem-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.libcrux-ml-kem]
path = ".."

[[bin]]
name = "keygen"
path = "fuzz_targets/keygen.rs"
test = false
doc = false
bench = false

[[bin]]
name = "encaps"
path = "fuzz_targets/encaps.rs"
test = false
doc = false
bench = false

[[bin]]
name = "decaps"
path = "fuzz_targets/decaps.rs"
test = false
doc = false
bench = false
25 changes: 25 additions & 0 deletions libcrux-ml-kem/fuzz/fuzz_targets/decaps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![no_main]

use libcrux_ml_kem::{mlkem768, ENCAPS_SEED_SIZE, KEY_GENERATION_SEED_SIZE};
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
if data.len() < KEY_GENERATION_SEED_SIZE + ENCAPS_SEED_SIZE {
// Not enough entropy
return;
}

let mut randomness = [0u8; KEY_GENERATION_SEED_SIZE];
randomness.copy_from_slice(&data[..KEY_GENERATION_SEED_SIZE]);

let key_pair = mlkem768::generate_key_pair(randomness);

let mut randomness = [0u8; ENCAPS_SEED_SIZE];
randomness.copy_from_slice(
&data[KEY_GENERATION_SEED_SIZE..KEY_GENERATION_SEED_SIZE + ENCAPS_SEED_SIZE],
);

let (ct, _ss) = mlkem768::encapsulate(key_pair.public_key(), randomness);

let _ = core::hint::black_box(mlkem768::decapsulate(key_pair.private_key(), &ct));
});
23 changes: 23 additions & 0 deletions libcrux-ml-kem/fuzz/fuzz_targets/encaps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_main]

use libcrux_ml_kem::{mlkem768, ENCAPS_SEED_SIZE, KEY_GENERATION_SEED_SIZE};
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
if data.len() < KEY_GENERATION_SEED_SIZE + ENCAPS_SEED_SIZE {
// Not enough entropy
return;
}

let mut randomness = [0u8; KEY_GENERATION_SEED_SIZE];
randomness.copy_from_slice(&data[..KEY_GENERATION_SEED_SIZE]);

let key_pair = mlkem768::generate_key_pair(randomness);

let mut randomness = [0u8; ENCAPS_SEED_SIZE];
randomness.copy_from_slice(
&data[KEY_GENERATION_SEED_SIZE..KEY_GENERATION_SEED_SIZE + ENCAPS_SEED_SIZE],
);

let _ = core::hint::black_box(mlkem768::encapsulate(key_pair.public_key(), randomness));
});
Loading

0 comments on commit f7dccd0

Please sign in to comment.