#![allow(non_snake_case)]
pub(crate) use spideroak_crypto::hpke::Mode;
use spideroak_crypto::{
hpke::{self, HpkeError},
kem::Kem,
};
use crate::{Csprng, ciphersuite::CipherSuite};
type Hpke<CS> =
hpke::Hpke<<CS as CipherSuite>::Kem, <CS as CipherSuite>::Kdf, <CS as CipherSuite>::Aead>;
type DecapKey<CS> = <<CS as CipherSuite>::Kem as Kem>::DecapKey;
type EncapKey<CS> = <<CS as CipherSuite>::Kem as Kem>::EncapKey;
type Encap<CS> = <<CS as CipherSuite>::Kem as Kem>::Encap;
type SendCtx<CS> =
hpke::SendCtx<<CS as CipherSuite>::Kem, <CS as CipherSuite>::Kdf, <CS as CipherSuite>::Aead>;
type RecvCtx<CS> =
hpke::RecvCtx<<CS as CipherSuite>::Kem, <CS as CipherSuite>::Kdf, <CS as CipherSuite>::Aead>;
#[inline(always)]
fn wrap_info<'a, CS>(info: impl IntoIterator<Item = &'a [u8]>) -> impl IntoIterator<Item = &'a [u8]>
where
CS: CipherSuite,
{
info.into_iter().chain(
#[allow(clippy::map_identity)]
CS::OIDS.encode().into_iter().map(|v| v),
)
}
#[allow(clippy::type_complexity)]
pub(crate) fn setup_send<'a, CS, R>(
rng: R,
mode: Mode<'_, &DecapKey<CS>>,
pkR: &EncapKey<CS>,
info: impl IntoIterator<Item = &'a [u8]>,
) -> Result<(Encap<CS>, SendCtx<CS>), HpkeError>
where
CS: CipherSuite,
R: Csprng,
{
Hpke::<CS>::setup_send(rng, mode, pkR, wrap_info::<CS>(info))
}
#[cfg(feature = "afc")]
#[allow(clippy::type_complexity)]
pub(crate) fn setup_send_deterministically<'a, CS>(
mode: Mode<'_, &DecapKey<CS>>,
pkR: &EncapKey<CS>,
info: impl IntoIterator<Item = &'a [u8]>,
skE: DecapKey<CS>,
) -> Result<(Encap<CS>, SendCtx<CS>), HpkeError>
where
CS: CipherSuite,
{
Hpke::<CS>::setup_send_deterministically(mode, pkR, wrap_info::<CS>(info), skE)
}
pub(crate) fn setup_recv<'a, CS>(
mode: Mode<'_, &EncapKey<CS>>,
enc: &Encap<CS>,
skR: &DecapKey<CS>,
info: impl IntoIterator<Item = &'a [u8]>,
) -> Result<RecvCtx<CS>, HpkeError>
where
CS: CipherSuite,
{
Hpke::<CS>::setup_recv(mode, enc, skR, wrap_info::<CS>(info))
}