use std::fmt;
use rand_core::CryptoRng;
use spake2::{Ed25519Group, Identity, Password, Spake2};
use crate::{CryptoError, Key};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PakeSide {
A,
B,
}
pub struct Pake(Spake2<Ed25519Group>);
impl Pake {
#[must_use]
pub fn start(
side: PakeSide,
password: &[u8],
identity_a: &[u8],
identity_b: &[u8],
rng: &mut impl CryptoRng,
) -> (Self, Vec<u8>) {
let password = Password::new(password);
let (a, b) = (Identity::new(identity_a), Identity::new(identity_b));
let (pake, message) = match side {
PakeSide::A => Spake2::start_a_with_rng(&password, &a, &b, rng),
PakeSide::B => Spake2::start_b_with_rng(&password, &a, &b, rng),
};
(Self(pake), message)
}
pub fn finish(self, message: &[u8]) -> Result<Key, CryptoError> {
let key = self.0.finish(message).map_err(|_| CryptoError::Pake)?;
Key::from_slice(&key).ok_or(CryptoError::Pake)
}
}
impl fmt::Debug for Pake {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Pake(..)")
}
}