Skip to content

Commit 5ac1ffd

Browse files
authored
aes: tweak weak key test (#469)
The new code results in a better codegen with resulting binaries still being const time: https://rust.godbolt.org/z/x1a8aTsbY
1 parent 717c382 commit 5ac1ffd

File tree

8 files changed

+55
-47
lines changed

8 files changed

+55
-47
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aes/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ categories = ["cryptography", "no-std"]
1515
[dependencies]
1616
cfg-if = "1"
1717
cipher = "=0.5.0-pre.7"
18-
subtle = { version = "2.6", default-features = false }
1918
zeroize = { version = "1.5.6", optional = true, default-features = false, features = [
2019
"aarch64",
2120
] }

aes/src/armv8.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ macro_rules! define_aes_impl {
110110
Self { encrypt, decrypt }
111111
}
112112

113+
#[inline]
113114
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
114-
weak_key_test!(key, Self)
115+
crate::weak_key_test(&key.0)
115116
}
116117
}
117118

@@ -199,8 +200,9 @@ macro_rules! define_aes_impl {
199200
Self { backend }
200201
}
201202

203+
#[inline]
202204
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
203-
weak_key_test!(key, Self)
205+
crate::weak_key_test(&key.0)
204206
}
205207
}
206208

@@ -265,8 +267,9 @@ macro_rules! define_aes_impl {
265267
Self { backend }
266268
}
267269

270+
#[inline]
268271
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
269-
weak_key_test!(key, Self)
272+
crate::weak_key_test(&key.0)
270273
}
271274
}
272275

aes/src/autodetect.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ macro_rules! define_aes_impl {
105105
Self { inner, token }
106106
}
107107

108+
#[inline]
108109
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
109-
weak_key_test!(key, Self)
110+
crate::weak_key_test(&key.0)
110111
}
111112
}
112113

@@ -226,8 +227,9 @@ macro_rules! define_aes_impl {
226227
Self { inner, token }
227228
}
228229

230+
#[inline]
229231
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
230-
weak_key_test!(key, Self)
232+
crate::weak_key_test(&key.0)
231233
}
232234
}
233235

@@ -357,8 +359,9 @@ macro_rules! define_aes_impl {
357359
Self { inner, token }
358360
}
359361

362+
#[inline]
360363
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
361-
weak_key_test!(key, Self)
364+
crate::weak_key_test(&key.0)
362365
}
363366
}
364367

aes/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,41 @@ cfg_if! {
143143
}
144144

145145
pub use cipher;
146-
use cipher::{array::Array, consts::U16};
146+
use cipher::{array::Array, consts::U16, crypto_common::WeakKeyError};
147147

148148
/// 128-bit AES block
149149
pub type Block = Array<u8, U16>;
150150

151+
/// Check if any bit of the upper half of the key is set.
152+
///
153+
/// This follows the interpretation laid out in section `11.4.10.4 Reject of weak keys`
154+
/// from the [TPM specification][0]:
155+
/// ```text
156+
/// In the case of AES, at least one bit in the upper half of the key must be set
157+
/// ```
158+
///
159+
/// [0]: https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=82
160+
pub(crate) fn weak_key_test<const N: usize>(key: &[u8; N]) -> Result<(), WeakKeyError> {
161+
let t = match N {
162+
16 => u64::from_ne_bytes(key[..8].try_into().unwrap()),
163+
24 => {
164+
let t1 = u64::from_ne_bytes(key[..8].try_into().unwrap());
165+
let t2 = u32::from_ne_bytes(key[8..12].try_into().unwrap());
166+
t1 | u64::from(t2)
167+
}
168+
32 => {
169+
let t1 = u64::from_ne_bytes(key[..8].try_into().unwrap());
170+
let t2 = u64::from_ne_bytes(key[8..16].try_into().unwrap());
171+
t1 | t2
172+
}
173+
_ => unreachable!(),
174+
};
175+
match t {
176+
0 => Err(WeakKeyError),
177+
_ => Ok(()),
178+
}
179+
}
180+
151181
#[cfg(test)]
152182
mod tests {
153183
#[cfg(feature = "zeroize")]

aes/src/macros.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,3 @@ macro_rules! impl_backends {
103103
}
104104
};
105105
}
106-
107-
macro_rules! weak_key_test {
108-
($key: expr, $k: ty) => {{
109-
// Check if any bit of the upper half of the key is set
110-
//
111-
// This follows the interpretation laid out in section `11.4.10.4 Reject of weak keys`
112-
// from the TPM specification:
113-
// ```
114-
// In the case of AES, at least one bit in the upper half of the key must be set
115-
// ```
116-
// See: https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=82
117-
let mut weak = subtle::Choice::from(0);
118-
119-
for v in &$key
120-
[..(<<$k as cipher::KeySizeUser>::KeySize as cipher::typenum::Unsigned>::USIZE / 2)]
121-
{
122-
weak |= <_ as subtle::ConstantTimeGreater>::ct_gt(v, &0);
123-
}
124-
125-
if weak.unwrap_u8() == 0 {
126-
Err(cipher::crypto_common::WeakKeyError)
127-
} else {
128-
Ok(())
129-
}
130-
}};
131-
}

aes/src/ni.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ macro_rules! define_aes_impl {
120120
Self { encrypt, decrypt }
121121
}
122122

123+
#[inline]
123124
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
124-
weak_key_test!(key, Self)
125+
crate::weak_key_test(&key.0)
125126
}
126127
}
127128

@@ -199,8 +200,9 @@ macro_rules! define_aes_impl {
199200
}
200201
}
201202

203+
#[inline]
202204
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
203-
weak_key_test!(key, Self)
205+
crate::weak_key_test(&key.0)
204206
}
205207
}
206208

@@ -263,8 +265,9 @@ macro_rules! define_aes_impl {
263265
$name_enc::new(key).into()
264266
}
265267

268+
#[inline]
266269
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
267-
weak_key_test!(key, Self)
270+
crate::weak_key_test(&key.0)
268271
}
269272
}
270273

aes/src/soft.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ macro_rules! define_aes_impl {
6969
}
7070
}
7171

72+
#[inline]
7273
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
73-
weak_key_test!(key, Self)
74+
crate::weak_key_test(&key.0)
7475
}
7576
}
7677

@@ -152,8 +153,9 @@ macro_rules! define_aes_impl {
152153
Self { inner }
153154
}
154155

156+
#[inline]
155157
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
156-
weak_key_test!(key, Self)
158+
crate::weak_key_test(&key.0)
157159
}
158160
}
159161

@@ -207,8 +209,9 @@ macro_rules! define_aes_impl {
207209
Self { inner }
208210
}
209211

212+
#[inline]
210213
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
211-
weak_key_test!(key, Self)
214+
crate::weak_key_test(&key.0)
212215
}
213216
}
214217

0 commit comments

Comments
 (0)