noxtls-crypto 0.1.2

Internal implementation crate for noxtls: hash, symmetric cipher, public-key, and DRBG primitives.
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
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
// Copyright (c) 2019-2026, Argenox Technologies LLC
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only OR LicenseRef-Argenox-Commercial-License
//
// This file is part of the NoxTLS Library.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation; version 2 of the License.
//
// Alternatively, this file may be used under the terms of a commercial
// license from Argenox Technologies LLC.
//
// See `noxtls/LICENSE` and `noxtls/LICENSE.md` in this repository for full details.
// CONTACT: info@argenox.com

use crate::drbg::HmacDrbgSha256;
use crate::hash::{sha1, sha256, sha384, sha512};
use crate::internal_alloc::Vec;
use noxtls_core::{Error, Result};

use super::bignum::BigUint;

const RSA_KEYGEN_MIN_BITS: usize = 1024;
const RSA_KEYGEN_MAX_BITS: usize = 4096;
const RSA_MIN_SECURE_BITS: usize = 2048;
const RSA_RECOMMENDED_SECURE_BITS: usize = 3072;

/// Represents an RSA private key with arbitrary-size modulus and exponent.
#[derive(Debug, Clone)]
pub struct RsaPrivateKey {
    pub n: BigUint,
    pub d: BigUint,
    crt: Option<RsaPrivateCrtComponents>,
}

/// Represents an RSA public key with arbitrary-size modulus and exponent.
#[derive(Debug, Clone)]
pub struct RsaPublicKey {
    pub n: BigUint,
    pub e: BigUint,
}

/// Stores optional RSA CRT decomposition parameters for accelerated private operations.
#[derive(Debug, Clone)]
struct RsaPrivateCrtComponents {
    p: BigUint,
    q: BigUint,
    dp: BigUint,
    dq: BigUint,
    qinv: BigUint,
}

/// Defines secure RSA key-size policy thresholds for safe key-generation entry points.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum RsaKeySizePolicy {
    /// Requires at least 2048-bit RSA modulus length.
    Minimum2048,
    /// Requires at least 3072-bit RSA modulus length.
    Minimum3072,
}

impl RsaKeySizePolicy {
    /// Returns the minimum RSA modulus size in bits for this policy.
    ///
    /// # Arguments
    ///
    /// * `self` — Selected policy variant.
    ///
    /// # Returns
    ///
    /// Minimum modulus bit length required for key generation.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    fn min_bits(self) -> usize {
        match self {
            Self::Minimum2048 => RSA_MIN_SECURE_BITS,
            Self::Minimum3072 => RSA_RECOMMENDED_SECURE_BITS,
        }
    }
}

impl RsaPrivateKey {
    /// Creates private key from big-endian modulus and private exponent bytes.
    ///
    /// # Arguments
    /// * `n`: RSA modulus encoded as big-endian bytes.
    /// * `d`: RSA private exponent encoded as big-endian bytes.
    ///
    /// # Returns
    /// Parsed `RsaPrivateKey` when both fields are non-empty.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidLength`] when fields are empty, or when modulus size is below
    /// 2048 bits in default-safe builds (legacy-compatible hazardous mode permits smaller imports),
    /// or other RSA component validation errors from [`validate_private_components`].
    pub fn from_be_bytes(n: &[u8], d: &[u8]) -> Result<Self> {
        if n.is_empty() || d.is_empty() {
            return Err(Error::InvalidLength(
                "rsa private key fields must not be empty",
            ));
        }
        let key = Self {
            n: BigUint::from_be_bytes(n),
            d: BigUint::from_be_bytes(d),
            crt: None,
        };
        if !cfg!(feature = "hazardous-legacy-crypto") && key.n.bit_len() < RSA_MIN_SECURE_BITS {
            return Err(Error::InvalidLength(
                "rsa private key modulus must be at least 2048 bits",
            ));
        }
        validate_private_components(&key.n, &key.d)?;
        Ok(key)
    }

    /// Creates private key from small integers for compatibility tests.
    ///
    /// # Arguments
    /// * `n`: RSA modulus value.
    /// * `d`: RSA private exponent value.
    ///
    /// # Returns
    /// `RsaPrivateKey` converted from the provided integer values.
    #[must_use]
    pub fn from_u128(n: u128, d: u128) -> Self {
        Self {
            n: BigUint::from_u128(n),
            d: BigUint::from_u128(d),
            crt: None,
        }
    }

    /// Clears private key material to a zeroized placeholder state.
    ///
    /// # Notes
    /// This mirrors explicit key free/reset lifecycle flows from the C surface.
    pub fn clear(&mut self) {
        self.n.clear();
        self.d.clear();
        if let Some(crt) = self.crt.as_mut() {
            crt.p.clear();
            crt.q.clear();
            crt.dp.clear();
            crt.dq.clear();
            crt.qinv.clear();
        }
        self.crt = None;
    }

    /// Attaches RSA CRT decomposition components to this private key.
    ///
    /// # Arguments
    /// * `p`: First RSA prime factor.
    /// * `q`: Second RSA prime factor.
    /// * `dp`: `d mod (p - 1)` CRT exponent.
    /// * `dq`: `d mod (q - 1)` CRT exponent.
    /// * `qinv`: `q^{-1} mod p` CRT coefficient.
    ///
    /// # Returns
    /// Updated private key configured with CRT components.
    pub fn with_crt_components(
        mut self,
        p: &[u8],
        q: &[u8],
        dp: &[u8],
        dq: &[u8],
        qinv: &[u8],
    ) -> Result<Self> {
        let crt = RsaPrivateCrtComponents {
            p: BigUint::from_be_bytes(p),
            q: BigUint::from_be_bytes(q),
            dp: BigUint::from_be_bytes(dp),
            dq: BigUint::from_be_bytes(dq),
            qinv: BigUint::from_be_bytes(qinv),
        };
        validate_crt_components(&self.n, &crt)?;
        self.crt = Some(crt);
        Ok(self)
    }

