use super::{Options, OreAes128Bit6Chained, Padding, VarCipherText};
use crate::testing::{i128_pt, salt, u128_pt};
use crate::OreScheme;
use std::cmp::Ordering;
use vitaminc_protected::Protected;
const K1: [u8; 16] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
];
const FIXED: Padding = Padding::Fixed {
max_bytes: std::num::NonZeroU16::new(40).unwrap(),
};
type C = OreAes128Bit6Chained;
fn seeded(seed: u8, options: Options) -> C {
C::init_seeded(Protected::new(K1), options, [seed; 32]).unwrap()
}
fn cipher_a() -> C {
seeded(0x2a, Options::default())
}
fn cipher_b() -> C {
seeded(0x77, Options::default())
}
fn cipher_left() -> C {
C::init(Protected::new(K1)).unwrap()
}
const SALTS: &[&[u8]] = &[b"age", b"users.dt", b"public.users.date_of_birth"];
fn cipher_a_salted(s: &[u8]) -> C {
seeded(0x2a, Options::default().with_salt(&salt(s)))
}
fn cipher_left_salted(s: &[u8]) -> C {
C::init_with_salt(Protected::new(K1), &salt(s)).unwrap()
}
fn cipher_a_with(padding: Padding) -> C {
seeded(0x2a, Options::default().with_padding(padding))
}
fn cipher_left_with(padding: Padding) -> C {
C::init_with(Protected::new(K1), Options::default().with_padding(padding)).unwrap()
}
type Pinned = &'static [(&'static str, &'static str)];
fn other_policies() -> [(Padding, Pinned, Pinned); 2] {
[
(Padding::None, LEFT_STR_UNPADDED, FULL_STR_UNPADDED),
(FIXED, LEFT_STR_FIXED, FULL_STR_FIXED),
]
}
const STRINGS: &[&str] = &[
"",
"a",
"alice",
"alice@example.com",
"alice@example.com/0123456789abcd",
"alice@example.com/0123456789abcde",
"alicf",
"bob",
];
const U128S: &[u128] = &[0, 1, 0x0123_4567_89ab_cdef_fedc_ba98_7654_3210, u128::MAX];
const I128S: &[i128] = &[i128::MIN, -1, 0, 1, i128::MAX];
#[path = "compat_vectors_data.rs"]
mod vectors;
use vectors::*;
fn left_u128(ore: &C, x: u128) -> Vec<u8> {
ore.encrypt_query_plaintext(&u128_pt(x))
.unwrap()
.into_bytes()
}
fn left_i128(ore: &C, x: i128) -> Vec<u8> {
ore.encrypt_query_plaintext(&i128_pt(x))
.unwrap()
.into_bytes()
}
fn full_u128(ore: &C, x: u128) -> Vec<u8> {
ore.encrypt_plaintext(&u128_pt(x)).unwrap().into_bytes()
}
fn full_i128(ore: &C, x: i128) -> Vec<u8> {
ore.encrypt_plaintext(&i128_pt(x)).unwrap().into_bytes()
}
#[test]
fn salted_alice_vectors() {
assert_eq!(SALTED_ALICE.len(), SALTS.len());
for (s, (pinned_salt, left, full)) in SALTS.iter().zip(SALTED_ALICE) {
assert_eq!(s, pinned_salt);
let left_ct = cipher_left_salted(s).encrypt_left_str("alice").unwrap();
assert_eq!(
hex::encode(left_ct.to_bytes()),
*left,
"left ciphertext mismatch for salt {s:?}"
);
let ct = cipher_a_salted(s).encrypt_str("alice").unwrap();
assert_eq!(
hex::encode(ct.to_bytes()),
*full,
"full ciphertext mismatch for salt {s:?}"
);
}
}
#[test]
fn left_str_vectors() {
let ore = cipher_left();
for (s, expected) in LEFT_STR {
let left = ore.encrypt_left_str(s).unwrap();
assert_eq!(
hex::encode(left.to_bytes()),
*expected,
"left ciphertext mismatch for {s:?}"
);
}
}
#[test]
fn left_str_vectors_other_policies() {
for (padding, lefts, _) in other_policies() {
let ore = cipher_left_with(padding);
for (s, expected) in lefts {
let left = ore.encrypt_left_str(s).unwrap();
assert_eq!(
hex::encode(left.to_bytes()),
*expected,
"left ciphertext mismatch for {s:?} under {padding:?}"
);
}
}
}
#[test]
fn full_str_vectors() {
for (s, expected) in FULL_STR {
let ct = cipher_a().encrypt_str(s).unwrap();
assert_eq!(
hex::encode(ct.to_bytes()),
*expected,
"full ciphertext mismatch for {s:?}"
);
}
}
#[test]
fn full_str_vectors_other_policies() {
for (padding, _, fulls) in other_policies() {
for (s, expected) in fulls {
let ct = cipher_a_with(padding).encrypt_str(s).unwrap();
assert_eq!(
hex::encode(ct.to_bytes()),
*expected,
"full ciphertext mismatch for {s:?} under {padding:?}"
);
}
}
}
#[test]
fn left_u128_vectors() {
let ore = cipher_left();
for (x, expected) in LEFT_U128 {
let left = left_u128(&ore, *x);
assert_eq!(hex::encode(&left), *expected, "left mismatch for {x}");
assert_eq!(left.len(), super::left_len(22));
}
for (x, expected) in LEFT_I128 {
let left = left_i128(&ore, *x);
assert_eq!(hex::encode(&left), *expected, "left mismatch for {x}");
assert_eq!(left.len(), super::left_len(22));
}
}
#[test]
fn full_u128_vectors() {
for (x, expected) in FULL_U128 {
let ct = full_u128(&cipher_a(), *x);
assert_eq!(hex::encode(&ct), *expected, "full mismatch for {x}");
assert_eq!(ct.len(), super::total_len(22));
}
for (x, expected) in FULL_I128 {
let ct = full_i128(&cipher_a(), *x);
assert_eq!(hex::encode(&ct), *expected, "full mismatch for {x}");
assert_eq!(ct.len(), super::total_len(22));
}
}
#[test]
fn compare_u128_vectors_matches_integer_order() {
for (xa, ha) in FULL_U128 {
for (xb, hb) in FULL_U128 {
assert_eq!(
C::compare_raw_slices(&pinned(ha), &pinned(hb)),
Some(xa.cmp(xb)),
"u128 {xa} vs {xb}"
);
}
for (xl, hl) in LEFT_U128 {
assert_eq!(
C::compare_left_to_full(&pinned(hl), &pinned(ha)),
Some(xl.cmp(xa)),
"u128 left {xl} vs {xa}"
);
}
let ct = VarCipherText::from_slice(&pinned(ha)).unwrap();
assert_eq!(ct.to_bytes(), pinned(ha));
}
for (xa, ha) in FULL_I128 {
for (xb, hb) in FULL_I128 {
assert_eq!(
C::compare_raw_slices(&pinned(ha), &pinned(hb)),
Some(xa.cmp(xb)),
"i128 {xa} vs {xb}"
);
}
for (xl, hl) in LEFT_I128 {
assert_eq!(
C::compare_left_to_full(&pinned(hl), &pinned(ha)),
Some(xl.cmp(xa)),
"i128 left {xl} vs {xa}"
);
}
}
}
#[test]
fn block_counts_are_lengths_not_types() {
let n = pinned(FULL_U128[0].1);
let (alice, unpadded) = FULL_STR_UNPADDED
.iter()
.find(|(s, _)| *s == "alice")
.map(|(s, h)| (*s, pinned(h)))
.unwrap();
assert_eq!(alice, "alice");
assert!(C::compare_raw_slices(&n, &unpadded).is_some());
let padded = pinned(full_alice());
assert_eq!(C::compare_raw_slices(&n, &padded), None);
}
#[test]
fn full_alice_alternate_nonce() {
let ct = cipher_b().encrypt_str("alice").unwrap();
assert_eq!(hex::encode(ct.to_bytes()), FULL_ALICE_SEED_B);
}
#[test]
fn full_ciphertext_starts_with_the_left_ciphertext() {
let mut sets = vec![(LEFT_STR, FULL_STR)];
sets.extend(other_policies().iter().map(|&(_, l, f)| (l, f)));
for (lefts, fulls) in sets {
for ((s, left), (s2, full)) in lefts.iter().zip(fulls.iter()) {
assert_eq!(s, s2);
assert!(full.starts_with(left), "left prefix mismatch for {:?}", s);
}
}
}
fn pinned(hex_str: &str) -> Vec<u8> {
hex::decode(hex_str).unwrap()
}
#[test]
fn compare_raw_slices_matches_plaintext_order() {
for (sa, ha) in FULL_STR {
for (sb, hb) in FULL_STR {
let expected = sa.as_bytes().cmp(sb.as_bytes());
assert_eq!(
C::compare_raw_slices(&pinned(ha), &pinned(hb)),
Some(expected),
"compare_raw_slices({sa:?}, {sb:?})"
);
}
}
}
#[test]
fn compare_matches_plaintext_order_other_policies() {
for (padding, lefts, fulls) in other_policies() {
for ((sa, ha), (_, hl)) in fulls.iter().zip(lefts.iter()) {
for (sb, hb) in fulls {
let expected = sa.as_bytes().cmp(sb.as_bytes());
assert_eq!(
C::compare_raw_slices(&pinned(ha), &pinned(hb)),
Some(expected),
"compare_raw_slices({sa:?}, {sb:?}) under {padding:?}"
);
assert_eq!(
C::compare_left_to_full(&pinned(hl), &pinned(hb)),
Some(expected),
"compare_left_to_full({sa:?}, {sb:?}) under {padding:?}"
);
}
}
}
}
#[test]
fn comparators_refuse_mixed_policies() {
let alice =
|set: &[(&str, &'static str)]| pinned(set.iter().find(|(s, _)| *s == "alice").unwrap().1);
let fulls = [
alice(FULL_STR),
alice(FULL_STR_UNPADDED),
alice(FULL_STR_FIXED),
];
let lefts = [
alice(LEFT_STR),
alice(LEFT_STR_UNPADDED),
alice(LEFT_STR_FIXED),
];
for (i, (full_a, left_a)) in fulls.iter().zip(&lefts).enumerate() {
for (j, full_b) in fulls.iter().enumerate() {
let expected = (i == j).then_some(Ordering::Equal);
assert_eq!(C::compare_raw_slices(full_a, full_b), expected);
assert_eq!(C::compare_left_to_full(left_a, full_b), expected);
}
}
}
#[test]
fn padding_extends_the_unpadded_blocks() {
const H: usize = 7;
for ((s, unpadded), (_, padded)) in LEFT_STR_UNPADDED.iter().zip(LEFT_STR) {
let (u, p) = (pinned(unpadded), pinned(padded));
let count = u16::from_be_bytes([u[2], u[3]]) as usize;
assert_eq!(u[H..H + count], p[H..H + count], "xt prefix for {s:?}");
}
}
#[test]
fn compare_left_to_full_matches_plaintext_order() {
for (sa, hl) in LEFT_STR {
for (sb, hf) in FULL_STR {
let expected = sa.as_bytes().cmp(sb.as_bytes());
assert_eq!(
C::compare_left_to_full(&pinned(hl), &pinned(hf)),
Some(expected),
"compare_left_to_full({sa:?}, {sb:?})"
);
}
}
}
#[test]
fn compare_raw_slices_equality_across_nonces() {
let alice_a = pinned(FULL_STR.iter().find(|(s, _)| *s == "alice").unwrap().1);
let alice_b = pinned(FULL_ALICE_SEED_B);
assert_ne!(
alice_a, alice_b,
"different nonces must give different bytes"
);
assert_eq!(
C::compare_raw_slices(&alice_a, &alice_b),
Some(Ordering::Equal)
);
assert_eq!(
C::compare_raw_slices(&alice_b, &alice_a),
Some(Ordering::Equal)
);
}
#[test]
fn pinned_bytes_round_trip_the_parser() {
let sets = other_policies();
let others = sets.iter().flat_map(|(_, _, fulls)| fulls.iter());
for (_, h) in FULL_STR.iter().chain(others) {
let bytes = pinned(h);
let ct = VarCipherText::from_slice(&bytes).unwrap();
assert_eq!(ct.to_bytes(), bytes);
}
}
const XT: usize = 7;
fn with_out_of_domain_symbol(hex_str: &str) -> Vec<u8> {
let mut bytes = pinned(hex_str);
bytes[XT] = 64;
bytes
}
fn full_alice() -> &'static str {
FULL_STR.iter().find(|(s, _)| *s == "alice").unwrap().1
}
fn left_alice() -> &'static str {
LEFT_STR.iter().find(|(s, _)| *s == "alice").unwrap().1
}
#[test]
fn parser_rejects_out_of_domain_symbols() {
assert!(VarCipherText::from_slice(&with_out_of_domain_symbol(full_alice())).is_err());
let mut edge = pinned(full_alice());
edge[XT] = 63;
assert!(VarCipherText::from_slice(&edge).is_ok());
}
#[test]
fn comparators_reject_out_of_domain_symbols() {
let good = pinned(full_alice());
let bad = with_out_of_domain_symbol(full_alice());
assert_eq!(C::compare_raw_slices(&bad, &good), None);
assert_eq!(C::compare_raw_slices(&good, &bad), None);
let bad_left = with_out_of_domain_symbol(left_alice());
assert_eq!(C::compare_left_to_full(&bad_left, &good), None);
assert_eq!(C::compare_left_to_full(&pinned(left_alice()), &bad), None);
}
#[test]
fn comparators_reject_wrong_lengths_and_headers() {
let full = pinned(full_alice());
let left = pinned(left_alice());
assert_eq!(C::compare_raw_slices(&left, &full), None);
assert_eq!(C::compare_left_to_full(&full, &full), None);
assert!(VarCipherText::from_slice(&left).is_err());
assert!(VarCipherText::from_slice(&full[..full.len() - 1]).is_err());
let mut longer = full.clone();
longer.push(0);
assert!(VarCipherText::from_slice(&longer).is_err());
let mut v = full.clone();
v[0] ^= 1;
assert_eq!(C::compare_raw_slices(&v, &full), None);
let mut s = full.clone();
s[1] ^= 1;
assert_eq!(C::compare_raw_slices(&s, &full), None);
let mut p = full.clone();
p[4] = 3;
assert_eq!(C::compare_raw_slices(&p, &p), None);
assert!(VarCipherText::from_slice(&p).is_err());
}
#[test]
#[ignore = "generator: prints the contents of src/scheme/chained/compat_vectors_data.rs"]
fn generate() {
println!("// Contents of src/scheme/chained/compat_vectors_data.rs");
println!(
"// Generated by `cargo test -p ore-rs --lib -- --ignored --nocapture compat_vectors::generate`"
);
println!("// DO NOT regenerate unless deliberately breaking the wire format (see plan doc).");
println!();
let ore = cipher_left();
println!("pub const LEFT_STR: &[(&str, &str)] = &[");
for s in STRINGS {
let left = ore.encrypt_left_str(s).unwrap();
println!(" ({s:?}, \"{}\"),", hex::encode(left.to_bytes()));
}
println!("];");
println!();
println!("pub const FULL_STR: &[(&str, &str)] = &[");
for s in STRINGS {
let ct = cipher_a().encrypt_str(s).unwrap();
println!(" ({s:?}, \"{}\"),", hex::encode(ct.to_bytes()));
}
println!("];");
println!();
let ct = cipher_b().encrypt_str("alice").unwrap();
println!(
"pub const FULL_ALICE_SEED_B: &str = \"{}\";",
hex::encode(ct.to_bytes())
);
for (padding, suffix) in [(Padding::None, "UNPADDED"), (FIXED, "FIXED")] {
println!();
let ore = cipher_left_with(padding);
println!("pub const LEFT_STR_{suffix}: &[(&str, &str)] = &[");
for s in STRINGS {
let left = ore.encrypt_left_str(s).unwrap();
println!(" ({s:?}, \"{}\"),", hex::encode(left.to_bytes()));
}
println!("];");
println!();
println!("pub const FULL_STR_{suffix}: &[(&str, &str)] = &[");
for s in STRINGS {
let ct = cipher_a_with(padding).encrypt_str(s).unwrap();
println!(" ({s:?}, \"{}\"),", hex::encode(ct.to_bytes()));
}
println!("];");
}
println!();
println!("pub const LEFT_U128: &[(u128, &str)] = &[");
for x in U128S {
println!(" ({x}, \"{}\"),", hex::encode(left_u128(&ore, *x)));
}
println!("];");
println!();
println!("pub const LEFT_I128: &[(i128, &str)] = &[");
for x in I128S {
println!(" ({x}, \"{}\"),", hex::encode(left_i128(&ore, *x)));
}
println!("];");
println!();
println!("pub const FULL_U128: &[(u128, &str)] = &[");
for x in U128S {
println!(
" ({x}, \"{}\"),",
hex::encode(full_u128(&cipher_a(), *x))
);
}
println!("];");
println!();
println!("pub const FULL_I128: &[(i128, &str)] = &[");
for x in I128S {
println!(
" ({x}, \"{}\"),",
hex::encode(full_i128(&cipher_a(), *x))
);
}
println!("];");
println!();
println!("pub const SALTED_ALICE: &[(&[u8], &str, &str)] = &[");
for s in SALTS {
let left = cipher_left_salted(s).encrypt_left_str("alice").unwrap();
let full = cipher_a_salted(s).encrypt_str("alice").unwrap();
println!(
" (b{:?}, \"{}\", \"{}\"),",
std::str::from_utf8(s).unwrap(),
hex::encode(left.to_bytes()),
hex::encode(full.to_bytes())
);
}
println!("];");
}