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,f64and 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
impl BlockOre
Sourcepub fn with_text_padding(self, padding: Padding) -> Self
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.
Sourcepub fn with_max_bytes(self, max_bytes: NonZeroUsize) -> Self
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.
Sourcepub fn text_padding(&self) -> Padding
pub fn text_padding(&self) -> Padding
The padding policy of this scheme’s text spaces.
Sourcepub fn max_bytes(&self) -> NonZeroUsize
pub fn max_bytes(&self) -> NonZeroUsize
The ceiling on text and bytes values.
Trait Implementations§
Source§impl OrderKeyInit for BlockOre
impl OrderKeyInit for BlockOre
Source§type KeyError = WeakKeyError
type KeyError = WeakKeyError
Source§fn new(key: Self::Key) -> Self
fn new(key: Self::Key) -> Self
Source§fn try_from_bytes(key: Protected<Vec<u8>>) -> Result<Self, WeakKeyError>
fn try_from_bytes(key: Protected<Vec<u8>>) -> Result<Self, WeakKeyError>
Source§impl OrderQuery for BlockOre
impl OrderQuery for BlockOre
Source§fn encrypt_query(
&self,
space: &BlockOreSpace,
plaintext: &OrderPlaintext,
) -> Result<Query, OreError>
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>
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§impl OrderScheme for BlockOre
impl OrderScheme for BlockOre
Source§fn open(
&self,
target: OrderTarget,
salt: &Context<'_>,
) -> Result<BlockOreSpace, OreError>
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>
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>
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"
const NAME: &'static str = "block-ore"
"cllw-ore", "cllw-ope", "block-ore".Source§type Error = OreError
type Error = OreError
Infallible
for a scheme that cannot fail once keyed.Source§type Space = BlockOreSpace
type Space = BlockOreSpace
Protected<Vec<u8>>). Read moreSource§impl Supports<OrderTarget> for BlockOre
impl Supports<OrderTarget> for BlockOre
Source§fn supports(target: OrderTarget) -> bool
fn supports(target: OrderTarget) -> bool
target. For a marker, true
(the impl exists); for an OrderTarget, the scheme’s list.Auto Trait Implementations§
impl Forget for BlockOre
impl Freeze for BlockOre
impl Move for BlockOre
impl RefUnwindSafe for BlockOre
impl Send for BlockOre
impl Sync for BlockOre
impl Unpin for BlockOre
impl UnsafeUnpin for BlockOre
impl UnwindSafe for BlockOre
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<S> OrderEncrypt for Swhere
S: OrderScheme + ?Sized,
impl<S> OrderEncrypt for Swhere
S: OrderScheme + ?Sized,
Source§fn target<T>(&self, target: T) -> OrderSpace<'_, Self, T>
fn target<T>(&self, target: T) -> OrderSpace<'_, Self, T>
target values under the empty context: a marker from
target, or an OrderTarget chosen while the
program runs. Chain context to bind one. Read moreSource§fn context<'a, C>(&self, context: C) -> OrderContext<'_, Self>where
C: IntoContext<'a>,
fn context<'a, C>(&self, context: C) -> OrderContext<'_, Self>where
C: IntoContext<'a>,
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>
fn encrypt<V>( &self, value: V, ) -> Result<OrderTerm<Self::Term, <V as Orderable>::Target>, Self::Error>
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 moreSource§fn encrypt_checked<V>(
&self,
value: V,
) -> Result<OrderTerm<Self::Term, <V as Orderable>::Target>, OrderError<Self::Error>>
fn encrypt_checked<V>( &self, value: V, ) -> Result<OrderTerm<Self::Term, <V as Orderable>::Target>, OrderError<Self::Error>>
scheme.target(V::Target).encrypt_checked(value). Read moreSource§fn column<'a, C>(&self, target: OrderTarget, context: C) -> OrderSpace<'_, Self>
fn column<'a, C>(&self, target: OrderTarget, context: C) -> OrderSpace<'_, Self>
use scheme.target(target).context(context)
scheme.target(target).context(context), as the 0.6 API spelt it.Source§impl<S> Supports<RuntimeNulls> for S
impl<S> Supports<RuntimeNulls> for S
Source§fn supports(target: RuntimeNulls) -> bool
fn supports(target: RuntimeNulls) -> bool
target. For a marker, true
(the impl exists); for an OrderTarget, the scheme’s list.