ytls_rustcrypto/
hkdf.rs

1//! yTLS RustCrypto Hdkf
2
3use ytls_traits::CryptoSha256HkdfExtractProcessor;
4use ytls_traits::CryptoSha256HkdfGenProcessor;
5
6/// yTLS RustCrypto Hdkdf
7pub struct Sha256Hkdf;
8pub struct Sha256HkdfExtract {
9    inner: GenericHkdf<Hmac<Sha256>>,
10}
11
12pub use hkdf::{hmac::Hmac, GenericHkdf, Hkdf};
13pub use sha2::{Digest, Sha256};
14
15impl Sha256Hkdf {
16    pub fn sha256_hkdf_init() -> Self {
17        Self {}
18    }
19    pub fn sha256_hkdf_from_prk(
20        prk: &[u8],
21    ) -> Result<impl CryptoSha256HkdfGenProcessor, hkdf::InvalidPrkLength> {
22        Ok(Sha256HkdfExtract {
23            inner: Hkdf::<Sha256>::from_prk(prk)?,
24        })
25    }
26}
27
28impl CryptoSha256HkdfExtractProcessor for Sha256Hkdf {
29    fn hkdf_sha256_extract(
30        &self,
31        salt: Option<&[u8]>,
32        ikm: &[u8],
33    ) -> ([u8; 32], impl CryptoSha256HkdfGenProcessor) {
34        let (handshake_secret, hs_hk) = Hkdf::<Sha256>::extract(salt, ikm);
35        (handshake_secret.into(), Sha256HkdfExtract { inner: hs_hk })
36    }
37}
38
39impl CryptoSha256HkdfGenProcessor for Sha256HkdfExtract {
40    type Error = hkdf::InvalidLength;
41    fn hkdf_sha256_expand(&self, info: &[u8], okm: &mut [u8]) -> Result<(), Self::Error> {
42        self.inner.expand(info, okm)
43    }
44}