use core::fmt;
use rand::CryptoRng;
use rand::RngCore;
use rings_aez::Aez;
use rings_aez::KeyError;
use rings_aez::KEY_BYTES;
use subtle::ConstantTimeEq;
use zeroize::Zeroize;
use zeroize::ZeroizeOnDrop;
use super::hkdf_expand;
use super::hkdf_extract;
use crate::onion::loop_shape::ONION_SEGMENT_RELAYS;
pub(crate) const ONION_CARRY_SEED_BYTES: usize = 32;
pub(super) const SEGMENT_SEED_DRAWS: usize = 4;
const RELAY_INDICES: [u8; ONION_SEGMENT_RELAYS] = [1, 2];
const SEED_SALT: &[u8] = b"rings-node:onion-carry-seed";
const KEY_SALT: &[u8] = b"rings-node:onion-carry-key";
const RELAY_INFO: &[u8] = b"relay";
const CONSUMER_INFO: &[u8] = b"consumer";
const KEY_INFO: &[u8] = b"aez";
macro_rules! onion_seed {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Zeroize, ZeroizeOnDrop)]
pub(crate) struct $name([u8; ONION_CARRY_SEED_BYTES]);
impl $name {
pub(crate) const fn new(bytes: [u8; ONION_CARRY_SEED_BYTES]) -> Self {
Self(bytes)
}
pub(crate) const fn as_bytes(&self) -> &[u8; ONION_CARRY_SEED_BYTES] {
&self.0
}
}
impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
self.0.ct_eq(&other.0).into()
}
}
impl Eq for $name {}
impl fmt::Debug for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(concat!(stringify!($name), "(..)"))
}
}
};
}
onion_seed! {
OnionCarrySeed
}
onion_seed! {
OnionSegmentSeed
}
pub(crate) struct OnionSegmentSeeds {
pub(crate) relays: [OnionCarrySeed; ONION_SEGMENT_RELAYS],
pub(crate) consumer: OnionCarrySeed,
}
pub(crate) struct OnionCarryKey(Aez);
pub(crate) struct OnionSegmentKeys {
relays: [OnionCarryKey; ONION_SEGMENT_RELAYS],
consumer: OnionCarryKey,
}
impl OnionCarrySeed {
pub(crate) fn key(&self) -> Result<OnionCarryKey, KeyError> {
let key = hkdf_expand::<KEY_BYTES>(&hkdf_extract(KEY_SALT, &[&self.0]), &[KEY_INFO]);
Aez::new(&key).map(OnionCarryKey)
}
}
impl OnionSegmentSeed {
pub(crate) fn random(rng: &mut (impl CryptoRng + RngCore)) -> Self {
let mut seed = Self([0; ONION_CARRY_SEED_BYTES]);
rng.fill_bytes(&mut seed.0);
seed
}
pub(crate) fn draw(
rng: &mut (impl CryptoRng + RngCore),
) -> Result<(Self, OnionSegmentKeys), KeyError> {
first_strong(|| Self::random(rng), Self::keys)
}
pub(crate) fn seeds(&self) -> OnionSegmentSeeds {
let kdf = hkdf_extract(SEED_SALT, &[&self.0]);
OnionSegmentSeeds {
relays: RELAY_INDICES.map(|relay| {
OnionCarrySeed(*hkdf_expand::<ONION_CARRY_SEED_BYTES>(&kdf, &[
RELAY_INFO,
&[relay],
]))
}),
consumer: OnionCarrySeed(*hkdf_expand::<ONION_CARRY_SEED_BYTES>(&kdf, &[
CONSUMER_INFO,
])),
}
}
pub(crate) fn keys(&self) -> Result<OnionSegmentKeys, KeyError> {
let seeds = self.seeds();
let [first, second] = seeds.relays.each_ref().map(OnionCarrySeed::key);
Ok(OnionSegmentKeys {
relays: [first?, second?],
consumer: seeds.consumer.key()?,
})
}
}
pub(super) fn first_strong<C, K>(
mut draw: impl FnMut() -> C,
derive: impl Fn(&C) -> Result<K, KeyError>,
) -> Result<(C, K), KeyError> {
let mut attempt = || {
let candidate = draw();
derive(&candidate).map(|derived| (candidate, derived))
};
(1..SEGMENT_SEED_DRAWS).fold(attempt(), |drawn, _| drawn.or_else(|_| attempt()))
}
impl OnionCarryKey {
pub(super) const fn aez(&self) -> &Aez {
&self.0
}
}
impl OnionSegmentKeys {
pub(crate) const fn relays(&self) -> &[OnionCarryKey; ONION_SEGMENT_RELAYS] {
&self.relays
}
pub(crate) const fn consumer(&self) -> &OnionCarryKey {
&self.consumer
}
}