Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic ml kem fuzzing #660

Merged
merged 8 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/mlkem.yml
franziskuskiefer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,35 @@ 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

- name: Update dependencies
run: cargo update

- run: 🏃🏻‍♀️ Decaps
franziskuskiefer marked this conversation as resolved.
Show resolved Hide resolved
run: CARGO_PROFILE_RELEASE_LTO=false cargo +nightly fuzz run decaps -- -runs=100000

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

- run: 🏃🏻‍♀️ 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
Loading