1use crate::error::IntoAnyError;
6use alloc::vec;
7use alloc::vec::Vec;
8use core::{
9 fmt::{self, Debug},
10 ops::Deref,
11};
12use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
13use zeroize::{ZeroizeOnDrop, Zeroizing};
14
15mod cipher_suite;
16pub use self::cipher_suite::*;
17
18#[cfg(feature = "test_suite")]
19pub mod test_suite;
20
21#[derive(Clone, PartialEq, Eq, MlsSize, MlsEncode, MlsDecode)]
22#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24pub struct HpkeCiphertext {
26 #[mls_codec(with = "mls_rs_codec::byte_vec")]
27 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
28 pub kem_output: Vec<u8>,
29 #[mls_codec(with = "mls_rs_codec::byte_vec")]
30 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
31 pub ciphertext: Vec<u8>,
32}
33
34impl Debug for HpkeCiphertext {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.debug_struct("HpkeCiphertext").finish()
37 }
38}
39
40#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, MlsSize, MlsDecode, MlsEncode)]
43#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
44#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
45pub struct HpkePublicKey(
46 #[mls_codec(with = "mls_rs_codec::byte_vec")]
47 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
48 Vec<u8>,
49);
50
51impl Debug for HpkePublicKey {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 crate::debug::pretty_bytes(&self.0)
54 .named("HpkePublicKey")
55 .fmt(f)
56 }
57}
58
59impl From<Vec<u8>> for HpkePublicKey {
60 fn from(data: Vec<u8>) -> Self {
61 Self(data)
62 }
63}
64
65impl From<HpkePublicKey> for Vec<u8> {
66 fn from(data: HpkePublicKey) -> Self {
67 data.0
68 }
69}
70
71impl Deref for HpkePublicKey {
72 type Target = [u8];
73
74 fn deref(&self) -> &Self::Target {
75 &self.0
76 }
77}
78
79impl AsRef<[u8]> for HpkePublicKey {
80 fn as_ref(&self) -> &[u8] {
81 &self.0
82 }
83}
84
85#[derive(Clone, PartialEq, Eq, MlsSize, MlsEncode, MlsDecode, ZeroizeOnDrop)]
87#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
88#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
89pub struct HpkeSecretKey(
90 #[mls_codec(with = "mls_rs_codec::byte_vec")]
91 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
92 Vec<u8>,
93);
94
95impl Debug for HpkeSecretKey {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 f.debug_struct("HpkeSecretKey").finish()
98 }
99}
100
101impl From<Vec<u8>> for HpkeSecretKey {
102 fn from(data: Vec<u8>) -> Self {
103 Self(data)
104 }
105}
106
107impl Deref for HpkeSecretKey {
108 type Target = [u8];
109
110 fn deref(&self) -> &Self::Target {
111 &self.0
112 }
113}
114
115impl AsRef<[u8]> for HpkeSecretKey {
116 fn as_ref(&self) -> &[u8] {
117 &self.0
118 }
119}
120
121#[derive(Clone, Debug, PartialEq, Eq, Default)]
123pub struct HpkePsk<'a> {
124 pub id: &'a [u8],
125 pub value: &'a [u8],
126}
127
128impl<'a> HpkePsk<'a> {
129 pub fn new(id: &'a [u8], value: &'a [u8]) -> Self {
130 Self { id, value }
131 }
132}
133
134#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
139#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))]
140#[cfg_attr(
141 all(not(target_arch = "wasm32"), mls_build_async),
142 maybe_async::must_be_async
143)]
144pub trait HpkeContextS {
145 type Error: IntoAnyError;
146
147 async fn seal(&mut self, aad: Option<&[u8]>, data: &[u8]) -> Result<Vec<u8>, Self::Error>;
150
151 async fn export(
153 &self,
154 exporter_context: &[u8],
155 len: usize,
156 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
157}
158
159#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
164#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))]
165#[cfg_attr(
166 all(not(target_arch = "wasm32"), mls_build_async),
167 maybe_async::must_be_async
168)]
169pub trait HpkeContextR {
170 type Error: IntoAnyError;
171
172 async fn open(
175 &mut self,
176 aad: Option<&[u8]>,
177 ciphertext: &[u8],
178 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
179
180 async fn export(
182 &self,
183 exporter_context: &[u8],
184 len: usize,
185 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
186}
187
188#[derive(Clone, PartialEq, Eq, Hash, Ord, PartialOrd, MlsSize, MlsEncode, MlsDecode)]
191#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
192#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
193pub struct SignaturePublicKey(
194 #[mls_codec(with = "mls_rs_codec::byte_vec")]
195 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
196 Vec<u8>,
197);
198
199impl Debug for SignaturePublicKey {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 crate::debug::pretty_bytes(&self.0)
202 .named("SignaturePublicKey")
203 .fmt(f)
204 }
205}
206
207impl SignaturePublicKey {
208 pub fn new(bytes: Vec<u8>) -> Self {
209 bytes.into()
210 }
211
212 pub fn new_slice(data: &[u8]) -> Self {
213 Self(data.to_vec())
214 }
215
216 pub fn as_bytes(&self) -> &[u8] {
217 &self.0
218 }
219}
220
221impl Deref for SignaturePublicKey {
222 type Target = [u8];
223
224 fn deref(&self) -> &Self::Target {
225 &self.0
226 }
227}
228
229impl AsRef<[u8]> for SignaturePublicKey {
230 fn as_ref(&self) -> &[u8] {
231 &self.0
232 }
233}
234
235impl From<Vec<u8>> for SignaturePublicKey {
236 fn from(data: Vec<u8>) -> Self {
237 SignaturePublicKey(data)
238 }
239}
240
241impl From<SignaturePublicKey> for Vec<u8> {
242 fn from(value: SignaturePublicKey) -> Self {
243 value.0
244 }
245}
246
247#[derive(Clone, PartialEq, Eq, ZeroizeOnDrop, MlsSize, MlsEncode, MlsDecode)]
249#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
250pub struct SignatureSecretKey {
251 #[mls_codec(with = "mls_rs_codec::byte_vec")]
252 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
253 bytes: Vec<u8>,
254}
255
256impl Debug for SignatureSecretKey {
257 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258 f.debug_struct("SignatureSecretKey").finish()
259 }
260}
261
262impl SignatureSecretKey {
263 pub fn new(bytes: Vec<u8>) -> Self {
264 bytes.into()
265 }
266
267 pub fn new_slice(data: &[u8]) -> Self {
268 Self {
269 bytes: data.to_vec(),
270 }
271 }
272
273 pub fn as_bytes(&self) -> &[u8] {
274 &self.bytes
275 }
276}
277
278impl From<Vec<u8>> for SignatureSecretKey {
279 fn from(bytes: Vec<u8>) -> Self {
280 Self { bytes }
281 }
282}
283
284impl Deref for SignatureSecretKey {
285 type Target = Vec<u8>;
286
287 fn deref(&self) -> &Self::Target {
288 &self.bytes
289 }
290}
291
292impl AsRef<[u8]> for SignatureSecretKey {
293 fn as_ref(&self) -> &[u8] {
294 &self.bytes
295 }
296}
297
298pub trait CryptoProvider: Send + Sync {
300 type CipherSuiteProvider: CipherSuiteProvider + Clone;
301
302 fn supported_cipher_suites(&self) -> Vec<CipherSuite>;
304
305 fn cipher_suite_provider(&self, cipher_suite: CipherSuite)
307 -> Option<Self::CipherSuiteProvider>;
308}
309
310#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
312#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))]
313#[cfg_attr(
314 all(not(target_arch = "wasm32"), mls_build_async),
315 maybe_async::must_be_async
316)]
317pub trait CipherSuiteProvider: Send + Sync {
318 type Error: IntoAnyError;
319
320 type HpkeContextS: HpkeContextS + Send + Sync;
321 type HpkeContextR: HpkeContextR + Send + Sync;
322
323 fn cipher_suite(&self) -> CipherSuite;
325
326 async fn hash(&self, data: &[u8]) -> Result<Vec<u8>, Self::Error>;
328
329 async fn mac(&self, key: &[u8], data: &[u8]) -> Result<Vec<u8>, Self::Error>;
333
334 async fn aead_seal(
339 &self,
340 key: &[u8],
341 data: &[u8],
342 aad: Option<&[u8]>,
343 nonce: &[u8],
344 ) -> Result<Vec<u8>, Self::Error>;
345
346 async fn aead_open(
350 &self,
351 key: &[u8],
352 ciphertext: &[u8],
353 aad: Option<&[u8]>,
354 nonce: &[u8],
355 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
356
357 fn aead_key_size(&self) -> usize;
360
361 fn aead_nonce_size(&self) -> usize;
364
365 async fn kdf_extract(&self, salt: &[u8], ikm: &[u8])
372 -> Result<Zeroizing<Vec<u8>>, Self::Error>;
373
374 async fn kdf_expand(
380 &self,
381 prk: &[u8],
382 info: &[u8],
383 len: usize,
384 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
385
386 fn kdf_extract_size(&self) -> usize;
389
390 async fn hpke_seal(
398 &self,
399 remote_key: &HpkePublicKey,
400 info: &[u8],
401 aad: Option<&[u8]>,
402 pt: &[u8],
403 ) -> Result<HpkeCiphertext, Self::Error>;
404
405 async fn hpke_seal_psk(
409 &self,
410 remote_key: &HpkePublicKey,
411 info: &[u8],
412 aad: Option<&[u8]>,
413 pt: &[u8],
414 psk: HpkePsk<'_>,
415 ) -> Result<HpkeCiphertext, Self::Error>;
416
417 async fn hpke_open(
423 &self,
424 ciphertext: &HpkeCiphertext,
425 local_secret: &HpkeSecretKey,
426 local_public: &HpkePublicKey,
427 info: &[u8],
428 aad: Option<&[u8]>,
429 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
430
431 async fn hpke_open_psk(
434 &self,
435 ciphertext: &HpkeCiphertext,
436 local_secret: &HpkeSecretKey,
437 local_public: &HpkePublicKey,
438 info: &[u8],
439 aad: Option<&[u8]>,
440 psk: HpkePsk<'_>,
441 ) -> Result<Zeroizing<Vec<u8>>, Self::Error>;
442
443 async fn hpke_setup_s(
455 &self,
456 remote_key: &HpkePublicKey,
457 info: &[u8],
458 ) -> Result<(Vec<u8>, Self::HpkeContextS), Self::Error>;
459
460 async fn hpke_setup_r(
475 &self,
476 kem_output: &[u8],
477 local_secret: &HpkeSecretKey,
478 local_public: &HpkePublicKey,
479
480 info: &[u8],
481 ) -> Result<Self::HpkeContextR, Self::Error>;
482
483 async fn kem_derive(&self, ikm: &[u8]) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error>;
488
489 async fn kem_generate(&self) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error>;
493
494 fn kem_public_key_validate(&self, key: &HpkePublicKey) -> Result<(), Self::Error>;
496
497 fn random_bytes(&self, out: &mut [u8]) -> Result<(), Self::Error>;
499
500 fn random_bytes_vec(&self, count: usize) -> Result<Vec<u8>, Self::Error> {
503 let mut vec = vec![0u8; count];
504 self.random_bytes(&mut vec)?;
505
506 Ok(vec)
507 }
508
509 async fn signature_key_generate(
512 &self,
513 ) -> Result<(SignatureSecretKey, SignaturePublicKey), Self::Error>;
514
515 async fn signature_key_derive_public(
517 &self,
518 secret_key: &SignatureSecretKey,
519 ) -> Result<SignaturePublicKey, Self::Error>;
520
521 async fn sign(
523 &self,
524 secret_key: &SignatureSecretKey,
525 data: &[u8],
526 ) -> Result<Vec<u8>, Self::Error>;
527
528 async fn verify(
530 &self,
531 public_key: &SignaturePublicKey,
532 signature: &[u8],
533 data: &[u8],
534 ) -> Result<(), Self::Error>;
535}