Skip to content

Commit 669d805

Browse files
authored
Relax Clone bounds (#250)
The bounds are necessary only for implementation of `Clone` for buffered types and it can be handled with a simple `#[derive(Clone)]`. Unfortunately, `Clone` is part of `(Reset)MacTraits`, so we have to use a more granular listing of traits.
1 parent 945339b commit 669d805

10 files changed

Lines changed: 63 additions & 60 deletions

File tree

belt-mac/src/block_api.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use belt_block::BeltBlock;
21
use cipher::{BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt};
32
use core::fmt;
43
use digest::{
@@ -17,9 +16,9 @@ use digest::zeroize::{Zeroize, ZeroizeOnDrop};
1716

1817
/// Generic core BeltMac instance, which operates over blocks.
1918
#[derive(Clone)]
20-
pub struct BeltMacCore<C = BeltBlock>
19+
pub struct BeltMacCore<C>
2120
where
22-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
21+
C: BlockCipherEncrypt + SmallBlockSizeUser,
2322
{
2423
cipher: C,
2524
state: Block<C>,
@@ -28,30 +27,30 @@ where
2827

2928
impl<C> BlockSizeUser for BeltMacCore<C>
3029
where
31-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
30+
C: BlockCipherEncrypt + SmallBlockSizeUser,
3231
{
3332
type BlockSize = C::BlockSize;
3433
}
3534

3635
impl<C> OutputSizeUser for BeltMacCore<C>
3736
where
38-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
37+
C: BlockCipherEncrypt + SmallBlockSizeUser,
3938
{
4039
type OutputSize = C::BlockSize;
4140
}
4241

4342
impl<C> InnerUser for BeltMacCore<C>
4443
where
45-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
44+
C: BlockCipherEncrypt + SmallBlockSizeUser,
4645
{
4746
type Inner = C;
4847
}
4948

50-
impl<C> MacMarker for BeltMacCore<C> where C: BlockCipherEncrypt + SmallBlockSizeUser + Clone {}
49+
impl<C> MacMarker for BeltMacCore<C> where C: BlockCipherEncrypt + SmallBlockSizeUser {}
5150

5251
impl<C> InnerInit for BeltMacCore<C>
5352
where
54-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
53+
C: BlockCipherEncrypt + SmallBlockSizeUser,
5554
{
5655
#[inline]
5756
fn inner_init(cipher: C) -> Self {
@@ -64,14 +63,14 @@ where
6463

6564
impl<C> BufferKindUser for BeltMacCore<C>
6665
where
67-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
66+
C: BlockCipherEncrypt + SmallBlockSizeUser,
6867
{
6968
type BufferKind = Lazy;
7069
}
7170

7271
impl<C> UpdateCore for BeltMacCore<C>
7372
where
74-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
73+
C: BlockCipherEncrypt + SmallBlockSizeUser,
7574
{
7675
#[inline]
7776
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
@@ -101,7 +100,7 @@ where
101100

102101
impl<C> Reset for BeltMacCore<C>
103102
where
104-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
103+
C: BlockCipherEncrypt + SmallBlockSizeUser,
105104
{
106105
#[inline(always)]
107106
fn reset(&mut self) {
@@ -111,7 +110,7 @@ where
111110

112111
impl<C> FixedOutputCore for BeltMacCore<C>
113112
where
114-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
113+
C: BlockCipherEncrypt + SmallBlockSizeUser,
115114
{
116115
#[inline]
117116
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
@@ -148,7 +147,7 @@ where
148147

149148
impl<C> AlgorithmName for BeltMacCore<C>
150149
where
151-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
150+
C: BlockCipherEncrypt + SmallBlockSizeUser,
152151
{
153152
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
154153
f.write_str("BeltMac")
@@ -157,7 +156,7 @@ where
157156

158157
impl<C> fmt::Debug for BeltMacCore<C>
159158
where
160-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
159+
C: BlockCipherEncrypt + SmallBlockSizeUser,
161160
{
162161
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163162
f.write_str("BeltMacCore { ... }")
@@ -167,7 +166,7 @@ where
167166
#[cfg(feature = "zeroize")]
168167
impl<C> Drop for BeltMacCore<C>
169168
where
170-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
169+
C: BlockCipherEncrypt + SmallBlockSizeUser,
171170
{
172171
fn drop(&mut self) {
173172
self.state.zeroize();
@@ -176,7 +175,7 @@ where
176175

177176
#[cfg(feature = "zeroize")]
178177
impl<C> ZeroizeOnDrop for BeltMacCore<C> where
179-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone + ZeroizeOnDrop
178+
C: BlockCipherEncrypt + SmallBlockSizeUser + ZeroizeOnDrop
180179
{
181180
}
182181

belt-mac/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use digest::block_api::SmallBlockSizeUser;
1818

1919
digest::buffer_fixed!(
2020
/// BeltMac instance generic over block cipher.
21-
pub struct GenericBeltMac<C: BlockCipherEncrypt + SmallBlockSizeUser + Clone>(block_api::BeltMacCore<C>);
22-
impl: ResetMacTraits AlgorithmName InnerInit;
21+
#[derive(Clone)]
22+
pub struct GenericBeltMac<C: BlockCipherEncrypt + SmallBlockSizeUser>(block_api::BeltMacCore<C>);
23+
impl: BaseFixedTraits MacMarker Reset FixedOutputReset AlgorithmName InnerInit;
2324
);
2425

2526
/// BeltMac instance.

cbc-mac/src/block_api.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,38 @@ use digest::zeroize::{Zeroize, ZeroizeOnDrop};
1818
#[derive(Clone)]
1919
pub struct CbcMacCore<C>
2020
where
21-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
21+
C: BlockCipherEncrypt + SmallBlockSizeUser,
2222
{
2323
cipher: C,
2424
state: Block<C>,
2525
}
2626

2727
impl<C> BlockSizeUser for CbcMacCore<C>
2828
where
29-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
29+
C: BlockCipherEncrypt + SmallBlockSizeUser,
3030
{
3131
type BlockSize = C::BlockSize;
3232
}
3333

3434
impl<C> OutputSizeUser for CbcMacCore<C>
3535
where
36-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
36+
C: BlockCipherEncrypt + SmallBlockSizeUser,
3737
{
3838
type OutputSize = C::BlockSize;
3939
}
4040

4141
impl<C> InnerUser for CbcMacCore<C>
4242
where
43-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
43+
C: BlockCipherEncrypt + SmallBlockSizeUser,
4444
{
4545
type Inner = C;
4646
}
4747

48-
impl<C> MacMarker for CbcMacCore<C> where C: BlockCipherEncrypt + SmallBlockSizeUser + Clone {}
48+
impl<C> MacMarker for CbcMacCore<C> where C: BlockCipherEncrypt + SmallBlockSizeUser {}
4949

5050
impl<C> InnerInit for CbcMacCore<C>
5151
where
52-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
52+
C: BlockCipherEncrypt + SmallBlockSizeUser,
5353
{
5454
#[inline]
5555
fn inner_init(cipher: C) -> Self {
@@ -60,14 +60,14 @@ where
6060

6161
impl<C> BufferKindUser for CbcMacCore<C>
6262
where
63-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
63+
C: BlockCipherEncrypt + SmallBlockSizeUser,
6464
{
6565
type BufferKind = Eager;
6666
}
6767

6868
impl<C> UpdateCore for CbcMacCore<C>
6969
where
70-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
70+
C: BlockCipherEncrypt + SmallBlockSizeUser,
7171
{
7272
#[inline]
7373
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
@@ -97,7 +97,7 @@ where
9797

9898
impl<C> Reset for CbcMacCore<C>
9999
where
100-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
100+
C: BlockCipherEncrypt + SmallBlockSizeUser,
101101
{
102102
#[inline(always)]
103103
fn reset(&mut self) {
@@ -107,7 +107,7 @@ where
107107

108108
impl<C> FixedOutputCore for CbcMacCore<C>
109109
where
110-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
110+
C: BlockCipherEncrypt + SmallBlockSizeUser,
111111
{
112112
#[inline]
113113
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
@@ -123,7 +123,7 @@ where
123123

124124
impl<C> AlgorithmName for CbcMacCore<C>
125125
where
126-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone + AlgorithmName,
126+
C: BlockCipherEncrypt + SmallBlockSizeUser + AlgorithmName,
127127
{
128128
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
129129
f.write_str("CbcMac<")?;
@@ -134,7 +134,7 @@ where
134134

135135
impl<C> fmt::Debug for CbcMacCore<C>
136136
where
137-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone + AlgorithmName,
137+
C: BlockCipherEncrypt + SmallBlockSizeUser + AlgorithmName,
138138
{
139139
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140140
f.write_str("CbcMacCore<")?;
@@ -146,7 +146,7 @@ where
146146
#[cfg(feature = "zeroize")]
147147
impl<C> Drop for CbcMacCore<C>
148148
where
149-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone,
149+
C: BlockCipherEncrypt + SmallBlockSizeUser,
150150
{
151151
fn drop(&mut self) {
152152
self.state.zeroize();
@@ -155,7 +155,7 @@ where
155155

156156
#[cfg(feature = "zeroize")]
157157
impl<C> ZeroizeOnDrop for CbcMacCore<C> where
158-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone + ZeroizeOnDrop
158+
C: BlockCipherEncrypt + SmallBlockSizeUser + ZeroizeOnDrop
159159
{
160160
}
161161

cbc-mac/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ use digest::block_api::CoreProxy;
1818

1919
digest::buffer_fixed!(
2020
/// Generic CBC-MAC instance.
21-
pub struct CbcMac<C: BlockCipherEncrypt + SmallBlockSizeUser + Clone>(block_api::CbcMacCore<C>);
22-
impl: ResetMacTraits InnerInit;
21+
#[derive(Clone)]
22+
pub struct CbcMac<C: BlockCipherEncrypt + SmallBlockSizeUser>(block_api::CbcMacCore<C>);
23+
impl: BaseFixedTraits MacMarker Reset FixedOutputReset InnerInit;
2324
);
2425

2526
impl<C> AlgorithmName for CbcMac<C>
2627
where
27-
C: BlockCipherEncrypt + SmallBlockSizeUser + Clone + AlgorithmName,
28+
C: BlockCipherEncrypt + SmallBlockSizeUser + AlgorithmName,
2829
{
2930
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
3031
<Self as CoreProxy>::Core::write_alg_name(f)

cmac/src/block_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ fn xor<N: ArraySize>(buf: &mut Array<u8, N>, data: &Array<u8, N>) {
140140
}
141141

142142
/// Helper trait implemented for cipher supported by CMAC
143-
pub trait CmacCipher: SmallBlockSizeUser + BlockCipherEncrypt + Clone {
143+
pub trait CmacCipher: SmallBlockSizeUser + BlockCipherEncrypt {
144144
/// Double block. See the [`Dbl`] trait docs for more information.
145145
fn dbl(block: Block<Self>) -> Block<Self>;
146146
}
147147

148148
impl<C> CmacCipher for C
149149
where
150-
Self: SmallBlockSizeUser + BlockCipherEncrypt + Clone,
150+
Self: SmallBlockSizeUser + BlockCipherEncrypt,
151151
Block<Self>: Dbl,
152152
{
153153
fn dbl(block: Block<Self>) -> Block<Self> {

cmac/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use digest::block_api::{AlgorithmName, CoreProxy};
1919

2020
digest::buffer_fixed!(
2121
/// Generic CMAC instance.
22+
#[derive(Clone)]
2223
pub struct Cmac<C: CmacCipher>(block_api::CmacCore<C>);
23-
impl: ResetMacTraits InnerInit;
24+
impl: BaseFixedTraits MacMarker Reset FixedOutputReset InnerInit;
2425
);
2526

2627
impl<C: CmacCipher + AlgorithmName> AlgorithmName for Cmac<C> {

pmac/src/block_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ fn xor<N: ArraySize>(buf: &mut Array<u8, N>, data: &Array<u8, N>) {
217217
///
218218
/// Currently this trait is implemented for all block cipher encryptors
219219
/// with block size equal to 64 and 128 bits.
220-
pub trait PmacCipher: SmallBlockSizeUser + BlockCipherEncrypt + Clone {
220+
pub trait PmacCipher: SmallBlockSizeUser + BlockCipherEncrypt {
221221
/// Double block. See the [`Dbl`] trait docs for more information.
222222
fn dbl(block: Block<Self>) -> Block<Self>;
223223
/// Reverse double block.. See the [`Dbl`] trait docs for more information.
@@ -226,7 +226,7 @@ pub trait PmacCipher: SmallBlockSizeUser + BlockCipherEncrypt + Clone {
226226

227227
impl<C> PmacCipher for C
228228
where
229-
Self: SmallBlockSizeUser + BlockCipherEncrypt + Clone,
229+
Self: SmallBlockSizeUser + BlockCipherEncrypt,
230230
Block<Self>: Dbl,
231231
{
232232
fn dbl(block: Block<Self>) -> Block<Self> {

pmac/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use digest::block_api::{AlgorithmName, CoreProxy};
1919

2020
digest::buffer_fixed!(
2121
/// Generic PMAC instance with `LC_SIZE` = 20.
22+
#[derive(Clone)]
2223
pub struct Pmac<C: PmacCipher>(block_api::PmacCore<C, 20>);
23-
impl: ResetMacTraits InnerInit;
24+
impl: BaseFixedTraits MacMarker Reset FixedOutputReset InnerInit;
2425
);
2526

2627
impl<C: PmacCipher + AlgorithmName> AlgorithmName for Pmac<C> {

0 commit comments

Comments
 (0)