From eee29e0399a0f2a952984de6f854e3729e0b76ab Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Mon, 14 Oct 2024 17:12:24 +0000 Subject: [PATCH] rust/crypto: Rename `derive_key` to `derive_aes256_gcm_key` and export it This is currently the only key type supported, therefore rename the function to make this clear. In addition, improve the documentation of the function as function is now exported. Signed-off-by: Marc Hartmayer Reviewed-by: Steffen Eiden Signed-off-by: Steffen Eiden --- rust/pv/src/crypto.rs | 8 ++++---- rust/pv/src/lib.rs | 2 +- rust/pv/src/req.rs | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/rust/pv/src/crypto.rs b/rust/pv/src/crypto.rs index ac8c0eef..6d9e14c5 100644 --- a/rust/pv/src/crypto.rs +++ b/rust/pv/src/crypto.rs @@ -106,12 +106,12 @@ pub(crate) fn hkdf_rfc_5869( Ok(res) } -/// Derive a symmetric key from a private and a public key. +/// Derive a symmetric AES 256 GCM key from a private and a public key. /// /// # Errors /// /// This function will return an error if something went bad in OpenSSL. -pub(crate) fn derive_key(k1: &PKeyRef, k2: &PKeyRef) -> Result { +pub fn derive_aes256_gcm_key(k1: &PKeyRef, k2: &PKeyRef) -> Result { let mut der = Deriver::new(k1)?; der.set_peer(k2)?; let mut key = der.derive_to_vec()?; @@ -392,7 +392,7 @@ mod tests { } #[test] - fn derive_key() { + fn derive_aes256_gcm_key() { let (cust_key, host_key) = get_test_keys(); let exp_key: Aes256Key = [ @@ -402,7 +402,7 @@ mod tests { ] .into(); - let calc_key = super::derive_key(&cust_key, &host_key).unwrap(); + let calc_key = super::derive_aes256_gcm_key(&cust_key, &host_key).unwrap(); assert_eq!(&calc_key, &exp_key); } diff --git a/rust/pv/src/lib.rs b/rust/pv/src/lib.rs index 021a6306..db5593a9 100644 --- a/rust/pv/src/lib.rs +++ b/rust/pv/src/lib.rs @@ -92,7 +92,7 @@ pub use pv_core::{FileAccessErrorType, FileIoErrorType}; /// Functionalities to build UV requests pub mod request { pub use crate::brcb::BootHdrTags; - pub use crate::crypto::{SymKey, SymKeyType}; + pub use crate::crypto::{derive_aes256_gcm_key, SymKey, SymKeyType}; pub use crate::req::{EcPubKeyCoord, Keyslot, ReqEncrCtx, Request}; pub use crate::verify::{CertVerifier, HkdVerifier, NoVerifyHkd}; diff --git a/rust/pv/src/req.rs b/rust/pv/src/req.rs index 3c6944c9..65884c89 100644 --- a/rust/pv/src/req.rs +++ b/rust/pv/src/req.rs @@ -4,8 +4,8 @@ use crate::assert_size; use crate::crypto::{ - decrypt_aes_gcm, derive_key, encrypt_aes_gcm, gen_ec_key, hash, random_array, AesGcmResult, - SymKey, SymKeyType, AES_256_GCM_TAG_SIZE, + decrypt_aes_gcm, derive_aes256_gcm_key, encrypt_aes_gcm, gen_ec_key, hash, random_array, + AesGcmResult, SymKey, SymKeyType, AES_256_GCM_TAG_SIZE, }; use crate::misc::to_u32; use crate::request::Confidential; @@ -106,7 +106,7 @@ impl Encrypt for Keyslot { priv_key: &PKeyRef, to: &mut Vec, ) -> Result<()> { - let derived_key = derive_key(priv_key, &self.0)?; + let derived_key = derive_aes256_gcm_key(priv_key, &self.0)?; let mut wrpk_and_kst = encrypt_aes_gcm(&derived_key.into(), &[0; 12], &[], prot_key)?.data(); let phk: EcPubKeyCoord = self.0.as_ref().try_into()?;