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-07-02 12:24:53 -07:00
|
|
|
envelope::{Envelope, ExportKeySize},
|
2020-06-05 09:35:14 -07:00
|
|
|
errors::{utils::check_slice_size, InternalPakeError, PakeError, ProtocolError},
|
|
|
|
|
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-06-05 09:35:14 -07:00
|
|
|
keypair::{Key, KeyPair, SizedBytes},
|
|
|
|
|
oprf,
|
|
|
|
|
oprf::OprfClientBytes,
|
2020-06-08 21:02:01 -07:00
|
|
|
slow_hash::SlowHash,
|
2020-06-05 09:35:14 -07:00
|
|
|
};
|
2020-07-02 12:24:53 -07:00
|
|
|
use generic_array::{typenum::Unsigned, GenericArray};
|
2020-06-05 09:35:14 -07:00
|
|
|
use rand_core::{CryptoRng, RngCore};
|
|
|
|
|
use std::{convert::TryFrom, marker::PhantomData};
|
|
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
|
|
|
|
|
// Messages
|
|
|
|
|
// =========
|
|
|
|
|
|
|
|
|
|
/// The message sent by the client to the server, to initiate registration
|
|
|
|
|
pub struct RegisterFirstMessage<Grp> {
|
|
|
|
|
/// blinded password information
|
|
|
|
|
alpha: Grp,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Grp: Group> TryFrom<&[u8]> for RegisterFirstMessage<Grp> {
|
|
|
|
|
type Error = ProtocolError;
|
|
|
|
|
fn try_from(first_message_bytes: &[u8]) -> Result<Self, Self::Error> {
|
2020-09-02 10:29:22 -04:00
|
|
|
let checked_slice = check_slice_size(
|
|
|
|
|
first_message_bytes,
|
|
|
|
|
Grp::ElemLen::to_usize(),
|
|
|
|
|
"first_message_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 arr = GenericArray::from_slice(checked_slice);
|
2020-06-05 09:35:14 -07:00
|
|
|
let alpha = Grp::from_element_slice(arr)?;
|
|
|
|
|
Ok(Self { alpha })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Grp: Group> RegisterFirstMessage<Grp> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the registration request
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> GenericArray<u8, Grp::ElemLen> {
|
2020-07-03 16:21:11 -04:00
|
|
|
self.alpha.to_arr()
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The answer sent by the server to the user, upon reception of the
|
|
|
|
|
/// registration attempt
|
|
|
|
|
pub struct RegisterSecondMessage<Grp> {
|
|
|
|
|
/// The server's oprf output
|
|
|
|
|
beta: Grp,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Grp> TryFrom<&[u8]> for RegisterSecondMessage<Grp>
|
|
|
|
|
where
|
|
|
|
|
Grp: Group,
|
|
|
|
|
{
|
|
|
|
|
type Error = ProtocolError;
|
|
|
|
|
|
|
|
|
|
fn try_from(second_message_bytes: &[u8]) -> Result<Self, Self::Error> {
|
|
|
|
|
let checked_slice = check_slice_size(
|
|
|
|
|
second_message_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 { beta })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Grp> RegisterSecondMessage<Grp>
|
|
|
|
|
where
|
|
|
|
|
Grp: Group,
|
|
|
|
|
{
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the registration response message
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
2020-07-03 16:21:11 -04:00
|
|
|
self.beta.to_arr().to_vec()
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-02 12:24:53 -07:00
|
|
|
/// The final message from the client, containing sealed cryptographic
|
2020-06-05 09:35:14 -07:00
|
|
|
/// identifiers
|
2020-07-27 15:25:04 -07:00
|
|
|
pub struct RegisterThirdMessage<KeyFormat: KeyPair, D: Hash> {
|
2020-07-02 12:24:53 -07:00
|
|
|
/// The "envelope" generated by the user, containing sealed
|
2020-06-05 09:35:14 -07:00
|
|
|
/// cryptographic identifiers
|
2020-07-27 15:25:04 -07:00
|
|
|
envelope: Envelope<D>,
|
2020-06-05 09:35:14 -07:00
|
|
|
/// The user's public key
|
|
|
|
|
client_s_pk: KeyFormat::Repr,
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 15:25:04 -07:00
|
|
|
impl<KeyFormat, D> RegisterThirdMessage<KeyFormat, D>
|
2020-06-05 09:35:14 -07:00
|
|
|
where
|
|
|
|
|
KeyFormat: KeyPair,
|
2020-07-27 15:25:04 -07:00
|
|
|
D: Hash,
|
2020-06-05 09:35:14 -07:00
|
|
|
{
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the registration upload message
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
2020-07-03 16:21:11 -04:00
|
|
|
[&self.envelope.to_bytes(), &self.client_s_pk.to_arr()[..]].concat()
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 15:25:04 -07:00
|
|
|
impl<KeyFormat, D> TryFrom<&[u8]> for RegisterThirdMessage<KeyFormat, D>
|
2020-06-05 09:35:14 -07:00
|
|
|
where
|
|
|
|
|
KeyFormat: KeyPair,
|
2020-07-27 15:25:04 -07:00
|
|
|
D: Hash,
|
2020-06-05 09:35:14 -07:00
|
|
|
{
|
|
|
|
|
type Error = ProtocolError;
|
|
|
|
|
|
|
|
|
|
fn try_from(third_message_bytes: &[u8]) -> Result<Self, Self::Error> {
|
|
|
|
|
let key_len = <KeyFormat::Repr as SizedBytes>::Len::to_usize();
|
2020-07-27 15:25:04 -07:00
|
|
|
let envelope_size = key_len + Envelope::<D>::additional_size();
|
2020-07-02 12:24:53 -07:00
|
|
|
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..])?;
|
2020-06-05 09:35:14 -07:00
|
|
|
let client_s_pk = KeyFormat::check_public_key(unchecked_client_s_pk)?;
|
|
|
|
|
|
|
|
|
|
Ok(Self {
|
2020-07-27 15:25:04 -07:00
|
|
|
envelope: Envelope::<D>::from_bytes(&checked_bytes[..envelope_size])?,
|
2020-06-05 09:35:14 -07:00
|
|
|
client_s_pk,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The message sent by the user to the server, to initiate registration
|
2020-07-13 15:23:29 -07:00
|
|
|
pub struct LoginFirstMessage<CS: CipherSuite> {
|
2020-06-05 09:35:14 -07:00
|
|
|
/// blinded password information
|
2020-07-13 15:23:29 -07:00
|
|
|
alpha: CS::Group,
|
2020-09-19 19:16:35 -04:00
|
|
|
ke1_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE1Message,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for LoginFirstMessage<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
type Error = ProtocolError;
|
|
|
|
|
fn try_from(first_message_bytes: &[u8]) -> Result<Self, Self::Error> {
|
2020-09-02 10:29:22 -04:00
|
|
|
let min_expected_len = <CS::Group as Group>::ElemLen::to_usize();
|
|
|
|
|
let checked_slice = (if first_message_bytes.len() <= min_expected_len {
|
|
|
|
|
Err(InternalPakeError::SizeError {
|
|
|
|
|
name: "first_message_bytes",
|
|
|
|
|
len: min_expected_len,
|
|
|
|
|
actual_len: first_message_bytes.len(),
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
Ok(first_message_bytes)
|
|
|
|
|
})?;
|
2020-06-05 09:35:14 -07:00
|
|
|
// Check that the message is actually containing an element of the
|
|
|
|
|
// correct subgroup
|
2020-07-13 15:23:29 -07:00
|
|
|
let elem_len = <CS::Group as Group>::ElemLen::to_usize();
|
2020-09-02 10:29:22 -04:00
|
|
|
let arr = GenericArray::from_slice(&checked_slice[..elem_len]);
|
2020-07-13 15:23:29 -07:00
|
|
|
let alpha = CS::Group::from_element_slice(arr)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
let ke1_message =
|
|
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE1Message::try_from(
|
|
|
|
|
checked_slice[elem_len..].to_vec(),
|
|
|
|
|
)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
Ok(Self { alpha, ke1_message })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
impl<CS: CipherSuite> LoginFirstMessage<CS> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the login request
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
2020-07-03 16:21:11 -04:00
|
|
|
[&self.alpha.to_arr()[..], &self.ke1_message.to_bytes()].concat()
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The answer sent by the server to the user, upon reception of the
|
|
|
|
|
/// login attempt.
|
2020-09-19 19:16:35 -04:00
|
|
|
pub struct LoginSecondMessage<CS: CipherSuite> {
|
2020-06-05 09:35:14 -07:00
|
|
|
/// the server's oprf output
|
2020-09-19 19:16:35 -04:00
|
|
|
beta: CS::Group,
|
2020-07-02 12:24:53 -07:00
|
|
|
/// the user's sealed information,
|
2020-09-19 19:16:35 -04:00
|
|
|
envelope: Envelope<CS::Hash>,
|
|
|
|
|
ke2_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE2Message,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
impl<CS: CipherSuite> LoginSecondMessage<CS> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the login response
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
|
[
|
2020-07-03 16:21:11 -04:00
|
|
|
&self.beta.to_arr()[..],
|
2020-06-05 09:35:14 -07:00
|
|
|
&self.envelope.to_bytes()[..],
|
|
|
|
|
&self.ke2_message.to_bytes()[..],
|
|
|
|
|
]
|
|
|
|
|
.concat()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for LoginSecondMessage<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
type Error = ProtocolError;
|
|
|
|
|
fn try_from(second_message_bytes: &[u8]) -> Result<Self, Self::Error> {
|
2020-09-19 19:16:35 -04:00
|
|
|
let key_len = <<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len::to_usize();
|
|
|
|
|
let envelope_size = key_len + Envelope::<CS::Hash>::additional_size();
|
|
|
|
|
let elem_len = <CS::Group as Group>::ElemLen::to_usize();
|
|
|
|
|
let ke2_message_size = CS::KeyExchange::ke2_message_size();
|
2020-06-05 09:35:14 -07:00
|
|
|
let checked_slice = check_slice_size(
|
|
|
|
|
second_message_bytes,
|
2020-07-13 15:23:29 -07:00
|
|
|
elem_len + envelope_size + ke2_message_size,
|
2020-06-05 09:35:14 -07:00
|
|
|
"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);
|
2020-09-19 19:16:35 -04:00
|
|
|
let beta = CS::Group::from_element_slice(arr)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-07-27 15:25:04 -07:00
|
|
|
let envelope =
|
2020-09-19 19:16:35 -04:00
|
|
|
Envelope::<CS::Hash>::from_bytes(&checked_slice[elem_len..elem_len + envelope_size])?;
|
2020-07-13 15:23:29 -07:00
|
|
|
|
|
|
|
|
let ke2_message =
|
2020-09-19 19:16:35 -04:00
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE2Message::try_from(
|
|
|
|
|
checked_slice[elem_len + envelope_size..].to_vec(),
|
|
|
|
|
)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
|
beta,
|
|
|
|
|
envelope,
|
|
|
|
|
ke2_message,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The answer sent by the client to the server, upon reception of the
|
2020-07-02 12:24:53 -07:00
|
|
|
/// sealed envelope
|
2020-07-13 15:23:29 -07:00
|
|
|
pub struct LoginThirdMessage<CS: CipherSuite> {
|
2020-09-19 19:16:35 -04:00
|
|
|
ke3_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE3Message,
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
impl<CS: CipherSuite> TryFrom<&[u8]> for LoginThirdMessage<CS> {
|
2020-06-05 09:35:14 -07:00
|
|
|
type Error = ProtocolError;
|
|
|
|
|
|
|
|
|
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
2020-07-27 15:25:04 -07:00
|
|
|
let ke3_message =
|
2020-09-19 19:16:35 -04:00
|
|
|
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeyFormat>>::KE3Message::try_from(
|
|
|
|
|
bytes.to_vec(),
|
|
|
|
|
)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
Ok(Self { ke3_message })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
impl<CS: CipherSuite> LoginThirdMessage<CS> {
|
2020-07-06 15:40:35 -04:00
|
|
|
/// byte representation for the login finalization
|
2020-06-05 09:35:14 -07:00
|
|
|
pub fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
|
self.ke3_message.to_bytes()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Registration
|
|
|
|
|
// ============
|
|
|
|
|
|
|
|
|
|
/// The state elements the client holds to register itself
|
2020-06-14 23:25:31 -07:00
|
|
|
pub struct ClientRegistration<CS: CipherSuite> {
|
2020-06-05 09:35:14 -07:00
|
|
|
/// a blinding factor
|
2020-07-03 15:58:42 -04:00
|
|
|
pub(crate) blinding_factor: <CS::Group as Group>::Scalar,
|
2020-06-05 09:35:14 -07:00
|
|
|
/// the client's password
|
|
|
|
|
password: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
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 {
|
|
|
|
|
blinding_factor,
|
|
|
|
|
password,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-07-03 16:21:11 -04:00
|
|
|
&CS::Group::scalar_as_bytes(&self.blinding_factor)[..],
|
2020-06-05 09:35:14 -07:00
|
|
|
&self.password,
|
|
|
|
|
]
|
|
|
|
|
.concat();
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use opaque_ke::opaque::ClientRegistration;
|
|
|
|
|
/// # 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 rng = OsRng;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// let (register_m1, registration_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn start<R: RngCore + CryptoRng>(
|
|
|
|
|
password: &[u8],
|
|
|
|
|
pepper: Option<&[u8]>,
|
|
|
|
|
blinding_factor_rng: &mut R,
|
2020-06-14 23:25:31 -07:00
|
|
|
) -> Result<(RegisterFirstMessage<CS::Group>, Self), ProtocolError> {
|
2020-06-05 09:35:14 -07:00
|
|
|
let OprfClientBytes {
|
|
|
|
|
alpha,
|
|
|
|
|
blinding_factor,
|
2020-06-14 23:25:31 -07:00
|
|
|
} = oprf::generate_oprf1::<R, CS::Group>(&password, pepper, blinding_factor_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
Ok((
|
2020-06-14 23:25:31 -07:00
|
|
|
RegisterFirstMessage::<CS::Group> { alpha },
|
2020-06-05 09:35:14 -07:00
|
|
|
Self {
|
|
|
|
|
blinding_factor,
|
|
|
|
|
password: password.to_vec(),
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use opaque_ke::{opaque::{ClientRegistration, ServerRegistration}, keypair::{X25519KeyPair, SizedBytes}};
|
|
|
|
|
/// # 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-06-14 23:25:31 -07:00
|
|
|
/// let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (register_m2, server_state) =
|
2020-06-14 23:25:31 -07:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, &mut server_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// let register_m3 = client_state.finish(register_m2, server_kp.public(), &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-06-05 09:35:14 -07:00
|
|
|
self,
|
2020-06-14 23:25:31 -07:00
|
|
|
r2: RegisterSecondMessage<CS::Group>,
|
2020-07-03 15:58:42 -04:00
|
|
|
server_s_pk: &<CS::KeyFormat as KeyPair>::Repr,
|
2020-06-05 09:35:14 -07:00
|
|
|
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-07-27 15:25:04 -07:00
|
|
|
let password_derived_key = get_password_derived_key::<CS::Group, CS::SlowHash, CS::Hash>(
|
2020-06-08 21:02:01 -07:00
|
|
|
self.password.clone(),
|
|
|
|
|
r2.beta,
|
|
|
|
|
&self.blinding_factor,
|
|
|
|
|
)?;
|
2020-07-02 12:24:53 -07:00
|
|
|
|
2020-07-27 15:25:04 -07:00
|
|
|
let (envelope, export_key) = Envelope::<CS::Hash>::seal(
|
2020-07-02 12:24:53 -07:00
|
|
|
&password_derived_key,
|
2020-06-05 09:35:14 -07:00
|
|
|
&client_static_keypair.private().to_arr(),
|
|
|
|
|
&server_s_pk.to_arr(),
|
|
|
|
|
rng,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
self.password.zeroize();
|
|
|
|
|
self.blinding_factor.zeroize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
self.password.zeroize();
|
|
|
|
|
self.blinding_factor.zeroize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
fn try_from(server_registration_bytes: &[u8]) -> Result<Self, Self::Error> {
|
2020-07-03 16:21:11 -04:00
|
|
|
let key_len = <<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len::to_usize();
|
2020-07-03 15:58:42 -04:00
|
|
|
let scalar_len = <CS::Group as Group>::ScalarLen::to_usize();
|
2020-07-27 15:25:04 -07:00
|
|
|
let envelope_size = key_len + Envelope::<CS::Hash>::additional_size();
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
if server_registration_bytes.len() == scalar_len {
|
|
|
|
|
return Ok(Self {
|
2020-06-14 23:25:31 -07:00
|
|
|
oprf_key: CS::Group::from_scalar_slice(GenericArray::from_slice(
|
2020-06-05 09:35:14 -07:00
|
|
|
server_registration_bytes,
|
|
|
|
|
))?,
|
|
|
|
|
client_s_pk: None,
|
|
|
|
|
envelope: None,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let checked_bytes = check_slice_size(
|
|
|
|
|
server_registration_bytes,
|
2020-07-02 12:24:53 -07:00
|
|
|
envelope_size + key_len + scalar_len,
|
2020-06-05 09:35:14 -07:00
|
|
|
"server_registration_bytes",
|
|
|
|
|
)?;
|
|
|
|
|
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-06-05 09:35:14 -07:00
|
|
|
Ok(Self {
|
2020-07-27 15:25:04 -07:00
|
|
|
envelope: Some(Envelope::<CS::Hash>::from_bytes(
|
2020-07-02 12:24:53 -07:00
|
|
|
&checked_bytes[checked_bytes.len() - envelope_size..],
|
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()
|
|
|
|
|
.for_each(|v| output.extend_from_slice(&v));
|
|
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use opaque_ke::{opaque::*, keypair::{X25519KeyPair, SizedBytes}};
|
|
|
|
|
/// # 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-06-14 23:25:31 -07:00
|
|
|
/// let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (register_m2, server_state) =
|
2020-06-14 23:25:31 -07:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, &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-06-05 09:35:14 -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-06-14 23:25:31 -07:00
|
|
|
let beta = oprf::generate_oprf2::<CS::Group>(message.alpha, &oprf_key)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
RegisterSecondMessage { beta },
|
|
|
|
|
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-06-14 23:25:31 -07:00
|
|
|
/// use opaque_ke::{opaque::*, keypair::{KeyPair, X25519KeyPair, SizedBytes}};
|
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-06-14 23:25:31 -07:00
|
|
|
/// let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (register_m2, server_state) =
|
2020-06-14 23:25:31 -07:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, &mut server_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut client_rng = OsRng;
|
2020-09-01 11:23:00 -07:00
|
|
|
/// let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &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-06-05 09:35:14 -07:00
|
|
|
/// A blinding factor, which is used to mask (and unmask) secret
|
|
|
|
|
/// information before transmission
|
2020-07-03 15:58:42 -04:00
|
|
|
blinding_factor: <CS::Group as Group>::Scalar,
|
2020-06-05 09:35:14 -07:00
|
|
|
/// The user's password
|
|
|
|
|
password: Vec<u8>,
|
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;
|
|
|
|
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
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 {
|
|
|
|
|
Ok(bytes)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
checked_slice[scalar_len..scalar_len + ke1_state_size].to_vec(),
|
|
|
|
|
)?;
|
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 {
|
|
|
|
|
blinding_factor,
|
|
|
|
|
password,
|
|
|
|
|
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-07-03 16:21:11 -04:00
|
|
|
&CS::Group::scalar_as_bytes(&self.blinding_factor)[..],
|
2020-06-05 09:35:14 -07:00
|
|
|
&self.ke1_state.to_bytes(),
|
|
|
|
|
&self.password,
|
|
|
|
|
]
|
|
|
|
|
.concat();
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
type ClientLoginFinishResult<CS> = (
|
|
|
|
|
LoginThirdMessage<CS>,
|
|
|
|
|
Vec<u8>,
|
|
|
|
|
GenericArray<u8, ExportKeySize>,
|
|
|
|
|
);
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use opaque_ke::opaque::ClientLogin;
|
|
|
|
|
/// # 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-06-14 23:25:31 -07:00
|
|
|
/// let (login_m1, client_login_state) = ClientLogin::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn start<R: RngCore + CryptoRng>(
|
|
|
|
|
password: &[u8],
|
|
|
|
|
pepper: Option<&[u8]>,
|
|
|
|
|
rng: &mut R,
|
2020-07-13 15:23:29 -07:00
|
|
|
) -> Result<(LoginFirstMessage<CS>, Self), ProtocolError> {
|
2020-06-05 09:35:14 -07:00
|
|
|
let OprfClientBytes {
|
|
|
|
|
alpha,
|
|
|
|
|
blinding_factor,
|
2020-06-14 23:25:31 -07:00
|
|
|
} = oprf::generate_oprf1::<R, CS::Group>(&password, pepper, rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
let (ke1_state, ke1_message) = CS::KeyExchange::generate_ke1(alpha.to_arr().to_vec(), rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
let l1 = LoginFirstMessage { alpha, ke1_message };
|
|
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
l1,
|
|
|
|
|
Self {
|
|
|
|
|
blinding_factor,
|
|
|
|
|
password: password.to_vec(),
|
|
|
|
|
ke1_state,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// 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};
|
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-06-14 23:25:31 -07:00
|
|
|
/// # let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let server_kp = X25519KeyPair::generate_random(&mut server_rng)?;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// # let (register_m2, server_state) = ServerRegistration::<Default>::start(register_m1, &mut server_rng)?;
|
2020-09-01 11:23:00 -07:00
|
|
|
/// # let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let p_file = server_state.finish(register_m3)?;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// let (login_m1, client_login_state) = ClientLogin::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (login_m2, server_login_state) = ServerLogin::start(p_file, &server_kp.private(), login_m1, &mut server_rng)?;
|
2020-09-01 11:23:00 -07:00
|
|
|
/// let (login_m3, client_transport, _export_key) = client_login_state.finish(login_m2, &server_kp.public(), &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: RngCore + CryptoRng>(
|
2020-06-05 09:35:14 -07:00
|
|
|
self,
|
2020-09-19 19:16:35 -04:00
|
|
|
l2: LoginSecondMessage<CS>,
|
2020-07-13 15:23:29 -07:00
|
|
|
server_s_pk: &<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr,
|
2020-06-05 09:35:14 -07:00
|
|
|
_client_e_sk_rng: &mut R,
|
2020-07-13 15:23:29 -07:00
|
|
|
) -> Result<ClientLoginFinishResult<CS>, ProtocolError> {
|
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-07-27 15:25:04 -07:00
|
|
|
let password_derived_key = get_password_derived_key::<CS::Group, CS::SlowHash, CS::Hash>(
|
2020-06-08 21:02:01 -07:00
|
|
|
self.password.clone(),
|
|
|
|
|
l2.beta,
|
|
|
|
|
&self.blinding_factor,
|
|
|
|
|
)?;
|
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
|
|
|
|
|
.open(&password_derived_key, &server_s_pk.to_arr())
|
|
|
|
|
.map_err(|e| match e {
|
|
|
|
|
InternalPakeError::SealOpenHmacError => PakeError::InvalidLoginError,
|
|
|
|
|
err => PakeError::from(err),
|
|
|
|
|
})?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
let (shared_secret, ke3_message) = CS::KeyExchange::generate_ke3(
|
2020-06-05 09:35:14 -07:00
|
|
|
l2_bytes,
|
|
|
|
|
l2.ke2_message,
|
|
|
|
|
&self.ke1_state,
|
|
|
|
|
server_s_pk.clone(),
|
2020-07-27 15:25:04 -07:00
|
|
|
Key::from_bytes(&opened_envelope.plaintext)?,
|
2020-06-05 09:35:14 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
LoginThirdMessage { ke3_message },
|
2020-07-13 15:23:29 -07:00
|
|
|
shared_secret,
|
2020-07-27 15:25:04 -07:00
|
|
|
opened_envelope.export_key,
|
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(
|
|
|
|
|
bytes.to_vec(),
|
|
|
|
|
)?,
|
2020-06-05 09:35:14 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
type ServerLoginStartResult<CS> = (LoginSecondMessage<CS>, ServerLogin<CS>);
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// 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};
|
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-06-14 23:25:31 -07:00
|
|
|
/// # let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let (register_m2, server_state) =
|
2020-06-14 23:25:31 -07:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, &mut server_rng)?;
|
2020-09-01 11:23:00 -07:00
|
|
|
/// # let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let p_file = server_state.finish(register_m3)?;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// let (login_m1, client_login_state) = ClientLogin::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (login_m2, server_login_state) = ServerLogin::start(p_file, &server_kp.private(), login_m1, &mut server_rng)?;
|
|
|
|
|
/// # 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-06-05 09:35:14 -07:00
|
|
|
server_s_sk: &Key,
|
2020-07-13 15:23:29 -07:00
|
|
|
l1: LoginFirstMessage<CS>,
|
2020-06-05 09:35:14 -07:00
|
|
|
rng: &mut R,
|
2020-06-14 23:25:31 -07:00
|
|
|
) -> Result<ServerLoginStartResult<CS>, ProtocolError> {
|
2020-06-05 09:35:14 -07:00
|
|
|
let l1_bytes = &l1.to_bytes();
|
|
|
|
|
let beta = oprf::generate_oprf2(l1.alpha, &password_file.oprf_key)?;
|
|
|
|
|
|
|
|
|
|
let client_s_pk = password_file
|
|
|
|
|
.client_s_pk
|
2020-07-02 12:24:53 -07:00
|
|
|
.ok_or(InternalPakeError::SealError)?;
|
|
|
|
|
let envelope = password_file.envelope.ok_or(InternalPakeError::SealError)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
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-09-19 19:16:35 -04:00
|
|
|
let (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-06-05 09:35:14 -07:00
|
|
|
client_s_pk,
|
|
|
|
|
server_s_sk.clone(),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let l2 = LoginSecondMessage {
|
|
|
|
|
beta,
|
|
|
|
|
envelope,
|
|
|
|
|
ke2_message,
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
Ok((
|
|
|
|
|
l2,
|
|
|
|
|
Self {
|
|
|
|
|
_cs: PhantomData,
|
|
|
|
|
ke2_state,
|
|
|
|
|
},
|
|
|
|
|
))
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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};
|
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-06-14 23:25:31 -07:00
|
|
|
/// # let (register_m1, client_state) = ClientRegistration::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let (register_m2, server_state) =
|
2020-06-14 23:25:31 -07:00
|
|
|
/// ServerRegistration::<Default>::start(register_m1, &mut server_rng)?;
|
2020-09-01 11:23:00 -07:00
|
|
|
/// # let (register_m3, _export_key) = client_state.finish(register_m2, server_kp.public(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// # let p_file = server_state.finish(register_m3)?;
|
2020-06-14 23:25:31 -07:00
|
|
|
/// let (login_m1, client_login_state) = ClientLogin::<Default>::start(b"hunter2", None, &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let (login_m2, server_login_state) = ServerLogin::start(p_file, &server_kp.private(), login_m1, &mut server_rng)?;
|
2020-09-01 11:23:00 -07:00
|
|
|
/// let (login_m3, client_transport, _export_key) = client_login_state.finish(login_m2, &server_kp.public(), &mut client_rng)?;
|
2020-06-05 09:35:14 -07:00
|
|
|
/// let mut server_transport = server_login_state.finish(login_m3)?;
|
|
|
|
|
/// # Ok::<(), ProtocolError>(())
|
|
|
|
|
/// ```
|
2020-07-13 15:23:29 -07:00
|
|
|
pub fn finish(&self, message: LoginThirdMessage<CS>) -> Result<Vec<u8>, ProtocolError> {
|
2020-09-19 19:16:35 -04:00
|
|
|
<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,
|
|
|
|
|
})
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
2020-07-27 15:25:04 -07:00
|
|
|
fn get_password_derived_key<G: Group, SH: SlowHash<D>, D: Hash>(
|
2020-06-05 09:35:14 -07:00
|
|
|
password: Vec<u8>,
|
|
|
|
|
beta: G,
|
|
|
|
|
blinding_factor: &G::Scalar,
|
2020-06-08 21:02:01 -07:00
|
|
|
) -> Result<Vec<u8>, InternalPakeError> {
|
2020-07-27 15:25:04 -07:00
|
|
|
let oprf_output = oprf::generate_oprf3::<G, D>(&password, beta, blinding_factor)?;
|
2020-06-08 21:02:01 -07:00
|
|
|
SH::hash(oprf_output)
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|