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;