From d8ec90b32709588ddefe4eb0636295f36b8544f0 Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Sat, 30 Mar 2024 07:22:41 -0700 Subject: [PATCH] Add more descriptive error --- protocol/src/hkdf.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/protocol/src/hkdf.rs b/protocol/src/hkdf.rs index 4deb622..5eaad6e 100644 --- a/protocol/src/hkdf.rs +++ b/protocol/src/hkdf.rs @@ -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) } } @@ -40,9 +42,11 @@ impl Hkdf { /// 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.