crypto-profile 0.10.2

Named cryptographic profiles over the AWS-LC FIPS module: algorithm types, per-module capabilities and a conformance suite
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
//! Named profiles: algorithm sets a backend resolves into a TLS policy and the key algorithms it
//! admits. A profile is an algorithm set, not a validation claim; whether the running module is
//! validated for it is a separate check.
use crate::algorithm::{
    AeadAlgorithm, Algorithm, CipherSuite as C, HashAlgorithm, KdfAlgorithm, KeyAgreementAlgorithm,
    KxGroup as K, MacAlgorithm, SignatureAlgorithm as S, TlsSignatureScheme as T, TlsVersion as V,
};
use crate::capability::{Module, capability};
use crate::error::{CryptoError, Family};
use std::fmt;
use std::str::FromStr;

/// A named profile. Each one that is named for a regime follows that regime's published text, in
/// the edition [`Profile::governing_text`] names, and changes only when a new edition is published.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Profile {
    /// Every algorithm in the inventory, post-quantum key exchange first. Not a government profile.
    Unrestricted,
    /// The crate's recommendation. Not a government profile; see `docs/profiles.md`.
    Preferred,
    /// The CNSA 2.0 algorithm set of CNSSP 15 (December 2024), Annex B, on TLS 1.3.
    Cnsa2,
    /// The CNSA 1.0 algorithm set of CNSSP 15 (20 October 2016), Annex B, in RFC 9151's TLS
    /// profile. A transition profile for CNSA 1.0 counterparts. Not every RFC 9151 requirement is
    /// met: `docs/profiles.md` names the algorithm gaps (the `cnsa1` entry in section 3, sections 4
    /// and 5), and the RFC 8603 certificate profile that §5.4 requires is not implemented.
    Cnsa1,
    /// BSI TR-02102-2 and TR-02102-1, version 2026-01.
    Bsi,
    /// What a FIPS 140-3 boundary approves under the published texts.
    Fips1403,
    /// What a FIPS 140-2 boundary approves under the published texts.
    Fips1402,
}

/// The text a profile follows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GoverningText {
    /// The crate's own recommendation, following no published text.
    CrateRecommendation,
    /// Published documents, each with its edition.
    Published {
        /// The documents, each naming its edition.
        documents: &'static [&'static str],
    },
}

impl Profile {
    /// Every profile.
    pub const ALL: &'static [Profile] = &[
        Profile::Unrestricted,
        Profile::Preferred,
        Profile::Cnsa2,
        Profile::Cnsa1,
        Profile::Bsi,
        Profile::Fips1403,
        Profile::Fips1402,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            Profile::Unrestricted => 0,
            Profile::Preferred => 1,
            Profile::Cnsa2 => 2,
            Profile::Cnsa1 => 3,
            Profile::Bsi => 4,
            Profile::Fips1403 => 5,
            Profile::Fips1402 => 6,
        }
    }

    /// The profile's stable configuration name.
    pub const fn as_str(self) -> &'static str {
        match self {
            Profile::Unrestricted => "unrestricted",
            Profile::Preferred => "preferred",
            Profile::Cnsa2 => "cnsa2",
            Profile::Cnsa1 => "cnsa1",
            Profile::Bsi => "bsi",
            Profile::Fips1403 => "fips1403",
            Profile::Fips1402 => "fips1402",
        }
    }

    /// The texts the profile follows, with each document's edition. The profile constant's doc
    /// comment also cites the standards and implementation guidance these texts rely on.
    pub const fn governing_text(self) -> GoverningText {
        match self {
            Profile::Unrestricted | Profile::Preferred => GoverningText::CrateRecommendation,
            Profile::Cnsa2 => GoverningText::Published {
                documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
            },
            Profile::Cnsa1 => GoverningText::Published {
                documents: &[
                    "CNSSP 15 (20 October 2016), Annex B, p. B-1",
                    "RFC 9151 (April 2022)",
                ],
            },
            Profile::Bsi => GoverningText::Published {
                documents: &[
                    "BSI TR-02102-2, version 2026-01",
                    "BSI TR-02102-1, version 2026-01",
                ],
            },
            Profile::Fips1403 => GoverningText::Published {
                documents: &[
                    "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
                    "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
                    "NIST SP 800-52r2",
                    "NIST SP 800-131A r2",
                ],
            },
            Profile::Fips1402 => GoverningText::Published {
                documents: &[
                    "FIPS 140-2 Annex A and Annex D (2021)",
                    "FIPS 186-4",
                    "NIST SP 800-131A r2",
                    "NIST SP 800-52r2",
                ],
            },
        }
    }
}

impl fmt::Display for Profile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A name that is not one of the crate's profiles. Names are matched exactly: no trimming and no
/// case folding.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseProfileError(pub String);

impl fmt::Display for ParseProfileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "unknown profile {:?}", self.0)
    }
}

impl std::error::Error for ParseProfileError {}

impl FromStr for Profile {
    type Err = ParseProfileError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Profile::ALL
            .iter()
            .copied()
            .find(|p| p.as_str() == s)
            .ok_or_else(|| ParseProfileError(s.to_string()))
    }
}

/// Options applied on top of any profile. The default changes nothing; `cnsa1_unclassified_nss`
/// widens `cnsa1`'s MACs, HKDFs, hashes and signatures, and the others narrow. Outside this crate,
/// build it from `Options::default()` and set the fields you need.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub struct Options {
    /// Keep only post-quantum key exchange (pure ML-KEM and hybrid groups), and so remove TLS 1.2,
    /// which has none. Key algorithms and signatures are not narrowed.
    pub pq_key_exchange_required: bool,
    /// Require peer leaf keys and certification paths to match the profile. Off by default.
    pub peer_certificates_match_profile: bool,
    /// Admit HMAC-SHA256, HKDF-SHA256 and SHA-256 hashing under `cnsa1`, as CNSSP 15 (20 October
    /// 2016) Annex B ¶2b (p. B-2) allows SHA-256 in the near term for UNCLASSIFIED NSS data and
    /// community-of-interest separation only, and 2048-bit RSA signatures with SHA-384 (PSS and
    /// PKCS#1 v1.5), as ¶2b allows RSA "at the 2048 bit level". Ignored by every other profile, and
    /// by every family but message authentication, HKDF, hashing and signatures outside TLS. It
    /// admits no one-step KDF and no curve but P-384, for key agreement or ECDSA, and does not
    /// widen `cnsa1`'s AEADs beyond AES-256-GCM or its TLS key algorithms. Off by default.
    pub cnsa1_unclassified_nss: bool,
}

impl fmt::Display for Options {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let names: Vec<&str> = [
            (
                self.pq_key_exchange_required,
                "post-quantum key exchange required",
            ),
            (
                self.peer_certificates_match_profile,
                "peer certificates match profile",
            ),
            (self.cnsa1_unclassified_nss, "cnsa1 UNCLASSIFIED NSS"),
        ]
        .into_iter()
        .filter_map(|(on, name)| on.then_some(name))
        .collect();
        if names.is_empty() {
            f.write_str("no options")
        } else {
            f.write_str(&names.join(", "))
        }
    }
}

/// A profile's contents, each list in preference order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProfileSpec {
    /// Protocol versions.
    pub versions: &'static [V],
    /// Cipher suites.
    pub cipher_suites: &'static [C],
    /// Key-exchange groups.
    pub kx_groups: &'static [K],
    /// Handshake signature schemes accepted from the peer.
    pub signature_schemes: &'static [T],
    /// TLS certificate key algorithms, for `resolve` and `Resolved::admit_key`.
    pub key_algorithms: &'static [S],
    /// Message authentication codes, for `hmac`; admitted through `admit`, not `resolve`.
    pub macs: &'static [MacAlgorithm],
    /// Key-derivation functions, for `kdf`; admitted through `admit`, not `resolve`.
    pub kdfs: &'static [KdfAlgorithm],
    /// Key agreement outside TLS, for `ecdh`; admitted through `admit`, not `resolve`.
    pub key_agreements: &'static [KeyAgreementAlgorithm],
    /// Authenticated encryption outside TLS, for `aead`; admitted through `admit`, not `resolve`.
    pub aeads: &'static [AeadAlgorithm],
    /// Hash functions outside TLS, for `digest`; admitted through `admit`, not `resolve`.
    pub hashes: &'static [HashAlgorithm],
    /// Signature algorithms outside TLS, for `signature`; admitted through `admit`, not `resolve`.
    pub signatures: &'static [S],
}

impl Profile {
    /// The profile's contents. Each entry's source is cited on the constant it comes from.
    pub const fn spec(self) -> &'static ProfileSpec {
        match self {
            Profile::Unrestricted => &UNRESTRICTED,
            Profile::Preferred => &PREFERRED,
            Profile::Cnsa2 => &CNSA2,
            Profile::Cnsa1 => &CNSA1,
            Profile::Bsi => &BSI,
            Profile::Fips1403 => &FIPS1403,
            Profile::Fips1402 => &FIPS1402,
        }
    }
}

