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

Add more descriptive error message to HDKF max length #17

Merged
merged 1 commit into from
Mar 31, 2024
Merged
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
12 changes: 8 additions & 4 deletions protocol/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const MAX_OUTPUT_BLOCKS: usize = 255;

/// Size of output exceeds maximum length allowed.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MaxLengthError;
pub struct MaxLengthError {
max: usize,
}

impl fmt::Display for MaxLengthError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "too large output")
write!(f, "exceeds {} byte max output material limit", self.max)
}
}

Expand All @@ -40,9 +42,11 @@ impl<T: Hash> Hkdf<T> {

/// Expand the key to generate output key material in okm.
pub fn expand(&self, info: &[u8], okm: &mut [u8]) -> Result<(), MaxLengthError> {
// Length of output keying material must be less than 255 * hash length.
// Length of output keying material in bytes must be less than 255 * hash length.
if okm.len() > (MAX_OUTPUT_BLOCKS * T::LEN) {
return Err(MaxLengthError);
return Err(MaxLengthError {
max: MAX_OUTPUT_BLOCKS * T::LEN,
});
}

// Counter starts at "1" based on RFC5869 spec and is committed to in the hash.
Expand Down
Loading