Trait BlockCipher

Source
pub trait BlockCipher {
    type Algorithm: CipherAlgorithm;
    type Key: AsRef<[u8]> + AsMut<[u8]> + Clone + Zeroize;

    // Required methods
    fn new(key: &Self::Key) -> Self;
    fn encrypt_block(&self, block: &mut [u8]) -> Result<()>;
    fn decrypt_block(&self, block: &mut [u8]) -> Result<()>;
    fn generate_key<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Key;

    // Provided methods
    fn key_size() -> usize { ... }
    fn block_size() -> usize { ... }
    fn name() -> &'static str { ... }
}
Expand description

Trait for block ciphers with type-level constraints

Required Associated Types§

Source

type Algorithm: CipherAlgorithm

The algorithm this cipher implements

Source

type Key: AsRef<[u8]> + AsMut<[u8]> + Clone + Zeroize

Key type with appropriate size guarantee

Required Methods§

Source

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

Creates a new block cipher instance with the given key

Source

fn encrypt_block(&self, block: &mut [u8]) -> Result<()>

Encrypts a single block in place

Source

fn decrypt_block(&self, block: &mut [u8]) -> Result<()>

Decrypts a single block in place

Source

fn generate_key<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Key

Generate a random key

Provided Methods§

Source

fn key_size() -> usize

Returns the key size in bytes

Source

fn block_size() -> usize

Returns the block size in bytes

Source

fn name() -> &'static str

Returns the name of the block cipher

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§