From 2561bdf64d1e2ea1b2f9de30c3fc805953413e20 Mon Sep 17 00:00:00 2001 From: Rushil Mehra Date: Mon, 10 Feb 2025 13:50:50 -0800 Subject: [PATCH] Expose EVP_HPKE_KEY --- boring/src/hpke.rs | 31 +++++++++++++++++++++++++++++++ boring/src/lib.rs | 1 + 2 files changed, 32 insertions(+) create mode 100644 boring/src/hpke.rs diff --git a/boring/src/hpke.rs b/boring/src/hpke.rs new file mode 100644 index 00000000..7f788224 --- /dev/null +++ b/boring/src/hpke.rs @@ -0,0 +1,31 @@ +use crate::error::ErrorStack; +use crate::{cvt_0i, cvt_p, ffi}; + +use foreign_types::ForeignType; + +foreign_type_and_impl_send_sync! { + type CType = ffi::EVP_HPKE_KEY; + fn drop = ffi::EVP_HPKE_KEY_free; + + pub struct HpkeKey; +} + +impl HpkeKey { + /// Allocates and initializes a key with the `EVP_HPKE_KEY` type using the + /// `EVP_hpke_x25519_hkdf_sha256` KEM algorithm. + pub fn dhkem_p256_sha256(pkey: &[u8]) -> Result { + unsafe { + ffi::init(); + let hpke = cvt_p(ffi::EVP_HPKE_KEY_new()).map(|p| HpkeKey::from_ptr(p))?; + + cvt_0i(ffi::EVP_HPKE_KEY_init( + hpke.as_ptr(), + ffi::EVP_hpke_x25519_hkdf_sha256(), + pkey.as_ptr(), + pkey.len(), + ))?; + + Ok(hpke) + } + } +} diff --git a/boring/src/lib.rs b/boring/src/lib.rs index 6779586a..93d56943 100644 --- a/boring/src/lib.rs +++ b/boring/src/lib.rs @@ -128,6 +128,7 @@ pub mod error; pub mod ex_data; pub mod fips; pub mod hash; +pub mod hpke; pub mod memcmp; pub mod nid; pub mod pkcs12;