cipher/
block.rs

1//! Traits used to define functionality of [block ciphers][1].
2//!
3//! # About block ciphers
4//!
5//! Block ciphers are keyed, deterministic permutations of a fixed-sized input
6//! "block" providing a reversible transformation to/from an encrypted output.
7//! They are one of the fundamental structural components of [symmetric cryptography][2].
8//!
9//! [1]: https://en.wikipedia.org/wiki/Block_cipher
10//! [2]: https://en.wikipedia.org/wiki/Symmetric-key_algorithm
11
12use crate::errors::InvalidLength;
13use core::convert::TryInto;
14use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
15
16/// Key for an algorithm that implements [`NewBlockCipher`].
17pub type BlockCipherKey<B> = GenericArray<u8, <B as NewBlockCipher>::KeySize>;
18
19/// Block on which a [`BlockCipher`] operates.
20pub type Block<B> = GenericArray<u8, <B as BlockCipher>::BlockSize>;
21
22/// Block on which a [`BlockCipher`] operates in parallel.
23pub type ParBlocks<B> = GenericArray<Block<B>, <B as BlockCipher>::ParBlocks>;
24
25/// Instantiate a [`BlockCipher`] algorithm.
26pub trait NewBlockCipher: Sized {
27    /// Key size in bytes with which cipher guaranteed to be initialized.
28    type KeySize: ArrayLength<u8>;
29
30    /// Create new block cipher instance from key with fixed size.
31    fn new(key: &BlockCipherKey<Self>) -> Self;
32
33    /// Create new block cipher instance from key with variable size.
34    ///
35    /// Default implementation will accept only keys with length equal to
36    /// `KeySize`, but some ciphers can accept range of key lengths.
37    fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
38        if key.len() != Self::KeySize::to_usize() {
39            Err(InvalidLength)
40        } else {
41            Ok(Self::new(GenericArray::from_slice(key)))
42        }
43    }
44}
45
46/// Trait which marks a type as being a block cipher.
47pub trait BlockCipher {
48    /// Size of the block in bytes
49    type BlockSize: ArrayLength<u8>;
50
51    /// Number of blocks which can be processed in parallel by
52    /// cipher implementation
53    type ParBlocks: ArrayLength<Block<Self>>;
54}
55
56/// Encrypt-only functionality for block ciphers.
57pub trait BlockEncrypt: BlockCipher {
58    /// Encrypt block in-place
59    fn encrypt_block(&self, block: &mut Block<Self>);
60
61    /// Encrypt several blocks in parallel using instruction level parallelism
62    /// if possible.
63    ///
64    /// If `ParBlocks` equals to 1 it's equivalent to `encrypt_block`.
65    #[inline]
66    fn encrypt_par_blocks(&self, blocks: &mut ParBlocks<Self>) {
67        for block in blocks.iter_mut() {
68            self.encrypt_block(block);
69        }
70    }
71
72    /// Encrypt a slice of blocks, leveraging parallelism when available.
73    #[inline]
74    fn encrypt_blocks(&self, mut blocks: &mut [Block<Self>]) {
75        let pb = Self::ParBlocks::to_usize();
76
77        if pb > 1 {
78            let mut iter = blocks.chunks_exact_mut(pb);
79
80            for chunk in &mut iter {
81                self.encrypt_par_blocks(chunk.try_into().unwrap())
82            }
83
84            blocks = iter.into_remainder();
85        }
86
87        for block in blocks {
88            self.encrypt_block(block);
89        }
90    }
91}
92
93/// Decrypt-only functionality for block ciphers.
94pub trait BlockDecrypt: BlockCipher {
95    /// Decrypt block in-place
96    fn decrypt_block(&self, block: &mut Block<Self>);
97
98    /// Decrypt several blocks in parallel using instruction level parallelism
99    /// if possible.
100    ///
101    /// If `ParBlocks` equals to 1 it's equivalent to `decrypt_block`.
102    #[inline]
103    fn decrypt_par_blocks(&self, blocks: &mut ParBlocks<Self>) {
104        for block in blocks.iter_mut() {
105            self.decrypt_block(block);
106        }
107    }
108
109    /// Decrypt a slice of blocks, leveraging parallelism when available.
110    #[inline]
111    fn decrypt_blocks(&self, mut blocks: &mut [Block<Self>]) {
112        let pb = Self::ParBlocks::to_usize();
113
114        if pb > 1 {
115            let mut iter = blocks.chunks_exact_mut(pb);
116
117            for chunk in &mut iter {
118                self.decrypt_par_blocks(chunk.try_into().unwrap())
119            }
120
121            blocks = iter.into_remainder();
122        }
123
124        for block in blocks {
125            self.decrypt_block(block);
126        }
127    }
128}
129
130/// Encrypt-only functionality for block ciphers with mutable access to `self`.
131///
132/// The main use case for this trait is hardware encryption engines which
133/// require `&mut self` access to an underlying hardware peripheral.
134pub trait BlockEncryptMut: BlockCipher {
135    /// Encrypt block in-place
136    fn encrypt_block_mut(&mut self, block: &mut Block<Self>);
137}
138
139/// Decrypt-only functionality for block ciphers with mutable access to `self`.
140///
141/// The main use case for this trait is hardware encryption engines which
142/// require `&mut self` access to an underlying hardware peripheral.
143pub trait BlockDecryptMut: BlockCipher {
144    /// Decrypt block in-place
145    fn decrypt_block_mut(&mut self, block: &mut Block<Self>);
146}
147
148impl<Alg: BlockEncrypt> BlockEncryptMut for Alg {
149    fn encrypt_block_mut(&mut self, block: &mut Block<Self>) {
150        self.encrypt_block(block);
151    }
152}
153
154impl<Alg: BlockDecrypt> BlockDecryptMut for Alg {
155    fn decrypt_block_mut(&mut self, block: &mut Block<Self>) {
156        self.decrypt_block(block);
157    }
158}
159
160// Impls of block cipher traits for reference types
161
162impl<Alg: BlockCipher> BlockCipher for &Alg {
163    type BlockSize = Alg::BlockSize;
164    type ParBlocks = Alg::ParBlocks;
165}
166
167impl<Alg: BlockEncrypt> BlockEncrypt for &Alg {
168    #[inline]
169    fn encrypt_block(&self, block: &mut Block<Self>) {
170        Alg::encrypt_block(self, block);
171    }
172
173    #[inline]
174    fn encrypt_par_blocks(&self, blocks: &mut ParBlocks<Self>) {
175        Alg::encrypt_par_blocks(self, blocks);
176    }
177
178    #[inline]
179    fn encrypt_blocks(&self, blocks: &mut [Block<Self>]) {
180        Alg::encrypt_blocks(self, blocks);
181    }
182}
183
184impl<Alg: BlockDecrypt> BlockDecrypt for &Alg {
185    #[inline]
186    fn decrypt_block(&self, block: &mut Block<Self>) {
187        Alg::decrypt_block(self, block);
188    }
189
190    #[inline]
191    fn decrypt_par_blocks(&self, blocks: &mut ParBlocks<Self>) {
192        Alg::decrypt_par_blocks(self, blocks);
193    }
194
195    #[inline]
196    fn decrypt_blocks(&self, blocks: &mut [Block<Self>]) {
197        Alg::decrypt_blocks(self, blocks);
198    }
199}