ore-rs 0.11.0

Order-revealing encryption library used by the CipherStash searchable encryption platform
Documentation
pub mod cmac;
pub mod hash;
pub mod prf;
pub mod prp;
pub mod simd;
pub(crate) mod stack;

use aes::cipher::{consts::U16, generic_array::GenericArray};
use aes::Block;
use thiserror::Error;
use zeroize::ZeroizeOnDrop;

// Every AES key schedule in the crate is an `aes::Aes128Enc`: the PRF, the Z2
// hash, the PRP's PRNG, the CMAC accumulator, the per-block ciphers the PRPs
// key locally and the v2 subkey derivation (`scheme::keys::aes_block`). Each is wiped on drop only because `Aes128Enc:
// ZeroizeOnDrop`, which the `aes` crate gates behind its `zeroize` feature.
// Assert it here, once, so the build fails fast if that feature is ever lost;
// otherwise the key-schedule wipe would silently vanish while the types above
// still (falsely) claimed `ZeroizeOnDrop` (ZA-0001).
const _: fn() = || {
    fn assert_zeroize_on_drop<T: ZeroizeOnDrop>() {}
    assert_zeroize_on_drop::<aes::Aes128Enc>();
};
pub type AesBlock = Block;
pub type PrfKey = GenericArray<u8, U16>;
pub type HashKey = GenericArray<u8, U16>;
pub const NONCE_SIZE: usize = 16;

pub trait Prf {
    fn new(key: &PrfKey) -> Self;
    fn encrypt_all(&self, data: &mut [AesBlock]);
}

pub trait Hash {
    fn new(key: &HashKey) -> Self;
    fn hash(&self, data: &[u8; 16]) -> u8;
    /// Hash every block in `input` (in place, trashing it) and pack the
    /// 1-bit outputs LSB-first into `out`: bit `j` of `out` is the hash of
    /// `input[j]`. `D` must be `8 * B`, checked at compile time
    /// ([`Packed`]).
    fn hash_all_into<const D: usize, const B: usize>(
        &self,
        input: &mut [AesBlock; D],
        out: &mut [u8; B],
    );
}

/// `D` one-bit values packed LSB-first into `B` bytes. [`Self::OK`] is a
/// compile-time check that they fit exactly; a function taking a
/// `[_; D]` and a `[u8; B]` evaluates it, so a mismatched pair does not
/// compile.
pub(crate) struct Packed<const D: usize, const B: usize>;

impl<const D: usize, const B: usize> Packed<D, B> {
    pub(crate) const OK: () = assert!(D == 8 * B, "Packed: D values need exactly D / 8 bytes");
}

#[derive(Debug, Error)]
#[error("PRP Error")]
pub struct PrpError;
pub type PrpResult<T> = Result<T, PrpError>;

/// A value of a `D`-element PRP domain: a byte known to be `< D`, for a
/// power-of-two `D ≤ 256`.
///
/// The type carries the domain check, so [`Prp::permute`] and
/// [`Prp::invert`] are infallible and the oblivious table lookup behind them
/// has no range branch on the (secret) symbol. A symbol is made by
/// [`Self::from_low_bits`], which masks and never branches, or (for the
/// 256-element domain, where every byte is a symbol) by `From<u8>`. Callers
/// that take symbols from outside validate them first; see
/// `scheme::decompose::Blocks6`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Symbol<const D: usize>(u8);

impl<const D: usize> Symbol<D> {
    const DOMAIN_OK: () = assert!(
        D.is_power_of_two() && D <= 256,
        "Symbol: the domain must be a power of two no larger than 256"
    );

    /// The low `log2(D)` bits of `b`: `b mod D`, by masking. Branch-free, and
    /// the identity on any `b < D`.
    #[inline]
    #[allow(clippy::let_unit_value)]
    pub fn from_low_bits(b: u8) -> Self {
        let () = Self::DOMAIN_OK;
        // `D - 1` is at most 255, so the mask fits a byte.
        Self(b & (D - 1) as u8)
    }

    #[inline]
    pub fn get(self) -> u8 {
        self.0
    }
}

/// Every byte is a symbol of the 256-element domain.
impl From<u8> for Symbol<256> {
    #[inline]
    fn from(b: u8) -> Self {
        Self(b)
    }
}

/// A pseudo-random permutation over the domain whose values are `S` (a
/// [`Symbol`]). Only key setup can fail; a symbol is in the domain by type.
pub trait Prp<S>: Sized {
    fn new(key: &[u8]) -> PrpResult<Self>;
    fn permute(&self, data: S) -> S;
    /// Inverse of [`Self::permute`]. The encrypt path uses the bulk
    /// [`Self::indicator_mask_xor`] instead; this remains the per-value
    /// reference (used by the mask equivalence tests).
    #[allow(dead_code)]
    fn invert(&self, data: S) -> S;

    /// The indicator mask: one bit per domain value, `[u8; DOMAIN / 8]`.
    type Mask;

    /// XOR the indicator mask for `data` into `out`: bit `j` of the mask is
    /// `1` iff `invert(j) > data`. Bit order matches
    /// `RightBlock32::set_bit` (LSB-first within each byte).
    ///
    /// This is the bulk form of the per-`j` `invert`-and-compare loop the
    /// right-ciphertext encoder needs; implementations walk their inverse
    /// table linearly instead of doing `DOMAIN` indexed lookups.
    fn indicator_mask_xor(&self, data: S, out: &mut Self::Mask);
}