/// `unrestricted`: the crate's own set, following no published text. Not a government profile.
///
/// - Versions: TLS 1.3, TLS 1.2. Suites: all nine. Groups: all seven. Schemes: all eleven
///   (`crypto_profile::algorithm`, each family's `ALL`). Keys: the nine TLS key algorithms, every
///   `SignatureAlgorithm` but the RSA-PSS variants.
/// - MACs: every HMAC in the inventory.
/// - KDFs: every HKDF and one-step KDF in the inventory.
/// - Key agreement: ECDH on P-384, P-521 and P-256, and X25519: every curve the backends implement.
/// - AEADs: AES-256-GCM, AES-128-GCM and ChaCha20-Poly1305: every AEAD the backends implement.
/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384 and SHA3-256: every hash the backends
///   implement.
/// - Signatures: ML-DSA-87; ECDSA on P-384, on P-521 with SHA-512 and with SHA-384, and on P-256;
///   Ed25519; RSA 4096, 3072 and 2048 with PSS, then with PKCS#1 v1.5: every signature algorithm
///   the backends implement.
pub const UNRESTRICTED: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls13Chacha20Poly1305Sha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
        C::Tls12EcdheEcdsaChacha20Poly1305Sha256,
        C::Tls12EcdheRsaChacha20Poly1305Sha256,
    ],
    kx_groups: &[
        K::MlKem1024,
        K::Secp384r1MlKem1024,
        K::X25519MlKem768,
        K::Secp256r1MlKem768,
        K::Secp384r1,
        K::Secp256r1,
        K::X25519,
    ],
    signature_schemes: &[
        T::MlDsa87,
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::Ed25519,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
        T::RsaPkcs1Sha384,
        T::RsaPkcs1Sha512,
        T::RsaPkcs1Sha256,
    ],
    key_algorithms: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Ed25519,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
        S::Rsa2048Sha384,
    ],
    macs: &[
        MacAlgorithm::HmacSha512,
        MacAlgorithm::HmacSha384,
        MacAlgorithm::HmacSha256,
    ],
    kdfs: &[
        KdfAlgorithm::HkdfSha512,
        KdfAlgorithm::HkdfSha384,
        KdfAlgorithm::HkdfSha256,
        KdfAlgorithm::OneStepSha512,
        KdfAlgorithm::OneStepSha384,
        KdfAlgorithm::OneStepSha256,
    ],
    key_agreements: &[
        KeyAgreementAlgorithm::EcdhP384,
        KeyAgreementAlgorithm::EcdhP521,
        KeyAgreementAlgorithm::EcdhP256,
        KeyAgreementAlgorithm::X25519,
    ],
    aeads: &[
        AeadAlgorithm::Aes256Gcm,
        AeadAlgorithm::Aes128Gcm,
        AeadAlgorithm::Chacha20Poly1305,
    ],
    hashes: &[
        HashAlgorithm::Sha512,
        HashAlgorithm::Sha384,
        HashAlgorithm::Sha256,
        HashAlgorithm::Sha3_512,
        HashAlgorithm::Sha3_384,
        HashAlgorithm::Sha3_256,
    ],
    signatures: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::EcdsaP256Sha256,
        S::Ed25519,
        S::Rsa4096Sha384Pss,
        S::Rsa3072Sha384Pss,
        S::Rsa2048Sha384Pss,
        S::Rsa4096Sha384,
        S::Rsa3072Sha384,
        S::Rsa2048Sha384,
    ],
};

/// `preferred`: the crate's recommendation. Not a government profile; see `docs/profiles.md`.
///
/// "ISM" is ASD, *Information security manual, Guidelines for cryptography*, September 2026;
/// SECRET-level controls ISM-1761 and ISM-1763 (p. 8), ISM-1765 (p. 10), ISM-1769 (p. 11).
/// - TLS 1.3 only: ISM-1139, p. 19.
/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: ISM-1369, p. 20; ISM-1769, p. 11.
/// - ML-KEM-1024: ISM-1995, p. 10.
/// - SecP384r1MLKEM1024, X25519MLKEM768, SecP256r1MLKEM768: ISM p. 12; ISM-1996, p. 13; FIPS 140-3 IG D.S
///   Scenario 2 and Additional Comment 8a (August 2026, pp. 223-226), which names all three.
/// - secp384r1, secp256r1: ISM-0474 and ISM-1761, p. 8; ISM-1446, p. 7.
/// - X25519 on its own excluded: FIPS 140-3 IG C.K Resolution 5 (p. 160).
/// - ChaCha20-Poly1305 excluded: ISM p. 6; SP 800-140Cr2 §6.1.
/// - Schemes ML-DSA-87 (ISM-1991, p. 9), ECDSA P-384, P-256, P-521 (ISM-1763, p. 8), RSA-PSS
///   with SHA-2 (ISM-1765, p. 10; ISM-1374, p. 20).
/// - Ed25519 excluded: ISM pp. 5-6.
/// - RSA PKCS#1 v1.5 schemes excluded: RFC 8446 §4.2.3.
/// - Keys ML-DSA-87 (ISM-1991, p. 9), ECDSA P-384, P-256, P-521 (ISM-1763, p. 8), RSA 3072 and
///   4096 (ISM-1765, p. 10); RSA 2048 excluded: ISM-1765, p. 10.
/// - ECDSA P-521 with SHA-384: ISM-1763, p. 8; SHA-2 output of at least 256 bits, ISM-1767,
///   p. 11.
/// - MACs: HMAC with SHA-2 for IPsec and TLS, ISM-1772 (p. 15), ISM-0998 (p. 16) and ISM-1375
///   (p. 20); SHA-2 output of at least 224 bits, ISM-1766 (p. 11). HMAC-SHA256 "will not be
///   approved beyond 2030" (pp. 15-16).
/// - KDFs: HKDF, the two-step method of SP 800-56C Rev. 2 §5, of which RFC 5869 "specifies a
///   version" (SP 800-56C Rev. 2, p. 20); the ISM cites SP 800-56C Rev. 2 for combining hybrid
///   key-encapsulation components (p. 13). SHA-2 output of at least 224 bits, ISM-1766 (p. 11);
///   SHA-256 "will not be approved beyond 2030" (p. 11). Decision 0010. The one-step KDF of SP
///   800-56C Rev. 2 §4, under the same reading of that citation (decision 0011).
/// - Key agreement: ECDH on P-384, P-521 and P-256, "preferably the NIST P-384 curve", ISM-1761
///   (p. 8). X25519 excluded: ISM-1761 names only the NIST curves. ECDH "will not be approved
///   beyond 2030" (p. 8).
/// - AEADs: AES-256-GCM, AES-128-GCM. "The only approved symmetric cryptographic algorithm is
///   ... AES" (p. 6); AES-128, AES-192 or AES-256, "preferably AES-256", ISM-1769 (p. 11); GCM,
///   named on p. 11, preferred by ISM-1771 (p. 15) and ISM-0998 (p. 16), and required for TLS by ISM-1369 (p. 20).
///   AES-128 "will not be approved beyond 2030" (p. 11). ChaCha20-Poly1305 excluded: p. 6 and
///   ISM-0471 (p. 7).
/// - Hashes: SHA-512, SHA-384, SHA-256. "The only approved hashing algorithm for general-purpose
///   use is Secure Hashing Algorithm 2 (SHA-2)" (p. 6); ISM-1767 (p. 11), an output size of at
///   least 256 bits, "preferably SHA-384 or SHA-512". SHA-256 "will not be approved beyond 2030"
///   (p. 11). SHA-3 excluded: "approved exclusively for use within ML-DSA and ML-KEM" (p. 6),
///   "restricted to use within internal steps of ML-DSA and ML-KEM" (p. 11).
/// - Signatures: ML-DSA-87, "preferably ML-DSA-87", ISM-1991 (p. 9); ECDSA on P-384, P-521 and
///   P-256, ISM-1763 (p. 8); RSA 4096 and 3072 with PSS, then with PKCS#1 v1.5, ISM-1765 (p. 10),
///   which names no padding. Ed25519 excluded: ISM pp. 5-6. RSA 2048 excluded: ISM-1765 (p. 10).
///   ECDSA P-521 with SHA-384 excluded: FIPS 186-5 §6.1.1 (p. 21), a hash weaker than the curve
///   "shall not be used" (decision 0015). ECDSA and RSA "will not be approved beyond 2030" (pp. 8
///   and 10).
pub const PREFERRED: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13],
    cipher_suites: &[C::Tls13Aes256GcmSha384, C::Tls13Aes128GcmSha256],
    kx_groups: &[
        K::MlKem1024,
        K::Secp384r1MlKem1024,
        K::X25519MlKem768,
        K::Secp256r1MlKem768,
        K::Secp384r1,
        K::Secp256r1,
    ],
    signature_schemes: &[
        T::MlDsa87,
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
    ],
    key_algorithms: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
    ],
    macs: &[
        MacAlgorithm::HmacSha512,
        MacAlgorithm::HmacSha384,
        MacAlgorithm::HmacSha256,
    ],
    kdfs: &[
        KdfAlgorithm::HkdfSha512,
        KdfAlgorithm::HkdfSha384,
        KdfAlgorithm::HkdfSha256,
        KdfAlgorithm::OneStepSha512,
        KdfAlgorithm::OneStepSha384,
        KdfAlgorithm::OneStepSha256,
    ],
    key_agreements: &[
        KeyAgreementAlgorithm::EcdhP384,
        KeyAgreementAlgorithm::EcdhP521,
        KeyAgreementAlgorithm::EcdhP256,
    ],
    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
    hashes: &[
        HashAlgorithm::Sha512,
        HashAlgorithm::Sha384,
        HashAlgorithm::Sha256,
    ],
    signatures: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP521Sha512,
        S::EcdsaP256Sha256,
        S::Rsa4096Sha384Pss,
        S::Rsa3072Sha384Pss,
        S::Rsa4096Sha384,
        S::Rsa3072Sha384,
    ],
};

