2020-08-14 16:20:42 -04:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
//
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
use crate::{
|
|
|
|
|
ciphersuite::CipherSuite,
|
2021-01-04 14:27:20 -08:00
|
|
|
envelope::{Envelope, InnerEnvelopeMode},
|
2021-06-15 17:31:12 -07:00
|
|
|
errors::*,
|
2020-08-14 16:20:42 -04:00
|
|
|
key_exchange::{
|
2021-06-14 21:22:21 -07:00
|
|
|
traits::{FromBytes, KeyExchange, ToBytes},
|
2021-02-02 12:12:20 -08:00
|
|
|
tripledh::{NonceLen, TripleDH},
|
2020-08-14 16:20:42 -04:00
|
|
|
},
|
2021-08-17 05:11:53 +02:00
|
|
|
keypair::KeyPair,
|
2020-11-30 11:37:32 -08:00
|
|
|
serialization::{i2osp, os2ip, serialize},
|
2020-11-16 14:05:43 -08:00
|
|
|
*,
|
2020-08-14 16:20:42 -04:00
|
|
|
};
|
2021-08-12 06:25:07 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
|
use alloc::vec;
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
use alloc::vec::Vec;
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
use curve25519_dalek::ristretto::RistrettoPoint;
|
2021-02-02 12:12:20 -08:00
|
|
|
use generic_array::typenum::Unsigned;
|
2020-08-14 16:20:42 -04:00
|
|
|
use proptest::{collection::vec, prelude::*};
|
2021-02-11 18:10:48 -08:00
|
|
|
use rand::{rngs::OsRng, RngCore};
|
2021-10-25 02:54:32 -07:00
|
|
|
use voprf::group::Group;
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-01-26 09:27:30 -08:00
|
|
|
use sha2::Digest;
|
2020-08-14 16:20:42 -04:00
|
|
|
|
|
|
|
|
struct Default;
|
|
|
|
|
impl CipherSuite for Default {
|
2021-08-04 21:24:46 +02:00
|
|
|
type OprfGroup = RistrettoPoint;
|
|
|
|
|
type KeGroup = RistrettoPoint;
|
2020-08-14 16:20:42 -04:00
|
|
|
type KeyExchange = TripleDH;
|
2021-01-25 13:20:36 -08:00
|
|
|
type Hash = sha2::Sha512;
|
2020-08-14 16:20:42 -04:00
|
|
|
type SlowHash = crate::slow_hash::NoOpHash;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 01:07:07 -07:00
|
|
|
const HASH_SIZE: usize = 64; // Because of SHA512
|
2021-01-25 13:20:36 -08:00
|
|
|
const MAC_SIZE: usize = 64; // Because of SHA512
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
fn random_ristretto_point() -> RistrettoPoint {
|
|
|
|
|
let mut rng = OsRng;
|
|
|
|
|
let mut random_bits = [0u8; 64];
|
|
|
|
|
rng.fill_bytes(&mut random_bits);
|
|
|
|
|
|
|
|
|
|
// This is because RistrettoPoint is on an obsolete sha2 version
|
|
|
|
|
let mut bits = [0u8; 64];
|
|
|
|
|
let mut hasher = sha2::Sha512::new();
|
|
|
|
|
hasher.update(&random_bits[..]);
|
|
|
|
|
bits.copy_from_slice(&hasher.finalize());
|
|
|
|
|
|
|
|
|
|
RistrettoPoint::from_uniform_bytes(&bits)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn client_registration_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let pw = b"hunter2";
|
|
|
|
|
let mut rng = OsRng;
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let blind_result =
|
|
|
|
|
&voprf::NonVerifiableClient::<RistrettoPoint, sha2::Sha512>::blind(pw.to_vec(), &mut rng)?;
|
|
|
|
|
|
|
|
|
|
let bytes: Vec<u8> = [
|
|
|
|
|
serialize(&blind_result.state.serialize(), 2)?,
|
|
|
|
|
serialize(&blind_result.message.serialize(), 2)?,
|
|
|
|
|
]
|
|
|
|
|
.concat();
|
|
|
|
|
|
|
|
|
|
let reg = ClientRegistration::<Default>::deserialize(&bytes[..])?;
|
|
|
|
|
let reg_bytes = reg.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(reg_bytes, bytes);
|
2021-10-25 02:54:32 -07:00
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn server_registration_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
// If we don't have envelope and client_pk, the server registration just
|
|
|
|
|
// contains the prf key
|
|
|
|
|
let mut rng = OsRng;
|
2021-04-16 01:07:07 -07:00
|
|
|
let mut masking_key = [0u8; HASH_SIZE];
|
|
|
|
|
rng.fill_bytes(&mut masking_key);
|
2021-02-21 18:44:36 -08:00
|
|
|
|
2020-10-09 10:51:58 -07:00
|
|
|
// Construct a mock envelope
|
|
|
|
|
let mut mock_envelope_bytes = Vec::new();
|
2021-08-17 05:11:53 +02:00
|
|
|
mock_envelope_bytes.extend_from_slice(&vec![0; NonceLen::USIZE]); // empty nonce
|
|
|
|
|
// mock_envelope_bytes.extend_from_slice(&ciphertext); // ciphertext which is an encrypted private key
|
2021-01-26 09:27:30 -08:00
|
|
|
mock_envelope_bytes.extend_from_slice(&[0; MAC_SIZE]); // length-MAC_SIZE hmac
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let mock_client_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2020-10-09 10:51:58 -07:00
|
|
|
// serialization order: oprf_key, public key, envelope
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut bytes = Vec::<u8>::new();
|
|
|
|
|
bytes.extend_from_slice(&mock_client_kp.public().to_arr());
|
2021-04-16 01:07:07 -07:00
|
|
|
bytes.extend_from_slice(&masking_key);
|
2020-08-14 16:20:42 -04:00
|
|
|
bytes.extend_from_slice(&mock_envelope_bytes);
|
2021-10-25 02:54:32 -07:00
|
|
|
let reg = ServerRegistration::<Default>::deserialize(&bytes[..])?;
|
|
|
|
|
let reg_bytes = reg.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(reg_bytes, bytes);
|
2021-10-25 02:54:32 -07:00
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn registration_request_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let pt = random_ristretto_point();
|
|
|
|
|
let pt_bytes = pt.to_arr().to_vec();
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut input = Vec::new();
|
|
|
|
|
input.extend_from_slice(pt_bytes.as_slice());
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let r1 = RegistrationRequest::<Default>::deserialize(input.as_slice())?;
|
|
|
|
|
let r1_bytes = r1.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(input, r1_bytes);
|
2021-06-15 17:31:12 -07:00
|
|
|
|
|
|
|
|
// Assert that identity group element is rejected
|
|
|
|
|
let identity = RistrettoPoint::identity();
|
|
|
|
|
let identity_bytes = identity.to_arr().to_vec();
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
|
match RegistrationRequest::<Default>::deserialize(identity_bytes.as_slice()) {
|
2021-10-25 02:54:32 -07:00
|
|
|
Err(ProtocolError::LibraryError(InternalError::OprfError(
|
|
|
|
|
voprf::errors::InternalError::PointError,
|
|
|
|
|
))) => true,
|
2021-06-15 17:31:12 -07:00
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn registration_response_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let pt = random_ristretto_point();
|
2020-10-09 10:51:58 -07:00
|
|
|
let beta_bytes = pt.to_arr();
|
|
|
|
|
let mut rng = OsRng;
|
2021-10-25 02:54:32 -07:00
|
|
|
let skp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2020-10-09 10:51:58 -07:00
|
|
|
let pubkey_bytes = skp.public().to_arr();
|
|
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut input = Vec::new();
|
2020-10-09 10:51:58 -07:00
|
|
|
input.extend_from_slice(beta_bytes.as_slice());
|
|
|
|
|
input.extend_from_slice(&pubkey_bytes.as_slice());
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let r2 = RegistrationResponse::<Default>::deserialize(input.as_slice())?;
|
|
|
|
|
let r2_bytes = r2.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(input, r2_bytes);
|
2021-06-15 17:31:12 -07:00
|
|
|
|
|
|
|
|
// Assert that identity group element is rejected
|
|
|
|
|
let identity = RistrettoPoint::identity();
|
|
|
|
|
let identity_bytes = identity.to_arr().to_vec();
|
|
|
|
|
|
|
|
|
|
assert!(match RegistrationResponse::<Default>::deserialize(
|
|
|
|
|
&[identity_bytes, pubkey_bytes.to_vec()].concat()
|
|
|
|
|
) {
|
2021-10-25 02:54:32 -07:00
|
|
|
Err(ProtocolError::LibraryError(InternalError::OprfError(
|
|
|
|
|
voprf::errors::InternalError::PointError,
|
|
|
|
|
))) => true,
|
2021-06-15 17:31:12 -07:00
|
|
|
_ => false,
|
|
|
|
|
});
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn registration_upload_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut rng = OsRng;
|
2021-10-25 02:54:32 -07:00
|
|
|
let skp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2020-08-14 16:20:42 -04:00
|
|
|
let pubkey_bytes = skp.public().to_arr();
|
|
|
|
|
|
|
|
|
|
let mut key = [0u8; 32];
|
|
|
|
|
rng.fill_bytes(&mut key);
|
2021-04-30 15:56:51 -07:00
|
|
|
let mut nonce = [0u8; 32];
|
|
|
|
|
rng.fill_bytes(&mut nonce);
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut masking_key = vec![0u8; <sha2::Sha512 as Digest>::OutputSize::USIZE];
|
2021-04-16 01:07:07 -07:00
|
|
|
rng.fill_bytes(&mut masking_key);
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let randomized_pwd_hasher = hkdf::Hkdf::new(None, &key);
|
|
|
|
|
|
|
|
|
|
let (envelope, _, _) = Envelope::<Default>::seal_raw(
|
|
|
|
|
randomized_pwd_hasher,
|
|
|
|
|
&nonce,
|
|
|
|
|
&pubkey_bytes,
|
|
|
|
|
InnerEnvelopeMode::Internal,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2020-10-09 10:51:58 -07:00
|
|
|
let envelope_bytes = envelope.serialize();
|
|
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut input = Vec::new();
|
|
|
|
|
input.extend_from_slice(&pubkey_bytes[..]);
|
2021-04-16 01:07:07 -07:00
|
|
|
input.extend_from_slice(&masking_key[..]);
|
2021-01-26 09:27:30 -08:00
|
|
|
input.extend_from_slice(&envelope_bytes);
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let r3 = RegistrationUpload::<Default>::deserialize(&input[..])?;
|
|
|
|
|
let r3_bytes = r3.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(input, r3_bytes);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn credential_request_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut rng = OsRng;
|
2020-10-09 10:51:58 -07:00
|
|
|
let alpha = random_ristretto_point();
|
|
|
|
|
let alpha_bytes = alpha.to_arr().to_vec();
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let client_e_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut client_nonce = vec![0u8; NonceLen::USIZE];
|
2020-08-14 16:20:42 -04:00
|
|
|
rng.fill_bytes(&mut client_nonce);
|
|
|
|
|
|
2021-04-30 15:56:51 -07:00
|
|
|
let ke1m: Vec<u8> = [&client_nonce[..], &client_e_kp.public()].concat();
|
2020-08-14 16:20:42 -04:00
|
|
|
|
|
|
|
|
let mut input = Vec::new();
|
2020-10-09 10:51:58 -07:00
|
|
|
input.extend_from_slice(&alpha_bytes);
|
2020-08-14 16:20:42 -04:00
|
|
|
input.extend_from_slice(&ke1m[..]);
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let l1 = CredentialRequest::<Default>::deserialize(input.as_slice())?;
|
|
|
|
|
let l1_bytes = l1.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(input, l1_bytes);
|
2021-06-15 17:31:12 -07:00
|
|
|
|
|
|
|
|
// Assert that identity group element is rejected
|
|
|
|
|
let identity = RistrettoPoint::identity();
|
|
|
|
|
let identity_bytes = identity.to_arr().to_vec();
|
|
|
|
|
|
|
|
|
|
assert!(match CredentialRequest::<Default>::deserialize(
|
|
|
|
|
&[identity_bytes, ke1m.to_vec()].concat()
|
|
|
|
|
) {
|
2021-10-25 02:54:32 -07:00
|
|
|
Err(ProtocolError::LibraryError(InternalError::OprfError(
|
|
|
|
|
voprf::errors::InternalError::PointError,
|
|
|
|
|
))) => true,
|
2021-06-15 17:31:12 -07:00
|
|
|
_ => false,
|
|
|
|
|
});
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn credential_response_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let pt = random_ristretto_point();
|
|
|
|
|
let pt_bytes = pt.to_arr().to_vec();
|
|
|
|
|
|
|
|
|
|
let mut rng = OsRng;
|
|
|
|
|
|
2021-04-16 01:07:07 -07:00
|
|
|
let mut masking_nonce = vec![0u8; 32];
|
|
|
|
|
rng.fill_bytes(&mut masking_nonce);
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut masked_response =
|
|
|
|
|
vec![0u8; <RistrettoPoint as Group>::ElemLen::USIZE + Envelope::<Default>::len()];
|
2021-04-16 01:07:07 -07:00
|
|
|
rng.fill_bytes(&mut masked_response);
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let server_e_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2021-01-25 13:20:36 -08:00
|
|
|
let mut mac = [0u8; MAC_SIZE];
|
2020-08-14 16:20:42 -04:00
|
|
|
rng.fill_bytes(&mut mac);
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut server_nonce = vec![0u8; NonceLen::USIZE];
|
2020-08-14 16:20:42 -04:00
|
|
|
rng.fill_bytes(&mut server_nonce);
|
|
|
|
|
|
2021-04-30 15:56:51 -07:00
|
|
|
let ke2m: Vec<u8> = [&server_nonce[..], &server_e_kp.public(), &mac[..]].concat();
|
2020-08-14 16:20:42 -04:00
|
|
|
|
|
|
|
|
let mut input = Vec::new();
|
|
|
|
|
input.extend_from_slice(pt_bytes.as_slice());
|
2021-04-16 01:07:07 -07:00
|
|
|
input.extend_from_slice(&masking_nonce);
|
|
|
|
|
input.extend_from_slice(&masked_response);
|
2020-08-14 16:20:42 -04:00
|
|
|
input.extend_from_slice(&ke2m[..]);
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let l2 = CredentialResponse::<Default>::deserialize(&input)?;
|
|
|
|
|
let l2_bytes = l2.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(input, l2_bytes);
|
2021-06-15 17:31:12 -07:00
|
|
|
|
|
|
|
|
// Assert that identity group element is rejected
|
|
|
|
|
let identity = RistrettoPoint::identity();
|
|
|
|
|
let identity_bytes = identity.to_arr().to_vec();
|
|
|
|
|
|
2021-06-28 18:04:57 -07:00
|
|
|
assert!(match CredentialResponse::<Default>::deserialize(
|
2021-06-15 17:31:12 -07:00
|
|
|
&[
|
|
|
|
|
identity_bytes,
|
2021-04-16 01:07:07 -07:00
|
|
|
masking_nonce.to_vec(),
|
|
|
|
|
masked_response,
|
2021-06-15 17:31:12 -07:00
|
|
|
ke2m.to_vec()
|
|
|
|
|
]
|
|
|
|
|
.concat()
|
|
|
|
|
) {
|
2021-10-25 02:54:32 -07:00
|
|
|
Err(ProtocolError::LibraryError(InternalError::OprfError(
|
|
|
|
|
voprf::errors::InternalError::PointError,
|
|
|
|
|
))) => true,
|
2021-06-15 17:31:12 -07:00
|
|
|
_ => false,
|
|
|
|
|
});
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 11:49:27 -08:00
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn credential_finalization_roundtrip() -> Result<(), ProtocolError> {
|
2020-11-16 11:49:27 -08:00
|
|
|
let mut rng = OsRng;
|
2021-01-25 13:20:36 -08:00
|
|
|
let mut mac = [0u8; MAC_SIZE];
|
2020-11-16 11:49:27 -08:00
|
|
|
rng.fill_bytes(&mut mac);
|
|
|
|
|
|
2021-01-14 15:30:37 -08:00
|
|
|
let input: Vec<u8> = [&mac[..]].concat();
|
2020-11-16 11:49:27 -08:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let l3 = CredentialFinalization::<Default>::deserialize(&input)?;
|
|
|
|
|
let l3_bytes = l3.serialize()?;
|
2020-11-16 11:49:27 -08:00
|
|
|
assert_eq!(input, l3_bytes);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-11-16 11:49:27 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn client_login_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let pw = b"hunter2";
|
|
|
|
|
let mut rng = OsRng;
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let client_e_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut client_nonce = vec![0u8; NonceLen::USIZE];
|
2020-08-14 16:20:42 -04:00
|
|
|
rng.fill_bytes(&mut client_nonce);
|
|
|
|
|
|
2021-02-02 12:12:20 -08:00
|
|
|
let serialized_credential_request = b"serialized credential_request".to_vec();
|
|
|
|
|
let l1_data = [client_e_kp.private().to_arr().to_vec(), client_nonce].concat();
|
2020-08-14 16:20:42 -04:00
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let blind_result =
|
|
|
|
|
&voprf::NonVerifiableClient::<RistrettoPoint, sha2::Sha512>::blind(pw.to_vec(), &mut rng)?;
|
|
|
|
|
|
2021-02-02 12:12:20 -08:00
|
|
|
let bytes: Vec<u8> = [
|
2021-10-25 02:54:32 -07:00
|
|
|
serialize(&blind_result.state.serialize(), 2)?,
|
|
|
|
|
serialize(&serialized_credential_request, 2)?,
|
|
|
|
|
serialize(&l1_data, 2)?,
|
2021-02-02 12:12:20 -08:00
|
|
|
]
|
|
|
|
|
.concat();
|
2021-10-25 02:54:32 -07:00
|
|
|
let reg = ClientLogin::<Default>::deserialize(&bytes[..])?;
|
|
|
|
|
let reg_bytes = reg.serialize()?;
|
2020-08-14 16:20:42 -04:00
|
|
|
assert_eq!(reg_bytes, bytes);
|
2021-10-25 02:54:32 -07:00
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn ke1_message_roundtrip() -> Result<(), ProtocolError> {
|
2020-08-14 16:20:42 -04:00
|
|
|
let mut rng = OsRng;
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let client_e_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut client_nonce = vec![0u8; NonceLen::USIZE];
|
2020-08-14 16:20:42 -04:00
|
|
|
rng.fill_bytes(&mut client_nonce);
|
|
|
|
|
|
2021-04-30 15:56:51 -07:00
|
|
|
let ke1m: Vec<u8> = [&client_nonce[..], &client_e_kp.public()].concat();
|
2021-06-14 21:22:21 -07:00
|
|
|
let reg = <TripleDH as KeyExchange<sha2::Sha512, RistrettoPoint>>::KE1Message::from_bytes::<
|
|
|
|
|
Default,
|
2021-10-25 02:54:32 -07:00
|
|
|
>(&ke1m[..])?;
|
2020-08-14 16:20:42 -04:00
|
|
|
let reg_bytes = reg.to_bytes();
|
|
|
|
|
assert_eq!(reg_bytes, ke1m);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 11:49:27 -08:00
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn ke2_message_roundtrip() -> Result<(), ProtocolError> {
|
2020-11-16 11:49:27 -08:00
|
|
|
let mut rng = OsRng;
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
let server_e_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
|
2021-01-25 13:20:36 -08:00
|
|
|
let mut mac = [0u8; MAC_SIZE];
|
2020-11-16 11:49:27 -08:00
|
|
|
rng.fill_bytes(&mut mac);
|
2021-08-17 05:11:53 +02:00
|
|
|
let mut server_nonce = vec![0u8; NonceLen::USIZE];
|
2020-11-16 11:49:27 -08:00
|
|
|
rng.fill_bytes(&mut server_nonce);
|
2021-04-30 15:56:51 -07:00
|
|
|
|
|
|
|
|
let ke2m: Vec<u8> = [&server_nonce[..], &server_e_kp.public(), &mac[..]].concat();
|
2020-11-16 11:49:27 -08:00
|
|
|
|
2021-06-14 21:22:21 -07:00
|
|
|
let reg = <TripleDH as KeyExchange<sha2::Sha512, RistrettoPoint>>::KE2Message::from_bytes::<
|
|
|
|
|
Default,
|
2021-10-25 02:54:32 -07:00
|
|
|
>(&ke2m[..])?;
|
2020-11-16 11:49:27 -08:00
|
|
|
let reg_bytes = reg.to_bytes();
|
|
|
|
|
assert_eq!(reg_bytes, ke2m);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-11-16 11:49:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-10-25 02:54:32 -07:00
|
|
|
fn ke3_message_roundtrip() -> Result<(), ProtocolError> {
|
2020-11-16 11:49:27 -08:00
|
|
|
let mut rng = OsRng;
|
2021-01-25 13:20:36 -08:00
|
|
|
let mut mac = [0u8; MAC_SIZE];
|
2020-11-16 11:49:27 -08:00
|
|
|
rng.fill_bytes(&mut mac);
|
|
|
|
|
|
2021-01-14 15:30:37 -08:00
|
|
|
let ke3m: Vec<u8> = [&mac[..]].concat();
|
2020-11-16 11:49:27 -08:00
|
|
|
|
2021-06-14 21:22:21 -07:00
|
|
|
let reg = <TripleDH as KeyExchange<sha2::Sha512, RistrettoPoint>>::KE3Message::from_bytes::<
|
|
|
|
|
Default,
|
2021-10-25 02:54:32 -07:00
|
|
|
>(&ke3m[..])?;
|
2020-11-16 11:49:27 -08:00
|
|
|
let reg_bytes = reg.to_bytes();
|
|
|
|
|
assert_eq!(reg_bytes, ke3m);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2020-11-16 11:49:27 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
proptest! {
|
|
|
|
|
|
2020-11-12 10:00:09 -08:00
|
|
|
#[test]
|
2021-08-12 06:25:07 +02:00
|
|
|
fn test_i2osp_os2ip(bytes in vec(any::<u8>(), 0..core::mem::size_of::<usize>())) {
|
|
|
|
|
assert_eq!(i2osp(os2ip(&bytes).unwrap(), bytes.len()).unwrap(), bytes);
|
2020-11-12 10:00:09 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 16:20:42 -04:00
|
|
|
#[test]
|
2021-02-20 03:51:04 -08:00
|
|
|
fn test_nocrash_registration_request(bytes in vec(any::<u8>(), 0..200)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
RegistrationRequest::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-20 03:51:04 -08:00
|
|
|
fn test_nocrash_registration_response(bytes in vec(any::<u8>(), 0..200)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
RegistrationResponse::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-20 03:51:04 -08:00
|
|
|
fn test_nocrash_registration_upload(bytes in vec(any::<u8>(), 0..200)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
RegistrationUpload::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-20 03:51:04 -08:00
|
|
|
fn test_nocrash_credential_request(bytes in vec(any::<u8>(), 0..500)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
CredentialRequest::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-20 03:51:04 -08:00
|
|
|
fn test_nocrash_credential_response(bytes in vec(any::<u8>(), 0..500)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
CredentialResponse::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-20 03:51:04 -08:00
|
|
|
fn test_nocrash_credential_finalization(bytes in vec(any::<u8>(), 0..500)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
CredentialFinalization::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_nocrash_client_registration(bytes in vec(any::<u8>(), 0..700)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
ClientRegistration::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_nocrash_server_registration(bytes in vec(any::<u8>(), 0..700)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
ServerRegistration::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_nocrash_client_login(bytes in vec(any::<u8>(), 0..700)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
ClientLogin::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_nocrash_server_login(bytes in vec(any::<u8>(), 0..700)) {
|
2021-02-17 02:47:00 -08:00
|
|
|
ServerLogin::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
|
2020-08-14 16:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|