Skip to main content

BlockOre

Struct BlockOre 

Source
pub struct BlockOre { /* private fields */ }
Expand description

Block ORE as a vitaminc order scheme, the typed front door: every target, routed to the engine its width wants when a space is opened.

vitaminc decides the encoding of every kind of value and seals it as an OrderPlaintext that only it can build, so a scheme here is never handed bytes that skipped the encoding. A caller opens a space on BlockOre with a target and a context, encrypts values there, and compares terms and queries through the space; it never names an engine.

One scheme, two engines. BlockOre routes a space to an engine by its target’s width, once, when the space is opened:

  • up to ten bytes, the packed-prefix Bit6 engine (OreAes128Bit6): the integers to 64 bits, f64 and dates;
  • wider, or of no fixed width, the chained engine (OreAes128Bit6Chained): 128-bit integers, uuids, timestamps, decimals, text and bytes.

Every term carries its engine in its wire header, so a comparison dispatches on the bytes and refuses a pair from two engines, which no space produces. The legacy scheme has no adapter: it is for stored data, and the README says not to put new data on it.

Keys. OrderKeyInit keys the scheme from a 32-byte key, from which one 16-byte ORE key is derived with HMAC-SHA256 under ORE_KEY when the scheme is made. Each space is then keyed from that key under the salt vitaminc derives, PAE("vitaminc/ore/v1", "block-ore", domain, context), through the engines’ own salt stage, so two targets or two contexts never share key material.

Padding. Text is padded under the scheme’s policy (BlockOre::with_text_padding, Padding::default unless set), so a text term reveals a length bucket, and text that ends in NUL is refused (OreError::TrailingNul). Bytes are never padded, since NUL padding would refuse every value that ends in a zero byte, so a bytes term reveals the exact length. Fixed-width targets have nothing to hide. Text and bytes are refused past the scheme’s ceiling (BlockOre::with_max_bytes, DEFAULT_MAX_STR_BYTES unless set), which counts the marker byte vitaminc puts before them.

A term is the serialised ciphertext, left and right halves; a query is the left half alone. Both are compared by the scheme, never byte by byte: a term carries a random nonce, so one value has many terms, which compare Equal.

§Examples

use std::cmp::Ordering;
use ore_rs::BlockOre;
use vitaminc_ore::target::{I32, Text};
use vitaminc_ore::{OrderEncrypt, OrderKeyInit};
use vitaminc_protected::Protected;

let scheme = BlockOre::new(Protected::new(key_bytes));

// A space of `i32`s under one context, keyed once for all its terms.
let quantity = scheme.target(I32).context(("orders", "quantity"));
let stored = quantity.encrypt(42i16)?;
let query = quantity.encrypt_query(41i16)?;
assert_eq!(quantity.compare_query(&query, &stored), Some(Ordering::Less));
let bytes: &[u8] = stored.as_ref();

// Text goes to the chained engine; the caller does not choose.
let names = scheme.target(Text).context("names");
let alice = names.encrypt_checked("alice")?;
let bob = names.encrypt_checked("bob")?;
assert_eq!(names.compare(&alice, &bob), Some(Ordering::Less));

Implementations§

Source§

impl BlockOre

Source

pub fn with_text_padding(self, padding: Padding) -> Self

The padding policy of every Text space this scheme opens; Padding::default unless set. Stored terms and the queries that compare with them must share a policy, so set it once, with the key.

Source

pub fn with_max_bytes(self, max_bytes: NonZeroUsize) -> Self

The longest text or bytes value, in bytes, every space of this scheme encrypts, counting the marker byte vitaminc puts before it; DEFAULT_MAX_STR_BYTES unless set. A longer one is refused (OreError::TooLong). Fixed-width spaces are not bounded by it.

The ceiling must be one the chained engine accepts: no longer than the space’s padding can pad within the wire format’s limit, MAX_STR_BYTES (49151 bytes). A Bytes space is unpadded, so it takes any ceiling from 1 to 49151. A Text space pads the ceiling under Self::with_text_padding. Under Padding::PowerOfTwo, the default, the longest is 32768 (or the floor, if that is higher), since a longer value would pad to 65536 bytes. Under Padding::Fixed the lower of the ceiling and the fixed length applies, so any ceiling opens while the fixed length is itself within the wire format’s limit. Outside that range a Text or Bytes space fails to open (OreError::InitFailed), and so does every encryption in it; the scheme does not check the ceiling when it is set.

Source

pub fn text_padding(&self) -> Padding

The padding policy of this scheme’s text spaces.

Source

pub fn max_bytes(&self) -> NonZeroUsize

The ceiling on text and bytes values.

Trait Implementations§

Source§

impl Debug for BlockOre

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl OrderKeyInit for BlockOre

Source§

type Key = Protected<[u8; 32]>

The scheme’s full-strength key, a protected array.
Source§

type KeyError = WeakKeyError

Why key material of a length only known at runtime was refused.
Source§

fn new(key: Self::Key) -> Self

Key the scheme with a full-strength key. Cannot fail: the type carries the length.
Source§

fn try_from_bytes(key: Protected<Vec<u8>>) -> Result<Self, WeakKeyError>

Key the scheme with material whose length is only known at runtime, such as a KMS response. Read more
Source§

impl OrderQuery for BlockOre

Source§

fn encrypt_query( &self, space: &BlockOreSpace, plaintext: &OrderPlaintext, ) -> Result<Query, OreError>

Refuses a plaintext of another target, as OrderScheme::encrypt_plaintext does.

Source§

fn compare_query(query: &Query, term: &Term) -> Option<Ordering>

The order of the queried value against the stored one, through the engine the two wire headers name; None as OrderScheme::compare is None.

