Debian Codegraph @

third_party/rust/mls-rs-crypto-hpke/src/test_utils.rs

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5 #![cfg(not(mls_build_async))]
6
7 use alloc::vec::Vec;
8
9 use crate::{
10 context::{Context, ContextR, ContextS, EncryptionContext},
11 dhkem::DhKem,
12 hpke::Hpke,
13 };
14
15 use mls_rs_core::crypto::test_suite::{EncapOutput, TestHpke};
16 use mls_rs_crypto_traits::{AeadType, DhType, KdfType, KemResult, KemType};
17
18 impl<DH: DhType, KDF: KdfType + Clone, AEAD: AeadType + Clone> TestHpke
19 for Hpke<DhKem<DH, KDF>, KDF, AEAD>
20 {
21 type ContextR = ContextR<KDF, AEAD>;
22 type ContextS = ContextS<KDF, AEAD>;
23
24 fn hpke_context(
25 &self,
26 key: Vec<u8>,
27 base_nonce: Vec<u8>,
28 exporter_secret: Vec<u8>,
29 ) -> (Self::ContextS, Self::ContextR) {
30 let encryption_context =
31 EncryptionContext::new(base_nonce, self.aead.clone().unwrap(), key).unwrap();
32
33 let context = Context::new(
34 Some(encryption_context),
35 exporter_secret.clone(),
36 self.kdf.clone(),
37 );
38
39 (ContextS(context.clone()), ContextR(context))
40 }
41
42 fn encap(&mut self, ikm_e: Vec<u8>, pk_rm: Vec<u8>) -> EncapOutput {
43 self.kem.set_test_data(ikm_e);
44 let KemResult { enc, shared_secret } = self.kem.encap(&pk_rm.into()).unwrap();
45
46 EncapOutput { enc, shared_secret }
47 }
48
49 fn decap(&mut self, enc: Vec<u8>, sk_rm: Vec<u8>, pk_rm: Vec<u8>) -> Vec<u8> {
50 self.kem.decap(&enc, &sk_rm.into(), &pk_rm.into()).unwrap()
51 }
52 }