[domistream] Control API: AES-GCM frame, control messages, control stream
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
//! The AES-128-GCM frame around every control message (`docs/PROTOCOL.md` 3.2).
|
||||
//!
|
||||
//! ```text
|
||||
//! 0 2 4 8 24
|
||||
//! | 0x0001 | len | seq (LE) | GCM tag (16) | ciphertext … | len = 4 + 16 + ciphertext
|
||||
//! nonce = seq (LE, 4) ‖ 0 × 6 ‖ direction ('C' client→host, 'H' host→client) ‖ 'C'
|
||||
//! ```
|
||||
|
||||
use core::fmt;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
/// Type field of an encrypted frame.
|
||||
pub const ENCRYPTED_FRAME_TYPE: u16 = 0x0001;
|
||||
/// GCM tag length.
|
||||
pub const GCM_TAG_LEN: usize = 16;
|
||||
/// Bytes before the ciphertext (type, length, sequence, tag).
|
||||
pub const FRAME_HEADER_LEN: usize = 2 + 2 + 4 + GCM_TAG_LEN;
|
||||
/// Smallest valid frame: header plus a 4-byte message header inside.
|
||||
pub const MIN_FRAME_BYTES: usize = FRAME_HEADER_LEN + 4;
|
||||
/// Maximum frame size accepted or produced, in bytes.
|
||||
pub const MAX_FRAME_BYTES: usize = 4096;
|
||||
/// GCM nonce length.
|
||||
pub const NONCE_LEN: usize = 12;
|
||||
|
||||
/// Which way a frame travels; selects byte 10 of the nonce.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Direction {
|
||||
/// Client to host (`'C'`).
|
||||
ClientToHost,
|
||||
/// Host to client (`'H'`).
|
||||
HostToClient,
|
||||
}
|
||||
|
||||
/// The nonce for sequence number `sequence` in `direction`.
|
||||
///
|
||||
/// # Errors
|
||||
/// Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn nonce(sequence: u32, direction: Direction) -> Result<[u8; NONCE_LEN]> {
|
||||
let _ = (sequence, direction);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// A frame split into its fields, not yet decrypted.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct EncryptedFrame<'a> {
|
||||
/// Sequence number.
|
||||
pub sequence: u32,
|
||||
/// GCM tag.
|
||||
pub tag: [u8; GCM_TAG_LEN],
|
||||
/// Ciphertext.
|
||||
pub ciphertext: &'a [u8],
|
||||
}
|
||||
|
||||
/// Splits a frame (one ENet packet) into its fields. The length field must match the bytes present exactly.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] over [`MAX_FRAME_BYTES`], [`crate::Error::Truncated`] under [`MIN_FRAME_BYTES`],
|
||||
/// [`crate::Error::UnexpectedValue`] for a type other than [`ENCRYPTED_FRAME_TYPE`], [`crate::Error::Malformed`]
|
||||
/// for a length that does not match. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn parse_encrypted_frame(frame: &[u8]) -> Result<EncryptedFrame<'_>> {
|
||||
let _ = frame;
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// AES-128-GCM with the session's `rikey`. `Debug` never prints the key.
|
||||
#[derive(Clone)]
|
||||
pub struct ControlCipher {
|
||||
key: [u8; 16],
|
||||
}
|
||||
|
||||
impl ControlCipher {
|
||||
/// A cipher for `rikey`.
|
||||
#[must_use]
|
||||
pub const fn new(rikey: [u8; 16]) -> Self {
|
||||
Self { key: rikey }
|
||||
}
|
||||
|
||||
/// Encrypts one message (plaintext = type, length, body) into a whole frame.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] when the frame would exceed [`MAX_FRAME_BYTES`]. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
pub fn seal(&self, sequence: u32, direction: Direction, plaintext: &[u8]) -> Result<Vec<u8>> {
|
||||
let _ = (sequence, direction, plaintext, &self.key);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Checks and decrypts one frame; returns its sequence number and the plaintext message.
|
||||
///
|
||||
/// # Errors
|
||||
/// Parser errors as [`parse_encrypted_frame`]; [`crate::Error::Crypto`] when the tag does not verify. Phase A:
|
||||
/// always [`crate::Error::Unimplemented`].
|
||||
pub fn open(&self, direction: Direction, frame: &[u8]) -> Result<(u32, Vec<u8>)> {
|
||||
let _ = (direction, frame);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for ControlCipher {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("ControlCipher(<redacted>)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user