/// `cnsa2`: the CNSA 2.0 algorithm set of CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2, on
/// TLS 1.3. No CNSA 2.0 TLS profile is published: CNSSP 15 ¶8a, footnote 1 (p. 5).
/// - ML-KEM-1024: Annex B ¶1 table, p. B-1.
/// - ML-DSA-87 scheme and key: Annex B ¶1 table, p. B-1.
/// - TLS_AES_256_GCM_SHA384: Annex B ¶1 table, p. B-1.
/// - TLS 1.3 only: ML-KEM-1024 is the set's only key establishment (Annex B ¶1 table, p. B-1);
///   TLS 1.2 key exchange is ECDHE or FFDHE (RFC 8422, RFC 7919).
/// - MACs: hash "SHA-384 or SHA-512", Annex B p. B-1; protocol guidance RFC 9206 and RFC 9151,
///   ¶8a and fn. 1 (p. 5); RFC 9206 §4 (SHA-384 "for all purposes") and §8 (PRF_HMAC_SHA2_384 or
///   PRF_HMAC_SHA2_512 only).
/// - KDFs: CNSSP 15 names no KDF. ¶8a (p. 5) names RFC 9151, whose §4 approves SHA-384 for HKDF
///   in TLS 1.3; Annex B (p. B-1) admits SHA-384 and SHA-512. Decision 0010. No one-step KDF:
///   RFC 9151 §4 reaches HKDF only.
/// - No key agreement outside TLS: Annex B ¶1 table (p. B-1) lists ML-KEM-1024 as the only key
///   establishment.
/// - AEADs: AES-256-GCM. Annex B ¶1 table (p. B-1), AES "Use 256 bit keys"; the mode through
///   ¶8a and fn. 1 (p. 5): RFC 9151 §4 (AES-256 in GCM) and RFC 9206.
/// - Hashes: SHA-384, SHA-512, in the MAC and KDF order. Annex B ¶1 table (p. B-1), "Use SHA-384 or
///   SHA-512 to protect up to TOP SECRET"; Annex B governs over RFC 9206 §4's SHA-384 only, which
///   fn. 1 (p. 5) says "will need to be updated for CNSA 2.0". SHA3 excluded: SHA3-384 or SHA3-512
///   "allowed for internal hardware functionality (e.g. boot-up integrity checks) only" (p. B-2).
/// - Signatures: ML-DSA-87, Annex B ¶1 table (p. B-1), "ML-DSA-87 to protect up to TOP SECRET".
pub const CNSA2: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13],
    cipher_suites: &[C::Tls13Aes256GcmSha384],
    kx_groups: &[K::MlKem1024],
    signature_schemes: &[T::MlDsa87],
    key_algorithms: &[S::MlDsa87],
    macs: &[MacAlgorithm::HmacSha384, MacAlgorithm::HmacSha512],
    kdfs: &[KdfAlgorithm::HkdfSha384, KdfAlgorithm::HkdfSha512],
    key_agreements: &[],
    aeads: &[AeadAlgorithm::Aes256Gcm],
    hashes: &[HashAlgorithm::Sha384, HashAlgorithm::Sha512],
    signatures: &[S::MlDsa87],
};

/// `cnsa1`: the CNSA 1.0 algorithm set of CNSSP 15 (20 October 2016), Annex B (p. B-1), in the
/// TLS 1.2 and 1.3 profile of RFC 9151 (April 2022). Transition dates: CNSSP 15 (December 2024)
/// ¶18 (pp. 7-8).
/// - AES-256, ECDH and ECDSA on P-384, SHA-384, RSA of at least 3072 bits: CNSSP 15 (2016)
///   Annex B ¶1 table, p. B-1.
/// - TLS 1.3 and 1.2: RFC 9151 §5. The § references below are RFC 9151's.
/// - TLS_AES_256_GCM_SHA384: §7.
/// - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: §6.
/// - secp384r1: §5.1, §7.
/// - ecdsa_secp384r1_sha384: §6.2, §7.1. rsa_pss_rsae_sha384: §6.2, §7.1. rsa_pkcs1_sha384:
///   §6.2; TLS 1.2 only, §7.1.
/// - Keys ECDSA P-384 with SHA-384 (§5.1); RSA 3072 and 4096 (§5.2).
/// - Not implemented by the stack: TLS_RSA_WITH_AES_256_GCM_SHA384 and
///   TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 (§6), ffdhe3072 and ffdhe4096 (§5.3, §7).
/// - MACs: hash "Use SHA-384", Annex B p. B-1; RFC 9151 §4 (SHA-384 for the TLS 1.2 PRF and the
///   TLS 1.3 HKDF). `Options::cnsa1_unclassified_nss` adds HMAC-SHA256 under ¶2b (p. B-2).
/// - KDFs: hash "Use SHA-384", Annex B p. B-1; RFC 9151 §4 (HKDF with SHA-384 in TLS 1.3).
///   `Options::cnsa1_unclassified_nss` adds HKDF-SHA256 under ¶2b (p. B-2). No one-step KDF:
///   RFC 9151 §4 reaches HKDF only.
/// - Key agreement: ECDH on P-384, Annex B ¶1 table (p. B-1). `Options::cnsa1_unclassified_nss`
///   does not widen it: ¶2b (p. B-2) requires NSA consultation for any other curve.
/// - AEADs: AES-256-GCM. Annex B ¶1 table (p. B-1), AES "Use 256 bit keys"; RFC 9151 §4 (AES-256
///   in GCM). `Options::cnsa1_unclassified_nss` does not widen it: ¶2b (p. B-2) names RSA, DH
///   and SHA-256 only.
/// - Hashes: SHA-384. Annex B ¶1 table (p. B-1), "Use SHA-384 to protect up to TOP SECRET".
///   `Options::cnsa1_unclassified_nss` adds SHA-256 under ¶2b (p. B-2), "SHA-256 in the near
///   term". SHA-512 and SHA3 excluded: the edition names neither (decision 0008).
/// - Signatures: ECDSA P-384 with SHA-384, then RSA 4096 and 3072 with PSS, then with PKCS#1 v1.5.
///   Annex B ¶1 table (p. B-1): ECDSA "Use Curve P-384", RSA "Minimum 3072 bit-modulus", both
///   under FIPS 186-4, whose §5.5 (p. 24) approves both paddings. `Options::cnsa1_unclassified_nss`
///   adds RSA 2048 with PSS and with PKCS#1 v1.5 under ¶2b (p. B-2), "RSA … at the 2048 bit level";
///   not ECDSA P-256: ¶2b requires NSA consultation for curves other than P-384.
pub const CNSA1: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
    ],
    kx_groups: &[K::Secp384r1],
    signature_schemes: &[T::EcdsaNistp384Sha384, T::RsaPssSha384, T::RsaPkcs1Sha384],
    key_algorithms: &[S::EcdsaP384Sha384, S::Rsa3072Sha384, S::Rsa4096Sha384],
    macs: &[MacAlgorithm::HmacSha384],
    kdfs: &[KdfAlgorithm::HkdfSha384],
    key_agreements: &[KeyAgreementAlgorithm::EcdhP384],
    aeads: &[AeadAlgorithm::Aes256Gcm],
    hashes: &[HashAlgorithm::Sha384],
    signatures: &[
        S::EcdsaP384Sha384,
        S::Rsa4096Sha384Pss,
        S::Rsa3072Sha384Pss,
        S::Rsa4096Sha384,
        S::Rsa3072Sha384,
    ],
};

/// `bsi`: BSI TR-02102-2 (TLS) and TR-02102-1, version 2026-01.
/// - TLS 1.3 and TLS 1.2: TR-02102-2 §3.2, Table 2.
/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: TR-02102-2 §3.4.4, Table 13.
/// - ECDHE-ECDSA and ECDHE-RSA with AES-128-GCM and AES-256-GCM: TR-02102-2 §3.3.1.1, Table 3.
/// - secp384r1, secp256r1: TR-02102-2 Table 10 (TLS 1.3) and Table 6 (TLS 1.2).
/// - ecdsa_secp384r1_sha384, ecdsa_secp256r1_sha256, ecdsa_secp521r1_sha512 and rsa_pss_rsae
///   with SHA-384, 512, 256: TR-02102-2 §3.4.3, Table 11; for TLS 1.2, the Table 7 note.
/// - rsa_pkcs1_* excluded: TR-02102-2 Table 7 and Table 12 notes.
/// - Keys ECDSA P-384/SHA-384, P-256/SHA-256, P-521/SHA-512: TR-02102-2 Table 12 and Table 14.
/// - RSA keys excluded: TR-02102-2 Table 12.
/// - ML-DSA excluded: TR-02102-1 §5.3.4.
/// - Hybrid groups excluded: TR-02102-2 Table 10 note.
/// - MACs: HMAC with a Table 4.1 hash (TR-02102-1 §5.2, pp. 49-50; Table 4.1, p. 47).
/// - KDFs: HKDF (RFC 5869) and the SP 800-56C two-step KDF, TR-02102-1 Table B.1 and §B.1.1
///   (p. 73), with a Table 4.1 hash (p. 47). The one-step KDF is not among them.
/// - No key agreement outside TLS: TR-02102-1 Table B.3 (p. 75) recommends only Brainpool curves,
///   which no backend implements.
/// - AEADs: AES-256-GCM, AES-128-GCM. TR-02102-1 Table 3.1 (p. 40), Table 3.2 (p. 42), §3.1.2
///   (pp. 42-43). ChaCha20-Poly1305 excluded: §3.2 (p. 44), "no dedicated stream ciphers are
///   recommended".
/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. TR-02102-1 Table 4.1
///   (p. 47), "Recommended hash functions".
/// - Signatures: RSA 4096 and 3072 with PSS. TR-02102-1 Table 5.4 (p. 52), EMSA-PSS; §5.3.1
///   (p. 52), a modulus of "at least 3000 bits". PKCS#1 v1.5 excluded: §1.5 (p. 22), "not
///   recommended to use this format in new systems, neither for encryption nor for signature
///   generation". ECDSA excluded: Table B.3 (p. 75) recommends only Brainpool curves, which no
///   backend implements. ML-DSA excluded: §5.3.4 (p. 54), only "in combination with a classic
///   signature scheme", which the crate does not offer. Ed25519 excluded: not in Table 5.3 (p. 51).
pub const BSI: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
    ],
    kx_groups: &[K::Secp384r1, K::Secp256r1],
    signature_schemes: &[
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
    ],
    key_algorithms: &[S::EcdsaP384Sha384, S::EcdsaP256Sha256, S::EcdsaP521Sha512],
    macs: &[
        MacAlgorithm::HmacSha512,
        MacAlgorithm::HmacSha384,
        MacAlgorithm::HmacSha256,
    ],
    kdfs: &[
        KdfAlgorithm::HkdfSha512,
        KdfAlgorithm::HkdfSha384,
        KdfAlgorithm::HkdfSha256,
    ],
    key_agreements: &[],
    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
    hashes: &[
        HashAlgorithm::Sha512,
        HashAlgorithm::Sha384,
        HashAlgorithm::Sha256,
        HashAlgorithm::Sha3_512,
        HashAlgorithm::Sha3_384,
        HashAlgorithm::Sha3_256,
    ],
    signatures: &[S::Rsa4096Sha384Pss, S::Rsa3072Sha384Pss],
};

