diff --git a/verity/src/salt.rs b/verity/src/salt.rs index d4efca4..482f567 100644 --- a/verity/src/salt.rs +++ b/verity/src/salt.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Advanced Micro Devices, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::{fmt, ops::Deref}; +use std::{fmt, io, ops::Deref, str::FromStr}; const MAX_SIZE: usize = 256; @@ -33,6 +33,48 @@ impl Default for Salt { } } +impl fmt::Display for Salt { + /// Formats the supplied bytes as lowercase hex, without a prefix. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for byte in self.as_ref() { + write!(f, "{byte:02x}")?; + } + Ok(()) + } +} + +impl FromStr for Salt { + type Err = io::Error; + + /// Parses up to 256 bytes of hex, accepting either case and an empty salt. + /// Prefixes, whitespace, odd lengths and non-hex characters are rejected. + fn from_str(text: &str) -> Result { + fn half(byte: u8) -> io::Result { + match byte { + b'0'..=b'9' => Ok(byte - b'0'), + b'a'..=b'f' => Ok(byte - b'a' + 10), + b'A'..=b'F' => Ok(byte - b'A' + 10), + _ => Err(io::Error::new( + io::ErrorKind::InvalidData, + "invalid salt hex digit", + )), + } + } + + if text.len() > 2 * MAX_SIZE || !text.len().is_multiple_of(2) { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "invalid salt hex length", + )); + } + let mut salt = Self([0; MAX_SIZE], text.len() / 2); + for (index, pair) in text.as_bytes().chunks(2).enumerate() { + salt.0[index] = (half(pair[0])? << 4) | half(pair[1])?; + } + Ok(salt) + } +} + impl fmt::Debug for Salt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Salt").field(&self.as_ref()).finish() @@ -57,6 +99,38 @@ impl AsRef<[u8]> for Salt { mod tests { use super::Salt; + #[test] + fn hex_round_trip_including_empty_and_maximum() { + for bytes in [Vec::new(), vec![0, 1, 0xab, 0xff], (0..=255).collect()] { + let salt = Salt::new(&bytes).unwrap(); + let text = salt.to_string(); + assert_eq!(text.len(), 2 * bytes.len()); + assert!(!text.bytes().any(|byte| byte.is_ascii_uppercase())); + assert_eq!(text.parse::().unwrap(), salt); + assert_eq!(text.to_uppercase().parse::().unwrap(), salt); + } + assert_eq!( + Salt::new(&[0, 1, 0xab, 0xff]).unwrap().to_string(), + "0001abff" + ); + } + + #[test] + fn rejects_invalid_hex_before_exceeding_storage() { + for text in ["0", "0x12", " 12", "12 ", "gg", "é", "12"] { + assert_eq!( + text.parse::().unwrap_err().kind(), + std::io::ErrorKind::InvalidData + ); + } + for length in [513, 514] { + assert_eq!( + "g".repeat(length).parse::().unwrap_err().to_string(), + "invalid salt hex length" + ); + } + } + #[test] fn debug_shows_only_salt_bytes() { assert_eq!(format!("{:?}", Salt::default()), "Salt([])");