use core::fmt;
use std::time::Duration;
pub(crate) const WG_LABEL_MAC1: &[u8] = b"mac1----";
pub(crate) const WG_LABEL_COOKIE: &[u8] = b"cookie--";
pub(crate) const NOISE_CONSTRUCTION: &[u8] = b"Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s";
pub(crate) const WG_IDENTIFIER: &[u8] = b"WireGuard v1 zx2c4 Jason@zx2c4.com";
pub(crate) const TAI64N_TIMESTAMP_SIZE: usize = 12;
pub(crate) const BLAKE2S_128_SIZE: usize = 16;
pub(crate) const BLAKE2S_256_SIZE: usize = 32;
pub(crate) const CHACHAPOLY_OVERHEAD: usize = 16;
#[allow(dead_code)]
pub(crate) const CHACHAPOLY_NONCE_SIZE: usize = 12;
#[allow(dead_code)]
pub(crate) const XCHACHAPOLY_NONCE_SIZE: usize = 24;
pub(crate) const CHACHAPOLY_KEY_SIZE: usize = 32;
pub(crate) const MESSAGE_INITIATION_TYPE: u32 = 1;
pub(crate) const MESSAGE_RESPONSE_TYPE: u32 = 2;
pub(crate) const MESSAGE_COOKIE_REPLY_TYPE: u32 = 3;
pub(crate) const MESSAGE_TRANSPORT_TYPE: u32 = 4;
pub(crate) const MESSAGE_INITIATION_SIZE: usize = 148;
pub(crate) const MESSAGE_RESPONSE_SIZE: usize = 92;
pub(crate) const MESSAGE_COOKIE_REPLY_SIZE: usize = 64;
pub(crate) const MESSAGE_TRANSPORT_HEADER_SIZE: usize = 16;
pub const COOKIE_REFRESH_TIME: Duration = Duration::from_secs(120);
pub const REJECT_AFTER_TIME: Duration = Duration::from_secs(180);
pub const REKEY_AFTER_MESSAGES: u64 = 1u64 << 60;
pub const REJECT_AFTER_MESSAGES: u64 = u64::MAX - (1u64 << 13);
pub const REKEY_AFTER_TIME: Duration = Duration::from_secs(120);
pub(crate) const DEFAULT_LOAD_THRESHOLD: usize = 20;
pub const WINDOW_SIZE: usize = 8192;
pub(crate) const MAX_HANDSHAKES: usize = 10000;
pub(crate) const MAX_SESSIONS: usize = 10000;
pub const NOISE_PUBLIC_KEY_SIZE: usize = 32;
pub const NOISE_PRIVATE_KEY_SIZE: usize = 32;
pub const NOISE_PRESHARED_KEY_SIZE: usize = 32;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoisePublicKey(pub [u8; NOISE_PUBLIC_KEY_SIZE]);
impl NoisePublicKey {
#[inline]
pub const fn zero() -> Self {
Self([0u8; NOISE_PUBLIC_KEY_SIZE])
}
#[inline]
pub const fn as_bytes(&self) -> &[u8; NOISE_PUBLIC_KEY_SIZE] {
&self.0
}
#[inline]
pub fn is_zero(&self) -> bool {
self.0 == [0u8; NOISE_PUBLIC_KEY_SIZE]
}
}
impl From<[u8; NOISE_PUBLIC_KEY_SIZE]> for NoisePublicKey {
fn from(b: [u8; NOISE_PUBLIC_KEY_SIZE]) -> Self {
Self(b)
}
}
impl fmt::Debug for NoisePublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let b = &self.0;
write!(
f,
"NoisePublicKey({:02x}{:02x}{:02x}{:02x}...)",
b[0], b[1], b[2], b[3]
)
}
}
#[derive(Clone, Default)]
pub struct NoisePrivateKey(pub [u8; NOISE_PRIVATE_KEY_SIZE]);
impl NoisePrivateKey {
#[inline]
pub const fn zero() -> Self {
Self([0u8; NOISE_PRIVATE_KEY_SIZE])
}
#[inline]
pub const fn as_bytes(&self) -> &[u8; NOISE_PRIVATE_KEY_SIZE] {
&self.0
}
#[inline]
pub fn is_zero(&self) -> bool {
self.0 == [0u8; NOISE_PRIVATE_KEY_SIZE]
}
}
impl From<[u8; NOISE_PRIVATE_KEY_SIZE]> for NoisePrivateKey {
fn from(b: [u8; NOISE_PRIVATE_KEY_SIZE]) -> Self {
Self(b)
}
}
impl fmt::Debug for NoisePrivateKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("NoisePrivateKey(<redacted>)")
}
}
impl Drop for NoisePrivateKey {
fn drop(&mut self) {
crate::zeroize::zeroize(&mut self.0);
}
}
#[derive(Clone)]
pub struct NoisePresharedKey(pub [u8; NOISE_PRESHARED_KEY_SIZE]);
impl NoisePresharedKey {
#[inline]
pub const fn zero() -> Self {
Self([0u8; NOISE_PRESHARED_KEY_SIZE])
}
#[inline]
pub const fn as_bytes(&self) -> &[u8; NOISE_PRESHARED_KEY_SIZE] {
&self.0
}
#[inline]
pub fn is_zero(&self) -> bool {
self.0 == [0u8; NOISE_PRESHARED_KEY_SIZE]
}
}
impl From<[u8; NOISE_PRESHARED_KEY_SIZE]> for NoisePresharedKey {
fn from(b: [u8; NOISE_PRESHARED_KEY_SIZE]) -> Self {
Self(b)
}
}
impl fmt::Debug for NoisePresharedKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("NoisePresharedKey(<redacted>)")
}
}
impl Drop for NoisePresharedKey {
fn drop(&mut self) {
crate::zeroize::zeroize(&mut self.0);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(dead_code)]
pub(crate) enum HandshakeState {
Zeroed,
InitiationCreated,
ResponseCreated,
}