/// `fips1403`: NIST SP 800-140C r2 and SP 800-140D r2 with the CMVP lists (updated August 21,
/// 2026), the FIPS 140-3 Implementation Guidance (August 19, 2026), SP 800-52r2 and SP 800-131A
/// r2. An algorithm set, not a validation claim.
/// - TLS 1.3 and 1.2: SP 800-52r2 §3.1 (p. 8), §4.1 (p. 33). TLS 1.2 extended master secret
///   (RFC 7627): IG D.Q (pp. 220-221).
/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: SP 800-52r2 §3.3.1.2 (p. 19); CMVP
///   SP 800-140C list §6.2.2.1.
/// - ECDHE-ECDSA and ECDHE-RSA with AES-128-GCM and AES-256-GCM: SP 800-52r2 §3.3.1.1.1-2
///   (pp. 16-17).
/// - ChaCha20-Poly1305 excluded: SP 800-140Cr2 §6.1; SP 800-52r2 §3.3.1.2.
/// - ML-KEM-1024: CMVP SP 800-140D list §6.2.10; IG D.S Scenario 1 (p. 223).
/// - SecP384r1MLKEM1024, X25519MLKEM768, SecP256r1MLKEM768: IG D.S Scenario 2 and Additional
///   Comment 8a (pp. 223-226), the three IANA parameter sets it names.
/// - secp384r1, secp256r1: SP 800-56A r3 Appendix D, Table 24 (p. 132); SP 800-52r2 §3.4.2.2
///   (pp. 24-25).
/// - X25519 on its own excluded: IG C.K Resolution 5 (p. 160).
/// - ECDSA P-256, P-384, P-521 schemes and keys: SP 800-140C list §6.2.3.1; SP 800-186 Table 2
///   (p. 7); SP 800-131A r2 Table 2 (p. 9).
/// - ECDSA P-521 with SHA-384 key, in TLS only: it signs handshakes with ecdsa_secp521r1_sha512,
///   and SHA-384 names only its certificate request's signature; FIPS 186-5 §6.1.1 (p. 21) and
///   §6.4 (pp. 23-24) (decision 0015).
/// - Ed25519 scheme and key excluded: SP 800-52r2 §3.2 (p. 9), Table 3-1 (pp. 10-11) and
///   Table 4-1 (p. 34) name RSA and ECDSA certificates and signatures, not EdDSA (decision 0018).
/// - RSA-PSS and RSA PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-5 §5.4; IG C.F (pp. 142-143);
///   PKCS#1 v1.5 on TLS 1.2 only, RFC 8446 §4.2.3.
/// - RSA 2048, 3072, 4096 keys: FIPS 186-5 §5.1; SP 800-131A r2 Table 2.
/// - ML-DSA-87 scheme and key: SP 800-140C list §6.2.3.1 (FIPS 204).
/// - MACs: FIPS 198-1 with SHA-2; SP 800-131A r2 Table 8 (p. 18) and Table 9 (p. 20).
/// - KDFs: HKDF as the two-step method of SP 800-56C Rev. 2 §5 (pp. 17-20), and the hash-based
///   one-step method of §4; SP 800-131A r2 §6 (p. 12).
/// - Key agreement: ECDH on P-384, P-521 and P-256: SP 800-56A r3 Appendix D, Table 24 (p. 132);
///   IG C.A (p. 135); IG D.F Scenario 2 (pp. 187-188). X25519 excluded: IG C.K Resolution 5
///   (p. 160).
/// - AEADs: AES-256-GCM, AES-128-GCM. CMVP SP 800-140C list §6.2.2.1 (FIPS 197-upd1, SP
///   800-38D), through SP 800-140C r2 §6.2 (p. 2); SP 800-131A r2 Table 1 (p. 7).
///   ChaCha20-Poly1305 excluded: SP 800-140C r2 §6.1 (p. 2) precludes every function not on the
///   §6.2 list.
/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. CMVP SP 800-140C list
///   §6.2.4.1 (FIPS 180-4) and §6.2.4.2 (FIPS 202), through SP 800-140C r2 §6.2 (p. 2); SP
///   800-131A r2 Table 8 (pp. 18-19); IG C.C Resolution 1 (p. 138), SHA-3 "as the standalone
///   functions".
/// - Signatures: ML-DSA-87; ECDSA on P-384, on P-521 with SHA-512, and on P-256; Ed25519; RSA 4096,
///   3072 and 2048 with PSS, then with PKCS#1 v1.5. CMVP SP 800-140C list §6.2.3.1 (FIPS 186-5,
///   FIPS 204), through SP 800-140C r2 §6.2 (p. 2); FIPS 186-5 §5.1 (p. 16) and §5.4 (p. 18),
///   both paddings; SP 800-131A r2 Table 2 (p. 9). ECDSA P-521 with SHA-384 excluded: FIPS 186-5
///   §6.1.1 (p. 21), "A hash function that provides a lower security strength than is associated
///   with the bit length of n shall not be used".
pub const FIPS1403: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
    ],
    kx_groups: &[
        K::MlKem1024,
        K::Secp384r1MlKem1024,
        K::X25519MlKem768,
        K::Secp256r1MlKem768,
        K::Secp384r1,
        K::Secp256r1,
    ],
    signature_schemes: &[
        T::MlDsa87,
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
        T::RsaPkcs1Sha384,
        T::RsaPkcs1Sha512,
        T::RsaPkcs1Sha256,
    ],
    key_algorithms: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
        S::Rsa2048Sha384,
    ],
    macs: &[
        MacAlgorithm::HmacSha512,
        MacAlgorithm::HmacSha384,
        MacAlgorithm::HmacSha256,
    ],
    kdfs: &[
        KdfAlgorithm::HkdfSha512,
        KdfAlgorithm::HkdfSha384,
        KdfAlgorithm::HkdfSha256,
        KdfAlgorithm::OneStepSha512,
        KdfAlgorithm::OneStepSha384,
        KdfAlgorithm::OneStepSha256,
    ],
    key_agreements: &[
        KeyAgreementAlgorithm::EcdhP384,
        KeyAgreementAlgorithm::EcdhP521,
        KeyAgreementAlgorithm::EcdhP256,
    ],
    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
    hashes: &[
        HashAlgorithm::Sha512,
        HashAlgorithm::Sha384,
        HashAlgorithm::Sha256,
        HashAlgorithm::Sha3_512,
        HashAlgorithm::Sha3_384,
        HashAlgorithm::Sha3_256,
    ],
    signatures: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP521Sha512,
        S::EcdsaP256Sha256,
        S::Ed25519,
        S::Rsa4096Sha384Pss,
        S::Rsa3072Sha384Pss,
        S::Rsa2048Sha384Pss,
        S::Rsa4096Sha384,
        S::Rsa3072Sha384,
        S::Rsa2048Sha384,
    ],
};

