ytls_rustcrypto/
lib.rs

1#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
2#![warn(
3    clippy::unwrap_used,
4    missing_docs,
5    rust_2018_idioms,
6    unused_lifetimes,
7    unused_qualifications
8)]
9#![doc = include_str!("../README.md")]
10#![allow(missing_docs)]
11#![allow(unused_imports)]
12#![allow(dead_code)]
13
14//***********************************************
15// Re-Exports
16//***********************************************
17
18//-----------------------------------------------
19// All Errors
20//-----------------------------------------------
21mod error;
22pub use error::*;
23
24//-----------------------------------------------
25//
26//-----------------------------------------------
27
28mod dh;
29#[doc(inline)]
30pub use dh::*;
31
32mod hasher;
33#[doc(inline)]
34pub use hasher::*;
35
36mod hmac;
37#[doc(inline)]
38pub use hmac::*;
39
40mod hkdf;
41#[doc(inline)]
42pub use hkdf::*;
43
44mod aead;
45#[doc(inline)]
46pub use aead::*;
47
48mod sign;
49#[doc(inline)]
50pub use sign::*;
51
52/// Use this together with yTLS by providing this struct into the
53/// client or server contextes.
54#[derive(Copy, Clone)]
55pub struct RustCrypto;
56
57impl RustCrypto {
58    pub fn init() -> Self {
59        Self {}
60    }
61}
62
63use ytls_traits::CryptoConfig;
64//---------------
65// Hashers
66//---------------
67use ytls_traits::CryptoSha256TranscriptProcessor;
68use ytls_traits::CryptoSha384TranscriptProcessor;
69//---------------
70// HMAC
71//---------------
72use ytls_traits::CryptoSha256HmacProcessor;
73use ytls_traits::CryptoSha384HmacProcessor;
74//---------------
75// HDKF
76//---------------
77use ytls_traits::CryptoSha256HkdfExtractProcessor;
78use ytls_traits::CryptoSha256HkdfGenProcessor;
79//---------------
80// ECDHE
81//---------------
82use ytls_traits::CryptoX25519Processor;
83//---------------
84// AEAD
85//---------------
86use ytls_traits::CryptoChaCha20Poly1305Processor;
87//---------------
88// Signers
89//---------------
90use ytls_traits::CryptoSignerP256Processor;
91
92use rand_core::CryptoRng;
93
94use ::hkdf::InvalidPrkLength;
95
96impl CryptoConfig for RustCrypto {
97    type PrkError = InvalidPrkLength;
98    type Hasher = Sha256Hasher;
99    type X25519 = X25519;
100    #[inline]
101    fn ecdhe_x25519<R: CryptoRng>(rng: &mut R) -> Self::X25519 {
102        X25519::x25519_init(rng)
103    }
104    #[inline]
105    fn hasher_sha256() -> Self::Hasher {
106        Sha256Hasher::sha256_init()
107    }
108    #[inline]
109    fn sign_p256_init(key: &[u8]) -> Option<impl CryptoSignerP256Processor> {
110        SignP256::sign_p256_init(key)
111    }
112    #[inline]
113    fn aead_chaha20poly1305(key: &[u8; 32]) -> impl CryptoChaCha20Poly1305Processor {
114        AeadChaCha20Poly1305::chacha20poly1305_init(key)
115    }
116    #[inline]
117    fn hkdf_sha256_from_prk(
118        prk: &[u8],
119    ) -> Result<impl CryptoSha256HkdfGenProcessor, Self::PrkError> {
120        Sha256Hkdf::sha256_hkdf_from_prk(prk)
121    }
122    #[inline]
123    fn hkdf_sha256_init() -> impl CryptoSha256HkdfExtractProcessor {
124        Sha256Hkdf::sha256_hkdf_init()
125    }
126    #[inline]
127    fn hmac_sha384_init_with_key(key: &[u8; 48]) -> impl CryptoSha384HmacProcessor {
128        Sha384Hmac::sha384_hmac_init_with_key(key)
129    }
130    #[inline]
131    fn hmac_sha256_init_with_key(key: &[u8; 32]) -> impl CryptoSha256HmacProcessor {
132        Sha256Hmac::sha256_hmac_init_with_key(key)
133    }
134    #[inline]
135    fn sha256_init() -> impl CryptoSha256TranscriptProcessor {
136        Sha256Hasher::sha256_init()
137    }
138    #[inline]
139    fn sha384_init() -> impl CryptoSha384TranscriptProcessor {
140        Sha384Hasher::sha384_init()
141    }
142    #[inline]
143    fn x25519_init<R: CryptoRng>(&mut self, rng: &mut R) -> impl CryptoX25519Processor {
144        X25519::x25519_init(rng)
145    }
146}