diff --git a/src/envelope.rs b/src/envelope.rs index 216f14e..e1e07f1 100644 --- a/src/envelope.rs +++ b/src/envelope.rs @@ -121,6 +121,31 @@ pub(crate) struct OpenedInnerEnvelope { pub(crate) export_key: GenericArray::OutputSize>, } +#[cfg(not(test))] +type SealRaw = ( + Envelope, + GenericArray::Hash as Digest>::OutputSize>, +); +#[cfg(test)] +type SealRaw = ( + Envelope, + GenericArray::Hash as Digest>::OutputSize>, + Vec, +); +#[cfg(not(test))] +type Seal = ( + Envelope, + PublicKey<::Group>, + GenericArray::Hash as Digest>::OutputSize>, +); +#[cfg(test)] +type Seal = ( + Envelope, + PublicKey<::Group>, + GenericArray::Hash as Digest>::OutputSize>, + Vec, +); + impl Envelope { fn hmac_key_size() -> usize { ::OutputSize::to_usize() @@ -182,14 +207,7 @@ impl Envelope { key: &[u8], server_s_pk: &[u8], optional_ids: Option, - ) -> Result< - ( - Self, - PublicKey, - GenericArray::OutputSize>, - ), - ProtocolError, - > { + ) -> Result, ProtocolError> { let mut nonce = vec![0u8; NONCE_LEN]; rng.fill_bytes(&mut nonce); @@ -202,8 +220,14 @@ impl Envelope { bytestrings_from_identifiers(&optional_ids, &client_s_pk.to_arr(), server_s_pk)?; let aad = construct_aad(&id_u, &id_s, server_s_pk); - let (envelope, export_key) = Self::seal_raw(key, &nonce, &aad, mode)?; - Ok((envelope, client_s_pk, export_key)) + let result = Self::seal_raw(key, &nonce, &aad, mode)?; + Ok(( + result.0, + client_s_pk, + result.1, + #[cfg(test)] + result.2, + )) } /// Uses a key to convert the plaintext into an envelope, authenticated by the aad field. @@ -214,7 +238,7 @@ impl Envelope { nonce: &[u8], aad: &[u8], mode: InnerEnvelopeMode, - ) -> Result<(Self, GenericArray::OutputSize>), InternalPakeError> { + ) -> Result, InternalPakeError> { let h = Hkdf::::new(None, key); let mut hmac_key = vec![0u8; Self::hmac_key_size()]; let mut export_key = vec![0u8; Self::export_key_size()]; @@ -238,6 +262,8 @@ impl Envelope { hmac: hmac_bytes, }, GenericArray::clone_from_slice(&export_key), + #[cfg(test)] + hmac_key, )) } diff --git a/src/opaque.rs b/src/opaque.rs index 3704fae..6a4a320 100644 --- a/src/opaque.rs +++ b/src/opaque.rs @@ -278,6 +278,9 @@ pub struct ClientRegistrationFinishResult { /// Instance of the ClientRegistration, only used in tests for checking zeroize #[cfg(test)] pub state: ClientRegistration, + /// AuthKey, only used in tests + #[cfg(test)] + pub auth_key: Vec, /// Password derived key, only used in tests #[cfg(test)] pub randomized_pwd: GenericArray::OutputSize>, @@ -293,6 +296,8 @@ impl Clone for ClientRegistrationFinishResult { #[cfg(test)] state: self.state.clone(), #[cfg(test)] + auth_key: self.auth_key.clone(), + #[cfg(test)] randomized_pwd: self.randomized_pwd.clone(), } } @@ -326,20 +331,22 @@ impl ClientRegistration { h.expand(STR_MASKING_KEY, &mut masking_key) .map_err(|_| InternalPakeError::HkdfError)?; - let (envelope, client_s_pk, export_key) = + let result = Envelope::::seal(rng, &password_derived_key, &r2.server_s_pk, optional_ids)?; Ok(ClientRegistrationFinishResult { message: RegistrationUpload { - envelope, + envelope: result.0, masking_key: GenericArray::clone_from_slice(&masking_key[..]), - client_s_pk, + client_s_pk: result.1, }, - export_key, + export_key: result.2, server_s_pk: r2.server_s_pk, #[cfg(test)] state: self, #[cfg(test)] + auth_key: result.3, + #[cfg(test)] randomized_pwd, }) } diff --git a/src/serialization/tests.rs b/src/serialization/tests.rs index 9109a92..6f096ad 100644 --- a/src/serialization/tests.rs +++ b/src/serialization/tests.rs @@ -155,7 +155,7 @@ fn registration_upload_roundtrip() { let mut masking_key = vec![0u8; ::OutputSize::to_usize()]; rng.fill_bytes(&mut masking_key); - let (envelope, _) = + let (envelope, _, _) = Envelope::::seal_raw(&key, &nonce, &pubkey_bytes, InnerEnvelopeMode::Internal) .unwrap(); let envelope_bytes = envelope.serialize(); diff --git a/src/tests/opaque_test_vectors.rs b/src/tests/opaque_test_vectors.rs index 139d445..41ce206 100644 --- a/src/tests/opaque_test_vectors.rs +++ b/src/tests/opaque_test_vectors.rs @@ -63,6 +63,7 @@ pub struct TestVectorParameters { pub KE3: Vec, pub export_key: Vec, pub session_key: Vec, + pub auth_key: Vec, pub randomized_pwd: Vec, } @@ -483,6 +484,7 @@ fn populate_test_vectors(values: &Value) -> TestVectorParameters { blind_login: parse!(values, "blind_login"), export_key: parse!(values, "export_key"), session_key: parse!(values, "session_key"), + auth_key: parse!(values, "auth_key"), randomized_pwd: parse!(values, "randomized_pwd"), } } @@ -569,6 +571,10 @@ fn test_registration_upload() -> Result<(), ProtocolError> { }, )?; + assert_eq!( + hex::encode(parameters.auth_key), + hex::encode(result.auth_key) + ); assert_eq!( hex::encode(parameters.randomized_pwd), hex::encode(result.randomized_pwd)