Skip to main content

dcap_qvl/
crypto.rs

1//! Audited [`CryptoProvider`] implementations.
2//!
3//! [`RingCrypto`] (gated by `ring`) and [`RustCryptoCrypto`] (gated by
4//! `rustcrypto`) are selected by [`crate::configs::RingConfig`] /
5//! [`crate::configs::RustCryptoConfig`] respectively.
6
7use crate::config::CryptoProvider;
8
9/// Audited [`CryptoProvider`] backed by the `ring` crate (gated by the `ring`
10/// feature). Selected by [`crate::configs::RingConfig`] /
11/// [`crate::configs::DefaultConfig`].
12#[cfg(feature = "ring")]
13pub struct RingCrypto;
14
15#[cfg(feature = "ring")]
16impl CryptoProvider for RingCrypto {
17    fn sig_algo() -> &'static dyn rustls_pki_types::SignatureVerificationAlgorithm {
18        webpki::ring::ECDSA_P256_SHA256
19    }
20
21    fn sha256(data: &[u8]) -> [u8; 32] {
22        let digest = ::ring::digest::digest(&::ring::digest::SHA256, data);
23        let mut out = [0u8; 32];
24        out.copy_from_slice(digest.as_ref());
25        out
26    }
27
28    fn sha384(data: &[u8]) -> [u8; 48] {
29        let digest = ring::digest::digest(&ring::digest::SHA384, data);
30        let mut out = [0u8; 48];
31        out.copy_from_slice(digest.as_ref());
32        out
33    }
34}
35
36/// Audited [`CryptoProvider`] backed by RustCrypto (`sha2` + `p256`, gated by
37/// the `rustcrypto` feature). Selected by [`crate::configs::RustCryptoConfig`].
38#[cfg(feature = "rustcrypto")]
39pub struct RustCryptoCrypto;
40
41#[cfg(feature = "rustcrypto")]
42impl CryptoProvider for RustCryptoCrypto {
43    fn sig_algo() -> &'static dyn rustls_pki_types::SignatureVerificationAlgorithm {
44        webpki::rustcrypto::ECDSA_P256_SHA256
45    }
46
47    fn sha256(data: &[u8]) -> [u8; 32] {
48        use sha2::Digest;
49        sha2::Sha256::digest(data).into()
50    }
51
52    fn sha384(data: &[u8]) -> [u8; 48] {
53        use sha2::Digest as _;
54        sha2::Sha384::digest(data).into()
55    }
56}