Skip to content

Commit 4b24d7f

Browse files
committed
Avoid cloning in the traits wrappers
1 parent ecdd84b commit 4b24d7f

File tree

6 files changed

+48
-96
lines changed

6 files changed

+48
-96
lines changed

src/compat/rustcrypto_traits_06/aegis128l.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl AeadInOut for Aegis128L<16> {
3535
&self,
3636
nonce: &Nonce<Self>,
3737
associated_data: &[u8],
38-
mut buffer: InOutBuf<'_, '_, u8>,
38+
buffer: InOutBuf<'_, '_, u8>,
3939
) -> Result<Tag<Self>> {
40-
let input = buffer.get_in().to_vec();
41-
let output = buffer.get_out();
42-
output.copy_from_slice(&input);
40+
let output = buffer.into_out_with_copied_in();
4341
let state = aegis128l::Aegis128L::<16>::new(self.key.as_ref(), nonce.as_ref());
4442
let tag = state.encrypt_in_place(output, associated_data);
4543
Ok(tag.into())
@@ -49,12 +47,10 @@ impl AeadInOut for Aegis128L<16> {
4947
&self,
5048
nonce: &Nonce<Self>,
5149
associated_data: &[u8],
52-
mut buffer: InOutBuf<'_, '_, u8>,
50+
buffer: InOutBuf<'_, '_, u8>,
5351
tag: &Tag<Self>,
5452
) -> Result<()> {
55-
let input = buffer.get_in().to_vec();
56-
let output = buffer.get_out();
57-
output.copy_from_slice(&input);
53+
let output = buffer.into_out_with_copied_in();
5854
let state = aegis128l::Aegis128L::<16>::new(self.key.as_ref(), nonce.as_ref());
5955
state
6056
.decrypt_in_place(output, tag.as_ref(), associated_data)
@@ -67,11 +63,9 @@ impl AeadInOut for Aegis128L<32> {
6763
&self,
6864
nonce: &Nonce<Self>,
6965
associated_data: &[u8],
70-
mut buffer: InOutBuf<'_, '_, u8>,
66+
buffer: InOutBuf<'_, '_, u8>,
7167
) -> Result<Tag<Self>> {
72-
let input = buffer.get_in().to_vec();
73-
let output = buffer.get_out();
74-
output.copy_from_slice(&input);
68+
let output = buffer.into_out_with_copied_in();
7569
let state = aegis128l::Aegis128L::<32>::new(self.key.as_ref(), nonce.as_ref());
7670
let tag = state.encrypt_in_place(output, associated_data);
7771
Ok(tag.into())
@@ -81,12 +75,10 @@ impl AeadInOut for Aegis128L<32> {
8175
&self,
8276
nonce: &Nonce<Self>,
8377
associated_data: &[u8],
84-
mut buffer: InOutBuf<'_, '_, u8>,
78+
buffer: InOutBuf<'_, '_, u8>,
8579
tag: &Tag<Self>,
8680
) -> Result<()> {
87-
let input = buffer.get_in().to_vec();
88-
let output = buffer.get_out();
89-
output.copy_from_slice(&input);
81+
let output = buffer.into_out_with_copied_in();
9082
let state = aegis128l::Aegis128L::<32>::new(self.key.as_ref(), nonce.as_ref());
9183
state
9284
.decrypt_in_place(output, tag.as_ref(), associated_data)

src/compat/rustcrypto_traits_06/aegis128x2.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl AeadInOut for Aegis128X2<16> {
3535
&self,
3636
nonce: &Nonce<Self>,
3737
associated_data: &[u8],
38-
mut buffer: InOutBuf<'_, '_, u8>,
38+
buffer: InOutBuf<'_, '_, u8>,
3939
) -> Result<Tag<Self>> {
40-
let input = buffer.get_in().to_vec();
41-
let output = buffer.get_out();
42-
output.copy_from_slice(&input);
40+
let output = buffer.into_out_with_copied_in();
4341
let state = aegis128x2::Aegis128X2::<16>::new(self.key.as_ref(), nonce.as_ref());
4442
let tag = state.encrypt_in_place(output, associated_data);
4543
Ok(tag.into())
@@ -49,12 +47,10 @@ impl AeadInOut for Aegis128X2<16> {
4947
&self,
5048
nonce: &Nonce<Self>,
5149
associated_data: &[u8],
52-
mut buffer: InOutBuf<'_, '_, u8>,
50+
buffer: InOutBuf<'_, '_, u8>,
5351
tag: &Tag<Self>,
5452
) -> Result<()> {
55-
let input = buffer.get_in().to_vec();
56-
let output = buffer.get_out();
57-
output.copy_from_slice(&input);
53+
let output = buffer.into_out_with_copied_in();
5854
let state = aegis128x2::Aegis128X2::<16>::new(self.key.as_ref(), nonce.as_ref());
5955
state
6056
.decrypt_in_place(output, tag.as_ref(), associated_data)
@@ -67,11 +63,9 @@ impl AeadInOut for Aegis128X2<32> {
6763
&self,
6864
nonce: &Nonce<Self>,
6965
associated_data: &[u8],
70-
mut buffer: InOutBuf<'_, '_, u8>,
66+
buffer: InOutBuf<'_, '_, u8>,
7167
) -> Result<Tag<Self>> {
72-
let input = buffer.get_in().to_vec();
73-
let output = buffer.get_out();
74-
output.copy_from_slice(&input);
68+
let output = buffer.into_out_with_copied_in();
7569
let state = aegis128x2::Aegis128X2::<32>::new(self.key.as_ref(), nonce.as_ref());
7670
let tag = state.encrypt_in_place(output, associated_data);
7771
Ok(tag.into())
@@ -81,12 +75,10 @@ impl AeadInOut for Aegis128X2<32> {
8175
&self,
8276
nonce: &Nonce<Self>,
8377
associated_data: &[u8],
84-
mut buffer: InOutBuf<'_, '_, u8>,
78+
buffer: InOutBuf<'_, '_, u8>,
8579
tag: &Tag<Self>,
8680
) -> Result<()> {
87-
let input = buffer.get_in().to_vec();
88-
let output = buffer.get_out();
89-
output.copy_from_slice(&input);
81+
let output = buffer.into_out_with_copied_in();
9082
let state = aegis128x2::Aegis128X2::<32>::new(self.key.as_ref(), nonce.as_ref());
9183
state
9284
.decrypt_in_place(output, tag.as_ref(), associated_data)

src/compat/rustcrypto_traits_06/aegis128x4.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl AeadInOut for Aegis128X4<16> {
3535
&self,
3636
nonce: &Nonce<Self>,
3737
associated_data: &[u8],
38-
mut buffer: InOutBuf<'_, '_, u8>,
38+
buffer: InOutBuf<'_, '_, u8>,
3939
) -> Result<Tag<Self>> {
40-
let input = buffer.get_in().to_vec();
41-
let output = buffer.get_out();
42-
output.copy_from_slice(&input);
40+
let output = buffer.into_out_with_copied_in();
4341
let state = aegis128x4::Aegis128X4::<16>::new(self.key.as_ref(), nonce.as_ref());
4442
let tag = state.encrypt_in_place(output, associated_data);
4543
Ok(tag.into())
@@ -49,12 +47,10 @@ impl AeadInOut for Aegis128X4<16> {
4947
&self,
5048
nonce: &Nonce<Self>,
5149
associated_data: &[u8],
52-
mut buffer: InOutBuf<'_, '_, u8>,
50+
buffer: InOutBuf<'_, '_, u8>,
5351
tag: &Tag<Self>,
5452
) -> Result<()> {
55-
let input = buffer.get_in().to_vec();
56-
let output = buffer.get_out();
57-
output.copy_from_slice(&input);
53+
let output = buffer.into_out_with_copied_in();
5854
let state = aegis128x4::Aegis128X4::<16>::new(self.key.as_ref(), nonce.as_ref());
5955
state
6056
.decrypt_in_place(output, tag.as_ref(), associated_data)
@@ -67,11 +63,9 @@ impl AeadInOut for Aegis128X4<32> {
6763
&self,
6864
nonce: &Nonce<Self>,
6965
associated_data: &[u8],
70-
mut buffer: InOutBuf<'_, '_, u8>,
66+
buffer: InOutBuf<'_, '_, u8>,
7167
) -> Result<Tag<Self>> {
72-
let input = buffer.get_in().to_vec();
73-
let output = buffer.get_out();
74-
output.copy_from_slice(&input);
68+
let output = buffer.into_out_with_copied_in();
7569
let state = aegis128x4::Aegis128X4::<32>::new(self.key.as_ref(), nonce.as_ref());
7670
let tag = state.encrypt_in_place(output, associated_data);
7771
Ok(tag.into())
@@ -81,12 +75,10 @@ impl AeadInOut for Aegis128X4<32> {
8175
&self,
8276
nonce: &Nonce<Self>,
8377
associated_data: &[u8],
84-
mut buffer: InOutBuf<'_, '_, u8>,
78+
buffer: InOutBuf<'_, '_, u8>,
8579
tag: &Tag<Self>,
8680
) -> Result<()> {
87-
let input = buffer.get_in().to_vec();
88-
let output = buffer.get_out();
89-
output.copy_from_slice(&input);
81+
let output = buffer.into_out_with_copied_in();
9082
let state = aegis128x4::Aegis128X4::<32>::new(self.key.as_ref(), nonce.as_ref());
9183
state
9284
.decrypt_in_place(output, tag.as_ref(), associated_data)

src/compat/rustcrypto_traits_06/aegis256.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl AeadInOut for Aegis256<16> {
3535
&self,
3636
nonce: &Nonce<Self>,
3737
associated_data: &[u8],
38-
mut buffer: InOutBuf<'_, '_, u8>,
38+
buffer: InOutBuf<'_, '_, u8>,
3939
) -> Result<Tag<Self>> {
40-
let input = buffer.get_in().to_vec();
41-
let output = buffer.get_out();
42-
output.copy_from_slice(&input);
40+
let output = buffer.into_out_with_copied_in();
4341
let state = aegis256::Aegis256::<16>::new(self.key.as_ref(), nonce.as_ref());
4442
let tag = state.encrypt_in_place(output, associated_data);
4543
Ok(tag.into())
@@ -49,12 +47,10 @@ impl AeadInOut for Aegis256<16> {
4947
&self,
5048
nonce: &Nonce<Self>,
5149
associated_data: &[u8],
52-
mut buffer: InOutBuf<'_, '_, u8>,
50+
buffer: InOutBuf<'_, '_, u8>,
5351
tag: &Tag<Self>,
5452
) -> Result<()> {
55-
let input = buffer.get_in().to_vec();
56-
let output = buffer.get_out();
57-
output.copy_from_slice(&input);
53+
let output = buffer.into_out_with_copied_in();
5854
let state = aegis256::Aegis256::<16>::new(self.key.as_ref(), nonce.as_ref());
5955
state
6056
.decrypt_in_place(output, tag.as_ref(), associated_data)
@@ -67,11 +63,9 @@ impl AeadInOut for Aegis256<32> {
6763
&self,
6864
nonce: &Nonce<Self>,
6965
associated_data: &[u8],
70-
mut buffer: InOutBuf<'_, '_, u8>,
66+
buffer: InOutBuf<'_, '_, u8>,
7167
) -> Result<Tag<Self>> {
72-
let input = buffer.get_in().to_vec();
73-
let output = buffer.get_out();
74-
output.copy_from_slice(&input);
68+
let output = buffer.into_out_with_copied_in();
7569
let state = aegis256::Aegis256::<32>::new(self.key.as_ref(), nonce.as_ref());
7670
let tag = state.encrypt_in_place(output, associated_data);
7771
Ok(tag.into())
@@ -81,12 +75,10 @@ impl AeadInOut for Aegis256<32> {
8175
&self,
8276
nonce: &Nonce<Self>,
8377
associated_data: &[u8],
84-
mut buffer: InOutBuf<'_, '_, u8>,
78+
buffer: InOutBuf<'_, '_, u8>,
8579
tag: &Tag<Self>,
8680
) -> Result<()> {
87-
let input = buffer.get_in().to_vec();
88-
let output = buffer.get_out();
89-
output.copy_from_slice(&input);
81+
let output = buffer.into_out_with_copied_in();
9082
let state = aegis256::Aegis256::<32>::new(self.key.as_ref(), nonce.as_ref());
9183
state
9284
.decrypt_in_place(output, tag.as_ref(), associated_data)

src/compat/rustcrypto_traits_06/aegis256x2.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl AeadInOut for Aegis256X2<16> {
3535
&self,
3636
nonce: &Nonce<Self>,
3737
associated_data: &[u8],
38-
mut buffer: InOutBuf<'_, '_, u8>,
38+
buffer: InOutBuf<'_, '_, u8>,
3939
) -> Result<Tag<Self>> {
40-
let input = buffer.get_in().to_vec();
41-
let output = buffer.get_out();
42-
output.copy_from_slice(&input);
40+
let output = buffer.into_out_with_copied_in();
4341
let state = aegis256x2::Aegis256X2::<16>::new(self.key.as_ref(), nonce.as_ref());
4442
let tag = state.encrypt_in_place(output, associated_data);
4543
Ok(tag.into())
@@ -49,12 +47,10 @@ impl AeadInOut for Aegis256X2<16> {
4947
&self,
5048
nonce: &Nonce<Self>,
5149
associated_data: &[u8],
52-
mut buffer: InOutBuf<'_, '_, u8>,
50+
buffer: InOutBuf<'_, '_, u8>,
5351
tag: &Tag<Self>,
5452
) -> Result<()> {
55-
let input = buffer.get_in().to_vec();
56-
let output = buffer.get_out();
57-
output.copy_from_slice(&input);
53+
let output = buffer.into_out_with_copied_in();
5854
let state = aegis256x2::Aegis256X2::<16>::new(self.key.as_ref(), nonce.as_ref());
5955
state
6056
.decrypt_in_place(output, tag.as_ref(), associated_data)
@@ -67,11 +63,9 @@ impl AeadInOut for Aegis256X2<32> {
6763
&self,
6864
nonce: &Nonce<Self>,
6965
associated_data: &[u8],
70-
mut buffer: InOutBuf<'_, '_, u8>,
66+
buffer: InOutBuf<'_, '_, u8>,
7167
) -> Result<Tag<Self>> {
72-
let input = buffer.get_in().to_vec();
73-
let output = buffer.get_out();
74-
output.copy_from_slice(&input);
68+
let output = buffer.into_out_with_copied_in();
7569
let state = aegis256x2::Aegis256X2::<32>::new(self.key.as_ref(), nonce.as_ref());
7670
let tag = state.encrypt_in_place(output, associated_data);
7771
Ok(tag.into())
@@ -81,12 +75,10 @@ impl AeadInOut for Aegis256X2<32> {
8175
&self,
8276
nonce: &Nonce<Self>,
8377
associated_data: &[u8],
84-
mut buffer: InOutBuf<'_, '_, u8>,
78+
buffer: InOutBuf<'_, '_, u8>,
8579
tag: &Tag<Self>,
8680
) -> Result<()> {
87-
let input = buffer.get_in().to_vec();
88-
let output = buffer.get_out();
89-
output.copy_from_slice(&input);
81+
let output = buffer.into_out_with_copied_in();
9082
let state = aegis256x2::Aegis256X2::<32>::new(self.key.as_ref(), nonce.as_ref());
9183
state
9284
.decrypt_in_place(output, tag.as_ref(), associated_data)

src/compat/rustcrypto_traits_06/aegis256x4.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl AeadInOut for Aegis256X4<16> {
3535
&self,
3636
nonce: &Nonce<Self>,
3737
associated_data: &[u8],
38-
mut buffer: InOutBuf<'_, '_, u8>,
38+
buffer: InOutBuf<'_, '_, u8>,
3939
) -> Result<Tag<Self>> {
40-
let input = buffer.get_in().to_vec();
41-
let output = buffer.get_out();
42-
output.copy_from_slice(&input);
40+
let output = buffer.into_out_with_copied_in();
4341
let state = aegis256x4::Aegis256X4::<16>::new(self.key.as_ref(), nonce.as_ref());
4442
let tag = state.encrypt_in_place(output, associated_data);
4543
Ok(tag.into())
@@ -49,12 +47,10 @@ impl AeadInOut for Aegis256X4<16> {
4947
&self,
5048
nonce: &Nonce<Self>,
5149
associated_data: &[u8],
52-
mut buffer: InOutBuf<'_, '_, u8>,
50+
buffer: InOutBuf<'_, '_, u8>,
5351
tag: &Tag<Self>,
5452
) -> Result<()> {
55-
let input = buffer.get_in().to_vec();
56-
let output = buffer.get_out();
57-
output.copy_from_slice(&input);
53+
let output = buffer.into_out_with_copied_in();
5854
let state = aegis256x4::Aegis256X4::<16>::new(self.key.as_ref(), nonce.as_ref());
5955
state
6056
.decrypt_in_place(output, tag.as_ref(), associated_data)
@@ -67,11 +63,9 @@ impl AeadInOut for Aegis256X4<32> {
6763
&self,
6864
nonce: &Nonce<Self>,
6965
associated_data: &[u8],
70-
mut buffer: InOutBuf<'_, '_, u8>,
66+
buffer: InOutBuf<'_, '_, u8>,
7167
) -> Result<Tag<Self>> {
72-
let input = buffer.get_in().to_vec();
73-
let output = buffer.get_out();
74-
output.copy_from_slice(&input);
68+
let output = buffer.into_out_with_copied_in();
7569
let state = aegis256x4::Aegis256X4::<32>::new(self.key.as_ref(), nonce.as_ref());
7670
let tag = state.encrypt_in_place(output, associated_data);
7771
Ok(tag.into())
@@ -81,12 +75,10 @@ impl AeadInOut for Aegis256X4<32> {
8175
&self,
8276
nonce: &Nonce<Self>,
8377
associated_data: &[u8],
84-
mut buffer: InOutBuf<'_, '_, u8>,
78+
buffer: InOutBuf<'_, '_, u8>,
8579
tag: &Tag<Self>,
8680
) -> Result<()> {
87-
let input = buffer.get_in().to_vec();
88-
let output = buffer.get_out();
89-
output.copy_from_slice(&input);
81+
let output = buffer.into_out_with_copied_in();
9082
let state = aegis256x4::Aegis256X4::<32>::new(self.key.as_ref(), nonce.as_ref());
9183
state
9284
.decrypt_in_place(output, tag.as_ref(), associated_data)

0 commit comments

Comments
 (0)