DeckFunction

Trait DeckFunction 

Source
pub trait DeckFunction {
    type OutputGenerator: CryptoReader;
    type InputWriter<'a>: Writer
       where Self: 'a;

    // Required methods
    fn init(key: &[u8; 32]) -> Self;
    fn input_writer<'a>(&'a mut self) -> Self::InputWriter<'a>;
    fn output_reader(&self) -> Self::OutputGenerator;
}
Expand description

A doubly-ended cryptographic keyed function.

A deck function is a Doubly Extendable Cryptographic Keyed function. It allows repeatedly inputting and outputting variable length streams of data. To input a stream of data, create an input writer using Self::input_writer. Data can then be written to it using the methods of the Writer trait. Writes to separate input writers are domain separated; writes to a single input writer are concatenated. To generate an output stream, create an output generator using Self::output_reader. An output stream can be generated from it using the methods of the Reader trait. Creating an output reader and reading from it does not mutate the state of the original deck function. Inputting new data into the deck function does not change an already existing output generator.

§Crypto

When a secret uniformly chosen key is used to initialise the deck function (using Self::init), the output the output generator generates should be secure as a pseudo-random function from on the input data, i.e. indistinguishable from a truly random function. Identical (key, input) pairs give identical output (determinism).

§Warning

This is a relatively low-level API and very flexible so you can easily create many modes on top of it, but it is not misuse resistant. In particular calling the output method doesn’t modify the state, and calling it twice on the same state gives identical output streams.

Required Associated Types§

Source

type OutputGenerator: CryptoReader

Source

type InputWriter<'a>: Writer where Self: 'a

Writer that inputs data that is written to it to the deck function.

Required Methods§

Source

fn init(key: &[u8; 32]) -> Self

Create a deck function from a 256 bit secret key.

Source

fn input_writer<'a>(&'a mut self) -> Self::InputWriter<'a>

Create a writer that inputs data into the deck function.

Every input writer starts writing a new stream, i.e. domain separation between input streams is applied. Separate writes to the same input writer are not domain separated, but just concatenated. In code:

// assume `deck1` is a `DeckFunction`
let mut deck2 = deck1.clone();
let mut deck3 = deck1.clone();

let mut writer1 = deck1.input_writer();
writer1.write_bytes(b"hello world");
writer1.finish();

let mut writer2 = deck2.input_writer();
writer2.write_bytes(b"hello");
writer2.write_bytes(b" world");
writer2.finish();

let mut writer3 = deck3.input_writer();
writer3.write_bytes(b"hello");
writer3.finish();
let mut writer3 = deck3.input_writer();
writer3.write_bytes(b" world");
writer3.finish();

assert_eq!(deck1, deck2);
assert_ne!(deck2, deck3);
Source

fn output_reader(&self) -> Self::OutputGenerator

Create an output generator from the current state.

§Warning

Never create an output generator from the same state twice, without inputting new data in between. These would generate identical output streams.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§