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::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
760pub 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#[derive(Debug, Clone, PartialEq, Eq)]
863#[non_exhaustive]
864pub struct Selection {
865 pub versions: Vec<V>,
867 pub cipher_suites: Vec<C>,
869 pub kx_groups: Vec<K>,
871 pub signature_schemes: Vec<T>,
873 pub key_algorithms: Vec<S>,
875 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
897pub 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#[derive(Debug, Clone, PartialEq, Eq)]
964#[non_exhaustive]
965pub struct AdmittedSelection {
966 pub macs: Vec<MacAlgorithm>,
968 pub kdfs: Vec<KdfAlgorithm>,
970 pub key_agreements: Vec<KeyAgreementAlgorithm>,
972 pub aeads: Vec<AeadAlgorithm>,
974 pub hashes: Vec<HashAlgorithm>,
976 pub signatures: Vec<S>,
978 pub excluded: Vec<Algorithm>,
980}
981
982pub 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
1099pub 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
1114pub 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
1131pub 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}