// 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. //! Provides the main OPAQUE API use crate::{ ciphersuite::CipherSuite, envelope::{Envelope, EnvelopeCredentialsFormat, ExportKeySize}, errors::{ utils::{check_slice_size, check_slice_size_atleast}, InternalPakeError, PakeError, ProtocolError, }, group::Group, hash::Hash, key_exchange::traits::{KeyExchange, ToBytes}, keypair::{KeyPair, SizedBytesExt}, map_to_curve::GroupWithMapToCurve, oprf, serialization::{ serialize, tokenize, u8_to_credential_type, CredentialType, ProtocolMessageType, }, slow_hash::SlowHash, }; use generic_array::{typenum::Unsigned, GenericArray}; use generic_bytes::SizedBytes; use rand_core::{CryptoRng, RngCore}; use std::collections::HashMap; use std::{convert::TryFrom, marker::PhantomData}; use zeroize::Zeroize; static STR_OPAQUE_VERSION: &[u8] = b"OPAQUE00"; // Messages // ========= /// The message sent by the client to the server, to initiate registration pub struct RegisterFirstMessage { /// blinded password information alpha: Grp, } impl TryFrom<&[u8]> for RegisterFirstMessage { type Error = ProtocolError; fn try_from(first_message_bytes: &[u8]) -> Result { let elem_len = Grp::ElemLen::to_usize(); let checked_slice = check_slice_size(first_message_bytes, elem_len, "first_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(&checked_slice[checked_slice.len() - elem_len..]); let alpha = Grp::from_element_slice(arr)?; Ok(Self { alpha }) } } impl RegisterFirstMessage { /// Byte representation for the registration request pub fn to_bytes(&self) -> Vec { self.alpha.to_arr().to_vec() } /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut registration_request: Vec = Vec::new(); registration_request.extend_from_slice(&serialize(&self.alpha.to_arr(), 2)); let mut output: Vec = Vec::new(); output.push(ProtocolMessageType::from(self) as u8 + 1); output.extend_from_slice(&serialize(®istration_request, 3)); output } /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { if input.is_empty() || input.is_empty() || input[0] != ProtocolMessageType::RegistrationRequest as u8 + 1 { return Err(PakeError::SerializationError.into()); } let (data, remainder) = tokenize(input[1..].to_vec(), 3)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } let (alpha_bytes, remainder) = tokenize(data, 2)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } let checked_slice = check_slice_size( &alpha_bytes, Grp::ElemLen::to_usize(), "first_message_bytes", )?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(checked_slice); let alpha = Grp::from_element_slice(arr)?; Ok(Self { alpha }) } } /// The answer sent by the server to the user, upon reception of the /// registration attempt pub struct RegisterSecondMessage { /// The server's oprf output beta: Grp, /// Server's static public key server_s_pk: Vec, /// Envelope credentials format ecf: EnvelopeCredentialsFormat, } impl TryFrom<&[u8]> for RegisterSecondMessage where Grp: Group, { type Error = ProtocolError; fn try_from(bytes: &[u8]) -> Result { let elem_len = Grp::ElemLen::to_usize(); let checked_slice = check_slice_size_atleast(bytes, elem_len, "second_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(&checked_slice[..elem_len]); let beta = Grp::from_element_slice(arr)?; let server_s_pk = checked_slice[elem_len..].to_vec(); // Note that we use a default envelope credentials format here, since it // is not included in the byte representation let ecf = EnvelopeCredentialsFormat::default()?; Ok(Self { beta, server_s_pk, ecf, }) } } impl RegisterSecondMessage where Grp: Group, { /// Byte representation for the registration response message. This does not /// include the envelope credentials format pub fn to_bytes(&self) -> Vec { [&self.beta.to_arr().to_vec()[..], &self.server_s_pk[..]].concat() } /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut registration_response: Vec = Vec::new(); registration_response.extend_from_slice(&serialize(&self.beta.to_arr(), 2)); registration_response.extend_from_slice(&serialize(&self.server_s_pk, 2)); // Handle ecf serialization let secret_credentials: Vec = self .ecf .secret_credentials .iter() .map(|&x| x as u8 + 1) .collect(); let cleartext_credentials: Vec = self .ecf .cleartext_credentials .iter() .map(|&x| x as u8 + 1) .collect(); let ecf_serialized = [ serialize(&secret_credentials, 1), serialize(&cleartext_credentials, 1), ] .concat(); registration_response.extend_from_slice(&ecf_serialized); let mut output: Vec = Vec::new(); output.push(ProtocolMessageType::from(self) as u8 + 1); output.extend_from_slice(&serialize(®istration_response, 3)); output } /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { if input.is_empty() || input[0] != ProtocolMessageType::RegistrationResponse as u8 + 1 { return Err(PakeError::SerializationError.into()); } let (data, remainder) = tokenize(input[1..].to_vec(), 3)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } let (beta_bytes, remainder) = tokenize(data, 2)?; let (server_s_pk, remainder) = tokenize(remainder, 2)?; // Handle ecf deserialization let (secret_credentials, remainder) = tokenize(remainder, 1)?; let (cleartext_credentials, remainder) = tokenize(remainder, 1)?; let sc = secret_credentials .iter() .map(|x| u8_to_credential_type(*x).ok_or(PakeError::SerializationError)) .collect::, _>>()?; let cc = cleartext_credentials .iter() .map(|x| u8_to_credential_type(*x).ok_or(PakeError::SerializationError)) .collect::, _>>()?; let ecf = EnvelopeCredentialsFormat::new(sc, cc)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } let checked_slice = check_slice_size( &beta_bytes, Grp::ElemLen::to_usize(), "second_message_bytes", )?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(&checked_slice); let beta = Grp::from_element_slice(arr)?; Ok(Self { ecf, server_s_pk, beta, }) } } /// The final message from the client, containing sealed cryptographic /// identifiers pub struct RegisterThirdMessage { /// The "envelope" generated by the user, containing sealed /// cryptographic identifiers envelope: Envelope, /// The user's public key client_s_pk: KeyFormat::Repr, } impl TryFrom<&[u8]> for RegisterThirdMessage where KeyFormat: KeyPair, D: Hash, { type Error = ProtocolError; fn try_from(third_message_bytes: &[u8]) -> Result { let key_len = ::Len::to_usize(); let envelope_size = key_len + Envelope::::additional_size(); let checked_bytes = check_slice_size( third_message_bytes, envelope_size + key_len, "third_message", )?; let unchecked_client_s_pk = KeyFormat::Repr::from_bytes(&checked_bytes[envelope_size..])?; let client_s_pk = KeyFormat::check_public_key(unchecked_client_s_pk)?; Ok(Self { envelope: Envelope::::from_bytes(&checked_bytes[..envelope_size])?, client_s_pk, }) } } impl RegisterThirdMessage where KeyFormat: KeyPair, D: Hash, { /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut registration_upload: Vec = Vec::new(); registration_upload.extend_from_slice(&self.envelope.serialize()); registration_upload.extend_from_slice(&serialize(&self.client_s_pk.to_arr(), 2)); let mut output: Vec = Vec::new(); output.push(ProtocolMessageType::from(self) as u8 + 1); output.extend_from_slice(&serialize(®istration_upload, 3)); output } /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { if input.is_empty() || input[0] != ProtocolMessageType::RegistrationUpload as u8 + 1 { return Err(PakeError::SerializationError.into()); } let (data, remainder) = tokenize(input[1..].to_vec(), 3)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } let (envelope, remainder) = Envelope::::deserialize(&data)?; let (client_s_pk, remainder) = tokenize(remainder, 2)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } Ok(Self { envelope, client_s_pk: KeyFormat::check_public_key(KeyFormat::Repr::from_bytes(&client_s_pk)?)?, }) } } /// The message sent by the user to the server, to initiate registration pub struct LoginFirstMessage { /// blinded password information alpha: CS::Group, ke1_message: >::KE1Message, } impl TryFrom<&[u8]> for LoginFirstMessage { type Error = ProtocolError; fn try_from(first_message_bytes: &[u8]) -> Result { Self::deserialize(first_message_bytes) } } impl LoginFirstMessage { /// byte representation for the login request fn to_bytes(&self) -> Vec { [&self.alpha.to_arr()[..], &self.ke1_message.to_bytes()].concat() } /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut credential_request: Vec = Vec::new(); credential_request.extend_from_slice(&serialize(&self.alpha.to_arr(), 2)); let mut output: Vec = Vec::new(); output.push(ProtocolMessageType::from(self) as u8 + 1); output.extend_from_slice(&serialize(&credential_request, 3)); output.extend_from_slice(&self.ke1_message.to_bytes()); output } /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { if input.is_empty() || input[0] != ProtocolMessageType::CredentialRequest as u8 + 1 { return Err(PakeError::SerializationError.into()); } let (data, ke1m) = tokenize(input[1..].to_vec(), 3)?; let (alpha_bytes, remainder) = tokenize(data, 2)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } let elem_len = ::ElemLen::to_usize(); let checked_slice = check_slice_size(&alpha_bytes, elem_len, "login_first_message_bytes")?; let arr = GenericArray::from_slice(&checked_slice[..elem_len]); let alpha = ::from_element_slice(arr)?; let ke1_message = >::KE1Message::try_from( &ke1m[..], )?; Ok(Self { alpha, ke1_message }) } } /// The answer sent by the server to the user, upon reception of the /// login attempt. pub struct LoginSecondMessage { /// the server's oprf output beta: CS::Group, /// the user's sealed information, envelope: Envelope, ke2_message: >::KE2Message, } impl LoginSecondMessage { /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut credential_response: Vec = Vec::new(); credential_response.extend_from_slice(&serialize(&self.beta.to_arr(), 2)); credential_response.extend_from_slice(&self.envelope.to_bytes()); let mut output: Vec = Vec::new(); output.push(ProtocolMessageType::from(self) as u8 + 1); output.extend_from_slice(&serialize(&credential_response, 3)); output.extend_from_slice(&self.ke2_message.to_bytes()); output } /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { if input.is_empty() || input[0] != ProtocolMessageType::CredentialResponse as u8 + 1 { return Err(PakeError::SerializationError.into()); } let (data, ke2m) = tokenize(input[1..].to_vec(), 3)?; let (beta_bytes, envelope_bytes) = tokenize(data, 2)?; let concatenated = [&beta_bytes[..], &envelope_bytes[..], &ke2m[..]].concat(); Self::try_from(&concatenated[..]) } } impl TryFrom<&[u8]> for LoginSecondMessage { type Error = ProtocolError; fn try_from(second_message_bytes: &[u8]) -> Result { let elem_len = ::ElemLen::to_usize(); let checked_slice = check_slice_size_atleast(second_message_bytes, elem_len, "login_second_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup let beta_bytes = &checked_slice[..elem_len]; let arr = GenericArray::from_slice(beta_bytes); let beta = CS::Group::from_element_slice(arr)?; let (envelope, remainder) = Envelope::::deserialize(&checked_slice[elem_len..])?; let ke2_message_size = CS::KeyExchange::ke2_message_size(); let checked_remainder = check_slice_size_atleast(&remainder, ke2_message_size, "login_second_message_bytes")?; let ke2_message = >::KE2Message::try_from( &checked_remainder, )?; Ok(Self { beta, envelope, ke2_message, }) } } /// The answer sent by the client to the server, upon reception of the /// sealed envelope pub struct LoginThirdMessage { ke3_message: >::KE3Message, } impl TryFrom<&[u8]> for LoginThirdMessage { type Error = ProtocolError; fn try_from(bytes: &[u8]) -> Result { let ke3_message = >::KE3Message::try_from(bytes)?; Ok(Self { ke3_message }) } } impl LoginThirdMessage { /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut output: Vec = Vec::new(); output.push(ProtocolMessageType::from(self) as u8 + 1); output.extend_from_slice(&self.ke3_message.to_bytes()); output } /// byte representation for the login finalization pub fn to_bytes(&self) -> Vec { self.ke3_message.to_bytes() } } // Registration // ============ /// The state elements the client holds to register itself pub struct ClientRegistration { /// User identity id_u: Vec, /// Server identity id_s: Vec, /// token containing the client's password and the blinding factor pub(crate) token: oprf::Token, } impl TryFrom<&[u8]> for ClientRegistration { type Error = ProtocolError; fn try_from(input: &[u8]) -> Result { let (id_u, bytes) = tokenize(input.to_vec(), 2)?; let (id_s, bytes) = tokenize(bytes.to_vec(), 2)?; let min_expected_len = ::ScalarLen::to_usize(); let checked_slice = (if bytes.len() <= min_expected_len { Err(InternalPakeError::SizeError { name: "client_registration_bytes", len: min_expected_len, actual_len: bytes.len(), }) } else { Ok(bytes) })?; // Check that the message is actually containing an element of the // correct subgroup let scalar_len = min_expected_len; let blinding_factor_bytes = GenericArray::from_slice(&checked_slice[..scalar_len]); let blinding_factor = CS::Group::from_scalar_slice(blinding_factor_bytes)?; let password = checked_slice[scalar_len..].to_vec(); Ok(Self { id_u, id_s, token: oprf::Token { data: password, blind: blinding_factor, }, }) } } impl ClientRegistration { /// byte representation for the client's registration state pub fn to_bytes(&self) -> Vec { let output: Vec = [ &serialize(&self.id_u, 2), &serialize(&self.id_s, 2), &CS::Group::scalar_as_bytes(&self.token.blind)[..], &self.token.data, ] .concat(); output } } impl ClientRegistration { /// Returns an initial "blinded" request to send to the server, as well as a ClientRegistration /// /// # Arguments /// * `password` - A user password /// /// # Example /// /// ``` /// use opaque_ke::opaque::ClientRegistration; /// # use opaque_ke::errors::ProtocolError; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut rng = OsRng; /// let (register_m1, registration_state) = ClientRegistration::::start(b"hunter2", &mut rng)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn start( password: &[u8], blinding_factor_rng: &mut R, ) -> Result<(RegisterFirstMessage, Self), ProtocolError> { Self::start_with_user_and_server_name( &Vec::new(), &Vec::new(), password, blinding_factor_rng, #[cfg(test)] std::convert::identity, ) } /// Same as ClientRegistration::start, but also accepts a username and /// server name as input /// as well as an optional postprocessing function for the blinding factor(used in tests) pub fn start_with_user_and_server_name( user_name: &[u8], server_name: &[u8], password: &[u8], blinding_factor_rng: &mut R, #[cfg(test)] postprocess: fn(::Scalar) -> ::Scalar, ) -> Result<(RegisterFirstMessage, Self), ProtocolError> { let (token, alpha) = oprf::blind::( &password, blinding_factor_rng, #[cfg(test)] postprocess, )?; Ok(( RegisterFirstMessage:: { alpha }, Self { id_u: user_name.to_vec(), id_s: server_name.to_vec(), token, }, )) } } type ClientRegistrationFinishResult = ( RegisterThirdMessage, GenericArray, ); impl ClientRegistration { /// "Unblinds" the server's answer and returns a final message containing /// cryptographic identifiers, to be sent to the server on setup finalization /// /// # Arguments /// * `message` - the server's answer to the initial registration attempt /// /// # Example /// /// ``` /// use opaque_ke::{opaque::{ClientRegistration, ServerRegistration}, keypair::X25519KeyPair}; /// # use opaque_ke::errors::ProtocolError; /// # use opaque_ke::keypair::KeyPair; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// let mut server_rng = OsRng; /// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?; /// let (register_m1, client_state) = ClientRegistration::::start(b"hunter2", &mut client_rng)?; /// let (register_m2, server_state) = /// ServerRegistration::::start(register_m1, &mut server_rng)?; /// let mut client_rng = OsRng; /// let register_m3 = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn finish( self, r2: RegisterSecondMessage, server_s_pk: &::Repr, rng: &mut R, ) -> Result, ProtocolError> { let mut r2_cloned = r2; r2_cloned.server_s_pk = server_s_pk.to_arr().to_vec(); self.finish_using_transmitted_server_public_key(r2_cloned, rng) } /// Same as finish, but without the server public key check pub fn finish_using_transmitted_server_public_key( self, r2: RegisterSecondMessage, rng: &mut R, ) -> Result, ProtocolError> { let client_static_keypair = CS::KeyFormat::generate_random(rng)?; let password_derived_key = get_password_derived_key::(&self.token, r2.beta)?; let mut credentials_map: HashMap> = HashMap::new(); credentials_map.insert( CredentialType::SkU, client_static_keypair.private().to_arr().to_vec(), ); credentials_map.insert( CredentialType::PkU, client_static_keypair.public().to_arr().to_vec(), ); credentials_map.insert(CredentialType::PkS, r2.server_s_pk); credentials_map.insert(CredentialType::IdU, self.id_u.clone()); credentials_map.insert(CredentialType::IdS, self.id_s.clone()); let (envelope, export_key) = Envelope::::seal(&password_derived_key, r2.ecf, credentials_map, rng)?; Ok(( RegisterThirdMessage { envelope, client_s_pk: client_static_keypair.public().clone(), }, export_key, )) } } // This can't be derived because of the use of a phantom parameter impl Zeroize for ClientRegistration { fn zeroize(&mut self) { self.token.data.zeroize(); self.token.blind.zeroize(); } } impl Drop for ClientRegistration { fn drop(&mut self) { self.zeroize(); } } // This can't be derived because of the use of a phantom parameter impl Zeroize for ClientLogin { fn zeroize(&mut self) { self.token.data.zeroize(); self.token.blind.zeroize(); } } impl Drop for ClientLogin { fn drop(&mut self) { self.zeroize(); } } /// The state elements the server holds to record a registration pub struct ServerRegistration { envelope: Option>, client_s_pk: Option<::Repr>, pub(crate) oprf_key: ::Scalar, } impl TryFrom<&[u8]> for ServerRegistration where <::Repr as SizedBytes>::Len: std::ops::Add<<::Repr as SizedBytes>::Len>, generic_array::typenum::Sum< <::Repr as SizedBytes>::Len, <::Repr as SizedBytes>::Len, >: generic_array::ArrayLength, { type Error = ProtocolError; /// The format of a serialized ServerRegistration object: /// oprf_key | client_s_pk | envelope fn try_from(input: &[u8]) -> Result { let scalar_len = ::ScalarLen::to_usize(); if input.len() == scalar_len { return Ok(Self { oprf_key: CS::Group::from_scalar_slice(GenericArray::from_slice(input))?, client_s_pk: None, envelope: None, }); } // Need to do this check manually because envelope is variable-size let key_len = <::Repr as SizedBytes>::Len::to_usize(); let checked_bytes = check_slice_size_atleast(&input, scalar_len + key_len, "server_registration_bytes")?; let oprf_key_bytes = GenericArray::from_slice(&checked_bytes[..scalar_len]); let oprf_key = CS::Group::from_scalar_slice(oprf_key_bytes)?; let unchecked_client_s_pk = ::Repr::from_bytes( &checked_bytes[scalar_len..scalar_len + key_len], )?; let client_s_pk = CS::KeyFormat::check_public_key(unchecked_client_s_pk)?; let envelope = Envelope::::from_bytes(&checked_bytes[scalar_len + key_len..])?; Ok(Self { envelope: Some(envelope), client_s_pk: Some(client_s_pk), oprf_key, }) } } impl ServerRegistration where <::Repr as SizedBytes>::Len: std::ops::Add<<::Repr as SizedBytes>::Len>, generic_array::typenum::Sum< <::Repr as SizedBytes>::Len, <::Repr as SizedBytes>::Len, >: generic_array::ArrayLength, { /// byte representation for the server's registration state pub fn to_bytes(&self) -> Vec { let mut output: Vec = CS::Group::scalar_as_bytes(&self.oprf_key).to_vec(); self.client_s_pk .iter() .for_each(|v| output.extend_from_slice(&v.to_arr())); self.envelope .iter() .for_each(|v| output.extend_from_slice(&v.to_bytes())); output } /// From the client's "blinded" password, returns a response to be /// sent back to the client, as well as a ServerRegistration /// /// # Arguments /// * `message` - the initial registration message /// /// # Example /// /// ``` /// use opaque_ke::{opaque::*, keypair::X25519KeyPair}; /// # use opaque_ke::errors::ProtocolError; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// let mut server_rng = OsRng; /// let (register_m1, client_state) = ClientRegistration::::start(b"hunter2", &mut client_rng)?; /// let (register_m2, server_state) = /// ServerRegistration::::start(register_m1, &mut server_rng)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn start( message: RegisterFirstMessage, rng: &mut R, ) -> Result<(RegisterSecondMessage, Self), ProtocolError> { Self::start_with_server_pk(message, &Vec::new(), rng) } /// Same as start, but with the ability to supply a server_s_pk as input pub fn start_with_server_pk( message: RegisterFirstMessage, server_s_pk: &[u8], rng: &mut R, ) -> Result<(RegisterSecondMessage, Self), ProtocolError> { Self::start_with_server_pk_and_ecf( message, server_s_pk, EnvelopeCredentialsFormat::default()?, rng, ) } /// Same as start, but with the ability to supply a server_s_pk as input and envelope credentials format pub fn start_with_server_pk_and_ecf( message: RegisterFirstMessage, server_s_pk: &[u8], ecf: EnvelopeCredentialsFormat, rng: &mut R, ) -> Result<(RegisterSecondMessage, Self), ProtocolError> { // RFC: generate oprf_key (salt) and v_u = g^oprf_key let oprf_key = CS::Group::random_scalar(rng); // Compute beta = alpha^oprf_key let beta = oprf::evaluate::(message.alpha, &oprf_key)?; Ok(( RegisterSecondMessage { beta, server_s_pk: server_s_pk.to_vec(), ecf, }, Self { envelope: None, client_s_pk: None, oprf_key, }, )) } /// From the client's cryptographic identifiers, fully populates and /// returns a ServerRegistration /// /// # Arguments /// * `message` - the final client message /// /// # Example /// /// ``` /// use opaque_ke::{opaque::*, keypair::{KeyPair, X25519KeyPair}}; /// # use opaque_ke::errors::ProtocolError; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// let mut server_rng = OsRng; /// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?; /// let (register_m1, client_state) = ClientRegistration::::start(b"hunter2", &mut client_rng)?; /// let (register_m2, server_state) = /// ServerRegistration::::start(register_m1, &mut server_rng)?; /// let mut client_rng = OsRng; /// let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?; /// let client_record = server_state.finish(register_m3)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn finish( self, message: RegisterThirdMessage, ) -> Result { Ok(Self { envelope: Some(message.envelope), client_s_pk: Some(message.client_s_pk), oprf_key: self.oprf_key, }) } } // Login // ===== /// The state elements the client holds to perform a login pub struct ClientLogin { /// User identity id_u: Vec, /// Server identity id_s: Vec, /// token containing the client's password and the blinding factor token: oprf::Token, ke1_state: >::KE1State, } impl TryFrom<&[u8]> for ClientLogin { type Error = ProtocolError; fn try_from(input: &[u8]) -> Result { let (id_u, bytes) = tokenize(input.to_vec(), 2)?; let (id_s, bytes) = tokenize(bytes.to_vec(), 2)?; let scalar_len = ::ScalarLen::to_usize(); let ke1_state_size = >::ke1_state_size(); let min_expected_len = scalar_len + ke1_state_size; let checked_slice = (if bytes.len() <= min_expected_len { Err(InternalPakeError::SizeError { name: "client_login_bytes", len: min_expected_len, actual_len: bytes.len(), }) } else { Ok(bytes.clone()) })?; let blinding_factor_bytes = GenericArray::from_slice(&checked_slice[..scalar_len]); let blinding_factor = CS::Group::from_scalar_slice(blinding_factor_bytes)?; let ke1_state = >::KE1State::try_from( &checked_slice[scalar_len..scalar_len + ke1_state_size], )?; let password = bytes[scalar_len + ke1_state_size..].to_vec(); Ok(Self { id_u, id_s, token: oprf::Token { data: password, blind: blinding_factor, }, ke1_state, }) } } impl ClientLogin { /// byte representation for the client's login state pub fn to_bytes(&self) -> Vec { let output: Vec = [ &serialize(&self.id_u, 2), &serialize(&self.id_s, 2), &CS::Group::scalar_as_bytes(&self.token.blind)[..], &self.ke1_state.to_bytes(), &self.token.data, ] .concat(); output } } type ClientLoginFinishResult = ( LoginThirdMessage, Vec, GenericArray, ); impl ClientLogin { /// Returns an initial "blinded" password request to send to the server, as well as a ClientLogin /// /// # Arguments /// * `password` - A user password /// /// # Example /// /// ``` /// use opaque_ke::opaque::ClientLogin; /// # use opaque_ke::errors::ProtocolError; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// let (login_m1, client_login_state) = ClientLogin::::start(b"hunter2", &mut client_rng)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn start( password: &[u8], rng: &mut R, ) -> Result<(LoginFirstMessage, Self), ProtocolError> { Self::start_with_user_and_server_name( &Vec::new(), &Vec::new(), password, rng, #[cfg(test)] std::convert::identity, ) } /// Same as start, but allows the user to supply a username and server name /// and, in tests, a postprocessing function pub fn start_with_user_and_server_name( user_name: &[u8], server_name: &[u8], password: &[u8], rng: &mut R, #[cfg(test)] postprocess: fn(::Scalar) -> ::Scalar, ) -> Result<(LoginFirstMessage, Self), ProtocolError> { let (token, alpha) = oprf::blind::( &password, rng, #[cfg(test)] postprocess, )?; let (ke1_state, ke1_message) = CS::KeyExchange::generate_ke1(alpha.to_arr().to_vec(), rng)?; let l1 = LoginFirstMessage { alpha, ke1_message }; Ok(( l1, Self { id_u: user_name.to_vec(), id_s: server_name.to_vec(), token, ke1_state, }, )) } /// "Unblinds" the server's answer and returns the opened assets from /// the server /// /// # Arguments /// * `message` - the server's answer to the initial login attempt /// /// # Example /// /// ``` /// use opaque_ke::opaque::{ClientLogin, ServerLogin}; /// # use opaque_ke::opaque::{ClientRegistration, ServerRegistration}; /// # use opaque_ke::errors::ProtocolError; /// # use opaque_ke::keypair::{X25519KeyPair, KeyPair}; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// # let mut server_rng = OsRng; /// # let (register_m1, client_state) = ClientRegistration::::start(b"hunter2", &mut client_rng)?; /// # let server_kp = X25519KeyPair::generate_random(&mut server_rng)?; /// # let (register_m2, server_state) = ServerRegistration::::start(register_m1, &mut server_rng)?; /// # let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?; /// # let p_file = server_state.finish(register_m3)?; /// let (login_m1, client_login_state) = ClientLogin::::start(b"hunter2", &mut client_rng)?; /// let (login_m2, server_login_state) = ServerLogin::start(p_file, &server_kp.private(), login_m1, &mut server_rng)?; /// let (login_m3, client_transport, _export_key) = client_login_state.finish(login_m2, &server_kp.public(), &mut client_rng)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn finish( self, l2: LoginSecondMessage, _server_s_pk: &<::KeyFormat as KeyPair>::Repr, _client_e_sk_rng: &mut R, ) -> Result, ProtocolError> { let l2_bytes: Vec = [&l2.beta.to_arr()[..], &l2.envelope.to_bytes()].concat(); let password_derived_key = get_password_derived_key::(&self.token, l2.beta)?; let opened_envelope = &l2 .envelope .open(&password_derived_key) .map_err(|e| match e { InternalPakeError::SealOpenHmacError => PakeError::InvalidLoginError, err => PakeError::from(err), })?; let (shared_secret, ke3_message) = CS::KeyExchange::generate_ke3( l2_bytes, l2.ke2_message, &self.ke1_state, ::Repr::from_bytes( &opened_envelope.credentials_map[&CredentialType::PkS], )?, ::Repr::from_bytes( &opened_envelope.credentials_map[&CredentialType::SkU], )?, )?; Ok(( LoginThirdMessage { ke3_message }, shared_secret, opened_envelope.export_key, )) } } /// The state elements the server holds to record a login pub struct ServerLogin { ke2_state: >::KE2State, _cs: PhantomData, } impl TryFrom<&[u8]> for ServerLogin { type Error = ProtocolError; fn try_from(bytes: &[u8]) -> Result { Ok(Self { _cs: PhantomData, ke2_state: >::KE2State::try_from( bytes, )?, }) } } type ServerLoginStartResult = (LoginSecondMessage, ServerLogin); impl ServerLogin { /// byte representation for the server's login state pub fn to_bytes(&self) -> Vec { self.ke2_state.to_bytes() } /// From the client's "blinded"" password, returns a challenge to be /// sent back to the client, as well as a ServerLogin /// /// # Arguments /// * `message` - the initial registration message /// /// # Example /// /// ``` /// use opaque_ke::opaque::{ClientLogin, ServerLogin}; /// # use opaque_ke::opaque::{ClientRegistration, ServerRegistration}; /// # use opaque_ke::errors::ProtocolError; /// # use opaque_ke::keypair::{KeyPair, X25519KeyPair}; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// let mut server_rng = OsRng; /// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?; /// # let (register_m1, client_state) = ClientRegistration::::start(b"hunter2", &mut client_rng)?; /// # let (register_m2, server_state) = /// ServerRegistration::::start(register_m1, &mut server_rng)?; /// # let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?; /// # let p_file = server_state.finish(register_m3)?; /// let (login_m1, client_login_state) = ClientLogin::::start(b"hunter2", &mut client_rng)?; /// let (login_m2, server_login_state) = ServerLogin::start(p_file, &server_kp.private(), login_m1, &mut server_rng)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn start( password_file: ServerRegistration, server_s_sk: &::Repr, l1: LoginFirstMessage, rng: &mut R, ) -> Result, ProtocolError> { let l1_bytes = &l1.to_bytes(); let beta = oprf::evaluate(l1.alpha, &password_file.oprf_key)?; let client_s_pk = password_file .client_s_pk .ok_or(InternalPakeError::SealError)?; let envelope = password_file.envelope.ok_or(InternalPakeError::SealError)?; let l2_component: Vec = [&beta.to_arr()[..], &envelope.to_bytes()].concat(); let (ke2_state, ke2_message) = CS::KeyExchange::generate_ke2( rng, l1_bytes.to_vec(), l2_component, l1.ke1_message, client_s_pk, server_s_sk.clone(), )?; let l2 = LoginSecondMessage { beta, envelope, ke2_message, }; Ok(( l2, Self { _cs: PhantomData, ke2_state, }, )) } /// From the client's second & final message, check the client's /// authentication & produce a message transport /// /// # Arguments /// * `message` - the client's second login message /// /// # Example /// /// ``` /// use opaque_ke::opaque::{ClientLogin, ServerLogin}; /// # use opaque_ke::opaque::{ClientRegistration, ServerRegistration}; /// # use opaque_ke::errors::ProtocolError; /// # use opaque_ke::keypair::{KeyPair, X25519KeyPair}; /// use rand_core::{OsRng, RngCore}; /// use opaque_ke::ciphersuite::CipherSuite; /// struct Default; /// impl CipherSuite for Default { /// type Group = curve25519_dalek::ristretto::RistrettoPoint; /// type KeyFormat = opaque_ke::keypair::X25519KeyPair; /// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH; /// type Hash = sha2::Sha256; /// type SlowHash = opaque_ke::slow_hash::NoOpHash; /// } /// let mut client_rng = OsRng; /// let mut server_rng = OsRng; /// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?; /// # let (register_m1, client_state) = ClientRegistration::::start(b"hunter2", &mut client_rng)?; /// # let (register_m2, server_state) = /// ServerRegistration::::start(register_m1, &mut server_rng)?; /// # let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?; /// # let p_file = server_state.finish(register_m3)?; /// let (login_m1, client_login_state) = ClientLogin::::start(b"hunter2", &mut client_rng)?; /// let (login_m2, server_login_state) = ServerLogin::start(p_file, &server_kp.private(), login_m1, &mut server_rng)?; /// let (login_m3, client_transport, _export_key) = client_login_state.finish(login_m2, &server_kp.public(), &mut client_rng)?; /// let mut server_transport = server_login_state.finish(login_m3)?; /// # Ok::<(), ProtocolError>(()) /// ``` pub fn finish(&self, message: LoginThirdMessage) -> Result, ProtocolError> { >::finish_ke( message.ke3_message, &self.ke2_state, ) .map_err(|e| match e { ProtocolError::VerificationError(PakeError::KeyExchangeMacValidationError) => { ProtocolError::VerificationError(PakeError::InvalidLoginError) } err => err, }) } } // Helper functions fn get_password_derived_key, D: Hash>( token: &oprf::Token, beta: G, ) -> Result, InternalPakeError> { let oprf_output = oprf::finalize::( &token.data, &oprf::unblind::(token, beta), STR_OPAQUE_VERSION, ); SH::hash(oprf_output) }