General Improvements (#268)
* Move `elliptic-curve` implementation to points to allow `Zeroize` * Simplify `Ristretto255::random_scalar` implementation * Fix `Ristretto255` deserialization * Remove unnecessary check in `Ristretto255::random_scalar` * Base `X25519` implementation on `curve25519-dalek` * Constrain public and secret key to `Copy` * Replace manual `ZeroizeOnDrop` implementation with `derive` * Update dependencies * Add `warn(unused_crate_dependencies)` * Sync crate feature naming with `voprf` * Remove unnecessary dependency crate features * Never produce a zero scalar * Rename `OprfGroup` to `OprfCs` * Rename `TripleDH` to `TripleDh` * Remove `slow-hash` crate feature * Rename `NoOpHash` to `Identity` * Rename `SlowHash` to `Ksf` * Move `KeyExchange` type definitions down * Deserialize secret and public keys from slices * Remove `PrivateKey::from_bytes` * Rename `From/ToBytes` to `De/Serialize` * Re-export `serde_` as `serde` * Custom `De/Serialize` implementation for keys * Remove custom `De/Serialize` implementation * Run Taplo v0.6
This commit is contained in:
+23
-27
@@ -45,38 +45,34 @@ pub(crate) fn os2ip(input: &[u8]) -> Result<usize, ProtocolError> {
|
||||
|
||||
/// Computes `I2OSP(len(input), max_bytes) || input` and helps hold output
|
||||
/// without allocation.
|
||||
pub(crate) struct Serialize<
|
||||
'a,
|
||||
L1: ArrayLength<u8>,
|
||||
L2: ArrayLength<u8> = U0,
|
||||
L3: ArrayLength<u8> = U0,
|
||||
> {
|
||||
pub(crate) struct Input<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8> = U0, L3: ArrayLength<u8> = U0>
|
||||
{
|
||||
octet: GenericArray<u8, L1>,
|
||||
input: Input<'a, L2, L3>,
|
||||
input: InnerInput<'a, L2, L3>,
|
||||
}
|
||||
|
||||
enum Input<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> {
|
||||
enum InnerInput<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> {
|
||||
Owned(GenericArray<u8, L1>),
|
||||
Borrowed(&'a [u8]),
|
||||
Label(([&'a [u8]; 2], PhantomData<L2>)),
|
||||
}
|
||||
|
||||
impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>, L3: ArrayLength<u8>> Serialize<'a, L1, L2, L3> {
|
||||
impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>, L3: ArrayLength<u8>> Input<'a, L1, L2, L3> {
|
||||
// Variation of `serialize` that takes a borrowed `input
|
||||
pub(crate) fn from(input: &'a [u8]) -> Result<Serialize<'a, L1, L2>, ProtocolError> {
|
||||
Ok(Serialize {
|
||||
pub(crate) fn from(input: &'a [u8]) -> Result<Input<'a, L1, L2>, ProtocolError> {
|
||||
Ok(Input {
|
||||
octet: i2osp::<L1>(input.len())?,
|
||||
input: Input::Borrowed(input),
|
||||
input: InnerInput::Borrowed(input),
|
||||
})
|
||||
}
|
||||
|
||||
// Variation of `serialize` that takes an owned `input`
|
||||
pub(crate) fn from_owned(
|
||||
input: GenericArray<u8, L2>,
|
||||
) -> Result<Serialize<'a, L1, L2>, ProtocolError> {
|
||||
Ok(Serialize {
|
||||
) -> Result<Input<'a, L1, L2>, ProtocolError> {
|
||||
Ok(Input {
|
||||
octet: i2osp::<L1>(input.len())?,
|
||||
input: Input::Owned(input),
|
||||
input: InnerInput::Owned(input),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -84,10 +80,10 @@ impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>, L3: ArrayLength<u8>> Serializ
|
||||
pub(crate) fn from_label(
|
||||
opaque: &'a [u8],
|
||||
label: &'a [u8],
|
||||
) -> Result<Serialize<'a, L1, U0, U2>, ProtocolError> {
|
||||
Ok(Serialize {
|
||||
) -> Result<Input<'a, L1, U0, U2>, ProtocolError> {
|
||||
Ok(Input {
|
||||
octet: i2osp::<L1>(opaque.len() + label.len())?,
|
||||
input: Input::Label(([opaque, label], PhantomData)),
|
||||
input: InnerInput::Label(([opaque, label], PhantomData)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -96,11 +92,11 @@ impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>, L3: ArrayLength<u8>> Serializ
|
||||
[self.octet.as_slice()]
|
||||
.into_iter()
|
||||
.chain(match &self.input {
|
||||
Input::Owned(bytes) => [bytes.as_slice()],
|
||||
Input::Borrowed(bytes) => [*bytes],
|
||||
Input::Label((iter, _)) => [iter[0]],
|
||||
InnerInput::Owned(bytes) => [bytes.as_slice()],
|
||||
InnerInput::Borrowed(bytes) => [*bytes],
|
||||
InnerInput::Label((iter, _)) => [iter[0]],
|
||||
})
|
||||
.chain(if let Input::Label((iter, _)) = &self.input {
|
||||
.chain(if let InnerInput::Label((iter, _)) = &self.input {
|
||||
Some(iter[1])
|
||||
} else {
|
||||
None
|
||||
@@ -108,11 +104,11 @@ impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>, L3: ArrayLength<u8>> Serializ
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> Serialize<'a, L1, L2, U0> {
|
||||
impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> Input<'a, L1, L2, U0> {
|
||||
pub(crate) fn to_array_2(&self) -> [&[u8]; 2] {
|
||||
let input = match &self.input {
|
||||
Input::Borrowed(value) => value,
|
||||
Input::Owned(value) => value.as_slice(),
|
||||
InnerInput::Borrowed(value) => value,
|
||||
InnerInput::Owned(value) => value.as_slice(),
|
||||
_ => unreachable!("unexpected `Serialize` constructed with wrong generics"),
|
||||
};
|
||||
|
||||
@@ -120,10 +116,10 @@ impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> Serialize<'a, L1, L2, U0> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> Serialize<'a, L1, L2, U2> {
|
||||
impl<'a, L1: ArrayLength<u8>, L2: ArrayLength<u8>> Input<'a, L1, L2, U2> {
|
||||
pub(crate) fn to_array_3(&self) -> [&[u8]; 3] {
|
||||
match self.input {
|
||||
Input::Label((label, _)) => [self.octet.as_slice(), label[0], label[1]],
|
||||
InnerInput::Label((label, _)) => [self.octet.as_slice(), label[0], label[1]],
|
||||
_ => unreachable!("unexpected `Serialize` constructed with wrong generics"),
|
||||
}
|
||||
}
|
||||
|
||||
+30
-30
@@ -25,9 +25,9 @@ use crate::errors::*;
|
||||
use crate::hash::{Hash, OutputSize, ProxyHash};
|
||||
use crate::key_exchange::group::KeGroup;
|
||||
use crate::key_exchange::traits::{
|
||||
FromBytes, Ke1MessageLen, Ke1StateLen, Ke2MessageLen, KeyExchange, ToBytes,
|
||||
Deserialize, Ke1MessageLen, Ke1StateLen, Ke2MessageLen, KeyExchange, Serialize,
|
||||
};
|
||||
use crate::key_exchange::tripledh::{NonceLen, TripleDH};
|
||||
use crate::key_exchange::tripledh::{NonceLen, TripleDh};
|
||||
use crate::keypair::{KeyPair, SecretKey};
|
||||
use crate::messages::CredentialResponseWithoutKeLen;
|
||||
use crate::opaque::{ClientLoginLen, ClientRegistrationLen, MaskedResponseLen};
|
||||
@@ -39,19 +39,19 @@ struct Ristretto255;
|
||||
|
||||
#[cfg(feature = "ristretto255")]
|
||||
impl CipherSuite for Ristretto255 {
|
||||
type OprfGroup = crate::Ristretto255;
|
||||
type OprfCs = crate::Ristretto255;
|
||||
type KeGroup = crate::Ristretto255;
|
||||
type KeyExchange = TripleDH;
|
||||
type SlowHash = crate::slow_hash::NoOpHash;
|
||||
type KeyExchange = TripleDh;
|
||||
type Ksf = crate::ksf::Identity;
|
||||
}
|
||||
|
||||
struct P256;
|
||||
|
||||
impl CipherSuite for P256 {
|
||||
type OprfGroup = ::p256::NistP256;
|
||||
type OprfCs = ::p256::NistP256;
|
||||
type KeGroup = ::p256::NistP256;
|
||||
type KeyExchange = TripleDH;
|
||||
type SlowHash = crate::slow_hash::NoOpHash;
|
||||
type KeyExchange = TripleDh;
|
||||
type Ksf = crate::ksf::Identity;
|
||||
}
|
||||
|
||||
fn random_point<CS: CipherSuite>() -> <CS::KeGroup as KeGroup>::Pk
|
||||
@@ -65,7 +65,7 @@ where
|
||||
{
|
||||
let mut rng = OsRng;
|
||||
let sk = CS::KeGroup::random_sk(&mut rng);
|
||||
CS::KeGroup::public_key(&sk)
|
||||
CS::KeGroup::public_key(sk)
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -85,7 +85,7 @@ fn client_registration_roundtrip() -> Result<(), ProtocolError> {
|
||||
let pw = b"hunter2";
|
||||
let mut rng = OsRng;
|
||||
|
||||
let blind_result = &voprf::NonVerifiableClient::<CS::OprfGroup>::blind(pw, &mut rng)?;
|
||||
let blind_result = &voprf::NonVerifiableClient::<CS::OprfCs>::blind(pw, &mut rng)?;
|
||||
|
||||
let bytes: Vec<u8> = blind_result
|
||||
.state
|
||||
@@ -145,7 +145,7 @@ fn server_registration_roundtrip() -> Result<(), ProtocolError> {
|
||||
let mock_client_kp = KeyPair::<CS::KeGroup>::generate_random(&mut rng);
|
||||
// serialization order: oprf_key, public key, envelope
|
||||
let mut bytes = Vec::<u8>::new();
|
||||
bytes.extend_from_slice(&mock_client_kp.public().to_bytes());
|
||||
bytes.extend_from_slice(&mock_client_kp.public().serialize());
|
||||
bytes.extend_from_slice(&masking_key);
|
||||
bytes.extend_from_slice(&mock_envelope_bytes);
|
||||
let reg = ServerRegistration::<CS>::deserialize(&bytes)?;
|
||||
@@ -173,7 +173,7 @@ fn registration_request_roundtrip() -> Result<(), ProtocolError> {
|
||||
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
|
||||
{
|
||||
let pt = random_point::<CS>();
|
||||
let pt_bytes = CS::KeGroup::serialize_pk(&pt);
|
||||
let pt_bytes = CS::KeGroup::serialize_pk(pt);
|
||||
|
||||
let mut input = Vec::new();
|
||||
input.extend_from_slice(&pt_bytes);
|
||||
@@ -218,10 +218,10 @@ fn registration_response_roundtrip() -> Result<(), ProtocolError> {
|
||||
RegistrationResponseLen<CS>: ArrayLength<u8>,
|
||||
{
|
||||
let pt = random_point::<CS>();
|
||||
let beta_bytes = CS::KeGroup::serialize_pk(&pt);
|
||||
let beta_bytes = CS::KeGroup::serialize_pk(pt);
|
||||
let mut rng = OsRng;
|
||||
let skp = KeyPair::<CS::KeGroup>::generate_random(&mut rng);
|
||||
let pubkey_bytes = skp.public().to_bytes();
|
||||
let pubkey_bytes = skp.public().serialize();
|
||||
|
||||
let mut input = Vec::new();
|
||||
input.extend_from_slice(&beta_bytes);
|
||||
@@ -275,7 +275,7 @@ fn registration_upload_roundtrip() -> Result<(), ProtocolError> {
|
||||
{
|
||||
let mut rng = OsRng;
|
||||
let skp = KeyPair::<CS::KeGroup>::generate_random(&mut rng);
|
||||
let pubkey_bytes = skp.public().to_bytes();
|
||||
let pubkey_bytes = skp.public().serialize();
|
||||
|
||||
let mut key = [0u8; 32];
|
||||
rng.fill_bytes(&mut key);
|
||||
@@ -331,7 +331,7 @@ fn credential_request_roundtrip() -> Result<(), ProtocolError> {
|
||||
{
|
||||
let mut rng = OsRng;
|
||||
let alpha = random_point::<CS>();
|
||||
let alpha_bytes = CS::KeGroup::serialize_pk(&alpha);
|
||||
let alpha_bytes = CS::KeGroup::serialize_pk(alpha);
|
||||
|
||||
let client_e_kp = KeyPair::<CS::KeGroup>::generate_random(&mut rng);
|
||||
let mut client_nonce = [0u8; NonceLen::USIZE];
|
||||
@@ -339,7 +339,7 @@ fn credential_request_roundtrip() -> Result<(), ProtocolError> {
|
||||
|
||||
let ke1m: Vec<u8> = [
|
||||
client_nonce.as_ref(),
|
||||
client_e_kp.public().to_bytes().as_ref(),
|
||||
client_e_kp.public().serialize().as_ref(),
|
||||
]
|
||||
.concat();
|
||||
|
||||
@@ -397,7 +397,7 @@ fn credential_response_roundtrip() -> Result<(), ProtocolError> {
|
||||
CredentialResponseLen<CS>: ArrayLength<u8>,
|
||||
{
|
||||
let pt = random_point::<CS>();
|
||||
let pt_bytes = CS::KeGroup::serialize_pk(&pt);
|
||||
let pt_bytes = CS::KeGroup::serialize_pk(pt);
|
||||
|
||||
let mut rng = OsRng;
|
||||
|
||||
@@ -416,7 +416,7 @@ fn credential_response_roundtrip() -> Result<(), ProtocolError> {
|
||||
|
||||
let ke2m: Vec<u8> = [
|
||||
server_nonce.as_ref(),
|
||||
server_e_kp.public().to_bytes().as_ref(),
|
||||
server_e_kp.public().serialize().as_ref(),
|
||||
&mac,
|
||||
]
|
||||
.concat();
|
||||
@@ -523,15 +523,15 @@ fn client_login_roundtrip() -> Result<(), ProtocolError> {
|
||||
]
|
||||
.concat();
|
||||
|
||||
let blind_result = voprf::NonVerifiableClient::<CS::OprfGroup>::blind(pw, &mut rng)?;
|
||||
let blind_result = voprf::NonVerifiableClient::<CS::OprfCs>::blind(pw, &mut rng)?;
|
||||
|
||||
let credential_request = CredentialRequest::<CS> {
|
||||
blinded_element: blind_result.message,
|
||||
ke1_message:
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message::from_bytes(
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message::deserialize(
|
||||
&[
|
||||
client_nonce.as_ref(),
|
||||
client_e_kp.public().to_bytes().as_ref(),
|
||||
client_e_kp.public().serialize().as_ref(),
|
||||
]
|
||||
.concat(),
|
||||
)?,
|
||||
@@ -577,14 +577,14 @@ fn ke1_message_roundtrip() -> Result<(), ProtocolError> {
|
||||
|
||||
let ke1m = [
|
||||
client_nonce.as_slice(),
|
||||
client_e_kp.public().to_bytes().as_ref(),
|
||||
client_e_kp.public().serialize().as_ref(),
|
||||
]
|
||||
.concat();
|
||||
let reg =
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message::from_bytes(
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message::deserialize(
|
||||
&ke1m,
|
||||
)?;
|
||||
let reg_bytes = reg.to_bytes();
|
||||
let reg_bytes = reg.serialize();
|
||||
assert_eq!(*reg_bytes, ke1m);
|
||||
|
||||
Ok(())
|
||||
@@ -618,16 +618,16 @@ fn ke2_message_roundtrip() -> Result<(), ProtocolError> {
|
||||
|
||||
let ke2m: Vec<u8> = [
|
||||
server_nonce.as_slice(),
|
||||
server_e_kp.public().to_bytes().as_ref(),
|
||||
server_e_kp.public().serialize().as_ref(),
|
||||
&mac,
|
||||
]
|
||||
.concat();
|
||||
|
||||
let reg =
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message::from_bytes(
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message::deserialize(
|
||||
&ke2m,
|
||||
)?;
|
||||
let reg_bytes = reg.to_bytes();
|
||||
let reg_bytes = reg.serialize();
|
||||
assert_eq!(*reg_bytes, ke2m);
|
||||
|
||||
Ok(())
|
||||
@@ -658,10 +658,10 @@ fn ke3_message_roundtrip() -> Result<(), ProtocolError> {
|
||||
let ke3m: Vec<u8> = [mac].concat();
|
||||
|
||||
let reg =
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message::from_bytes(
|
||||
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message::deserialize(
|
||||
&ke3m,
|
||||
)?;
|
||||
let reg_bytes = reg.to_bytes();
|
||||
let reg_bytes = reg.serialize();
|
||||
assert_eq!(*reg_bytes, ke3m);
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user