use crate::errors::*;
use crate::identity::*;
use crate::random::*;
use crate::types::*;
use crate::verify::*;
use core::fmt;
#[derive(Clone, Copy)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Hello {
pub id: AliceId,
pub v: u8,
}
#[derive(Clone, Copy)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Challenge {
pub n: [u8; 32],
}
#[derive(Clone, Copy)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Response {
pub s: AliceSig,
}
#[derive(Clone, Copy)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AuthResult {
Ok([u8; 16]),
Fail,
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Pending {
pub id: AliceId,
pub c: [u8; 32],
}
impl fmt::Debug for Hello {
#[inline(always)]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
impl fmt::Debug for Challenge {
#[inline(always)]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
impl fmt::Debug for Response {
#[inline(always)]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
impl fmt::Debug for AuthResult {
#[inline(always)]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
impl fmt::Debug for Pending {
#[inline(always)]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
#[inline(always)]
pub fn make_challenge(id: AliceId) -> Result<Pending> {
Ok(Pending {
id,
c: challenge()?,
})
}
#[inline(always)]
#[must_use]
pub fn check(p: &Pending, r: &Response) -> AuthResult {
verify32(&p.id, &p.c, &r.s).map_or(AuthResult::Fail, |()| {
rand::<16>().map_or(AuthResult::Fail, AuthResult::Ok)
})
}
#[inline(always)]
#[must_use]
pub fn hello(i: &Identity) -> Hello {
Hello { id: i.id(), v: 1 }
}
#[inline(always)]
#[must_use]
pub fn respond(i: &Identity, c: &Challenge) -> Response {
Response { s: i.sign32(&c.n) }
}