Skip to content

Commit

Permalink
Test coverage improvement
Browse files Browse the repository at this point in the history
* Improved integration test coverage
* Fine tuned main usage example in documentation
  • Loading branch information
jmlepisto committed Nov 12, 2024
1 parent e7e6257 commit d0314ef
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ fn main() {
// Move to transport state
let mut alice = alice.finalize().unwrap();

// All done! Use .send() and .receive() on the transport state to communicate
// with the peer
// All done! Use .send() and .receive() on the transport state to encrypt
// and decrypt communication with the peer
let n = alice.send(b"Hello from Alice", &mut buf_alice_send).unwrap();
my_send_function(&buf_alice_send[..n]);
}
Expand Down
73 changes: 35 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,52 +64,49 @@
//! Simplified example with the most straightforward (and unsecure) PQ handshake pattern and
//! no handshake payload data at all:
//!
//! ```ignore
//! ```no_run
//! use clatter::crypto::cipher::ChaChaPoly;
//! use clatter::crypto::hash::Sha512;
//! use clatter::crypto::kem::rust_crypto_kyber::Kyber512;
//! use clatter::handshakepattern::noise_pqnn;
//! use clatter::traits::Handshaker;
//! use clatter::PqHandshake;
//!
//! fn main() {
//! let mut rng_alice = rand::thread_rng();
//!
//! // Instantiate initiator handshake
//! let mut alice = PqHandshake::<Kyber512, Kyber512, ChaChaPoly, Sha512, _>::new(
//! noise_pqnn(), // Handshake pattern
//! &[], // Prologue data
//! true, // Are we the initiator
//! None, // Pre-shared keys..
//! None, // ..
//! None, // ..
//! None, // ..
//! &mut rng_alice, // RNG instance
//! ).unwrap();
//!
//! let mut buf_alice_send = [0u8; 4096];
//! let mut buf_alice_receive = [0u8; 4096];
//!
//! // Write handshake message and deliver to peer
//! let n = alice.write_message(&[], &mut buf_alice_send).unwrap();
//! my_send_function(&buf_alice_send[..n]);
//!
//! // Receive handshake message and process it
//! let n = my_receive_function(&mut buf_alice_receive);
//! let _ = alice.read_message(&buf_alice_receive[..n], &mut[]).unwrap();
//!
//! assert!(alice.is_finished());
//!
//! // Move to transport state
//! let mut alice = alice.finalize().unwrap();
//!
//! // All done! Use .send() and .receive() on the transport state to communicate
//! // with the peer
//! let n = alice.send(b"Hello from Alice", &mut buf_alice_send).unwrap();
//! my_send_function(& buf_alice_send[..n]);
//! }
//! let mut rng_alice = rand::thread_rng();
//!
//! // Instantiate initiator handshake
//! let mut alice = PqHandshake::<Kyber512, Kyber512, ChaChaPoly, Sha512, _>::new(
//! noise_pqnn(), // Handshake pattern
//! &[], // Prologue data
//! true, // Are we the initiator
//! None, // Pre-shared keys..
//! None, // ..
//! None, // ..
//! None, // ..
//! &mut rng_alice, // RNG instance
//! ).unwrap();
//!
//! let mut buf_alice_send = [0u8; 4096];
//! let mut buf_alice_receive = [0u8; 4096];
//!
//! // Write handshake message and deliver to peer
//! let n = alice.write_message(&[], &mut buf_alice_send).unwrap();
//! // --> Send &buf_alice_send[..n]) to peer
//!
//! // Receive handshake message and process it
//! // <-- Receive message from peer to &buf_alice_receive
//! let _ = alice.read_message(&buf_alice_receive[..n], &mut[]).unwrap();
//!
//! assert!(alice.is_finished());
//!
//! // Move to transport state
//! let mut alice = alice.finalize().unwrap();
//!
//! // All done! Use .send() and .receive() on the transport state to encrypt
//! // and decrypt communication with the peer
//! let n = alice.send(b"Hello from Alice", &mut buf_alice_send).unwrap();
//! // --> Send &buf_alice_send[..n]) to peer
//! ```
#![allow(clippy::needless_doctest_main)]
// Not really used for now
#[cfg(feature = "alloc")]
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cargo test --features=\
cargo test --release --features=\
use-sha,\
use-blake2,\
use-aes-gcm,\
Expand Down
46 changes: 39 additions & 7 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use clatter::bytearray::ByteArray;
use clatter::crypto::cipher::{AesGcm, ChaChaPoly};
use clatter::crypto::dh::X25519;
use clatter::crypto::hash::{Blake2b, Sha512};
use clatter::crypto::hash::{Blake2b, Blake2s, Sha256, Sha512};
#[cfg(feature = "use-argyle-kyber768")]
use clatter::crypto::kem::argyle_software_kyber::Kyber768 as ArgyleKyber;
use clatter::crypto::kem::{pqclean_kyber, rust_crypto_kyber};
Expand Down Expand Up @@ -56,7 +56,14 @@ fn smoke_nq_handshakes() {

for pattern in handshakes {
nq_handshake::<X25519, ChaChaPoly, Sha512>(pattern.clone());
nq_handshake::<X25519, AesGcm, Blake2b>(pattern);
nq_handshake::<X25519, ChaChaPoly, Sha256>(pattern.clone());
nq_handshake::<X25519, ChaChaPoly, Blake2b>(pattern.clone());
nq_handshake::<X25519, ChaChaPoly, Blake2s>(pattern.clone());

nq_handshake::<X25519, AesGcm, Sha512>(pattern.clone());
nq_handshake::<X25519, AesGcm, Sha256>(pattern.clone());
nq_handshake::<X25519, AesGcm, Blake2b>(pattern.clone());
nq_handshake::<X25519, AesGcm, Blake2s>(pattern.clone());
}
}

