Files
opaque-vx/src/serialization/tests.rs
T

448 lines
14 KiB
Rust
Raw Normal View History

// Copyright (c) Facebook, Inc. and its affiliates.
//
2021-12-03 14:38:11 -08:00
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree and the Apache
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
// of this source tree.
use crate::{
ciphersuite::CipherSuite,
envelope::{Envelope, InnerEnvelopeMode},
errors::*,
key_exchange::{
traits::{FromBytes, KeyExchange, ToBytes},
tripledh::{NonceLen, TripleDH},
},
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
*,
};
2021-08-12 06:25:07 +02:00
#[cfg(test)]
use alloc::vec;
#[cfg(test)]
use alloc::vec::Vec;
2021-10-25 02:54:32 -07:00
use curve25519_dalek::ristretto::RistrettoPoint;
use generic_array::typenum::Unsigned;
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;
use sha2::Digest;
struct Default;
impl CipherSuite for Default {
2021-08-04 21:24:46 +02:00
type OprfGroup = RistrettoPoint;
type KeGroup = RistrettoPoint;
type KeyExchange = TripleDH;
2021-01-25 13:20:36 -08:00
type Hash = sha2::Sha512;
type SlowHash = crate::slow_hash::NoOpHash;
}
const HASH_SIZE: usize = 64; // Because of SHA512
2021-01-25 13:20:36 -08:00
const MAC_SIZE: usize = 64; // Because of SHA512
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> {
let pw = b"hunter2";
let mut rng = OsRng;
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()?;
assert_eq!(reg_bytes, bytes);
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn server_registration_roundtrip() -> Result<(), ProtocolError> {
// If we don't have envelope and client_pk, the server registration just
// contains the prf key
let mut rng = OsRng;
let mut masking_key = [0u8; HASH_SIZE];
rng.fill_bytes(&mut masking_key);
// 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
mock_envelope_bytes.extend_from_slice(&[0; MAC_SIZE]); // length-MAC_SIZE hmac
2021-10-25 02:54:32 -07:00
let mock_client_kp = KeyPair::<<Default as CipherSuite>::OprfGroup>::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_arr());
bytes.extend_from_slice(&masking_key);
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()?;
assert_eq!(reg_bytes, bytes);
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn registration_request_roundtrip() -> Result<(), ProtocolError> {
let pt = random_ristretto_point();
let pt_bytes = pt.to_arr().to_vec();
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()?;
assert_eq!(input, r1_bytes);
// 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,
_ => false,
}
);
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn registration_response_roundtrip() -> Result<(), ProtocolError> {
let pt = random_ristretto_point();
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)?;
let pubkey_bytes = skp.public().to_arr();
let mut input = Vec::new();
input.extend_from_slice(beta_bytes.as_slice());
input.extend_from_slice(&pubkey_bytes.as_slice());
2021-10-25 02:54:32 -07:00
let r2 = RegistrationResponse::<Default>::deserialize(input.as_slice())?;
let r2_bytes = r2.serialize()?;
assert_eq!(input, r2_bytes);
// 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,
_ => false,
});
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn registration_upload_roundtrip() -> Result<(), ProtocolError> {
let mut rng = OsRng;
2021-10-25 02:54:32 -07:00
let skp = KeyPair::<<Default as CipherSuite>::OprfGroup>::generate_random(&mut rng)?;
let pubkey_bytes = skp.public().to_arr();
let mut key = [0u8; 32];
rng.fill_bytes(&mut key);
let mut nonce = [0u8; 32];
rng.fill_bytes(&mut nonce);
2021-08-17 05:11:53 +02:00
let mut masking_key = vec![0u8; <sha2::Sha512 as Digest>::OutputSize::USIZE];
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();
let envelope_bytes = envelope.serialize();
let mut input = Vec::new();
input.extend_from_slice(&pubkey_bytes[..]);
input.extend_from_slice(&masking_key[..]);
input.extend_from_slice(&envelope_bytes);
2021-10-25 02:54:32 -07:00
let r3 = RegistrationUpload::<Default>::deserialize(&input[..])?;
let r3_bytes = r3.serialize()?;
assert_eq!(input, r3_bytes);
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn credential_request_roundtrip() -> Result<(), ProtocolError> {
let mut rng = OsRng;
let alpha = random_ristretto_point();
let alpha_bytes = alpha.to_arr().to_vec();
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];
rng.fill_bytes(&mut client_nonce);
let ke1m: Vec<u8> = [&client_nonce[..], &client_e_kp.public()].concat();
let mut input = Vec::new();
input.extend_from_slice(&alpha_bytes);
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()?;
assert_eq!(input, l1_bytes);
// 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,
_ => false,
});
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn credential_response_roundtrip() -> Result<(), ProtocolError> {
let pt = random_ristretto_point();
let pt_bytes = pt.to_arr().to_vec();
let mut rng = OsRng;
let mut masking_nonce = vec![0u8; 32];
rng.fill_bytes(&mut masking_nonce);
2021-08-17 05:11:53 +02:00
let mut masked_response =
vec![0u8; <RistrettoPoint as Group>::ElemLen::USIZE + Envelope::<Default>::len()];
rng.fill_bytes(&mut masked_response);
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];
rng.fill_bytes(&mut mac);
2021-08-17 05:11:53 +02:00
let mut server_nonce = vec![0u8; NonceLen::USIZE];
rng.fill_bytes(&mut server_nonce);
let ke2m: Vec<u8> = [&server_nonce[..], &server_e_kp.public(), &mac[..]].concat();
let mut input = Vec::new();
input.extend_from_slice(pt_bytes.as_slice());
input.extend_from_slice(&masking_nonce);
input.extend_from_slice(&masked_response);
input.extend_from_slice(&ke2m[..]);
2021-10-25 02:54:32 -07:00
let l2 = CredentialResponse::<Default>::deserialize(&input)?;
let l2_bytes = l2.serialize()?;
assert_eq!(input, l2_bytes);
// Assert that identity group element is rejected
let identity = RistrettoPoint::identity();
let identity_bytes = identity.to_arr().to_vec();
assert!(match CredentialResponse::<Default>::deserialize(
&[
identity_bytes,
masking_nonce.to_vec(),
masked_response,
ke2m.to_vec()
]
.concat()
) {
2021-10-25 02:54:32 -07:00
Err(ProtocolError::LibraryError(InternalError::OprfError(
voprf::errors::InternalError::PointError,
))) => true,
_ => false,
});
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 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);
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
}
#[test]
2021-10-25 02:54:32 -07:00
fn client_login_roundtrip() -> Result<(), ProtocolError> {
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];
rng.fill_bytes(&mut client_nonce);
let serialized_credential_request = b"serialized credential_request".to_vec();
let l1_data = [client_e_kp.private().to_arr().to_vec(), client_nonce].concat();
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> = [
2021-10-25 02:54:32 -07:00
serialize(&blind_result.state.serialize(), 2)?,
serialize(&serialized_credential_request, 2)?,
serialize(&l1_data, 2)?,
]
.concat();
2021-10-25 02:54:32 -07:00
let reg = ClientLogin::<Default>::deserialize(&bytes[..])?;
let reg_bytes = reg.serialize()?;
assert_eq!(reg_bytes, bytes);
2021-10-25 02:54:32 -07:00
Ok(())
}
#[test]
2021-10-25 02:54:32 -07:00
fn ke1_message_roundtrip() -> Result<(), ProtocolError> {
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];
rng.fill_bytes(&mut client_nonce);
let ke1m: Vec<u8> = [&client_nonce[..], &client_e_kp.public()].concat();
let reg = <TripleDH as KeyExchange<sha2::Sha512, RistrettoPoint>>::KE1Message::from_bytes::<
Default,
2021-10-25 02:54:32 -07:00
>(&ke1m[..])?;
let reg_bytes = reg.to_bytes();
assert_eq!(reg_bytes, ke1m);
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 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);
let ke2m: Vec<u8> = [&server_nonce[..], &server_e_kp.public(), &mac[..]].concat();
2020-11-16 11:49:27 -08: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);
let ke3m: Vec<u8> = [&mac[..]].concat();
2020-11-16 11:49:27 -08: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
}
proptest! {
#[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);
}
#[test]
fn test_nocrash_registration_request(bytes in vec(any::<u8>(), 0..200)) {
RegistrationRequest::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_registration_response(bytes in vec(any::<u8>(), 0..200)) {
RegistrationResponse::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_registration_upload(bytes in vec(any::<u8>(), 0..200)) {
RegistrationUpload::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_credential_request(bytes in vec(any::<u8>(), 0..500)) {
CredentialRequest::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_credential_response(bytes in vec(any::<u8>(), 0..500)) {
CredentialResponse::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_credential_finalization(bytes in vec(any::<u8>(), 0..500)) {
CredentialFinalization::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_client_registration(bytes in vec(any::<u8>(), 0..700)) {
ClientRegistration::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_server_registration(bytes in vec(any::<u8>(), 0..700)) {
ServerRegistration::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_client_login(bytes in vec(any::<u8>(), 0..700)) {
ClientLogin::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_server_login(bytes in vec(any::<u8>(), 0..700)) {
ServerLogin::<Default>::deserialize(&bytes[..]).map_or(true, |_| true);
}
}