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

crypto-common: test for weak keys / IVs #1739

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 52 additions & 0 deletions crypto-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,30 @@ pub trait KeyInit: KeySizeUser + Sized {
rng.try_fill_bytes(&mut key)?;
Ok(key)
}

/// Check if a key might be considered weak
fn weak_key_test(_key: &Key<Self>) -> Result<(), WeakKeyError> {
Ok(())
}

/// Create new value from fixed size key and check for weakness
fn new_checked(key: &Key<Self>) -> Result<Self, WeakKeyError> {
Self::weak_key_test(key)?;
Ok(Self::new(key))
}
}

/// Types which can be initialized from key and initialization vector (nonce).
pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
/// Create new value from fixed length key and nonce.
fn new(key: &Key<Self>, iv: &Iv<Self>) -> Self;

/// Create new value from fixed length key and nonce. This will check for for weakness
fn new_checked(key: &Key<Self>, iv: &Iv<Self>) -> Result<Self, WeakKeyError> {
Self::weak_iv_test(iv)?;
Ok(Self::new(key, iv))
}

/// Create new value from variable length key and nonce.
#[inline]
fn new_from_slices(key: &[u8], iv: &[u8]) -> Result<Self, InvalidLength> {
Expand Down Expand Up @@ -258,6 +275,11 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
let iv = Self::generate_iv_with_rng(rng)?;
Ok((key, iv))
}

/// Check if an IV might be considered weak
fn weak_iv_test(_iv: &Iv<Self>) -> Result<(), WeakKeyError> {
Ok(())
}
Copy link
Member

@newpavlov newpavlov Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this method. Plus, we can add it later in a backward-compatible way if it's needed for something after all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask you take over?
I'm not sure I can follow what you're asking or how you want the traits to be laid out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can do it a bit later. Though this PR is almost ready in my opinion. You just need to remove KeyIvInit::weak_iv_test and keep KeyIvInit::weak_key_test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also could allow edits by maintainers (there should be a checkbox on the right) and I can make the necessary edits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored the branch, you now have access (somehow I thought it was on by default? but it appears not)

I didn't mean to be rude, I'm just not sure I can follow the entire logic of the Inner trait and how they are meant to integrate together, and I thought that would have been easier for you to just write the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I already created #1742 based on this PR.

}

/// Types which can be initialized from another type (usually block ciphers).
Expand Down Expand Up @@ -300,6 +322,11 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
rng.try_fill_bytes(&mut iv)?;
Ok(iv)
}

/// Check if an IV might be considered weak
fn weak_iv_test(_iv: &Iv<Self>) -> Result<(), WeakKeyError> {
Ok(())
}
}

/// Trait for loading current IV state.
Expand Down Expand Up @@ -330,6 +357,12 @@ where
fn new_from_slices(key: &[u8], iv: &[u8]) -> Result<Self, InvalidLength> {
T::Inner::new_from_slice(key).and_then(|i| T::inner_iv_slice_init(i, iv))
}

#[inline]
fn new_checked(key: &Key<Self>, iv: &Iv<Self>) -> Result<Self, WeakKeyError> {
Self::weak_iv_test(iv)?;
Ok(Self::inner_iv_init(T::Inner::new_checked(key)?, iv))
}
}

impl<T> KeyInit for T
Expand All @@ -348,6 +381,12 @@ where
.map_err(|_| InvalidLength)
.map(Self::inner_init)
}

#[inline]
fn new_checked(key: &Key<Self>) -> Result<Self, WeakKeyError> {
T::Inner::weak_key_test(key)?;
Ok(Self::inner_init(T::Inner::new(key)))
}
}

// Unfortunately this blanket impl is impossible without mutually
Expand Down Expand Up @@ -387,3 +426,16 @@ impl fmt::Display for InvalidLength {
}

impl core::error::Error for InvalidLength {}

/// The error type returned when a key is tested to be weak the [`KeyInit::weak_key_test`].
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct WeakKeyError;

impl fmt::Display for WeakKeyError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("WeakKey")
}
}

impl core::error::Error for WeakKeyError {}