Files
CustosPublic/crates/custos-seal/tests/mandatory.rs
T

75 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Mandatory tests for sealed boxes (docs/SEAL.md).
use custos_seal::{Recipient, SealError, match_code, open_at_rest, seal, seal_at_rest, sender_code};
#[test]
fn only_the_recipient_opens_and_only_for_its_purpose() {
let r = Recipient::generate();
let other = Recipient::generate();
let b = seal(&r.public(), b"approval:f1:r1", b"factor material").unwrap();
assert_eq!(r.open(&b, b"approval:f1:r1").unwrap().as_slice(), b"factor material");
assert_eq!(other.open(&b, b"approval:f1:r1"), Err(SealError::Rejected));
// The same box for another request or factor does not open.
assert_eq!(r.open(&b, b"approval:f1:r2"), Err(SealError::Rejected));
// Two seals of the same secret differ (fresh ephemeral key and nonce).
assert_ne!(b, seal(&r.public(), b"approval:f1:r1", b"factor material").unwrap());
assert!(!b.windows(15).any(|w| w == b"factor material"));
}
#[test]
fn every_changed_byte_is_refused() {
let r = Recipient::generate();
let b = seal(&r.public(), b"c", b"secret").unwrap();
for i in 0..b.len() {
let mut bad = b.clone();
bad[i] ^= 1;
assert!(r.open(&bad, b"c").is_err(), "byte {i}");
}
assert_eq!(r.open(&b[..b.len() - 1], b"c"), Err(SealError::Rejected));
assert_eq!(r.open(&b[..20], b"c"), Err(SealError::Malformed));
}
#[test]
fn bad_public_keys_are_refused() {
assert_eq!(seal(&[1u8; 31], b"c", b"s"), Err(SealError::BadKey));
// The all-zero point (low order) would give a shared secret everyone knows.
assert_eq!(seal(&[0u8; 32], b"c", b"s"), Err(SealError::BadKey));
assert_eq!(seal(&Recipient::generate().public(), b"c", &vec![0u8; 40_000]), Err(SealError::TooLarge));
}
#[test]
fn the_match_code_is_six_digits_and_follows_the_key() {
let r = Recipient::generate();
assert_eq!(r.match_code(), match_code(&r.public()));
assert_eq!(r.match_code().len(), 6);
assert!(r.match_code().bytes().all(|b| b.is_ascii_digit()));
// A known vector, so every implementation (the web app's wasm too) shows the same digits.
assert_eq!(match_code(&[7u8; 32]), KNOWN_MATCH_CODE);
let codes: std::collections::HashSet<String> = (0..50).map(|_| Recipient::generate().match_code()).collect();
assert!(codes.len() > 45);
assert!(!format!("{r:?}").contains(&format!("{:?}", r.public())));
}
/// SHA-256("custos match v1" ‖ 0x07 × 32), first four bytes big-endian, mod 1 000 000.
const KNOWN_MATCH_CODE: &str = "684361";
#[test]
fn the_sender_code_differs_per_box_and_needs_a_box() {
let r = Recipient::generate();
let a = seal(&r.public(), b"pair:1", b"x").unwrap();
let b = seal(&r.public(), b"pair:1", b"x").unwrap();
assert_eq!(sender_code(&a).unwrap(), match_code(&a[4..36]));
assert_ne!(a[4..36], b[4..36], "a fresh one-time key per box");
assert_eq!(sender_code(&a[..40]), Err(SealError::Malformed));
}
#[test]
fn at_rest_seals_open_with_the_same_key_and_context_only() {
let key = [9u8; 32];
let b = seal_at_rest(&key, b"totp:f1", b"share");
assert_eq!(open_at_rest(&key, b"totp:f1", &b).unwrap().as_slice(), b"share");
assert_eq!(open_at_rest(&[8u8; 32], b"totp:f1", &b), Err(SealError::Rejected));
assert_eq!(open_at_rest(&key, b"totp:f2", &b), Err(SealError::Rejected));
assert_eq!(open_at_rest(&key, b"totp:f1", b"CSR1"), Err(SealError::Malformed));
}