Skip to content

Commit

Permalink
Initial fuzzer setup for X25519 (#15)
Browse files Browse the repository at this point in the history
* Initial fuzzer setup for X25519 (see orion-rs/orion#197)

* NIT

* Update Cargo.toml
  • Loading branch information
brycx authored Oct 4, 2021
1 parent 38d7697 commit 0a07cc4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ blake2-rfc = "0.2.18"
chacha = "0.3.0"
orion = { git = "https://github.com/orion-rs/orion", branch = "master" }
rust-argon2 = "0.8.3"
x25519-dalek = "1.2.0"

[[bin]]
name = "high_level_api"
Expand Down Expand Up @@ -47,5 +48,9 @@ path = "src/kdf.rs"
name = "aead_stream"
path = "src/aead_stream.rs"

[[bin]]
name = "ecc"
path = "src/ecc.rs"

[profile.release]
opt-level = 3
opt-level = 3
50 changes: 50 additions & 0 deletions src/ecc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#[macro_use]
extern crate honggfuzz;
extern crate orion;
extern crate x25519_dalek;

use orion::hazardous::ecc::x25519;
use std::convert::{TryFrom, TryInto};
use utils::{make_seeded_rng, ChaChaRng, RngCore};

pub mod utils;

/// `orion::hazardous::ecc::x25519`
fn fuzz_x25519(seeded_rng: &mut ChaChaRng) {
// Key-agreement
let mut alice_k = [0u8; x25519::SECRET_KEY_SIZE];
let mut bob_k = [0u8; x25519::SECRET_KEY_SIZE];
seeded_rng.fill_bytes(&mut alice_k);
seeded_rng.fill_bytes(&mut bob_k);

let alice_secret = x25519::SecretKey::from_slice(&alice_k).unwrap();
let alice_public = x25519::PublicKey::try_from(&alice_secret).unwrap();
let bob_secret = x25519::SecretKey::from_slice(&bob_k).unwrap();
let bob_public = x25519::PublicKey::try_from(&bob_secret).unwrap();

let alice_shared = x25519::key_agreement(&alice_secret, &bob_public).unwrap();
let bob_shared = x25519::key_agreement(&bob_secret, &alice_public).unwrap();

assert_eq!(alice_shared, bob_shared);

// x25519_dalek (we use the bare-byte function since this is the one documented as adherent to RFC)
let dalek_alice_public: [u8; 32] = alice_public.as_ref().try_into().unwrap();
let dalek_bob_public: [u8; 32] = bob_public.as_ref().try_into().unwrap();
let dalek_alice_shared = x25519_dalek::x25519(alice_k, dalek_bob_public);
let dalek_bob_shared = x25519_dalek::x25519(bob_k, dalek_alice_public);

assert_eq!(alice_shared, dalek_alice_shared.as_ref());
assert_eq!(bob_shared, dalek_bob_shared.as_ref());
}

fn main() {
loop {
fuzz!(|data: &[u8]| {
// Seed the RNG
let mut seeded_rng = make_seeded_rng(data);

// Test `orion::hazardous::ecc::x25519`
fuzz_x25519(&mut seeded_rng);
});
}
}

0 comments on commit 0a07cc4

Please sign in to comment.