    /// Signs a representative digest interpreted as big-endian integer modulo `n`.
    ///
    /// # Arguments
    /// * `digest`: Digest bytes to convert into an RSA message representative.
    ///
    /// # Returns
    /// Signature bytes padded to modulus length.
    pub fn sign_digest(&self, digest: &[u8]) -> Result<Vec<u8>> {
        if digest.is_empty() {
            return Err(Error::InvalidLength("digest must not be empty"));
        }
        validate_private_components(&self.n, &self.d)?;
        let m = BigUint::from_be_bytes(digest).modulo(&self.n);
        let s = BigUint::mod_exp(&m, &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Signs a message using RSASSA-PKCS1-v1_5 style encoding with SHA-256.
    ///
    /// # Arguments
    /// * `msg`: Message bytes to hash and sign.
    ///
    /// # Returns
    /// PKCS#1 v1.5 RSA signature bytes.
    pub fn sign_pkcs1_v15_sha256(&self, msg: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        let hash = sha256(msg);
        let em = emsa_pkcs1_v15_encode(
            &hash,
            PKCS1_V15_DIGESTINFO_SHA256_PREFIX,
            self.modulus_len(),
        )?;
        let m = BigUint::from_be_bytes(&em);
        let s = BigUint::mod_exp(&m, &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Signs a message using RSASSA-PKCS1-v1_5 style encoding with SHA-1.
    ///
    /// # Arguments
    /// * `msg`: Message bytes to hash and sign.
    ///
    /// # Returns
    /// PKCS#1 v1.5 RSA signature bytes.
    pub fn sign_pkcs1_v15_sha1(&self, msg: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        let hash = sha1(msg);
        let em =
            emsa_pkcs1_v15_encode(&hash, PKCS1_V15_DIGESTINFO_SHA1_PREFIX, self.modulus_len())?;
        let m = BigUint::from_be_bytes(&em);
        let s = BigUint::mod_exp(&m, &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Signs a message using RSASSA-PKCS1-v1_5 style encoding with SHA-384.
    ///
    /// # Arguments
    /// * `msg`: Message bytes to hash and sign.
    ///
    /// # Returns
    /// PKCS#1 v1.5 RSA signature bytes.
    pub fn sign_pkcs1_v15_sha384(&self, msg: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        let hash = sha384(msg);
        let em = emsa_pkcs1_v15_encode(
            &hash,
            PKCS1_V15_DIGESTINFO_SHA384_PREFIX,
            self.modulus_len(),
        )?;
        let m = BigUint::from_be_bytes(&em);
        let s = BigUint::mod_exp(&m, &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Signs a message using RSASSA-PKCS1-v1_5 style encoding with SHA-512.
    ///
    /// # Arguments
    /// * `msg`: Message bytes to hash and sign.
    ///
    /// # Returns
    /// PKCS#1 v1.5 RSA signature bytes.
    pub fn sign_pkcs1_v15_sha512(&self, msg: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        let hash = sha512(msg);
        let em = emsa_pkcs1_v15_encode(
            &hash,
            PKCS1_V15_DIGESTINFO_SHA512_PREFIX,
            self.modulus_len(),
        )?;
        let m = BigUint::from_be_bytes(&em);
        let s = BigUint::mod_exp(&m, &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Signs a message using RSASSA-PSS with SHA-256 and caller-provided salt.
    ///
    /// # Arguments
    /// * `msg`: Message bytes to hash and sign.
    /// * `salt`: Caller-provided random salt used by PSS encoding.
    ///
    /// # Returns
    /// RSASSA-PSS RSA signature bytes.
    pub fn sign_pss_sha256(&self, msg: &[u8], salt: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        let em_bits = self.n.bit_len().saturating_sub(1);
        let em_len = em_bits.div_ceil(8);
        let m_hash = sha256(msg);
        let em = emsa_pss_encode_sha256(&m_hash, salt, em_bits, em_len)?;
        let s = BigUint::mod_exp(&BigUint::from_be_bytes(&em), &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Signs a message using RSASSA-PSS with SHA-384 and caller-provided salt.
    ///
    /// # Arguments
    /// * `msg`: Message bytes to hash and sign.
    /// * `salt`: Caller-provided random salt used by PSS encoding.
    ///
    /// # Returns
    /// RSASSA-PSS RSA signature bytes.
    pub fn sign_pss_sha384(&self, msg: &[u8], salt: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        let em_bits = self.n.bit_len().saturating_sub(1);
        let em_len = em_bits.div_ceil(8);
        let m_hash = sha384(msg);
        let em = emsa_pss_encode_sha384(&m_hash, salt, em_bits, em_len)?;
        let s = BigUint::mod_exp(&BigUint::from_be_bytes(&em), &self.d, &self.n);
        s.to_be_bytes_padded(self.modulus_len())
    }

    /// Decrypts RSAES-PKCS1-v1_5 ciphertext with private exponent `d`.
    ///
    /// # Arguments
    /// * `ciphertext`: Ciphertext bytes with length equal to modulus length.
    ///
    /// # Returns
    /// Decrypted plaintext when PKCS#1 v1.5 structure is valid.
    pub fn decrypt_pkcs1_v15(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        if ciphertext.len() != self.modulus_len() {
            return Err(Error::CryptoFailure("rsa decryption failed"));
        }
        let em = BigUint::mod_exp(&BigUint::from_be_bytes(ciphertext), &self.d, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        decode_pkcs1_v15_plaintext(&em)
    }

    /// Decrypts RSAES-PKCS1-v1_5 ciphertext using configured CRT components.
    ///
    /// # Arguments
    /// * `ciphertext`: Ciphertext bytes with length equal to modulus length.
    ///
    /// # Returns
    /// Decrypted plaintext when CRT components are configured and PKCS#1 v1.5 structure is valid.
    pub fn decrypt_pkcs1_v15_crt_only(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        if ciphertext.len() != self.modulus_len() {
            return Err(Error::CryptoFailure("rsa decryption failed"));
        }
        let crt = self
            .crt
            .as_ref()
            .ok_or(Error::StateError("rsa crt parameters are not configured"))?;
        let c = BigUint::from_be_bytes(ciphertext);
        let m1 = BigUint::mod_exp(&c, &crt.dp, &crt.p);
        let m2 = BigUint::mod_exp(&c, &crt.dq, &crt.q);
        let diff = if m1.cmp(&m2).is_ge() {
            m1.sub(&m2)
        } else {
            m1.add(&crt.p).sub(&m2)
        };
        let h = crt.qinv.mul(&diff).modulo(&crt.p);
        let m = m2.add(&crt.q.mul(&h));
        let em = m.to_be_bytes_padded(self.modulus_len())?;
        decode_pkcs1_v15_plaintext(&em)
    }

    /// Decrypts RSAES-OAEP ciphertext with SHA-256 and caller-provided label.
    ///
    /// # Arguments
    /// * `ciphertext`: Ciphertext bytes with length equal to modulus length.
    /// * `label`: OAEP label bytes hashed into encoding parameters.
    ///
    /// # Returns
    /// Decrypted plaintext when OAEP structure validates.
    pub fn decrypt_oaep_sha256(&self, ciphertext: &[u8], label: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        if ciphertext.len() != self.modulus_len() {
            return Err(Error::CryptoFailure("rsa decryption failed"));
        }
        let em = BigUint::mod_exp(&BigUint::from_be_bytes(ciphertext), &self.d, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        decode_oaep_sha256_plaintext(&em, label)
    }

    /// Decrypts RSAES-OAEP ciphertext using configured CRT components.
    ///
    /// # Arguments
    /// * `ciphertext`: Ciphertext bytes with length equal to modulus length.
    /// * `label`: OAEP label bytes hashed into encoding parameters.
    ///
    /// # Returns
    /// Decrypted plaintext when CRT parameters are configured and OAEP structure validates.
    pub fn decrypt_oaep_sha256_crt_only(&self, ciphertext: &[u8], label: &[u8]) -> Result<Vec<u8>> {
        validate_private_components(&self.n, &self.d)?;
        if ciphertext.len() != self.modulus_len() {
            return Err(Error::CryptoFailure("rsa decryption failed"));
        }
        let crt = self
            .crt
            .as_ref()
            .ok_or(Error::StateError("rsa crt parameters are not configured"))?;
        let c = BigUint::from_be_bytes(ciphertext);
        let m1 = BigUint::mod_exp(&c, &crt.dp, &crt.p);
        let m2 = BigUint::mod_exp(&c, &crt.dq, &crt.q);
        let diff = if m1.cmp(&m2).is_ge() {
            m1.sub(&m2)
        } else {
            m1.add(&crt.p).sub(&m2)
        };
        let h = crt.qinv.mul(&diff).modulo(&crt.p);
        let m = m2.add(&crt.q.mul(&h));
        let em = m.to_be_bytes_padded(self.modulus_len())?;
        decode_oaep_sha256_plaintext(&em, label)
    }

    /// Returns the RSA modulus length in bytes for PKCS encoding helpers.
    ///
    /// # Arguments
    ///
    /// * `self` — Private key whose modulus `n` defines the length.
    ///
    /// # Returns
    ///
    /// Byte length of the big-endian modulus encoding.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    fn modulus_len(&self) -> usize {
        self.n.to_be_bytes().len()
    }
}

impl Drop for RsaPrivateKey {
    fn drop(&mut self) {
        self.clear();
    }
}

impl RsaPublicKey {
    /// Creates public key from big-endian modulus and exponent bytes.
    ///
    /// # Arguments
    /// * `n`: RSA modulus encoded as big-endian bytes.
    /// * `e`: RSA public exponent encoded as big-endian bytes.
    ///
    /// # Returns
    /// Parsed `RsaPublicKey` when both fields are non-empty.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidLength`] when fields are empty, or when modulus size is below
    /// 2048 bits in default-safe builds (legacy-compatible hazardous mode permits smaller imports),
    /// or other RSA component validation errors from [`validate_public_components`].
    pub fn from_be_bytes(n: &[u8], e: &[u8]) -> Result<Self> {
        if n.is_empty() || e.is_empty() {
            return Err(Error::InvalidLength(
                "rsa public key fields must not be empty",
            ));
        }
        let key = Self {
            n: BigUint::from_be_bytes(n),
            e: BigUint::from_be_bytes(e),
        };
        if !cfg!(feature = "hazardous-legacy-crypto") && key.n.bit_len() < RSA_MIN_SECURE_BITS {
            return Err(Error::InvalidLength(
                "rsa public key modulus must be at least 2048 bits",
            ));
        }
        validate_public_components(&key.n, &key.e)?;
        Ok(key)
    }

    /// Creates public key from small integers for compatibility tests.
    ///
    /// # Arguments
    /// * `n`: RSA modulus value.
    /// * `e`: RSA public exponent value.
    ///
    /// # Returns
    /// `RsaPublicKey` converted from the provided integer values.
    #[must_use]
    pub fn from_u128(n: u128, e: u128) -> Self {
        Self {
            n: BigUint::from_u128(n),
            e: BigUint::from_u128(e),
        }
    }

    /// Clears public key material to a zeroized placeholder state.
    ///
    /// # Notes
    /// This mirrors explicit key free/reset lifecycle flows from the C surface.
    pub fn clear(&mut self) {
        self.n = BigUint::zero();
        self.e = BigUint::zero();
    }

    /// Verifies a digest representative by recovering signature with exponent `e`.
    ///
    /// # Arguments
    /// * `digest`: Expected digest representative bytes.
    /// * `signature`: RSA signature to verify.
    ///
    /// # Returns
    /// `Ok(())` when the recovered representative equals `digest mod n`.
    pub fn verify_digest(&self, digest: &[u8], signature: &[u8]) -> Result<()> {
        if digest.is_empty() {
            return Err(Error::InvalidLength("digest must not be empty"));
        }
        validate_public_components(&self.n, &self.e)?;
        let k = self.modulus_len();
        let expected = BigUint::from_be_bytes(digest)
            .modulo(&self.n)
            .to_be_bytes_padded(k)?;
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(k)?;
        if ct_bytes_eq(&recovered, &expected) {
            Ok(())
        } else {
            Err(Error::CryptoFailure("RSA verification failed"))
        }
    }

    /// Verifies RSASSA-PKCS1-v1_5 signature for SHA-256 hashed message.
    ///
    /// # Arguments
    /// * `msg`: Original message bytes.
    /// * `signature`: RSA signature expected to be PKCS#1 v1.5 encoded.
    ///
    /// # Returns
    /// `Ok(())` when signature verification succeeds.
    pub fn verify_pkcs1_v15_sha256(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
        validate_public_components(&self.n, &self.e)?;
        if signature.len() != self.modulus_len() {
            return Err(Error::InvalidLength("rsa signature length mismatch"));
        }
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        let expected = emsa_pkcs1_v15_encode(
            &sha256(msg),
            PKCS1_V15_DIGESTINFO_SHA256_PREFIX,
            self.modulus_len(),
        )?;
        if ct_bytes_eq(&recovered, &expected) {
            Ok(())
        } else {
            Err(Error::CryptoFailure("RSA verification failed"))
        }
    }

    /// Verifies RSASSA-PKCS1-v1_5 signature for SHA-1 hashed message.
    ///
    /// # Arguments
    /// * `msg`: Original message bytes.
    /// * `signature`: RSA signature expected to be PKCS#1 v1.5 encoded.
    ///
    /// # Returns
    /// `Ok(())` when signature verification succeeds.
    pub fn verify_pkcs1_v15_sha1(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
        validate_public_components(&self.n, &self.e)?;
        if signature.len() != self.modulus_len() {
            return Err(Error::InvalidLength("rsa signature length mismatch"));
        }
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        let expected = emsa_pkcs1_v15_encode(
            &sha1(msg),
            PKCS1_V15_DIGESTINFO_SHA1_PREFIX,
            self.modulus_len(),
        )?;
        if ct_bytes_eq(&recovered, &expected) {
            Ok(())
        } else {
            Err(Error::CryptoFailure("RSA verification failed"))
        }
    }

    /// Verifies RSASSA-PKCS1-v1_5 signature for SHA-384 hashed message.
    ///
    /// # Arguments
    /// * `msg`: Original message bytes.
    /// * `signature`: RSA signature expected to be PKCS#1 v1.5 encoded.
    ///
    /// # Returns
    /// `Ok(())` when signature verification succeeds.
    pub fn verify_pkcs1_v15_sha384(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
        validate_public_components(&self.n, &self.e)?;
        if signature.len() != self.modulus_len() {
            return Err(Error::InvalidLength("rsa signature length mismatch"));
        }
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        let expected = emsa_pkcs1_v15_encode(
            &sha384(msg),
            PKCS1_V15_DIGESTINFO_SHA384_PREFIX,
            self.modulus_len(),
        )?;
        if ct_bytes_eq(&recovered, &expected) {
            Ok(())
        } else {
            Err(Error::CryptoFailure("RSA verification failed"))
        }
    }

    /// Verifies RSASSA-PKCS1-v1_5 signature for SHA-512 hashed message.
    ///
    /// # Arguments
    /// * `msg`: Original message bytes.
    /// * `signature`: RSA signature expected to be PKCS#1 v1.5 encoded.
    ///
    /// # Returns
    /// `Ok(())` when signature verification succeeds.
    pub fn verify_pkcs1_v15_sha512(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
        validate_public_components(&self.n, &self.e)?;
        if signature.len() != self.modulus_len() {
            return Err(Error::InvalidLength("rsa signature length mismatch"));
        }
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        let expected = emsa_pkcs1_v15_encode(
            &sha512(msg),
            PKCS1_V15_DIGESTINFO_SHA512_PREFIX,
            self.modulus_len(),
        )?;
        if ct_bytes_eq(&recovered, &expected) {
            Ok(())
        } else {
            Err(Error::CryptoFailure("RSA verification failed"))
        }
    }

    /// Verifies RSASSA-PSS signature for SHA-256 hashed message.
    ///
    /// # Arguments
    /// * `msg`: Original message bytes.
    /// * `signature`: RSA signature expected to be PSS encoded.
    /// * `salt_len`: Expected salt length used by signer.
    ///
    /// # Returns
    /// `Ok(())` when signature verification succeeds.
    pub fn verify_pss_sha256(&self, msg: &[u8], signature: &[u8], salt_len: usize) -> Result<()> {
        validate_public_components(&self.n, &self.e)?;
        if signature.len() != self.modulus_len() {
            return Err(Error::InvalidLength("rsa signature length mismatch"));
        }
        let em_bits = self.n.bit_len().saturating_sub(1);
        let em_len = em_bits.div_ceil(8);
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        let em = &recovered[recovered.len() - em_len..];
        emsa_pss_verify_sha256(&sha256(msg), em, em_bits, salt_len)
    }

    /// Verifies RSASSA-PSS signature for SHA-384 hashed message.
    ///
    /// # Arguments
    /// * `msg`: Original message bytes.
    /// * `signature`: RSA signature expected to be PSS encoded.
    /// * `salt_len`: Expected salt length used by signer.
    ///
    /// # Returns
    /// `Ok(())` when signature verification succeeds.
    pub fn verify_pss_sha384(&self, msg: &[u8], signature: &[u8], salt_len: usize) -> Result<()> {
        validate_public_components(&self.n, &self.e)?;
        if signature.len() != self.modulus_len() {
            return Err(Error::InvalidLength("rsa signature length mismatch"));
        }
        let em_bits = self.n.bit_len().saturating_sub(1);
        let em_len = em_bits.div_ceil(8);
        let recovered = BigUint::mod_exp(&BigUint::from_be_bytes(signature), &self.e, &self.n)
            .to_be_bytes_padded(self.modulus_len())?;
        let em = &recovered[recovered.len() - em_len..];
        emsa_pss_verify_sha384(&sha384(msg), em, em_bits, salt_len)
    }

    /// Encrypts plaintext using RSAES-PKCS1-v1_5 with DRBG-sourced non-zero padding.
    ///
    /// # Arguments
    /// * `plaintext`: Plaintext bytes to encrypt.
    /// * `drbg`: DRBG used to generate PKCS#1 v1.5 PS bytes.
    ///
    /// # Returns
    /// Ciphertext bytes padded to modulus length.
    pub fn encrypt_pkcs1_v15_auto(
        &self,
        plaintext: &[u8],
        drbg: &mut HmacDrbgSha256,
    ) -> Result<Vec<u8>> {
        validate_public_components(&self.n, &self.e)?;
        let k = self.modulus_len();
        if plaintext.len() > k.saturating_sub(11) {
            return Err(Error::InvalidLength(
                "rsa plaintext too long for pkcs1 v1.5 encryption",
            ));
        }
        let ps_len = k - plaintext.len() - 3;
        let ps = drbg_nonzero_padding(drbg, ps_len)?;
        let mut em = Vec::with_capacity(k);
        em.push(0x00);
        em.push(0x02);
        em.extend_from_slice(&ps);
        em.push(0x00);
        em.extend_from_slice(plaintext);
        let c = BigUint::mod_exp(&BigUint::from_be_bytes(&em), &self.e, &self.n);
        c.to_be_bytes_padded(k)
    }

    /// Encrypts plaintext using RSAES-OAEP with SHA-256 and DRBG-derived seed.
    ///
    /// # Arguments
    /// * `plaintext`: Plaintext bytes to encrypt.
    /// * `label`: OAEP label bytes hashed into encoding parameters.
    /// * `drbg`: DRBG used to generate OAEP seed bytes.
    ///
    /// # Returns
    /// Ciphertext bytes padded to modulus length.
    pub fn encrypt_oaep_sha256_auto(
        &self,
        plaintext: &[u8],
        label: &[u8],
        drbg: &mut HmacDrbgSha256,
    ) -> Result<Vec<u8>> {
        validate_public_components(&self.n, &self.e)?;
        let k = self.modulus_len();
        let seed = drbg.generate(32, b"rsa_oaep_sha256_seed")?;
        let em = emea_oaep_encode_sha256(plaintext, label, &seed, k)?;
        let c = BigUint::mod_exp(&BigUint::from_be_bytes(&em), &self.e, &self.n);
        c.to_be_bytes_padded(k)
    }

    /// Returns the RSA modulus length in bytes for encryption and encoding helpers.
    ///
    /// # Arguments
    ///
    /// * `self` — Public key whose modulus `n` defines the length.
    ///
    /// # Returns
    ///
    /// Byte length of the big-endian modulus encoding.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    fn modulus_len(&self) -> usize {
        self.n.to_be_bytes().len()
    }
}

/// Generates an RSA keypair with a caller-provided public exponent using DRBG entropy.
///
/// # Arguments
/// * `modulus_bits`: Target modulus size in bits (supported range: 1024..=4096).
/// * `public_exponent`: Public exponent value (must be odd and >= 3).
/// * `drbg`: DRBG source used to sample prime candidates.
///
/// # Returns
/// Generated `(private_key, public_key)` pair including CRT parameters.
#[cfg(feature = "hazardous-legacy-crypto")]
pub fn rsa_generate_keypair_with_exponent_auto(
    modulus_bits: usize,
    public_exponent: u32,
    drbg: &mut HmacDrbgSha256,
) -> Result<(RsaPrivateKey, RsaPublicKey)> {
    rsa_generate_keypair_backend_auto(modulus_bits, public_exponent, drbg)
}

/// Generates RSA keypair material with backend-supported modulus range and exponent checks.
///
/// # Arguments
///
/// * `modulus_bits` — Target modulus size in bits (supported inclusive range enforced inside).
/// * `public_exponent` — Desired public exponent (must be odd and at least 3).
/// * `drbg` — DRBG used for prime sampling and auxiliary randomness.
///
/// # Returns
///
/// On success, a `(private_key, public_key)` pair including CRT parameters when applicable.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when parameters are out of range, prime search fails, or internal invariants fail.
///
/// # Panics
///
/// This function does not panic.
fn rsa_generate_keypair_backend_auto(
    modulus_bits: usize,
    public_exponent: u32,
    drbg: &mut HmacDrbgSha256,
) -> Result<(RsaPrivateKey, RsaPublicKey)> {
    if !(RSA_KEYGEN_MIN_BITS..=RSA_KEYGEN_MAX_BITS).contains(&modulus_bits) {
        return Err(Error::InvalidLength(
            "rsa modulus bits must be in supported range 1024..=4096",
        ));
    }
    if public_exponent < 3 || (public_exponent & 1) == 0 {
        return Err(Error::CryptoFailure(
            "rsa public exponent must be odd and at least 3",
        ));
    }
    let e = BigUint::from_u128(u128::from(public_exponent));
    let one = BigUint::one();
    let p_bits = modulus_bits / 2;
    let q_bits = modulus_bits - p_bits;
    let mut attempts = 0_u32;
    while attempts < 256 {
        let mut p = generate_rsa_prime_candidate_auto(p_bits, &e, drbg)?;
        let mut q = generate_rsa_prime_candidate_auto(q_bits, &e, drbg)?;
        let mut distinct_attempts = 0_u32;
        while p.cmp(&q).is_eq() {
            if distinct_attempts >= 32 {
                break;
            }
            q = generate_rsa_prime_candidate_auto(q_bits, &e, drbg)?;
            distinct_attempts = distinct_attempts.saturating_add(1);
        }
        if p.cmp(&q).is_eq() {
            attempts = attempts.saturating_add(1);
            continue;
        }
        if p.cmp(&q).is_gt() {
            core::mem::swap(&mut p, &mut q);
        }
        let n = p.mul(&q);
        if n.bit_len() != modulus_bits {
            attempts = attempts.saturating_add(1);
            continue;
        }
        let pm1 = p.sub(&one);
        let qm1 = q.sub(&one);
        let phi = pm1.mul(&qm1);
        if BigUint::gcd(&e, &phi).cmp(&one).is_ne() {
            attempts = attempts.saturating_add(1);
            continue;
        }
        let Some(d) = BigUint::mod_inverse(&e, &phi) else {
            attempts = attempts.saturating_add(1);
            continue;
        };
        let dp = d.modulo(&pm1);
        let dq = d.modulo(&qm1);
        let Some(qinv) = BigUint::mod_inverse(&q, &p) else {
            attempts = attempts.saturating_add(1);
            continue;
        };
        let private = RsaPrivateKey {
            n: n.clone(),
            d,
            crt: Some(RsaPrivateCrtComponents { p, q, dp, dq, qinv }),
        };
        let public = RsaPublicKey { n, e };
        validate_private_components(&private.n, &private.d)?;
        validate_public_components(&public.n, &public.e)?;
        validate_crt_components(&private.n, private.crt.as_ref().expect("crt must exist"))?;
        return Ok((private, public));
    }
    Err(Error::StateError(
        "rsa key generation exhausted attempt budget",
    ))
}

/// Generates an RSA keypair with default public exponent `65537` using DRBG entropy.
///
/// # Arguments
/// * `modulus_bits`: Target modulus size in bits (supported range: 1024..=4096).
/// * `drbg`: DRBG source used to sample prime candidates.
///
/// # Returns
/// Generated `(private_key, public_key)` pair including CRT parameters.
#[cfg(feature = "hazardous-legacy-crypto")]
pub fn rsa_generate_keypair_auto(
    modulus_bits: usize,
    drbg: &mut HmacDrbgSha256,
) -> Result<(RsaPrivateKey, RsaPublicKey)> {
    rsa_generate_keypair_backend_auto(modulus_bits, 65_537, drbg)
}

/// Generates an RSA keypair under one secure minimum key-size policy.
///
/// # Arguments
/// * `modulus_bits`: Target modulus size in bits for generated key material.
/// * `public_exponent`: Public exponent value (must be odd and >= 3).
/// * `policy`: Secure minimum modulus-size policy to enforce.
/// * `drbg`: DRBG source used to sample prime candidates.
///
/// # Returns
/// Generated `(private_key, public_key)` pair when key size satisfies policy and backend support.
pub fn rsa_generate_keypair_with_policy_auto(
    modulus_bits: usize,
    public_exponent: u32,
    policy: RsaKeySizePolicy,
    drbg: &mut HmacDrbgSha256,
) -> Result<(RsaPrivateKey, RsaPublicKey)> {
    if !(RSA_MIN_SECURE_BITS..=RSA_KEYGEN_MAX_BITS).contains(&modulus_bits) {
        return Err(Error::InvalidLength(
            "secure rsa modulus bits must be in supported range 2048..=4096",
        ));
    }
    if modulus_bits < policy.min_bits() {
        return Err(Error::InvalidLength(
            "rsa modulus bits do not satisfy configured secure policy minimum",
        ));
    }
    rsa_generate_keypair_backend_auto(modulus_bits, public_exponent, drbg)
}

/// Generates an RSA keypair with secure minimum modulus policy and default exponent `65537`.
///
/// # Arguments
/// * `modulus_bits`: Target modulus size in bits for generated key material.
/// * `policy`: Secure minimum modulus-size policy to enforce.
/// * `drbg`: DRBG source used to sample prime candidates.
///
/// # Returns
/// Generated `(private_key, public_key)` pair when key size satisfies secure policy.
pub fn rsa_generate_keypair_secure_auto(
    modulus_bits: usize,
    policy: RsaKeySizePolicy,
    drbg: &mut HmacDrbgSha256,
) -> Result<(RsaPrivateKey, RsaPublicKey)> {
    rsa_generate_keypair_with_policy_auto(modulus_bits, 65_537, policy, drbg)
}

/// Hashes and signs message using RSASSA-PKCS1-v1_5 with SHA-256.
///
/// # Arguments
/// * `private`: RSA private key used to produce the signature.
/// * `msg`: Message bytes to hash and sign.
///
/// # Returns
/// PKCS#1 v1.5 RSA signature bytes.
pub fn rsassa_sha256_sign(private: &RsaPrivateKey, msg: &[u8]) -> Result<Vec<u8>> {
    private.sign_pkcs1_v15_sha256(msg)
}

/// Hashes and verifies message using RSASSA-PKCS1-v1_5 with SHA-256.
///
/// # Arguments
/// * `public`: RSA public key used to verify the signature.
/// * `msg`: Original message bytes.
/// * `signature`: Signature bytes to validate.
///
/// # Returns
/// `Ok(())` when the signature is valid.
pub fn rsassa_sha256_verify(public: &RsaPublicKey, msg: &[u8], signature: &[u8]) -> Result<()> {
    public.verify_pkcs1_v15_sha256(msg, signature)
}

/// Hashes and signs message using RSASSA-PKCS1-v1_5 with SHA-1.
///
/// # Arguments
/// * `private`: RSA private key used to produce the signature.
/// * `msg`: Message bytes to hash and sign.
///
/// # Returns
/// PKCS#1 v1.5 RSA signature bytes.
pub fn rsassa_sha1_sign(private: &RsaPrivateKey, msg: &[u8]) -> Result<Vec<u8>> {
    private.sign_pkcs1_v15_sha1(msg)
}

/// Hashes and verifies message using RSASSA-PKCS1-v1_5 with SHA-1.
///
/// # Arguments
/// * `public`: RSA public key used to verify the signature.
/// * `msg`: Original message bytes.
/// * `signature`: Signature bytes to validate.
///
/// # Returns
/// `Ok(())` when the signature is valid.
pub fn rsassa_sha1_verify(public: &RsaPublicKey, msg: &[u8], signature: &[u8]) -> Result<()> {
    public.verify_pkcs1_v15_sha1(msg, signature)
}

/// Hashes and signs message using RSASSA-PKCS1-v1_5 with SHA-384.
///
/// # Arguments
/// * `private`: RSA private key used to produce the signature.
/// * `msg`: Message bytes to hash and sign.
///
/// # Returns
/// PKCS#1 v1.5 RSA signature bytes.
pub fn rsassa_sha384_sign(private: &RsaPrivateKey, msg: &[u8]) -> Result<Vec<u8>> {
    private.sign_pkcs1_v15_sha384(msg)
}

/// Hashes and verifies message using RSASSA-PKCS1-v1_5 with SHA-384.
///
/// # Arguments
/// * `public`: RSA public key used to verify the signature.
/// * `msg`: Original message bytes.
/// * `signature`: Signature bytes to validate.
///
/// # Returns
/// `Ok(())` when the signature is valid.
pub fn rsassa_sha384_verify(public: &RsaPublicKey, msg: &[u8], signature: &[u8]) -> Result<()> {
    public.verify_pkcs1_v15_sha384(msg, signature)
}

/// Hashes and signs message using RSASSA-PKCS1-v1_5 with SHA-512.
///
/// # Arguments
/// * `private`: RSA private key used to produce the signature.
/// * `msg`: Message bytes to hash and sign.
///
/// # Returns
/// PKCS#1 v1.5 RSA signature bytes.
pub fn rsassa_sha512_sign(private: &RsaPrivateKey, msg: &[u8]) -> Result<Vec<u8>> {
    private.sign_pkcs1_v15_sha512(msg)
}

/// Hashes and verifies message using RSASSA-PKCS1-v1_5 with SHA-512.
///
/// # Arguments
/// * `public`: RSA public key used to verify the signature.
/// * `msg`: Original message bytes.
/// * `signature`: Signature bytes to validate.
///
/// # Returns
/// `Ok(())` when the signature is valid.
pub fn rsassa_sha512_verify(public: &RsaPublicKey, msg: &[u8], signature: &[u8]) -> Result<()> {
    public.verify_pkcs1_v15_sha512(msg, signature)
}

/// Signs message using RSASSA-PSS with SHA-256 and caller-provided salt.
///
/// # Arguments
/// * `private`: RSA private key used to sign.
/// * `msg`: Message bytes to hash and sign.
/// * `salt`: Caller-provided random salt used by PSS encoding.
///
/// # Returns
/// RSASSA-PSS signature bytes.
pub fn rsassa_pss_sha256_sign(private: &RsaPrivateKey, msg: &[u8], salt: &[u8]) -> Result<Vec<u8>> {
    private.sign_pss_sha256(msg, salt)
}

/// Signs message using RSASSA-PSS with SHA-256 and DRBG-generated salt.
///
/// # Arguments
/// * `private`: RSA private key used to sign.
/// * `msg`: Message bytes to hash and sign.
/// * `drbg`: DRBG used to generate PSS salt bytes.
/// * `salt_len`: Requested salt length in bytes.
///
/// # Returns
/// RSASSA-PSS signature bytes.
pub fn rsassa_pss_sha256_sign_auto(
    private: &RsaPrivateKey,
    msg: &[u8],
    drbg: &mut HmacDrbgSha256,
    salt_len: usize,
) -> Result<Vec<u8>> {
    let salt = drbg.generate(salt_len, b"rsa_pss_sha256_salt")?;
    private.sign_pss_sha256(msg, &salt)
}

/// Verifies RSASSA-PSS signature for SHA-256 with expected salt length.
///
/// # Arguments
/// * `public`: RSA public key used to verify.
/// * `msg`: Original message bytes.
/// * `signature`: Signature bytes to validate.
/// * `salt_len`: Expected salt length used in PSS encoding.
///
/// # Returns
/// `Ok(())` when the signature is valid.
pub fn rsassa_pss_sha256_verify(
    public: &RsaPublicKey,
    msg: &[u8],
    signature: &[u8],
    salt_len: usize,
) -> Result<()> {
    public.verify_pss_sha256(msg, signature, salt_len)
}

/// Signs message using RSASSA-PSS with SHA-384 and caller-provided salt.
///
/// # Arguments
/// * `private`: RSA private key used to sign.
/// * `msg`: Message bytes to hash and sign.
/// * `salt`: Caller-provided random salt used by PSS encoding.
///
/// # Returns
/// RSASSA-PSS signature bytes.
pub fn rsassa_pss_sha384_sign(private: &RsaPrivateKey, msg: &[u8], salt: &[u8]) -> Result<Vec<u8>> {
    private.sign_pss_sha384(msg, salt)
}

/// Signs message using RSASSA-PSS with SHA-384 and DRBG-generated salt.
///
/// # Arguments
/// * `private`: RSA private key used to sign.
/// * `msg`: Message bytes to hash and sign.
/// * `drbg`: DRBG used to generate PSS salt bytes.
/// * `salt_len`: Requested salt length in bytes.
///
/// # Returns
/// RSASSA-PSS signature bytes.
pub fn rsassa_pss_sha384_sign_auto(
    private: &RsaPrivateKey,
    msg: &[u8],
    drbg: &mut HmacDrbgSha256,
    salt_len: usize,
) -> Result<Vec<u8>> {
    let salt = drbg.generate(salt_len, b"rsa_pss_sha384_salt")?;
    private.sign_pss_sha384(msg, &salt)
}

/// Verifies RSASSA-PSS signature for SHA-384 with expected salt length.
///
/// # Arguments
/// * `public`: RSA public key used to verify.
/// * `msg`: Original message bytes.
/// * `signature`: Signature bytes to validate.
/// * `salt_len`: Expected salt length used in PSS encoding.
///
/// # Returns
/// `Ok(())` when the signature is valid.
pub fn rsassa_pss_sha384_verify(
    public: &RsaPublicKey,
    msg: &[u8],
    signature: &[u8],
    salt_len: usize,
) -> Result<()> {
    public.verify_pss_sha384(msg, signature, salt_len)
}

/// Encrypts plaintext using RSAES-PKCS1-v1_5 with DRBG-generated non-zero padding.
///
/// # Arguments
/// * `public`: RSA public key used to encrypt.
/// * `plaintext`: Plaintext bytes to encrypt.
/// * `drbg`: DRBG used to generate PKCS#1 v1.5 PS bytes.
///
/// # Returns
/// Ciphertext bytes padded to modulus length.
pub fn rsaes_pkcs1_v15_encrypt_auto(
    public: &RsaPublicKey,
    plaintext: &[u8],
    drbg: &mut HmacDrbgSha256,
) -> Result<Vec<u8>> {
    public.encrypt_pkcs1_v15_auto(plaintext, drbg)
}

/// Decrypts RSAES-PKCS1-v1_5 ciphertext.
///
/// # Arguments
/// * `private`: RSA private key used to decrypt.
/// * `ciphertext`: Ciphertext bytes to decrypt.
///
/// # Returns
/// Decrypted plaintext bytes.
pub fn rsaes_pkcs1_v15_decrypt(private: &RsaPrivateKey, ciphertext: &[u8]) -> Result<Vec<u8>> {
    private.decrypt_pkcs1_v15(ciphertext)
}

/// Decrypts RSAES-PKCS1-v1_5 ciphertext via CRT-only compatibility API.
///
/// # Arguments
/// * `private`: RSA private key used to decrypt.
/// * `ciphertext`: Ciphertext bytes to decrypt.
///
/// # Returns
/// Decrypted plaintext bytes.
///
/// # Notes
/// This API mirrors the C compatibility surface. Current Rust key material stores
/// `(n, d)` only, so it delegates to standard private exponent decryption while
/// preserving external API shape for parity tracking.
pub fn rsaes_pkcs1_v15_decrypt_crt_only(
    private: &RsaPrivateKey,
    ciphertext: &[u8],
) -> Result<Vec<u8>> {
    private.decrypt_pkcs1_v15_crt_only(ciphertext)
}

/// Encrypts plaintext using RSAES-OAEP with SHA-256 and DRBG-derived seed.
///
/// # Arguments
/// * `public`: RSA public key used to encrypt.
/// * `plaintext`: Plaintext bytes to encrypt.
/// * `label`: OAEP label bytes hashed into encoding parameters.
/// * `drbg`: DRBG used to generate OAEP seed bytes.
///
/// # Returns
/// Ciphertext bytes padded to modulus length.
pub fn rsaes_oaep_sha256_encrypt_auto(
    public: &RsaPublicKey,
    plaintext: &[u8],
    label: &[u8],
    drbg: &mut HmacDrbgSha256,
) -> Result<Vec<u8>> {
    public.encrypt_oaep_sha256_auto(plaintext, label, drbg)
}

/// Decrypts RSAES-OAEP ciphertext with SHA-256 and caller-provided label.
///
/// # Arguments
/// * `private`: RSA private key used to decrypt.
/// * `ciphertext`: Ciphertext bytes to decrypt.
/// * `label`: OAEP label bytes hashed into encoding parameters.
///
/// # Returns
/// Decrypted plaintext bytes.
pub fn rsaes_oaep_sha256_decrypt(
    private: &RsaPrivateKey,
    ciphertext: &[u8],
    label: &[u8],
) -> Result<Vec<u8>> {
    private.decrypt_oaep_sha256(ciphertext, label)
}

/// Decrypts RSAES-OAEP ciphertext via CRT-only compatibility API.
///
/// # Arguments
/// * `private`: RSA private key used to decrypt.
/// * `ciphertext`: Ciphertext bytes to decrypt.
/// * `label`: OAEP label bytes hashed into encoding parameters.
///
/// # Returns
/// Decrypted plaintext bytes.
pub fn rsaes_oaep_sha256_decrypt_crt_only(
    private: &RsaPrivateKey,
    ciphertext: &[u8],
    label: &[u8],
) -> Result<Vec<u8>> {
    private.decrypt_oaep_sha256_crt_only(ciphertext, label)
}

const PKCS1_V15_DIGESTINFO_SHA1_PREFIX: &[u8] = &[
    0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
];
const PKCS1_V15_DIGESTINFO_SHA256_PREFIX: &[u8] = &[
    0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
    0x00, 0x04, 0x20,
];
const PKCS1_V15_DIGESTINFO_SHA384_PREFIX: &[u8] = &[
    0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
    0x00, 0x04, 0x30,
];
const PKCS1_V15_DIGESTINFO_SHA512_PREFIX: &[u8] = &[
    0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
    0x00, 0x04, 0x40,
];

/// Encodes digest bytes into EMSA-PKCS1-v1_5 block for modulus length `k`.
///
/// # Arguments
///
/// * `hash` — `&[u8]`.
/// * `digest_info_prefix` — `&[u8]`.
/// * `k` — `usize`.
///
/// # Returns
///
/// On success, the `Ok` payload from `emsa_pkcs1_v15_encode`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn emsa_pkcs1_v15_encode(hash: &[u8], digest_info_prefix: &[u8], k: usize) -> Result<Vec<u8>> {
    let t_len = digest_info_prefix.len() + hash.len();
    if k < t_len + 11 {
        return Err(Error::InvalidLength("rsa modulus too short for pkcs1 v1.5"));
    }
    let ps_len = k - t_len - 3;
    let mut em = Vec::with_capacity(k);
    em.push(0x00);
    em.push(0x01);
    em.extend(core::iter::repeat_n(0xff_u8, ps_len));
    em.push(0x00);
    em.extend_from_slice(digest_info_prefix);
    em.extend_from_slice(hash);
    Ok(em)
}

/// Encodes a message hash using EMSA-PSS (SHA-256) with caller-provided salt.
///
/// # Arguments
///
/// * `m_hash` — 32-byte message digest.
/// * `salt` — PSS salt bytes.
/// * `em_bits` — Effective encoded message bit length.
/// * `em_len` — Encoded message byte length `em_bits` maps to.
///
/// # Returns
///
/// On success, the encoded message bytes.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when the modulus is too short for the chosen parameters.
///
/// # Panics
///
/// This function does not panic.
fn emsa_pss_encode_sha256(
    m_hash: &[u8; 32],
    salt: &[u8],
    em_bits: usize,
    em_len: usize,
) -> Result<Vec<u8>> {
    const HASH_LEN: usize = 32;
    if em_len < HASH_LEN + salt.len() + 2 {
        return Err(Error::InvalidLength("rsa modulus too short for pss"));
    }

    let mut m_prime = vec![0_u8; 8];
    m_prime.extend_from_slice(m_hash);
    m_prime.extend_from_slice(salt);
    let h = sha256(&m_prime);

    let ps_len = em_len - salt.len() - HASH_LEN - 2;
    let mut db = vec![0_u8; ps_len];
    db.push(0x01);
    db.extend_from_slice(salt);

    let db_mask = mgf1_sha256(&h, em_len - HASH_LEN - 1)?;
    for (byte, mask) in db.iter_mut().zip(db_mask.iter()) {
        *byte ^= *mask;
    }

    let unused_bits = 8 * em_len - em_bits;
    if unused_bits > 0 {
        db[0] &= 0xff_u8 >> unused_bits;
    }

    let mut em = db;
    em.extend_from_slice(&h);
    em.push(0xbc);
    Ok(em)
}

/// Verifies an EMSA-PSS (SHA-256) encoded message block against a digest and salt length.
///
/// # Arguments
///
/// * `m_hash` — Expected 32-byte message digest.
/// * `em` — Encoded message bytes to verify.
/// * `em_bits` — Effective encoded message bit length.
/// * `salt_len` — Expected salt byte length embedded in the encoding.
///
/// # Returns
///
/// `Ok(())` when the PSS structure and digest match.
///
/// # Errors
///
/// Returns `noxtls_core::Error` on malformed padding, hash mismatch, or insufficient length.
///
/// # Panics
///
/// This function does not panic.
fn emsa_pss_verify_sha256(
    m_hash: &[u8; 32],
    em: &[u8],
    em_bits: usize,
    salt_len: usize,
) -> Result<()> {
    const HASH_LEN: usize = 32;
    if em.len() < HASH_LEN + salt_len + 2 {
        return Err(Error::InvalidLength("rsa modulus too short for pss"));
    }
    if em.last().copied() != Some(0xbc) {
        return Err(Error::CryptoFailure("RSA verification failed"));
    }

    let db_len = em.len() - HASH_LEN - 1;
    let (masked_db, rest) = em.split_at(db_len);
    let h = &rest[..HASH_LEN];

    let unused_bits = 8 * em.len() - em_bits;
    if unused_bits > 0 {
        let mask = 0xff_u8 << (8 - unused_bits);
        if masked_db[0] & mask != 0 {
            return Err(Error::CryptoFailure("RSA verification failed"));
        }
    }

    let db_mask = mgf1_sha256(h, db_len)?;
    let mut db = masked_db.to_vec();
    for (byte, mask) in db.iter_mut().zip(db_mask.iter()) {
        *byte ^= *mask;
    }
    if unused_bits > 0 {
        db[0] &= 0xff_u8 >> unused_bits;
    }

    let ps_len = em.len() - HASH_LEN - salt_len - 2;
    if !ct_all_zero(&db[..ps_len]) || db[ps_len] != 0x01 {
        return Err(Error::CryptoFailure("RSA verification failed"));
    }
    let salt = &db[db.len() - salt_len..];

    let mut m_prime = vec![0_u8; 8];
    m_prime.extend_from_slice(m_hash);
    m_prime.extend_from_slice(salt);
    let expected_h = sha256(&m_prime);
    if ct_bytes_eq(expected_h.as_slice(), h) {
        Ok(())
    } else {
        Err(Error::CryptoFailure("RSA verification failed"))
    }
}

/// Encodes a message hash using EMSA-PSS (SHA-384) with caller-provided salt.
///
/// # Arguments
///
/// * `m_hash` — 48-byte message digest.
/// * `salt` — PSS salt bytes.
/// * `em_bits` — Effective encoded message bit length.
/// * `em_len` — Encoded message byte length.
///
/// # Returns
///
/// On success, the encoded message bytes.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when the modulus is too short for the chosen parameters.
///
/// # Panics
///
/// This function does not panic.
fn emsa_pss_encode_sha384(
    m_hash: &[u8; 48],
    salt: &[u8],
    em_bits: usize,
    em_len: usize,
) -> Result<Vec<u8>> {
    const HASH_LEN: usize = 48;
    if em_len < HASH_LEN + salt.len() + 2 {
        return Err(Error::InvalidLength("rsa modulus too short for pss"));
    }

    let mut m_prime = vec![0_u8; 8];
    m_prime.extend_from_slice(m_hash);
    m_prime.extend_from_slice(salt);
    let h = sha384(&m_prime);

    let ps_len = em_len - salt.len() - HASH_LEN - 2;
    let mut db = vec![0_u8; ps_len];
    db.push(0x01);
    db.extend_from_slice(salt);

    let db_mask = mgf1_sha384(&h, em_len - HASH_LEN - 1)?;
    for (byte, mask) in db.iter_mut().zip(db_mask.iter()) {
        *byte ^= *mask;
    }

    let unused_bits = 8 * em_len - em_bits;
    if unused_bits > 0 {
        db[0] &= 0xff_u8 >> unused_bits;
    }

    let mut em = db;
    em.extend_from_slice(&h);
    em.push(0xbc);
    Ok(em)
}

/// Verifies an EMSA-PSS (SHA-384) encoded message block against a digest and salt length.
///
/// # Arguments
///
/// * `m_hash` — Expected 48-byte message digest.
/// * `em` — Encoded message bytes to verify.
/// * `em_bits` — Effective encoded message bit length.
/// * `salt_len` — Expected salt byte length.
///
/// # Returns
///
/// `Ok(())` when the PSS structure and digest match.
///
/// # Errors
///
/// Returns `noxtls_core::Error` on malformed padding, hash mismatch, or insufficient length.
///
/// # Panics
///
/// This function does not panic.
fn emsa_pss_verify_sha384(
    m_hash: &[u8; 48],
    em: &[u8],
    em_bits: usize,
    salt_len: usize,
) -> Result<()> {
    const HASH_LEN: usize = 48;
    if em.len() < HASH_LEN + salt_len + 2 {
        return Err(Error::InvalidLength("rsa modulus too short for pss"));
    }
    if em.last().copied() != Some(0xbc) {
        return Err(Error::CryptoFailure("RSA verification failed"));
    }

    let db_len = em.len() - HASH_LEN - 1;
    let (masked_db, rest) = em.split_at(db_len);
    let h = &rest[..HASH_LEN];

    let unused_bits = 8 * em.len() - em_bits;
    if unused_bits > 0 {
        let mask = 0xff_u8 << (8 - unused_bits);
        if masked_db[0] & mask != 0 {
            return Err(Error::CryptoFailure("RSA verification failed"));
        }
    }

    let db_mask = mgf1_sha384(h, db_len)?;
    let mut db = masked_db.to_vec();
    for (byte, mask) in db.iter_mut().zip(db_mask.iter()) {
        *byte ^= *mask;
    }
    if unused_bits > 0 {
        db[0] &= 0xff_u8 >> unused_bits;
    }

    let ps_len = em.len() - HASH_LEN - salt_len - 2;
    if !ct_all_zero(&db[..ps_len]) || db[ps_len] != 0x01 {
        return Err(Error::CryptoFailure("RSA verification failed"));
    }
    let salt = &db[db.len() - salt_len..];

    let mut m_prime = vec![0_u8; 8];
    m_prime.extend_from_slice(m_hash);
    m_prime.extend_from_slice(salt);
    let expected_h = sha384(&m_prime);
    if ct_bytes_eq(expected_h.as_slice(), h) {
        Ok(())
    } else {
        Err(Error::CryptoFailure("RSA verification failed"))
    }
}

/// Implements MGF1 using SHA-256. Parameters: `seed` mask-generation seed and `out_len` requested mask length.
///
/// # Arguments
///
/// * `seed` — `&[u8]`.
/// * `out_len` — `usize`.
///
/// # Returns
///
/// On success, the `Ok` payload from `mgf1_sha256`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn mgf1_sha256(seed: &[u8], out_len: usize) -> Result<Vec<u8>> {
    let mut out = Vec::with_capacity(out_len);
    let mut counter = 0_u32;
    while out.len() < out_len {
        if counter == u32::MAX {
            return Err(Error::InvalidLength("mgf1 output too large"));
        }
        let mut block_input = Vec::with_capacity(seed.len() + 4);
        block_input.extend_from_slice(seed);
        block_input.extend_from_slice(&counter.to_be_bytes());
        out.extend_from_slice(&sha256(&block_input));
        counter = counter.wrapping_add(1);
    }
    out.truncate(out_len);
    Ok(out)
}

/// Implements MGF1 using SHA-384.
///
/// # Arguments
///
/// * `seed` — `&[u8]`.
/// * `out_len` — `usize`.
///
/// # Returns
///
/// On success, the `Ok` payload from `mgf1_sha384`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn mgf1_sha384(seed: &[u8], out_len: usize) -> Result<Vec<u8>> {
    let mut out = Vec::with_capacity(out_len);
    let mut counter = 0_u32;
    while out.len() < out_len {
        if counter == u32::MAX {
            return Err(Error::InvalidLength("mgf1 output too large"));
        }
        let mut block_input = Vec::with_capacity(seed.len() + 4);
        block_input.extend_from_slice(seed);
        block_input.extend_from_slice(&counter.to_be_bytes());
        out.extend_from_slice(&sha384(&block_input));
        counter = counter.wrapping_add(1);
    }
    out.truncate(out_len);
    Ok(out)
}

/// Builds DRBG-backed non-zero PKCS#1 v1.5 padding bytes for encryption.
///
/// # Arguments
///
/// * `drbg` — `&mut HmacDrbgSha256`.
/// * `len` — `usize`.
///
/// # Returns
///
/// On success, the `Ok` payload from `drbg_nonzero_padding`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn drbg_nonzero_padding(drbg: &mut HmacDrbgSha256, len: usize) -> Result<Vec<u8>> {
    let mut out = Vec::with_capacity(len);
    while out.len() < len {
        let block = drbg.generate(len.saturating_sub(out.len()), b"rsa_pkcs1_v15_ps")?;
        for byte in block {
            if byte != 0 {
                out.push(byte);
                if out.len() == len {
                    break;
                }
            }
        }
    }
    Ok(out)
}

/// Encodes a message with EME-OAEP-SHA256 using a caller-provided seed.
///
/// # Arguments
///
/// * `plaintext` — Message bytes to encode.
/// * `label` — OAEP label bytes.
/// * `seed` — 32-byte seed (must match `HASH_LEN`).
/// * `k` — Modulus byte length (encoded message size target).
///
/// # Returns
///
/// On success, the encoded message as a byte vector of length `k`.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when lengths are inconsistent with OAEP-SHA256 constraints.
///
/// # Panics
///
/// This function does not panic.
fn emea_oaep_encode_sha256(
    plaintext: &[u8],
    label: &[u8],
    seed: &[u8],
    k: usize,
) -> Result<Vec<u8>> {
    const HASH_LEN: usize = 32;
    if seed.len() != HASH_LEN {
        return Err(Error::InvalidLength("rsa oaep seed must be 32 bytes"));
    }
    if k < (2 * HASH_LEN + 2) {
        return Err(Error::InvalidLength(
            "rsa modulus too short for oaep sha256",
        ));
    }
    if plaintext.len() > k - (2 * HASH_LEN + 2) {
        return Err(Error::InvalidLength(
            "rsa plaintext too long for oaep sha256",
        ));
    }
    let l_hash = sha256(label);
    let ps_len = k - plaintext.len() - (2 * HASH_LEN + 2);
    let mut db = Vec::with_capacity(k - HASH_LEN - 1);
    db.extend_from_slice(&l_hash);
    db.extend(core::iter::repeat_n(0_u8, ps_len));
    db.push(0x01);
    db.extend_from_slice(plaintext);
    let db_mask = mgf1_sha256(seed, k - HASH_LEN - 1)?;
    for (byte, mask) in db.iter_mut().zip(db_mask.iter()) {
        *byte ^= *mask;
    }
    let seed_mask = mgf1_sha256(&db, HASH_LEN)?;
    let mut masked_seed = seed.to_vec();
    for (byte, mask) in masked_seed.iter_mut().zip(seed_mask.iter()) {
        *byte ^= *mask;
    }
    let mut em = Vec::with_capacity(k);
    em.push(0x00);
    em.extend_from_slice(&masked_seed);
    em.extend_from_slice(&db);
    Ok(em)
}

/// Decodes EME-OAEP encoded bytes using SHA-256 and caller-provided label.
///
/// # Arguments
///
/// * `encoded` — `&[u8]`.
/// * `label` — `&[u8]`.
///
/// # Returns
///
/// On success, the `Ok` payload from `decode_oaep_sha256_plaintext`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn decode_oaep_sha256_plaintext(encoded: &[u8], label: &[u8]) -> Result<Vec<u8>> {
    const HASH_LEN: usize = 32;
    if encoded.len() < (2 * HASH_LEN + 2) {
        return Err(Error::InvalidLength(
            "rsa modulus too short for oaep sha256",
        ));
    }
    let mut invalid = 0_u8;
    invalid |= encoded[0];
    let (masked_seed, masked_db) = encoded[1..].split_at(HASH_LEN);
    let seed_mask = mgf1_sha256(masked_db, HASH_LEN)?;
    let mut seed = masked_seed.to_vec();
    for (byte, mask) in seed.iter_mut().zip(seed_mask.iter()) {
        *byte ^= *mask;
    }
    let db_mask = mgf1_sha256(&seed, masked_db.len())?;
    let mut db = masked_db.to_vec();
    for (byte, mask) in db.iter_mut().zip(db_mask.iter()) {
        *byte ^= *mask;
    }
    let expected_l_hash = sha256(label);
    invalid |= u8::from(!ct_bytes_eq(&db[..HASH_LEN], expected_l_hash.as_slice()));
    let rest = &db[HASH_LEN..];
    let mut marker_idx = 0_usize;
    let mut found_marker = 0_u8;
    let mut invalid_ps = 0_u8;
    for (idx, &byte) in rest.iter().enumerate() {
        let is_zero = u8::from(byte == 0);
        let is_one = u8::from(byte == 1);
        let before_marker = 1_u8 ^ found_marker;
        let should_set = before_marker & is_one;
        marker_idx = ct_select_usize(should_set, idx, marker_idx);
        invalid_ps |= before_marker & (1_u8 ^ is_zero) & (1_u8 ^ is_one);
        found_marker |= is_one;
    }
    invalid |= invalid_ps;
    invalid |= 1_u8 ^ found_marker;
    if invalid != 0 {
        return Err(Error::CryptoFailure("rsa decryption failed"));
    }
    Ok(rest[marker_idx.saturating_add(1)..].to_vec())
}

/// Decodes PKCS#1 v1.5 encoded message and returns plaintext.
///
/// # Arguments
///
/// * `encoded` — `&[u8]`.
///
/// # Returns
///
/// On success, the `Ok` payload from `decode_pkcs1_v15_plaintext`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn decode_pkcs1_v15_plaintext(encoded: &[u8]) -> Result<Vec<u8>> {
    if encoded.len() < 11 {
        return Err(Error::CryptoFailure("rsa decryption failed"));
    }
    let mut invalid = 0_u8;
    invalid |= encoded[0];
    invalid |= encoded[1] ^ 0x02;

    let mut sep_idx = 0_usize;
    let mut found_sep = 0_u8;
    for (idx, &byte) in encoded.iter().enumerate().skip(2) {
        let is_zero = u8::from(byte == 0);
        let should_set = is_zero & (1_u8 ^ found_sep);
        sep_idx = ct_select_usize(should_set, idx, sep_idx);
        found_sep |= is_zero;
    }
    if found_sep == 0 {
        invalid |= 1;
    }
    if sep_idx < 10 {
        invalid |= 1;
    }
    if invalid != 0 {
        return Err(Error::CryptoFailure("rsa decryption failed"));
    }
    Ok(encoded[sep_idx + 1..].to_vec())
}

/// Compares two byte slices in constant-time when lengths are equal. Parameters: `left` and `right` byte slices to compare.
///
/// # Arguments
///
/// * `left` — `&[u8]`.
/// * `right` — `&[u8]`.
///
/// # Returns
///
/// `bool` produced by `ct_bytes_eq` (see implementation).
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn ct_bytes_eq(left: &[u8], right: &[u8]) -> bool {
    if left.len() != right.len() {
        return false;
    }
    let mut diff = 0_u8;
    for (&l, &r) in left.iter().zip(right.iter()) {
        diff |= l ^ r;
    }
    diff == 0
}

/// Returns true when every byte in one slice is zero without early exit. Parameter: `bytes` candidate slice.
///
/// # Arguments
///
/// * `bytes` — `&[u8]`.
///
/// # Returns
///
/// `bool` produced by `ct_all_zero` (see implementation).
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn ct_all_zero(bytes: &[u8]) -> bool {
    let mut acc = 0_u8;
    for &byte in bytes {
        acc |= byte;
    }
    acc == 0
}

/// Selects one of two usize values using one-byte selector without branch-on-secret. Parameters: `selector` must be 0 or 1, `if_one` selected when 1, `if_zero` when 0.
///
/// # Arguments
///
/// * `selector` — `u8`.
/// * `if_one` — `usize`.
/// * `if_zero` — `usize`.
///
/// # Returns
///
/// `usize` produced by `ct_select_usize` (see implementation).
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn ct_select_usize(selector: u8, if_one: usize, if_zero: usize) -> usize {
    let mask = (0_usize).wrapping_sub(usize::from(selector));
    (if_one & mask) | (if_zero & !mask)
}

/// Validates RSA private-key scalar components before private operations.
///
/// # Arguments
///
/// * `n` — `&BigUint`.
/// * `d` — `&BigUint`.
///
/// # Returns
///
/// On success, the `Ok` payload from `validate_private_components`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn validate_private_components(n: &BigUint, d: &BigUint) -> Result<()> {
    validate_modulus(n)?;
    if d.is_zero() {
        return Err(Error::CryptoFailure(
            "rsa private exponent must be non-zero",
        ));
    }
    if !d.is_odd() {
        return Err(Error::CryptoFailure("rsa private exponent must be odd"));
    }
    if d.cmp(n).is_ge() {
        return Err(Error::CryptoFailure(
            "rsa private exponent must be smaller than modulus",
        ));
    }
    Ok(())
}

/// Validates RSA public-key scalar components before public operations.
///
/// # Arguments
///
/// * `n` — `&BigUint`.
/// * `e` — `&BigUint`.
///
/// # Returns
///
/// On success, the `Ok` payload from `validate_public_components`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn validate_public_components(n: &BigUint, e: &BigUint) -> Result<()> {
    validate_modulus(n)?;
    let three = BigUint::from_u128(3);
    if e.cmp(&three).is_lt() {
        return Err(Error::CryptoFailure(
            "rsa public exponent must be at least 3",
        ));
    }
    if !e.is_odd() {
        return Err(Error::CryptoFailure("rsa public exponent must be odd"));
    }
    if e.cmp(n).is_ge() {
        return Err(Error::CryptoFailure(
            "rsa public exponent must be smaller than modulus",
        ));
    }
    Ok(())
}

/// Validates shared modulus requirements for RSA public/private keys.
///
/// # Arguments
///
/// * `n` — `&BigUint`.
///
/// # Returns
///
/// On success, the `Ok` payload from `validate_modulus`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn validate_modulus(n: &BigUint) -> Result<()> {
    let three = BigUint::from_u128(3);
    if n.cmp(&three).is_lt() {
        return Err(Error::CryptoFailure("rsa modulus must be greater than 3"));
    }
    if !n.is_odd() {
        return Err(Error::CryptoFailure("rsa modulus must be odd"));
    }
    Ok(())
}

/// Validates CRT parameter relationships for a private RSA key.
///
/// # Arguments
///
/// * `n` — `&BigUint`.
/// * `crt` — `&RsaPrivateCrtComponents`.
///
/// # Returns
///
/// On success, the `Ok` payload from `validate_crt_components`; see implementation for value shape.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when validation or a numeric step fails; see implementation for specific variants.
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn validate_crt_components(n: &BigUint, crt: &RsaPrivateCrtComponents) -> Result<()> {
    if crt.p.is_zero()
        || crt.q.is_zero()
        || crt.dp.is_zero()
        || crt.dq.is_zero()
        || crt.qinv.is_zero()
    {
        return Err(Error::CryptoFailure("rsa crt parameters must be non-zero"));
    }
    if !crt.p.is_odd() || !crt.q.is_odd() {
        return Err(Error::CryptoFailure("rsa crt primes must be odd"));
    }
    if crt.p.mul(&crt.q).cmp(n).is_ne() {
        return Err(Error::CryptoFailure(
            "rsa crt prime product must equal modulus",
        ));
    }
    if crt.dp.cmp(&crt.p).is_ge() || crt.dq.cmp(&crt.q).is_ge() {
        return Err(Error::CryptoFailure("rsa crt exponents must be reduced"));
    }
    if crt.qinv.cmp(&crt.p).is_ge() {
        return Err(Error::CryptoFailure(
            "rsa crt coefficient must be smaller than p",
        ));
    }
    let one = BigUint::one();
    if crt.q.mul(&crt.qinv).modulo(&crt.p).cmp(&one).is_ne() {
        return Err(Error::CryptoFailure(
            "rsa crt coefficient must be inverse of q modulo p",
        ));
    }
    Ok(())
}

/// Samples odd RSA prime candidates until one passes primality and coprimality checks.
///
/// # Arguments
///
/// * `bits` — Desired prime bit width.
/// * `e` — Public exponent used for the gcd(`p-1`, `e`) test.
/// * `drbg` — DRBG source for random candidates.
///
/// # Returns
///
/// On success, a probable prime `BigUint` of the requested width.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when randomness is unavailable or generation exhausts its attempt budget.
///
/// # Panics
///
/// This function does not panic.
fn generate_rsa_prime_candidate_auto(
    bits: usize,
    e: &BigUint,
    drbg: &mut HmacDrbgSha256,
) -> Result<BigUint> {
    let one = BigUint::one();
    let mut attempts = 0_u32;
    while attempts < 20_000 {
        let candidate = random_biguint_with_bits(bits, drbg, b"rsa_prime_candidate")?;
        if candidate.bit_len() != bits {
            attempts = attempts.saturating_add(1);
            continue;
        }
        if !is_probable_prime(&candidate) {
            attempts = attempts.saturating_add(1);
            continue;
        }
        let pm1 = candidate.sub(&one);
        if BigUint::gcd(e, &pm1).cmp(&one).is_eq() {
            return Ok(candidate);
        }
        attempts = attempts.saturating_add(1);
    }
    Err(Error::StateError(
        "rsa prime generation exhausted attempt budget",
    ))
}

/// Samples a random odd `BigUint` with an exact bit width from DRBG output.
///
/// # Arguments
///
/// * `bits` — Target bit width (at least 2).
/// * `drbg` — DRBG used to draw random bytes.
/// * `label` — Domain separation label passed to `drbg.generate`.
///
/// # Returns
///
/// On success, an odd integer occupying exactly `bits` bits.
///
/// # Errors
///
/// Returns `noxtls_core::Error` when `bits` is too small or DRBG output is insufficient.
///
/// # Panics
///
/// This function does not panic.
fn random_biguint_with_bits(
    bits: usize,
    drbg: &mut HmacDrbgSha256,
    label: &[u8],
) -> Result<BigUint> {
    if bits < 2 {
        return Err(Error::InvalidLength(
            "rsa prime candidate bits must be at least 2",
        ));
    }
    let byte_len = bits.div_ceil(8);
    let mut random = drbg.generate(byte_len, label)?;
    let top_bits = bits % 8;
    if top_bits != 0 {
        random[0] &= (1_u8 << top_bits) - 1;
    }
    let high_bit_index = (bits - 1) % 8;
    random[0] |= 1_u8 << high_bit_index;
    let last = random.len() - 1;
    random[last] |= 1;
    Ok(BigUint::from_be_bytes(&random))
}

/// Performs probabilistic primality check for BigUint candidates.
///
/// # Arguments
///
/// * `n` — `&BigUint`.
///
/// # Returns
///
/// `bool` produced by `is_probable_prime` (see implementation).
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn is_probable_prime(n: &BigUint) -> bool {
    let two = BigUint::from_u128(2);
    if n.cmp(&two).is_lt() {
        return false;
    }
    for small in [2_u32, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] {
        let small_bn = BigUint::from_u128(u128::from(small));
        if n.cmp(&small_bn).is_eq() {
            return true;
        }
        if n.mod_u32(small) == 0 {
            return false;
        }
    }
    let one = BigUint::one();
    let n_minus_one = n.sub(&one);
    let mut d = n_minus_one.clone();
    let mut s = 0_u32;
    while d.is_even() {
        d = d.shr1();
        s = s.saturating_add(1);
    }
    for witness in [2_u32, 3, 5, 7, 11, 13, 17, 19, 23, 29] {
        if !miller_rabin_round(n, &d, s, witness) {
            return false;
        }
    }
    true
}

/// Runs one Miller-Rabin witness round for one odd candidate.
///
/// # Arguments
///
/// * `n` — `&BigUint`.
/// * `d` — `&BigUint`.
/// * `s` — `u32`.
/// * `witness` — `u32`.
///
/// # Returns
///
/// `bool` produced by `miller_rabin_round` (see implementation).
///
/// # Panics
///
/// This function does not panic unless otherwise noted.
fn miller_rabin_round(n: &BigUint, d: &BigUint, s: u32, witness: u32) -> bool {
    let a = BigUint::from_u128(u128::from(witness)).modulo(n);
    if a.is_zero() {
        return true;
    }
    let one = BigUint::one();
    let n_minus_one = n.sub(&one);
    let mut x = BigUint::mod_exp(&a, d, n);
    if x.cmp(&one).is_eq() || x.cmp(&n_minus_one).is_eq() {
        return true;
    }
    for _ in 1..s {
        x = x.mul(&x).modulo(n);
        if x.cmp(&n_minus_one).is_eq() {
            return true;
        }
    }
    false
}