mirror of
https://github.com/matter-labs/teepot.git
synced 2025-07-21 07:03:56 +02:00
feat(sha384-extend): enhance SHA384 extend utility with padding and tests
- Refactor `sha384-extend` to include digest padding and validation. - Add `extend_sha384` function for hex-string-based digest extension. - Introduce comprehensive test coverage for edge cases and errors. Signed-off-by: Harald Hoyer <harald@matterlabs.dev>
This commit is contained in:
parent
7258452b79
commit
fa2ecee4bd
6 changed files with 234 additions and 33 deletions
|
@ -16,15 +16,4 @@ pub mod quote;
|
|||
pub mod server;
|
||||
pub mod sgx;
|
||||
pub mod tdx;
|
||||
|
||||
/// pad a byte slice to a fixed sized array
|
||||
pub fn pad<const T: usize>(input: &[u8]) -> [u8; T] {
|
||||
let mut output = [0; T];
|
||||
let len = input.len();
|
||||
if len > T {
|
||||
output.copy_from_slice(&input[..T]);
|
||||
} else {
|
||||
output[..len].copy_from_slice(input);
|
||||
}
|
||||
output
|
||||
}
|
||||
pub mod util;
|
||||
|
|
83
crates/teepot/src/util/mod.rs
Normal file
83
crates/teepot/src/util/mod.rs
Normal file
|
@ -0,0 +1,83 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright (c) 2023-2025 Matter Labs
|
||||
|
||||
//! utility functions.
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
/// Errors that can occur when padding byte vectors to fixed-size arrays.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum PadError {
|
||||
/// Indicates that the input vector's length exceeds the target array size.
|
||||
///
|
||||
/// # Fields
|
||||
/// * `expected` - The target size of the array in bytes
|
||||
/// * `actual` - The actual length of the input vector in bytes
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// # use teepot::util::{pad, PadError};
|
||||
/// let long_input = vec![1, 2, 3, 4];
|
||||
/// let result = pad::<2>(&long_input);
|
||||
/// assert!(matches!(
|
||||
/// result,
|
||||
/// Err(PadError::InputTooLong { expected: 2, actual: 4 })
|
||||
/// ));
|
||||
/// ```
|
||||
#[error("Input vector is too long - expected {expected} bytes, got {actual}")]
|
||||
InputTooLong {
|
||||
/// The expected length (target array size)
|
||||
expected: usize,
|
||||
/// The actual length of the provided input
|
||||
actual: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// Pad a byte vector to a fixed-size array by appending zeros. If the input is longer
|
||||
/// than the target size, returns an error.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `input` - Input byte vector to be padded with zeros
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<[u8; T], PadError>` - A fixed-size array of length T, or a PadError if input is too long
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns `PadError::InputTooLong` if the input vector length exceeds the target array size T,
|
||||
/// containing both the expected and actual sizes
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use teepot::util::{pad, PadError};
|
||||
/// let input = vec![1, 2, 3];
|
||||
/// let padded: [u8; 5] = pad(&input)?;
|
||||
/// assert_eq!(padded, [1, 2, 3, 0, 0]);
|
||||
///
|
||||
/// // Error case: input too long
|
||||
/// let long_input = vec![1, 2, 3, 4, 5, 6];
|
||||
/// assert!(matches!(
|
||||
/// pad::<5>(&long_input),
|
||||
/// Err(PadError::InputTooLong { expected: 5, actual: 6 })
|
||||
/// ));
|
||||
/// # Ok::<(), PadError>(())
|
||||
/// ```
|
||||
///
|
||||
/// # Type Parameters
|
||||
/// * `T` - The fixed size of the output array in bytes
|
||||
pub fn pad<const T: usize>(input: &[u8]) -> Result<[u8; T], PadError> {
|
||||
let mut output = [0u8; T];
|
||||
match input.len().cmp(&T) {
|
||||
std::cmp::Ordering::Greater => Err(PadError::InputTooLong {
|
||||
expected: T,
|
||||
actual: input.len(),
|
||||
}),
|
||||
std::cmp::Ordering::Equal => {
|
||||
output.copy_from_slice(input);
|
||||
Ok(output)
|
||||
}
|
||||
std::cmp::Ordering::Less => {
|
||||
output[..input.len()].copy_from_slice(input);
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue