1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use BlockCipher;
/// An initialized mode of operation over an underlying block cipher.
///
/// Mode initialization uses
/// [`BlockCipherInit`](tc_block_cipher::BlockCipherInit) and processing uses
/// [`BlockCipher`]. This trait adds the mode-specific operations from Bouncy
/// Castle's `IBlockCipherMode` and is usable as `dyn BlockCipherMode`.
///
/// # Example
///
/// Generic code can restart any mode from its IV:
///
/// ```
/// use tc_aes::AesEngine;
/// use tc_block_cipher::{BlockCipher, BlockCipherInit, CipherDirection};
/// use tc_block_modes::{BlockCipherMode, FixedCtrBlockCipher, KeyWithIvRef};
///
/// fn first_block_twice<M: BlockCipherMode>(mode: &mut M) -> Result<bool, M::Error> {
/// let (mut first, mut again) = ([0; 16], [0; 16]);
/// mode.process_block(&[0; 16], &mut first)?;
/// mode.reset();
/// mode.process_block(&[0; 16], &mut again)?;
/// Ok(first == again)
/// }
///
/// let (key, nonce) = ([0x42; 16], [0x24; 12]);
/// let mut mode = FixedCtrBlockCipher::<_, 16>::new(AesEngine::new());
/// mode.init(CipherDirection::Encrypt, &KeyWithIvRef::new(&key, &nonce))?;
/// assert!(first_block_twice(&mut mode)?);
/// assert!(mode.is_partial_block_okay());
/// assert_eq!(mode.underlying_cipher().to_string(), "AES");
/// # Ok::<(), Box<dyn core::error::Error>>(())
/// ```