Skip to content

Commit d583925

Browse files
authored
chacha20: make autodetect unions non-Copy; MSRV 1.49+ (#212)
Uses the new `core::mem::ManuallyDrop` feature to allow the inner types of unions to be non-Copy.
1 parent 03ecd49 commit d583925

6 files changed

Lines changed: 30 additions & 27 deletions

File tree

.github/workflows/chacha20.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
strategy:
2626
matrix:
2727
rust:
28-
- 1.41.0 # MSRV
28+
- 1.49.0 # MSRV
2929
- stable
3030
target:
3131
- thumbv7em-none-eabi
@@ -52,15 +52,15 @@ jobs:
5252
include:
5353
# 32-bit Linux
5454
- target: i686-unknown-linux-gnu
55-
rust: 1.41.0 # MSRV
55+
rust: 1.49.0 # MSRV
5656
deps: sudo apt update && sudo apt install gcc-multilib
5757
- target: i686-unknown-linux-gnu
5858
rust: stable
5959
deps: sudo apt update && sudo apt install gcc-multilib
6060

6161
# 64-bit Linux
6262
- target: x86_64-unknown-linux-gnu
63-
rust: 1.41.0 # MSRV
63+
rust: 1.49.0 # MSRV
6464
- target: x86_64-unknown-linux-gnu
6565
rust: stable
6666
steps:
@@ -89,15 +89,15 @@ jobs:
8989
include:
9090
# 32-bit Linux
9191
- target: i686-unknown-linux-gnu
92-
rust: 1.41.0 # MSRV
92+
rust: 1.49.0 # MSRV
9393
deps: sudo apt update && sudo apt install gcc-multilib
9494
- target: i686-unknown-linux-gnu
9595
rust: stable
9696
deps: sudo apt update && sudo apt install gcc-multilib
9797

9898
# 64-bit Linux
9999
- target: x86_64-unknown-linux-gnu
100-
rust: 1.41.0 # MSRV
100+
rust: 1.49.0 # MSRV
101101
- target: x86_64-unknown-linux-gnu
102102
rust: stable
103103
steps:
@@ -117,23 +117,23 @@ jobs:
117117
- run: cargo test --target ${{ matrix.target }} --release --features zeroize
118118
- run: cargo test --target ${{ matrix.target }} --release --all-features
119119

120-
# Tests for the portable software backend
120+
# Tests for the portable software backend (i.e. `force-soft`)
121121
soft:
122122
runs-on: ubuntu-latest
123123
strategy:
124124
matrix:
125125
include:
126126
# 32-bit Linux
127127
- target: i686-unknown-linux-gnu
128-
rust: 1.41.0 # MSRV
128+
rust: 1.49.0 # MSRV
129129
deps: sudo apt update && sudo apt install gcc-multilib
130130
- target: i686-unknown-linux-gnu
131131
rust: stable
132132
deps: sudo apt update && sudo apt install gcc-multilib
133133

134134
# 64-bit Linux
135135
- target: x86_64-unknown-linux-gnu
136-
rust: 1.41.0 # MSRV
136+
rust: 1.49.0 # MSRV
137137
- target: x86_64-unknown-linux-gnu
138138
rust: stable
139139
steps:
@@ -159,13 +159,13 @@ jobs:
159159
include:
160160
# ARM64
161161
- target: aarch64-unknown-linux-gnu
162-
rust: 1.41.0 # MSRV
162+
rust: 1.49.0 # MSRV
163163
- target: aarch64-unknown-linux-gnu
164164
rust: stable
165165

166166
# PPC32
167167
- target: powerpc-unknown-linux-gnu
168-
rust: 1.41.0 # MSRV
168+
rust: 1.49.0 # MSRV
169169
- target: powerpc-unknown-linux-gnu
170170
rust: stable
171171

.github/workflows/workspace.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ jobs:
1616
- uses: actions/checkout@v1
1717
- uses: actions-rs/toolchain@v1
1818
with:
19-
profile: minimal
20-
toolchain: 1.41.0 # MSRV
19+
toolchain: 1.49.0 # MSRV (highest in repo)
2120
components: clippy
21+
override: true
22+
profile: minimal
2223
- run: cargo clippy --all -- -D warnings
2324

2425
rustfmt:
@@ -30,9 +31,10 @@ jobs:
3031
- name: Install stable toolchain
3132
uses: actions-rs/toolchain@v1
3233
with:
33-
profile: minimal
3434
toolchain: stable
3535
components: rustfmt
36+
override: true
37+
profile: minimal
3638

3739
- name: Run cargo fmt
3840
uses: actions-rs/cargo@v1

chacha20/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ stream cipher itself) are designed to execute in constant time.
6262

6363
## Minimum Supported Rust Version
6464

