hpke_dispatch/
lib.rs

1#![cfg_attr(feature = "cfg_eval", feature(cfg_eval))]
2#![forbid(unsafe_code)]
3#![deny(
4    clippy::dbg_macro,
5    missing_copy_implementations,
6    rustdoc::missing_crate_level_docs,
7    missing_debug_implementations,
8    nonstandard_style,
9    unused_qualifications
10)]
11#![warn(missing_docs, clippy::cargo)]
12#![allow(
13    clippy::missing_errors_doc,
14    clippy::use_self,
15    clippy::multiple_crate_versions
16)]
17#![doc = include_str!("../README.md")]
18
19use hpke::Deserializable;
20
21#[cfg(target_arch = "wasm32")]
22use wasm_bindgen::prelude::*;
23
24mod base_mode_open;
25pub use base_mode_open::base_mode_open;
26
27mod base_mode_seal;
28pub use base_mode_seal::base_mode_seal;
29
30mod config;
31pub use config::Config;
32
33mod keypair;
34pub use keypair::{gen_keypair, Keypair};
35
36mod ciphertext;
37pub use ciphertext::EncappedKeyAndCiphertext;
38
39mod aead;
40pub use aead::{Aead, AEAD_ALL};
41
42mod kdf;
43pub use kdf::{Kdf, KDF_ALL};
44
45mod kem;
46pub use kem::{Kem, KEM_ALL};
47
48mod macros;
49pub(crate) use macros::match_algo;
50
51/**
52A simple error type for failed id lookups
53 */
54#[derive(Copy, Clone, Debug)]
55#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
56#[cfg_attr(
57    feature = "serde",
58    derive(serde_crate::Serialize, serde_crate::Deserialize)
59)]
60#[cfg_attr(feature = "serde", serde(crate = "serde_crate"))]
61pub struct IdLookupError(&'static str);
62impl std::fmt::Display for IdLookupError {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        f.write_fmt(format_args!("id lookup error {}", self.0))
65    }
66}
67impl std::error::Error for IdLookupError {}
68
69pub(crate) fn from_bytes<T: Deserializable>(encoded: &[u8]) -> Result<T, HpkeError> {
70    let result = T::from_bytes(encoded);
71    cfg_if::cfg_if! {
72        if #[cfg(target_arch = "wasm32")] {
73            result.map_err(Into::into)
74        } else {
75            result
76        }
77    }
78}
79
80cfg_if::cfg_if! {
81    if #[cfg(target_arch = "wasm32")] {
82        /**
83        a newtype wrapper for HpkeError so we can use it in wasm_bindgen
84         */
85        #[wasm_bindgen]
86        #[derive(Debug, Clone, Copy)]
87        pub struct HpkeError(hpke::HpkeError);
88        impl From<hpke::HpkeError> for HpkeError {
89            fn from(h: hpke::HpkeError) -> Self {
90                Self(h)
91            }
92        }
93
94        impl core::fmt::Display for HpkeError {
95            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
96               core::fmt::Display::fmt(&self.0, f)
97            }
98        }
99    } else {
100        pub use hpke::HpkeError;
101    }
102}