Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from RustCrypto:master #18

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion aes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ categories = ["cryptography", "no-std"]
[dependencies]
cfg-if = "1"
cipher = "=0.5.0-pre.7"
subtle = { version = "2.6", default-features = false }
zeroize = { version = "1.5.6", optional = true, default-features = false, features = [
"aarch64",
] }
Expand Down
9 changes: 6 additions & 3 deletions aes/src/armv8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ macro_rules! define_aes_impl {
Self { encrypt, decrypt }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -199,8 +200,9 @@ macro_rules! define_aes_impl {
Self { backend }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -265,8 +267,9 @@ macro_rules! define_aes_impl {
Self { backend }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down
9 changes: 6 additions & 3 deletions aes/src/autodetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ macro_rules! define_aes_impl {
Self { inner, token }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -226,8 +227,9 @@ macro_rules! define_aes_impl {
Self { inner, token }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -357,8 +359,9 @@ macro_rules! define_aes_impl {
Self { inner, token }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down
32 changes: 31 additions & 1 deletion aes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,41 @@ cfg_if! {
}

pub use cipher;
use cipher::{array::Array, consts::U16};
use cipher::{array::Array, consts::U16, crypto_common::WeakKeyError};

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

/// Check if any bit of the upper half of the key is set.
///
/// This follows the interpretation laid out in section `11.4.10.4 Reject of weak keys`
/// from the [TPM specification][0]:
/// ```text
/// In the case of AES, at least one bit in the upper half of the key must be set
/// ```
///
/// [0]: https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=82
pub(crate) fn weak_key_test<const N: usize>(key: &[u8; N]) -> Result<(), WeakKeyError> {
let t = match N {
16 => u64::from_ne_bytes(key[..8].try_into().unwrap()),
24 => {
let t1 = u64::from_ne_bytes(key[..8].try_into().unwrap());
let t2 = u32::from_ne_bytes(key[8..12].try_into().unwrap());
t1 | u64::from(t2)
}
32 => {
let t1 = u64::from_ne_bytes(key[..8].try_into().unwrap());
let t2 = u64::from_ne_bytes(key[8..16].try_into().unwrap());
t1 | t2
}
_ => unreachable!(),
};
match t {
0 => Err(WeakKeyError),
_ => Ok(()),
}
}

#[cfg(test)]
mod tests {
#[cfg(feature = "zeroize")]
Expand Down
26 changes: 0 additions & 26 deletions aes/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,3 @@ macro_rules! impl_backends {
}
};
}

macro_rules! weak_key_test {
($key: expr, $k: ty) => {{
// Check if any bit of the upper half of the key is set
//
// This follows the interpretation laid out in section `11.4.10.4 Reject of weak keys`
// from the TPM specification:
// ```
// In the case of AES, at least one bit in the upper half of the key must be set
// ```
// See: https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=82
let mut weak = subtle::Choice::from(0);

for v in &$key
[..(<<$k as cipher::KeySizeUser>::KeySize as cipher::typenum::Unsigned>::USIZE / 2)]
{
weak |= <_ as subtle::ConstantTimeGreater>::ct_gt(v, &0);
}

if weak.unwrap_u8() == 0 {
Err(cipher::crypto_common::WeakKeyError)
} else {
Ok(())
}
}};
}
9 changes: 6 additions & 3 deletions aes/src/ni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ macro_rules! define_aes_impl {
Self { encrypt, decrypt }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -199,8 +200,9 @@ macro_rules! define_aes_impl {
}
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -263,8 +265,9 @@ macro_rules! define_aes_impl {
$name_enc::new(key).into()
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down
9 changes: 6 additions & 3 deletions aes/src/soft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ macro_rules! define_aes_impl {
}
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -152,8 +153,9 @@ macro_rules! define_aes_impl {
Self { inner }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down Expand Up @@ -207,8 +209,9 @@ macro_rules! define_aes_impl {
Self { inner }
}

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
weak_key_test!(key, Self)
crate::weak_key_test(&key.0)
}
}

Expand Down
1 change: 0 additions & 1 deletion des/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ categories = ["cryptography", "no-std"]

[dependencies]
cipher = "=0.5.0-pre.7"
subtle = { version = "2.6", default-features = false }

[dev-dependencies]
cipher = { version = "=0.5.0-pre.7", features = ["dev"] }
Expand Down
73 changes: 73 additions & 0 deletions des/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,76 @@ pub const SBOXES: [[u8; 64]; 8] = [
0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11,
],
];

macro_rules! as_ne_u64 {
[$($key:expr,)*] => {
[$(u64::from_ne_bytes($key),)*]
};
}

pub(crate) static WEAK_KEYS: &[u64; 64] = &as_ne_u64![
[0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
[0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE],
[0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1],
[0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E],
[0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E],
[0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01],
[0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1],
[0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01],
[0x01, 0xFE, 0x01, 0xF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