65-
Rust **1.41** or higher.
65+
Rust **1.49** or higher.
6666

6767
Minimum supported Rust version can be changed in the future, but it will be
6868
done with a minor version bump.
@@ -94,7 +94,7 @@ dual licensed as above, without any additional terms or conditions.
9494
[docs-image]: https://docs.rs/chacha20/badge.svg
9595
[docs-link]: https://docs.rs/chacha20/
9696
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
97-
[rustc-image]: https://img.shields.io/badge/rustc-1.41+-blue.svg
97+
[rustc-image]: https://img.shields.io/badge/rustc-1.49+-blue.svg
9898
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
9999
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260049-stream-ciphers
100100
[build-image]: https://github.com/RustCrypto/stream-ciphers/workflows/chacha20/badge.svg?branch=master&event=push

chacha20/src/backend/autodetect.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use crate::{rounds::Rounds, IV_SIZE, KEY_SIZE, BLOCK_SIZE};
66
use super::{avx2, sse2};
7+
use core::mem::ManuallyDrop;
78

89
/// Size of buffers passed to `generate` and `apply_keystream` for this
910
/// backend, which operates on two blocks in parallel for optimal performance.
@@ -18,8 +19,8 @@ pub struct Core<R: Rounds> {
1819
}
1920

2021
union Inner<R: Rounds> {
21-
avx2: avx2::Core<R>,
22-
sse2: sse2::Core<R>,
22+
avx2: ManuallyDrop<avx2::Core<R>>,
23+
sse2: ManuallyDrop<sse2::Core<R>>,
2324
}
2425

2526
impl<R: Rounds> Core<R> {
@@ -31,11 +32,11 @@ impl<R: Rounds> Core<R> {
3132

3233
let inner = if avx2_present {
3334
Inner {
34-
avx2: avx2::Core::new(key, iv),
35+
avx2: ManuallyDrop::new(avx2::Core::new(key, iv)),
3536
}
3637
} else {
3738
Inner {
38-
sse2: sse2::Core::new(key, iv),
39+
sse2: ManuallyDrop::new(sse2::Core::new(key, iv)),
3940
}
4041
};
4142

@@ -46,9 +47,9 @@ impl<R: Rounds> Core<R> {
4647
#[inline]
4748
pub fn generate(&self, counter: u64, output: &mut [u8]) {
4849
if self.token.get() {
49-
unsafe { self.inner.avx2.generate(counter, output) }
50+
unsafe { (*self.inner.avx2).generate(counter, output) }
5051
} else {
51-
unsafe { self.inner.sse2.generate(counter, output) }
52+
unsafe { (*self.inner.sse2).generate(counter, output) }
5253
}
5354
}
5455

@@ -57,9 +58,9 @@ impl<R: Rounds> Core<R> {
5758
#[cfg(feature = "cipher")]
5859
pub fn apply_keystream(&self, counter: u64, output: &mut [u8]) {
5960
if self.token.get() {
60-
unsafe { self.inner.avx2.apply_keystream(counter, output) }
61+
unsafe { (*self.inner.avx2).apply_keystream(counter, output) }
6162
} else {
62-
unsafe { self.inner.sse2.apply_keystream(counter, output) }
63+
unsafe { (*self.inner.sse2).apply_keystream(counter, output) }
6364
}
6465
}
6566
}
@@ -68,11 +69,11 @@ impl<R: Rounds> Clone for Core<R> {
6869
fn clone(&self) -> Self {
6970
let inner = if self.token.get() {
7071
Inner {
71-
avx2: unsafe { self.inner.avx2 },
72+
avx2: ManuallyDrop::new(unsafe { (*self.inner.avx2).clone() }),
7273
}
7374
} else {
7475
Inner {
75-
sse2: unsafe { self.inner.sse2 },
76+
sse2: ManuallyDrop::new(unsafe { (*self.inner.sse2).clone() }),
7677
}
7778
};
7879

chacha20/src/backend/avx2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::arch::x86_64::*;
1919

2020
/// The ChaCha20 core function (AVX2 accelerated implementation for x86/x86_64)
2121
// TODO(tarcieri): zeroize?
22-
#[derive(Copy, Clone)]
22+
#[derive(Clone)]
2323
pub(crate) struct Core<R: Rounds> {
2424
v0: __m256i,
2525
v1: __m256i,

chacha20/src/backend/sse2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::arch::x86_64::*;
1515

1616
/// The ChaCha20 core function (SSE2 accelerated implementation for x86/x86_64)
1717
// TODO(tarcieri): zeroize?
18-
#[derive(Copy, Clone)]
18+
#[derive(Clone)]
1919
pub struct Core<R: Rounds> {
2020
v0: __m128i,
2121
v1: __m128i,

0 commit comments

Comments
 (0)