#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use crate::{
base_mode_open, base_mode_seal, Aead, EncappedKeyAndCiphertext, HpkeError, IdLookupError, Kdf,
Kem,
};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[cfg_attr(
feature = "serde",
derive(serde_crate::Serialize, serde_crate::Deserialize)
)]
#[cfg_attr(feature = "serde", serde(crate = "serde_crate"))]
pub struct Config {
pub aead: Aead,
pub kdf: Kdf,
pub kem: Kem,
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl Config {
#[cfg(feature = "base-mode-seal")]
pub fn base_mode_seal(
&self,
recipient_public_key: &[u8],
info: &[u8],
plaintext: &[u8],
aad: &[u8],
) -> Result<EncappedKeyAndCiphertext, HpkeError> {
base_mode_seal(self, recipient_public_key, info, plaintext, aad)
}
#[cfg(feature = "base-mode-open")]
pub fn base_mode_open(
&self,
private_key: &[u8],
encapped_key: &[u8],
info: &[u8],
ciphertext: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, HpkeError> {
base_mode_open(self, private_key, encapped_key, info, ciphertext, aad)
}
#[allow(clippy::use_self)] pub fn try_from_ids(aead_id: u16, kdf_id: u16, kem_id: u16) -> Result<Config, IdLookupError> {
Ok(Self {
aead: aead_id.try_into().map_err(|_| IdLookupError("aead"))?,
kdf: kdf_id.try_into().map_err(|_| IdLookupError("kdf"))?,
kem: kem_id.try_into().map_err(|_| IdLookupError("kem"))?,
})
}
}