Source§

type Query = Query

What one query encrypts to. For a scheme whose terms are their own queries, Term. It need not wipe itself; see above for why.
Source§

impl OrderScheme for BlockOre

Source§

fn open( &self, target: OrderTarget, salt: &Context<'_>, ) -> Result<BlockOreSpace, OreError>

Key the engine the target’s width wants under salt. A Bytes space never pads, and a Text space pads under the scheme’s policy, as does any other target of no fixed width, so one vitaminc adds later cannot reveal its exact length. All of them keep the scheme’s ceiling, and fail to open (OreError::InitFailed) under a ceiling the chained engine refuses (see BlockOre::with_max_bytes). A fixed-width space on the chained engine takes neither the padding nor the ceiling, so no setting of either keeps it from opening.

Source§

fn encrypt_plaintext( &self, space: &BlockOreSpace, plaintext: &OrderPlaintext, ) -> Result<Term, OreError>

A plaintext of another target than the space’s is refused (OreError::TargetMismatch): the space’s engine is keyed and configured for its own target alone. vitaminc_ore::OrderSpace never hands one over; a direct call can.

Source§

fn compare(a: &Term, b: &Term) -> Option<Ordering>

The engine’s comparison, chosen by the terms’ wire headers. None for a term that is not a well-formed v2 ciphertext, or two terms of different engines, which no space makes. Two well-formed terms of different targets or contexts under one engine compare as noise, which the scheme cannot tell; a space keeps its terms together.

Source§

const NAME: &'static str = "block-ore"

The scheme’s name, bound into every term’s salt and named in its refusals: "cllw-ore", "cllw-ope", "block-ore".
Source§

type Term = Term

What one term encrypts to.
Source§

type Error = OreError

The scheme’s own failure. Infallible for a scheme that cannot fail once keyed.
Source§

type Space = BlockOreSpace

What the scheme keeps for one space: whatever it derives from the salt, so that it is derived for the space rather than for every term. Block ORE keeps the keys and cipher it derives under the salt; a scheme that passes the salt straight to its primitive, as CLWW does, keeps the salt’s bytes (Protected<Vec<u8>>). Read more
Source§

impl Supports<Bytes> for BlockOre

Source§

fn supports(_: Bytes) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<Date> for BlockOre

Source§

fn supports(_: Date) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<Decimal> for BlockOre

Source§

fn supports(_: Decimal) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<F64> for BlockOre

Source§

fn supports(_: F64) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<I16> for BlockOre

Source§

fn supports(_: I16) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<I32> for BlockOre

Source§

fn supports(_: I32) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<I64> for BlockOre

Source§

fn supports(_: I64) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<I128> for BlockOre

Source§

fn supports(_: I128) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<OrderTarget> for BlockOre

Source§

fn supports(target: OrderTarget) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<Text> for BlockOre

Source§

fn supports(_: Text) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<Timestamp> for BlockOre

Source§

fn supports(_: Timestamp) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<U16> for BlockOre

Source§

fn supports(_: U16) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<U32> for BlockOre

Source§

fn supports(_: U32) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<U64> for BlockOre

Source§

fn supports(_: U64) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<U128> for BlockOre

Source§

fn supports(_: U128) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl Supports<Uuid> for BlockOre

Source§

fn supports(_: Uuid) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<S> OrderEncrypt for S
where S: OrderScheme + ?Sized,

Source§

fn target<T>(&self, target: T) -> OrderSpace<'_, Self, T>
where T: Target, Self: Supports<T> + Supports<OrderTarget>,

A space of target values under the empty context: a marker from target, or an OrderTarget chosen while the program runs. Chain context to bind one. Read more
Source§

fn context<'a, C>(&self, context: C) -> OrderContext<'_, Self>
where C: IntoContext<'a>,

The terms that compare within context (a table and column, a tenant, whatever they must compare within): chain target, or encrypt a value whose type is its target.
Source§

fn encrypt<V>( &self, value: V, ) -> Result<OrderTerm<Self::Term, <V as Orderable>::Target>, Self::Error>
where V: Orderable + FitsIn<<V as Orderable>::Target>, Self: Supports<<V as Orderable>::Target> + Supports<OrderTarget>,

Encrypt a value as the target its Rust type is, under the empty context: scheme.target(V::Target).encrypt(value). A one-shot chain makes a space per call, so the scheme opens per value; hold scheme.target(..).context(..) to derive once for many values. Read more
Source§

fn encrypt_checked<V>( &self, value: V, ) -> Result<OrderTerm<Self::Term, <V as Orderable>::Target>, OrderError<Self::Error>>
where V: Orderable, Self: Supports<<V as Orderable>::Target> + Supports<OrderTarget>,

Encrypt a value as the target its Rust type is, checking it while the program runs, under the empty context: scheme.target(V::Target).encrypt_checked(value). Read more
Source§

fn column<'a, C>(&self, target: OrderTarget, context: C) -> OrderSpace<'_, Self>
where C: IntoContext<'a>, Self: Supports<OrderTarget>,

👎Deprecated since 0.7.0:

use scheme.target(target).context(context)

scheme.target(target).context(context), as the 0.6 API spelt it.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<S, T, P> Supports<Nulls<T, P>> for S
where S: Supports<T> + ?Sized, T: FixedWidth, P: NullPolicy,

Source§

fn supports(target: Nulls<T, P>) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl<S> Supports<RuntimeNulls> for S
where S: Supports<OrderTarget> + ?Sized,

Source§

fn supports(target: RuntimeNulls) -> bool

Whether the scheme takes terms of target. For a marker, true (the impl exists); for an OrderTarget, the scheme’s list.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.