Expand Down Expand Up @@ -95,15 +102,40 @@ fn smoke_pq_handshakes() {
noise_pqxx_psk3(),
];

fn cipher_hash_combos<EKEM: Kem, SKEM: Kem>(pattern: HandshakePattern) {
pq_handshake::<EKEM, SKEM, ChaChaPoly, Blake2b>(pattern.clone());
pq_handshake::<EKEM, SKEM, ChaChaPoly, Blake2s>(pattern.clone());
pq_handshake::<EKEM, SKEM, ChaChaPoly, Sha256>(pattern.clone());
pq_handshake::<EKEM, SKEM, ChaChaPoly, Sha512>(pattern.clone());

pq_handshake::<EKEM, SKEM, AesGcm, Blake2b>(pattern.clone());
pq_handshake::<EKEM, SKEM, AesGcm, Blake2s>(pattern.clone());
pq_handshake::<EKEM, SKEM, AesGcm, Sha256>(pattern.clone());
pq_handshake::<EKEM, SKEM, AesGcm, Sha512>(pattern.clone());
}

for pattern in handshakes {
pq_handshake::<rust_crypto_kyber::Kyber512, rust_crypto_kyber::Kyber768, ChaChaPoly, Blake2b>(
// Rust crypto
cipher_hash_combos::<rust_crypto_kyber::Kyber512, rust_crypto_kyber::Kyber512>(
pattern.clone(),
);
cipher_hash_combos::<rust_crypto_kyber::Kyber768, rust_crypto_kyber::Kyber768>(
pattern.clone(),
);
pq_handshake::<rust_crypto_kyber::Kyber1024, pqclean_kyber::Kyber512, AesGcm, Sha512>(
cipher_hash_combos::<rust_crypto_kyber::Kyber1024, rust_crypto_kyber::Kyber1024>(
pattern.clone(),
);

// PQCLean
cipher_hash_combos::<pqclean_kyber::Kyber512, pqclean_kyber::Kyber512>(pattern.clone());
cipher_hash_combos::<pqclean_kyber::Kyber768, pqclean_kyber::Kyber768>(pattern.clone());
cipher_hash_combos::<pqclean_kyber::Kyber1024, pqclean_kyber::Kyber1024>(pattern.clone());

// One cross-use test just in case
cipher_hash_combos::<pqclean_kyber::Kyber768, rust_crypto_kyber::Kyber768>(pattern.clone());

#[cfg(feature = "use-argyle-kyber768")]
pq_handshake::<ArgyleKyber, rust_crypto_kyber::Kyber512, AesGcm, Sha512>(pattern);
cipher_hash_combos::<ArgyleKyber, ArgyleKyber>(pattern);
}
}

Expand Down Expand Up @@ -217,8 +249,8 @@ fn pq_handshake<EKEM: Kem, SKEM: Kem, C: Cipher, H: Hash>(pattern: HandshakePatt
bob.push_psk(psk);
}

let mut alice_buf = [0u8; 4096];
let mut bob_buf = [0u8; 4096];
let mut alice_buf = [0u8; 8182];
let mut bob_buf = [0u8; 8182];

loop {
let n = alice.write_message(&[], &mut alice_buf).unwrap();
Expand Down

0 comments on commit d0314ef

Please sign in to comment.