From 050441922bcc8f2a758e4c0baf08d97c641eccf7 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Mon, 14 Oct 2024 18:26:41 +0000 Subject: [PATCH] rust/crypto: Add `key_type` method to `SymKey` impl This function is easier to be used than a match! statement. Reviewed-by: Steffen Eiden Signed-off-by: Marc Hartmayer Signed-off-by: Steffen Eiden --- rust/pv/src/crypto.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rust/pv/src/crypto.rs b/rust/pv/src/crypto.rs index 10cce1e2..2639448c 100644 --- a/rust/pv/src/crypto.rs +++ b/rust/pv/src/crypto.rs @@ -82,6 +82,14 @@ impl SymKey { Self::Aes256Xts(key) => key.value(), } } + + /// Return the key type of this [`SymKey`]. + pub fn key_type(&self) -> SymKeyType { + match self { + Self::Aes256(_) => SymKeyType::Aes256, + Self::Aes256Xts(_) => SymKeyType::Aes256Xts, + } + } } impl From for SymKey { @@ -512,4 +520,16 @@ mod tests { Nid::AES_256_XTS ); } + + #[test] + fn key_type() { + assert_eq!( + SymKey::random(SymKeyType::Aes256).unwrap().key_type(), + SymKeyType::Aes256 + ); + assert_eq!( + SymKey::random(SymKeyType::Aes256Xts).unwrap().key_type(), + SymKeyType::Aes256Xts + ); + } }