/// `fips1402`: FIPS 140-2 Annex A and Annex D (2021-10-12 editions), FIPS 186-4, SP 800-131A r2
/// and SP 800-52r2. An algorithm set, not a validation claim.
/// - 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
///   key schedule, Annex D item 11 (RFC 8446 §7.1).
/// - ChaCha20-Poly1305 excluded: Annex A.
/// - secp384r1, secp256r1: Annex D item 3 (SP 800-56A r3), Table 24.
/// - ML-KEM and the hybrids excluded: Annex A; Annex D items 12-13.
/// - X25519 excluded: SP 800-56A r3 Table 24.
/// - ECDSA P-256, P-384, P-521 schemes; RSA-PSS and PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-4
///   (Annex A, p. 2), §5.4-5.5, Appendix D.1.2; SP 800-131A r2 Table 2 (p. 9).
/// - Ed25519 and ML-DSA excluded: Annex A.
/// - ECDSA keys P-384/SHA-384, P-256/SHA-256, P-521/SHA-512, P-521/SHA-384: FIPS 186-4 §6.4
///   (pp. 29-30).
/// - RSA 3072 and 2048 keys: FIPS 186-4 §5.1; RSA 4096: FIPS 140-2 IG A.14 (p. 221).
/// - MACs: FIPS 140-2 Annex A, Message Authentication 3 (p. 3); SP 800-131A r2 Table 9 (p. 20).
/// - KDFs: FIPS 140-2 Annex D (p. 1) items 12 and 13, SP 800-56C Rev. 1 and SP 800-56C (2011),
///   each specifying extraction-then-expansion; the one-step method of SP 800-56C Rev. 1, which
///   IG D.8 Scenario X1 (p. 241) admits for key agreement.
/// - Key agreement: ECDH on P-384, P-521 and P-256: Annex D item 3 (SP 800-56A r3), Table 24;
///   IG D.8 Scenario X1 (p. 241). X25519 excluded: not in Table 24.
/// - AEADs: AES-256-GCM, AES-128-GCM. Annex A (pp. 1, 3): AES, SP 800-38D; IV rules, FIPS 140-2
///   IG A.5. ChaCha20-Poly1305 excluded: Annex A.
/// - Hashes: SHA-512, SHA-384, SHA-256, SHA3-512, SHA3-384, SHA3-256. Annex A (p. 2), FIPS 180-4
///   and FIPS 202; FIPS 140-2 IG A.11 Resolution 1 (p. 216), SHA-3 "as the standalone functions".
/// - Signatures: ECDSA on P-384, on P-521 with SHA-512 and with SHA-384, and on P-256; RSA 4096,
///   3072 and 2048 with PSS, then with PKCS#1 v1.5. Annex A (p. 2), FIPS 186-4: §5.5 (p. 24), both
///   paddings; §6.4 (p. 30), a hash weaker than the curve "ordinarily should not be used"; RSA
///   4096, FIPS 140-2 IG A.14 (p. 221). Ed25519 and ML-DSA excluded: Annex A.
pub const FIPS1402: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
    ],
    kx_groups: &[K::Secp384r1, K::Secp256r1],
    signature_schemes: &[
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
        T::RsaPkcs1Sha384,
        T::RsaPkcs1Sha512,
        T::RsaPkcs1Sha256,
    ],
    key_algorithms: &[
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
        S::Rsa2048Sha384,
    ],
    macs: &[
        MacAlgorithm::HmacSha512,
        MacAlgorithm::HmacSha384,
        MacAlgorithm::HmacSha256,
    ],
    kdfs: &[
        KdfAlgorithm::HkdfSha512,
        KdfAlgorithm::HkdfSha384,
        KdfAlgorithm::HkdfSha256,
        KdfAlgorithm::OneStepSha512,
        KdfAlgorithm::OneStepSha384,
        KdfAlgorithm::OneStepSha256,
    ],
    key_agreements: &[
        KeyAgreementAlgorithm::EcdhP384,
        KeyAgreementAlgorithm::EcdhP521,
        KeyAgreementAlgorithm::EcdhP256,
    ],
    aeads: &[AeadAlgorithm::Aes256Gcm, AeadAlgorithm::Aes128Gcm],
    hashes: &[
        HashAlgorithm::Sha512,
        HashAlgorithm::Sha384,
        HashAlgorithm::Sha256,
        HashAlgorithm::Sha3_512,
        HashAlgorithm::Sha3_384,
        HashAlgorithm::Sha3_256,
    ],
    signatures: &[
        S::EcdsaP384Sha384,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::EcdsaP256Sha256,
        S::Rsa4096Sha384Pss,
        S::Rsa3072Sha384Pss,
        S::Rsa2048Sha384Pss,
        S::Rsa4096Sha384,
        S::Rsa3072Sha384,
        S::Rsa2048Sha384,
    ],
};

/// A profile narrowed by its options, every entry implemented by the module's backend, in the
/// profile's preference order.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Selection {
    /// Protocol versions.
    pub versions: Vec<V>,
    /// Cipher suites, each of a version in `versions`.
    pub cipher_suites: Vec<C>,
    /// Key-exchange groups.
    pub kx_groups: Vec<K>,
    /// Handshake signature schemes.
    pub signature_schemes: Vec<T>,
    /// Key algorithms.
    pub key_algorithms: Vec<S>,
    /// Entries unavailable on the module line after Options, in family and preference order.
    pub excluded: Vec<Algorithm>,
}

const fn is_tls12_only(t: T) -> bool {
    match t {
        T::RsaPkcs1Sha256 | T::RsaPkcs1Sha384 | T::RsaPkcs1Sha512 => true,
        T::EcdsaNistp256Sha256
        | T::EcdsaNistp384Sha384
        | T::EcdsaNistp521Sha512
        | T::Ed25519
        | T::RsaPssSha256
        | T::RsaPssSha384
        | T::RsaPssSha512
        | T::MlDsa87 => false,
    }
}

fn keep<X: Copy>(items: &[X], mut wanted: impl FnMut(X) -> bool) -> Vec<X> {
    items.iter().copied().filter(|x| wanted(*x)).collect()
}

/// The backend-independent half of a backend's `resolve`: `profile`'s lists, narrowed by
/// `options`; `pq_key_exchange_required` also removes TLS 1.2, its suites and the RSA PKCS#1 v1.5
/// handshake schemes. Records entries the module line lacks in [`Selection::excluded`], then
/// refuses a family left empty with [`CryptoError::EmptyProfile`]. Both use versions, suites,
/// groups, schemes and keys order, preserving preference order. Approval is a separate check.
pub fn select(
    profile: Profile,
    options: Options,
    module: Module,
) -> Result<Selection, CryptoError> {
    select_spec(profile, profile.spec(), options, module)
}

fn select_spec(
    profile: Profile,
    spec: &ProfileSpec,
    options: Options,
    module: Module,
) -> Result<Selection, CryptoError> {
    let pq = options.pq_key_exchange_required;
    let versions = keep(spec.versions, |v| !(pq && v == V::Tls12));
    let cipher_suites = keep(spec.cipher_suites, |c| versions.contains(&c.version()));
    let kx_groups = keep(spec.kx_groups, |k| !pq || k.is_post_quantum());
    let signature_schemes = keep(spec.signature_schemes, |t| !(pq && is_tls12_only(t)));
    let key_algorithms = spec.key_algorithms.to_vec();
    let mut excluded = Vec::new();
    let mut available = |a| {
        let on = capability(module, a).available;
        if !on {
            excluded.push(a);
        }
        on
    };
    let versions = keep(&versions, |v| available(Algorithm::Protocol(v)));
    let cipher_suites = keep(&cipher_suites, |c| available(Algorithm::Cipher(c)));
    let kx_groups = keep(&kx_groups, |k| available(Algorithm::Kx(k)));
    let signature_schemes = keep(&signature_schemes, |t| available(Algorithm::Scheme(t)));
    let key_algorithms = keep(&key_algorithms, |s| available(Algorithm::Signature(s)));
    for (empty, category) in [
        (versions.is_empty(), Family::Version),
        (cipher_suites.is_empty(), Family::CipherSuite),
        (kx_groups.is_empty(), Family::KxGroup),
        (signature_schemes.is_empty(), Family::SignatureScheme),
        (key_algorithms.is_empty(), Family::KeyAlgorithm),
    ] {
        if empty {
            return Err(CryptoError::EmptyProfile {
                profile,
                options,
                category,
            });
        }
    }
    Ok(Selection {
        versions,
        cipher_suites,
        kx_groups,
        signature_schemes,
        key_algorithms,
        excluded,
    })
}

/// A profile's non-TLS families under its options, every entry implemented by the module's
/// backend, in preference order. Never refused: an empty family means every constructor of that
/// family refuses under the profile.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct AdmittedSelection {
    /// Message authentication codes.
    pub macs: Vec<MacAlgorithm>,
    /// Key-derivation functions.
    pub kdfs: Vec<KdfAlgorithm>,
    /// Key agreement.
    pub key_agreements: Vec<KeyAgreementAlgorithm>,
    /// Authenticated encryption.
    pub aeads: Vec<AeadAlgorithm>,
    /// Hash functions.
    pub hashes: Vec<HashAlgorithm>,
    /// Signature algorithms.
    pub signatures: Vec<S>,
    /// Entries unavailable on the module line, in family and preference order.
    pub excluded: Vec<Algorithm>,
}

/// The backend-independent half of a backend's `admit`: `profile`'s non-TLS families, under
/// `options`, less what `module` lacks. Independent of [`select`].
pub fn select_admitted(profile: Profile, options: Options, module: Module) -> AdmittedSelection {
    select_admitted_with(profile, profile.spec(), options, |a| {
        capability(module, a).available
    })
}

fn select_admitted_with(
    profile: Profile,
    spec: &ProfileSpec,
    options: Options,
    available: impl Fn(Algorithm) -> bool,
) -> AdmittedSelection {
    let mut macs = spec.macs.to_vec();
    let mut kdfs = spec.kdfs.to_vec();
    let mut hashes = spec.hashes.to_vec();
    let mut signatures = spec.signatures.to_vec();
    if options.cnsa1_unclassified_nss {
        macs.extend_from_slice(unclassified_nss_macs(profile));
        kdfs.extend_from_slice(unclassified_nss_kdfs(profile));
        hashes.extend_from_slice(unclassified_nss_hashes(profile));
        signatures.extend_from_slice(unclassified_nss_signatures(profile));
    }
    let mut key_agreements = spec.key_agreements.to_vec();
    let mut aeads = spec.aeads.to_vec();
    let mut excluded = Vec::new();
    retain_available(&mut macs, Algorithm::Mac, &available, &mut excluded);
    retain_available(&mut kdfs, Algorithm::Kdf, &available, &mut excluded);
    retain_available(
        &mut key_agreements,
        Algorithm::KeyAgreement,
        &available,
        &mut excluded,
    );
    retain_available(&mut aeads, Algorithm::Aead, &available, &mut excluded);
    retain_available(&mut hashes, Algorithm::Hash, &available, &mut excluded);
    retain_available(
        &mut signatures,
        Algorithm::Signature,
        &available,
        &mut excluded,
    );
    AdmittedSelection {
        macs,
        kdfs,
        key_agreements,
        aeads,
        hashes,
        signatures,
        excluded,
    }
}

fn retain_available<X: Copy>(
    xs: &mut Vec<X>,
    tag: fn(X) -> Algorithm,
    available: &impl Fn(Algorithm) -> bool,
    excluded: &mut Vec<Algorithm>,
) {
    xs.retain(|x| {
        let on = available(tag(*x));
        if !on {
            excluded.push(tag(*x));
        }
        on
    });
}

fn unclassified_nss_macs(p: Profile) -> &'static [MacAlgorithm] {
    match p {
        Profile::Cnsa1 => &[MacAlgorithm::HmacSha256],
        Profile::Unrestricted
        | Profile::Preferred
        | Profile::Cnsa2
        | Profile::Bsi
        | Profile::Fips1403
        | Profile::Fips1402 => &[],
    }
}

