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 excluded: SP 800-52r2 §3.2 (p. 9), Table 3-1 (pp. 10-11) and
653///   Table 4-1 (p. 34) name RSA and ECDSA certificates and signatures, not EdDSA (decision 0018).
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::RsaPssSha384,
702        T::RsaPssSha512,
703        T::RsaPssSha256,
704        T::RsaPkcs1Sha384,
705        T::RsaPkcs1Sha512,
706        T::RsaPkcs1Sha256,
707    ],
708    key_algorithms: &[
709        S::MlDsa87,
710        S::EcdsaP384Sha384,
711        S::EcdsaP256Sha256,
712        S::EcdsaP521Sha512,
713        S::EcdsaP521Sha384,
714        S::Rsa3072Sha384,
715        S::Rsa4096Sha384,
716        S::Rsa2048Sha384,
717    ],
718    macs: &[
719        MacAlgorithm::HmacSha512,
720        MacAlgorithm::HmacSha384,
721        MacAlgorithm::HmacSha256,
722    ],
723    kdfs: &[
724        KdfAlgorithm::HkdfSha512,
725        KdfAlgorithm::HkdfSha384,
726        KdfAlgorithm::HkdfSha256,
727        KdfAlgorithm::OneStepSha512,
728        KdfAlgorithm::OneStepSha384,
729        KdfAlgorithm::OneStepSha256,
730    ],
731    key_agreements: &[
732        KeyAgreementAlgorithm::EcdhP384,
733        KeyAgreementAlgorithm::EcdhP521,
734        KeyAgreementAlgorithm::EcdhP256,
735    ],
736    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
737    hashes: &[
738        HashAlgorithm::Sha512,
739        HashAlgorithm::Sha384,
740        HashAlgorithm::Sha256,
741        HashAlgorithm::Sha3_512,
742        HashAlgorithm::Sha3_384,
743        HashAlgorithm::Sha3_256,
744    ],
745    signatures: &[
746        S::MlDsa87,
747        S::EcdsaP384Sha384,
748        S::EcdsaP521Sha512,
749        S::EcdsaP256Sha256,
750        S::Ed25519,
751        S::Rsa4096Sha384Pss,
752        S::Rsa3072Sha384Pss,
753        S::Rsa2048Sha384Pss,
754        S::Rsa4096Sha384,
755        S::Rsa3072Sha384,
756        S::Rsa2048Sha384,
757    ],
758};
759
760/// `fips1402`: FIPS 140-2 Annex A and Annex D (2021-10-12 editions), FIPS 186-4, SP 800-131A r2
761/// and SP 800-52r2. An algorithm set, not a validation claim.
762/// - 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
763///   key schedule, Annex D item 11 (RFC 8446 §7.1).
764/// - ChaCha20-Poly1305 excluded: Annex A.
765/// - secp384r1, secp256r1: Annex D item 3 (SP 800-56A r3), Table 24.
766/// - ML-KEM and the hybrids excluded: Annex A; Annex D items 12-13.
767/// - X25519 excluded: SP 800-56A r3 Table 24.
768/// - ECDSA P-256, P-384, P-521 schemes; RSA-PSS and PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-4
769///   (Annex A, p. 2), §5.4-5.5, Appendix D.1.2; SP 800-131A r2 Table 2 (p. 9).
770/// - Ed25519 and ML-DSA excluded: Annex A.
771/// - ECDSA keys P-384/SHA-384, P-256/SHA-256, P-521/SHA-512, P-521/SHA-384: FIPS 186-4 §6.4
772///   (pp. 29-30).
773/// - RSA 3072 and 2048 keys: FIPS 186-4 §5.1; RSA 4096: FIPS 140-2 IG A.14 (p. 221).
774/// - MACs: FIPS 140-2 Annex A, Message Authentication 3 (p. 3); SP 800-131A r2 Table 9 (p. 20).
775/// - KDFs: FIPS 140-2 Annex D (p. 1) items 12 and 13, SP 800-56C Rev. 1 and SP 800-56C (2011),
776///   each specifying extraction-then-expansion; the one-step method of SP 800-56C Rev. 1, which
777///   IG D.8 Scenario X1 (p. 241) admits for key agreement.
778/// - Key agreement: ECDH on P-384, P-521 and P-256: Annex D item 3 (SP 800-56A r3), Table 24;
779///   IG D.8 Scenario X1 (p. 241). X25519 excluded: not in Table 24.
780/// - AEADs: AES-256-GCM, AES-128-GCM. Annex A (pp. 1, 3): AES, SP 800-38D; IV rules, FIPS 140-2
781///   IG A.5. ChaCha20-Poly1305 excluded: Annex A.
782/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. Annex A (p. 2), FIPS 180-4
783///   and FIPS 202; FIPS 140-2 IG A.11 Resolution 1 (p. 216), SHA-3 "as the standalone functions".
784/// - Signatures: ECDSA on P-384, on P-521 with SHA-512 and with SHA-384, and on P-256; RSA 4096,
785///   3072 and 2048 with PSS, then with PKCS#1 v1.5. Annex A (p. 2), FIPS 186-4: §5.5 (p. 24), both
786///   paddings; §6.4 (p. 30), a hash weaker than the curve "ordinarily should not be used"; RSA
787///   4096, FIPS 140-2 IG A.14 (p. 221). Ed25519 and ML-DSA excluded: Annex A.
788pub const FIPS1402: ProfileSpec = ProfileSpec {
789    versions: &[V::Tls13, V::Tls12],
790    cipher_suites: &[
791        C::Tls13Aes256GcmSha384,
792        C::Tls13Aes128GcmSha256,
793        C::Tls12EcdheEcdsaAes256GcmSha384,
794        C::Tls12EcdheRsaAes256GcmSha384,
795        C::Tls12EcdheEcdsaAes128GcmSha256,
796        C::Tls12EcdheRsaAes128GcmSha256,
797    ],
798    kx_groups: &[K::Secp384r1, K::Secp256r1],
799    signature_schemes: &[
800        T::EcdsaNistp384Sha384,
801        T::EcdsaNistp256Sha256,
802        T::EcdsaNistp521Sha512,
803        T::RsaPssSha384,
804        T::RsaPssSha512,
805        T::RsaPssSha256,
806        T::RsaPkcs1Sha384,
807        T::RsaPkcs1Sha512,
808        T::RsaPkcs1Sha256,
809    ],
810    key_algorithms: &[
811        S::EcdsaP384Sha384,
812        S::EcdsaP256Sha256,
813        S::EcdsaP521Sha512,
814        S::EcdsaP521Sha384,
815        S::Rsa3072Sha384,
816        S::Rsa4096Sha384,
817        S::Rsa2048Sha384,
818    ],
819    macs: &[
820        MacAlgorithm::HmacSha512,
821        MacAlgorithm::HmacSha384,
822        MacAlgorithm::HmacSha256,
823    ],
824    kdfs: &[
825        KdfAlgorithm::HkdfSha512,
826        KdfAlgorithm::HkdfSha384,
827        KdfAlgorithm::HkdfSha256,
828        KdfAlgorithm::OneStepSha512,
829        KdfAlgorithm::OneStepSha384,
830        KdfAlgorithm::OneStepSha256,
831    ],
832    key_agreements: &[
833        KeyAgreementAlgorithm::EcdhP384,
834        KeyAgreementAlgorithm::EcdhP521,
835        KeyAgreementAlgorithm::EcdhP256,
836    ],
837    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
838    hashes: &[
839        HashAlgorithm::Sha512,
840        HashAlgorithm::Sha384,
841        HashAlgorithm::Sha256,
842        HashAlgorithm::Sha3_512,
843        HashAlgorithm::Sha3_384,
844        HashAlgorithm::Sha3_256,
845    ],
846    signatures: &[
847        S::EcdsaP384Sha384,
848        S::EcdsaP521Sha512,
849        S::EcdsaP521Sha384,
850        S::EcdsaP256Sha256,
851        S::Rsa4096Sha384Pss,
852        S::Rsa3072Sha384Pss,
853        S::Rsa2048Sha384Pss,
854        S::Rsa4096Sha384,
855        S::Rsa3072Sha384,
856        S::Rsa2048Sha384,
857    ],
858};
859
860/// A profile narrowed by its options, every entry implemented by the module's backend, in the
861/// profile's preference order.
862#[derive(Debug, Clone, PartialEq, Eq)]
863#[non_exhaustive]
864pub struct Selection {
865    /// Protocol versions.
866    pub versions: Vec<V>,
867    /// Cipher suites, each of a version in `versions`.
868    pub cipher_suites: Vec<C>,
869    /// Key-exchange groups.
870    pub kx_groups: Vec<K>,
871    /// Handshake signature schemes.
872    pub signature_schemes: Vec<T>,
873    /// Key algorithms.
874    pub key_algorithms: Vec<S>,
875    /// Entries unavailable on the module line after Options, in family and preference order.
876    pub excluded: Vec<Algorithm>,
877}
878
879const fn is_tls12_only(t: T) -> bool {
880    match t {
881        T::RsaPkcs1Sha256 | T::RsaPkcs1Sha384 | T::RsaPkcs1Sha512 => true,
882        T::EcdsaNistp256Sha256
883        | T::EcdsaNistp384Sha384
884        | T::EcdsaNistp521Sha512
885        | T::Ed25519
886        | T::RsaPssSha256
887        | T::RsaPssSha384
888        | T::RsaPssSha512
889        | T::MlDsa87 => false,
890    }
891}
892
893fn keep<X: Copy>(items: &[X], mut wanted: impl FnMut(X) -> bool) -> Vec<X> {
894    items.iter().copied().filter(|x| wanted(*x)).collect()
895}
896
897/// The backend-independent half of a backend's `resolve`: `profile`'s lists, narrowed by
898/// `options`; `pq_key_exchange_required` also removes TLS 1.2, its suites and the RSA PKCS#1 v1.5
899/// handshake schemes. Records entries the module line lacks in [`Selection::excluded`], then
900/// refuses a family left empty with [`CryptoError::EmptyProfile`]. Both use versions, suites,
901/// groups, schemes and keys order, preserving preference order. Approval is a separate check.
902pub fn select(
903    profile: Profile,
904    options: Options,
905    module: Module,
906) -> Result<Selection, CryptoError> {
907    select_spec(profile, profile.spec(), options, module)
908}
909
910fn select_spec(
911    profile: Profile,
912    spec: &ProfileSpec,
913    options: Options,
914    module: Module,
915) -> Result<Selection, CryptoError> {
916    let pq = options.pq_key_exchange_required;
917    let versions = keep(spec.versions, |v| !(pq && v == V::Tls12));
918    let cipher_suites = keep(spec.cipher_suites, |c| versions.contains(&c.version()));
919    let kx_groups = keep(spec.kx_groups, |k| !pq || k.is_post_quantum());
920    let signature_schemes = keep(spec.signature_schemes, |t| !(pq && is_tls12_only(t)));
921    let key_algorithms = spec.key_algorithms.to_vec();
922    let mut excluded = Vec::new();
923    let mut available = |a| {
924        let on = capability(module, a).available;
925        if !on {
926            excluded.push(a);
927        }
928        on
929    };
930    let versions = keep(&versions, |v| available(Algorithm::Protocol(v)));
931    let cipher_suites = keep(&cipher_suites, |c| available(Algorithm::Cipher(c)));
932    let kx_groups = keep(&kx_groups, |k| available(Algorithm::Kx(k)));
933    let signature_schemes = keep(&signature_schemes, |t| available(Algorithm::Scheme(t)));
934    let key_algorithms = keep(&key_algorithms, |s| available(Algorithm::Signature(s)));
935    for (empty, category) in [
936        (versions.is_empty(), Family::Version),
937        (cipher_suites.is_empty(), Family::CipherSuite),
938        (kx_groups.is_empty(), Family::KxGroup),
939        (signature_schemes.is_empty(), Family::SignatureScheme),
940        (key_algorithms.is_empty(), Family::KeyAlgorithm),
941    ] {
942        if empty {
943            return Err(CryptoError::EmptyProfile {
944                profile,
945                options,
946                category,
947            });
948        }
949    }
950    Ok(Selection {
951        versions,
952        cipher_suites,
953        kx_groups,
954        signature_schemes,
955        key_algorithms,
956        excluded,
957    })
958}
959
960/// A profile's non-TLS families under its options, every entry implemented by the module's
961/// backend, in preference order. Never refused: an empty family means every constructor of that
962/// family refuses under the profile.
963#[derive(Debug, Clone, PartialEq, Eq)]
964#[non_exhaustive]
965pub struct AdmittedSelection {
966    /// Message authentication codes.
967    pub macs: Vec<MacAlgorithm>,
968    /// Key-derivation functions.
969    pub kdfs: Vec<KdfAlgorithm>,
970    /// Key agreement.
971    pub key_agreements: Vec<KeyAgreementAlgorithm>,
972    /// Authenticated encryption.
973    pub aeads: Vec<AeadAlgorithm>,
974    /// Hash functions.
975    pub hashes: Vec<HashAlgorithm>,
976    /// Signature algorithms.
977    pub signatures: Vec<S>,
978    /// Entries unavailable on the module line, in family and preference order.
979    pub excluded: Vec<Algorithm>,
980}
981
982/// The backend-independent half of a backend's `admit`: `profile`'s non-TLS families, under
983/// `options`, less what `module` lacks. Independent of [`select`].
984pub fn select_admitted(profile: Profile, options: Options, module: Module) -> AdmittedSelection {
985    select_admitted_with(profile, profile.spec(), options, |a| {
986        capability(module, a).available
987    })
988}
989
990fn select_admitted_with(
991    profile: Profile,
992    spec: &ProfileSpec,
993    options: Options,
994    available: impl Fn(Algorithm) -> bool,
995) -> AdmittedSelection {
996    let mut macs = spec.macs.to_vec();
997    let mut kdfs = spec.kdfs.to_vec();
998    let mut hashes = spec.hashes.to_vec();
999    let mut signatures = spec.signatures.to_vec();
1000    if options.cnsa1_unclassified_nss {
1001        macs.extend_from_slice(unclassified_nss_macs(profile));
1002        kdfs.extend_from_slice(unclassified_nss_kdfs(profile));
1003        hashes.extend_from_slice(unclassified_nss_hashes(profile));
1004        signatures.extend_from_slice(unclassified_nss_signatures(profile));
1005    }
1006    let mut key_agreements = spec.key_agreements.to_vec();
1007    let mut aeads = spec.aeads.to_vec();
1008    let mut excluded = Vec::new();
1009    retain_available(&mut macs, Algorithm::Mac, &available, &mut excluded);
1010    retain_available(&mut kdfs, Algorithm::Kdf, &available, &mut excluded);
1011    retain_available(
1012        &mut key_agreements,
1013        Algorithm::KeyAgreement,
1014        &available,
1015        &mut excluded,
1016    );
1017    retain_available(&mut aeads, Algorithm::Aead, &available, &mut excluded);
1018    retain_available(&mut hashes, Algorithm::Hash, &available, &mut excluded);
1019    retain_available(
1020        &mut signatures,
1021        Algorithm::Signature,
1022        &available,
1023        &mut excluded,
1024    );
1025    AdmittedSelection {
1026        macs,
1027        kdfs,
1028        key_agreements,
1029        aeads,
1030        hashes,
1031        signatures,
1032        excluded,
1033    }
1034}
1035
1036fn retain_available<X: Copy>(
1037    xs: &mut Vec<X>,
1038    tag: fn(X) -> Algorithm,
1039    available: &impl Fn(Algorithm) -> bool,
1040    excluded: &mut Vec<Algorithm>,
1041) {
1042    xs.retain(|x| {
1043        let on = available(tag(*x));
1044        if !on {
1045            excluded.push(tag(*x));
1046        }
1047        on
1048    });
1049}
1050
1051fn unclassified_nss_macs(p: Profile) -> &'static [MacAlgorithm] {
1052    match p {
1053        Profile::Cnsa1 => &[MacAlgorithm::HmacSha256],
1054        Profile::Unrestricted
1055        | Profile::Preferred
1056        | Profile::Cnsa2
1057        | Profile::Bsi
1058        | Profile::Fips1403
1059        | Profile::Fips1402 => &[],
1060    }
1061}
1062
1063fn unclassified_nss_kdfs(p: Profile) -> &'static [KdfAlgorithm] {
1064    match p {
1065        Profile::Cnsa1 => &[KdfAlgorithm::HkdfSha256],
1066        Profile::Unrestricted
1067        | Profile::Preferred
1068        | Profile::Cnsa2
1069        | Profile::Bsi
1070        | Profile::Fips1403
1071        | Profile::Fips1402 => &[],
1072    }
1073}
1074
1075fn unclassified_nss_hashes(p: Profile) -> &'static [HashAlgorithm] {
1076    match p {
1077        Profile::Cnsa1 => &[HashAlgorithm::Sha256],
1078        Profile::Unrestricted
1079        | Profile::Preferred
1080        | Profile::Cnsa2
1081        | Profile::Bsi
1082        | Profile::Fips1403
1083        | Profile::Fips1402 => &[],
1084    }
1085}
1086
1087fn unclassified_nss_signatures(p: Profile) -> &'static [S] {
1088    match p {
1089        Profile::Cnsa1 => &[S::Rsa2048Sha384Pss, S::Rsa2048Sha384],
1090        Profile::Unrestricted
1091        | Profile::Preferred
1092        | Profile::Cnsa2
1093        | Profile::Bsi
1094        | Profile::Fips1403
1095        | Profile::Fips1402 => &[],
1096    }
1097}
1098
1099/// Whether `profile` admits an empty HKDF `info`: `unrestricted` only, which makes no approval
1100/// claim. The module does not mark an HKDF call with an empty `info` approved (AWS-LC
1101/// `HKDF_verify_service_indicator`), so every other profile refuses one (decision 0010).
1102pub const fn kdf_info_may_be_empty(profile: Profile) -> bool {
1103    match profile {
1104        Profile::Unrestricted => true,
1105        Profile::Preferred
1106        | Profile::Cnsa2
1107        | Profile::Cnsa1
1108        | Profile::Bsi
1109        | Profile::Fips1403
1110        | Profile::Fips1402 => false,
1111    }
1112}
1113
1114/// The shortest HMAC key, in bits, `profile` admits. 112 is the module's approved minimum: SP
1115/// 800-131A r2 §10 Table 9 (p. 20) disallows shorter keys for MAC generation, and the AWS-LC FIPS
1116/// 3.1.0 security policies list "Key Length: 112-524288" (Table 5, p. 9). `bsi` takes the 128 of
1117/// TR-02102-1 Table 5.2 (p. 50). SP 800-107 r1 §5.3.2 (pp. 13-14): a key "shall have a security
1118/// strength that meets or exceeds the security strength required to protect the data".
1119pub const fn mac_key_floor_bits(profile: Profile) -> usize {
1120    match profile {
1121        Profile::Bsi => 128,
1122        Profile::Unrestricted
1123        | Profile::Preferred
1124        | Profile::Cnsa2
1125        | Profile::Cnsa1
1126        | Profile::Fips1403
1127        | Profile::Fips1402 => 112,
1128    }
1129}
1130
1131/// The longest HMAC key, in bits, the module was tested with: "Key Length: 112-524288" in the
1132/// AWS-LC FIPS 3.1.0 security policies (Table 5, p. 9) and on CAVP certificate A7855.
1133pub const MAC_KEY_MAX_BITS: usize = 524_288;
1134
1135#[cfg(test)]
1136mod tests {
1137    use super::*;
1138    use crate::algorithm::*;
1139    use crate::capability::{Linkage, Module, ModuleLine};
1140    use crate::error::{CryptoError, Family};
1141
1142    #[test]
1143    fn every_profile_lists_its_schemes_in_the_canonical_order() {
1144        assert_eq!(
1145            Profile::Unrestricted.spec().signature_schemes.len(),
1146            TlsSignatureScheme::ALL.len()
1147        );
1148        for &p in Profile::ALL {
1149            let s = p.spec().signature_schemes;
1150            assert!(
1151                s.windows(2).all(|w| w[0].preference() < w[1].preference()),
1152                "{p}"
1153            );
1154        }
1155    }
1156
1157    const M3S: Module = Module {
1158        line: ModuleLine::AwsLcFips3,
1159        linkage: Linkage::Static,
1160    };
1161    const M4S: Module = Module {
1162        line: ModuleLine::AwsLcFips4,
1163        linkage: Linkage::Static,
1164    };
1165    const PQ: Options = Options {
1166        peer_certificates_match_profile: false,
1167        pq_key_exchange_required: true,
1168        cnsa1_unclassified_nss: false,
1169    };
1170    const PKCS1: [TlsSignatureScheme; 3] = [
1171        TlsSignatureScheme::RsaPkcs1Sha256,
1172        TlsSignatureScheme::RsaPkcs1Sha384,
1173        TlsSignatureScheme::RsaPkcs1Sha512,
1174    ];
1175
1176    fn no_duplicates<X: PartialEq + std::fmt::Debug>(xs: &[X]) {
1177        for (i, x) in xs.iter().enumerate() {
1178            assert!(!xs[i + 1..].contains(x), "{x:?} twice");
1179        }
1180    }
1181
1182    #[test]
1183    fn every_spec_is_well_formed() {
1184        for &p in Profile::ALL {
1185            let s = p.spec();
1186            no_duplicates(s.versions);
1187            no_duplicates(s.cipher_suites);
1188            no_duplicates(s.kx_groups);
1189            no_duplicates(s.signature_schemes);
1190            no_duplicates(s.key_algorithms);
1191            no_duplicates(s.macs);
1192            no_duplicates(s.kdfs);
1193            no_duplicates(s.key_agreements);
1194            no_duplicates(s.aeads);
1195            no_duplicates(s.hashes);
1196            no_duplicates(s.signatures);
1197            assert!(!s.versions.is_empty() && !s.kx_groups.is_empty(), "{p}");
1198            assert!(
1199                !s.signature_schemes.is_empty() && !s.key_algorithms.is_empty(),
1200                "{p}"
1201            );
1202            assert!(
1203                s.cipher_suites
1204                    .iter()
1205                    .all(|c| s.versions.contains(&c.version())),
1206                "{p}"
1207            );
1208            let has_pkcs1 = s.signature_schemes.iter().any(|t| PKCS1.contains(t));
1209            assert!(!has_pkcs1 || s.versions.contains(&TlsVersion::Tls12), "{p}");
1210        }
1211    }
1212
1213    #[test]
1214    fn preferred_excludes_what_its_definition_names() {
1215        let s = Profile::Preferred.spec();
1216        assert_eq!(s.versions, &[TlsVersion::Tls13]);
1217        assert!(!s.kx_groups.contains(&KxGroup::X25519));
1218        assert!(
1219            !s.cipher_suites
1220                .contains(&CipherSuite::Tls13Chacha20Poly1305Sha256)
1221        );
1222        assert!(!s.signature_schemes.contains(&TlsSignatureScheme::Ed25519));
1223        assert!(!s.key_algorithms.contains(&SignatureAlgorithm::Ed25519));
1224        assert!(!s.aeads.contains(&AeadAlgorithm::Chacha20Poly1305));
1225        for h in [
1226            HashAlgorithm::Sha3_256,
1227            HashAlgorithm::Sha3_384,
1228            HashAlgorithm::Sha3_512,
1229        ] {
1230            assert!(!s.hashes.contains(&h), "{h:?}");
1231        }
1232        for a in [
1233            SignatureAlgorithm::Ed25519,
1234            SignatureAlgorithm::EcdsaP521Sha384,
1235            SignatureAlgorithm::Rsa2048Sha384,
1236            SignatureAlgorithm::Rsa2048Sha384Pss,
1237        ] {
1238            assert!(!s.signatures.contains(&a), "{a}");
1239        }
1240    }
1241
1242    #[test]
1243    fn no_tls_key_list_holds_an_rsa_pss_variant() {
1244        for &p in Profile::ALL {
1245            for &k in p.spec().key_algorithms {
1246                assert!(k.tls_scheme().is_some(), "{p} {k}");
1247            }
1248        }
1249        for a in [
1250            S::Rsa2048Sha384Pss,
1251            S::Rsa3072Sha384Pss,
1252            S::Rsa4096Sha384Pss,
1253        ] {
1254            assert_eq!(a.tls_scheme(), None, "{a}");
1255            assert!(Profile::Unrestricted.spec().signatures.contains(&a), "{a}");
1256        }
1257    }
1258
1259    #[test]
1260    fn p521_with_sha384_signs_outside_tls_only_where_fips_186_5_does_not_govern() {
1261        for &p in Profile::ALL {
1262            let outside = p.spec().signatures.contains(&S::EcdsaP521Sha384);
1263            let want = match p {
1264                Profile::Unrestricted | Profile::Fips1402 => true,
1265                Profile::Preferred
1266                | Profile::Cnsa2
1267                | Profile::Cnsa1
1268                | Profile::Bsi
1269                | Profile::Fips1403 => false,
1270            };
1271            assert_eq!(outside, want, "{p}");
1272        }
1273        for p in [Profile::Preferred, Profile::Fips1403] {
1274            assert!(p.spec().key_algorithms.contains(&S::EcdsaP521Sha384), "{p}");
1275        }
1276    }
1277
1278    #[test]
1279    fn bsi_signs_with_rsa_pss_only() {
1280        assert_eq!(
1281            Profile::Bsi.spec().signatures,
1282            &[S::Rsa4096Sha384Pss, S::Rsa3072Sha384Pss]
1283        );
1284    }
1285
1286    #[test]
1287    fn bsi_admits_only_ecdsa_keys() {
1288        assert_eq!(
1289            Profile::Bsi.spec().key_algorithms,
1290            &[
1291                SignatureAlgorithm::EcdsaP384Sha384,
1292                SignatureAlgorithm::EcdsaP256Sha256,
1293                SignatureAlgorithm::EcdsaP521Sha512,
1294            ]
1295        );
1296    }
1297
1298    #[test]
1299    fn select_records_line_exclusions_after_options() {
1300        for module in [
1301            M3S,
1302            Module {
1303                linkage: Linkage::Dynamic,
1304                ..M3S
1305            },
1306        ] {
1307            for p in [Profile::Unrestricted, Profile::Preferred, Profile::Fips1403] {
1308                for options in [Options::default(), PQ] {
1309                    let s = select(p, options, module).unwrap();
1310                    assert_eq!(
1311                        s.excluded,
1312                        vec![
1313                            Algorithm::Scheme(T::MlDsa87),
1314                            Algorithm::Signature(S::MlDsa87),
1315                        ]
1316                    );
1317                    no_duplicates(&s.excluded);
1318                    assert!(!s.signature_schemes.contains(&T::MlDsa87));
1319                    assert!(!s.key_algorithms.contains(&S::MlDsa87));
1320                }
1321            }
1322            for options in [Options::default(), PQ] {
1323                assert_eq!(
1324                    select(Profile::Cnsa2, options, module),
1325                    Err(CryptoError::EmptyProfile {
1326                        profile: Profile::Cnsa2,
1327                        options,
1328                        category: Family::SignatureScheme,
1329                    })
1330                );
1331            }
1332            for p in [Profile::Cnsa1, Profile::Bsi, Profile::Fips1402] {
1333                assert!(
1334                    select(p, Options::default(), module)
1335                        .unwrap()
1336                        .excluded
1337                        .is_empty()
1338                );
1339            }
1340        }
1341    }
1342
1343    #[test]
1344    fn select_checks_keys_and_options_precede_line_emptiness() {
1345        const KEY_ONLY: ProfileSpec = ProfileSpec {
1346            signature_schemes: &[T::EcdsaNistp384Sha384],
1347            ..CNSA2
1348        };
1349        assert_eq!(
1350            select_spec(Profile::Cnsa2, &KEY_ONLY, Options::default(), M3S),
1351            Err(CryptoError::EmptyProfile {
1352                profile: Profile::Cnsa2,
1353                options: Options::default(),
1354                category: Family::KeyAlgorithm,
1355            })
1356        );
1357        const BSI_WITH_ML_DSA: ProfileSpec = ProfileSpec {
1358            signature_schemes: &[T::MlDsa87],
1359            ..BSI
1360        };
1361        for spec in [&BSI_WITH_ML_DSA, &BSI] {
1362            assert_eq!(
1363                select_spec(Profile::Bsi, spec, PQ, M3S),
1364                Err(CryptoError::EmptyProfile {
1365                    profile: Profile::Bsi,
1366                    options: PQ,
1367                    category: Family::KxGroup,
1368                })
1369            );
1370        }
1371    }
1372
1373    #[test]
1374    fn select_refuses_each_empty_family_in_order() {
1375        const EMPTY: ProfileSpec = ProfileSpec {
1376            versions: &[],
1377            cipher_suites: &[],
1378            kx_groups: &[],
1379            signature_schemes: &[],
1380            key_algorithms: &[],
1381            macs: &[],
1382            kdfs: &[],
1383            key_agreements: &[],
1384            aeads: &[],
1385            hashes: &[],
1386            signatures: &[],
1387        };
1388        let empty = |category| {
1389            Err(CryptoError::EmptyProfile {
1390                profile: Profile::Bsi,
1391                options: Options::default(),
1392                category,
1393            })
1394        };
1395        let cases: [(ProfileSpec, Family); 5] = [
1396            (EMPTY, Family::Version),
1397            (
1398                ProfileSpec {
1399                    versions: BSI.versions,
1400                    ..EMPTY
1401                },
1402                Family::CipherSuite,
1403            ),
1404            (
1405                ProfileSpec {
1406                    kx_groups: &[],
1407                    signature_schemes: &[],
1408                    key_algorithms: &[],
1409                    ..BSI
1410                },
1411                Family::KxGroup,
1412            ),
1413            (
1414                ProfileSpec {
1415                    signature_schemes: &[],
1416                    key_algorithms: &[],
1417                    ..BSI
1418                },
1419                Family::SignatureScheme,
1420            ),
1421            (
1422                ProfileSpec {
1423                    key_algorithms: &[],
1424                    ..BSI
1425                },
1426                Family::KeyAlgorithm,
1427            ),
1428        ];
1429        for (spec, category) in cases {
1430            assert_eq!(
1431                select_spec(Profile::Bsi, &spec, Options::default(), M4S),
1432                empty(category),
1433                "{category:?}"
1434            );
1435        }
1436    }
1437
1438    #[test]
1439    fn select_without_options_is_the_spec_on_a_module_that_has_everything() {
1440        for &p in Profile::ALL {
1441            let s = select(p, Options::default(), M4S).unwrap();
1442            let spec = p.spec();
1443            assert!(s.excluded.is_empty());
1444            assert_eq!(s.versions, spec.versions.to_vec(), "{p}");
1445            assert_eq!(s.cipher_suites, spec.cipher_suites.to_vec(), "{p}");
1446            assert_eq!(s.kx_groups, spec.kx_groups.to_vec(), "{p}");
1447            assert_eq!(s.signature_schemes, spec.signature_schemes.to_vec(), "{p}");
1448            assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
1449        }
1450    }
1451
1452    #[test]
1453    fn the_option_keeps_post_quantum_groups_tls13_and_no_pkcs1_schemes() {
1454        for &p in Profile::ALL {
1455            let refused = match p {
1456                Profile::Unrestricted | Profile::Preferred | Profile::Cnsa2 | Profile::Fips1403 => {
1457                    false
1458                }
1459                Profile::Cnsa1 | Profile::Bsi | Profile::Fips1402 => true,
1460            };
1461            match select(p, PQ, M4S) {
1462                Ok(s) => {
1463                    assert!(!refused, "{p}");
1464                    let spec = p.spec();
1465                    assert_eq!(s.versions, vec![TlsVersion::Tls13], "{p}");
1466                    let suites: Vec<_> = spec
1467                        .cipher_suites
1468                        .iter()
1469                        .copied()
1470                        .filter(|c| c.version() == TlsVersion::Tls13)
1471                        .collect();
1472                    assert_eq!(s.cipher_suites, suites, "{p}");
1473                    let groups: Vec<_> = spec
1474                        .kx_groups
1475                        .iter()
1476                        .copied()
1477                        .filter(|k| k.is_post_quantum())
1478                        .collect();
1479                    assert_eq!(s.kx_groups, groups, "{p}");
1480                    let schemes: Vec<_> = spec
1481                        .signature_schemes
1482                        .iter()
1483                        .copied()
1484                        .filter(|t| !PKCS1.contains(t))
1485                        .collect();
1486                    assert_eq!(s.signature_schemes, schemes, "{p}");
1487                    assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
1488                }
1489                Err(e) => {
1490                    assert!(refused, "{p}");
1491                    assert_eq!(
1492                        e,
1493                        CryptoError::EmptyProfile {
1494                            profile: p,
1495                            options: PQ,
1496                            category: Family::KxGroup
1497                        }
1498                    );
1499                }
1500            }
1501        }
1502    }
1503
1504    #[test]
1505    fn the_option_removes_every_pkcs1_scheme_from_unrestricted_and_fips1403() {
1506        use TlsSignatureScheme as T;
1507        let unrestricted = vec![
1508            T::MlDsa87,
1509            T::EcdsaNistp384Sha384,
1510            T::EcdsaNistp256Sha256,
1511            T::EcdsaNistp521Sha512,
1512            T::Ed25519,
1513            T::RsaPssSha384,
1514            T::RsaPssSha512,
1515            T::RsaPssSha256,
1516        ];
1517        let fips1403 = vec![
1518            T::MlDsa87,
1519            T::EcdsaNistp384Sha384,
1520            T::EcdsaNistp256Sha256,
1521            T::EcdsaNistp521Sha512,
1522            T::RsaPssSha384,
1523            T::RsaPssSha512,
1524            T::RsaPssSha256,
1525        ];
1526        for (p, expected) in [
1527            (Profile::Unrestricted, unrestricted),
1528            (Profile::Fips1403, fips1403),
1529        ] {
1530            assert_eq!(
1531                select(p, PQ, M4S).unwrap().signature_schemes,
1532                expected,
1533                "{p}"
1534            );
1535            assert_eq!(
1536                select(p, Options::default(), M4S)
1537                    .unwrap()
1538                    .signature_schemes
1539                    .len(),
1540                expected.len() + 3,
1541                "{p}"
1542            );
1543        }
1544    }
1545
1546    #[test]
1547    fn profile_names_are_stable_and_exact() {
1548        assert_eq!(
1549            Profile::ALL
1550                .iter()
1551                .copied()
1552                .map(Profile::as_str)
1553                .collect::<Vec<_>>(),
1554            [
1555                "unrestricted",
1556                "preferred",
1557                "cnsa2",
1558                "cnsa1",
1559                "bsi",
1560                "fips1403",
1561                "fips1402"
1562            ]
1563        );
1564        for &p in Profile::ALL {
1565            assert_eq!(p.as_str().parse::<Profile>(), Ok(p));
1566            assert_eq!(p.to_string(), p.as_str());
1567        }
1568        for bad in [
1569            "",
1570            "CNSA2",
1571            "Cnsa2",
1572            "FIPS1403",
1573            "fips140-3",
1574            "fips-140-3",
1575            " fips1403",
1576            "fips1403 ",
1577            "fips1403\n",
1578            " bsi",
1579            "cnsa",
1580            "bsi2026",
1581            "preferred,",
1582            "default",
1583        ] {
1584            assert_eq!(
1585                bad.parse::<Profile>(),
1586                Err(ParseProfileError(bad.to_string())),
1587                "{bad:?}"
1588            );
1589        }
1590        assert_eq!(
1591            ParseProfileError("FIPS1403".to_string()).to_string(),
1592            "unknown profile \"FIPS1403\""
1593        );
1594    }
1595
1596    #[test]
1597    fn profile_all_lists_every_variant_in_ordinal_order() {
1598        assert_eq!(
1599            Profile::ALL
1600                .iter()
1601                .copied()
1602                .map(Profile::ordinal)
1603                .collect::<Vec<_>>(),
1604            std::array::from_fn::<usize, 7, _>(|i| i)
1605        );
1606    }
1607
1608    #[test]
1609    fn governing_texts_are_the_published_editions() {
1610        use GoverningText::{CrateRecommendation as Own, Published};
1611        let table: [(Profile, GoverningText); 7] = [
1612            (Profile::Unrestricted, Own),
1613            (Profile::Preferred, Own),
1614            (
1615                Profile::Cnsa2,
1616                Published {
1617                    documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
1618                },
1619            ),
1620            (
1621                Profile::Cnsa1,
1622                Published {
1623                    documents: &[
1624                        "CNSSP 15 (20 October 2016), Annex B, p. B-1",
1625                        "RFC 9151 (April 2022)",
1626                    ],
1627                },
1628            ),
1629            (
1630                Profile::Bsi,
1631                Published {
1632                    documents: &[
1633                        "BSI TR-02102-2, version 2026-01",
1634                        "BSI TR-02102-1, version 2026-01",
1635                    ],
1636                },
1637            ),
1638            (
1639                Profile::Fips1403,
1640                Published {
1641                    documents: &[
1642                        "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
1643                        "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
1644                        "NIST SP 800-52r2",
1645                        "NIST SP 800-131A r2",
1646                    ],
1647                },
1648            ),
1649            (
1650                Profile::Fips1402,
1651                Published {
1652                    documents: &[
1653                        "FIPS 140-2 Annex A and Annex D (2021)",
1654                        "FIPS 186-4",
1655                        "NIST SP 800-131A r2",
1656                        "NIST SP 800-52r2",
1657                    ],
1658                },
1659            ),
1660        ];
1661        for (p, text) in table {
1662            assert_eq!(p.governing_text(), text, "{p}");
1663        }
1664    }
1665
1666    #[test]
1667    fn options_default_narrows_nothing_and_display_names_the_option() {
1668        assert_eq!(
1669            Options::default(),
1670            Options {
1671                peer_certificates_match_profile: false,
1672                pq_key_exchange_required: false,
1673                cnsa1_unclassified_nss: false,
1674            }
1675        );
1676        assert_eq!(Options::default().to_string(), "no options");
1677        assert_eq!(
1678            Options {
1679                peer_certificates_match_profile: false,
1680                pq_key_exchange_required: true,
1681                cnsa1_unclassified_nss: false,
1682            }
1683            .to_string(),
1684            "post-quantum key exchange required"
1685        );
1686    }
1687    #[test]
1688    fn peer_certificate_options_combinations_preserve_selection() {
1689        for (pq, peer, display) in [
1690            (false, false, "no options"),
1691            (true, false, "post-quantum key exchange required"),
1692            (false, true, "peer certificates match profile"),
1693            (
1694                true,
1695                true,
1696                "post-quantum key exchange required, peer certificates match profile",
1697            ),
1698        ] {
1699            let options = Options {
1700                pq_key_exchange_required: pq,
1701                peer_certificates_match_profile: peer,
1702                cnsa1_unclassified_nss: false,
1703            };
1704            assert_eq!(options.to_string(), display);
1705            for &profile in Profile::ALL {
1706                for module in [M3S, M4S] {
1707                    let baseline = Options {
1708                        peer_certificates_match_profile: false,
1709                        ..options
1710                    };
1711                    let actual = select(profile, options, module);
1712                    let expected = select(profile, baseline, module);
1713                    match (actual, expected) {
1714                        (Ok(actual), Ok(expected)) => assert_eq!(actual, expected),
1715                        (
1716                            Err(CryptoError::EmptyProfile {
1717                                profile: p,
1718                                options: o,
1719                                category: f,
1720                            }),
1721                            Err(CryptoError::EmptyProfile {
1722                                profile: ep,
1723                                options: eo,
1724                                category: ef,
1725                            }),
1726                        ) => {
1727                            assert_eq!((p, o, f), (ep, options, ef));
1728                            assert_eq!(eo, baseline);
1729                        }
1730                        (actual, expected) => panic!("{actual:?} != {expected:?}"),
1731                    }
1732                }
1733            }
1734        }
1735    }
1736
1737    #[test]
1738    fn cnsa1_unclassified_nss_names_itself_and_leaves_tls_selection_alone() {
1739        let on = Options {
1740            cnsa1_unclassified_nss: true,
1741            ..Options::default()
1742        };
1743        assert_eq!(on.to_string(), "cnsa1 UNCLASSIFIED NSS");
1744        assert_eq!(
1745            Options {
1746                pq_key_exchange_required: true,
1747                peer_certificates_match_profile: true,
1748                cnsa1_unclassified_nss: true,
1749            }
1750            .to_string(),
1751            "post-quantum key exchange required, peer certificates match profile, cnsa1 UNCLASSIFIED NSS"
1752        );
1753        for &profile in Profile::ALL {
1754            for module in [M3S, M4S] {
1755                for pq in [false, true] {
1756                    let off = Options {
1757                        pq_key_exchange_required: pq,
1758                        ..Options::default()
1759                    };
1760                    let on = Options {
1761                        cnsa1_unclassified_nss: true,
1762                        ..off
1763                    };
1764                    match (select(profile, on, module), select(profile, off, module)) {
1765                        (Ok(a), Ok(b)) => assert_eq!(a, b),
1766                        (
1767                            Err(CryptoError::EmptyProfile {
1768                                profile: p,
1769                                options: o,
1770                                category: f,
1771                            }),
1772                            Err(CryptoError::EmptyProfile {
1773                                profile: ep,
1774                                options: eo,
1775                                category: ef,
1776                            }),
1777                        ) => {
1778                            assert_eq!((p, f), (ep, ef));
1779                            assert_eq!((o, eo), (on, off));
1780                        }
1781                        (a, b) => panic!("{a:?} != {b:?}"),
1782                    }
1783                }
1784            }
1785        }
1786    }
1787
1788    #[test]
1789    fn select_admitted_never_refuses_an_empty_family() {
1790        let spec = ProfileSpec {
1791            macs: &[],
1792            kdfs: &[],
1793            aeads: &[],
1794            hashes: &[],
1795            signatures: &[],
1796            ..BSI
1797        };
1798        assert_eq!(
1799            select_admitted_with(Profile::Bsi, &spec, Options::default(), |_| true),
1800            AdmittedSelection {
1801                macs: vec![],
1802                kdfs: vec![],
1803                key_agreements: vec![],
1804                aeads: vec![],
1805                hashes: vec![],
1806                signatures: vec![],
1807                excluded: vec![],
1808            }
1809        );
1810    }
1811
1812    #[test]
1813    fn select_admitted_records_what_the_module_lacks() {
1814        use AeadAlgorithm as D;
1815        use HashAlgorithm as H;
1816        use KdfAlgorithm as K;
1817        use KeyAgreementAlgorithm as E;
1818        use MacAlgorithm as M;
1819        let lacking = [
1820            Algorithm::Mac(M::HmacSha384),
1821            Algorithm::Kdf(K::HkdfSha512),
1822            Algorithm::Kdf(K::OneStepSha384),
1823            Algorithm::KeyAgreement(E::EcdhP521),
1824            Algorithm::Aead(D::Aes128Gcm),
1825            Algorithm::Hash(H::Sha384),
1826            Algorithm::Hash(H::Sha3_512),
1827            Algorithm::Signature(S::MlDsa87),
1828            Algorithm::Signature(S::Rsa3072Sha384Pss),
1829        ];
1830        assert_eq!(
1831            select_admitted_with(Profile::Fips1403, &FIPS1403, Options::default(), |a| {
1832                !lacking.contains(&a)
1833            }),
1834            AdmittedSelection {
1835                macs: vec![M::HmacSha512, M::HmacSha256],
1836                kdfs: vec![
1837                    K::HkdfSha384,
1838                    K::HkdfSha256,
1839                    K::OneStepSha512,
1840                    K::OneStepSha256
1841                ],
1842                key_agreements: vec![E::EcdhP384, E::EcdhP256],
1843                aeads: vec![D::Aes256Gcm],
1844                hashes: vec![H::Sha512, H::Sha256, H::Sha3_384, H::Sha3_256],
1845                signatures: vec![
1846                    S::EcdsaP384Sha384,
1847                    S::EcdsaP521Sha512,
1848                    S::EcdsaP256Sha256,
1849                    S::Ed25519,
1850                    S::Rsa4096Sha384Pss,
1851                    S::Rsa2048Sha384Pss,
1852                    S::Rsa4096Sha384,
1853                    S::Rsa3072Sha384,
1854                    S::Rsa2048Sha384,
1855                ],
1856                excluded: lacking.to_vec(),
1857            }
1858        );
1859    }
1860
1861    fn strength_bits(k: KeyAgreementAlgorithm) -> usize {
1862        match k {
1863            KeyAgreementAlgorithm::EcdhP256 | KeyAgreementAlgorithm::X25519 => 128,
1864            KeyAgreementAlgorithm::EcdhP384 => 192,
1865            KeyAgreementAlgorithm::EcdhP521 => 256,
1866        }
1867    }
1868
1869    #[test]
1870    fn every_admitted_curve_and_kdf_pair_meets_sp_800_56c_section_7() {
1871        let on = Options {
1872            cnsa1_unclassified_nss: true,
1873            ..Options::default()
1874        };
1875        let mut pairs = 0;
1876        for &p in Profile::ALL {
1877            for o in [Options::default(), on] {
1878                let s = select_admitted(p, o, M4S);
1879                for &k in &s.key_agreements {
1880                    for &d in &s.kdfs {
1881                        pairs += 1;
1882                        assert!(
1883                            8 * d.hash().output_len() >= strength_bits(k),
1884                            "{p} {o} {k:?} {d:?}"
1885                        );
1886                    }
1887                }
1888            }
1889        }
1890        assert_eq!(pairs, 2 * (4 * 6 + 3 * 6 + 3 * 6 + 3 * 6) + 1 + 2);
1891    }
1892
1893    #[test]
1894    fn the_unclassified_nss_option_adds_hmac_sha256_to_cnsa1_only() {
1895        let on = Options {
1896            cnsa1_unclassified_nss: true,
1897            ..Options::default()
1898        };
1899        for &p in Profile::ALL {
1900            let mut want = select_admitted(p, Options::default(), M4S).macs;
1901            if p == Profile::Cnsa1 {
1902                want.push(MacAlgorithm::HmacSha256);
1903            }
1904            assert_eq!(select_admitted(p, on, M4S).macs, want, "{p}");
1905        }
1906    }
1907
1908    #[test]
1909    fn the_unclassified_nss_option_adds_hkdf_sha256_to_cnsa1_only() {
1910        let on = Options {
1911            cnsa1_unclassified_nss: true,
1912            ..Options::default()
1913        };
1914        for &p in Profile::ALL {
1915            let mut want = select_admitted(p, Options::default(), M4S).kdfs;
1916            if p == Profile::Cnsa1 {
1917                want.push(KdfAlgorithm::HkdfSha256);
1918            }
1919            assert_eq!(select_admitted(p, on, M4S).kdfs, want, "{p}");
1920        }
1921    }
1922
1923    #[test]
1924    fn the_unclassified_nss_option_adds_sha256_hashing_to_cnsa1_only() {
1925        let on = Options {
1926            cnsa1_unclassified_nss: true,
1927            ..Options::default()
1928        };
1929        for &p in Profile::ALL {
1930            let mut want = select_admitted(p, Options::default(), M4S).hashes;
1931            if p == Profile::Cnsa1 {
1932                assert_eq!(want, [HashAlgorithm::Sha384]);
1933                want.push(HashAlgorithm::Sha256);
1934            }
1935            assert_eq!(select_admitted(p, on, M4S).hashes, want, "{p}");
1936            assert_eq!(select_admitted(p, on, M3S).hashes, want, "{p}");
1937        }
1938    }
1939
1940    #[test]
1941    fn the_unclassified_nss_option_adds_rsa_2048_signatures_to_cnsa1_only() {
1942        let on = Options {
1943            cnsa1_unclassified_nss: true,
1944            ..Options::default()
1945        };
1946        for &p in Profile::ALL {
1947            for m in [M3S, M4S] {
1948                let mut want = select_admitted(p, Options::default(), m).signatures;
1949                if p == Profile::Cnsa1 {
1950                    assert_eq!(
1951                        want,
1952                        [
1953                            S::EcdsaP384Sha384,
1954                            S::Rsa4096Sha384Pss,
1955                            S::Rsa3072Sha384Pss,
1956                            S::Rsa4096Sha384,
1957                            S::Rsa3072Sha384,
1958                        ]
1959                    );
1960                    want.extend([S::Rsa2048Sha384Pss, S::Rsa2048Sha384]);
1961                }
1962                assert_eq!(select_admitted(p, on, m).signatures, want, "{p} {m:?}");
1963            }
1964        }
1965    }
1966
1967    #[test]
1968    fn an_empty_kdf_info_is_admitted_under_unrestricted_only() {
1969        for &p in Profile::ALL {
1970            let may = match p {
1971                Profile::Unrestricted => true,
1972                Profile::Preferred
1973                | Profile::Cnsa2
1974                | Profile::Cnsa1
1975                | Profile::Bsi
1976                | Profile::Fips1403
1977                | Profile::Fips1402 => false,
1978            };
1979            assert_eq!(kdf_info_may_be_empty(p), may, "{p}");
1980        }
1981    }
1982
1983    #[test]
1984    fn mac_keys_are_bounded_by_the_module_and_by_bsi() {
1985        for &p in Profile::ALL {
1986            let floor = match p {
1987                Profile::Bsi => 128,
1988                Profile::Unrestricted
1989                | Profile::Preferred
1990                | Profile::Cnsa2
1991                | Profile::Cnsa1
1992                | Profile::Fips1403
1993                | Profile::Fips1402 => 112,
1994            };
1995            assert_eq!(mac_key_floor_bits(p), floor, "{p}");
1996        }
1997        assert_eq!(MAC_KEY_MAX_BITS, 524_288);
1998    }
1999}