Skip to main content

crypto_profile/
lib.rs

1//! Algorithm types, the per-module capability model, the named profiles and the conformance suite
2//! shared by every `crypto-profile` backend. This crate performs no cryptography.
3#![deny(missing_docs)]
4
5pub mod algorithm;
6mod error;
7pub mod peer_certificate;
8pub use peer_certificate::{
9    CertificateSignature, MlDsaParameterSet, PeerCertificateRule, PeerCertificateRuleId,
10    PeerCertificateViolation, PeerCurve, PeerKey, PeerKeyRule, RsaSizeRule, peer_certificate_rule,
11};
12
13pub use algorithm::{
14    AeadAlgorithm, Algorithm, CipherSuite, HashAlgorithm, KdfAlgorithm, KeyAgreementAlgorithm,
15    KxGroup, MacAlgorithm, ParseAlgorithmError, SignatureAlgorithm, TlsSignatureScheme, TlsVersion,
16};
17pub use error::{CryptoError, CsrError, Family, PeerVerifierIdentity, TlsError};
18
19pub mod capability;
20pub use capability::{
21    Approval, Capability, CertStatus, Certificate, Linkage, Module, ModuleLine, capability,
22    certificate, ensure_available,
23};
24pub mod profile;
25pub use profile::{
26    AdmittedSelection, GoverningText, MAC_KEY_MAX_BITS, Options, ParseProfileError, Profile,
27    ProfileSpec, kdf_info_may_be_empty, mac_key_floor_bits, select_admitted,
28};
29#[cfg(feature = "conformance")]
30pub mod conformance;
31pub mod sealbox_wire;
32#[doc(hidden)]
33pub mod __test_data {
34    pub const RSA8192_PKCS8: &[u8] = include_bytes!("../testdata/rsa8192.pkcs8.der");
35}
36/// `Options`, `ProfileSpec`, `Selection` and `AdmittedSelection` are `#[non_exhaustive]`
37/// (decision 0012), so a new field is not a breaking change. Outside this crate `Options` is built
38/// from its default:
39///
40/// ```
41/// use crypto_profile::Options;
42/// let mut o = Options::default();
43/// o.pq_key_exchange_required = true;
44/// assert_eq!(o.to_string(), "post-quantum key exchange required");
45/// ```
46///
47/// ```compile_fail,E0639
48/// use crypto_profile::Options;
49/// let mut o = Options { ..Options::default() };
50/// o.pq_key_exchange_required = true;
51/// assert_eq!(o.to_string(), "post-quantum key exchange required");
52/// ```
53#[allow(dead_code)]
54mod options_are_built_from_their_default {}
55/// The algorithm enums, `Algorithm` and `Profile` are `#[non_exhaustive]` (decision 0012), so a
56/// new variant is not a breaking change. A `match` outside this crate needs a `_` arm:
57///
58/// ```
59/// use crypto_profile::{Algorithm, MacAlgorithm};
60/// fn outside_tls(a: Algorithm) -> Option<bool> {
61///     match a {
62///         Algorithm::Scheme(_) | Algorithm::Kx(_) => Some(false),
63///         Algorithm::Cipher(_) | Algorithm::Protocol(_) => Some(false),
64///         Algorithm::Mac(_) | Algorithm::Kdf(_) | Algorithm::KeyAgreement(_) => Some(true),
65///         Algorithm::Aead(_) | Algorithm::Hash(_) | Algorithm::Signature(_) => Some(true),
66///         _ => None,
67///     }
68/// }
69/// assert_eq!(outside_tls(Algorithm::Mac(MacAlgorithm::HmacSha256)), Some(true));
70/// ```
71///
72/// ```compile_fail,E0004
73/// use crypto_profile::{Algorithm, MacAlgorithm};
74/// fn outside_tls(a: Algorithm) -> Option<bool> {
75///     match a {
76///         Algorithm::Scheme(_) | Algorithm::Kx(_) => Some(false),
77///         Algorithm::Cipher(_) | Algorithm::Protocol(_) => Some(false),
78///         Algorithm::Mac(_) | Algorithm::Kdf(_) | Algorithm::KeyAgreement(_) => Some(true),
79///         Algorithm::Aead(_) | Algorithm::Hash(_) | Algorithm::Signature(_) => Some(true),
80///     }
81/// }
82/// assert_eq!(outside_tls(Algorithm::Mac(MacAlgorithm::HmacSha256)), Some(true));
83/// ```
84///
85/// ```
86/// use crypto_profile::Profile;
87/// fn government(p: Profile) -> Option<bool> {
88///     match p {
89///         Profile::Unrestricted | Profile::Preferred => Some(false),
90///         Profile::Cnsa2 | Profile::Cnsa1 | Profile::Bsi => Some(true),
91///         Profile::Fips1403 | Profile::Fips1402 => Some(true),
92///         _ => None,
93///     }
94/// }
95/// assert_eq!(government(Profile::Bsi), Some(true));
96/// ```
97///
98/// ```compile_fail,E0004
99/// use crypto_profile::Profile;
100/// fn government(p: Profile) -> Option<bool> {
101///     match p {
102///         Profile::Unrestricted | Profile::Preferred => Some(false),
103///         Profile::Cnsa2 | Profile::Cnsa1 | Profile::Bsi => Some(true),
104///         Profile::Fips1403 | Profile::Fips1402 => Some(true),
105///     }
106/// }
107/// assert_eq!(government(Profile::Bsi), Some(true));
108/// ```
109///
110/// ```
111/// use crypto_profile::HashAlgorithm;
112/// fn sha3(h: HashAlgorithm) -> Option<bool> {
113///     match h {
114///         HashAlgorithm::Sha256 | HashAlgorithm::Sha384 | HashAlgorithm::Sha512 => Some(false),
115///         HashAlgorithm::Sha3_256 | HashAlgorithm::Sha3_384 | HashAlgorithm::Sha3_512 => Some(true),
116///         _ => None,
117///     }
118/// }
119/// assert_eq!(sha3(HashAlgorithm::Sha3_384), Some(true));
120/// ```
121///
122/// ```compile_fail,E0004
123/// use crypto_profile::HashAlgorithm;
124/// fn sha3(h: HashAlgorithm) -> Option<bool> {
125///     match h {
126///         HashAlgorithm::Sha256 | HashAlgorithm::Sha384 | HashAlgorithm::Sha512 => Some(false),
127///         HashAlgorithm::Sha3_256 | HashAlgorithm::Sha3_384 | HashAlgorithm::Sha3_512 => Some(true),
128///     }
129/// }
130/// assert_eq!(sha3(HashAlgorithm::Sha3_384), Some(true));
131/// ```
132///
133/// ```
134/// use crypto_profile::SignatureAlgorithm as S;
135/// fn pss(a: S) -> Option<bool> {
136///     match a {
137///         S::Rsa2048Sha384Pss | S::Rsa3072Sha384Pss | S::Rsa4096Sha384Pss => Some(true),
138///         S::Rsa2048Sha384 | S::Rsa3072Sha384 | S::Rsa4096Sha384 => Some(false),
139///         S::MlDsa87 | S::EcdsaP256Sha256 | S::EcdsaP384Sha384 => Some(false),
140///         S::EcdsaP521Sha384 | S::EcdsaP521Sha512 | S::Ed25519 => Some(false),
141///         _ => None,
142///     }
143/// }
144/// assert_eq!(pss(S::Rsa3072Sha384Pss), Some(true));
145/// ```
146///
147/// ```compile_fail,E0004
148/// use crypto_profile::SignatureAlgorithm as S;
149/// fn pss(a: S) -> Option<bool> {
150///     match a {
151///         S::Rsa2048Sha384Pss | S::Rsa3072Sha384Pss | S::Rsa4096Sha384Pss => Some(true),
152///         S::Rsa2048Sha384 | S::Rsa3072Sha384 | S::Rsa4096Sha384 => Some(false),
153///         S::MlDsa87 | S::EcdsaP256Sha256 | S::EcdsaP384Sha384 => Some(false),
154///         S::EcdsaP521Sha384 | S::EcdsaP521Sha512 | S::Ed25519 => Some(false),
155///     }
156/// }
157/// assert_eq!(pss(S::Rsa3072Sha384Pss), Some(true));
158/// ```
159#[allow(dead_code)]
160mod matches_outside_the_crate_need_a_wildcard {}
161/// Every `ALL` is a `&'static [T]` (decision 0012), so a new variant changes its length, not its
162/// type:
163///
164/// ```
165/// use crypto_profile::{HashAlgorithm, Profile};
166/// let all: &'static [Profile] = Profile::ALL;
167/// assert!(all.contains(&Profile::Cnsa2));
168/// let mut lens = Vec::new();
169/// for &h in HashAlgorithm::ALL {
170///     lens.push(h.output_len());
171/// }
172/// assert_eq!(lens, [32, 48, 64, 32, 48, 64]);
173/// ```
174#[allow(dead_code)]
175mod every_all_is_a_slice {}
176include!(concat!(env!("OUT_DIR"), "/provider.rs"));
177include!(concat!(env!("OUT_DIR"), "/backend_body.rs"));