Skip to main content

crypto_profile/
profile.rs

1//! Named profiles: algorithm sets a backend resolves into a TLS policy and the key algorithms it
2//! admits. A profile is an algorithm set, not a validation claim; whether the running module is
3//! validated for it is a separate check.
4use crate::algorithm::{
5    AeadAlgorithm, Algorithm, CipherSuite as C, HashAlgorithm, KdfAlgorithm, KeyAgreementAlgorithm,
6    KxGroup as K, MacAlgorithm, SignatureAlgorithm as S, TlsSignatureScheme as T, TlsVersion as V,
7};
8use crate::capability::{Module, capability};
9use crate::error::{CryptoError, Family};
10use std::fmt;
11use std::str::FromStr;
12
13/// A named profile. Each one that is named for a regime follows that regime's published text, in
14/// the edition [`Profile::governing_text`] names, and changes only when a new edition is published.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[non_exhaustive]
17pub enum Profile {
18    /// Every algorithm in the inventory, post-quantum key exchange first. Not a government profile.
19    Unrestricted,
20    /// The crate's recommendation. Not a government profile; see `docs/profiles.md`.
21    Preferred,
22    /// The CNSA 2.0 algorithm set of CNSSP 15 (December 2024), Annex B, on TLS 1.3.
23    Cnsa2,
24    /// The CNSA 1.0 algorithm set of CNSSP 15 (20 October 2016), Annex B, in RFC 9151's TLS
25    /// profile. A transition profile for CNSA 1.0 counterparts. Not every RFC 9151 requirement is
26    /// met: `docs/profiles.md` names the algorithm gaps (the `cnsa1` entry in section 3, sections 4
27    /// and 5), and the RFC 8603 certificate profile that §5.4 requires is not implemented.
28    Cnsa1,
29    /// BSI TR-02102-2 and TR-02102-1, version 2026-01.
30    Bsi,
31    /// What a FIPS 140-3 boundary approves under the published texts.
32    Fips1403,
33    /// What a FIPS 140-2 boundary approves under the published texts.
34    Fips1402,
35}
36
37/// The text a profile follows.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub enum GoverningText {
41    /// The crate's own recommendation, following no published text.
42    CrateRecommendation,
43    /// Published documents, each with its edition.
44    Published {
45        /// The documents, each naming its edition.
46        documents: &'static [&'static str],
47    },
48}
49
50impl Profile {
51    /// Every profile.
52    pub const ALL: &'static [Profile] = &[
53        Profile::Unrestricted,
54        Profile::Preferred,
55        Profile::Cnsa2,
56        Profile::Cnsa1,
57        Profile::Bsi,
58        Profile::Fips1403,
59        Profile::Fips1402,
60    ];
61
62    /// The position of `self` in [`Self::ALL`].
63    #[cfg(test)]
64    pub(crate) const fn ordinal(self) -> usize {
65        match self {
66            Profile::Unrestricted => 0,
67            Profile::Preferred => 1,
68            Profile::Cnsa2 => 2,
69            Profile::Cnsa1 => 3,
70            Profile::Bsi => 4,
71            Profile::Fips1403 => 5,
72            Profile::Fips1402 => 6,
73        }
74    }
75
76    /// The profile's stable configuration name.
77    pub const fn as_str(self) -> &'static str {
78        match self {
79            Profile::Unrestricted => "unrestricted",
80            Profile::Preferred => "preferred",
81            Profile::Cnsa2 => "cnsa2",
82            Profile::Cnsa1 => "cnsa1",
83            Profile::Bsi => "bsi",
84            Profile::Fips1403 => "fips1403",
85            Profile::Fips1402 => "fips1402",
86        }
87    }
88
89    /// The texts the profile follows, with each document's edition. The profile constant's doc
90    /// comment also cites the standards and implementation guidance these texts rely on.
91    pub const fn governing_text(self) -> GoverningText {
92        match self {
93            Profile::Unrestricted | Profile::Preferred => GoverningText::CrateRecommendation,
94            Profile::Cnsa2 => GoverningText::Published {
95                documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
96            },
97            Profile::Cnsa1 => GoverningText::Published {
98                documents: &[
99                    "CNSSP 15 (20 October 2016), Annex B, p. B-1",
100                    "RFC 9151 (April 2022)",
101                ],
102            },
103            Profile::Bsi => GoverningText::Published {
104                documents: &[
105                    "BSI TR-02102-2, version 2026-01",
106                    "BSI TR-02102-1, version 2026-01",
107                ],
108            },
109            Profile::Fips1403 => GoverningText::Published {
110                documents: &[
111                    "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
112                    "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
113                    "NIST SP 800-52r2",
114                    "NIST SP 800-131A r2",
115                ],
116            },
117            Profile::Fips1402 => GoverningText::Published {
118                documents: &[
119                    "FIPS 140-2 Annex A and Annex D (2021)",
120                    "FIPS 186-4",
121                    "NIST SP 800-131A r2",
122                    "NIST SP 800-52r2",
123                ],
124            },
125        }
126    }
127}
128
129impl fmt::Display for Profile {
130    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131        f.write_str(self.as_str())
132    }
133}
134
135/// A name that is not one of the crate's profiles. Names are matched exactly: no trimming and no
136/// case folding.
137#[derive(Debug, Clone, PartialEq, Eq)]
138#[non_exhaustive]
139pub struct ParseProfileError(pub String);
140
141impl fmt::Display for ParseProfileError {
142    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143        write!(f, "unknown profile {:?}", self.0)
144    }
145}
146
147impl std::error::Error for ParseProfileError {}
148
149impl FromStr for Profile {
150    type Err = ParseProfileError;
151    fn from_str(s: &str) -> Result<Self, Self::Err> {
152        Profile::ALL
153            .iter()
154            .copied()
155            .find(|p| p.as_str() == s)
156            .ok_or_else(|| ParseProfileError(s.to_string()))
157    }
158}
159
160/// Options applied on top of any profile. The default changes nothing; `cnsa1_unclassified_nss`
161/// widens `cnsa1`'s MACs, HKDFs, hashes and signatures, and the others narrow. Outside this crate,
162/// build it from `Options::default()` and set the fields you need.
163#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
164#[non_exhaustive]
165pub struct Options {
166    /// Keep only post-quantum key exchange (pure ML-KEM and hybrid groups), and so remove TLS 1.2,
167    /// which has none. Key algorithms and signatures are not narrowed.
168    pub pq_key_exchange_required: bool,
169    /// Require peer leaf keys and certification paths to match the profile. Off by default.
170    pub peer_certificates_match_profile: bool,
171    /// Admit HMAC-SHA256, HKDF-SHA256 and SHA-256 hashing under `cnsa1`, as CNSSP 15 (20 October
172    /// 2016) Annex B ¶2b (p. B-2) allows SHA-256 in the near term for UNCLASSIFIED NSS data and
173    /// community-of-interest separation only, and 2048-bit RSA signatures with SHA-384 (PSS and
174    /// PKCS#1 v1.5), as ¶2b allows RSA "at the 2048 bit level". Ignored by every other profile, and
175    /// by every family but message authentication, HKDF, hashing and signatures outside TLS. It
176    /// admits no one-step KDF and no curve but P-384, for key agreement or ECDSA, and does not
177    /// widen `cnsa1`'s AEADs beyond AES-256-GCM or its TLS key algorithms. Off by default.
178    pub cnsa1_unclassified_nss: bool,
179}
180
181impl fmt::Display for Options {
182    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183        let names: Vec<&str> = [
184            (
185                self.pq_key_exchange_required,
186                "post-quantum key exchange required",
187            ),
188            (
189                self.peer_certificates_match_profile,
190                "peer certificates match profile",
191            ),
192            (self.cnsa1_unclassified_nss, "cnsa1 UNCLASSIFIED NSS"),
193        ]
194        .into_iter()
195        .filter_map(|(on, name)| on.then_some(name))
196        .collect();
197        if names.is_empty() {
198            f.write_str("no options")
199        } else {
200            f.write_str(&names.join(", "))
201        }
202    }
203}
204
205/// A profile's contents, each list in preference order.
206#[derive(Debug, Clone, Copy, PartialEq, Eq)]
207#[non_exhaustive]
208pub struct ProfileSpec {
209    /// Protocol versions.
210    pub versions: &'static [V],
211    /// Cipher suites.
212    pub cipher_suites: &'static [C],
213    /// Key-exchange groups.
214    pub kx_groups: &'static [K],
215    /// Handshake signature schemes accepted from the peer.
216    pub signature_schemes: &'static [T],
217    /// TLS certificate key algorithms, for `resolve` and `Resolved::admit_key`.
218    pub key_algorithms: &'static [S],
219    /// Message authentication codes, for `hmac`; admitted through `admit`, not `resolve`.
220    pub macs: &'static [MacAlgorithm],
221    /// Key-derivation functions, for `kdf`; admitted through `admit`, not `resolve`.
222    pub kdfs: &'static [KdfAlgorithm],
223    /// Key agreement outside TLS, for `ecdh`; admitted through `admit`, not `resolve`.
224    pub key_agreements: &'static [KeyAgreementAlgorithm],
225    /// Authenticated encryption outside TLS, for `aead`; admitted through `admit`, not `resolve`.
226    pub aeads: &'static [AeadAlgorithm],
227    /// Hash functions outside TLS, for `digest`; admitted through `admit`, not `resolve`.
228    pub hashes: &'static [HashAlgorithm],
229    /// Signature algorithms outside TLS, for `signature`; admitted through `admit`, not `resolve`.
230    pub signatures: &'static [S],
231}
232
233impl Profile {
234    /// The profile's contents. Each entry's source is cited on the constant it comes from.
235    pub const fn spec(self) -> &'static ProfileSpec {
236        match self {
237            Profile::Unrestricted => &UNRESTRICTED,
238            Profile::Preferred => &PREFERRED,
239            Profile::Cnsa2 => &CNSA2,
240            Profile::Cnsa1 => &CNSA1,
241            Profile::Bsi => &BSI,
242            Profile::Fips1403 => &FIPS1403,
243            Profile::Fips1402 => &FIPS1402,
244        }
245    }
246}
247
248/// `unrestricted`: the crate's own set, following no published text. Not a government profile.
249///
250/// - Versions: TLS 1.3, TLS 1.2. Suites: all nine. Groups: all seven. Schemes: all eleven
251///   (`crypto_profile::algorithm`, each family's `ALL`). Keys: the nine TLS key algorithms, every
252///   `SignatureAlgorithm` but the RSA-PSS variants.
253/// - MACs: every HMAC in the inventory.
254/// - KDFs: every HKDF and one-step KDF in the inventory.
255/// - Key agreement: ECDH on P-384, P-521 and P-256, and X25519: every curve the backends implement.
256/// - AEADs: AES-256-GCM, AES-128-GCM and ChaCha20-Poly1305: every AEAD the backends implement.
257/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384 and SHA3-256: every hash the backends
258///   implement.
259/// - Signatures: ML-DSA-87; ECDSA on P-384, on P-521 with SHA-512 and with SHA-384, and on P-256;
260///   Ed25519; RSA 4096, 3072 and 2048 with PSS, then with PKCS#1 v1.5: every signature algorithm
261///   the backends implement.
262pub const UNRESTRICTED: ProfileSpec = ProfileSpec {
263    versions: &[V::Tls13, V::Tls12],
264    cipher_suites: &[
265        C::Tls13Aes256GcmSha384,
266        C::Tls13Aes128GcmSha256,
267        C::Tls13Chacha20Poly1305Sha256,
268        C::Tls12EcdheEcdsaAes256GcmSha384,
269        C::Tls12EcdheRsaAes256GcmSha384,
270        C::Tls12EcdheEcdsaAes128GcmSha256,
271        C::Tls12EcdheRsaAes128GcmSha256,
272        C::Tls12EcdheEcdsaChacha20Poly1305Sha256,
273        C::Tls12EcdheRsaChacha20Poly1305Sha256,
274    ],
275    kx_groups: &[
276        K::MlKem1024,
277        K::Secp384r1MlKem1024,
278        K::X25519MlKem768,
279        K::Secp256r1MlKem768,
280        K::Secp384r1,
281        K::Secp256r1,
282        K::X25519,
283    ],
284    signature_schemes: &[
285        T::MlDsa87,
286        T::EcdsaNistp384Sha384,
287        T::EcdsaNistp256Sha256,
288        T::EcdsaNistp521Sha512,
289        T::Ed25519,
290        T::RsaPssSha384,
291        T::RsaPssSha512,
292        T::RsaPssSha256,
293        T::RsaPkcs1Sha384,
294        T::RsaPkcs1Sha512,
295        T::RsaPkcs1Sha256,
296    ],
297    key_algorithms: &[
298        S::MlDsa87,
299        S::EcdsaP384Sha384,
300        S::EcdsaP256Sha256,
301        S::EcdsaP521Sha512,
302        S::EcdsaP521Sha384,
303        S::Ed25519,
304        S::Rsa3072Sha384,
305        S::Rsa4096Sha384,
306        S::Rsa2048Sha384,
307    ],
308    macs: &[
309        MacAlgorithm::HmacSha512,
310        MacAlgorithm::HmacSha384,
311        MacAlgorithm::HmacSha256,
312    ],
313    kdfs: &[
314        KdfAlgorithm::HkdfSha512,
315        KdfAlgorithm::HkdfSha384,
316        KdfAlgorithm::HkdfSha256,
317        KdfAlgorithm::OneStepSha512,
318        KdfAlgorithm::OneStepSha384,
319        KdfAlgorithm::OneStepSha256,
320    ],
321    key_agreements: &[
322        KeyAgreementAlgorithm::EcdhP384,
323        KeyAgreementAlgorithm::EcdhP521,
324        KeyAgreementAlgorithm::EcdhP256,
325        KeyAgreementAlgorithm::X25519,
326    ],
327    aeads: &[
328        AeadAlgorithm::Aes256Gcm,
329        AeadAlgorithm::Aes128Gcm,
330        AeadAlgorithm::Chacha20Poly1305,
331    ],
332    hashes: &[
333        HashAlgorithm::Sha512,
334        HashAlgorithm::Sha384,
335        HashAlgorithm::Sha256,
336        HashAlgorithm::Sha3_512,
337        HashAlgorithm::Sha3_384,
338        HashAlgorithm::Sha3_256,
339    ],
340    signatures: &[
341        S::MlDsa87,
342        S::EcdsaP384Sha384,
343        S::EcdsaP521Sha512,
344        S::EcdsaP521Sha384,
345        S::EcdsaP256Sha256,
346        S::Ed25519,
347        S::Rsa4096Sha384Pss,
348        S::Rsa3072Sha384Pss,
349        S::Rsa2048Sha384Pss,
350        S::Rsa4096Sha384,
351        S::Rsa3072Sha384,
352        S::Rsa2048Sha384,
353    ],
354};
355
356/// `preferred`: the crate's recommendation. Not a government profile; see `docs/profiles.md`.
357///
358/// "ISM" is ASD, *Information security manual, Guidelines for cryptography*, September 2026;
359/// SECRET-level controls ISM-1761 and ISM-1763 (p. 8), ISM-1765 (p. 10), ISM-1769 (p. 11).
360/// - TLS 1.3 only: ISM-1139, p. 19.
361/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: ISM-1369, p. 20; ISM-1769, p. 11.
362/// - ML-KEM-1024: ISM-1995, p. 10.
363/// - SecP384r1MLKEM1024, X25519MLKEM768, SecP256r1MLKEM768: ISM p. 12; ISM-1996, p. 13; FIPS 140-3 IG D.S
364///   Scenario 2 and Additional Comment 8a (August 2026, pp. 223-226), which names all three.
365/// - secp384r1, secp256r1: ISM-0474 and ISM-1761, p. 8; ISM-1446, p. 7.
366/// - X25519 on its own excluded: FIPS 140-3 IG C.K Resolution 5 (p. 160).
367/// - ChaCha20-Poly1305 excluded: ISM p. 6; SP 800-140Cr2 §6.1.
368/// - Schemes ML-DSA-87 (ISM-1991, p. 9), ECDSA P-384, P-256, P-521 (ISM-1763, p. 8), RSA-PSS
369///   with SHA-2 (ISM-1765, p. 10; ISM-1374, p. 20).
370/// - Ed25519 excluded: ISM pp. 5-6.
371/// - RSA PKCS#1 v1.5 schemes excluded: RFC 8446 §4.2.3.
372/// - Keys ML-DSA-87 (ISM-1991, p. 9), ECDSA P-384, P-256, P-521 (ISM-1763, p. 8), RSA 3072 and
373///   4096 (ISM-1765, p. 10); RSA 2048 excluded: ISM-1765, p. 10.
374/// - ECDSA P-521 with SHA-384: ISM-1763, p. 8; SHA-2 output of at least 256 bits, ISM-1767,
375///   p. 11.
376/// - MACs: HMAC with SHA-2 for IPsec and TLS, ISM-1772 (p. 15), ISM-0998 (p. 16) and ISM-1375
377///   (p. 20); SHA-2 output of at least 224 bits, ISM-1766 (p. 11). HMAC-SHA256 "will not be
378///   approved beyond 2030" (pp. 15-16).
379/// - KDFs: HKDF, the two-step method of SP 800-56C Rev. 2 §5, of which RFC 5869 "specifies a
380///   version" (SP 800-56C Rev. 2, p. 20); the ISM cites SP 800-56C Rev. 2 for combining hybrid
381///   key-encapsulation components (p. 13). SHA-2 output of at least 224 bits, ISM-1766 (p. 11);
382///   SHA-256 "will not be approved beyond 2030" (p. 11). Decision 0010. The one-step KDF of SP
383///   800-56C Rev. 2 §4, under the same reading of that citation (decision 0011).
384/// - Key agreement: ECDH on P-384, P-521 and P-256, "preferably the NIST P-384 curve", ISM-1761
385///   (p. 8). X25519 excluded: ISM-1761 names only the NIST curves. ECDH "will not be approved
386///   beyond 2030" (p. 8).
387/// - AEADs: AES-256-GCM, AES-128-GCM. "The only approved symmetric cryptographic algorithm is
388///   ... AES" (p. 6); AES-128, AES-192 or AES-256, "preferably AES-256", ISM-1769 (p. 11); GCM,
389///   named on p. 11, preferred by ISM-1771 (p. 15) and ISM-0998 (p. 16), and required for TLS by ISM-1369 (p. 20).
390///   AES-128 "will not be approved beyond 2030" (p. 11). ChaCha20-Poly1305 excluded: p. 6 and
391///   ISM-0471 (p. 7).
392/// - Hashes: SHA-512, SHA-384, SHA-256. "The only approved hashing algorithm for general-purpose
393///   use is Secure Hashing Algorithm 2 (SHA-2)" (p. 6); ISM-1767 (p. 11), an output size of at
394///   least 256 bits, "preferably SHA-384 or SHA-512". SHA-256 "will not be approved beyond 2030"
395///   (p. 11). SHA-3 excluded: "approved exclusively for use within ML-DSA and ML-KEM" (p. 6),
396///   "restricted to use within internal steps of ML-DSA and ML-KEM" (p. 11).
397/// - Signatures: ML-DSA-87, "preferably ML-DSA-87", ISM-1991 (p. 9); ECDSA on P-384, P-521 and
398///   P-256, ISM-1763 (p. 8); RSA 4096 and 3072 with PSS, then with PKCS#1 v1.5, ISM-1765 (p. 10),
399///   which names no padding. Ed25519 excluded: ISM pp. 5-6. RSA 2048 excluded: ISM-1765 (p. 10).
400///   ECDSA P-521 with SHA-384 excluded: FIPS 186-5 §6.1.1 (p. 21), a hash weaker than the curve
401///   "shall not be used" (decision 0015). ECDSA and RSA "will not be approved beyond 2030" (pp. 8
402///   and 10).
403pub const PREFERRED: ProfileSpec = ProfileSpec {
404    versions: &[V::Tls13],
405    cipher_suites: &[C::Tls13Aes256GcmSha384, C::Tls13Aes128GcmSha256],
406    kx_groups: &[
407        K::MlKem1024,
408        K::Secp384r1MlKem1024,
409        K::X25519MlKem768,
410        K::Secp256r1MlKem768,
411        K::Secp384r1,
412        K::Secp256r1,
413    ],
414    signature_schemes: &[
415        T::MlDsa87,
416        T::EcdsaNistp384Sha384,
417        T::EcdsaNistp256Sha256,
418        T::EcdsaNistp521Sha512,
419        T::RsaPssSha384,
420        T::RsaPssSha512,
421        T::RsaPssSha256,
422    ],
423    key_algorithms: &[
424        S::MlDsa87,
425        S::EcdsaP384Sha384,
426        S::EcdsaP256Sha256,
427        S::EcdsaP521Sha512,
428        S::EcdsaP521Sha384,
429        S::Rsa3072Sha384,
430        S::Rsa4096Sha384,
431    ],
432    macs: &[
433        MacAlgorithm::HmacSha512,
434        MacAlgorithm::HmacSha384,
435        MacAlgorithm::HmacSha256,
436    ],
437    kdfs: &[
438        KdfAlgorithm::HkdfSha512,
439        KdfAlgorithm::HkdfSha384,
440        KdfAlgorithm::HkdfSha256,
441        KdfAlgorithm::OneStepSha512,
442        KdfAlgorithm::OneStepSha384,
443        KdfAlgorithm::OneStepSha256,
444    ],
445    key_agreements: &[
446        KeyAgreementAlgorithm::EcdhP384,
447        KeyAgreementAlgorithm::EcdhP521,
448        KeyAgreementAlgorithm::EcdhP256,
449    ],
450    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
451    hashes: &[
452        HashAlgorithm::Sha512,
453        HashAlgorithm::Sha384,
454        HashAlgorithm::Sha256,
455    ],
456    signatures: &[
457        S::MlDsa87,
458        S::EcdsaP384Sha384,
459        S::EcdsaP521Sha512,
460        S::EcdsaP256Sha256,
461        S::Rsa4096Sha384Pss,
462        S::Rsa3072Sha384Pss,
463        S::Rsa4096Sha384,
464        S::Rsa3072Sha384,
465    ],
466};
467
468/// `cnsa2`: the CNSA 2.0 algorithm set of CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2, on
469/// TLS 1.3. No CNSA 2.0 TLS profile is published: CNSSP 15 ¶8a, footnote 1 (p. 5).
470/// - ML-KEM-1024: Annex B ¶1 table, p. B-1.
471/// - ML-DSA-87 scheme and key: Annex B ¶1 table, p. B-1.
472/// - TLS_AES_256_GCM_SHA384: Annex B ¶1 table, p. B-1.
473/// - TLS 1.3 only: ML-KEM-1024 is the set's only key establishment (Annex B ¶1 table, p. B-1);
474///   TLS 1.2 key exchange is ECDHE or FFDHE (RFC 8422, RFC 7919).
475/// - MACs: hash "SHA-384 or SHA-512", Annex B p. B-1; protocol guidance RFC 9206 and RFC 9151,
476///   ¶8a and fn. 1 (p. 5); RFC 9206 §4 (SHA-384 "for all purposes") and §8 (PRF_HMAC_SHA2_384 or
477///   PRF_HMAC_SHA2_512 only).
478/// - KDFs: CNSSP 15 names no KDF. ¶8a (p. 5) names RFC 9151, whose §4 approves SHA-384 for HKDF
479///   in TLS 1.3; Annex B (p. B-1) admits SHA-384 and SHA-512. Decision 0010. No one-step KDF:
480///   RFC 9151 §4 reaches HKDF only.
481/// - No key agreement outside TLS: Annex B ¶1 table (p. B-1) lists ML-KEM-1024 as the only key
482///   establishment.
483/// - AEADs: AES-256-GCM. Annex B ¶1 table (p. B-1), AES "Use 256 bit keys"; the mode through
484///   ¶8a and fn. 1 (p. 5): RFC 9151 §4 (AES-256 in GCM) and RFC 9206.
485/// - Hashes: SHA-384, SHA-512, in the MAC and KDF order. Annex B ¶1 table (p. B-1), "Use SHA-384 or
486///   SHA-512 to protect up to TOP SECRET"; Annex B governs over RFC 9206 §4's SHA-384 only, which
487///   fn. 1 (p. 5) says "will need to be updated for CNSA 2.0". SHA3 excluded: SHA3-384 or SHA3-512
488///   "allowed for internal hardware functionality (e.g. boot-up integrity checks) only" (p. B-2).
489/// - Signatures: ML-DSA-87, Annex B ¶1 table (p. B-1), "ML-DSA-87 to protect up to TOP SECRET".
490pub const CNSA2: ProfileSpec = ProfileSpec {
491    versions: &[V::Tls13],
492    cipher_suites: &[C::Tls13Aes256GcmSha384],
493    kx_groups: &[K::MlKem1024],
494    signature_schemes: &[T::MlDsa87],
495    key_algorithms: &[S::MlDsa87],
496    macs: &[MacAlgorithm::HmacSha384, MacAlgorithm::HmacSha512],
497    kdfs: &[KdfAlgorithm::HkdfSha384, KdfAlgorithm::HkdfSha512],
498    key_agreements: &[],
499    aeads: &[AeadAlgorithm::Aes256Gcm],
500    hashes: &[HashAlgorithm::Sha384, HashAlgorithm::Sha512],
501    signatures: &[S::MlDsa87],
502};
503
504/// `cnsa1`: the CNSA 1.0 algorithm set of CNSSP 15 (20 October 2016), Annex B (p. B-1), in the
505/// TLS 1.2 and 1.3 profile of RFC 9151 (April 2022). Transition dates: CNSSP 15 (December 2024)
506/// ¶18 (pp. 7-8).
507/// - AES-256, ECDH and ECDSA on P-384, SHA-384, RSA of at least 3072 bits: CNSSP 15 (2016)
508///   Annex B ¶1 table, p. B-1.
509/// - TLS 1.3 and 1.2: RFC 9151 §5. The § references below are RFC 9151's.
510/// - TLS_AES_256_GCM_SHA384: §7.
511/// - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: §6.
512/// - secp384r1: §5.1, §7.
513/// - ecdsa_secp384r1_sha384: §6.2, §7.1. rsa_pss_rsae_sha384: §6.2, §7.1. rsa_pkcs1_sha384:
514///   §6.2; TLS 1.2 only, §7.1.
515/// - Keys ECDSA P-384 with SHA-384 (§5.1); RSA 3072 and 4096 (§5.2).
516/// - Not implemented by the stack: TLS_RSA_WITH_AES_256_GCM_SHA384 and
517///   TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 (§6), ffdhe3072 and ffdhe4096 (§5.3, §7).
518/// - MACs: hash "Use SHA-384", Annex B p. B-1; RFC 9151 §4 (SHA-384 for the TLS 1.2 PRF and the
519///   TLS 1.3 HKDF). `Options::cnsa1_unclassified_nss` adds HMAC-SHA256 under ¶2b (p. B-2).
520/// - KDFs: hash "Use SHA-384", Annex B p. B-1; RFC 9151 §4 (HKDF with SHA-384 in TLS 1.3).
521///   `Options::cnsa1_unclassified_nss` adds HKDF-SHA256 under ¶2b (p. B-2). No one-step KDF:
522///   RFC 9151 §4 reaches HKDF only.
523/// - Key agreement: ECDH on P-384, Annex B ¶1 table (p. B-1). `Options::cnsa1_unclassified_nss`
524///   does not widen it: ¶2b (p. B-2) requires NSA consultation for any other curve.
525/// - AEADs: AES-256-GCM. Annex B ¶1 table (p. B-1), AES "Use 256 bit keys"; RFC 9151 §4 (AES-256
526///   in GCM). `Options::cnsa1_unclassified_nss` does not widen it: ¶2b (p. B-2) names RSA, DH
527///   and SHA-256 only.
528/// - Hashes: SHA-384. Annex B ¶1 table (p. B-1), "Use SHA-384 to protect up to TOP SECRET".
529///   `Options::cnsa1_unclassified_nss` adds SHA-256 under ¶2b (p. B-2), "SHA-256 in the near
530///   term". SHA-512 and SHA3 excluded: the edition names neither (decision 0008).
531/// - Signatures: ECDSA P-384 with SHA-384, then RSA 4096 and 3072 with PSS, then with PKCS#1 v1.5.
532///   Annex B ¶1 table (p. B-1): ECDSA "Use Curve P-384", RSA "Minimum 3072 bit-modulus", both
533///   under FIPS 186-4, whose §5.5 (p. 24) approves both paddings. `Options::cnsa1_unclassified_nss`
534///   adds RSA 2048 with PSS and with PKCS#1 v1.5 under ¶2b (p. B-2), "RSA … at the 2048 bit level";
535///   not ECDSA P-256: ¶2b requires NSA consultation for curves other than P-384.
536pub const CNSA1: ProfileSpec = ProfileSpec {
537    versions: &[V::Tls13, V::Tls12],
538    cipher_suites: &[
539        C::Tls13Aes256GcmSha384,
540        C::Tls12EcdheEcdsaAes256GcmSha384,
541        C::Tls12EcdheRsaAes256GcmSha384,
542    ],
543    kx_groups: &[K::Secp384r1],
544    signature_schemes: &[T::EcdsaNistp384Sha384, T::RsaPssSha384, T::RsaPkcs1Sha384],
545    key_algorithms: &[S::EcdsaP384Sha384, S::Rsa3072Sha384, S::Rsa4096Sha384],
546    macs: &[MacAlgorithm::HmacSha384],
547    kdfs: &[KdfAlgorithm::HkdfSha384],
548    key_agreements: &[KeyAgreementAlgorithm::EcdhP384],
549    aeads: &[AeadAlgorithm::Aes256Gcm],
550    hashes: &[HashAlgorithm::Sha384],
551    signatures: &[
552        S::EcdsaP384Sha384,
553        S::Rsa4096Sha384Pss,
554        S::Rsa3072Sha384Pss,
555        S::Rsa4096Sha384,
556        S::Rsa3072Sha384,
557    ],
558};
559
560/// `bsi`: BSI TR-02102-2 (TLS) and TR-02102-1, version 2026-01.
561/// - TLS 1.3 and TLS 1.2: TR-02102-2 §3.2, Table 2.
562/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: TR-02102-2 §3.4.4, Table 13.
563/// - ECDHE-ECDSA and ECDHE-RSA with AES-128-GCM and AES-256-GCM: TR-02102-2 §3.3.1.1, Table 3.
564/// - secp384r1, secp256r1: TR-02102-2 Table 10 (TLS 1.3) and Table 6 (TLS 1.2).
565/// - ecdsa_secp384r1_sha384, ecdsa_secp256r1_sha256, ecdsa_secp521r1_sha512 and rsa_pss_rsae
566///   with SHA-384, 512, 256: TR-02102-2 §3.4.3, Table 11; for TLS 1.2, the Table 7 note.
567/// - rsa_pkcs1_* excluded: TR-02102-2 Table 7 and Table 12 notes.
568/// - Keys ECDSA P-384/SHA-384, P-256/SHA-256, P-521/SHA-512: TR-02102-2 Table 12 and Table 14.
569/// - RSA keys excluded: TR-02102-2 Table 12.
570/// - ML-DSA excluded: TR-02102-1 §5.3.4.
571/// - Hybrid groups excluded: TR-02102-2 Table 10 note.
572/// - MACs: HMAC with a Table 4.1 hash (TR-02102-1 §5.2, pp. 49-50; Table 4.1, p. 47).
573/// - KDFs: HKDF (RFC 5869) and the SP 800-56C two-step KDF, TR-02102-1 Table B.1 and §B.1.1
574///   (p. 73), with a Table 4.1 hash (p. 47). The one-step KDF is not among them.
575/// - No key agreement outside TLS: TR-02102-1 Table B.3 (p. 75) recommends only Brainpool curves,
576///   which no backend implements.
577/// - AEADs: AES-256-GCM, AES-128-GCM. TR-02102-1 Table 3.1 (p. 40), Table 3.2 (p. 42), §3.1.2
578///   (pp. 42-43). ChaCha20-Poly1305 excluded: §3.2 (p. 44), "no dedicated stream ciphers are
579///   recommended".
580/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. TR-02102-1 Table 4.1
581///   (p. 47), "Recommended hash functions".
582/// - Signatures: RSA 4096 and 3072 with PSS. TR-02102-1 Table 5.4 (p. 52), EMSA-PSS; §5.3.1
583///   (p. 52), a modulus of "at least 3000 bits". PKCS#1 v1.5 excluded: §1.5 (p. 22), "not
584///   recommended to use this format in new systems, neither for encryption nor for signature
585///   generation". ECDSA excluded: Table B.3 (p. 75) recommends only Brainpool curves, which no
586///   backend implements. ML-DSA excluded: §5.3.4 (p. 54), only "in combination with a classic
587///   signature scheme", which the crate does not offer. Ed25519 excluded: not in Table 5.3 (p. 51).
588pub const BSI: ProfileSpec = ProfileSpec {
589    versions: &[V::Tls13, V::Tls12],
590    cipher_suites: &[
591        C::Tls13Aes256GcmSha384,
592        C::Tls13Aes128GcmSha256,
593        C::Tls12EcdheEcdsaAes256GcmSha384,
594        C::Tls12EcdheRsaAes256GcmSha384,
595        C::Tls12EcdheEcdsaAes128GcmSha256,
596        C::Tls12EcdheRsaAes128GcmSha256,
597    ],
598    kx_groups: &[K::Secp384r1, K::Secp256r1],
599    signature_schemes: &[
600        T::EcdsaNistp384Sha384,
601        T::EcdsaNistp256Sha256,
602        T::EcdsaNistp521Sha512,
603        T::RsaPssSha384,
604        T::RsaPssSha512,
605        T::RsaPssSha256,
606    ],
607    key_algorithms: &[S::EcdsaP384Sha384, S::EcdsaP256Sha256, S::EcdsaP521Sha512],
608    macs: &[
609        MacAlgorithm::HmacSha512,
610        MacAlgorithm::HmacSha384,
611        MacAlgorithm::HmacSha256,
612    ],
613    kdfs: &[
614        KdfAlgorithm::HkdfSha512,
615        KdfAlgorithm::HkdfSha384,
616        KdfAlgorithm::HkdfSha256,
617    ],
618    key_agreements: &[],
619    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
620    hashes: &[
621        HashAlgorithm::Sha512,
622        HashAlgorithm::Sha384,
623        HashAlgorithm::Sha256,
624        HashAlgorithm::Sha3_512,
625        HashAlgorithm::Sha3_384,
626        HashAlgorithm::Sha3_256,
627    ],
628    signatures: &[S::Rsa4096Sha384Pss, S::Rsa3072Sha384Pss],
629};
630
631/// `fips1403`: NIST SP 800-140C r2 and SP 800-140D r2 with the CMVP lists (updated August 21,
632/// 2026), the FIPS 140-3 Implementation Guidance (August 19, 2026), SP 800-52r2 and SP 800-131A
633/// r2. An algorithm set, not a validation claim.
634/// - TLS 1.3 and 1.2: SP 800-52r2 §3.1 (p. 8), §4.1 (p. 33). TLS 1.2 extended master secret
635///   (RFC 7627): IG D.Q (pp. 220-221).
636/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: SP 800-52r2 §3.3.1.2 (p. 19); CMVP
637///   SP 800-140C list §6.2.2.1.
638/// - ECDHE-ECDSA and ECDHE-RSA with AES-128-GCM and AES-256-GCM: SP 800-52r2 §3.3.1.1.1-2
639///   (pp. 16-17).
640/// - ChaCha20-Poly1305 excluded: SP 800-140Cr2 §6.1; SP 800-52r2 §3.3.1.2.
641/// - ML-KEM-1024: CMVP SP 800-140D list §6.2.10; IG D.S Scenario 1 (p. 223).
642/// - SecP384r1MLKEM1024, X25519MLKEM768, SecP256r1MLKEM768: IG D.S Scenario 2 and Additional
643///   Comment 8a (pp. 223-226), the three IANA parameter sets it names.
644/// - secp384r1, secp256r1: SP 800-56A r3 Appendix D, Table 24 (p. 132); SP 800-52r2 §3.4.2.2
645///   (pp. 24-25).
646/// - X25519 on its own excluded: IG C.K Resolution 5 (p. 160).
647/// - ECDSA P-256, P-384, P-521 schemes and keys: SP 800-140C list §6.2.3.1; SP 800-186 Table 2
648///   (p. 7); SP 800-131A r2 Table 2 (p. 9).
649/// - ECDSA P-521 with SHA-384 key, in TLS only: it signs handshakes with ecdsa_secp521r1_sha512,
650///   and SHA-384 names only its certificate request's signature; FIPS 186-5 §6.1.1 (p. 21) and
651///   §6.4 (pp. 23-24) (decision 0015).
652/// - Ed25519 scheme and key: SP 800-140C list §6.2.3.1; IG C.A (p. 136); IG C.K Additional
653///   Comment 8 (p. 161).
654/// - RSA-PSS and RSA PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-5 §5.4; IG C.F (pp. 142-143);
655///   PKCS#1 v1.5 on TLS 1.2 only, RFC 8446 §4.2.3.
656/// - RSA 2048, 3072, 4096 keys: FIPS 186-5 §5.1; SP 800-131A r2 Table 2.
657/// - ML-DSA-87 scheme and key: SP 800-140C list §6.2.3.1 (FIPS 204).
658/// - MACs: FIPS 198-1 with SHA-2; SP 800-131A r2 Table 8 (p. 18) and Table 9 (p. 20).
659/// - KDFs: HKDF as the two-step method of SP 800-56C Rev. 2 §5 (pp. 17-20), and the hash-based
660///   one-step method of §4; SP 800-131A r2 §6 (p. 12).
661/// - Key agreement: ECDH on P-384, P-521 and P-256: SP 800-56A r3 Appendix D, Table 24 (p. 132);
662///   IG C.A (p. 135); IG D.F Scenario 2 (pp. 187-188). X25519 excluded: IG C.K Resolution 5
663///   (p. 160).
664/// - AEADs: AES-256-GCM, AES-128-GCM. CMVP SP 800-140C list §6.2.2.1 (FIPS 197-upd1, SP
665///   800-38D), through SP 800-140C r2 §6.2 (p. 2); SP 800-131A r2 Table 1 (p. 7).
666///   ChaCha20-Poly1305 excluded: SP 800-140C r2 §6.1 (p. 2) precludes every function not on the
667///   §6.2 list.
668/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. CMVP SP 800-140C list
669///   §6.2.4.1 (FIPS 180-4) and §6.2.4.2 (FIPS 202), through SP 800-140C r2 §6.2 (p. 2); SP
670///   800-131A r2 Table 8 (pp. 18-19); IG C.C Resolution 1 (p. 138), SHA-3 "as the standalone
671///   functions".
672/// - Signatures: ML-DSA-87; ECDSA on P-384, on P-521 with SHA-512, and on P-256; Ed25519; RSA 4096,
673///   3072 and 2048 with PSS, then with PKCS#1 v1.5. CMVP SP 800-140C list §6.2.3.1 (FIPS 186-5,
674///   FIPS 204), through SP 800-140C r2 §6.2 (p. 2); FIPS 186-5 §5.1 (p. 16) and §5.4 (p. 18),
675///   both paddings; SP 800-131A r2 Table 2 (p. 9). ECDSA P-521 with SHA-384 excluded: FIPS 186-5
676///   §6.1.1 (p. 21), "A hash function that provides a lower security strength than is associated
677///   with the bit length of n shall not be used".
678pub const FIPS1403: ProfileSpec = ProfileSpec {
679    versions: &[V::Tls13, V::Tls12],
680    cipher_suites: &[
681        C::Tls13Aes256GcmSha384,
682        C::Tls13Aes128GcmSha256,
683        C::Tls12EcdheEcdsaAes256GcmSha384,
684        C::Tls12EcdheRsaAes256GcmSha384,
685        C::Tls12EcdheEcdsaAes128GcmSha256,
686        C::Tls12EcdheRsaAes128GcmSha256,
687    ],
688    kx_groups: &[
689        K::MlKem1024,
690        K::Secp384r1MlKem1024,
691        K::X25519MlKem768,
692        K::Secp256r1MlKem768,
693        K::Secp384r1,
694        K::Secp256r1,
695    ],
696    signature_schemes: &[
697        T::MlDsa87,
698        T::EcdsaNistp384Sha384,
699        T::EcdsaNistp256Sha256,
700        T::EcdsaNistp521Sha512,
701        T::Ed25519,
702        T::RsaPssSha384,
703        T::RsaPssSha512,
704        T::RsaPssSha256,
705        T::RsaPkcs1Sha384,
706        T::RsaPkcs1Sha512,
707        T::RsaPkcs1Sha256,
708    ],
709    key_algorithms: &[
710        S::MlDsa87,
711        S::EcdsaP384Sha384,
712        S::EcdsaP256Sha256,
713        S::EcdsaP521Sha512,
714        S::EcdsaP521Sha384,
715        S::Ed25519,
716        S::Rsa3072Sha384,
717        S::Rsa4096Sha384,
718        S::Rsa2048Sha384,
719    ],
720    macs: &[
721        MacAlgorithm::HmacSha512,
722        MacAlgorithm::HmacSha384,
723        MacAlgorithm::HmacSha256,
724    ],
725    kdfs: &[
726        KdfAlgorithm::HkdfSha512,
727        KdfAlgorithm::HkdfSha384,
728        KdfAlgorithm::HkdfSha256,
729        KdfAlgorithm::OneStepSha512,
730        KdfAlgorithm::OneStepSha384,
731        KdfAlgorithm::OneStepSha256,
732    ],
733    key_agreements: &[
734        KeyAgreementAlgorithm::EcdhP384,
735        KeyAgreementAlgorithm::EcdhP521,
736        KeyAgreementAlgorithm::EcdhP256,
737    ],
738    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
739    hashes: &[
740        HashAlgorithm::Sha512,
741        HashAlgorithm::Sha384,
742        HashAlgorithm::Sha256,
743        HashAlgorithm::Sha3_512,
744        HashAlgorithm::Sha3_384,
745        HashAlgorithm::Sha3_256,
746    ],
747    signatures: &[
748        S::MlDsa87,
749        S::EcdsaP384Sha384,
750        S::EcdsaP521Sha512,
751        S::EcdsaP256Sha256,
752        S::Ed25519,
753        S::Rsa4096Sha384Pss,
754        S::Rsa3072Sha384Pss,
755        S::Rsa2048Sha384Pss,
756        S::Rsa4096Sha384,
757        S::Rsa3072Sha384,
758        S::Rsa2048Sha384,
759    ],
760};
761
762/// `fips1402`: FIPS 140-2 Annex A and Annex D (2021-10-12 editions), FIPS 186-4, SP 800-131A r2
763/// and SP 800-52r2. An algorithm set, not a validation claim.
764/// - TLS 1.3 and 1.2, and the AES-GCM suites: SP 800-52r2 §3.1, §3.3.1.1.1-2, §3.3.1.2; TLS 1.3
765///   key schedule, Annex D item 11 (RFC 8446 §7.1).
766/// - ChaCha20-Poly1305 excluded: Annex A.
767/// - secp384r1, secp256r1: Annex D item 3 (SP 800-56A r3), Table 24.
768/// - ML-KEM and the hybrids excluded: Annex A; Annex D items 12-13.
769/// - X25519 excluded: SP 800-56A r3 Table 24.
770/// - ECDSA P-256, P-384, P-521 schemes; RSA-PSS and PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-4
771///   (Annex A, p. 2), §5.4-5.5, Appendix D.1.2; SP 800-131A r2 Table 2 (p. 9).
772/// - Ed25519 and ML-DSA excluded: Annex A.
773/// - ECDSA keys P-384/SHA-384, P-256/SHA-256, P-521/SHA-512, P-521/SHA-384: FIPS 186-4 §6.4
774///   (pp. 29-30).
775/// - RSA 3072 and 2048 keys: FIPS 186-4 §5.1; RSA 4096: FIPS 140-2 IG A.14 (p. 221).
776/// - MACs: FIPS 140-2 Annex A, Message Authentication 3 (p. 3); SP 800-131A r2 Table 9 (p. 20).
777/// - KDFs: FIPS 140-2 Annex D (p. 1) items 12 and 13, SP 800-56C Rev. 1 and SP 800-56C (2011),
778///   each specifying extraction-then-expansion; the one-step method of SP 800-56C Rev. 1, which
779///   IG D.8 Scenario X1 (p. 241) admits for key agreement.
780/// - Key agreement: ECDH on P-384, P-521 and P-256: Annex D item 3 (SP 800-56A r3), Table 24;
781///   IG D.8 Scenario X1 (p. 241). X25519 excluded: not in Table 24.
782/// - AEADs: AES-256-GCM, AES-128-GCM. Annex A (pp. 1, 3): AES, SP 800-38D; IV rules, FIPS 140-2
783///   IG A.5. ChaCha20-Poly1305 excluded: Annex A.
784/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. Annex A (p. 2), FIPS 180-4
785///   and FIPS 202; FIPS 140-2 IG A.11 Resolution 1 (p. 216), SHA-3 "as the standalone functions".
786/// - Signatures: ECDSA on P-384, on P-521 with SHA-512 and with SHA-384, and on P-256; RSA 4096,
787///   3072 and 2048 with PSS, then with PKCS#1 v1.5. Annex A (p. 2), FIPS 186-4: §5.5 (p. 24), both
788///   paddings; §6.4 (p. 30), a hash weaker than the curve "ordinarily should not be used"; RSA
789///   4096, FIPS 140-2 IG A.14 (p. 221). Ed25519 and ML-DSA excluded: Annex A.
790pub const FIPS1402: ProfileSpec = ProfileSpec {
791    versions: &[V::Tls13, V::Tls12],
792    cipher_suites: &[
793        C::Tls13Aes256GcmSha384,
794        C::Tls13Aes128GcmSha256,
795        C::Tls12EcdheEcdsaAes256GcmSha384,
796        C::Tls12EcdheRsaAes256GcmSha384,
797        C::Tls12EcdheEcdsaAes128GcmSha256,
798        C::Tls12EcdheRsaAes128GcmSha256,
799    ],
800    kx_groups: &[K::Secp384r1, K::Secp256r1],
801    signature_schemes: &[
802        T::EcdsaNistp384Sha384,
803        T::EcdsaNistp256Sha256,
804        T::EcdsaNistp521Sha512,
805        T::RsaPssSha384,
806        T::RsaPssSha512,
807        T::RsaPssSha256,
808        T::RsaPkcs1Sha384,
809        T::RsaPkcs1Sha512,
810        T::RsaPkcs1Sha256,
811    ],
812    key_algorithms: &[
813        S::EcdsaP384Sha384,
814        S::EcdsaP256Sha256,
815        S::EcdsaP521Sha512,
816        S::EcdsaP521Sha384,
817        S::Rsa3072Sha384,
818        S::Rsa4096Sha384,
819        S::Rsa2048Sha384,
820    ],
821    macs: &[
822        MacAlgorithm::HmacSha512,
823        MacAlgorithm::HmacSha384,
824        MacAlgorithm::HmacSha256,
825    ],
826    kdfs: &[
827        KdfAlgorithm::HkdfSha512,
828        KdfAlgorithm::HkdfSha384,
829        KdfAlgorithm::HkdfSha256,
830        KdfAlgorithm::OneStepSha512,
831        KdfAlgorithm::OneStepSha384,
832        KdfAlgorithm::OneStepSha256,
833    ],
834    key_agreements: &[
835        KeyAgreementAlgorithm::EcdhP384,
836        KeyAgreementAlgorithm::EcdhP521,
837        KeyAgreementAlgorithm::EcdhP256,
838    ],
839    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
840    hashes: &[
841        HashAlgorithm::Sha512,
842        HashAlgorithm::Sha384,
843        HashAlgorithm::Sha256,
844        HashAlgorithm::Sha3_512,
845        HashAlgorithm::Sha3_384,
846        HashAlgorithm::Sha3_256,
847    ],
848    signatures: &[
849        S::EcdsaP384Sha384,
850        S::EcdsaP521Sha512,
851        S::EcdsaP521Sha384,
852        S::EcdsaP256Sha256,
853        S::Rsa4096Sha384Pss,
854        S::Rsa3072Sha384Pss,
855        S::Rsa2048Sha384Pss,
856        S::Rsa4096Sha384,
857        S::Rsa3072Sha384,
858        S::Rsa2048Sha384,
859    ],
860};
861
862/// A profile narrowed by its options, every entry implemented by the module's backend, in the
863/// profile's preference order.
864#[derive(Debug, Clone, PartialEq, Eq)]
865#[non_exhaustive]
866pub struct Selection {
867    /// Protocol versions.
868    pub versions: Vec<V>,
869    /// Cipher suites, each of a version in `versions`.
870    pub cipher_suites: Vec<C>,
871    /// Key-exchange groups.
872    pub kx_groups: Vec<K>,
873    /// Handshake signature schemes.
874    pub signature_schemes: Vec<T>,
875    /// Key algorithms.
876    pub key_algorithms: Vec<S>,
877    /// Entries unavailable on the module line after Options, in family and preference order.
878    pub excluded: Vec<Algorithm>,
879}
880
881const fn is_tls12_only(t: T) -> bool {
882    match t {
883        T::RsaPkcs1Sha256 | T::RsaPkcs1Sha384 | T::RsaPkcs1Sha512 => true,
884        T::EcdsaNistp256Sha256
885        | T::EcdsaNistp384Sha384
886        | T::EcdsaNistp521Sha512
887        | T::Ed25519
888        | T::RsaPssSha256
889        | T::RsaPssSha384
890        | T::RsaPssSha512
891        | T::MlDsa87 => false,
892    }
893}
894
895fn keep<X: Copy>(items: &[X], mut wanted: impl FnMut(X) -> bool) -> Vec<X> {
896    items.iter().copied().filter(|x| wanted(*x)).collect()
897}
898
899/// The backend-independent half of a backend's `resolve`: `profile`'s lists, narrowed by
900/// `options`; `pq_key_exchange_required` also removes TLS 1.2, its suites and the RSA PKCS#1 v1.5
901/// handshake schemes. Records entries the module line lacks in [`Selection::excluded`], then
902/// refuses a family left empty with [`CryptoError::EmptyProfile`]. Both use versions, suites,
903/// groups, schemes and keys order, preserving preference order. Approval is a separate check.
904pub fn select(
905    profile: Profile,
906    options: Options,
907    module: Module,
908) -> Result<Selection, CryptoError> {
909    select_spec(profile, profile.spec(), options, module)
910}
911
912fn select_spec(
913    profile: Profile,
914    spec: &ProfileSpec,
915    options: Options,
916    module: Module,
917) -> Result<Selection, CryptoError> {
918    let pq = options.pq_key_exchange_required;
919    let versions = keep(spec.versions, |v| !(pq && v == V::Tls12));
920    let cipher_suites = keep(spec.cipher_suites, |c| versions.contains(&c.version()));
921    let kx_groups = keep(spec.kx_groups, |k| !pq || k.is_post_quantum());
922    let signature_schemes = keep(spec.signature_schemes, |t| !(pq && is_tls12_only(t)));
923    let key_algorithms = spec.key_algorithms.to_vec();
924    let mut excluded = Vec::new();
925    let mut available = |a| {
926        let on = capability(module, a).available;
927        if !on {
928            excluded.push(a);
929        }
930        on
931    };
932    let versions = keep(&versions, |v| available(Algorithm::Protocol(v)));
933    let cipher_suites = keep(&cipher_suites, |c| available(Algorithm::Cipher(c)));
934    let kx_groups = keep(&kx_groups, |k| available(Algorithm::Kx(k)));
935    let signature_schemes = keep(&signature_schemes, |t| available(Algorithm::Scheme(t)));
936    let key_algorithms = keep(&key_algorithms, |s| available(Algorithm::Signature(s)));
937    for (empty, category) in [
938        (versions.is_empty(), Family::Version),
939        (cipher_suites.is_empty(), Family::CipherSuite),
940        (kx_groups.is_empty(), Family::KxGroup),
941        (signature_schemes.is_empty(), Family::SignatureScheme),
942        (key_algorithms.is_empty(), Family::KeyAlgorithm),
943    ] {
944        if empty {
945            return Err(CryptoError::EmptyProfile {
946                profile,
947                options,
948                category,
949            });
950        }
951    }
952    Ok(Selection {
953        versions,
954        cipher_suites,
955        kx_groups,
956        signature_schemes,
957        key_algorithms,
958        excluded,
959    })
960}
961
962/// A profile's non-TLS families under its options, every entry implemented by the module's
963/// backend, in preference order. Never refused: an empty family means every constructor of that
964/// family refuses under the profile.
965#[derive(Debug, Clone, PartialEq, Eq)]
966#[non_exhaustive]
967pub struct AdmittedSelection {
968    /// Message authentication codes.
969    pub macs: Vec<MacAlgorithm>,
970    /// Key-derivation functions.
971    pub kdfs: Vec<KdfAlgorithm>,
972    /// Key agreement.
973    pub key_agreements: Vec<KeyAgreementAlgorithm>,
974    /// Authenticated encryption.
975    pub aeads: Vec<AeadAlgorithm>,
976    /// Hash functions.
977    pub hashes: Vec<HashAlgorithm>,
978    /// Signature algorithms.
979    pub signatures: Vec<S>,
980    /// Entries unavailable on the module line, in family and preference order.
981    pub excluded: Vec<Algorithm>,
982}
983
984/// The backend-independent half of a backend's `admit`: `profile`'s non-TLS families, under
985/// `options`, less what `module` lacks. Independent of [`select`].
986pub fn select_admitted(profile: Profile, options: Options, module: Module) -> AdmittedSelection {
987    select_admitted_with(profile, profile.spec(), options, |a| {
988        capability(module, a).available
989    })
990}
991
992fn select_admitted_with(
993    profile: Profile,
994    spec: &ProfileSpec,
995    options: Options,
996    available: impl Fn(Algorithm) -> bool,
997) -> AdmittedSelection {
998    let mut macs = spec.macs.to_vec();
999    let mut kdfs = spec.kdfs.to_vec();
1000    let mut hashes = spec.hashes.to_vec();
1001    let mut signatures = spec.signatures.to_vec();
1002    if options.cnsa1_unclassified_nss {
1003        macs.extend_from_slice(unclassified_nss_macs(profile));
1004        kdfs.extend_from_slice(unclassified_nss_kdfs(profile));
1005        hashes.extend_from_slice(unclassified_nss_hashes(profile));
1006        signatures.extend_from_slice(unclassified_nss_signatures(profile));
1007    }
1008    let mut key_agreements = spec.key_agreements.to_vec();
1009    let mut aeads = spec.aeads.to_vec();
1010    let mut excluded = Vec::new();
1011    retain_available(&mut macs, Algorithm::Mac, &available, &mut excluded);
1012    retain_available(&mut kdfs, Algorithm::Kdf, &available, &mut excluded);
1013    retain_available(
1014        &mut key_agreements,
1015        Algorithm::KeyAgreement,
1016        &available,
1017        &mut excluded,
1018    );
1019    retain_available(&mut aeads, Algorithm::Aead, &available, &mut excluded);
1020    retain_available(&mut hashes, Algorithm::Hash, &available, &mut excluded);
1021    retain_available(
1022        &mut signatures,
1023        Algorithm::Signature,
1024        &available,
1025        &mut excluded,
1026    );
1027    AdmittedSelection {
1028        macs,
1029        kdfs,
1030        key_agreements,
1031        aeads,
1032        hashes,
1033        signatures,
1034        excluded,
1035    }
1036}
1037
1038fn retain_available<X: Copy>(
1039    xs: &mut Vec<X>,
1040    tag: fn(X) -> Algorithm,
1041    available: &impl Fn(Algorithm) -> bool,
1042    excluded: &mut Vec<Algorithm>,
1043) {
1044    xs.retain(|x| {
1045        let on = available(tag(*x));
1046        if !on {
1047            excluded.push(tag(*x));
1048        }
1049        on
1050    });
1051}
1052
1053fn unclassified_nss_macs(p: Profile) -> &'static [MacAlgorithm] {
1054    match p {
1055        Profile::Cnsa1 => &[MacAlgorithm::HmacSha256],
1056        Profile::Unrestricted
1057        | Profile::Preferred
1058        | Profile::Cnsa2
1059        | Profile::Bsi
1060        | Profile::Fips1403
1061        | Profile::Fips1402 => &[],
1062    }
1063}
1064
1065fn unclassified_nss_kdfs(p: Profile) -> &'static [KdfAlgorithm] {
1066    match p {
1067        Profile::Cnsa1 => &[KdfAlgorithm::HkdfSha256],
1068        Profile::Unrestricted
1069        | Profile::Preferred
1070        | Profile::Cnsa2
1071        | Profile::Bsi
1072        | Profile::Fips1403
1073        | Profile::Fips1402 => &[],
1074    }
1075}
1076
1077fn unclassified_nss_hashes(p: Profile) -> &'static [HashAlgorithm] {
1078    match p {
1079        Profile::Cnsa1 => &[HashAlgorithm::Sha256],
1080        Profile::Unrestricted
1081        | Profile::Preferred
1082        | Profile::Cnsa2
1083        | Profile::Bsi
1084        | Profile::Fips1403
1085        | Profile::Fips1402 => &[],
1086    }
1087}
1088
1089fn unclassified_nss_signatures(p: Profile) -> &'static [S] {
1090    match p {
1091        Profile::Cnsa1 => &[S::Rsa2048Sha384Pss, S::Rsa2048Sha384],
1092        Profile::Unrestricted
1093        | Profile::Preferred
1094        | Profile::Cnsa2
1095        | Profile::Bsi
1096        | Profile::Fips1403
1097        | Profile::Fips1402 => &[],
1098    }
1099}
1100
1101/// Whether `profile` admits an empty HKDF `info`: `unrestricted` only, which makes no approval
1102/// claim. The module does not mark an HKDF call with an empty `info` approved (AWS-LC
1103/// `HKDF_verify_service_indicator`), so every other profile refuses one (decision 0010).
1104pub const fn kdf_info_may_be_empty(profile: Profile) -> bool {
1105    match profile {
1106        Profile::Unrestricted => true,
1107        Profile::Preferred
1108        | Profile::Cnsa2
1109        | Profile::Cnsa1
1110        | Profile::Bsi
1111        | Profile::Fips1403
1112        | Profile::Fips1402 => false,
1113    }
1114}
1115
1116/// The shortest HMAC key, in bits, `profile` admits. 112 is the module's approved minimum: SP
1117/// 800-131A r2 §10 Table 9 (p. 20) disallows shorter keys for MAC generation, and the AWS-LC FIPS
1118/// 3.1.0 security policies list "Key Length: 112-524288" (Table 5, p. 9). `bsi` takes the 128 of
1119/// TR-02102-1 Table 5.2 (p. 50). SP 800-107 r1 §5.3.2 (pp. 13-14): a key "shall have a security
1120/// strength that meets or exceeds the security strength required to protect the data".
1121pub const fn mac_key_floor_bits(profile: Profile) -> usize {
1122    match profile {
1123        Profile::Bsi => 128,
1124        Profile::Unrestricted
1125        | Profile::Preferred
1126        | Profile::Cnsa2
1127        | Profile::Cnsa1
1128        | Profile::Fips1403
1129        | Profile::Fips1402 => 112,
1130    }
1131}
1132
1133/// The longest HMAC key, in bits, the module was tested with: "Key Length: 112-524288" in the
1134/// AWS-LC FIPS 3.1.0 security policies (Table 5, p. 9) and on CAVP certificate A7855.
1135pub const MAC_KEY_MAX_BITS: usize = 524_288;
1136
1137#[cfg(test)]
1138mod tests {
1139    use super::*;
1140    use crate::algorithm::*;
1141    use crate::capability::{Linkage, Module, ModuleLine};
1142    use crate::error::{CryptoError, Family};
1143
1144    #[test]
1145    fn every_profile_lists_its_schemes_in_the_canonical_order() {
1146        assert_eq!(
1147            Profile::Unrestricted.spec().signature_schemes.len(),
1148            TlsSignatureScheme::ALL.len()
1149        );
1150        for &p in Profile::ALL {
1151            let s = p.spec().signature_schemes;
1152            assert!(
1153                s.windows(2).all(|w| w[0].preference() < w[1].preference()),
1154                "{p}"
1155            );
1156        }
1157    }
1158
1159    const M3S: Module = Module {
1160        line: ModuleLine::AwsLcFips3,
1161        linkage: Linkage::Static,
1162    };
1163    const M4S: Module = Module {
1164        line: ModuleLine::AwsLcFips4,
1165        linkage: Linkage::Static,
1166    };
1167    const PQ: Options = Options {
1168        peer_certificates_match_profile: false,
1169        pq_key_exchange_required: true,
1170        cnsa1_unclassified_nss: false,
1171    };
1172    const PKCS1: [TlsSignatureScheme; 3] = [
1173        TlsSignatureScheme::RsaPkcs1Sha256,
1174        TlsSignatureScheme::RsaPkcs1Sha384,
1175        TlsSignatureScheme::RsaPkcs1Sha512,
1176    ];
1177
1178    fn no_duplicates<X: PartialEq + std::fmt::Debug>(xs: &[X]) {
1179        for (i, x) in xs.iter().enumerate() {
1180            assert!(!xs[i + 1..].contains(x), "{x:?} twice");
1181        }
1182    }
1183
1184    #[test]
1185    fn every_spec_is_well_formed() {
1186        for &p in Profile::ALL {
1187            let s = p.spec();
1188            no_duplicates(s.versions);
1189            no_duplicates(s.cipher_suites);
1190            no_duplicates(s.kx_groups);
1191            no_duplicates(s.signature_schemes);
1192            no_duplicates(s.key_algorithms);
1193            no_duplicates(s.macs);
1194            no_duplicates(s.kdfs);
1195            no_duplicates(s.key_agreements);
1196            no_duplicates(s.aeads);
1197            no_duplicates(s.hashes);
1198            no_duplicates(s.signatures);
1199            assert!(!s.versions.is_empty() && !s.kx_groups.is_empty(), "{p}");
1200            assert!(
1201                !s.signature_schemes.is_empty() && !s.key_algorithms.is_empty(),
1202                "{p}"
1203            );
1204            assert!(
1205                s.cipher_suites
1206                    .iter()
1207                    .all(|c| s.versions.contains(&c.version())),
1208                "{p}"
1209            );
1210            let has_pkcs1 = s.signature_schemes.iter().any(|t| PKCS1.contains(t));
1211            assert!(!has_pkcs1 || s.versions.contains(&TlsVersion::Tls12), "{p}");
1212        }
1213    }
1214
1215    #[test]
1216    fn preferred_excludes_what_its_definition_names() {
1217        let s = Profile::Preferred.spec();
1218        assert_eq!(s.versions, &[TlsVersion::Tls13]);
1219        assert!(!s.kx_groups.contains(&KxGroup::X25519));
1220        assert!(
1221            !s.cipher_suites
1222                .contains(&CipherSuite::Tls13Chacha20Poly1305Sha256)
1223        );
1224        assert!(!s.signature_schemes.contains(&TlsSignatureScheme::Ed25519));
1225        assert!(!s.key_algorithms.contains(&SignatureAlgorithm::Ed25519));
1226        assert!(!s.aeads.contains(&AeadAlgorithm::Chacha20Poly1305));
1227        for h in [
1228            HashAlgorithm::Sha3_256,
1229            HashAlgorithm::Sha3_384,
1230            HashAlgorithm::Sha3_512,
1231        ] {
1232            assert!(!s.hashes.contains(&h), "{h:?}");
1233        }
1234        for a in [
1235            SignatureAlgorithm::Ed25519,
1236            SignatureAlgorithm::EcdsaP521Sha384,
1237            SignatureAlgorithm::Rsa2048Sha384,
1238            SignatureAlgorithm::Rsa2048Sha384Pss,
1239        ] {
1240            assert!(!s.signatures.contains(&a), "{a}");
1241        }
1242    }
1243
1244    #[test]
1245    fn no_tls_key_list_holds_an_rsa_pss_variant() {
1246        for &p in Profile::ALL {
1247            for &k in p.spec().key_algorithms {
1248                assert!(k.tls_scheme().is_some(), "{p} {k}");
1249            }
1250        }
1251        for a in [
1252            S::Rsa2048Sha384Pss,
1253            S::Rsa3072Sha384Pss,
1254            S::Rsa4096Sha384Pss,
1255        ] {
1256            assert_eq!(a.tls_scheme(), None, "{a}");
1257            assert!(Profile::Unrestricted.spec().signatures.contains(&a), "{a}");
1258        }
1259    }
1260
1261    #[test]
1262    fn p521_with_sha384_signs_outside_tls_only_where_fips_186_5_does_not_govern() {
1263        for &p in Profile::ALL {
1264            let outside = p.spec().signatures.contains(&S::EcdsaP521Sha384);
1265            let want = match p {
1266                Profile::Unrestricted | Profile::Fips1402 => true,
1267                Profile::Preferred
1268                | Profile::Cnsa2
1269                | Profile::Cnsa1
1270                | Profile::Bsi
1271                | Profile::Fips1403 => false,
1272            };
1273            assert_eq!(outside, want, "{p}");
1274        }
1275        for p in [Profile::Preferred, Profile::Fips1403] {
1276            assert!(p.spec().key_algorithms.contains(&S::EcdsaP521Sha384), "{p}");
1277        }
1278    }
1279
1280    #[test]
1281    fn bsi_signs_with_rsa_pss_only() {
1282        assert_eq!(
1283            Profile::Bsi.spec().signatures,
1284            &[S::Rsa4096Sha384Pss, S::Rsa3072Sha384Pss]
1285        );
1286    }
1287
1288    #[test]
1289    fn bsi_admits_only_ecdsa_keys() {
1290        assert_eq!(
1291            Profile::Bsi.spec().key_algorithms,
1292            &[
1293                SignatureAlgorithm::EcdsaP384Sha384,
1294                SignatureAlgorithm::EcdsaP256Sha256,
1295                SignatureAlgorithm::EcdsaP521Sha512,
1296            ]
1297        );
1298    }
1299
1300    #[test]
1301    fn select_records_line_exclusions_after_options() {
1302        for module in [
1303            M3S,
1304            Module {
1305                linkage: Linkage::Dynamic,
1306                ..M3S
1307            },
1308        ] {
1309            for p in [Profile::Unrestricted, Profile::Preferred, Profile::Fips1403] {
1310                for options in [Options::default(), PQ] {
1311                    let s = select(p, options, module).unwrap();
1312                    assert_eq!(
1313                        s.excluded,
1314                        vec![
1315                            Algorithm::Scheme(T::MlDsa87),
1316                            Algorithm::Signature(S::MlDsa87),
1317                        ]
1318                    );
1319                    no_duplicates(&s.excluded);
1320                    assert!(!s.signature_schemes.contains(&T::MlDsa87));
1321                    assert!(!s.key_algorithms.contains(&S::MlDsa87));
1322                }
1323            }
1324            for options in [Options::default(), PQ] {
1325                assert_eq!(
1326                    select(Profile::Cnsa2, options, module),
1327                    Err(CryptoError::EmptyProfile {
1328                        profile: Profile::Cnsa2,
1329                        options,
1330                        category: Family::SignatureScheme,
1331                    })
1332                );
1333            }
1334            for p in [Profile::Cnsa1, Profile::Bsi, Profile::Fips1402] {
1335                assert!(
1336                    select(p, Options::default(), module)
1337                        .unwrap()
1338                        .excluded
1339                        .is_empty()
1340                );
1341            }
1342        }
1343    }
1344
1345    #[test]
1346    fn select_checks_keys_and_options_precede_line_emptiness() {
1347        const KEY_ONLY: ProfileSpec = ProfileSpec {
1348            signature_schemes: &[T::EcdsaNistp384Sha384],
1349            ..CNSA2
1350        };
1351        assert_eq!(
1352            select_spec(Profile::Cnsa2, &KEY_ONLY, Options::default(), M3S),
1353            Err(CryptoError::EmptyProfile {
1354                profile: Profile::Cnsa2,
1355                options: Options::default(),
1356                category: Family::KeyAlgorithm,
1357            })
1358        );
1359        const BSI_WITH_ML_DSA: ProfileSpec = ProfileSpec {
1360            signature_schemes: &[T::MlDsa87],
1361            ..BSI
1362        };
1363        for spec in [&BSI_WITH_ML_DSA, &BSI] {
1364            assert_eq!(
1365                select_spec(Profile::Bsi, spec, PQ, M3S),
1366                Err(CryptoError::EmptyProfile {
1367                    profile: Profile::Bsi,
1368                    options: PQ,
1369                    category: Family::KxGroup,
1370                })
1371            );
1372        }
1373    }
1374
1375    #[test]
1376    fn select_refuses_each_empty_family_in_order() {
1377        const EMPTY: ProfileSpec = ProfileSpec {
1378            versions: &[],
1379            cipher_suites: &[],
1380            kx_groups: &[],
1381            signature_schemes: &[],
1382            key_algorithms: &[],
1383            macs: &[],
1384            kdfs: &[],
1385            key_agreements: &[],
1386            aeads: &[],
1387            hashes: &[],
1388            signatures: &[],
1389        };
1390        let empty = |category| {
1391            Err(CryptoError::EmptyProfile {
1392                profile: Profile::Bsi,
1393                options: Options::default(),
1394                category,
1395            })
1396        };
1397        let cases: [(ProfileSpec, Family); 5] = [
1398            (EMPTY, Family::Version),
1399            (
1400                ProfileSpec {
1401                    versions: BSI.versions,
1402                    ..EMPTY
1403                },
1404                Family::CipherSuite,
1405            ),
1406            (
1407                ProfileSpec {
1408                    kx_groups: &[],
1409                    signature_schemes: &[],
1410                    key_algorithms: &[],
1411                    ..BSI
1412                },
1413                Family::KxGroup,
1414            ),
1415            (
1416                ProfileSpec {
1417                    signature_schemes: &[],
1418                    key_algorithms: &[],
1419                    ..BSI
1420                },
1421                Family::SignatureScheme,
1422            ),
1423            (
1424                ProfileSpec {
1425                    key_algorithms: &[],
1426                    ..BSI
1427                },
1428                Family::KeyAlgorithm,
1429            ),
1430        ];
1431        for (spec, category) in cases {
1432            assert_eq!(
1433                select_spec(Profile::Bsi, &spec, Options::default(), M4S),
1434                empty(category),
1435                "{category:?}"
1436            );
1437        }
1438    }
1439
1440    #[test]
1441    fn select_without_options_is_the_spec_on_a_module_that_has_everything() {
1442        for &p in Profile::ALL {
1443            let s = select(p, Options::default(), M4S).unwrap();
1444            let spec = p.spec();
1445            assert!(s.excluded.is_empty());
1446            assert_eq!(s.versions, spec.versions.to_vec(), "{p}");
1447            assert_eq!(s.cipher_suites, spec.cipher_suites.to_vec(), "{p}");
1448            assert_eq!(s.kx_groups, spec.kx_groups.to_vec(), "{p}");
1449            assert_eq!(s.signature_schemes, spec.signature_schemes.to_vec(), "{p}");
1450            assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
1451        }
1452    }
1453
1454    #[test]
1455    fn the_option_keeps_post_quantum_groups_tls13_and_no_pkcs1_schemes() {
1456        for &p in Profile::ALL {
1457            let refused = match p {
1458                Profile::Unrestricted | Profile::Preferred | Profile::Cnsa2 | Profile::Fips1403 => {
1459                    false
1460                }
1461                Profile::Cnsa1 | Profile::Bsi | Profile::Fips1402 => true,
1462            };
1463            match select(p, PQ, M4S) {
1464                Ok(s) => {
1465                    assert!(!refused, "{p}");
1466                    let spec = p.spec();
1467                    assert_eq!(s.versions, vec![TlsVersion::Tls13], "{p}");
1468                    let suites: Vec<_> = spec
1469                        .cipher_suites
1470                        .iter()
1471                        .copied()
1472                        .filter(|c| c.version() == TlsVersion::Tls13)
1473                        .collect();
1474                    assert_eq!(s.cipher_suites, suites, "{p}");
1475                    let groups: Vec<_> = spec
1476                        .kx_groups
1477                        .iter()
1478                        .copied()
1479                        .filter(|k| k.is_post_quantum())
1480                        .collect();
1481                    assert_eq!(s.kx_groups, groups, "{p}");
1482                    let schemes: Vec<_> = spec
1483                        .signature_schemes
1484                        .iter()
1485                        .copied()
1486                        .filter(|t| !PKCS1.contains(t))
1487                        .collect();
1488                    assert_eq!(s.signature_schemes, schemes, "{p}");
1489                    assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
1490                }
1491                Err(e) => {
1492                    assert!(refused, "{p}");
1493                    assert_eq!(
1494                        e,
1495                        CryptoError::EmptyProfile {
1496                            profile: p,
1497                            options: PQ,
1498                            category: Family::KxGroup
1499                        }
1500                    );
1501                }
1502            }
1503        }
1504    }
1505
1506    #[test]
1507    fn the_option_removes_every_pkcs1_scheme_from_unrestricted_and_fips1403() {
1508        use TlsSignatureScheme as T;
1509        let expected = vec![
1510            T::MlDsa87,
1511            T::EcdsaNistp384Sha384,
1512            T::EcdsaNistp256Sha256,
1513            T::EcdsaNistp521Sha512,
1514            T::Ed25519,
1515            T::RsaPssSha384,
1516            T::RsaPssSha512,
1517            T::RsaPssSha256,
1518        ];
1519        for p in [Profile::Unrestricted, Profile::Fips1403] {
1520            assert_eq!(
1521                select(p, PQ, M4S).unwrap().signature_schemes,
1522                expected,
1523                "{p}"
1524            );
1525            assert_eq!(
1526                select(p, Options::default(), M4S)
1527                    .unwrap()
1528                    .signature_schemes
1529                    .len(),
1530                expected.len() + 3,
1531                "{p}"
1532            );
1533        }
1534    }
1535
1536    #[test]
1537    fn profile_names_are_stable_and_exact() {
1538        assert_eq!(
1539            Profile::ALL
1540                .iter()
1541                .copied()
1542                .map(Profile::as_str)
1543                .collect::<Vec<_>>(),
1544            [
1545                "unrestricted",
1546                "preferred",
1547                "cnsa2",
1548                "cnsa1",
1549                "bsi",
1550                "fips1403",
1551                "fips1402"
1552            ]
1553        );
1554        for &p in Profile::ALL {
1555            assert_eq!(p.as_str().parse::<Profile>(), Ok(p));
1556            assert_eq!(p.to_string(), p.as_str());
1557        }
1558        for bad in [
1559            "",
1560            "CNSA2",
1561            "Cnsa2",
1562            "FIPS1403",
1563            "fips140-3",
1564            "fips-140-3",
1565            " fips1403",
1566            "fips1403 ",
1567            "fips1403\n",
1568            " bsi",
1569            "cnsa",
1570            "bsi2026",
1571            "preferred,",
1572            "default",
1573        ] {
1574            assert_eq!(
1575                bad.parse::<Profile>(),
1576                Err(ParseProfileError(bad.to_string())),
1577                "{bad:?}"
1578            );
1579        }
1580        assert_eq!(
1581            ParseProfileError("FIPS1403".to_string()).to_string(),
1582            "unknown profile \"FIPS1403\""
1583        );
1584    }
1585
1586    #[test]
1587    fn profile_all_lists_every_variant_in_ordinal_order() {
1588        assert_eq!(
1589            Profile::ALL
1590                .iter()
1591                .copied()
1592                .map(Profile::ordinal)
1593                .collect::<Vec<_>>(),
1594            std::array::from_fn::<usize, 7, _>(|i| i)
1595        );
1596    }
1597
1598    #[test]
1599    fn governing_texts_are_the_published_editions() {
1600        use GoverningText::{CrateRecommendation as Own, Published};
1601        let table: [(Profile, GoverningText); 7] = [
1602            (Profile::Unrestricted, Own),
1603            (Profile::Preferred, Own),
1604            (
1605                Profile::Cnsa2,
1606                Published {
1607                    documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
1608                },
1609            ),
1610            (
1611                Profile::Cnsa1,
1612                Published {
1613                    documents: &[
1614                        "CNSSP 15 (20 October 2016), Annex B, p. B-1",
1615                        "RFC 9151 (April 2022)",
1616                    ],
1617                },
1618            ),
1619            (
1620                Profile::Bsi,
1621                Published {
1622                    documents: &[
1623                        "BSI TR-02102-2, version 2026-01",
1624                        "BSI TR-02102-1, version 2026-01",
1625                    ],
1626                },
1627            ),
1628            (
1629                Profile::Fips1403,
1630                Published {
1631                    documents: &[
1632                        "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
1633                        "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
1634                        "NIST SP 800-52r2",
1635                        "NIST SP 800-131A r2",
1636                    ],
1637                },
1638            ),
1639            (
1640                Profile::Fips1402,
1641                Published {
1642                    documents: &[
1643                        "FIPS 140-2 Annex A and Annex D (2021)",
1644                        "FIPS 186-4",
1645                        "NIST SP 800-131A r2",
1646                        "NIST SP 800-52r2",
1647                    ],
1648                },
1649            ),
1650        ];
1651        for (p, text) in table {
1652            assert_eq!(p.governing_text(), text, "{p}");
1653        }
1654    }
1655
1656    #[test]
1657    fn options_default_narrows_nothing_and_display_names_the_option() {
1658        assert_eq!(
1659            Options::default(),
1660            Options {
1661                peer_certificates_match_profile: false,
1662                pq_key_exchange_required: false,
1663                cnsa1_unclassified_nss: false,
1664            }
1665        );
1666        assert_eq!(Options::default().to_string(), "no options");
1667        assert_eq!(
1668            Options {
1669                peer_certificates_match_profile: false,
1670                pq_key_exchange_required: true,
1671                cnsa1_unclassified_nss: false,
1672            }
1673            .to_string(),
1674            "post-quantum key exchange required"
1675        );
1676    }
1677    #[test]
1678    fn peer_certificate_options_combinations_preserve_selection() {
1679        for (pq, peer, display) in [
1680            (false, false, "no options"),
1681            (true, false, "post-quantum key exchange required"),
1682            (false, true, "peer certificates match profile"),
1683            (
1684                true,
1685                true,
1686                "post-quantum key exchange required, peer certificates match profile",
1687            ),
1688        ] {
1689            let options = Options {
1690                pq_key_exchange_required: pq,
1691                peer_certificates_match_profile: peer,
1692                cnsa1_unclassified_nss: false,
1693            };
1694            assert_eq!(options.to_string(), display);
1695            for &profile in Profile::ALL {
1696                for module in [M3S, M4S] {
1697                    let baseline = Options {
1698                        peer_certificates_match_profile: false,
1699                        ..options
1700                    };
1701                    let actual = select(profile, options, module);
1702                    let expected = select(profile, baseline, module);
1703                    match (actual, expected) {
1704                        (Ok(actual), Ok(expected)) => assert_eq!(actual, expected),
1705                        (
1706                            Err(CryptoError::EmptyProfile {
1707                                profile: p,
1708                                options: o,
1709                                category: f,
1710                            }),
1711                            Err(CryptoError::EmptyProfile {
1712                                profile: ep,
1713                                options: eo,
1714                                category: ef,
1715                            }),
1716                        ) => {
1717                            assert_eq!((p, o, f), (ep, options, ef));
1718                            assert_eq!(eo, baseline);
1719                        }
1720                        (actual, expected) => panic!("{actual:?} != {expected:?}"),
1721                    }
1722                }
1723            }
1724        }
1725    }
1726
1727    #[test]
1728    fn cnsa1_unclassified_nss_names_itself_and_leaves_tls_selection_alone() {
1729        let on = Options {
1730            cnsa1_unclassified_nss: true,
1731            ..Options::default()
1732        };
1733        assert_eq!(on.to_string(), "cnsa1 UNCLASSIFIED NSS");
1734        assert_eq!(
1735            Options {
1736                pq_key_exchange_required: true,
1737                peer_certificates_match_profile: true,
1738                cnsa1_unclassified_nss: true,
1739            }
1740            .to_string(),
1741            "post-quantum key exchange required, peer certificates match profile, cnsa1 UNCLASSIFIED NSS"
1742        );
1743        for &profile in Profile::ALL {
1744            for module in [M3S, M4S] {
1745                for pq in [false, true] {
1746                    let off = Options {
1747                        pq_key_exchange_required: pq,
1748                        ..Options::default()
1749                    };
1750                    let on = Options {
1751                        cnsa1_unclassified_nss: true,
1752                        ..off
1753                    };
1754                    match (select(profile, on, module), select(profile, off, module)) {
1755                        (Ok(a), Ok(b)) => assert_eq!(a, b),
1756                        (
1757                            Err(CryptoError::EmptyProfile {
1758                                profile: p,
1759                                options: o,
1760                                category: f,
1761                            }),
1762                            Err(CryptoError::EmptyProfile {
1763                                profile: ep,
1764                                options: eo,
1765                                category: ef,
1766                            }),
1767                        ) => {
1768                            assert_eq!((p, f), (ep, ef));
1769                            assert_eq!((o, eo), (on, off));
1770                        }
1771                        (a, b) => panic!("{a:?} != {b:?}"),
1772                    }
1773                }
1774            }
1775        }
1776    }
1777
1778    #[test]
1779    fn select_admitted_never_refuses_an_empty_family() {
1780        let spec = ProfileSpec {
1781            macs: &[],
1782            kdfs: &[],
1783            aeads: &[],
1784            hashes: &[],
1785            signatures: &[],
1786            ..BSI
1787        };
1788        assert_eq!(
1789            select_admitted_with(Profile::Bsi, &spec, Options::default(), |_| true),
1790            AdmittedSelection {
1791                macs: vec![],
1792                kdfs: vec![],
1793                key_agreements: vec![],
1794                aeads: vec![],
1795                hashes: vec![],
1796                signatures: vec![],
1797                excluded: vec![],
1798            }
1799        );
1800    }
1801
1802    #[test]
1803    fn select_admitted_records_what_the_module_lacks() {
1804        use AeadAlgorithm as D;
1805        use HashAlgorithm as H;
1806        use KdfAlgorithm as K;
1807        use KeyAgreementAlgorithm as E;
1808        use MacAlgorithm as M;
1809        let lacking = [
1810            Algorithm::Mac(M::HmacSha384),
1811            Algorithm::Kdf(K::HkdfSha512),
1812            Algorithm::Kdf(K::OneStepSha384),
1813            Algorithm::KeyAgreement(E::EcdhP521),
1814            Algorithm::Aead(D::Aes128Gcm),
1815            Algorithm::Hash(H::Sha384),
1816            Algorithm::Hash(H::Sha3_512),
1817            Algorithm::Signature(S::MlDsa87),
1818            Algorithm::Signature(S::Rsa3072Sha384Pss),
1819        ];
1820        assert_eq!(
1821            select_admitted_with(Profile::Fips1403, &FIPS1403, Options::default(), |a| {
1822                !lacking.contains(&a)
1823            }),
1824            AdmittedSelection {
1825                macs: vec![M::HmacSha512, M::HmacSha256],
1826                kdfs: vec![
1827                    K::HkdfSha384,
1828                    K::HkdfSha256,
1829                    K::OneStepSha512,
1830                    K::OneStepSha256
1831                ],
1832                key_agreements: vec![E::EcdhP384, E::EcdhP256],
1833                aeads: vec![D::Aes256Gcm],
1834                hashes: vec![H::Sha512, H::Sha256, H::Sha3_384, H::Sha3_256],
1835                signatures: vec![
1836                    S::EcdsaP384Sha384,
1837                    S::EcdsaP521Sha512,
1838                    S::EcdsaP256Sha256,
1839                    S::Ed25519,
1840                    S::Rsa4096Sha384Pss,
1841                    S::Rsa2048Sha384Pss,
1842                    S::Rsa4096Sha384,
1843                    S::Rsa3072Sha384,
1844                    S::Rsa2048Sha384,
1845                ],
1846                excluded: lacking.to_vec(),
1847            }
1848        );
1849    }
1850
1851    fn strength_bits(k: KeyAgreementAlgorithm) -> usize {
1852        match k {
1853            KeyAgreementAlgorithm::EcdhP256 | KeyAgreementAlgorithm::X25519 => 128,
1854            KeyAgreementAlgorithm::EcdhP384 => 192,
1855            KeyAgreementAlgorithm::EcdhP521 => 256,
1856        }
1857    }
1858
1859    #[test]
1860    fn every_admitted_curve_and_kdf_pair_meets_sp_800_56c_section_7() {
1861        let on = Options {
1862            cnsa1_unclassified_nss: true,
1863            ..Options::default()
1864        };
1865        let mut pairs = 0;
1866        for &p in Profile::ALL {
1867            for o in [Options::default(), on] {
1868                let s = select_admitted(p, o, M4S);
1869                for &k in &s.key_agreements {
1870                    for &d in &s.kdfs {
1871                        pairs += 1;
1872                        assert!(
1873                            8 * d.hash().output_len() >= strength_bits(k),
1874                            "{p} {o} {k:?} {d:?}"
1875                        );
1876                    }
1877                }
1878            }
1879        }
1880        assert_eq!(pairs, 2 * (4 * 6 + 3 * 6 + 3 * 6 + 3 * 6) + 1 + 2);
1881    }
1882
1883    #[test]
1884    fn the_unclassified_nss_option_adds_hmac_sha256_to_cnsa1_only() {
1885        let on = Options {
1886            cnsa1_unclassified_nss: true,
1887            ..Options::default()
1888        };
1889        for &p in Profile::ALL {
1890            let mut want = select_admitted(p, Options::default(), M4S).macs;
1891            if p == Profile::Cnsa1 {
1892                want.push(MacAlgorithm::HmacSha256);
1893            }
1894            assert_eq!(select_admitted(p, on, M4S).macs, want, "{p}");
1895        }
1896    }
1897
1898    #[test]
1899    fn the_unclassified_nss_option_adds_hkdf_sha256_to_cnsa1_only() {
1900        let on = Options {
1901            cnsa1_unclassified_nss: true,
1902            ..Options::default()
1903        };
1904        for &p in Profile::ALL {
1905            let mut want = select_admitted(p, Options::default(), M4S).kdfs;
1906            if p == Profile::Cnsa1 {
1907                want.push(KdfAlgorithm::HkdfSha256);
1908            }
1909            assert_eq!(select_admitted(p, on, M4S).kdfs, want, "{p}");
1910        }
1911    }
1912
1913    #[test]
1914    fn the_unclassified_nss_option_adds_sha256_hashing_to_cnsa1_only() {
1915        let on = Options {
1916            cnsa1_unclassified_nss: true,
1917            ..Options::default()
1918        };
1919        for &p in Profile::ALL {
1920            let mut want = select_admitted(p, Options::default(), M4S).hashes;
1921            if p == Profile::Cnsa1 {
1922                assert_eq!(want, [HashAlgorithm::Sha384]);
1923                want.push(HashAlgorithm::Sha256);
1924            }
1925            assert_eq!(select_admitted(p, on, M4S).hashes, want, "{p}");
1926            assert_eq!(select_admitted(p, on, M3S).hashes, want, "{p}");
1927        }
1928    }
1929
1930    #[test]
1931    fn the_unclassified_nss_option_adds_rsa_2048_signatures_to_cnsa1_only() {
1932        let on = Options {
1933            cnsa1_unclassified_nss: true,
1934            ..Options::default()
1935        };
1936        for &p in Profile::ALL {
1937            for m in [M3S, M4S] {
1938                let mut want = select_admitted(p, Options::default(), m).signatures;
1939                if p == Profile::Cnsa1 {
1940                    assert_eq!(
1941                        want,
1942                        [
1943                            S::EcdsaP384Sha384,
1944                            S::Rsa4096Sha384Pss,
1945                            S::Rsa3072Sha384Pss,
1946                            S::Rsa4096Sha384,
1947                            S::Rsa3072Sha384,
1948                        ]
1949                    );
1950                    want.extend([S::Rsa2048Sha384Pss, S::Rsa2048Sha384]);
1951                }
1952                assert_eq!(select_admitted(p, on, m).signatures, want, "{p} {m:?}");
1953            }
1954        }
1955    }
1956
1957    #[test]
1958    fn an_empty_kdf_info_is_admitted_under_unrestricted_only() {
1959        for &p in Profile::ALL {
1960            let may = match p {
1961                Profile::Unrestricted => true,
1962                Profile::Preferred
1963                | Profile::Cnsa2
1964                | Profile::Cnsa1
1965                | Profile::Bsi
1966                | Profile::Fips1403
1967                | Profile::Fips1402 => false,
1968            };
1969            assert_eq!(kdf_info_may_be_empty(p), may, "{p}");
1970        }
1971    }
1972
1973    #[test]
1974    fn mac_keys_are_bounded_by_the_module_and_by_bsi() {
1975        for &p in Profile::ALL {
1976            let floor = match p {
1977                Profile::Bsi => 128,
1978                Profile::Unrestricted
1979                | Profile::Preferred
1980                | Profile::Cnsa2
1981                | Profile::Cnsa1
1982                | Profile::Fips1403
1983                | Profile::Fips1402 => 112,
1984            };
1985            assert_eq!(mac_key_floor_bits(p), floor, "{p}");
1986        }
1987        assert_eq!(MAC_KEY_MAX_BITS, 524_288);
1988    }
1989}