noq_proto/crypto/
ring_like.rs1#[cfg(all(feature = "aws-lc-rs", not(feature = "ring")))]
2use aws_lc_rs::{aead, error, hkdf, hmac};
3#[cfg(feature = "ring")]
4use ring::{aead, error, hkdf, hmac};
5
6use crate::crypto::{self, CryptoError};
7
8impl crypto::HmacKey for hmac::Key {
9 fn sign(&self, data: &[u8], out: &mut [u8]) {
10 out.copy_from_slice(hmac::sign(self, data).as_ref());
11 }
12
13 fn signature_len(&self) -> usize {
14 32
15 }
16
17 fn verify(&self, data: &[u8], signature: &[u8]) -> Result<(), CryptoError> {
18 Ok(hmac::verify(self, data, signature)?)
19 }
20}
21
22pub(crate) struct RetryTokenKey(hkdf::Prk);
34
35impl RetryTokenKey {
36 pub(crate) fn new(rng: &mut impl rand::Rng) -> Self {
37 let mut master_key = [0u8; 64];
38 rng.fill_bytes(&mut master_key);
39 let master_key = hkdf::Salt::new(hkdf::HKDF_SHA256, &[]).extract(&master_key);
40 Self(master_key)
41 }
42
43 fn derive_aead(&self, token_nonce: u128) -> aead::LessSafeKey {
44 let nonce_bytes = token_nonce.to_le_bytes();
45 let info = &[&nonce_bytes[..]];
46 let okm = self.0.expand(info, hkdf::HKDF_SHA256).unwrap();
47
48 let mut key_buffer = [0u8; 32];
49 okm.fill(&mut key_buffer).unwrap();
50
51 let key = aead::UnboundKey::new(&aead::AES_256_GCM, &key_buffer).unwrap();
52 aead::LessSafeKey::new(key)
53 }
54}
55
56impl crypto::HandshakeTokenKey for RetryTokenKey {
57 fn seal(&self, token_nonce: u128, data: &mut Vec<u8>) -> Result<(), CryptoError> {
58 let aead_key = self.derive_aead(token_nonce);
59 let nonce = aead::Nonce::assume_unique_for_key([0u8; 12]); let aad = aead::Aad::empty();
61 aead_key.seal_in_place_append_tag(nonce, aad, data)?;
62 Ok(())
63 }
64
65 fn open<'a>(&self, token_nonce: u128, data: &'a mut [u8]) -> Result<&'a [u8], CryptoError> {
66 let aead_key = self.derive_aead(token_nonce);
67 let aad = aead::Aad::empty();
68 let nonce = aead::Nonce::assume_unique_for_key([0u8; 12]); Ok(aead_key.open_in_place(nonce, aad, data)?)
70 }
71}
72
73impl From<error::Unspecified> for CryptoError {
74 fn from(_: error::Unspecified) -> Self {
75 Self
76 }
77}