Skip to content

Commit

Permalink
Initial fuzzer setup for X25519 (see orion-rs/orion#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
brycx committed Sep 30, 2021
1 parent 38d7697 commit 752f42d
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 @@ -12,8 +12,9 @@ sodiumoxide = "0.2.6"
ring = "0.16.20"
blake2-rfc = "0.2.18"
chacha = "0.3.0"
orion = { git = "https://github.com/orion-rs/orion", branch = "master" }
orion = { git = "https://github.com/orion-rs/orion", branch = "curve25519" }
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
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, rand_vec_in_range, 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);
assert_eq!(bob_shared, &dalek_bob_shared);
}

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 752f42d

Please sign in to comment.