General improvements (#250)
* Remove unnecessary constraints on hash * Remove unnecessary `Result` on `KeyPair::generate_random` * Fix de-serialization issue on `Ke1State` * Fix rustfmt * Remove allocations in `envelope` * Run Clippy for tests and rustdoc lints too * Fix `Debug` implementation * Fix missing constraints on `ClientRegistration` * Fix de-serialization * Pin temporary dependency * Update dependencies * Replace macro with derive-where * Remove unnecessary installation of Rust components * Improve macro naming * Implement `Copy`, `Debug`, `Ord` and `PartialOrd` for high-level items * Add `rust-version` field to `Cargo.toml` * Remove unnecessary allocations * Fix MSRV * Fix no_std * Remove unnecessary allocations * Remove unnecessary allocations * Not importing items from voprf helps readability * Fix rustdoc * Remove unnecessary allocations * Remove unnecessary allocations * Replace `Vec` from `diffie_hellman` with `GenericArray` * Remove unnecessary allocations * Remove unnecessary allocations * Remove `cfg(feature = bench)` guard for `missing_docs` * Fix documentation * Remove all remaining allocations from `KeyExchange` * Improve type-safety * Remove all remaining allocations in `keypair` * Remove last remaining allocations except `NonVerifiableClient` input * Remove base64 encoding in Serde implementation * Remove unnecessary Serde `alloc` feature * Make curve25519-dalek optional * Rename `serialize` crate feature to `serde` * Switch `KeGroup` implementations to higher-level libraries - Fixes missing clamping in X25519 - X25519 is now a separate crate feature * Fix typo
This commit is contained in:
@@ -5,152 +5,47 @@
|
||||
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
|
||||
// of this source tree.
|
||||
|
||||
//! Key Exchange group implementation for x25519
|
||||
//! Key Exchange group implementation for X25519
|
||||
|
||||
use super::KeGroup;
|
||||
use crate::errors::InternalError;
|
||||
use curve25519_dalek::{constants::X25519_BASEPOINT, montgomery::MontgomeryPoint, scalar::Scalar};
|
||||
use generic_array::{typenum::U32, GenericArray};
|
||||
use rand::{CryptoRng, RngCore};
|
||||
use x25519_dalek::{PublicKey, StaticSecret};
|
||||
|
||||
/// The implementation of such a subgroup for Ristretto
|
||||
impl KeGroup for MontgomeryPoint {
|
||||
impl KeGroup for PublicKey {
|
||||
type PkLen = U32;
|
||||
type SkLen = U32;
|
||||
|
||||
fn from_pk_slice(element_bits: &GenericArray<u8, Self::PkLen>) -> Result<Self, InternalError> {
|
||||
Ok(Self(*element_bits.as_ref()))
|
||||
Ok(Self::from(<[u8; 32]>::from(*element_bits)))
|
||||
}
|
||||
|
||||
fn random_sk<R: RngCore + CryptoRng>(rng: &mut R) -> GenericArray<u8, Self::SkLen> {
|
||||
let mut scalar_bytes = [0u8; 32];
|
||||
|
||||
loop {
|
||||
let scalar = {
|
||||
#[cfg(not(test))]
|
||||
{
|
||||
let mut scalar_bytes = [0u8; 64];
|
||||
rng.fill_bytes(&mut scalar_bytes);
|
||||
Scalar::from_bytes_mod_order_wide(&scalar_bytes)
|
||||
}
|
||||
rng.fill_bytes(&mut scalar_bytes);
|
||||
|
||||
// Tests need an exact conversion from bytes to scalar, sampling only 32 bytes from rng
|
||||
#[cfg(test)]
|
||||
{
|
||||
let mut scalar_bytes = [0u8; 32];
|
||||
rng.fill_bytes(&mut scalar_bytes);
|
||||
Scalar::from_bytes_mod_order(scalar_bytes)
|
||||
}
|
||||
};
|
||||
|
||||
if scalar != Scalar::zero() {
|
||||
break GenericArray::clone_from_slice(&scalar.to_bytes());
|
||||
if scalar_bytes != [0u8; 32] {
|
||||
break StaticSecret::from(scalar_bytes).to_bytes().into();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn public_key(sk: &GenericArray<u8, Self::SkLen>) -> Self {
|
||||
X25519_BASEPOINT * Scalar::from_bits(*sk.as_ref())
|
||||
Self::from(&StaticSecret::from(<[u8; 32]>::from(*sk)))
|
||||
}
|
||||
|
||||
fn to_arr(&self) -> GenericArray<u8, Self::PkLen> {
|
||||
self.to_bytes().into()
|
||||
}
|
||||
|
||||
fn diffie_hellman(&self, sk: &GenericArray<u8, Self::SkLen>) -> GenericArray<u8, Self::PkLen> {
|
||||
(self * Scalar::from_bits(*sk.as_ref())).to_arr()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::errors::ProtocolError;
|
||||
|
||||
#[test]
|
||||
fn test_x25519() -> Result<(), ProtocolError> {
|
||||
use crate::{
|
||||
key_exchange::tripledh::TripleDH, slow_hash::NoOpHash, CipherSuite, ClientLogin,
|
||||
ClientLoginFinishParameters, ClientLoginFinishResult, ClientLoginStartResult,
|
||||
ClientRegistration, ClientRegistrationFinishParameters, ClientRegistrationFinishResult,
|
||||
ClientRegistrationStartResult, ServerLogin, ServerLoginStartParameters,
|
||||
ServerLoginStartResult, ServerRegistration, ServerSetup,
|
||||
};
|
||||
use curve25519_dalek::ristretto::RistrettoPoint;
|
||||
use rand::rngs::OsRng;
|
||||
|
||||
struct X25519Sha512NoSlowHash;
|
||||
impl CipherSuite for X25519Sha512NoSlowHash {
|
||||
type OprfGroup = RistrettoPoint;
|
||||
type KeGroup = MontgomeryPoint;
|
||||
type KeyExchange = TripleDH;
|
||||
type Hash = sha2::Sha512;
|
||||
type SlowHash = NoOpHash;
|
||||
}
|
||||
|
||||
const PASSWORD: &[u8] = b"1234";
|
||||
|
||||
let server_setup = ServerSetup::<X25519Sha512NoSlowHash>::new(&mut OsRng)?;
|
||||
|
||||
let ClientRegistrationStartResult {
|
||||
message,
|
||||
state: client,
|
||||
} = ClientRegistration::start(&mut OsRng, PASSWORD)?;
|
||||
let message = ServerRegistration::start(&server_setup, message, &[])?.message;
|
||||
let ClientRegistrationFinishResult {
|
||||
message,
|
||||
export_key: register_export_key,
|
||||
..
|
||||
} = client.finish(
|
||||
&mut OsRng,
|
||||
message,
|
||||
ClientRegistrationFinishParameters::default(),
|
||||
)?;
|
||||
let server_registration = ServerRegistration::finish(message);
|
||||
|
||||
let ClientLoginStartResult {
|
||||
message,
|
||||
state: client,
|
||||
} = ClientLogin::start(&mut OsRng, PASSWORD)?;
|
||||
let ServerLoginStartResult {
|
||||
message,
|
||||
state: server,
|
||||
..
|
||||
} = ServerLogin::start(
|
||||
&mut OsRng,
|
||||
&server_setup,
|
||||
Some(server_registration),
|
||||
message,
|
||||
&[],
|
||||
ServerLoginStartParameters::default(),
|
||||
)?;
|
||||
let ClientLoginFinishResult {
|
||||
message,
|
||||
session_key: client_session_key,
|
||||
export_key: login_export_key,
|
||||
..
|
||||
} = client.finish(message, ClientLoginFinishParameters::default())?;
|
||||
let server_session_key = server.finish(message)?.session_key;
|
||||
|
||||
assert_eq!(register_export_key, login_export_key);
|
||||
assert_eq!(client_session_key, server_session_key);
|
||||
|
||||
let ClientLoginStartResult {
|
||||
message,
|
||||
state: client,
|
||||
} = ClientLogin::start(&mut OsRng, PASSWORD)?;
|
||||
let ServerLoginStartResult { message, .. } = ServerLogin::start(
|
||||
&mut OsRng,
|
||||
&server_setup,
|
||||
None,
|
||||
message,
|
||||
&[],
|
||||
ServerLoginStartParameters::default(),
|
||||
)?;
|
||||
|
||||
assert!(matches!(
|
||||
client.finish(message, ClientLoginFinishParameters::default()),
|
||||
Err(ProtocolError::InvalidLoginError)
|
||||
));
|
||||
|
||||
Ok(())
|
||||
fn diffie_hellman(&self, sk: &GenericArray<u8, Self::SkLen>) -> GenericArray<u8, Self::SkLen> {
|
||||
StaticSecret::from(<[u8; 32]>::from(*sk))
|
||||
.diffie_hellman(self)
|
||||
.to_bytes()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user