fn unclassified_nss_kdfs(p: Profile) -> &'static [KdfAlgorithm] {
    match p {
        Profile::Cnsa1 => &[KdfAlgorithm::HkdfSha256],
        Profile::Unrestricted
        | Profile::Preferred
        | Profile::Cnsa2
        | Profile::Bsi
        | Profile::Fips1403
        | Profile::Fips1402 => &[],
    }
}

fn unclassified_nss_hashes(p: Profile) -> &'static [HashAlgorithm] {
    match p {
        Profile::Cnsa1 => &[HashAlgorithm::Sha256],
        Profile::Unrestricted
        | Profile::Preferred
        | Profile::Cnsa2
        | Profile::Bsi
        | Profile::Fips1403
        | Profile::Fips1402 => &[],
    }
}

fn unclassified_nss_signatures(p: Profile) -> &'static [S] {
    match p {
        Profile::Cnsa1 => &[S::Rsa2048Sha384Pss, S::Rsa2048Sha384],
        Profile::Unrestricted
        | Profile::Preferred
        | Profile::Cnsa2
        | Profile::Bsi
        | Profile::Fips1403
        | Profile::Fips1402 => &[],
    }
}

/// Whether `profile` admits an empty HKDF `info`: `unrestricted` only, which makes no approval
/// claim. The module does not mark an HKDF call with an empty `info` approved (AWS-LC
/// `HKDF_verify_service_indicator`), so every other profile refuses one (decision 0010).
pub const fn kdf_info_may_be_empty(profile: Profile) -> bool {
    match profile {
        Profile::Unrestricted => true,
        Profile::Preferred
        | Profile::Cnsa2
        | Profile::Cnsa1
        | Profile::Bsi
        | Profile::Fips1403
        | Profile::Fips1402 => false,
    }
}

/// The shortest HMAC key, in bits, `profile` admits. 112 is the module's approved minimum: SP
/// 800-131A r2 §10 Table 9 (p. 20) disallows shorter keys for MAC generation, and the AWS-LC FIPS
/// 3.1.0 security policies list "Key Length: 112-524288" (Table 5, p. 9). `bsi` takes the 128 of
/// TR-02102-1 Table 5.2 (p. 50). SP 800-107 r1 §5.3.2 (pp. 13-14): a key "shall have a security
/// strength that meets or exceeds the security strength required to protect the data".
pub const fn mac_key_floor_bits(profile: Profile) -> usize {
    match profile {
        Profile::Bsi => 128,
        Profile::Unrestricted
        | Profile::Preferred
        | Profile::Cnsa2
        | Profile::Cnsa1
        | Profile::Fips1403
        | Profile::Fips1402 => 112,
    }
}

