Skip to content

Commit 332f20d

Browse files
committed
Use copy_within for aes-siv/xsalsa20poly1305; MSRV 1.37+
`aes-siv` and `xsalsa20poly1305` use a prepended authentication tag, which means during in-place encryption/decryption, they need the message shifted by the tag size. It would be nice if this could happen during encryption/decryption: https://github.com/RustCrypto/block-ciphers/issues/59 ...but barring that, using `copy_within` (which performs a `memmove` behind the scenes) should be the fastest available option. This commit replaces a previous `for` loop which performed this copy with `copy_within`. Note that `copy_within` is MSRV 1.37+, so this bumps the project MSRV up by a single version (from 1.36).
1 parent 32b39da commit 332f20d

File tree

9 files changed

+13
-27
lines changed

9 files changed

+13
-27
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ matrix:
1919
- rust: nightly
2020
fast_finish: true
2121
include:
22-
- name: "Rust: 1.36.0"
23-
rust: 1.36.0
22+
- name: "Rust: 1.37.0"
23+
rust: 1.37.0
2424
env: {} # clear `-D warnings` above; allow warnings
2525
- name: "Rust: stable (thumbv7em-none-eabihf)"
2626
rust: stable

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ security reviews.
2121
| [XSalsa20Poly1305](https://nacl.cr.yp.to/secretbox.html) | [![crates.io](https://img.shields.io/crates/v/xsalsa20poly1305.svg)](https://crates.io/crates/xsalsa20poly1305) | [![Documentation](https://docs.rs/xsalsa20poly1305/badge.svg)](https://docs.rs/xsalsa20poly1305) |
2222

2323
### Minimum Supported Rust Version
24-
All crates in this repository support Rust 1.36 or higher. In future minimum
24+
All crates in this repository support Rust 1.37 or higher. In future minimum
2525
supported Rust version can be changed, but it will be done with the minor
2626
version bump.
2727

aes-gcm-siv/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ dual licensed as above, without any additional terms or conditions.
5656
[docs-image]: https://docs.rs/aes-gcm-siv/badge.svg
5757
[docs-link]: https://docs.rs/aes-gcm-siv/
5858
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
59-
[rustc-image]: https://img.shields.io/badge/rustc-1.36+-blue.svg
59+
[rustc-image]: https://img.shields.io/badge/rustc-1.37+-blue.svg
6060
[maintenance-image]: https://img.shields.io/badge/maintenance-experimental-blue.svg
6161
[build-image]: https://travis-ci.com/RustCrypto/AEADs.svg?branch=master
6262
[build-link]: https://travis-ci.com/RustCrypto/AEADs

aes-gcm/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dual licensed as above, without any additional terms or conditions.
4242
[docs-image]: https://docs.rs/aes-gcm/badge.svg
4343
[docs-link]: https://docs.rs/aes-gcm/
4444
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
45-
[rustc-image]: https://img.shields.io/badge/rustc-1.36+-blue.svg
45+
[rustc-image]: https://img.shields.io/badge/rustc-1.37+-blue.svg
4646
[maintenance-image]: https://img.shields.io/badge/maintenance-experimental-blue.svg
4747
[build-image]: https://travis-ci.com/RustCrypto/AEADs.svg?branch=master
4848
[build-link]: https://travis-ci.com/RustCrypto/AEADs

aes-siv/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dual licensed as above, without any additional terms or conditions.
4343
[docs-image]: https://docs.rs/aes-siv/badge.svg
4444
[docs-link]: https://docs.rs/aes-siv/
4545
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
46-
[rustc-image]: https://img.shields.io/badge/rustc-1.36+-blue.svg
46+
[rustc-image]: https://img.shields.io/badge/rustc-1.37+-blue.svg
4747
[maintenance-image]: https://img.shields.io/badge/maintenance-experimental-blue.svg
4848
[build-image]: https://travis-ci.com/RustCrypto/AEADs.svg?branch=master
4949
[build-link]: https://travis-ci.com/RustCrypto/AEADs

aes-siv/src/siv.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ where
127127
buffer.extend_from_slice(Tag::default().as_slice())?;
128128

129129
// TODO(tarcieri): add offset param to `encrypt_in_place_detached`
130-
for i in (0..pt_len).rev() {
131-
let byte = buffer.as_ref()[i];
132-
buffer.as_mut()[i + IV_SIZE] = byte;
133-
}
130+
buffer.as_mut().copy_within(..pt_len, IV_SIZE);
134131

135132
let tag = self.encrypt_in_place_detached(headers, &mut buffer.as_mut()[IV_SIZE..])?;
136133
buffer.as_mut()[..IV_SIZE].copy_from_slice(tag.as_slice());
@@ -194,11 +191,7 @@ where
194191
let pt_len = buffer.len() - IV_SIZE;
195192

196193
// TODO(tarcieri): add offset param to `encrypt_in_place_detached`
197-
for i in 0..pt_len {
198-
let byte = buffer.as_ref()[i + IV_SIZE];
199-
buffer.as_mut()[i] = byte;
200-
}
201-
194+
buffer.as_mut().copy_within(IV_SIZE.., 0);
202195
buffer.truncate(pt_len);
203196
Ok(())
204197
}

