cascade: add secret types and bucket padding
Secrets never print and are wiped on drop (S8); padding hides sizes behind buckets and unpads strictly. Refs: R3-2, R3-8
This commit is contained in:
parent
f8c64390e3
commit
7776380a93
|
|
@ -140,6 +140,16 @@ dependencies = [
|
||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ccc-cascade"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"ccc-crypto-core",
|
||||||
|
"ccc-crypto-wolfssl",
|
||||||
|
"thiserror",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ccc-conformance-tests"
|
name = "ccc-conformance-tests"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = [
|
members = [
|
||||||
"crates/ccc-crypto-core",
|
"crates/ccc-crypto-core",
|
||||||
|
"crates/ccc-cascade",
|
||||||
"crates/ccc-crypto-wolfssl",
|
"crates/ccc-crypto-wolfssl",
|
||||||
"tests/conformance",
|
"tests/conformance",
|
||||||
]
|
]
|
||||||
|
|
@ -26,4 +27,5 @@ env_logger = "0.11"
|
||||||
|
|
||||||
# ccc-crypto-core is referenced by every crate in the workspace.
|
# ccc-crypto-core is referenced by every crate in the workspace.
|
||||||
ccc-crypto-core = { path = "crates/ccc-crypto-core" }
|
ccc-crypto-core = { path = "crates/ccc-crypto-core" }
|
||||||
|
ccc-cascade = { path = "crates/ccc-cascade" }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
[package]
|
||||||
|
name = "ccc-cascade"
|
||||||
|
version.workspace = true
|
||||||
|
edition.workspace = true
|
||||||
|
authors.workspace = true
|
||||||
|
license.workspace = true
|
||||||
|
description = "CCC v2 cascade: per-layer HKDF keys, rounds, salt, AAD binding, padding, STREAM mode, suites and policy."
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
ccc-crypto-core.workspace = true
|
||||||
|
zeroize.workspace = true
|
||||||
|
thiserror.workspace = true
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
ccc-crypto-wolfssl = { path = "../ccc-crypto-wolfssl" }
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
//! # ccc-cascade
|
||||||
|
//!
|
||||||
|
//! CCC v2 (design: "Cascade encryption: CCC v2", D-8, D-25). Nothing here is
|
||||||
|
//! specific to LetUsMsg and nothing does I/O.
|
||||||
|
#![forbid(unsafe_code)]
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
|
pub mod padding;
|
||||||
|
pub mod secret;
|
||||||
|
|
||||||
|
pub use padding::{pad, unpad, Bucketing};
|
||||||
|
pub use secret::{Key32, SecretBytes};
|
||||||
|
|
||||||
|
/// Errors from the cascade.
|
||||||
|
#[derive(Debug, Clone, thiserror::Error)]
|
||||||
|
pub enum CascadeError {
|
||||||
|
/// Malformed input.
|
||||||
|
#[error("invalid input: {0}")]
|
||||||
|
InvalidInput(&'static str),
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,96 @@
|
||||||
|
//! Bucket padding (design: "Record envelope" and the profile padding rows).
|
||||||
|
//!
|
||||||
|
//! Padded form: `u32be(len) || data || zeros`, with the total rounded up to a
|
||||||
|
//! bucket so sizes leak less. Unpadding is strict: the fill must be all zeros.
|
||||||
|
|
||||||
|
use crate::CascadeError;
|
||||||
|
|
||||||
|
/// How sizes are rounded.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct Bucketing {
|
||||||
|
/// Bucket step in bytes below `linear_limit`.
|
||||||
|
pub step: usize,
|
||||||
|
/// Up to this size, buckets are multiples of `step`; above it, powers of two.
|
||||||
|
pub linear_limit: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bucketing {
|
||||||
|
/// Records at rest: 256-byte steps up to 4 KiB, then powers of two.
|
||||||
|
pub const RECORDS: Bucketing = Bucketing { step: 256, linear_limit: 4096 };
|
||||||
|
/// Standard and the floor: 256-byte buckets.
|
||||||
|
pub const WIRE_256: Bucketing = Bucketing { step: 256, linear_limit: 4096 };
|
||||||
|
/// High: 1 KiB buckets.
|
||||||
|
pub const WIRE_1K: Bucketing = Bucketing { step: 1024, linear_limit: 4096 };
|
||||||
|
/// Ultra: one 4 KiB bucket for every message (plus random extra steps).
|
||||||
|
pub const WIRE_4K: Bucketing = Bucketing { step: 4096, linear_limit: 4096 };
|
||||||
|
|
||||||
|
/// The bucket size for `n` bytes (including the 4-byte length prefix).
|
||||||
|
pub fn bucket(&self, n: usize) -> usize {
|
||||||
|
let step = self.step.max(1);
|
||||||
|
if n <= self.linear_limit {
|
||||||
|
n.div_ceil(step).max(1) * step
|
||||||
|
} else {
|
||||||
|
n.next_power_of_two()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pad `data`. `extra_steps` adds that many more `step`s (Ultra's random
|
||||||
|
/// extra; the caller draws it from its RNG port).
|
||||||
|
pub fn pad(data: &[u8], b: Bucketing, extra_steps: usize) -> Result<Vec<u8>, CascadeError> {
|
||||||
|
let len = u32::try_from(data.len()).map_err(|_| CascadeError::InvalidInput("too large to pad"))?;
|
||||||
|
let total = b.bucket(data.len() + 4) + extra_steps * b.step;
|
||||||
|
let mut out = Vec::with_capacity(total);
|
||||||
|
out.extend_from_slice(&len.to_be_bytes());
|
||||||
|
out.extend_from_slice(data);
|
||||||
|
out.resize(total, 0);
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Undo [`pad`]. Fails if the length prefix or the zero fill is wrong.
|
||||||
|
pub fn unpad(padded: &[u8]) -> Result<&[u8], CascadeError> {
|
||||||
|
let bad = CascadeError::InvalidInput("bad padding");
|
||||||
|
let prefix: [u8; 4] = padded.get(..4).and_then(|p| p.try_into().ok()).ok_or(bad.clone())?;
|
||||||
|
let len = u32::from_be_bytes(prefix) as usize;
|
||||||
|
let end = 4usize.checked_add(len).filter(|e| *e <= padded.len()).ok_or(bad.clone())?;
|
||||||
|
if padded[end..].iter().any(|b| *b != 0) {
|
||||||
|
return Err(bad);
|
||||||
|
}
|
||||||
|
Ok(&padded[4..end])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn buckets() {
|
||||||
|
let r = Bucketing::RECORDS;
|
||||||
|
assert_eq!(r.bucket(1), 256);
|
||||||
|
assert_eq!(r.bucket(256), 256);
|
||||||
|
assert_eq!(r.bucket(257), 512);
|
||||||
|
assert_eq!(r.bucket(4096), 4096);
|
||||||
|
assert_eq!(r.bucket(4097), 8192);
|
||||||
|
assert_eq!(Bucketing::WIRE_4K.bucket(10), 4096);
|
||||||
|
assert_eq!(Bucketing::WIRE_1K.bucket(1500), 2048);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn round_trip_and_strict_unpad() {
|
||||||
|
for n in [0usize, 1, 251, 252, 253, 4092, 4093, 10_000] {
|
||||||
|
let data: Vec<u8> = (0..n).map(|i| i as u8).collect();
|
||||||
|
let p = pad(&data, Bucketing::RECORDS, 0).unwrap();
|
||||||
|
assert_eq!(p.len(), Bucketing::RECORDS.bucket(n + 4));
|
||||||
|
assert_eq!(unpad(&p).unwrap(), data.as_slice());
|
||||||
|
}
|
||||||
|
let p = pad(b"hi", Bucketing::WIRE_256, 2).unwrap();
|
||||||
|
assert_eq!(p.len(), 256 * 3);
|
||||||
|
let mut bad = p.clone();
|
||||||
|
*bad.last_mut().unwrap() = 1;
|
||||||
|
assert!(unpad(&bad).is_err(), "non-zero fill");
|
||||||
|
let mut bad = p.clone();
|
||||||
|
bad[..4].copy_from_slice(&10_000u32.to_be_bytes());
|
||||||
|
assert!(unpad(&bad).is_err(), "length past end");
|
||||||
|
assert!(unpad(&[0, 0]).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
//! Secret byte types (R3-8, S8): zeroized on drop and never printed.
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use zeroize::{Zeroize, Zeroizing};
|
||||||
|
|
||||||
|
/// Secret bytes. `Debug` prints only the length; the bytes are wiped on drop.
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct SecretBytes(Zeroizing<Vec<u8>>);
|
||||||
|
|
||||||
|
impl SecretBytes {
|
||||||
|
/// Take ownership of `bytes`.
|
||||||
|
pub fn new(bytes: Vec<u8>) -> Self {
|
||||||
|
Self(Zeroizing::new(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy `bytes` in; the caller should wipe its own copy.
|
||||||
|
pub fn from_slice(bytes: &[u8]) -> Self {
|
||||||
|
Self(Zeroizing::new(bytes.to_vec()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The secret bytes.
|
||||||
|
pub fn expose(&self) -> &[u8] {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Length in bytes.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.0.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether it is empty.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.0.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Zeroizing<Vec<u8>>> for SecretBytes {
|
||||||
|
fn from(z: Zeroizing<Vec<u8>>) -> Self {
|
||||||
|
Self(z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for SecretBytes {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "SecretBytes([REDACTED; {}])", self.0.len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A 32-byte secret key.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Key32([u8; 32]);
|
||||||
|
|
||||||
|
impl Key32 {
|
||||||
|
/// Wrap `bytes`.
|
||||||
|
pub fn new(bytes: [u8; 32]) -> Self {
|
||||||
|
Self(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The key bytes.
|
||||||
|
pub fn expose(&self) -> &[u8; 32] {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Key32 {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.0.zeroize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Key32 {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("Key32([REDACTED])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn debug_never_prints_secrets() {
|
||||||
|
let s = SecretBytes::from_slice(b"top secret value");
|
||||||
|
assert_eq!(format!("{s:?}"), "SecretBytes([REDACTED; 16])");
|
||||||
|
let k = Key32::new([0x41; 32]);
|
||||||
|
assert_eq!(format!("{k:?}"), "Key32([REDACTED])");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue