literate_crypto/cipher/
block.rs

1pub mod aes;
2mod modes;
3mod padding;
4
5pub use {
6    aes::{Aes128, Aes192, Aes256},
7    modes::{BlockMode, BlockSizeTooSmall, Cbc, Ctr, Ecb},
8    padding::{Padding, Pkcs7},
9};
10
11/// A block cipher encrypts and decrypts data in blocks of fixed size.
12///
13/// Note that a block cipher alone does not fulfill the definition of a
14/// [cipher](crate::Cipher), because it can't handle inputs of arbitrary length.
15/// To be a cipher, a block cipher must be used with a
16/// [block mode](crate::BlockMode).
17///
18/// The encrypt and decrypt methods must fulfill the same contract as those in
19/// the [`crate::Cipher`] trait.
20pub trait BlockCipher:
21    BlockEncrypt<EncryptionBlock = Self::Block, EncryptionKey = Self::Key>
22    + BlockDecrypt<DecryptionBlock = Self::Block, DecryptionKey = Self::Key>
23{
24    type Block;
25    type Key;
26}
27
28/// The encryption half of a [block cipher](BlockCipher).
29pub trait BlockEncrypt {
30    type EncryptionBlock;
31    type EncryptionKey;
32
33    /// Encrypt the plaintext.
34    fn encrypt(
35        &self,
36        data: Self::EncryptionBlock,
37        key: Self::EncryptionKey,
38    ) -> Self::EncryptionBlock;
39}
40
41/// The decryption half of a [block cipher](BlockCipher).
42pub trait BlockDecrypt {
43    type DecryptionBlock;
44    type DecryptionKey;
45
46    /// Decrypt the ciphertext.
47    fn decrypt(
48        &self,
49        data: Self::DecryptionBlock,
50        key: Self::DecryptionKey,
51    ) -> Self::DecryptionBlock;
52}