Skip to content

Commit beb7203

Browse files
Remove AES weak key test (#526)
A test for alleged AES weak keys was introduced in #465 based on [TCG documentation](https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=82). But as noted in a [comment](#525 (comment)) to #525, no rationale or further citation for the claim was given in that documentation or was otherwise known when the test was added. Unless there is a known rationale for the weak key claim, it seems unnecessary to retain the test at all. This PR removes it for AES.
1 parent c638cf2 commit beb7203

File tree

7 files changed

+1
-135
lines changed

7 files changed

+1
-135
lines changed

aes/src/armv8.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use cipher::{
2121
AlgorithmName, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncClosure,
2222
BlockCipherEncrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
2323
consts::{self, U16, U24, U32},
24-
crypto_common::WeakKeyError,
2524
};
2625
use core::fmt;
2726

@@ -104,11 +103,6 @@ macro_rules! define_aes_impl {
104103
let decrypt = $name_back_dec::from(encrypt.clone());
105104
Self { encrypt, decrypt }
106105
}
107-
108-
#[inline]
109-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
110-
crate::weak_key_test(&key.0)
111-
}
112106
}
113107

114108
impl From<$name_enc> for $name {
@@ -187,11 +181,6 @@ macro_rules! define_aes_impl {
187181
let backend = $name_back_enc::new(key);
188182
Self { backend }
189183
}
190-
191-
#[inline]
192-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
193-
crate::weak_key_test(&key.0)
194-
}
195184
}
196185

197186
impl BlockSizeUser for $name_enc {
@@ -247,11 +236,6 @@ macro_rules! define_aes_impl {
247236
let backend = encrypt.clone().into();
248237
Self { backend }
249238
}
250-
251-
#[inline]
252-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
253-
crate::weak_key_test(&key.0)
254-
}
255239
}
256240

257241
impl From<$name_enc> for $name_dec {

aes/src/autodetect.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use cipher::{
66
AlgorithmName, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncClosure,
77
BlockCipherEncrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
88
consts::{U16, U24, U32},
9-
crypto_common::WeakKeyError,
109
};
1110
use core::fmt;
1211
use core::mem::ManuallyDrop;
@@ -100,11 +99,6 @@ macro_rules! define_aes_impl {
10099

101100
Self { inner, token }
102101
}
103-
104-
#[inline]
105-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
106-
crate::weak_key_test(&key.0)
107-
}
108102
}
109103

110104
impl Clone for $name {
@@ -204,11 +198,6 @@ macro_rules! define_aes_impl {
204198

205199
Self { inner, token }
206200
}
207-
208-
#[inline]
209-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
210-
crate::weak_key_test(&key.0)
211-
}
212201
}
213202

214203
impl Clone for $name_enc {
@@ -325,11 +314,6 @@ macro_rules! define_aes_impl {
325314

326315
Self { inner, token }
327316
}
328-
329-
#[inline]
330-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
331-
crate::weak_key_test(&key.0)
332-
}
333317
}
334318

335319
impl Clone for $name_dec {

aes/src/lib.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,41 +149,11 @@ cfg_if! {
149149
}
150150

151151
pub use cipher;
152-
use cipher::{array::Array, consts::U16, crypto_common::WeakKeyError};
152+
use cipher::{array::Array, consts::U16};
153153

154154
/// 128-bit AES block
155155
pub type Block = Array<u8, U16>;
156156

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

aes/src/ni.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use cipher::{
3232
AlgorithmName, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncClosure,
3333
BlockCipherEncrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
3434
consts::{self, U16, U24, U32},
35-
crypto_common::WeakKeyError,
3635
};
3736
use core::fmt;
3837

@@ -119,11 +118,6 @@ macro_rules! define_aes_impl {
119118
let decrypt = $name_dec::from(&encrypt);
120119
Self { encrypt, decrypt }
121120
}
122-
123-
#[inline]
124-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
125-
crate::weak_key_test(&key.0)
126-
}
127121
}
128122

129123
impl From<$name_enc> for $name {
@@ -199,11 +193,6 @@ macro_rules! define_aes_impl {
199193
backend: $name_back_enc::new(key),
200194
}
201195
}
202-
203-
#[inline]
204-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
205-
crate::weak_key_test(&key.0)
206-
}
207196
}
208197

209198
impl BlockSizeUser for $name_enc {
@@ -264,11 +253,6 @@ macro_rules! define_aes_impl {
264253
fn new(key: &Key<Self>) -> Self {
265254
$name_enc::new(key).into()
266255
}
267-
268-
#[inline]
269-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
270-
crate::weak_key_test(&key.0)
271-
}
272256
}
273257

274258
impl From<$name_enc> for $name_dec {

aes/src/soft.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use cipher::{
1818
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, Key, KeyInit,
1919
KeySizeUser, ParBlocksSizeUser,
2020
consts::{U16, U24, U32},
21-
crypto_common::WeakKeyError,
2221
inout::InOut,
2322
};
2423
use core::fmt;
@@ -56,11 +55,6 @@ macro_rules! define_aes_impl {
5655
keys: $fixslice_key_schedule(key.into()),
5756
}
5857
}
59-
60-
#[inline]
61-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
62-
crate::weak_key_test(&key.0)
63-
}
6458
}
6559

6660
impl BlockSizeUser for $name {
@@ -133,11 +127,6 @@ macro_rules! define_aes_impl {
133127
let inner = $name::new(key);
134128
Self { inner }
135129
}
136-
137-
#[inline]
138-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
139-
crate::weak_key_test(&key.0)
140-
}
141130
}
142131

143132
impl BlockSizeUser for $name_enc {
@@ -182,11 +171,6 @@ macro_rules! define_aes_impl {
182171
let inner = $name::new(key);
183172
Self { inner }
184173
}
185-
186-
#[inline]
187-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
188-
crate::weak_key_test(&key.0)
189-
}
190174
}
191175

192176
impl From<$name_enc> for $name_dec {

aes/src/x86.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use cipher::{
1818
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
1919
KeyInit, KeySizeUser, ParBlocksSizeUser,
2020
consts::{U8, U16, U24, U32},
21-
crypto_common::WeakKeyError,
2221
};
2322
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
2423
use cipher::{Array, InOutBuf, consts::U30, typenum::Unsigned};
@@ -208,11 +207,6 @@ macro_rules! define_aes_impl {
208207
let decrypt = $name_dec::from(&encrypt);
209208
Self { encrypt, decrypt }
210209
}
211-
212-
#[inline]
213-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
214-
crate::weak_key_test(&key.0)
215-
}
216210
}
217211

218212
impl From<$name_enc> for $name {
@@ -296,11 +290,6 @@ macro_rules! define_aes_impl {
296290
features: Features::new(),
297291
}
298292
}
299-
300-
#[inline]
301-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
302-
crate::weak_key_test(&key.0)
303-
}
304293
}
305294

306295
impl BlockSizeUser for $name_enc {
@@ -371,11 +360,6 @@ macro_rules! define_aes_impl {
371360
fn new(key: &Key<Self>) -> Self {
372361
$name_enc::new(key).into()
373362
}
374-
375-
#[inline]
376-
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
377-
crate::weak_key_test(&key.0)
378-
}
379363
}
380364

381365
impl From<$name_enc> for $name_dec {

aes/tests/weak.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)