Skip to content

Commit

Permalink
Implement Debug trait
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail committed Oct 20, 2023
1 parent 7f6b37e commit 633b082
Showing 1 changed file with 58 additions and 5 deletions.
63 changes: 58 additions & 5 deletions aws-lc-rs/src/key_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! Ok(())
//! # }
//! ```
use std::{mem::MaybeUninit, ptr::null};
use std::{fmt::Debug, mem::MaybeUninit, ptr::null};

use aws_lc::{AES_unwrap_key, AES_unwrap_key_padded};

Expand All @@ -42,17 +42,55 @@ mod tests;

/// The Key Wrapping Algorithm
pub struct Algorithm {
id: AlgorithmId,
key_len: usize,
}

impl Algorithm {
/// Returns the algorithm identifier.
pub fn id(&self) -> AlgorithmId {
self.id
}

/// Returns the algorithm key length.
pub fn key_len(&self) -> usize {
self.key_len
}
}

impl Debug for Algorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Algorithm")
.field("id", &self.id)
.field("key_len", &self.key_len)
.finish()
}
}

/// The Key Wrapping Algorithm Identifier
#[derive(Debug, Clone, Copy)]
pub enum AlgorithmId {
/// AES-128 Key Wrap
Aes128,

/// AES-256 Key Wrap
Aes256,
}

/// AES-128 Key Wrapping
pub const AES_128: Algorithm = Algorithm { key_len: 16 };
pub const AES_128: Algorithm = Algorithm {
id: AlgorithmId::Aes128,
key_len: 16,
};

/// AES-256 Key Wrapping
pub const AES_256: Algorithm = Algorithm { key_len: 32 };
pub const AES_256: Algorithm = Algorithm {
id: AlgorithmId::Aes256,
key_len: 32,
};

/// The mode of operation for the wrapping algorithm.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub enum WrappingMode {
/// Key Wrap with Padding
Padded,
Expand Down Expand Up @@ -263,9 +301,24 @@ impl KeyEncryptionKey {
.unwrap(&self.key[..self.algorithm.key_len], input, output)
}

/// Returns the configure `WrappingMode`.
/// Returns the configured `Algorithm`.
pub fn algorithm(&self) -> &'static Algorithm {
self.algorithm
}

/// Returns the configured `WrappingMode`.
#[must_use]
pub fn wrapping_mode(&self) -> WrappingMode {
self.mode
}
}

#[allow(clippy::missing_fields_in_debug)]
impl Debug for KeyEncryptionKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("KeyEncryptionKey")
.field("algorithm", &self.algorithm)
.field("mode", &self.mode)
.finish()
}
}

0 comments on commit 633b082

Please sign in to comment.