2020-06-05 09:35:14 -07: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.
|
|
|
|
|
|
|
|
|
|
//! Provides the main OPAQUE API
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2020-06-14 23:25:31 -07:00
|
|
|
ciphersuite::CipherSuite,
|
2020-10-09 10:51:58 -07:00
|
|
|
envelope::{Envelope, EnvelopeCredentialsFormat, ExportKeySize},
|
2020-11-16 14:05:43 -08:00
|
|
|
errors::{utils::check_slice_size_atleast, InternalPakeError, PakeError, ProtocolError},
|
2020-06-05 09:35:14 -07:00
|
|
|
group::Group,
|
2020-07-27 15:25:04 -07:00
|
|
|
hash::Hash,
|
2020-07-13 15:23:29 -07:00
|
|
|
key_exchange::traits::{KeyExchange, ToBytes},
|
2020-11-03 21:44:00 +00:00
|
|
|
keypair::{KeyPair, SizedBytesExt},
|
2020-11-12 10:00:09 -08:00
|
|
|
map_to_curve::GroupWithMapToCurve,
|
2020-06-05 09:35:14 -07:00
|
|
|
oprf,
|
2020-11-16 14:05:43 -08:00
|
|
|
serialization::{serialize, tokenize, CredentialType},
|
2020-06-08 21:02:01 -07:00
|
|
|
slow_hash::SlowHash,
|
2020-11-16 14:05:43 -08:00
|
|
|
LoginFirstMessage, LoginSecondMessage, LoginThirdMessage, RegisterFirstMessage,
|
|
|
|
|
RegisterSecondMessage, RegisterThirdMessage,
|
2020-06-05 09:35:14 -07:00
|
|
|
};
|
2020-07-02 12:24:53 -07:00
|
|
|
use generic_array::{typenum::Unsigned, GenericArray};
|
2020-11-03 21:44:00 +00:00
|
|
|
use generic_bytes::SizedBytes;
|
2020-06-05 09:35:14 -07:00
|
|
|
use rand_core::{CryptoRng, RngCore};
|
2020-10-09 10:51:58 -07:00
|
|
|
use std::collections::HashMap;
|
2020-06-05 09:35:14 -07:00
|
|
|
use std::{convert::TryFrom, marker::PhantomData};
|
|
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
|
2020-11-12 10:00:09 -08:00
|
|
|
static STR_OPAQUE_VERSION: &[u8] = b"OPAQUE00";
|
|
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
// Registration
|
|
|
|
|
// ============
|
|
|
|
|
|
|
|
|
|
/// The state elements the client holds to register itself
|
2020-06-14 23:25:31 -07:00
|
|
|
pub struct ClientRegistration<CS: CipherSuite> {
|
2020-10-09 10:51:58 -07:00
|
|
|
/// User identity
|
|
|
|
|
id_u: Vec<u8>,
|
|
|
|
|
/// Server identity
|
|
|
|
|
id_s: Vec<u8>,
|
2020-11-02 13:51:43 -08:00
|
|
|
/// token containing the client's password and the blinding factor
|
|
|
|
|
pub(crate) token: oprf::Token<CS::Group>,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for ClientRegistration<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
type Error = ProtocolError;
|
2020-10-09 10:51:58 -07:00
|
|
|
fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
|
2020-11-16 11:49:27 -08:00
|
|
|
let (id_u, bytes) = tokenize(&input, 2)?;
|
|
|
|
|
let (id_s, bytes) = tokenize(&bytes, 2)?;
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2020-09-02 10:29:22 -04:00
|
|
|
let min_expected_len = <CS::Group as Group>::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)
|
|
|
|
|
})?;
|
|
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
// Check that the message is actually containing an element of the
|
|
|
|
|
// correct subgroup
|
2020-09-02 10:29:22 -04:00
|
|
|
let scalar_len = min_expected_len;
|
|
|
|
|
let blinding_factor_bytes = GenericArray::from_slice(&checked_slice[..scalar_len]);
|
2020-06-14 23:25:31 -07:00
|
|
|
let blinding_factor = CS::Group::from_scalar_slice(blinding_factor_bytes)?;
|
2020-09-02 10:29:22 -04:00
|
|
|
let password = checked_slice[scalar_len..].to_vec();
|
2020-06-05 09:35:14 -07:00
|
|
|
Ok(Self {
|
2020-10-09 10:51:58 -07:00
|
|
|
id_u,
|
|
|
|
|
id_s,
|
2020-11-02 13:51:43 -08:00
|
|
|
token: oprf::Token {
|
|
|
|
|
data: password,
|
|
|
|
|
blind: blinding_factor,
|
|
|
|
|
},
|
2020-06-05 09:35:14 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> ClientRegistration<CS> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the client's registration state
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
|
let output: Vec<u8> = [
|
2020-10-09 10:51:58 -07:00
|
|
|
&serialize(&self.id_u, 2),
|
|
|
|
|
&serialize(&self.id_s, 2),
|
2020-11-02 13:51:43 -08:00
|
|
|
&CS::Group::scalar_as_bytes(&self.token.blind)[..],
|
|
|
|
|
&self.token.data,
|
2020-06-05 09:35:14 -07:00
|
|
|
]
|
|
|
|
|
.concat();
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
/// Optional parameters for client registration start
|
|
|
|
|
pub enum ClientRegistrationStartParameters {
|
|
|
|
|
/// Specifying the identifiers idU and idS
|
|
|
|
|
WithIdentifiers(Vec<u8>, Vec<u8>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ClientRegistrationStartParameters {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::WithIdentifiers(Vec::new(), Vec::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> ClientRegistration<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
/// Returns an initial "blinded" request to send to the server, as well as a ClientRegistration
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * `password` - A user password
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{ClientRegistration, ClientRegistrationStartParameters};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// let (register_m1, registration_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn start<R: RngCore + CryptoRng>(
|
|
|
|
|
password: &[u8],
|
2020-11-16 14:05:43 -08:00
|
|
|
params: ClientRegistrationStartParameters,
|
2020-11-02 13:51:43 -08:00
|
|
|
blinding_factor_rng: &mut R,
|
2020-11-04 11:33:58 -05:00
|
|
|
#[cfg(test)] postprocess: fn(<CS::Group as Group>::Scalar) -> <CS::Group as Group>::Scalar,
|
2020-11-02 13:51:43 -08:00
|
|
|
) -> Result<(RegisterFirstMessage<CS::Group>, Self), ProtocolError> {
|
2020-11-16 14:05:43 -08:00
|
|
|
let (id_u, id_s) = match params {
|
|
|
|
|
ClientRegistrationStartParameters::WithIdentifiers(id_u, id_s) => (id_u, id_s),
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-04 11:33:58 -05:00
|
|
|
let (token, alpha) = oprf::blind::<R, CS::Group>(
|
2020-11-02 13:51:43 -08:00
|
|
|
&password,
|
|
|
|
|
blinding_factor_rng,
|
2020-11-04 11:33:58 -05:00
|
|
|
#[cfg(test)]
|
2020-11-02 13:51:43 -08:00
|
|
|
postprocess,
|
|
|
|
|
)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
Ok((
|
2020-11-06 10:06:49 -08:00
|
|
|
RegisterFirstMessage::<CS::Group> { alpha },
|
2020-11-16 14:05:43 -08:00
|
|
|
Self { id_u, id_s, token },
|
2020-06-05 09:35:14 -07:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 15:25:04 -07:00
|
|
|
type ClientRegistrationFinishResult<KeyFormat, D> = (
|
|
|
|
|
RegisterThirdMessage<KeyFormat, D>,
|
2020-07-02 12:24:53 -07:00
|
|
|
GenericArray<u8, ExportKeySize>,
|
2020-06-05 09:35:14 -07:00
|
|
|
);
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> ClientRegistration<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
/// "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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{ClientRegistration, ClientRegistrationStartParameters, ServerRegistration, keypair::X25519KeyPair};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// # use opaque_ke::keypair::KeyPair;
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// let mut server_rng = OsRng;
|
|
|
|
|
/// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (register_m2, server_state) =
|
2020-11-16 14:05:43 -08:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, server_kp.public(), &mut server_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let register_m3 = client_state.finish(register_m2, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
2020-06-14 23:25:31 -07:00
|
|
|
pub fn finish<R: CryptoRng + RngCore>(
|
2020-10-09 10:51:58 -07:00
|
|
|
self,
|
|
|
|
|
r2: RegisterSecondMessage<CS::Group>,
|
|
|
|
|
rng: &mut R,
|
2020-07-27 15:25:04 -07:00
|
|
|
) -> Result<ClientRegistrationFinishResult<CS::KeyFormat, CS::Hash>, ProtocolError> {
|
2020-06-14 23:25:31 -07:00
|
|
|
let client_static_keypair = CS::KeyFormat::generate_random(rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-02 13:51:43 -08:00
|
|
|
let password_derived_key =
|
|
|
|
|
get_password_derived_key::<CS::Group, CS::SlowHash, CS::Hash>(&self.token, r2.beta)?;
|
2020-07-02 12:24:53 -07:00
|
|
|
|
2020-10-09 10:51:58 -07:00
|
|
|
let mut credentials_map: HashMap<CredentialType, Vec<u8>> = 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::<CS::Hash>::seal(&password_derived_key, r2.ecf, credentials_map, rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
RegisterThirdMessage {
|
|
|
|
|
envelope,
|
|
|
|
|
client_s_pk: client_static_keypair.public().clone(),
|
|
|
|
|
},
|
2020-07-02 12:24:53 -07:00
|
|
|
export_key,
|
2020-06-05 09:35:14 -07:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This can't be derived because of the use of a phantom parameter
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> Zeroize for ClientRegistration<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
fn zeroize(&mut self) {
|
2020-11-02 13:51:43 -08:00
|
|
|
self.token.data.zeroize();
|
|
|
|
|
self.token.blind.zeroize();
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> Drop for ClientRegistration<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.zeroize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This can't be derived because of the use of a phantom parameter
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> Zeroize for ClientLogin<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
fn zeroize(&mut self) {
|
2020-11-02 13:51:43 -08:00
|
|
|
self.token.data.zeroize();
|
|
|
|
|
self.token.blind.zeroize();
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> Drop for ClientLogin<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.zeroize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The state elements the server holds to record a registration
|
2020-06-14 23:25:31 -07:00
|
|
|
pub struct ServerRegistration<CS: CipherSuite> {
|
2020-07-27 15:25:04 -07:00
|
|
|
envelope: Option<Envelope<CS::Hash>>,
|
2020-07-03 15:58:42 -04:00
|
|
|
client_s_pk: Option<<CS::KeyFormat as KeyPair>::Repr>,
|
|
|
|
|
pub(crate) oprf_key: <CS::Group as Group>::Scalar,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for ServerRegistration<CS>
|
2020-06-05 09:35:14 -07:00
|
|
|
where
|
2020-07-03 15:58:42 -04:00
|
|
|
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len:
|
|
|
|
|
std::ops::Add<<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>,
|
2020-06-05 09:35:14 -07:00
|
|
|
generic_array::typenum::Sum<
|
2020-07-03 15:58:42 -04:00
|
|
|
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
|
|
|
|
|
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
|
2020-06-05 09:35:14 -07:00
|
|
|
>: generic_array::ArrayLength<u8>,
|
|
|
|
|
{
|
|
|
|
|
type Error = ProtocolError;
|
|
|
|
|
|
2020-10-09 10:51:58 -07:00
|
|
|
/// The format of a serialized ServerRegistration object:
|
|
|
|
|
/// oprf_key | client_s_pk | envelope
|
|
|
|
|
fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
|
|
|
|
|
let scalar_len = <CS::Group as Group>::ScalarLen::to_usize();
|
|
|
|
|
if input.len() == scalar_len {
|
2020-06-05 09:35:14 -07:00
|
|
|
return Ok(Self {
|
2020-10-09 10:51:58 -07:00
|
|
|
oprf_key: CS::Group::from_scalar_slice(GenericArray::from_slice(input))?,
|
2020-06-05 09:35:14 -07:00
|
|
|
client_s_pk: None,
|
|
|
|
|
envelope: None,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 10:51:58 -07:00
|
|
|
// Need to do this check manually because envelope is variable-size
|
|
|
|
|
let key_len = <<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len::to_usize();
|
|
|
|
|
|
|
|
|
|
let checked_bytes =
|
|
|
|
|
check_slice_size_atleast(&input, scalar_len + key_len, "server_registration_bytes")?;
|
|
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
let oprf_key_bytes = GenericArray::from_slice(&checked_bytes[..scalar_len]);
|
2020-06-14 23:25:31 -07:00
|
|
|
let oprf_key = CS::Group::from_scalar_slice(oprf_key_bytes)?;
|
2020-07-03 15:58:42 -04:00
|
|
|
let unchecked_client_s_pk = <CS::KeyFormat as KeyPair>::Repr::from_bytes(
|
2020-06-14 23:25:31 -07:00
|
|
|
&checked_bytes[scalar_len..scalar_len + key_len],
|
|
|
|
|
)?;
|
|
|
|
|
let client_s_pk = CS::KeyFormat::check_public_key(unchecked_client_s_pk)?;
|
2020-10-09 10:51:58 -07:00
|
|
|
|
|
|
|
|
let envelope = Envelope::<CS::Hash>::from_bytes(&checked_bytes[scalar_len + key_len..])?;
|
|
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
Ok(Self {
|
2020-10-09 10:51:58 -07:00
|
|
|
envelope: Some(envelope),
|
2020-06-05 09:35:14 -07:00
|
|
|
client_s_pk: Some(client_s_pk),
|
|
|
|
|
oprf_key,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> ServerRegistration<CS>
|
2020-06-05 09:35:14 -07:00
|
|
|
where
|
2020-07-03 15:58:42 -04:00
|
|
|
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len:
|
|
|
|
|
std::ops::Add<<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>,
|
2020-06-05 09:35:14 -07:00
|
|
|
generic_array::typenum::Sum<
|
2020-07-03 15:58:42 -04:00
|
|
|
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
|
|
|
|
|
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
|
2020-06-05 09:35:14 -07:00
|
|
|
>: generic_array::ArrayLength<u8>,
|
|
|
|
|
{
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the server's registration state
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
2020-06-14 23:25:31 -07:00
|
|
|
let mut output: Vec<u8> = CS::Group::scalar_as_bytes(&self.oprf_key).to_vec();
|
2020-09-19 19:16:30 -04:00
|
|
|
self.client_s_pk
|
|
|
|
|
.iter()
|
2020-09-22 08:50:39 -04:00
|
|
|
.for_each(|v| output.extend_from_slice(&v.to_arr()));
|
2020-09-19 19:16:30 -04:00
|
|
|
self.envelope
|
|
|
|
|
.iter()
|
|
|
|
|
.for_each(|v| output.extend_from_slice(&v.to_bytes()));
|
2020-06-05 09:35:14 -07:00
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{*, keypair::{KeyPair, X25519KeyPair}};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// let mut server_rng = OsRng;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
|
|
|
|
/// let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (register_m2, server_state) =
|
2020-11-16 14:05:43 -08:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, server_kp.public(), &mut server_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn start<R: RngCore + CryptoRng>(
|
2020-06-14 23:25:31 -07:00
|
|
|
message: RegisterFirstMessage<CS::Group>,
|
2020-11-16 14:05:43 -08:00
|
|
|
server_s_pk: &<CS::KeyFormat as KeyPair>::Repr,
|
2020-10-09 10:51:58 -07:00
|
|
|
rng: &mut R,
|
2020-06-14 23:25:31 -07:00
|
|
|
) -> Result<(RegisterSecondMessage<CS::Group>, Self), ProtocolError> {
|
2020-06-05 09:35:14 -07:00
|
|
|
// RFC: generate oprf_key (salt) and v_u = g^oprf_key
|
2020-06-14 23:25:31 -07:00
|
|
|
let oprf_key = CS::Group::random_scalar(rng);
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
// Compute beta = alpha^oprf_key
|
2020-11-29 23:06:55 -08:00
|
|
|
let beta = oprf::evaluate::<CS::Group>(message.alpha, &oprf_key);
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
Ok((
|
2020-10-09 10:51:58 -07:00
|
|
|
RegisterSecondMessage {
|
|
|
|
|
beta,
|
2020-11-16 14:05:43 -08:00
|
|
|
server_s_pk: server_s_pk.to_arr().to_vec(),
|
|
|
|
|
ecf: EnvelopeCredentialsFormat::default()?,
|
2020-10-09 10:51:58 -07:00
|
|
|
},
|
2020-06-05 09:35:14 -07:00
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{*, keypair::{KeyPair, X25519KeyPair}};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// let mut server_rng = OsRng;
|
|
|
|
|
/// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (register_m2, server_state) =
|
2020-11-16 14:05:43 -08:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, server_kp.public(), &mut server_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let (register_m3, _export_key) = client_state.finish(register_m2, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let client_record = server_state.finish(register_m3)?;
|
|
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn finish(
|
|
|
|
|
self,
|
2020-07-27 15:25:04 -07:00
|
|
|
message: RegisterThirdMessage<CS::KeyFormat, CS::Hash>,
|
2020-06-05 09:35:14 -07:00
|
|
|
) -> Result<Self, ProtocolError> {
|
|
|
|
|
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
|
2020-06-14 23:25:31 -07:00
|
|
|
pub struct ClientLogin<CS: CipherSuite> {
|
2020-10-09 10:51:58 -07:00
|
|
|
/// User identity
|
|
|
|
|
id_u: Vec<u8>,
|
|
|
|
|
/// Server identity
|
|
|
|
|
id_s: Vec<u8>,
|
2020-11-02 13:51:43 -08:00
|
|
|
/// token containing the client's password and the blinding factor
|
|
|
|
|
token: oprf::Token<CS::Group>,
|
2020-09-19 19:16:35 -04:00
|
|
|
ke1_state: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE1State,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for ClientLogin<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
type Error = ProtocolError;
|
2020-10-09 10:51:58 -07:00
|
|
|
fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
|
2020-11-16 11:49:27 -08:00
|
|
|
let (id_u, bytes) = tokenize(&input, 2)?;
|
|
|
|
|
let (id_s, bytes) = tokenize(&bytes, 2)?;
|
2020-10-09 10:51:58 -07:00
|
|
|
|
2020-07-03 15:58:42 -04:00
|
|
|
let scalar_len = <CS::Group as Group>::ScalarLen::to_usize();
|
2020-09-19 19:16:35 -04:00
|
|
|
let ke1_state_size =
|
|
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::ke1_state_size();
|
2020-09-02 10:29:22 -04:00
|
|
|
|
|
|
|
|
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 {
|
2020-10-09 10:51:58 -07:00
|
|
|
Ok(bytes.clone())
|
2020-09-02 10:29:22 -04:00
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let blinding_factor_bytes = GenericArray::from_slice(&checked_slice[..scalar_len]);
|
|
|
|
|
let blinding_factor = CS::Group::from_scalar_slice(blinding_factor_bytes)?;
|
2020-09-19 19:16:35 -04:00
|
|
|
let ke1_state =
|
|
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE1State::try_from(
|
2020-09-24 14:31:29 +00:00
|
|
|
&checked_slice[scalar_len..scalar_len + ke1_state_size],
|
2020-09-19 19:16:35 -04:00
|
|
|
)?;
|
2020-07-13 15:23:29 -07:00
|
|
|
let password = bytes[scalar_len + ke1_state_size..].to_vec();
|
2020-06-05 09:35:14 -07:00
|
|
|
Ok(Self {
|
2020-10-09 10:51:58 -07:00
|
|
|
id_u,
|
|
|
|
|
id_s,
|
2020-11-02 13:51:43 -08:00
|
|
|
token: oprf::Token {
|
|
|
|
|
data: password,
|
|
|
|
|
blind: blinding_factor,
|
|
|
|
|
},
|
2020-06-05 09:35:14 -07:00
|
|
|
ke1_state,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> ClientLogin<CS> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the client's login state
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
|
let output: Vec<u8> = [
|
2020-10-09 10:51:58 -07:00
|
|
|
&serialize(&self.id_u, 2),
|
|
|
|
|
&serialize(&self.id_s, 2),
|
2020-11-02 13:51:43 -08:00
|
|
|
&CS::Group::scalar_as_bytes(&self.token.blind)[..],
|
2020-06-05 09:35:14 -07:00
|
|
|
&self.ke1_state.to_bytes(),
|
2020-11-02 13:51:43 -08:00
|
|
|
&self.token.data,
|
2020-06-05 09:35:14 -07:00
|
|
|
]
|
|
|
|
|
.concat();
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
/// Optional parameters for client login start
|
|
|
|
|
pub enum ClientLoginStartParameters {
|
|
|
|
|
/// Specifying an info field that will be sent to the server
|
|
|
|
|
WithInfo(Vec<u8>),
|
|
|
|
|
/// Specifying the info field along with idU and idS
|
2020-11-29 23:06:55 -08:00
|
|
|
WithInfoAndIdentifiers(Vec<u8>, Vec<u8>, Vec<u8>),
|
2020-11-16 14:05:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ClientLoginStartParameters {
|
|
|
|
|
fn default() -> Self {
|
2020-11-29 23:06:55 -08:00
|
|
|
Self::WithInfoAndIdentifiers(Vec::new(), Vec::new(), Vec::new())
|
2020-11-16 14:05:43 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Contains the fields that are returned by a client login start
|
|
|
|
|
pub struct ClientLoginStartResult<CS: CipherSuite> {
|
|
|
|
|
/// The message to send to the server to begin the login protocol
|
|
|
|
|
pub credential_request: LoginFirstMessage<CS>,
|
|
|
|
|
/// The state that the client must keep in order to complete the protocol
|
|
|
|
|
pub client_login_state: ClientLogin<CS>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Optional parameters for client login finish
|
|
|
|
|
pub enum ClientLoginFinishParameters {
|
|
|
|
|
/// Specifying an info and confidential info field that will be sent to the server
|
|
|
|
|
WithInfo(Vec<u8>, Vec<u8>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ClientLoginFinishParameters {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::WithInfo(Vec::new(), Vec::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Contains the fields that are returned by a client login finish
|
|
|
|
|
pub struct ClientLoginFinishResult<CS: CipherSuite> {
|
|
|
|
|
/// The plaintext info sent by the client
|
|
|
|
|
pub plain_info: Vec<u8>,
|
|
|
|
|
/// The message to send back to the client
|
|
|
|
|
pub confidential_info: Vec<u8>,
|
|
|
|
|
/// The message to send to the server to complete the protocol
|
|
|
|
|
pub key_exchange: LoginThirdMessage<CS>,
|
|
|
|
|
/// The shared session secret
|
|
|
|
|
pub session_secret: Vec<u8>,
|
|
|
|
|
/// The client-side export key
|
|
|
|
|
pub export_key: GenericArray<u8, ExportKeySize>,
|
2020-11-29 23:06:55 -08:00
|
|
|
/// The server's static public key
|
|
|
|
|
pub server_s_pk: Vec<u8>,
|
|
|
|
|
/// An optional id_s if suppleid by the server
|
|
|
|
|
pub id_s: Option<Vec<u8>>,
|
2020-11-16 14:05:43 -08:00
|
|
|
}
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-06-14 23:25:31 -07:00
|
|
|
impl<CS: CipherSuite> ClientLogin<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
/// Returns an initial "blinded" password request to send to the server, as well as a ClientLogin
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * `password` - A user password
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{ClientLogin, ClientLoginStartParameters};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let client_login_start_result = ClientLogin::<Default>::start(b"hunter2", &mut client_rng, ClientLoginStartParameters::default())?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn start<R: RngCore + CryptoRng>(
|
|
|
|
|
password: &[u8],
|
|
|
|
|
rng: &mut R,
|
2020-11-16 14:05:43 -08:00
|
|
|
params: ClientLoginStartParameters,
|
2020-11-04 11:33:58 -05:00
|
|
|
#[cfg(test)] postprocess: fn(<CS::Group as Group>::Scalar) -> <CS::Group as Group>::Scalar,
|
2020-11-16 14:05:43 -08:00
|
|
|
) -> Result<ClientLoginStartResult<CS>, ProtocolError> {
|
|
|
|
|
let (info, id_u, id_s) = match params {
|
|
|
|
|
ClientLoginStartParameters::WithInfo(info) => (info, Vec::new(), Vec::new()),
|
2020-11-29 23:06:55 -08:00
|
|
|
ClientLoginStartParameters::WithInfoAndIdentifiers(info, id_u, id_s) => {
|
2020-11-16 14:05:43 -08:00
|
|
|
(info, id_u, id_s)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-04 11:33:58 -05:00
|
|
|
let (token, alpha) = oprf::blind::<R, CS::Group>(
|
|
|
|
|
&password,
|
|
|
|
|
rng,
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
postprocess,
|
|
|
|
|
)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
let (ke1_state, ke1_message) =
|
|
|
|
|
CS::KeyExchange::generate_ke1(alpha.to_arr().to_vec(), info, rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-06 10:06:49 -08:00
|
|
|
let l1 = LoginFirstMessage { alpha, ke1_message };
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
Ok(ClientLoginStartResult {
|
|
|
|
|
credential_request: l1,
|
|
|
|
|
client_login_state: Self {
|
|
|
|
|
id_u,
|
|
|
|
|
id_s,
|
2020-11-02 13:51:43 -08:00
|
|
|
token,
|
2020-06-05 09:35:14 -07:00
|
|
|
ke1_state,
|
|
|
|
|
},
|
2020-11-16 14:05:43 -08:00
|
|
|
})
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-02 12:24:53 -07:00
|
|
|
/// "Unblinds" the server's answer and returns the opened assets from
|
2020-06-05 09:35:14 -07:00
|
|
|
/// the server
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * `message` - the server's answer to the initial login attempt
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{ClientLogin, ClientLoginStartParameters, ClientLoginFinishParameters, ServerLogin, ServerLoginStartParameters};
|
|
|
|
|
/// # use opaque_ke::{ClientRegistration, ClientRegistrationStartParameters, ServerRegistration};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// # use opaque_ke::keypair::{X25519KeyPair, KeyPair};
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// # let mut server_rng = OsRng;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// # let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// # let (register_m2, server_state) = ServerRegistration::<Default>::start(register_m1, server_kp.public(), &mut server_rng)?;
|
|
|
|
|
/// # let (register_m3, _export_key) = client_state.finish(register_m2, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let p_file = server_state.finish(register_m3)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let client_login_start_result = ClientLogin::<Default>::start(b"hunter2", &mut client_rng, ClientLoginStartParameters::default())?;
|
|
|
|
|
/// let server_login_start_result = ServerLogin::start(p_file, &server_kp.private(), client_login_start_result.credential_request, &mut server_rng, ServerLoginStartParameters::default())?;
|
|
|
|
|
/// let client_login_finish_result = client_login_start_result.client_login_state.finish(server_login_start_result.credential_response, ClientLoginFinishParameters::default())?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
pub fn finish(
|
2020-06-05 09:35:14 -07:00
|
|
|
self,
|
2020-09-19 19:16:35 -04:00
|
|
|
l2: LoginSecondMessage<CS>,
|
2020-11-16 14:05:43 -08:00
|
|
|
params: ClientLoginFinishParameters,
|
2020-07-13 15:23:29 -07:00
|
|
|
) -> Result<ClientLoginFinishResult<CS>, ProtocolError> {
|
2020-11-16 14:05:43 -08:00
|
|
|
let (info, e_info) = match params {
|
|
|
|
|
ClientLoginFinishParameters::WithInfo(info, e_info) => (info, e_info),
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-03 16:21:11 -04:00
|
|
|
let l2_bytes: Vec<u8> = [&l2.beta.to_arr()[..], &l2.envelope.to_bytes()].concat();
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-02 13:51:43 -08:00
|
|
|
let password_derived_key =
|
|
|
|
|
get_password_derived_key::<CS::Group, CS::SlowHash, CS::Hash>(&self.token, l2.beta)?;
|
2020-07-02 12:24:53 -07:00
|
|
|
|
2020-07-27 15:25:04 -07:00
|
|
|
let opened_envelope = &l2
|
2020-07-02 12:24:53 -07:00
|
|
|
.envelope
|
2020-10-09 10:51:58 -07:00
|
|
|
.open(&password_derived_key)
|
2020-07-02 12:24:53 -07:00
|
|
|
.map_err(|e| match e {
|
|
|
|
|
InternalPakeError::SealOpenHmacError => PakeError::InvalidLoginError,
|
|
|
|
|
err => PakeError::from(err),
|
|
|
|
|
})?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
let client_s_sk = <CS::KeyFormat as KeyPair>::Repr::from_bytes(
|
|
|
|
|
&opened_envelope.credentials_map[&CredentialType::SkU],
|
|
|
|
|
)?;
|
|
|
|
|
let server_s_pk = <CS::KeyFormat as KeyPair>::Repr::from_bytes(
|
|
|
|
|
&opened_envelope.credentials_map[&CredentialType::PkS],
|
2020-06-05 09:35:14 -07:00
|
|
|
)?;
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
let id_u = match opened_envelope.credentials_map.get(&CredentialType::IdU) {
|
|
|
|
|
Some(id_u) => id_u.clone(),
|
|
|
|
|
None => CS::KeyFormat::public_from_private(&client_s_sk)
|
|
|
|
|
.to_arr()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-29 23:06:55 -08:00
|
|
|
let (id_s, ret_id_s) = match opened_envelope.credentials_map.get(&CredentialType::IdS) {
|
|
|
|
|
Some(id_s) => (id_s.clone(), Some(id_s.clone())),
|
|
|
|
|
None => (server_s_pk.to_arr().to_vec(), None),
|
2020-11-16 14:05:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (plain_info, confidential_info, session_secret, ke3_message) =
|
|
|
|
|
CS::KeyExchange::generate_ke3(
|
|
|
|
|
l2_bytes,
|
|
|
|
|
l2.ke2_message,
|
|
|
|
|
&self.ke1_state,
|
2020-11-29 23:06:55 -08:00
|
|
|
server_s_pk.clone(),
|
2020-11-16 14:05:43 -08:00
|
|
|
client_s_sk,
|
|
|
|
|
id_u,
|
|
|
|
|
id_s,
|
|
|
|
|
info,
|
|
|
|
|
e_info,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(ClientLoginFinishResult {
|
|
|
|
|
plain_info,
|
|
|
|
|
confidential_info,
|
|
|
|
|
key_exchange: LoginThirdMessage { ke3_message },
|
|
|
|
|
session_secret,
|
|
|
|
|
export_key: opened_envelope.export_key,
|
2020-11-29 23:06:55 -08:00
|
|
|
server_s_pk: server_s_pk.to_arr().to_vec(),
|
|
|
|
|
id_s: ret_id_s,
|
2020-11-16 14:05:43 -08:00
|
|
|
})
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The state elements the server holds to record a login
|
2020-07-13 15:23:29 -07:00
|
|
|
pub struct ServerLogin<CS: CipherSuite> {
|
2020-09-19 19:16:35 -04:00
|
|
|
ke2_state: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE2State,
|
2020-07-13 15:23:29 -07:00
|
|
|
_cs: PhantomData<CS>,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for ServerLogin<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
type Error = ProtocolError;
|
|
|
|
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
|
|
|
|
Ok(Self {
|
2020-07-13 15:23:29 -07:00
|
|
|
_cs: PhantomData,
|
2020-09-19 19:16:35 -04:00
|
|
|
ke2_state:
|
|
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE2State::try_from(
|
2020-09-24 14:31:29 +00:00
|
|
|
bytes,
|
2020-09-19 19:16:35 -04:00
|
|
|
)?,
|
2020-06-05 09:35:14 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
/// Optional parameters for server login start
|
|
|
|
|
pub enum ServerLoginStartParameters {
|
|
|
|
|
/// Specifying an info and confidential info field that will be sent to the client
|
|
|
|
|
WithInfo(Vec<u8>, Vec<u8>),
|
|
|
|
|
/// Specifying an info, confidential info that will be sent to the client,
|
|
|
|
|
/// along with an id_u and id_s that will be matched against the client
|
|
|
|
|
WithInfoAndIdentifiers(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ServerLoginStartParameters {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::WithInfo(Vec::new(), Vec::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Contains the fields that are returned by a server login start
|
|
|
|
|
pub struct ServerLoginStartResult<CS: CipherSuite> {
|
|
|
|
|
/// The plaintext info sent by the client
|
|
|
|
|
pub plain_info: Vec<u8>,
|
|
|
|
|
/// The message to send back to the client
|
|
|
|
|
pub credential_response: LoginSecondMessage<CS>,
|
|
|
|
|
/// The state that the server must keep in order to finish the protocl
|
|
|
|
|
pub server_login_state: ServerLogin<CS>,
|
2020-11-29 23:06:55 -08:00
|
|
|
/// The client's static public key
|
|
|
|
|
pub client_s_pk: Vec<u8>,
|
2020-11-16 14:05:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Contains the fields that are returned by a server login finish
|
|
|
|
|
pub struct ServerLoginFinishResult {
|
|
|
|
|
/// The plaintext info sent by the client
|
|
|
|
|
pub plain_info: Vec<u8>,
|
|
|
|
|
/// The confidential info sent by the client
|
|
|
|
|
pub confidential_info: Vec<u8>,
|
|
|
|
|
/// The shared session secret between client and server
|
|
|
|
|
pub session_secret: Vec<u8>,
|
|
|
|
|
}
|
2020-06-14 23:25:31 -07:00
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
impl<CS: CipherSuite> ServerLogin<CS> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the server's login state
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{ClientLogin, ClientLoginStartParameters, ServerLogin, ServerLoginStartParameters};
|
|
|
|
|
/// # use opaque_ke::{ClientRegistration, ClientRegistrationStartParameters, ServerRegistration};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// # use opaque_ke::keypair::{KeyPair, X25519KeyPair};
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// let mut server_rng = OsRng;
|
|
|
|
|
/// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// # let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let (register_m2, server_state) =
|
2020-11-16 14:05:43 -08:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, server_kp.public(), &mut server_rng)?;
|
|
|
|
|
/// # let (register_m3, _export_key) = client_state.finish(register_m2, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let p_file = server_state.finish(register_m3)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let client_login_start_result = ClientLogin::<Default>::start(b"hunter2", &mut client_rng, ClientLoginStartParameters::default())?;
|
|
|
|
|
/// let server_login_start_result = ServerLogin::start(p_file, &server_kp.private(), client_login_start_result.credential_request, &mut server_rng, ServerLoginStartParameters::default())?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
2020-07-13 15:23:29 -07:00
|
|
|
pub fn start<R: RngCore + CryptoRng>(
|
2020-06-14 23:25:31 -07:00
|
|
|
password_file: ServerRegistration<CS>,
|
2020-09-22 08:50:39 -04:00
|
|
|
server_s_sk: &<CS::KeyFormat as KeyPair>::Repr,
|
2020-07-13 15:23:29 -07:00
|
|
|
l1: LoginFirstMessage<CS>,
|
2020-06-05 09:35:14 -07:00
|
|
|
rng: &mut R,
|
2020-11-16 14:05:43 -08:00
|
|
|
params: ServerLoginStartParameters,
|
2020-06-14 23:25:31 -07:00
|
|
|
) -> Result<ServerLoginStartResult<CS>, ProtocolError> {
|
2020-06-05 09:35:14 -07:00
|
|
|
let client_s_pk = password_file
|
|
|
|
|
.client_s_pk
|
2020-07-02 12:24:53 -07:00
|
|
|
.ok_or(InternalPakeError::SealError)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
let (info, e_info, id_u, id_s) = match params {
|
|
|
|
|
ServerLoginStartParameters::WithInfo(info, e_info) => (info, e_info, None, None),
|
|
|
|
|
ServerLoginStartParameters::WithInfoAndIdentifiers(info, e_info, id_u, id_s) => {
|
|
|
|
|
(info, e_info, Some(id_u), Some(id_s))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let id_u = match id_u {
|
|
|
|
|
Some(id_u) => id_u,
|
|
|
|
|
None => client_s_pk.to_arr().to_vec(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let id_s = match id_s {
|
|
|
|
|
Some(id_s) => id_s,
|
|
|
|
|
None => CS::KeyFormat::public_from_private(server_s_sk)
|
|
|
|
|
.to_arr()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let l1_bytes = &l1.to_bytes();
|
2020-11-29 23:06:55 -08:00
|
|
|
let beta = oprf::evaluate(l1.alpha, &password_file.oprf_key);
|
2020-11-16 14:05:43 -08:00
|
|
|
let envelope = password_file.envelope.ok_or(InternalPakeError::SealError)?;
|
2020-07-03 16:21:11 -04:00
|
|
|
let l2_component: Vec<u8> = [&beta.to_arr()[..], &envelope.to_bytes()].concat();
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
let (plain_info, ke2_state, ke2_message) = CS::KeyExchange::generate_ke2(
|
2020-06-05 09:35:14 -07:00
|
|
|
rng,
|
|
|
|
|
l1_bytes.to_vec(),
|
|
|
|
|
l2_component,
|
2020-07-13 15:23:29 -07:00
|
|
|
l1.ke1_message,
|
2020-11-29 23:06:55 -08:00
|
|
|
client_s_pk.clone(),
|
2020-06-05 09:35:14 -07:00
|
|
|
server_s_sk.clone(),
|
2020-11-16 14:05:43 -08:00
|
|
|
id_u,
|
|
|
|
|
id_s,
|
|
|
|
|
info,
|
|
|
|
|
e_info,
|
2020-06-05 09:35:14 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let l2 = LoginSecondMessage {
|
|
|
|
|
beta,
|
|
|
|
|
envelope,
|
|
|
|
|
ke2_message,
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
Ok(ServerLoginStartResult {
|
|
|
|
|
plain_info,
|
|
|
|
|
credential_response: l2,
|
|
|
|
|
server_login_state: Self {
|
2020-07-13 15:23:29 -07:00
|
|
|
_cs: PhantomData,
|
|
|
|
|
ke2_state,
|
|
|
|
|
},
|
2020-11-29 23:06:55 -08:00
|
|
|
client_s_pk: client_s_pk.to_arr().to_vec(),
|
2020-11-16 14:05:43 -08:00
|
|
|
})
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
/// From the client's second and final message, check the client's
|
|
|
|
|
/// authentication and produce a message transport
|
2020-06-05 09:35:14 -07:00
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * `message` - the client's second login message
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
/// use opaque_ke::{ClientLogin, ClientLoginFinishParameters, ClientLoginStartParameters, ServerLogin, ServerLoginStartParameters};
|
|
|
|
|
/// # use opaque_ke::{ClientRegistration, ClientRegistrationStartParameters, ServerRegistration};
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # use opaque_ke::errors::ProtocolError;
|
|
|
|
|
/// # use opaque_ke::keypair::{KeyPair, X25519KeyPair};
|
|
|
|
|
/// use rand_core::{OsRng, RngCore};
|
2020-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::ciphersuite::CipherSuite;
|
|
|
|
|
/// struct Default;
|
|
|
|
|
/// impl CipherSuite for Default {
|
|
|
|
|
/// type Group = curve25519_dalek::ristretto::RistrettoPoint;
|
|
|
|
|
/// type KeyFormat = opaque_ke::keypair::X25519KeyPair;
|
2020-07-13 15:23:29 -07:00
|
|
|
/// type KeyExchange = opaque_ke::key_exchange::tripledh::TripleDH;
|
2020-07-27 15:25:04 -07:00
|
|
|
/// type Hash = sha2::Sha256;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// type SlowHash = opaque_ke::slow_hash::NoOpHash;
|
|
|
|
|
/// }
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
|
|
|
|
/// let mut server_rng = OsRng;
|
|
|
|
|
/// let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// # let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", ClientRegistrationStartParameters::default(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let (register_m2, server_state) =
|
2020-11-16 14:05:43 -08:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, server_kp.public(), &mut server_rng)?;
|
|
|
|
|
/// # let (register_m3, _export_key) = client_state.finish(register_m2, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let p_file = server_state.finish(register_m3)?;
|
2020-11-16 14:05:43 -08:00
|
|
|
/// let client_login_start_result = ClientLogin::<Default>::start(b"hunter2", &mut client_rng, ClientLoginStartParameters::default())?;
|
|
|
|
|
/// let server_login_start_result = ServerLogin::start(p_file, &server_kp.private(), client_login_start_result.credential_request, &mut server_rng, ServerLoginStartParameters::default())?;
|
|
|
|
|
/// let client_login_finish_result = client_login_start_result.client_login_state.finish(server_login_start_result.credential_response, ClientLoginFinishParameters::default())?;
|
|
|
|
|
/// let mut server_transport = server_login_start_result.server_login_state.finish(client_login_finish_result.key_exchange)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
2020-11-16 14:05:43 -08:00
|
|
|
pub fn finish(
|
|
|
|
|
&self,
|
|
|
|
|
message: LoginThirdMessage<CS>,
|
|
|
|
|
) -> Result<ServerLoginFinishResult, ProtocolError> {
|
|
|
|
|
let (plain_info, confidential_info, session_secret) =
|
|
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::finish_ke(
|
|
|
|
|
message.ke3_message,
|
|
|
|
|
&self.ke2_state,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|e| match e {
|
|
|
|
|
ProtocolError::VerificationError(PakeError::KeyExchangeMacValidationError) => {
|
|
|
|
|
ProtocolError::VerificationError(PakeError::InvalidLoginError)
|
|
|
|
|
}
|
|
|
|
|
err => err,
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Ok(ServerLoginFinishResult {
|
|
|
|
|
plain_info,
|
|
|
|
|
confidential_info,
|
|
|
|
|
session_secret,
|
2020-09-19 19:16:35 -04:00
|
|
|
})
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
2020-11-12 10:00:09 -08:00
|
|
|
fn get_password_derived_key<G: GroupWithMapToCurve, SH: SlowHash<D>, D: Hash>(
|
2020-11-02 13:51:43 -08:00
|
|
|
token: &oprf::Token<G>,
|
2020-06-05 09:35:14 -07:00
|
|
|
beta: G,
|
2020-06-08 21:02:01 -07:00
|
|
|
) -> Result<Vec<u8>, InternalPakeError> {
|
2020-11-12 10:00:09 -08:00
|
|
|
let oprf_output = oprf::finalize::<G, D>(
|
|
|
|
|
&token.data,
|
|
|
|
|
&oprf::unblind::<G>(token, beta),
|
|
|
|
|
STR_OPAQUE_VERSION,
|
|
|
|
|
);
|
2020-06-08 21:02:01 -07:00
|
|
|
SH::hash(oprf_output)
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|