1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[non_exhaustive]
17pub enum Profile {
18 Unrestricted,
20 Preferred,
22 Cnsa2,
24 Cnsa1,
29 Bsi,
31 Fips1403,
33 Fips1402,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub enum GoverningText {
41 CrateRecommendation,
43 Published {
45 documents: &'static [&'static str],
47 },
48}
49
50impl Profile {
51 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 #[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 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 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#[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
164#[non_exhaustive]
165pub struct Options {
166 pub pq_key_exchange_required: bool,
169 pub peer_certificates_match_profile: bool,
171 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
207#[non_exhaustive]
208pub struct ProfileSpec {
209 pub versions: &'static [V],
211 pub cipher_suites: &'static [C],
213 pub kx_groups: &'static [K],
215 pub signature_schemes: &'static [T],
217 pub key_algorithms: &'static [S],
219 pub macs: &'static [MacAlgorithm],
221 pub kdfs: &'static [KdfAlgorithm],
223 pub key_agreements: &'static [KeyAgreementAlgorithm],
225 pub aeads: &'static [AeadAlgorithm],
227 pub hashes: &'static [HashAlgorithm],
229 pub signatures: &'static [S],
231}
232
233impl Profile {
234 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
248pub 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
356pub 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
468pub 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
504pub 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
560pub 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
631pub 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
762pub 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#[derive(Debug, Clone, PartialEq, Eq)]
865#[non_exhaustive]
866pub struct Selection {
867 pub versions: Vec<V>,
869 pub cipher_suites: Vec<C>,
871 pub kx_groups: Vec<K>,
873 pub signature_schemes: Vec<T>,
875 pub key_algorithms: Vec<S>,
877 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
899pub 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#[derive(Debug, Clone, PartialEq, Eq)]
966#[non_exhaustive]
967pub struct AdmittedSelection {
968 pub macs: Vec<MacAlgorithm>,
970 pub kdfs: Vec<KdfAlgorithm>,
972 pub key_agreements: Vec<KeyAgreementAlgorithm>,
974 pub aeads: Vec<AeadAlgorithm>,
976 pub hashes: Vec<HashAlgorithm>,
978 pub signatures: Vec<S>,
980 pub excluded: Vec<Algorithm>,
982}
983
984pub 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
1101pub 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
1116pub 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
1133pub 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}