[domistream] Implementation: HTTP, pairing, RTSP/SDP, ENet, control, input, RTP, FEC, video and audio receivers
Per PROTOCOL.md and the Wolf docs; interop with a real host is unverified. Strict, capped parsers throughout, shared helpers in util.rs. Where PROTOCOL.md says UNKNOWN the Wolf-documented behaviour is used. The audio receiver waits for the jitter budget before rebuilding a missing packet, so a late packet beats a rebuilt one.
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
//! The pure cryptographic steps of pairing (`docs/PROTOCOL.md` 1.5), public so QA can test them against
|
||||
//! independent vectors (for example Python `hashlib` + `cryptography` with fixed test keys).
|
||||
|
||||
use super::{CHALLENGE_LEN, HASH_LEN, Pin, SALT_LEN, SECRET_LEN};
|
||||
use aes::Aes128;
|
||||
use aes::cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use super::{CHALLENGE_LEN, HASH_LEN, MAX_SIGNATURE_BYTES, Pin, SALT_LEN, SECRET_LEN};
|
||||
use crate::Result;
|
||||
use crate::util::{cap, malformed};
|
||||
|
||||
/// AES-128 key used for every encrypted pairing value.
|
||||
pub type PairingKey = [u8; 16];
|
||||
@@ -10,20 +15,40 @@ pub type PairingKey = [u8; 16];
|
||||
/// `K = SHA-256(salt ‖ PIN digits)[0..16]`.
|
||||
///
|
||||
/// # Errors
|
||||
/// Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// None today; the `Result` keeps the signature uniform with the other steps.
|
||||
pub fn derive_key(salt: &[u8; SALT_LEN], pin: &Pin) -> Result<PairingKey> {
|
||||
let _ = (salt, pin);
|
||||
Err(crate::Error::Unimplemented)
|
||||
let mut h = Sha256::new();
|
||||
h.update(salt);
|
||||
h.update(pin.as_bytes());
|
||||
let digest = h.finalize();
|
||||
let mut key = [0u8; 16];
|
||||
key.copy_from_slice(digest.get(..16).ok_or(malformed("sha-256 output"))?);
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
fn ecb(key: &PairingKey, data: &[u8], encrypt: bool) -> Result<Vec<u8>> {
|
||||
if data.is_empty() || !data.len().is_multiple_of(16) {
|
||||
return Err(malformed("aes-ecb input is not whole blocks"));
|
||||
}
|
||||
let cipher = Aes128::new(key.into());
|
||||
let mut out = data.to_vec();
|
||||
for chunk in out.chunks_exact_mut(16) {
|
||||
let block: &mut aes::Block = chunk.try_into().map_err(|_| malformed("aes block"))?;
|
||||
if encrypt {
|
||||
cipher.encrypt_block(block);
|
||||
} else {
|
||||
cipher.decrypt_block(block);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// AES-128-ECB encryption without padding.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Malformed`] when `data` is not a whole number of 16-byte blocks. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::Malformed`] when `data` is not a whole, non-zero number of 16-byte blocks.
|
||||
pub fn ecb_encrypt(key: &PairingKey, data: &[u8]) -> Result<Vec<u8>> {
|
||||
let _ = (key, data);
|
||||
Err(crate::Error::Unimplemented)
|
||||
ecb(key, data, true)
|
||||
}
|
||||
|
||||
/// AES-128-ECB decryption without padding.
|
||||
@@ -31,8 +56,7 @@ pub fn ecb_encrypt(key: &PairingKey, data: &[u8]) -> Result<Vec<u8>> {
|
||||
/// # Errors
|
||||
/// As [`ecb_encrypt`].
|
||||
pub fn ecb_decrypt(key: &PairingKey, data: &[u8]) -> Result<Vec<u8>> {
|
||||
let _ = (key, data);
|
||||
Err(crate::Error::Unimplemented)
|
||||
ecb(key, data, false)
|
||||
}
|
||||
|
||||
/// `SHA-256(challenge ‖ cert_signature ‖ secret)`: the host's proof `H_s` (with the client challenge, the host
|
||||
@@ -40,32 +64,46 @@ pub fn ecb_decrypt(key: &PairingKey, data: &[u8]) -> Result<Vec<u8>> {
|
||||
/// certificate's signature and the client secret).
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] for a signature over [`super::MAX_SIGNATURE_BYTES`]. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::TooLarge`] for a signature over [`MAX_SIGNATURE_BYTES`].
|
||||
pub fn proof_hash(
|
||||
challenge: &[u8; CHALLENGE_LEN],
|
||||
cert_signature: &[u8],
|
||||
secret: &[u8; SECRET_LEN],
|
||||
) -> Result<[u8; HASH_LEN]> {
|
||||
let _ = (challenge, cert_signature, secret);
|
||||
Err(crate::Error::Unimplemented)
|
||||
cap("certificate signature", MAX_SIGNATURE_BYTES, cert_signature.len())?;
|
||||
let mut h = Sha256::new();
|
||||
h.update(challenge);
|
||||
h.update(cert_signature);
|
||||
h.update(secret);
|
||||
let mut out = [0u8; HASH_LEN];
|
||||
out.copy_from_slice(&h.finalize());
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Splits a decrypted `challengeresponse` (48 bytes) into the host proof `H_s` and the host challenge `S_c`.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Malformed`] unless exactly 48 bytes. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::Malformed`] unless exactly 48 bytes.
|
||||
pub fn split_challenge_response(decrypted: &[u8]) -> Result<([u8; HASH_LEN], [u8; CHALLENGE_LEN])> {
|
||||
let _ = decrypted;
|
||||
Err(crate::Error::Unimplemented)
|
||||
if decrypted.len() != HASH_LEN + CHALLENGE_LEN {
|
||||
return Err(malformed("challengeresponse is not 48 bytes"));
|
||||
}
|
||||
let hash = crate::util::array::<HASH_LEN>("challengeresponse", decrypted, 0)?;
|
||||
let challenge = crate::util::array::<CHALLENGE_LEN>("challengeresponse", decrypted, HASH_LEN)?;
|
||||
Ok((hash, challenge))
|
||||
}
|
||||
|
||||
/// Splits a `pairingsecret` into the host secret (16 bytes) and its signature (the rest).
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Malformed`] when shorter than 17 bytes, [`crate::Error::TooLarge`] when the signature is over
|
||||
/// [`super::MAX_SIGNATURE_BYTES`]. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`MAX_SIGNATURE_BYTES`].
|
||||
pub fn split_pairing_secret(pairing_secret: &[u8]) -> Result<([u8; SECRET_LEN], &[u8])> {
|
||||
let _ = pairing_secret;
|
||||
Err(crate::Error::Unimplemented)
|
||||
if pairing_secret.len() <= SECRET_LEN {
|
||||
return Err(malformed("pairingsecret too short"));
|
||||
}
|
||||
let signature = pairing_secret.get(SECRET_LEN..).unwrap_or(&[]);
|
||||
cap("pairingsecret signature", MAX_SIGNATURE_BYTES, signature.len())?;
|
||||
let secret = crate::util::array::<SECRET_LEN>("pairingsecret", pairing_secret, 0)?;
|
||||
Ok((secret, signature))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user