Coverage Report

Created: 2025-07-23 06:36

/rust/registry/src/index.crates.io-6f17d22bba15001f/cbc-0.1.2/src/encrypt.rs
Line
Count
Source
1
use crate::xor;
2
use cipher::{
3
    consts::U1,
4
    crypto_common::{InnerUser, IvSizeUser},
5
    generic_array::{ArrayLength, GenericArray},
6
    inout::InOut,
7
    AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockEncryptMut, BlockSizeUser,
8
    InnerIvInit, Iv, IvState, ParBlocksSizeUser,
9
};
10
use core::fmt;
11
12
#[cfg(feature = "zeroize")]
13
use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
14
15
/// CBC mode encryptor.
16
#[derive(Clone)]
17
pub struct Encryptor<C>
18
where
19
    C: BlockEncryptMut + BlockCipher,
20
{
21
    cipher: C,
22
    iv: Block<C>,
23
}
24
25
impl<C> BlockSizeUser for Encryptor<C>
26
where
27
    C: BlockEncryptMut + BlockCipher,
28
{
29
    type BlockSize = C::BlockSize;
30
}
31
32
impl<C> BlockEncryptMut for Encryptor<C>
33
where
34
    C: BlockEncryptMut + BlockCipher,
35
{
36
25.5k
    fn encrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
37
25.5k
        let Self { cipher, iv } = self;
38
25.5k
        cipher.encrypt_with_backend_mut(Closure { iv, f })
39
25.5k
    }
40
}
41
42
impl<C> InnerUser for Encryptor<C>
43
where
44
    C: BlockEncryptMut + BlockCipher,
45
{
46
    type Inner = C;
47
}
48
49
impl<C> IvSizeUser for Encryptor<C>
50
where
51
    C: BlockEncryptMut + BlockCipher,
52
{
53
    type IvSize = C::BlockSize;
54
}
55
56
impl<C> InnerIvInit for Encryptor<C>
57
where
58
    C: BlockEncryptMut + BlockCipher,
59
{
60
    #[inline]
61
2.13k
    fn inner_iv_init(cipher: C, iv: &Iv<Self>) -> Self {
62
2.13k
        Self {
63
2.13k
            cipher,
64
2.13k
            iv: iv.clone(),
65
2.13k
        }
66
2.13k
    }
67
}
68
69
impl<C> IvState for Encryptor<C>
70
where
71
    C: BlockEncryptMut + BlockCipher,
72
{
73
    #[inline]
74
    fn iv_state(&self) -> Iv<Self> {
75
        self.iv.clone()
76
    }
77
}
78
79
impl<C> AlgorithmName for Encryptor<C>
80
where
81
    C: BlockEncryptMut + BlockCipher + AlgorithmName,
82
{
83
    fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
84
        f.write_str("cbc::Encryptor<")?;
85
        <C as AlgorithmName>::write_alg_name(f)?;
86
        f.write_str(">")
87
    }
88
}
89
90
impl<C> fmt::Debug for Encryptor<C>
91
where
92
    C: BlockEncryptMut + BlockCipher + AlgorithmName,
93
{
94
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95
        f.write_str("cbc::Encryptor<")?;
96
        <C as AlgorithmName>::write_alg_name(f)?;
97
        f.write_str("> { ... }")
98
    }
99
}
100
101
#[cfg(feature = "zeroize")]
102
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
103
impl<C: BlockEncryptMut + BlockCipher> Drop for Encryptor<C> {
104
    fn drop(&mut self) {
105
        self.iv.zeroize();
106
    }
107
}
108
109
#[cfg(feature = "zeroize")]
110
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
111
impl<C: BlockEncryptMut + BlockCipher + ZeroizeOnDrop> ZeroizeOnDrop for Encryptor<C> {}
112
113
struct Closure<'a, BS, BC>
114
where
115
    BS: ArrayLength<u8>,
116
    BC: BlockClosure<BlockSize = BS>,
117
{
118
    iv: &'a mut GenericArray<u8, BS>,
119
    f: BC,
120
}
121
122
impl<'a, BS, BC> BlockSizeUser for Closure<'a, BS, BC>
123
where
124
    BS: ArrayLength<u8>,
125
    BC: BlockClosure<BlockSize = BS>,
126
{
127
    type BlockSize = BS;
128
}
129
130
impl<'a, BS, BC> BlockClosure for Closure<'a, BS, BC>
131
where
132
    BS: ArrayLength<u8>,
133
    BC: BlockClosure<BlockSize = BS>,
134
{
135
    #[inline(always)]
136
25.5k
    fn call<B: BlockBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
137
25.5k
        let Self { iv, f } = self;
138
25.5k
        f.call(&mut Backend { iv, backend });
139
25.5k
    }
<cbc::encrypt::Closure<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, cipher::block::BlockCtx<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>> as cipher::block::BlockClosure>::call::<aes::ni::Aes256BackEnc>
Line
Count
Source
136
25.5k
    fn call<B: BlockBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
137
25.5k
        let Self { iv, f } = self;
138
25.5k
        f.call(&mut Backend { iv, backend });
139
25.5k
    }
Unexecuted instantiation: <cbc::encrypt::Closure<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, cipher::block::BlockCtx<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>> as cipher::block::BlockClosure>::call::<aes::soft::Aes256BackEnc>
140
}
141
142
struct Backend<'a, BS, BK>
143
where
144
    BS: ArrayLength<u8>,
145
    BK: BlockBackend<BlockSize = BS>,
146
{
147
    iv: &'a mut GenericArray<u8, BS>,
148
    backend: &'a mut BK,
149
}
150
151
impl<'a, BS, BK> BlockSizeUser for Backend<'a, BS, BK>
152
where
153
    BS: ArrayLength<u8>,
154
    BK: BlockBackend<BlockSize = BS>,
155
{
156
    type BlockSize = BS;
157
}
158
159
impl<'a, BS, BK> ParBlocksSizeUser for Backend<'a, BS, BK>
160
where
161
    BS: ArrayLength<u8>,
162
    BK: BlockBackend<BlockSize = BS>,
163
{
164
    type ParBlocksSize = U1;
165
}
166
167
impl<'a, BS, BK> BlockBackend for Backend<'a, BS, BK>
168
where
169
    BS: ArrayLength<u8>,
170
    BK: BlockBackend<BlockSize = BS>,
171
{
172
    #[inline(always)]
173
25.5k
    fn proc_block(&mut self, mut block: InOut<'_, '_, Block<Self>>) {
174
25.5k
        let mut t = block.clone_in();
175
25.5k
        xor(&mut t, self.iv);
176
25.5k
        self.backend.proc_block((&mut t).into());
177
25.5k
        *self.iv = t.clone();
178
25.5k
        *block.get_out() = t;
179
25.5k
    }
<cbc::encrypt::Backend<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, aes::ni::Aes256BackEnc> as cipher::block::BlockBackend>::proc_block
Line
Count
Source
173
25.5k
    fn proc_block(&mut self, mut block: InOut<'_, '_, Block<Self>>) {
174
25.5k
        let mut t = block.clone_in();
175
25.5k
        xor(&mut t, self.iv);
176
25.5k
        self.backend.proc_block((&mut t).into());
177
25.5k
        *self.iv = t.clone();
178
25.5k
        *block.get_out() = t;
179
25.5k
    }
Unexecuted instantiation: <cbc::encrypt::Backend<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, aes::soft::Aes256BackEnc> as cipher::block::BlockBackend>::proc_block
180
}