Skip to content

Commit bd282be

Browse files
Merge pull request #96 from dsprenkels/black-box
Replace black_box with std::hint::black_box
2 parents 72993f2 + 0dfc572 commit bd282be

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ matrix:
2525
script:
2626
- cargo test && cargo test --no-default-features &&
2727
cargo test --no-default-features --features std &&
28-
cargo test --no-default-features --features "std i128"
28+
cargo test --no-default-features --features "std i128" &&
29+
cargo test --no-default-features --features "std core_hint_black_box" &&
30+
cargo test --no-default-features --features "std i128 core_hint_black_box"
2931

3032
notifications:
3133
slack:

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ travis-ci = { repository = "dalek-cryptography/subtle", branch = "master"}
2828
rand = { version = "0.7" }
2929

3030
[features]
31+
core_hint_black_box = []
3132
default = ["std", "i128"]
3233
std = []
3334
i128 = []

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ prevent this refinement, the crate tries to hide the value of a `Choice`'s
2626
inner `u8` by passing it through a volatile read. For more information, see
2727
the _About_ section below.
2828

29+
Rust versions from 1.66 or higher support a new best-effort optimization
30+
barrier ([`core::hint::black_box`]). To use the new optimization barrier,
31+
enable the `core_hint_black_box` feature.
32+
2933
Versions prior to `2.2` recommended use of the `nightly` feature to enable an
3034
optimization barrier; this is not required in versions `2.2` and above.
3135

@@ -48,10 +52,15 @@ Minimum supported Rust version can be changed in the future, but it will be done
4852

4953
This library aims to be the Rust equivalent of Go’s `crypto/subtle` module.
5054

51-
The optimization barrier in `impl From<u8> for Choice` was based on Tim
52-
Maclean's [work on `rust-timing-shield`][rust-timing-shield], which attempts to
53-
provide a more comprehensive approach for preventing software side-channels in
54-
Rust code.
55+
Old versions of the optimization barrier in `impl From<u8> for Choice` were
56+
based on Tim Maclean's [work on `rust-timing-shield`][rust-timing-shield],
57+
which attempts to provide a more comprehensive approach for preventing
58+
software side-channels in Rust code.
59+
60+
From version `2.2`, it was based on Diane Hosfelt and Amber Sprenkels' work on
61+
"Secret Types in Rust". Version `2.3` adds the `core_hint_black_box` feature,
62+
which uses the original method through the [`core::hint::black_box`] function
63+
from the Rust standard library.
5564

5665
`subtle` is authored by isis agora lovecruft and Henry de Valence.
5766

@@ -66,4 +75,5 @@ effort is fundamentally limited.
6675
**USE AT YOUR OWN RISK**
6776

6877
[docs]: https://docs.rs/subtle
78+
[`core::hint::black_box`]: https://doc.rust-lang.org/core/hint/fn.black_box.html
6979
[rust-timing-shield]: https://www.chosenplaintext.ca/open-source/rust-timing-shield/security

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl Not for Choice {
210210
/// Note: Rust's notion of "volatile" is subject to change over time. While this
211211
/// code may break in a non-destructive way in the future, “constant-time” code
212212
/// is a continually moving target, and this is better than doing nothing.
213+
#[cfg(not(feature = "core_hint_black_box"))]
213214
#[inline(never)]
214215
fn black_box(input: u8) -> u8 {
215216
debug_assert!((input == 0u8) | (input == 1u8));
@@ -227,6 +228,13 @@ fn black_box(input: u8) -> u8 {
227228
}
228229
}
229230

231+
#[cfg(feature = "core_hint_black_box")]
232+
#[inline]
233+
fn black_box(input: u8) -> u8 {
234+
debug_assert!((input == 0u8) | (input == 1u8));
235+
core::hint::black_box(input)
236+
}
237+
230238
impl From<u8> for Choice {
231239
#[inline]
232240
fn from(input: u8) -> Choice {

0 commit comments

Comments
 (0)