/// The longest HMAC key, in bits, the module was tested with: "Key Length: 112-524288" in the
/// AWS-LC FIPS 3.1.0 security policies (Table 5, p. 9) and on CAVP certificate A7855.
pub const MAC_KEY_MAX_BITS: usize = 524_288;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algorithm::*;
    use crate::capability::{Linkage, Module, ModuleLine};
    use crate::error::{CryptoError, Family};

    #[test]
    fn every_profile_lists_its_schemes_in_the_canonical_order() {
        assert_eq!(
            Profile::Unrestricted.spec().signature_schemes.len(),
            TlsSignatureScheme::ALL.len()
        );
        for &p in Profile::ALL {
            let s = p.spec().signature_schemes;
            assert!(
                s.windows(2).all(|w| w[0].preference() < w[1].preference()),
                "{p}"
            );
        }
    }

    const M3S: Module = Module {
        line: ModuleLine::AwsLcFips3,
        linkage: Linkage::Static,
    };
    const M4S: Module = Module {
        line: ModuleLine::AwsLcFips4,
        linkage: Linkage::Static,
    };
    const PQ: Options = Options {
        peer_certificates_match_profile: false,
        pq_key_exchange_required: true,
        cnsa1_unclassified_nss: false,
    };
    const PKCS1: [TlsSignatureScheme; 3] = [
        TlsSignatureScheme::RsaPkcs1Sha256,
        TlsSignatureScheme::RsaPkcs1Sha384,
        TlsSignatureScheme::RsaPkcs1Sha512,
    ];

    fn no_duplicates<X: PartialEq + std::fmt::Debug>(xs: &[X]) {
        for (i, x) in xs.iter().enumerate() {
            assert!(!xs[i + 1..].contains(x), "{x:?} twice");
        }
    }

    #[test]
    fn every_spec_is_well_formed() {
        for &p in Profile::ALL {
            let s = p.spec();
            no_duplicates(s.versions);
            no_duplicates(s.cipher_suites);
            no_duplicates(s.kx_groups);
            no_duplicates(s.signature_schemes);
            no_duplicates(s.key_algorithms);
            no_duplicates(s.macs);
            no_duplicates(s.kdfs);
            no_duplicates(s.key_agreements);
            no_duplicates(s.aeads);
            no_duplicates(s.hashes);
            no_duplicates(s.signatures);
            assert!(!s.versions.is_empty() && !s.kx_groups.is_empty(), "{p}");
            assert!(
                !s.signature_schemes.is_empty() && !s.key_algorithms.is_empty(),
                "{p}"
            );
            assert!(
                s.cipher_suites
                    .iter()
                    .all(|c| s.versions.contains(&c.version())),
                "{p}"
            );
            let has_pkcs1 = s.signature_schemes.iter().any(|t| PKCS1.contains(t));
            assert!(!has_pkcs1 || s.versions.contains(&TlsVersion::Tls12), "{p}");
        }
    }

    #[test]
    fn preferred_excludes_what_its_definition_names() {
        let s = Profile::Preferred.spec();
        assert_eq!(s.versions, &[TlsVersion::Tls13]);
        assert!(!s.kx_groups.contains(&KxGroup::X25519));
        assert!(
            !s.cipher_suites
                .contains(&CipherSuite::Tls13Chacha20Poly1305Sha256)
        );
        assert!(!s.signature_schemes.contains(&TlsSignatureScheme::Ed25519));
        assert!(!s.key_algorithms.contains(&SignatureAlgorithm::Ed25519));
        assert!(!s.aeads.contains(&AeadAlgorithm::Chacha20Poly1305));
        for h in [
            HashAlgorithm::Sha3_256,
            HashAlgorithm::Sha3_384,
            HashAlgorithm::Sha3_512,
        ] {
            assert!(!s.hashes.contains(&h), "{h:?}");
        }
        for a in [
            SignatureAlgorithm::Ed25519,
            SignatureAlgorithm::EcdsaP521Sha384,
            SignatureAlgorithm::Rsa2048Sha384,
            SignatureAlgorithm::Rsa2048Sha384Pss,
        ] {
            assert!(!s.signatures.contains(&a), "{a}");
        }
    }

    #[test]
    fn no_tls_key_list_holds_an_rsa_pss_variant() {
        for &p in Profile::ALL {
            for &k in p.spec().key_algorithms {
                assert!(k.tls_scheme().is_some(), "{p} {k}");
            }
        }
        for a in [
            S::Rsa2048Sha384Pss,
            S::Rsa3072Sha384Pss,
            S::Rsa4096Sha384Pss,
        ] {
            assert_eq!(a.tls_scheme(), None, "{a}");
            assert!(Profile::Unrestricted.spec().signatures.contains(&a), "{a}");
        }
    }

    #[test]
    fn p521_with_sha384_signs_outside_tls_only_where_fips_186_5_does_not_govern() {
        for &p in Profile::ALL {
            let outside = p.spec().signatures.contains(&S::EcdsaP521Sha384);
            let want = match p {
                Profile::Unrestricted | Profile::Fips1402 => true,
                Profile::Preferred
                | Profile::Cnsa2
                | Profile::Cnsa1
                | Profile::Bsi
                | Profile::Fips1403 => false,
            };
            assert_eq!(outside, want, "{p}");
        }
        for p in [Profile::Preferred, Profile::Fips1403] {
            assert!(p.spec().key_algorithms.contains(&S::EcdsaP521Sha384), "{p}");
        }
    }

    #[test]
    fn bsi_signs_with_rsa_pss_only() {
        assert_eq!(
            Profile::Bsi.spec().signatures,
            &[S::Rsa4096Sha384Pss, S::Rsa3072Sha384Pss]
        );
    }

    #[test]
    fn bsi_admits_only_ecdsa_keys() {
        assert_eq!(
            Profile::Bsi.spec().key_algorithms,
            &[
                SignatureAlgorithm::EcdsaP384Sha384,
                SignatureAlgorithm::EcdsaP256Sha256,
                SignatureAlgorithm::EcdsaP521Sha512,
            ]
        );
    }

    #[test]
    fn select_records_line_exclusions_after_options() {
        for module in [
            M3S,
            Module {
                linkage: Linkage::Dynamic,
                ..M3S
            },
        ] {
            for p in [Profile::Unrestricted, Profile::Preferred, Profile::Fips1403] {
                for options in [Options::default(), PQ] {
                    let s = select(p, options, module).unwrap();
                    assert_eq!(
                        s.excluded,
                        vec![
                            Algorithm::Scheme(T::MlDsa87),
                            Algorithm::Signature(S::MlDsa87),
                        ]
                    );
                    no_duplicates(&s.excluded);
                    assert!(!s.signature_schemes.contains(&T::MlDsa87));
                    assert!(!s.key_algorithms.contains(&S::MlDsa87));
                }
            }
            for options in [Options::default(), PQ] {
                assert_eq!(
                    select(Profile::Cnsa2, options, module),
                    Err(CryptoError::EmptyProfile {
                        profile: Profile::Cnsa2,
                        options,
                        category: Family::SignatureScheme,
                    })
                );
            }
            for p in [Profile::Cnsa1, Profile::Bsi, Profile::Fips1402] {
                assert!(
                    select(p, Options::default(), module)
                        .unwrap()
                        .excluded
                        .is_empty()
                );
            }
        }
    }

    #[test]
    fn select_checks_keys_and_options_precede_line_emptiness() {
        const KEY_ONLY: ProfileSpec = ProfileSpec {
            signature_schemes: &[T::EcdsaNistp384Sha384],
            ..CNSA2
        };
        assert_eq!(
            select_spec(Profile::Cnsa2, &KEY_ONLY, Options::default(), M3S),
            Err(CryptoError::EmptyProfile {
                profile: Profile::Cnsa2,
                options: Options::default(),
                category: Family::KeyAlgorithm,
            })
        );
        const BSI_WITH_ML_DSA: ProfileSpec = ProfileSpec {
            signature_schemes: &[T::MlDsa87],
            ..BSI
        };
        for spec in [&BSI_WITH_ML_DSA, &BSI] {
            assert_eq!(
                select_spec(Profile::Bsi, spec, PQ, M3S),
                Err(CryptoError::EmptyProfile {
                    profile: Profile::Bsi,
                    options: PQ,
                    category: Family::KxGroup,
                })
            );
        }
    }

    #[test]
    fn select_refuses_each_empty_family_in_order() {
        const EMPTY: ProfileSpec = ProfileSpec {
            versions: &[],
            cipher_suites: &[],
            kx_groups: &[],
            signature_schemes: &[],
            key_algorithms: &[],
            macs: &[],
            kdfs: &[],
            key_agreements: &[],
            aeads: &[],
            hashes: &[],
            signatures: &[],
        };
        let empty = |category| {
            Err(CryptoError::EmptyProfile {
                profile: Profile::Bsi,
                options: Options::default(),
                category,
            })
        };
        let cases: [(ProfileSpec, Family); 5] = [
            (EMPTY, Family::Version),
            (
                ProfileSpec {
                    versions: BSI.versions,
                    ..EMPTY
                },
                Family::CipherSuite,
            ),
            (
                ProfileSpec {
                    kx_groups: &[],
                    signature_schemes: &[],
                    key_algorithms: &[],
                    ..BSI
                },
                Family::KxGroup,
            ),
            (
                ProfileSpec {
                    signature_schemes: &[],
                    key_algorithms: &[],
                    ..BSI
                },
                Family::SignatureScheme,
            ),
            (
                ProfileSpec {
                    key_algorithms: &[],
                    ..BSI
                },
                Family::KeyAlgorithm,
            ),
        ];
        for (spec, category) in cases {
            assert_eq!(
                select_spec(Profile::Bsi, &spec, Options::default(), M4S),
                empty(category),
                "{category:?}"
            );
        }
    }

    #[test]
    fn select_without_options_is_the_spec_on_a_module_that_has_everything() {
        for &p in Profile::ALL {
            let s = select(p, Options::default(), M4S).unwrap();
            let spec = p.spec();
            assert!(s.excluded.is_empty());
            assert_eq!(s.versions, spec.versions.to_vec(), "{p}");
            assert_eq!(s.cipher_suites, spec.cipher_suites.to_vec(), "{p}");
            assert_eq!(s.kx_groups, spec.kx_groups.to_vec(), "{p}");
            assert_eq!(s.signature_schemes, spec.signature_schemes.to_vec(), "{p}");
            assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
        }
    }

    #[test]
    fn the_option_keeps_post_quantum_groups_tls13_and_no_pkcs1_schemes() {
        for &p in Profile::ALL {
            let refused = match p {
                Profile::Unrestricted | Profile::Preferred | Profile::Cnsa2 | Profile::Fips1403 => {
                    false
                }
                Profile::Cnsa1 | Profile::Bsi | Profile::Fips1402 => true,
            };
            match select(p, PQ, M4S) {
                Ok(s) => {
                    assert!(!refused, "{p}");
                    let spec = p.spec();
                    assert_eq!(s.versions, vec![TlsVersion::Tls13], "{p}");
                    let suites: Vec<_> = spec
                        .cipher_suites
                        .iter()
                        .copied()
                        .filter(|c| c.version() == TlsVersion::Tls13)
                        .collect();
                    assert_eq!(s.cipher_suites, suites, "{p}");
                    let groups: Vec<_> = spec
                        .kx_groups
                        .iter()
                        .copied()
                        .filter(|k| k.is_post_quantum())
                        .collect();
                    assert_eq!(s.kx_groups, groups, "{p}");
                    let schemes: Vec<_> = spec
                        .signature_schemes
                        .iter()
                        .copied()
                        .filter(|t| !PKCS1.contains(t))
                        .collect();
                    assert_eq!(s.signature_schemes, schemes, "{p}");
                    assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
                }
                Err(e) => {
                    assert!(refused, "{p}");
                    assert_eq!(
                        e,
                        CryptoError::EmptyProfile {
                            profile: p,
                            options: PQ,
                            category: Family::KxGroup
                        }
                    );
                }
            }
        }
    }

    #[test]
    fn the_option_removes_every_pkcs1_scheme_from_unrestricted_and_fips1403() {
        use TlsSignatureScheme as T;
        let unrestricted = vec![
            T::MlDsa87,
            T::EcdsaNistp384Sha384,
            T::EcdsaNistp256Sha256,
            T::EcdsaNistp521Sha512,
            T::Ed25519,
            T::RsaPssSha384,
            T::RsaPssSha512,
            T::RsaPssSha256,
        ];
        let fips1403 = vec![
            T::MlDsa87,
            T::EcdsaNistp384Sha384,
            T::EcdsaNistp256Sha256,
            T::EcdsaNistp521Sha512,
            T::RsaPssSha384,
            T::RsaPssSha512,
            T::RsaPssSha256,
        ];
        for (p, expected) in [
            (Profile::Unrestricted, unrestricted),
            (Profile::Fips1403, fips1403),
        ] {
            assert_eq!(
                select(p, PQ, M4S).unwrap().signature_schemes,
                expected,
                "{p}"
            );
            assert_eq!(
                select(p, Options::default(), M4S)
                    .unwrap()
                    .signature_schemes
                    .len(),
                expected.len() + 3,
                "{p}"
            );
        }
    }

    #[test]
    fn profile_names_are_stable_and_exact() {
        assert_eq!(
            Profile::ALL
                .iter()
                .copied()
                .map(Profile::as_str)
                .collect::<Vec<_>>(),
            [
                "unrestricted",
                "preferred",
                "cnsa2",
                "cnsa1",
                "bsi",
                "fips1403",
                "fips1402"
            ]
        );
        for &p in Profile::ALL {
            assert_eq!(p.as_str().parse::<Profile>(), Ok(p));
            assert_eq!(p.to_string(), p.as_str());
        }
        for bad in [
            "",
            "CNSA2",
            "Cnsa2",
            "FIPS1403",
            "fips140-3",
            "fips-140-3",
            " fips1403",
            "fips1403 ",
            "fips1403\n",
            " bsi",
            "cnsa",
            "bsi2026",
            "preferred,",
            "default",
        ] {
            assert_eq!(
                bad.parse::<Profile>(),
                Err(ParseProfileError(bad.to_string())),
                "{bad:?}"
            );
        }
        assert_eq!(
            ParseProfileError("FIPS1403".to_string()).to_string(),
            "unknown profile \"FIPS1403\""
        );
    }

    #[test]
    fn profile_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            Profile::ALL
                .iter()
                .copied()
                .map(Profile::ordinal)
                .collect::<Vec<_>>(),
            std::array::from_fn::<usize, 7, _>(|i| i)
        );
    }

    #[test]
    fn governing_texts_are_the_published_editions() {
        use GoverningText::{CrateRecommendation as Own, Published};
        let table: [(Profile, GoverningText); 7] = [
            (Profile::Unrestricted, Own),
            (Profile::Preferred, Own),
            (
                Profile::Cnsa2,
                Published {
                    documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
                },
            ),
            (
                Profile::Cnsa1,
                Published {
                    documents: &[
                        "CNSSP 15 (20 October 2016), Annex B, p. B-1",
                        "RFC 9151 (April 2022)",
                    ],
                },
            ),
            (
                Profile::Bsi,
                Published {
                    documents: &[
                        "BSI TR-02102-2, version 2026-01",
                        "BSI TR-02102-1, version 2026-01",
                    ],
                },
            ),
            (
                Profile::Fips1403,
                Published {
                    documents: &[
                        "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
                        "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
                        "NIST SP 800-52r2",
                        "NIST SP 800-131A r2",
                    ],
                },
            ),
            (
                Profile::Fips1402,
                Published {
                    documents: &[
                        "FIPS 140-2 Annex A and Annex D (2021)",
                        "FIPS 186-4",
                        "NIST SP 800-131A r2",
                        "NIST SP 800-52r2",
                    ],
                },
            ),
        ];
        for (p, text) in table {
            assert_eq!(p.governing_text(), text, "{p}");
        }
    }

    #[test]
    fn options_default_narrows_nothing_and_display_names_the_option() {
        assert_eq!(
            Options::default(),
            Options {
                peer_certificates_match_profile: false,
                pq_key_exchange_required: false,
                cnsa1_unclassified_nss: false,
            }
        );
        assert_eq!(Options::default().to_string(), "no options");
        assert_eq!(
            Options {
                peer_certificates_match_profile: false,
                pq_key_exchange_required: true,
                cnsa1_unclassified_nss: false,
            }
            .to_string(),
            "post-quantum key exchange required"
        );
    }
    #[test]
    fn peer_certificate_options_combinations_preserve_selection() {
        for (pq, peer, display) in [
            (false, false, "no options"),
            (true, false, "post-quantum key exchange required"),
            (false, true, "peer certificates match profile"),
            (
                true,
                true,
                "post-quantum key exchange required, peer certificates match profile",
            ),
        ] {
            let options = Options {
                pq_key_exchange_required: pq,
                peer_certificates_match_profile: peer,
                cnsa1_unclassified_nss: false,
            };
            assert_eq!(options.to_string(), display);
            for &profile in Profile::ALL {
                for module in [M3S, M4S] {
                    let baseline = Options {
                        peer_certificates_match_profile: false,
                        ..options
                    };
                    let actual = select(profile, options, module);
                    let expected = select(profile, baseline, module);
                    match (actual, expected) {
                        (Ok(actual), Ok(expected)) => assert_eq!(actual, expected),
                        (
                            Err(CryptoError::EmptyProfile {
                                profile: p,
                                options: o,
                                category: f,
                            }),
                            Err(CryptoError::EmptyProfile {
                                profile: ep,
                                options: eo,
                                category: ef,
                            }),
                        ) => {
                            assert_eq!((p, o, f), (ep, options, ef));
                            assert_eq!(eo, baseline);
                        }
                        (actual, expected) => panic!("{actual:?} != {expected:?}"),
                    }
                }
            }
        }
    }

    #[test]
    fn cnsa1_unclassified_nss_names_itself_and_leaves_tls_selection_alone() {
        let on = Options {
            cnsa1_unclassified_nss: true,
            ..Options::default()
        };
        assert_eq!(on.to_string(), "cnsa1 UNCLASSIFIED NSS");
        assert_eq!(
            Options {
                pq_key_exchange_required: true,
                peer_certificates_match_profile: true,
                cnsa1_unclassified_nss: true,
            }
            .to_string(),
            "post-quantum key exchange required, peer certificates match profile, cnsa1 UNCLASSIFIED NSS"
        );
        for &profile in Profile::ALL {
            for module in [M3S, M4S] {
                for pq in [false, true] {
                    let off = Options {
                        pq_key_exchange_required: pq,
                        ..Options::default()
                    };
                    let on = Options {
                        cnsa1_unclassified_nss: true,
                        ..off
                    };
                    match (select(profile, on, module), select(profile, off, module)) {
                        (Ok(a), Ok(b)) => assert_eq!(a, b),
                        (
                            Err(CryptoError::EmptyProfile {
                                profile: p,
                                options: o,
                                category: f,
                            }),
                            Err(CryptoError::EmptyProfile {
                                profile: ep,
                                options: eo,
                                category: ef,
                            }),
                        ) => {
                            assert_eq!((p, f), (ep, ef));
                            assert_eq!((o, eo), (on, off));
                        }
                        (a, b) => panic!("{a:?} != {b:?}"),
                    }
                }
            }
        }
    }

    #[test]
    fn select_admitted_never_refuses_an_empty_family() {
        let spec = ProfileSpec {
            macs: &[],
            kdfs: &[],
            aeads: &[],
            hashes: &[],
            signatures: &[],
            ..BSI
        };
        assert_eq!(
            select_admitted_with(Profile::Bsi, &spec, Options::default(), |_| true),
            AdmittedSelection {
                macs: vec![],
                kdfs: vec![],
                key_agreements: vec![],
                aeads: vec![],
                hashes: vec![],
                signatures: vec![],
                excluded: vec![],
            }
        );
    }

    #[test]
    fn select_admitted_records_what_the_module_lacks() {
        use AeadAlgorithm as D;
        use HashAlgorithm as H;
        use KdfAlgorithm as K;
        use KeyAgreementAlgorithm as E;
        use MacAlgorithm as M;
        let lacking = [
            Algorithm::Mac(M::HmacSha384),
            Algorithm::Kdf(K::HkdfSha512),
            Algorithm::Kdf(K::OneStepSha384),
            Algorithm::KeyAgreement(E::EcdhP521),
            Algorithm::Aead(D::Aes128Gcm),
            Algorithm::Hash(H::Sha384),
            Algorithm::Hash(H::Sha3_512),
            Algorithm::Signature(S::MlDsa87),
            Algorithm::Signature(S::Rsa3072Sha384Pss),
        ];
        assert_eq!(
            select_admitted_with(Profile::Fips1403, &FIPS1403, Options::default(), |a| {
                !lacking.contains(&a)
            }),
            AdmittedSelection {
                macs: vec![M::HmacSha512, M::HmacSha256],
                kdfs: vec![
                    K::HkdfSha384,
                    K::HkdfSha256,
                    K::OneStepSha512,
                    K::OneStepSha256
                ],
                key_agreements: vec![E::EcdhP384, E::EcdhP256],
                aeads: vec![D::Aes256Gcm],
                hashes: vec![H::Sha512, H::Sha256, H::Sha3_384, H::Sha3_256],
                signatures: vec![
                    S::EcdsaP384Sha384,
                    S::EcdsaP521Sha512,
                    S::EcdsaP256Sha256,
                    S::Ed25519,
                    S::Rsa4096Sha384Pss,
                    S::Rsa2048Sha384Pss,
                    S::Rsa4096Sha384,
                    S::Rsa3072Sha384,
                    S::Rsa2048Sha384,
                ],
                excluded: lacking.to_vec(),
            }
        );
    }

    fn strength_bits(k: KeyAgreementAlgorithm) -> usize {
        match k {
            KeyAgreementAlgorithm::EcdhP256 | KeyAgreementAlgorithm::X25519 => 128,
            KeyAgreementAlgorithm::EcdhP384 => 192,
            KeyAgreementAlgorithm::EcdhP521 => 256,
        }
    }

    #[test]
    fn every_admitted_curve_and_kdf_pair_meets_sp_800_56c_section_7() {
        let on = Options {
            cnsa1_unclassified_nss: true,
            ..Options::default()
        };
        let mut pairs = 0;
        for &p in Profile::ALL {
            for o in [Options::default(), on] {
                let s = select_admitted(p, o, M4S);
                for &k in &s.key_agreements {
                    for &d in &s.kdfs {
                        pairs += 1;
                        assert!(
                            8 * d.hash().output_len() >= strength_bits(k),
                            "{p} {o} {k:?} {d:?}"
                        );
                    }
                }
            }
        }
        assert_eq!(pairs, 2 * (4 * 6 + 3 * 6 + 3 * 6 + 3 * 6) + 1 + 2);
    }

    #[test]
    fn the_unclassified_nss_option_adds_hmac_sha256_to_cnsa1_only() {
        let on = Options {
            cnsa1_unclassified_nss: true,
            ..Options::default()
        };
        for &p in Profile::ALL {
            let mut want = select_admitted(p, Options::default(), M4S).macs;
            if p == Profile::Cnsa1 {
                want.push(MacAlgorithm::HmacSha256);
            }
            assert_eq!(select_admitted(p, on, M4S).macs, want, "{p}");
        }
    }

    #[test]
    fn the_unclassified_nss_option_adds_hkdf_sha256_to_cnsa1_only() {
        let on = Options {
            cnsa1_unclassified_nss: true,
            ..Options::default()
        };
        for &p in Profile::ALL {
            let mut want = select_admitted(p, Options::default(), M4S).kdfs;
            if p == Profile::Cnsa1 {
                want.push(KdfAlgorithm::HkdfSha256);
            }
            assert_eq!(select_admitted(p, on, M4S).kdfs, want, "{p}");
        }
    }

    #[test]
    fn the_unclassified_nss_option_adds_sha256_hashing_to_cnsa1_only() {
        let on = Options {
            cnsa1_unclassified_nss: true,
            ..Options::default()
        };
        for &p in Profile::ALL {
            let mut want = select_admitted(p, Options::default(), M4S).hashes;
            if p == Profile::Cnsa1 {
                assert_eq!(want, [HashAlgorithm::Sha384]);
                want.push(HashAlgorithm::Sha256);
            }
            assert_eq!(select_admitted(p, on, M4S).hashes, want, "{p}");
            assert_eq!(select_admitted(p, on, M3S).hashes, want, "{p}");
        }
    }

    #[test]
    fn the_unclassified_nss_option_adds_rsa_2048_signatures_to_cnsa1_only() {
        let on = Options {
            cnsa1_unclassified_nss: true,
            ..Options::default()
        };
        for &p in Profile::ALL {
            for m in [M3S, M4S] {
                let mut want = select_admitted(p, Options::default(), m).signatures;
                if p == Profile::Cnsa1 {
                    assert_eq!(
                        want,
                        [
                            S::EcdsaP384Sha384,
                            S::Rsa4096Sha384Pss,
                            S::Rsa3072Sha384Pss,
                            S::Rsa4096Sha384,
                            S::Rsa3072Sha384,
                        ]
                    );
                    want.extend([S::Rsa2048Sha384Pss, S::Rsa2048Sha384]);
                }
                assert_eq!(select_admitted(p, on, m).signatures, want, "{p} {m:?}");
            }
        }
    }

    #[test]
    fn an_empty_kdf_info_is_admitted_under_unrestricted_only() {
        for &p in Profile::ALL {
            let may = match p {
                Profile::Unrestricted => true,
                Profile::Preferred
                | Profile::Cnsa2
                | Profile::Cnsa1
                | Profile::Bsi
                | Profile::Fips1403
                | Profile::Fips1402 => false,
            };
            assert_eq!(kdf_info_may_be_empty(p), may, "{p}");
        }
    }

    #[test]
    fn mac_keys_are_bounded_by_the_module_and_by_bsi() {
        for &p in Profile::ALL {
            let floor = match p {
                Profile::Bsi => 128,
                Profile::Unrestricted
                | Profile::Preferred
                | Profile::Cnsa2
                | Profile::Cnsa1
                | Profile::Fips1403
                | Profile::Fips1402 => 112,
            };
            assert_eq!(mac_key_floor_bits(p), floor, "{p}");
        }
        assert_eq!(MAC_KEY_MAX_BITS, 524_288);
    }
}