Skip to main content

rama_boring/
hpke.rs

1use crate::error::ErrorStack;
2use crate::{cvt_0i, cvt_p, ffi};
3
4use foreign_types::ForeignType;
5
6foreign_type_and_impl_send_sync! {
7    type CType = ffi::EVP_HPKE_KEY;
8    fn drop = ffi::EVP_HPKE_KEY_free;
9
10    pub struct HpkeKey;
11}
12
13impl HpkeKey {
14    /// Allocates and initializes a key with the `EVP_HPKE_KEY` type using the
15    /// `EVP_hpke_x25519_hkdf_sha256` KEM algorithm.
16    pub fn dhkem_p256_sha256(pkey: &[u8]) -> Result<HpkeKey, ErrorStack> {
17        unsafe {
18            ffi::init();
19            let hpke = cvt_p(ffi::EVP_HPKE_KEY_new()).map(|p| HpkeKey::from_ptr(p))?;
20
21            cvt_0i(ffi::EVP_HPKE_KEY_init(
22                hpke.as_ptr(),
23                ffi::EVP_hpke_x25519_hkdf_sha256(),
24                pkey.as_ptr(),
25                pkey.len(),
26            ))?;
27
28            Ok(hpke)
29        }
30    }
31}