Skip to content

Commit cc7bd8d

Browse files
authored
des: add subkey equality check to TDES weak key test (#470)
1 parent 5ac1ffd commit cc7bd8d

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

des/src/des.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ impl KeyInit for Des {
5353

5454
#[inline]
5555
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
56-
match super::weak_key_test(&key.0) {
56+
let key = u64::from_ne_bytes(key.0);
57+
match super::weak_key_test(key) {
5758
0 => Ok(()),
5859
_ => Err(WeakKeyError),
5960
}

des/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ pub use crate::tdes::{TdesEde2, TdesEde3, TdesEee2, TdesEee3};
3434
/// Checks whether the key is weak.
3535
///
3636
/// Returns 1 if the key is weak; otherwise, returns 0.
37-
fn weak_key_test(key: &[u8; 8]) -> u8 {
38-
let key = u64::from_ne_bytes(*key);
37+
fn weak_key_test(key: u64) -> u8 {
3938
let mut is_weak = 0u8;
4039
for &weak_key in crate::consts::WEAK_KEYS {
4140
is_weak |= u8::from(key == weak_key);

des/src/tdes.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::{utils::gen_keys, Des};
44
use cipher::{
55
consts::{U1, U16, U24, U8},
66
crypto_common::WeakKeyError,
7-
typenum::Unsigned,
87
AlgorithmName, Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt,
98
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
109
KeyInit, KeySizeUser, ParBlocksSizeUser,
@@ -15,15 +14,34 @@ use core::fmt;
1514
use cipher::zeroize::ZeroizeOnDrop;
1615

1716
#[inline]
18-
fn weak_key_test(key: &[u8]) -> Result<(), WeakKeyError> {
19-
let sub_key_size = <Des as KeySizeUser>::KeySize::USIZE;
20-
assert_eq!(key.len() % sub_key_size, 0);
17+
fn weak_key_test2(key: &[u8; 16]) -> Result<(), WeakKeyError> {
18+
let k1 = u64::from_ne_bytes(key[..8].try_into().unwrap());
19+
let k2 = u64::from_ne_bytes(key[8..16].try_into().unwrap());
2120

2221
let mut is_weak = 0u8;
23-
for des_key in key.chunks_exact(sub_key_size) {
24-
let des_key = des_key.try_into().unwrap();
25-
is_weak |= super::weak_key_test(des_key);
22+
is_weak |= super::weak_key_test(k1);
23+
is_weak |= super::weak_key_test(k2);
24+
is_weak |= u8::from(k1 == k2);
25+
26+
match is_weak {
27+
0 => Ok(()),
28+
_ => Err(WeakKeyError),
2629
}
30+
}
31+
32+
#[inline]
33+
fn weak_key_test3(key: &[u8; 24]) -> Result<(), WeakKeyError> {
34+
let k1 = u64::from_ne_bytes(key[..8].try_into().unwrap());
35+
let k2 = u64::from_ne_bytes(key[8..16].try_into().unwrap());
36+
let k3 = u64::from_ne_bytes(key[16..24].try_into().unwrap());
37+
38+
let mut is_weak = 0u8;
39+
is_weak |= super::weak_key_test(k1);
40+
is_weak |= super::weak_key_test(k2);
41+
is_weak |= super::weak_key_test(k3);
42+
is_weak |= u8::from(k1 == k2);
43+
is_weak |= u8::from(k1 == k3);
44+
is_weak |= u8::from(k2 == k3);
2745

2846
match is_weak {
2947
0 => Ok(()),
@@ -57,7 +75,7 @@ impl KeyInit for TdesEde3 {
5775

5876
#[inline]
5977
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
60-
weak_key_test(key)
78+
weak_key_test3(&key.0)
6179
}
6280
}
6381

@@ -146,7 +164,7 @@ impl KeyInit for TdesEee3 {
146164

147165
#[inline]
148166
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
149-
weak_key_test(key)
167+
weak_key_test3(&key.0)
150168
}
151169
}
152170

@@ -232,7 +250,7 @@ impl KeyInit for TdesEde2 {
232250

233251
#[inline]
234252
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
235-
weak_key_test(key)
253+
weak_key_test2(&key.0)
236254
}
237255
}
238256

@@ -318,7 +336,7 @@ impl KeyInit for TdesEee2 {
318336

319337
#[inline]
320338
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
321-
weak_key_test(key)
339+
weak_key_test2(&key.0)
322340
}
323341
}
324342

des/tests/weak.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ fn weak_des() {
1717
hex!("010101010101010100000000000000000000000000000000"),
1818
hex!("0000000000000000fefefefefefefefe0000000000000000"),
1919
hex!("00000000000000000000000000000000e0e0e0e0f1f1f1f1"),
20+
hex!("010203040506070801020304050607081112131415161718"),
21+
hex!("010203040506070811121314151617180102030405060708"),
22+
hex!("111213141516171801020304050607080102030405060708"),
2023
] {
2124
let k = Key::<TdesEde3>::from(*k);
2225
assert!(TdesEde3::weak_key_test(&k).is_err());
@@ -27,6 +30,7 @@ fn weak_des() {
2730
hex!("01010101010101010000000000000000"),
2831
hex!("0000000000000000fefefefefefefefe"),
2932
hex!("0000000000000000e0e0e0e0f1f1f1f1"),
33+
hex!("01020304050607080102030405060708"),
3034
] {
3135
let k = Key::<TdesEde2>::from(*k);
3236
assert!(TdesEde2::weak_key_test(&k).is_err());

0 commit comments

Comments
 (0)