chacha20poly1305/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ dual licensed as above, without any additional terms or conditions.
4646
[docs-image]: https://docs.rs/chacha20poly1305/badge.svg
4747
[docs-link]: https://docs.rs/chacha20poly1305/
4848
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
49-
[rustc-image]: https://img.shields.io/badge/rustc-1.36+-blue.svg
49+
[rustc-image]: https://img.shields.io/badge/rustc-1.37+-blue.svg
5050
[maintenance-image]: https://img.shields.io/badge/maintenance-experimental-blue.svg
5151
[build-image]: https://travis-ci.com/RustCrypto/AEADs.svg?branch=master
5252
[build-link]: https://travis-ci.com/RustCrypto/AEADs

xsalsa20poly1305/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
![Maintenance Status: Experimental][maintenance-image]
88
[![Build Status][build-image]][build-link]
99

10-
**XSalsa20Poly1305** (a.k.a. NaCl `crypto_secretbox`[1]) is an
10+
**XSalsa20Poly1305** (a.k.a. NaCl [`crypto_secretbox`][1]) is an
1111
[authenticated encryption][2] cipher amenable to fast, constant-time
1212
implementations in software, based on the [Salsa20][3] stream cipher
1313
(with [XSalsa20][4] 192-bit nonce extension) and the [Poly1305][5] universal
@@ -49,7 +49,7 @@ dual licensed as above, without any additional terms or conditions.
4949
[docs-image]: https://docs.rs/xsalsa20poly1305/badge.svg
5050
[docs-link]: https://docs.rs/xsalsa20poly1305/
5151
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
52-
[rustc-image]: https://img.shields.io/badge/rustc-1.36+-blue.svg
52+
[rustc-image]: https://img.shields.io/badge/rustc-1.37+-blue.svg
5353
[maintenance-image]: https://img.shields.io/badge/maintenance-experimental-blue.svg
5454
[build-image]: https://travis-ci.com/RustCrypto/AEADs.svg?branch=master
5555
[build-link]: https://travis-ci.com/RustCrypto/AEADs

xsalsa20poly1305/src/lib.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ impl Aead for XSalsa20Poly1305 {
134134
buffer.extend_from_slice(Tag::default().as_slice())?;
135135

136136
// TODO(tarcieri): add offset param to `encrypt_in_place_detached`
137-
for i in (0..pt_len).rev() {
138-
let byte = buffer.as_ref()[i];
139-
buffer.as_mut()[i + tag_len] = byte;
140-
}
137+
buffer.as_mut().copy_within(..pt_len, tag_len);
141138

142139
let tag = self.encrypt_in_place_detached(
143140
nonce,
@@ -181,11 +178,7 @@ impl Aead for XSalsa20Poly1305 {
181178
let pt_len = buffer.len() - tag_len;
182179

183180
// TODO(tarcieri): add offset param to `encrypt_in_place_detached`
184-
for i in 0..pt_len {
185-
let byte = buffer.as_ref()[i + tag_len];
186-
buffer.as_mut()[i] = byte;
187-
}
188-
181+
buffer.as_mut().copy_within(tag_len.., 0);
189182
buffer.truncate(pt_len);
190183
Ok(())
191184
}

0 commit comments

Comments
 (0)