Skip to main content

BlockCipher

Trait BlockCipher 

Source
pub trait BlockCipher<const KEY_LEN: usize, const INIT_DATA_LEN: usize, const BLOCK_LEN: usize>: SymmetricCipher<KEY_LEN, INIT_DATA_LEN> + Sized {
    // Required methods
    fn do_encrypt_init(
        key: &KeyMaterial<KEY_LEN>,
    ) -> Result<(Self, [u8; INIT_DATA_LEN]), SymmetricCipherError>;
    fn do_encrypt_block(
        &mut self,
        plaintext: &[u8; BLOCK_LEN],
    ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>;
    fn do_encrypt_block_out(
        &mut self,
        plaintext: &[u8; BLOCK_LEN],
        ciphertext: &mut [u8; BLOCK_LEN],
    ) -> Result<usize, SymmetricCipherError>;
    fn do_encrypt_final(
        &mut self,
        plaintext: &[u8; BLOCK_LEN],
    ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>;
    fn do_encrypt_final_out(
        &mut self,
        plaintext: &[u8; BLOCK_LEN],
        ciphertext: &mut [u8; BLOCK_LEN],
    ) -> Result<usize, SymmetricCipherError>;
    fn do_decrypt_init(
        key: &KeyMaterial<KEY_LEN>,
        init_data: &[u8; INIT_DATA_LEN],
    ) -> Result<Self, SymmetricCipherError>;
    fn do_decrypt_block(
        &mut self,
        ciphertext: &[u8; BLOCK_LEN],
    ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>;
    fn do_decrypt_block_out(
        &mut self,
        ciphertext: &[u8; BLOCK_LEN],
        plaintext: &mut [u8; BLOCK_LEN],
    ) -> Result<usize, SymmetricCipherError>;
    fn do_decrypt_final(
        &mut self,
        ciphertext: &[u8; BLOCK_LEN],
    ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>;
    fn do_decrypt_final_out(
        &mut self,
        ciphertext: &[u8; BLOCK_LEN],
        plaintext: &mut [u8; BLOCK_LEN],
    ) -> Result<usize, SymmetricCipherError>;
}
Expand description

The basic functions of a block cipher. This trait allows for a block cipher to generate initialization data, such as an Initialization Vector (IV) or Counter (CTR) which is not technically part of the ciphertext, but must be transmitted along with the ciphertext in order for the recipient to perform successful decryption. The length of the initialization data is specified by the implementing struct via the INIT_DATA_LEN constant. In order for these one-shot APIs to be usable securely in all contexts, the init data will be generated securely by the block cipher implementation and returned along with the ciphertext, and there is no API for the user to provide the init data. If you require this functionality, see the documentation for the underlying implementation.

Required Methods§

Source

fn do_encrypt_init( key: &KeyMaterial<KEY_LEN>, ) -> Result<(Self, [u8; INIT_DATA_LEN]), SymmetricCipherError>

Constructor that begins a flow of the streaming API for encrypting one block at a time. Allows for the implementation to return init data such as an IV which is generated prior to encrypting the first block.

Source

fn do_encrypt_block( &mut self, plaintext: &[u8; BLOCK_LEN], ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>

Encrypts a single block of plaintext.

Source

fn do_encrypt_block_out( &mut self, plaintext: &[u8; BLOCK_LEN], ciphertext: &mut [u8; BLOCK_LEN], ) -> Result<usize, SymmetricCipherError>

Encrypts a single block of plaintext and writes the ciphertext to the provided buffer.

Source

fn do_encrypt_final( &mut self, plaintext: &[u8; BLOCK_LEN], ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>

Encrypts the final block of plaintext.

Source

fn do_encrypt_final_out( &mut self, plaintext: &[u8; BLOCK_LEN], ciphertext: &mut [u8; BLOCK_LEN], ) -> Result<usize, SymmetricCipherError>

Encrypts the final block of plaintext and writes the ciphertext to the provided buffer.

Source

fn do_decrypt_init( key: &KeyMaterial<KEY_LEN>, init_data: &[u8; INIT_DATA_LEN], ) -> Result<Self, SymmetricCipherError>

Constructor that begins a flow of the streaming API for decryption one block at a time.

Source

fn do_decrypt_block( &mut self, ciphertext: &[u8; BLOCK_LEN], ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>

Decrypts a single block of ciphertext.

Source

fn do_decrypt_block_out( &mut self, ciphertext: &[u8; BLOCK_LEN], plaintext: &mut [u8; BLOCK_LEN], ) -> Result<usize, SymmetricCipherError>

Decrypts a single block of ciphertext and writes the plaintext to the provided buffer.

Source

fn do_decrypt_final( &mut self, ciphertext: &[u8; BLOCK_LEN], ) -> Result<[u8; BLOCK_LEN], SymmetricCipherError>

Decrypts the final block of ciphertext. This is the decryption counterpart to BlockCipher::do_encrypt_final and is where an implementation validates and strips any padding (or otherwise finalizes the flow).

Source

fn do_decrypt_final_out( &mut self, ciphertext: &[u8; BLOCK_LEN], plaintext: &mut [u8; BLOCK_LEN], ) -> Result<usize, SymmetricCipherError>

Decrypts the final block of ciphertext and writes the plaintext to the provided buffer.

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§