1#![doc = include_str!("../Readme.md")]
2#![no_std]
3
4extern crate alloc;
5#[cfg(feature = "std")]
6extern crate std;
7
8use alloc::string::String;
9use alloc::vec::Vec;
10
11use error::Error;
12use types::{AeadAlgorithm, KemAlgorithm};
13
14pub mod error;
15pub mod types;
16
17pub use rand_core::{CryptoRng, RngCore};
19
20pub trait HpkeCrypto: core::fmt::Debug + Send + Sync {
23 type HpkePrng: RngCore + CryptoRng + HpkeTestRng;
25
26 fn name() -> String;
28
29 fn supports_kdf(alg: types::KdfAlgorithm) -> Result<(), Error>;
31
32 fn supports_kem(alg: types::KemAlgorithm) -> Result<(), Error>;
34
35 fn supports_aead(alg: types::AeadAlgorithm) -> Result<(), Error>;
37
38 fn prng() -> Self::HpkePrng;
41
42 #[inline(always)]
44 fn kdf_digest_length(alg: types::KdfAlgorithm) -> usize {
45 match alg {
46 types::KdfAlgorithm::HkdfSha256 => 32,
47 types::KdfAlgorithm::HkdfSha384 => 48,
48 types::KdfAlgorithm::HkdfSha512 => 64,
49 }
50 }
51
52 fn kdf_extract(alg: types::KdfAlgorithm, salt: &[u8], ikm: &[u8]) -> Result<Vec<u8>, Error>;
54
55 fn kdf_expand(
57 alg: types::KdfAlgorithm,
58 prk: &[u8],
59 info: &[u8],
60 output_size: usize,
61 ) -> Result<Vec<u8>, Error>;
62
63 fn dh(alg: KemAlgorithm, pk: &[u8], sk: &[u8]) -> Result<Vec<u8>, Error>;
65
66 fn secret_to_public(alg: KemAlgorithm, sk: &[u8]) -> Result<Vec<u8>, Error>;
68
69 fn kem_key_gen(
71 alg: KemAlgorithm,
72 prng: &mut Self::HpkePrng,
73 ) -> Result<(Vec<u8>, Vec<u8>), Error>;
74
75 fn kem_key_gen_derand(alg: KemAlgorithm, seed: &[u8]) -> Result<(Vec<u8>, Vec<u8>), Error>;
78
79 fn kem_encaps(
81 alg: KemAlgorithm,
82 pk_r: &[u8],
83 prng: &mut Self::HpkePrng,
84 ) -> Result<(Vec<u8>, Vec<u8>), Error>;
85
86 fn kem_decaps(alg: KemAlgorithm, ct: &[u8], sk_r: &[u8]) -> Result<Vec<u8>, Error>;
89
90 fn dh_validate_sk(alg: KemAlgorithm, sk: &[u8]) -> Result<Vec<u8>, Error>;
92
93 fn aead_seal(
95 alg: AeadAlgorithm,
96 key: &[u8],
97 nonce: &[u8],
98 aad: &[u8],
99 msg: &[u8],
100 ) -> Result<Vec<u8>, Error>;
101
102 fn aead_open(
104 alg: AeadAlgorithm,
105 key: &[u8],
106 nonce: &[u8],
107 aad: &[u8],
108 msg: &[u8],
109 ) -> Result<Vec<u8>, Error>;
110
111 fn aead_key_length(alg: AeadAlgorithm) -> usize {
115 match alg {
116 AeadAlgorithm::Aes128Gcm => 16,
117 AeadAlgorithm::Aes256Gcm => 32,
118 AeadAlgorithm::ChaCha20Poly1305 => 32,
119 AeadAlgorithm::HpkeExport => 0,
120 }
121 }
122
123 fn aead_nonce_length(alg: AeadAlgorithm) -> usize {
127 match alg {
128 AeadAlgorithm::Aes128Gcm => 12,
129 AeadAlgorithm::Aes256Gcm => 12,
130 AeadAlgorithm::ChaCha20Poly1305 => 12,
131 AeadAlgorithm::HpkeExport => 0,
132 }
133 }
134
135 fn aead_tag_length(alg: AeadAlgorithm) -> usize {
139 match alg {
140 AeadAlgorithm::Aes128Gcm => 16,
141 AeadAlgorithm::Aes256Gcm => 16,
142 AeadAlgorithm::ChaCha20Poly1305 => 16,
143 AeadAlgorithm::HpkeExport => 0,
144 }
145 }
146}
147
148pub trait HpkeTestRng {
150 type Error: core::fmt::Debug + core::fmt::Display;
152 fn try_fill_test_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>;
154
155 fn seed(&mut self, seed: &[u8]);
157}