use crate::{
ciphertext::*,
primitives::{
hash::Aes128Z2Hash, prf::Aes128Prf, AesBlock, Hash, HashKey, Prf, Prp, Symbol, NONCE_SIZE,
},
scheme::width::{AesBlockBuf, Bit8, BlockWidth, FirstDiff, RightBitVec},
OreCipher, OreError, OreScheme, PlainText, Query, Term,
};
use aes::cipher::generic_array::GenericArray;
use rand::TryRng;
use std::cmp::Ordering;
use std::convert::TryInto;
use std::sync::Mutex;
use subtle_ng::{Choice, ConditionallySelectable, ConstantTimeEq};
use vitaminc_ore::OrderPlaintext;
use vitaminc_protected::{Controlled, Protected};
use vitaminc_random::SafeRand;
use zeroize::{Zeroize, ZeroizeOnDrop};
pub(crate) mod block_types;
pub(crate) use self::block_types::*;
#[derive(ZeroizeOnDrop)]
pub struct OreAes128 {
prf1: Aes128Prf,
prf2: Aes128Prf,
#[zeroize(skip)]
rng: Mutex<SafeRand>,
}
impl std::fmt::Debug for OreAes128 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OreAes128").finish_non_exhaustive()
}
}
type EncryptLeftResult<const N: usize> = Result<Left<OreAes128, N>, OreError>;
type EncryptResult<const N: usize> = Result<CipherText<OreAes128, N>, OreError>;
struct SeedBuf<const N: usize>([AesBlock; N]);
impl<const N: usize> Drop for SeedBuf<N> {
fn drop(&mut self) {
for seed in self.0.iter_mut() {
seed.zeroize();
}
}
}
fn derive_prp_seeds<const N: usize>(prf2: &Aes128Prf, x: &PlainText<N>) -> SeedBuf<N> {
let mut seeds = [AesBlock::default(); N];
for (n, block) in seeds.iter_mut().enumerate() {
block[0..n].clone_from_slice(&x[0..n]);
}
prf2.encrypt_all(&mut seeds);
SeedBuf(seeds)
}
pub(crate) fn encode_right_block<W: BlockWidth, H: Hash>(
block: &mut W::RightBlock,
prp: &W::Prp,
x: W::Symbol,
hasher: &H,
ro_blocks: &mut W::RoKeyBuf,
) {
let out = block.as_mut_bits();
W::hash_into(hasher, ro_blocks, out);
prp.indicator_mask_xor(x, out);
}
pub const MAX_BLOCKS: usize = 15;
impl OreAes128 {
fn build_left<const N: usize>(
&self,
left: &mut Left<Self, N>,
seeds: &SeedBuf<N>,
x: &PlainText<N>,
) -> Result<(), OreError> {
for n in 0..N {
let prp: <Bit8 as BlockWidth>::Prp = Prp::new(&seeds.0[n])?;
left.xt[n] = prp.permute(Symbol::from(x[n])).get();
left.f[n][0..n].clone_from_slice(&x[0..n]);
left.f[n][n] = left.xt[n];
left.f[n][N] = n as u8;
}
Ok(())
}
}
impl OreAes128 {
pub fn init(k1: Protected<[u8; 16]>, k2: Protected<[u8; 16]>) -> Result<Self, OreError> {
Self::init_with_rng(k1, k2, SafeRand::from_entropy()?)
}
#[cfg(any(test, feature = "test-support"))]
pub fn init_seeded(
k1: Protected<[u8; 16]>,
k2: Protected<[u8; 16]>,
seed: [u8; 32],
) -> Result<Self, OreError> {
use vitaminc_random::SeedableRng;
Self::init_with_rng(k1, k2, SafeRand::from_seed(seed))
}
fn init_with_rng(
k1: Protected<[u8; 16]>,
k2: Protected<[u8; 16]>,
rng: SafeRand,
) -> Result<Self, OreError> {
Ok(OreAes128 {
prf1: Prf::new(GenericArray::from_slice(k1.risky_ref())),
prf2: Prf::new(GenericArray::from_slice(k2.risky_ref())),
rng: Mutex::new(rng),
})
}
fn fill_nonce(&self, nonce: &mut [u8; NONCE_SIZE]) {
let mut rng = self
.rng
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
match rng.try_fill_bytes(nonce) {
Ok(()) => {}
Err(never) => match never {},
}
}
}
fn fixed<const N: usize>(cipher: &OreAes128, bytes: &[u8]) -> Result<Vec<u8>, OreError> {
let input: &[u8; N] = bytes
.try_into()
.map_err(|_| OreError::InitFailed)?;
OreCipher::encrypt(cipher, input).map(|ct| ct.to_bytes())
}
fn fixed_left<const N: usize>(cipher: &OreAes128, bytes: &[u8]) -> Result<Vec<u8>, OreError> {
let input: &[u8; N] = bytes.try_into().map_err(|_| OreError::InitFailed)?;
OreCipher::encrypt_left(cipher, input).map(|left| left.to_bytes())
}
fn fixed_width_bytes(plaintext: &OrderPlaintext) -> Result<&[u8], OreError> {
match plaintext.width() {
None => Err(OreError::VariableLength),
Some(width) if width > MAX_BLOCKS => Err(OreError::TooManyBlocks),
Some(_) => Ok(plaintext.bytes().risky_ref().as_slice()),
}
}
macro_rules! by_width {
($f:ident, $cipher:expr, $bytes:expr) => {
match $bytes.len() {
0 => $f::<0>($cipher, $bytes),
1 => $f::<1>($cipher, $bytes),
2 => $f::<2>($cipher, $bytes),
3 => $f::<3>($cipher, $bytes),
4 => $f::<4>($cipher, $bytes),
5 => $f::<5>($cipher, $bytes),
6 => $f::<6>($cipher, $bytes),
7 => $f::<7>($cipher, $bytes),
8 => $f::<8>($cipher, $bytes),
9 => $f::<9>($cipher, $bytes),
10 => $f::<10>($cipher, $bytes),
11 => $f::<11>($cipher, $bytes),
12 => $f::<12>($cipher, $bytes),
13 => $f::<13>($cipher, $bytes),
14 => $f::<14>($cipher, $bytes),
15 => $f::<15>($cipher, $bytes),
_ => Err(OreError::TooManyBlocks),
}
};
}
impl crate::sealed::Sealed for OreAes128 {}
impl OreScheme for OreAes128 {
fn encrypt_plaintext(&self, plaintext: &OrderPlaintext) -> Result<Term, OreError> {
let bytes = fixed_width_bytes(plaintext)?;
by_width!(fixed, self, bytes).map(Term::new)
}
fn encrypt_query_plaintext(&self, plaintext: &OrderPlaintext) -> Result<Query, OreError> {
let bytes = fixed_width_bytes(plaintext)?;
by_width!(fixed_left, self, bytes).map(Query::new)
}
fn compare_raw_slices(a: &[u8], b: &[u8]) -> Option<Ordering> {
if a.len() != b.len() {
return None;
};
let left_size = LeftBlock16::BLOCK_SIZE;
let right_size = RightBlock32::BLOCK_SIZE;
let body = a.len().checked_sub(NONCE_SIZE)?;
let per_block = left_size + right_size + 1;
if body % per_block != 0 {
return None;
}
let num_blocks = body / per_block;
if num_blocks > MAX_BLOCKS {
return None;
}
let mut is_equal = Choice::from(1);
let mut diff = FirstDiff::<{ RightBlock32::BLOCK_SIZE }>::new();
let a_f = &a[num_blocks..];
let b_f = &b[num_blocks..];
let b_right = &b[num_blocks * (left_size + 1)..];
let b_right_blocks = &b_right[NONCE_SIZE..];
for n in 0..num_blocks {
let prp_eq: Choice = !a[n].ct_eq(&b[n]);
let left_block_comparison: Choice = !left_block(a_f, n).ct_eq(left_block(b_f, n));
let condition: Choice = prp_eq | left_block_comparison;
let first = is_equal & condition;
diff.latch(
a[n],
left_block(a_f, n),
right_block(b_right_blocks, n),
first,
);
is_equal.conditional_assign(&Choice::from(0), first);
}
if bool::from(is_equal) {
return Some(Ordering::Equal);
}
let hash_key = HashKey::from_slice(&b_right[0..NONCE_SIZE]);
let hash: Aes128Z2Hash = Hash::new(hash_key);
if diff.resolve(&hash) == 1 {
return Some(Ordering::Greater);
}
Some(Ordering::Less)
}
fn compare_left_to_full(left: &[u8], full: &[u8]) -> Option<Ordering> {
let per_block = LeftBlock16::BLOCK_SIZE + 1;
if left.len() % per_block != 0 {
return None;
}
fn typed<const N: usize>(left: &[u8], full: &[u8]) -> Option<Ordering> {
let left = Left::<OreAes128, N>::from_slice(left).ok()?;
let full = CipherText::<OreAes128, N>::from_slice(full).ok()?;
Some(compare_typed(&left, &full))
}
match left.len() / per_block {
0 => typed::<0>(left, full),
1 => typed::<1>(left, full),
2 => typed::<2>(left, full),
3 => typed::<3>(left, full),
4 => typed::<4>(left, full),
5 => typed::<5>(left, full),
6 => typed::<6>(left, full),
7 => typed::<7>(left, full),
8 => typed::<8>(left, full),
9 => typed::<9>(left, full),
10 => typed::<10>(left, full),
11 => typed::<11>(left, full),
12 => typed::<12>(left, full),
13 => typed::<13>(left, full),
14 => typed::<14>(left, full),
15 => typed::<15>(left, full),
_ => None,
}
}
}
impl OreCipher for OreAes128 {
type LeftBlockType = LeftBlock16;
type RightBlockType = RightBlock32;
fn encrypt_left<const N: usize>(&self, x: &PlainText<N>) -> EncryptLeftResult<N> {
if N > MAX_BLOCKS {
return Err(OreError::TooManyBlocks);
}
let mut output = Left::<Self, N>::init();
let seeds = derive_prp_seeds(&self.prf2, x);
self.build_left(&mut output, &seeds, x)?;
self.prf1.encrypt_all(&mut output.f);
Ok(output)
}
fn encrypt<const N: usize>(&self, x: &PlainText<N>) -> EncryptResult<N> {
if N > MAX_BLOCKS {
return Err(OreError::TooManyBlocks);
}
let mut left = Left::<Self, N>::init();
let mut right = Right::<Self, N>::init();
self.fill_nonce(&mut right.nonce);
let seeds = derive_prp_seeds(&self.prf2, x);
let hasher: Aes128Z2Hash = Hash::new(AesBlock::from_slice(&right.nonce));
let mut template = <Bit8 as BlockWidth>::RoKeyBuf::zeroed();
for (j, entry) in template.iter_mut().enumerate() {
entry[0] = j as u8;
}
let mut work = <Bit8 as BlockWidth>::RoKeyBuf::zeroed();
for n in 0..N {
let prp: <Bit8 as BlockWidth>::Prp = Prp::new(&seeds.0[n])?;
left.xt[n] = prp.permute(Symbol::from(x[n])).get();
left.f[n][0..n].clone_from_slice(&x[0..n]);
left.f[n][n] = left.xt[n];
left.f[n][N] = n as u8;
if n > 0 {
for (j, entry) in template.iter_mut().enumerate() {
entry[n - 1] = x[n - 1];
entry[n] = j as u8;
entry[N] = n as u8;
}
}
work.copy_from(&template);
self.prf1.encrypt_all(work.as_mut_slice());
encode_right_block::<Bit8, _>(
&mut right.data[n],
&prp,
Symbol::from(x[n]),
&hasher,
&mut work,
);
}
self.prf1.encrypt_all(&mut left.f);
for entry in template.iter_mut() {
entry.zeroize();
}
for entry in work.as_mut_slice() {
entry.zeroize();
}
Ok(CipherText { left, right })
}
}
#[inline]
fn left_block(input: &[u8], n: usize) -> &[u8] {
let f_pos = n * LeftBlock16::BLOCK_SIZE;
&input[f_pos..(f_pos + LeftBlock16::BLOCK_SIZE)]
}
#[inline]
fn right_block(input: &[u8], n: usize) -> &[u8] {
let f_pos = n * RightBlock32::BLOCK_SIZE;
&input[f_pos..(f_pos + RightBlock32::BLOCK_SIZE)]
}
impl<const N: usize> PartialEq for CipherText<OreAes128, N> {
fn eq(&self, b: &Self) -> bool {
matches!(self.cmp(b), Ordering::Equal)
}
}
impl<const N: usize> Ord for CipherText<OreAes128, N> {
fn cmp(&self, b: &Self) -> Ordering {
compare_typed(&self.left, b)
}
}
fn compare_typed<const N: usize>(
left: &Left<OreAes128, N>,
b: &CipherText<OreAes128, N>,
) -> Ordering {
let mut is_equal = Choice::from(1);
let mut diff = FirstDiff::<{ RightBlock32::BLOCK_SIZE }>::new();
for n in 0..N {
let condition: Choice =
!(left.xt[n].ct_eq(&b.left.xt[n])) | !(left.f[n].ct_eq(&b.left.f[n]));
let first = is_equal & condition;
diff.latch(left.xt[n], &left.f[n], b.right.data[n].bytes(), first);
is_equal.conditional_assign(&Choice::from(0), first);
}
if bool::from(is_equal) {
return Ordering::Equal;
}
let hash: Aes128Z2Hash = Hash::new(AesBlock::from_slice(&b.right.nonce));
if diff.resolve(&hash) == 1 {
return Ordering::Greater;
}
Ordering::Less
}
impl<const N: usize> PartialOrd for CipherText<OreAes128, N> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<const N: usize> Eq for CipherText<OreAes128, N> {}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::{bytes_pt, f64_pt, fixed_key, key, timestamp_pt, u128_pt, u32_pt, u64_pt};
use quickcheck::TestResult;
type Ore = OreAes128;
fn init_ore() -> Ore {
Ore::init(key(), key()).unwrap()
}
fn full<const N: usize>(ore: &Ore, plaintext: &OrderPlaintext) -> CipherText<Ore, N> {
let term = ore.encrypt_plaintext(plaintext).unwrap();
CipherText::from_slice(term.as_bytes()).unwrap()
}
fn full_u64(ore: &Ore, x: u64) -> CipherText<Ore, 8> {
full(ore, &u64_pt(x))
}
fn term_bytes(ore: &Ore, plaintext: &OrderPlaintext) -> Vec<u8> {
ore.encrypt_plaintext(plaintext).unwrap().into_bytes()
}
#[test]
fn sixteen_bytes_is_an_error_not_a_panic() {
let ore = init_ore();
assert!(matches!(
OreCipher::encrypt(&ore, &[0u8; 16]),
Err(OreError::TooManyBlocks)
));
assert!(matches!(
OreCipher::encrypt_left(&ore, &[0u8; 16]),
Err(OreError::TooManyBlocks)
));
assert!(OreCipher::encrypt(&ore, &[0u8; 15]).is_ok());
assert!(OreCipher::encrypt_left(&ore, &[0u8; 15]).is_ok());
assert!(matches!(
ore.encrypt_plaintext(&u128_pt(1)),
Err(OreError::TooManyBlocks)
));
assert!(matches!(
ore.encrypt_query_plaintext(&u128_pt(1)),
Err(OreError::TooManyBlocks)
));
assert!(ore.encrypt_plaintext(×tamp_pt(1)).is_ok());
assert!(matches!(
ore.encrypt_plaintext(&bytes_pt(b"x")),
Err(OreError::VariableLength)
));
}
#[test]
fn compare_raw_slices_refuses_malformed_lengths() {
let ore = init_ore();
let good = term_bytes(&ore, &u64_pt(42));
assert_eq!(Ore::compare_raw_slices(&[], &[]), None);
assert_eq!(Ore::compare_raw_slices(&good[..15], &good[..15]), None);
assert_eq!(Ore::compare_raw_slices(&good[..17], &good[..17]), None);
assert_eq!(
Ore::compare_raw_slices(&good[..good.len() - 1], &good[..good.len() - 1]),
None
);
assert_eq!(Ore::compare_raw_slices(&good, &good), Some(Ordering::Equal));
}
#[test]
fn compare_raw_slices_refuses_more_than_max_blocks() {
let per_block = LeftBlock16::BLOCK_SIZE + RightBlock32::BLOCK_SIZE + 1;
let over = vec![0u8; NONCE_SIZE + (MAX_BLOCKS + 1) * per_block];
let at = vec![0u8; NONCE_SIZE + MAX_BLOCKS * per_block];
assert_eq!(Ore::compare_raw_slices(&over, &over), None);
assert_eq!(
Ore::compare_left_to_full(&over[..(MAX_BLOCKS + 1) * 17], &over),
None
);
assert_eq!(Ore::compare_raw_slices(&at, &at), Some(Ordering::Equal));
assert_eq!(
Ore::compare_left_to_full(&at[..MAX_BLOCKS * 17], &at),
Some(Ordering::Equal)
);
}
#[test]
fn compare_raw_slices_accepts_zero_blocks() {
let ore = init_ore();
let a = OreCipher::encrypt(&ore, &[0u8; 0]).unwrap();
let b = OreCipher::encrypt(&ore, &[0u8; 0]).unwrap();
let (a_bytes, b_bytes) = (a.to_bytes(), b.to_bytes());
assert_eq!(a_bytes.len(), NONCE_SIZE);
assert_eq!(
Ore::compare_raw_slices(&a_bytes, &b_bytes),
Some(Ordering::Equal)
);
let parsed = CipherText::<Ore, 0>::from_slice(&a_bytes).unwrap();
assert_eq!(parsed.cmp(&b), Ordering::Equal);
}
#[test]
fn left_to_full_orders_as_the_plaintexts_and_refuses_the_rest() {
let ore = init_ore();
let query = ore.encrypt_query_plaintext(&u64_pt(7)).unwrap();
let (below, same, above) = (
term_bytes(&ore, &u64_pt(6)),
term_bytes(&ore, &u64_pt(7)),
term_bytes(&ore, &u64_pt(8)),
);
let left = query.as_bytes();
assert_eq!(left.len(), 8 * 17);
assert_eq!(
Ore::compare_left_to_full(left, &below),
Some(Ordering::Greater)
);
assert_eq!(
Ore::compare_left_to_full(left, &same),
Some(Ordering::Equal)
);
assert_eq!(
Ore::compare_left_to_full(left, &above),
Some(Ordering::Less)
);
assert_eq!(Ore::compare_left_to_full(&same, &same), None);
assert_eq!(Ore::compare_left_to_full(left, left), None);
assert_eq!(
Ore::compare_left_to_full(left, &term_bytes(&ore, &u32_pt(7))),
None
);
assert_eq!(Ore::compare_left_to_full(&left[1..], &same), None);
let typed_left = Left::<Ore, 8>::from_slice(left).unwrap();
assert_eq!(typed_left.to_bytes(), full_u64(&ore, 7).left.to_bytes());
}
quickcheck! {
fn compare_u64(x: u64, y: u64) -> bool {
let ore = init_ore();
let a = full_u64(&ore, x);
let b = full_u64(&ore, y);
match x.cmp(&y) {
Ordering::Greater => a > b,
Ordering::Less => a < b,
Ordering::Equal => a == b
}
}
fn compare_u64_raw_slices(x: u64, y: u64) -> bool {
let ore = init_ore();
let a = term_bytes(&ore, &u64_pt(x));
let b = term_bytes(&ore, &u64_pt(y));
match Ore::compare_raw_slices(&a, &b) {
Some(Ordering::Greater) => x > y,
Some(Ordering::Less) => x < y,
Some(Ordering::Equal) => x == y,
None => false
}
}
fn left_to_full_u64(x: u64, y: u64) -> bool {
let ore = init_ore();
let left = ore.encrypt_query_plaintext(&u64_pt(x)).unwrap();
let full = term_bytes(&ore, &u64_pt(y));
Ore::compare_left_to_full(left.as_bytes(), &full) == Some(x.cmp(&y))
}
fn equality_u64(x: u64) -> bool {
let ore = init_ore();
full_u64(&ore, x) == full_u64(&ore, x)
}
fn equality_u64_raw_slices(x: u64) -> bool {
let ore = init_ore();
let a = term_bytes(&ore, &u64_pt(x));
let b = term_bytes(&ore, &u64_pt(x));
Ore::compare_raw_slices(&a, &b) == Some(Ordering::Equal)
}
fn compare_u32(x: u32, y: u32) -> bool {
let ore = init_ore();
let a: CipherText<Ore, 4> = full(&ore, &u32_pt(x));
let b: CipherText<Ore, 4> = full(&ore, &u32_pt(y));
match x.cmp(&y) {
Ordering::Greater => a > b,
Ordering::Less => a < b,
Ordering::Equal => a == b
}
}
fn equality_u32(x: u32) -> bool {
let ore = init_ore();
let a: CipherText<Ore, 4> = full(&ore, &u32_pt(x));
let b: CipherText<Ore, 4> = full(&ore, &u32_pt(x));
a == b
}
fn compare_f64(x: f64, y: f64) -> TestResult {
if x.is_nan() || x.is_infinite() || y.is_nan() || y.is_infinite() {
return TestResult::discard();
}
let ore = init_ore();
let a: CipherText<Ore, 8> = full(&ore, &f64_pt(x));
let b: CipherText<Ore, 8> = full(&ore, &f64_pt(y));
match x.partial_cmp(&y) {
Some(Ordering::Greater) => TestResult::from_bool(a > b),
Some(Ordering::Less) => TestResult::from_bool(a < b),
Some(Ordering::Equal) => TestResult::from_bool(a == b),
None => TestResult::failed()
}
}
fn equality_f64(x: f64) -> bool {
let ore = init_ore();
let a: CipherText<Ore, 8> = full(&ore, &f64_pt(x));
let b: CipherText<Ore, 8> = full(&ore, &f64_pt(x));
a == b
}
fn compare_plaintext(x: u64, y: u64) -> bool {
let ore = init_ore();
let a = OreCipher::encrypt(&ore, &x.to_be_bytes()).unwrap();
let b = OreCipher::encrypt(&ore, &y.to_be_bytes()).unwrap();
match x.cmp(&y) {
Ordering::Greater => a > b,
Ordering::Less => a < b,
Ordering::Equal => a == b
}
}
fn equality_plaintext(x: u64) -> bool {
let ore = init_ore();
let a = OreCipher::encrypt(&ore, &x.to_be_bytes()).unwrap();
let b = OreCipher::encrypt(&ore, &x.to_be_bytes()).unwrap();
a == b && a == full_u64(&ore, x)
}
}
#[test]
fn smallest_to_largest() {
let ore = init_ore();
assert!(full_u64(&ore, 0) < full_u64(&ore, u64::MAX));
}
#[test]
fn largest_to_smallest() {
let ore = init_ore();
assert!(full_u64(&ore, u64::MAX) > full_u64(&ore, 0));
}
#[test]
fn smallest_to_smallest() {
let ore = init_ore();
assert!(full_u64(&ore, 0) == full_u64(&ore, 0));
}
#[test]
fn largest_to_largest() {
let ore = init_ore();
assert!(full_u64(&ore, u64::MAX) == full_u64(&ore, u64::MAX));
}
#[test]
fn signed_zeros_compare_equal() {
let ore = init_ore();
let pos_zero: CipherText<Ore, 8> = full(&ore, &f64_pt(0.0));
let neg_zero: CipherText<Ore, 8> = full(&ore, &f64_pt(-0.0));
assert_eq!(0.0_f64.partial_cmp(&-0.0_f64), Some(Ordering::Equal));
assert!(pos_zero == neg_zero);
}
#[test]
fn comparisons_in_first_block() {
let ore = init_ore();
let a = full_u64(&ore, 18446744073709551615u64);
let b = full_u64(&ore, 18446744073709551612u64);
assert!(a > b);
assert!(b < a);
}
#[test]
fn comparisons_in_last_block() {
let ore = init_ore();
let a = full_u64(&ore, 10);
let b = full_u64(&ore, 73);
assert!(a < b);
assert!(b > a);
}
#[test]
fn compare_raw_slices_mismatched_lengths() {
let ore = init_ore();
let a_64 = term_bytes(&ore, &u64_pt(10));
let a_32 = term_bytes(&ore, &u32_pt(10));
assert_eq!(Ore::compare_raw_slices(&a_64, &a_32), Option::None);
}
#[test]
fn binary_encoding() {
let ore = init_ore();
let a = full_u64(&ore, 10);
let bin = a.to_bytes();
assert_eq!(a, CipherText::<Ore, 8>::from_slice(&bin).unwrap());
}
#[test]
#[should_panic(expected = "ParseError")]
fn binary_encoding_invalid_length() {
let bin = vec![0, 1, 2, 3];
CipherText::<Ore, 8>::from_slice(&bin).unwrap();
}
const K1: [u8; 16] = [
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
];
const K2: [u8; 16] = [
129, 4, 114, 186, 102, 145, 225, 73, 166, 57, 244, 251, 56, 92, 188, 36,
];
const K3: [u8; 16] = [
49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 97, 98, 99, 100, 101, 102,
];
#[test]
fn test_different_prf_keys() {
let ore1 = Ore::init(Protected::new(K1), Protected::new(K2)).unwrap();
let ore2 = Ore::init(Protected::new(K3), Protected::new(K2)).unwrap();
let a = term_bytes(&ore1, &u32_pt(1000));
let b = term_bytes(&ore2, &u32_pt(1000));
assert_ne!(Some(Ordering::Equal), Ore::compare_raw_slices(&a, &b));
}
#[test]
fn test_different_prp_keys() {
let ore1 = Ore::init(Protected::new(K1), Protected::new(K2)).unwrap();
let ore2 = Ore::init(Protected::new(K1), Protected::new(K3)).unwrap();
let a = term_bytes(&ore1, &u32_pt(1000));
let b = term_bytes(&ore2, &u32_pt(1000));
assert_ne!(Some(Ordering::Equal), Ore::compare_raw_slices(&a, &b));
}
#[test]
fn a_seeded_cipher_is_reproducible() {
let a = Ore::init_seeded(fixed_key(1), fixed_key(2), [9; 32]).unwrap();
let b = Ore::init_seeded(fixed_key(1), fixed_key(2), [9; 32]).unwrap();
assert_eq!(term_bytes(&a, &u64_pt(5)), term_bytes(&b, &u64_pt(5)));
let c = Ore::init(fixed_key(1), fixed_key(2)).unwrap();
assert_ne!(term_bytes(&a, &u64_pt(5)), term_bytes(&c, &u64_pt(5)));
}
#[test]
fn every_term_draws_a_fresh_nonce() {
let a = Ore::init(fixed_key(1), fixed_key(2)).unwrap();
let b = Ore::init(fixed_key(1), fixed_key(2)).unwrap();
let (a1, a2, b1) = (
term_bytes(&a, &u64_pt(5)),
term_bytes(&a, &u64_pt(5)),
term_bytes(&b, &u64_pt(5)),
);
assert_ne!(a1, a2, "one cipher reused a nonce");
assert_ne!(a1, b1, "two ciphers drew the same nonce stream");
assert_eq!(Ore::compare_raw_slices(&a1, &b1), Some(Ordering::Equal));
}
#[test]
fn a_poisoned_nonce_lock_still_encrypts() {
let ore = Ore::init(fixed_key(1), fixed_key(2)).unwrap();
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _guard = ore.rng.lock().unwrap();
panic!("poison the nonce lock");
}));
assert!(ore.rng.is_poisoned());
let (a, b) = (term_bytes(&ore, &u64_pt(5)), term_bytes(&ore, &u64_pt(5)));
assert_ne!(a, b, "a poisoned lock reused a nonce");
assert_eq!(Ore::compare_raw_slices(&a, &b), Some(Ordering::Equal));
}
#[test]
fn the_scheme_is_send_and_sync() {
fn is_send_sync<X: Send + Sync>(_: &X) {}
let ore = init_ore();
is_send_sync(&ore);
let shared = &ore;
let terms: Vec<Vec<u8>> = std::thread::scope(|scope| {
let handles: Vec<_> = (0..4u64)
.map(|i| scope.spawn(move || term_bytes(shared, &u64_pt(i))))
.collect();
handles.into_iter().map(|h| h.join().unwrap()).collect()
});
for pair in terms.windows(2) {
assert_eq!(
Ore::compare_raw_slices(&pair[0], &pair[1]),
Some(Ordering::Less)
);
}
}
}