Skip to content

Commit 6bae3ff

Browse files
committed
Merge remote-tracking branch 'survived/selectable-arrays' into develop
2 parents 6410953 + 1a2d9e6 commit 6bae3ff

File tree

6 files changed

+78
-7
lines changed

6 files changed

+78
-7
lines changed

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ script:
2727
cargo test --no-default-features --features std &&
2828
cargo test --no-default-features --features "std i128" &&
2929
cargo test --no-default-features --features "std core_hint_black_box" &&
30-
cargo test --no-default-features --features "std i128 core_hint_black_box"
30+
cargo test --no-default-features --features "std const-generics" &&
31+
cargo test --no-default-features --features "std i128 core_hint_black_box" &&
32+
cargo test --no-default-features --features "std i128 core_hint_black_box const-generics"
3133

3234
notifications:
3335
slack:

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ travis-ci = { repository = "dalek-cryptography/subtle", branch = "master"}
2929
rand = { version = "0.8" }
3030

3131
[features]
32+
const-generics = []
3233
core_hint_black_box = []
3334
default = ["std", "i128"]
3435
std = []

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Rust versions from 1.66 or higher support a new best-effort optimization
3030
barrier ([`core::hint::black_box`]). To use the new optimization barrier,
3131
enable the `core_hint_black_box` feature.
3232

33+
Rust versions from 1.51 or higher have const generics support. You may enable
34+
`const-generics` feautre to have `subtle` traits implemented for arrays `[T; N]`.
35+
3336
Versions prior to `2.2` recommended use of the `nightly` feature to enable an
3437
optimization barrier; this is not required in versions `2.2` and above.
3538

fuzz/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cargo-fuzz = true
1010

1111
[dependencies.subtle]
1212
path = ".."
13-
features = ["nightly"]
13+
features = ["nightly", "const-generics"]
1414

1515
[dependencies]
1616
libfuzzer-sys = "0.4"
@@ -42,3 +42,7 @@ name = "conditional_assign_i128"
4242
path = "fuzzers/conditional_assign_i128.rs"
4343
test = false
4444
doc = false
45+
46+
[[bin]]
47+
name = "conditional_assign_array"
48+
path = "fuzzers/conditional_assign_array.rs"
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![no_main]
2+
3+
#[macro_use]
4+
extern crate libfuzzer_sys;
5+
extern crate subtle;
6+
extern crate core;
7+
8+
use core::convert::TryFrom;
9+
10+
use subtle::ConditionallySelectable;
11+
12+
fuzz_target!(|data: &[u8]| {
13+
let chunk_size: usize = 16;
14+
15+
if data.len() % chunk_size != 0 {
16+
return;
17+
}
18+
19+
for bytes in data.chunks(chunk_size) {
20+
let mut x = [0u8; 16];
21+
let y = <[u8; 16]>::try_from(bytes).unwrap();
22+
23+
x.conditional_assign(&y, 0.into());
24+
assert_eq!(x, [0u8; 16]);
25+
26+
x.conditional_assign(&y, 1.into());
27+
assert_eq!(x, y);
28+
}
29+
});

src/lib.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
//! inner `u8` by passing it through a volatile read. For more information, see
4242
//! the _About_ section below.
4343
//!
44+
//! Rust versions from 1.66 or higher support a new best-effort optimization
45+
//! barrier ([`core::hint::black_box`]). To use the new optimization barrier,
46+
//! enable the `core_hint_black_box` feature.
47+
//!
48+
//! Rust versions from 1.51 or higher have const generics support. You may enable
49+
//! `const-generics` feautre to have `subtle` traits implemented for arrays `[T; N]`.
50+
//!
4451
//! Versions prior to `2.2` recommended use of the `nightly` feature to enable an
4552
//! optimization barrier; this is not required in versions `2.2` and above.
4653
//!
@@ -63,10 +70,15 @@
6370
//!
6471
//! This library aims to be the Rust equivalent of Go’s `crypto/subtle` module.
6572
//!
66-
//! The optimization barrier in `impl From<u8> for Choice` was based on Tim
67-
//! Maclean's [work on `rust-timing-shield`][rust-timing-shield], which attempts to
68-
//! provide a more comprehensive approach for preventing software side-channels in
69-
//! Rust code.
73+
//! Old versions of the optimization barrier in `impl From<u8> for Choice` were
74+
//! based on Tim Maclean's [work on `rust-timing-shield`][rust-timing-shield],
75+
//! which attempts to provide a more comprehensive approach for preventing
76+
//! software side-channels in Rust code.
77+
//!
78+
//! From version `2.2`, it was based on Diane Hosfelt and Amber Sprenkels' work on
79+
//! "Secret Types in Rust". Version `2.3` adds the `core_hint_black_box` feature,
80+
//! which uses the original method through the [`core::hint::black_box`] function
81+
//! from the Rust standard library.
7082
//!
7183
//! `subtle` is authored by isis agora lovecruft and Henry de Valence.
7284
//!
@@ -81,6 +93,7 @@
8193
//! **USE AT YOUR OWN RISK**
8294
//!
8395
//! [docs]: https://docs.rs/subtle
96+
//! [`core::hint::black_box`]: https://doc.rust-lang.org/core/hint/fn.black_box.html
8497
//! [rust-timing-shield]: https://www.chosenplaintext.ca/open-source/rust-timing-shield/security
8598
8699
#[cfg(feature = "std")]
@@ -565,6 +578,25 @@ impl ConditionallySelectable for Choice {
565578
}
566579
}
567580

581+
#[cfg(feature = "const-generics")]
582+
impl<T, const N: usize> ConditionallySelectable for [T; N]
583+
where
584+
T: ConditionallySelectable,
585+
{
586+
#[inline]
587+
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
588+
let mut output = *a;
589+
output.conditional_assign(b, choice);
590+
output
591+
}
592+
593+
fn conditional_assign(&mut self, other: &Self, choice: Choice) {
594+
for (a_i, b_i) in self.iter_mut().zip(other) {
595+
a_i.conditional_assign(b_i, choice)
596+
}
597+
}
598+
}
599+
568600
/// A type which can be conditionally negated in constant time.
569601
///
570602
/// # Note
@@ -862,7 +894,7 @@ macro_rules! generate_unsigned_integer_greater {
862894
Choice::from((bit & 1) as u8)
863895
}
864896
}
865-
}
897+
};
866898
}
867899

868900
generate_unsigned_integer_greater!(u8, 8);

0 commit comments

Comments
 (0)