flpdf 0.1.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
//! Standard Security Handler key derivation for PDF V=1, V=2, V=4, and V=5.
//!
//! Implements the following algorithms from PDF 1.7 §7.6.3.3:
//! - **Algorithm 2**: Compute the file encryption key from password + dictionary entries.
//! - **Algorithm 6**: Test a user password (returns the file key on success).
//! - **Algorithm 7**: Test an owner password (returns the file key on success).
//!
//! Output is an RC4 file key of 40–128 bits (5–16 bytes).
//!
//! # V=4 Crypt Filter support
//! This module also contains the V=4 key derivation shim (`compute_file_key_v4`) and the
//! Crypt Filter (CF) dispatch types: `CryptFilterMethod`, `CryptFilter`, `CryptFilterRef`,
//! and `select_crypt_filter`. The `/StmF`, `/StrF`, `/EFF` use-site selection and the
//! `cfm_to_object_key_alg` helper are included for completeness.
//!
//! Actual PDF parsing of the `/Encrypt` dictionary and end-to-end round-trip decryption
//! are deferred to issues flpdf-9hc.3.7, 3.8, 3.14, and 3.15.
//!
//! # V=5 AES-256 support
//! R=5 is the deprecated pre-ISO 32000-2 AES-256 Standard handler. It uses a
//! single SHA-256 pass over `password || salt` and AES-256-CBC with a zero IV to
//! unwrap the 256-bit file key from `/UE` or `/OE`. New output using this legacy
//! handler must remain behind weak-crypto opt-in if writer support is added.
//! R=6 is the ISO 32000-2 AES-256 Standard handler. It keeps the same dictionary
//! entry shape and replaces the salted hash with Algorithm 2.B's iterative
//! SHA-256/384/512 construction.
//!
//! # Scope
//! V=1 (R=2, 40-bit), V=2 (R=2/R=3, 40–128-bit), V=4 (R=4, 128-bit), and
//! V=5 R=5/R=6 AES-256 key derivation are covered here.
//!
//! # Note on end-to-end compatibility
//! Full qpdf-compatible fixture testing (real encrypted PDF files) is deferred to
//! issues flpdf-9hc.3.14 and flpdf-9hc.3.15.  The tests here use Python-generated
//! known-answer vectors (see inline comments) to verify algorithmic correctness.
//!
//! # Dead-code notice
//! The items in this module are scaffolding for the encrypted-PDF support epic
//! (flpdf-9hc.3) and are not yet wired up to any call site within the lower
//! stack layers. They become live as subsequent layers (string decryption,
//! stream decryption, CLI `--password`) land. The module-level
//! `allow(dead_code)` keeps each layer's CI green without silencing the lint
//! elsewhere.
#![allow(dead_code)]

use crate::error::{EncryptedError, Result};
use crate::security::primitives::{md5, rc4, sha256, sha384, sha512};
use crate::{Object, ObjectRef};
use aes::{Aes128, Aes256};
use cbc::cipher::{block_padding::NoPadding, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
use cbc::{Decryptor, Encryptor};

// ────────────────────────────────────────────────────────────────────────────
// Constants
// ────────────────────────────────────────────────────────────────────────────

/// The 32-byte password-padding string from PDF 1.7 §7.6.3.3, Algorithm 2, step 1.
pub(crate) const PASSWORD_PADDING: [u8; 32] = [
    0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
    0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A,
];

// ────────────────────────────────────────────────────────────────────────────
// Public types
// ────────────────────────────────────────────────────────────────────────────

/// All dictionary-derived inputs needed for V=1/V=2 key derivation.
///
/// Callers are responsible for parsing these fields from the `/Encrypt`
/// dictionary before invoking the functions in this module.
pub(crate) struct StandardHandlerInputs<'a> {
    /// `/V` — algorithm version (1 or 2).
    pub v: i64,
    /// `/R` — revision (2 or 3 for V=1/V=2).
    pub r: i64,
    /// `/Length` in bits (40–128, must be a multiple of 8).
    pub length_bits: i64,
    /// `/P` — permissions flags (signed 32-bit, encoded as 4-byte little-endian).
    pub p: i32,
    /// First element of the `/ID` array.
    pub id0: &'a [u8],
    /// `/U` entry — 32 bytes.
    pub u: &'a [u8; 32],
    /// `/O` entry — 32 bytes.
    pub o: &'a [u8; 32],
    /// `/EncryptMetadata` flag.  For R ≤ 3, this is always treated as `true`
    /// per spec (the 0xFF×4 tail in Algorithm 2 is only appended for R ≥ 4).
    pub encrypt_metadata: bool,
}

/// Dictionary-derived inputs for legacy V=5 R=5 AES-256 key derivation.
///
/// `/U` and `/O` are 48 bytes each: 32-byte validation hash, 8-byte validation
/// salt, and 8-byte file-key salt. `/UE` and `/OE` are the 32-byte AES-256-CBC
/// encrypted file-key entries.
pub(crate) struct StandardHandlerR5Inputs<'a> {
    /// `/U` entry — validation hash, validation salt, key salt.
    pub u: &'a [u8; 48],
    /// `/O` entry — validation hash, validation salt, key salt.
    pub o: &'a [u8; 48],
    /// `/UE` entry — encrypted file key for the user password.
    pub ue: &'a [u8; 32],
    /// `/OE` entry — encrypted file key for the owner password.
    pub oe: &'a [u8; 32],
}

// ────────────────────────────────────────────────────────────────────────────
// Internal helpers
// ────────────────────────────────────────────────────────────────────────────

/// Pad or truncate `password` to exactly 32 bytes using `PASSWORD_PADDING`.
///
/// PDF 1.7 §7.6.3.3 Algorithm 2, step 1:
/// > Pad or truncate the password string to exactly 32 bytes.  If the password
/// > string is more than 32 bytes long, use only its first 32 bytes; if it is
/// > less than 32 bytes long, pad it by appending the required number of
/// > additional bytes from the beginning of the following padding string.
fn pad_password(password: &[u8]) -> [u8; 32] {
    let mut out = [0u8; 32];
    let pw_len = password.len().min(32);
    out[..pw_len].copy_from_slice(&password[..pw_len]);
    if pw_len < 32 {
        let pad_needed = 32 - pw_len;
        out[pw_len..].copy_from_slice(&PASSWORD_PADDING[..pad_needed]);
    }
    out
}

/// Validate `inputs` fields that are in scope for V=1/V=2.
///
/// Only the V/R/Length combinations that this module's Algorithms 2/6/7
/// actually implement are accepted:
///
/// - V=1 ⇒ R=2 and Length=40 (RC4-40, fixed)
/// - V=2 ⇒ R∈{2,3} and Length∈[40,128] in 8-bit steps (RC4-{40..128})
///
/// Other handlers (V=4 CF dispatch, V=5 R=5/R=6 AES-256) belong to other
/// subtasks. Refusing them here prevents wrong-handler inputs from
/// silently flowing into the R≥3/R≥4 branches in `compute_file_key()`
/// and `check_user_password()`.
fn validate_inputs(inputs: &StandardHandlerInputs<'_>) -> Result<usize> {
    if inputs.v != 1 && inputs.v != 2 {
        return Err(EncryptedError::UnsupportedHandler {
            filter: "Standard".into(),
            v: inputs.v,
            r: inputs.r,
            cfm: None,
        }
        .into());
    }
    // R must be a revision this module handles.
    if inputs.r != 2 && inputs.r != 3 {
        return Err(EncryptedError::UnsupportedHandler {
            filter: "Standard".into(),
            v: inputs.v,
            r: inputs.r,
            cfm: None,
        }
        .into());
    }
    // V=1 is fixed at R=2 / Length=40 by spec.
    if inputs.v == 1 && (inputs.r != 2 || inputs.length_bits != 40) {
        return Err(EncryptedError::UnsupportedHandler {
            filter: "Standard".into(),
            v: inputs.v,
            r: inputs.r,
            cfm: None,
        }
        .into());
    }
    // R=2 is a 40-bit revision regardless of V; reject longer keys to keep
    // the R=2 branch in compute_file_key/check_user_password from emitting
    // longer-than-spec keys.
    if inputs.r == 2 && inputs.length_bits != 40 {
        return Err(EncryptedError::UnsupportedHandler {
            filter: "Standard".into(),
            v: inputs.v,
            r: inputs.r,
            cfm: None,
        }
        .into());
    }
    if inputs.length_bits < 40 || inputs.length_bits > 128 || inputs.length_bits % 8 != 0 {
        return Err(EncryptedError::Malformed {
            reason: format!(
                "/Length {} is invalid; must be a multiple of 8 between 40 and 128",
                inputs.length_bits
            ),
        }
        .into());
    }
    Ok((inputs.length_bits / 8) as usize)
}

/// Validate `inputs` for V=4, R=4, Length=128 (the only combination this module
/// supports for the CF-dispatch handler).
///
/// Accepts exactly V=4, R=4, Length=128.  Other combinations return
/// [`EncryptedError::UnsupportedHandler`].
fn validate_v4_inputs(inputs: &StandardHandlerInputs<'_>) -> Result<usize> {
    if inputs.v != 4 || inputs.r != 4 || inputs.length_bits != 128 {
        return Err(EncryptedError::UnsupportedHandler {
            filter: "Standard".into(),
            v: inputs.v,
            r: inputs.r,
            cfm: None,
        }
        .into());
    }
    Ok(16)
}

fn r5_salted_hash(password: &[u8], salt: &[u8], extra: &[u8]) -> [u8; 32] {
    let password = &password[..password.len().min(127)];
    let mut input = Vec::with_capacity(password.len() + salt.len() + extra.len());
    input.extend_from_slice(password);
    input.extend_from_slice(salt);
    input.extend_from_slice(extra);
    sha256(&input)
}

fn aes256_cbc_zero_iv_unwrap(encrypted_key: &[u8; 32], aes_key: &[u8; 32]) -> Result<Vec<u8>> {
    let iv = [0u8; 16];
    let mut ciphertext = encrypted_key.to_vec();
    let dec = <Decryptor<Aes256> as KeyIvInit>::new(aes_key.into(), (&iv).into());
    dec.decrypt_padded_mut::<NoPadding>(&mut ciphertext)
        .map_err(|_| EncryptedError::Malformed {
            reason: "invalid V=5 encrypted file-key entry".into(),
        })?;
    Ok(ciphertext)
}

fn decrypt_r5_file_key(
    password: &[u8],
    entry: &[u8; 48],
    encrypted_key: &[u8; 32],
    extra: &[u8],
) -> Result<Vec<u8>> {
    let validation_salt = &entry[32..40];
    let key_salt = &entry[40..48];

    let validation_hash = r5_salted_hash(password, validation_salt, extra);
    if validation_hash[..] != entry[..32] {
        return Err(EncryptedError::BadPassword.into());
    }

    let aes_key = r5_salted_hash(password, key_salt, extra);
    aes256_cbc_zero_iv_unwrap(encrypted_key, &aes_key)
}

fn r6_password_hash(password: &[u8], salt: &[u8], extra: &[u8]) -> [u8; 32] {
    let password = &password[..password.len().min(127)];
    let mut input = Vec::with_capacity(password.len() + salt.len() + extra.len());
    input.extend_from_slice(password);
    input.extend_from_slice(salt);
    input.extend_from_slice(extra);
    let mut key = sha256(&input).to_vec();

    let mut round_number = 0usize;
    loop {
        round_number += 1;

        let mut k1 = Vec::with_capacity(password.len() + key.len() + extra.len());
        k1.extend_from_slice(password);
        k1.extend_from_slice(&key);
        k1.extend_from_slice(extra);

        let mut e = Vec::with_capacity(k1.len() * 64);
        for _ in 0..64 {
            e.extend_from_slice(&k1);
        }

        let mut aes_key = [0u8; 16];
        aes_key.copy_from_slice(&key[..16]);
        let mut iv = [0u8; 16];
        iv.copy_from_slice(&key[16..32]);
        let enc = <Encryptor<Aes128> as KeyIvInit>::new((&aes_key).into(), (&iv).into());
        enc.encrypt_padded_mut::<NoPadding>(&mut e, k1.len() * 64)
            .expect("R=6 hash input repeated 64 times is block-aligned");

        let e_mod_3 = e[..16]
            .iter()
            .fold(0u16, |acc, byte| acc + u16::from(*byte))
            % 3;
        key = match e_mod_3 {
            0 => sha256(&e).to_vec(),
            1 => sha384(&e).to_vec(),
            _ => sha512(&e).to_vec(),
        };

        if round_number >= 64
            && usize::from(*e.last().expect("R=6 E is non-empty")) <= round_number - 32
        {
            let mut out = [0u8; 32];
            out.copy_from_slice(&key[..32]);
            return out;
        }
    }
}

fn decrypt_r6_file_key(
    password: &[u8],
    entry: &[u8; 48],
    encrypted_key: &[u8; 32],
    extra: &[u8],
) -> Result<Vec<u8>> {
    let validation_salt = &entry[32..40];
    let key_salt = &entry[40..48];

    let validation_hash = r6_password_hash(password, validation_salt, extra);
    if validation_hash[..] != entry[..32] {
        return Err(EncryptedError::BadPassword.into());
    }

    let aes_key = r6_password_hash(password, key_salt, extra);
    aes256_cbc_zero_iv_unwrap(encrypted_key, &aes_key)
}

// ────────────────────────────────────────────────────────────────────────────
// V=4 Crypt Filter types
// ────────────────────────────────────────────────────────────────────────────

/// Crypt-filter method (PDF 1.7 §7.6.5 /CFM).
///
/// Only the three methods used by V=4 are represented.  An unknown /CFM value
/// encountered during parsing should be rejected with `UnsupportedHandler`
/// before a `CryptFilter` is constructed — the type system then guarantees
/// that any `CryptFilter` in scope uses a supported method.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CryptFilterMethod {
    /// `/CFM /V2` — RC4-128.
    V2,
    /// `/CFM /AESV2` — AES-128 CBC.
    AesV2,
    /// `/CFM /Identity` — no-op pass-through.
    Identity,
}

/// A single entry in the `/CF` dictionary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CryptFilter {
    /// The name of this filter as it appears in the `/CF` dictionary.
    pub name: String,
    /// The cipher method for this filter.
    pub cfm: CryptFilterMethod,
    /// Optional `/Length` override in **bits** (some PDFs include this per-entry).
    pub length_bits: Option<i64>,
}

/// The result of resolving a use-site name against the `/CF` table.
///
/// Returned by [`select_crypt_filter`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CryptFilterRef<'a> {
    /// The use-site name was `None` or `"/Identity"` — no encryption is applied.
    Identity,
    /// The use-site name resolved to a specific entry in the `/CF` table.
    Named(&'a CryptFilter),
}

/// Convenience holder for the three use-site selector fields from `/Encrypt`
/// for a V=4 document.
///
/// PDF 1.7 §7.6.5.1 default semantics, encoded here as the value-stored
/// `Option<String>` plus the resolver method [`V4UseSiteSelectors::eff_or_stm`]:
///
/// - `/StmF` absent ⇒ `/Identity` (no encryption applied to streams)
/// - `/StrF` absent ⇒ `/Identity` (no encryption applied to strings)
/// - `/EFF`  absent ⇒ **falls back to `/StmF`** — embedded file streams use
///   the same crypt filter as regular streams. Resolving `eff` raw via
///   [`select_crypt_filter`] would treat absence as `/Identity` and miss
///   encrypted embedded files; callers MUST go through `eff_or_stm()` for
///   the EFF use-site.
#[derive(Debug, Clone)]
pub(crate) struct V4UseSiteSelectors {
    /// `/StmF` — default crypt filter for streams.
    pub stm_f: Option<String>,
    /// `/StrF` — default crypt filter for strings.
    pub str_f: Option<String>,
    /// `/EFF` — default crypt filter for embedded file streams.
    ///
    /// **Do not resolve this field directly with [`select_crypt_filter`].**
    /// Use [`V4UseSiteSelectors::eff_or_stm`] to honor the
    /// `/EFF absent ⇒ /StmF` fallback from the spec.
    pub eff: Option<String>,
}

impl V4UseSiteSelectors {
    /// Effective embedded-file-stream selector name, per PDF 1.7 §7.6.5.1:
    /// returns `self.eff` if present, otherwise `self.stm_f`. Pass the result
    /// to [`select_crypt_filter`] as the use-site name.
    pub(crate) fn eff_or_stm(&self) -> Option<&str> {
        self.eff.as_deref().or(self.stm_f.as_deref())
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Public API
// ────────────────────────────────────────────────────────────────────────────

/// PDF 1.7 §7.6.3.3 Algorithm 2 — Compute the file encryption key.
///
/// Returns the file encryption key as a `Vec<u8>` of length `length_bits/8`.
pub(crate) fn compute_file_key(
    password: &[u8],
    inputs: &StandardHandlerInputs<'_>,
) -> Result<Vec<u8>> {
    let n = validate_inputs(inputs)?;

    // Step 1: Pad/truncate the password to 32 bytes.
    let padded = pad_password(password);

    // Step 2: Initialise MD5 with padded_password || /O || P (LE) || /ID[0]
    //         (and 0xFF×4 only for R≥4 && !encrypt_metadata — not applicable here).
    let p_le = inputs.p.to_le_bytes();
    let mut md5_input = Vec::with_capacity(32 + 32 + 4 + inputs.id0.len() + 4);
    md5_input.extend_from_slice(&padded);
    md5_input.extend_from_slice(inputs.o);
    md5_input.extend_from_slice(&p_le);
    md5_input.extend_from_slice(inputs.id0);
    // Step 3 (R≥4 tail) — omitted for V=1/V=2.
    if inputs.r >= 4 && !inputs.encrypt_metadata {
        md5_input.extend_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
    }

    // Step 3: Take the MD5 digest.
    let mut digest = md5(&md5_input);

    // Step 4: If revision ≥ 3, do 50 iterations of MD5 on the first n bytes.
    if inputs.r >= 3 {
        for _ in 0..50 {
            digest = md5(&digest[..n]);
        }
    }

    // Step 5: Return the first n bytes as the file encryption key.
    Ok(digest[..n].to_vec())
}

/// PDF 1.7 §7.6.3.3 Algorithm 2 (R=4 path) — Compute the V=4 file encryption key.
///
/// This is a thin shim that validates the inputs as V=4/R=4/Length=128 and then
/// delegates to the same MD5-based key derivation used by `compute_file_key`.
/// The 0xFF×4 tail required by Algorithm 2 step 3 when `!encrypt_metadata && R≥4`
/// is already handled inside `compute_file_key`'s inner loop; no new logic is needed.
///
/// Returns the 16-byte file encryption key.
pub(crate) fn compute_file_key_v4(
    password: &[u8],
    inputs: &StandardHandlerInputs<'_>,
) -> Result<Vec<u8>> {
    // Validate that inputs are specifically V=4/R=4/Length=128.
    let _n = validate_v4_inputs(inputs)?;
    // compute_file_key contains the full Algorithm 2 R=4 path (including the
    // conditional 0xFF×4 tail for !encrypt_metadata). We invoke it directly,
    // bypassing validate_inputs (which rejects V=4) by constructing equivalent
    // inputs that the inner function accepts — but that would couple the two
    // validators. Instead, inline only the algorithmic core so we stay DRY
    // without punching a hole in validate_inputs.
    //
    // Algorithmic core (mirrors compute_file_key):
    let n = 16usize; // length_bits=128 → 16 bytes
    let padded = pad_password(password);
    let p_le = inputs.p.to_le_bytes();
    let mut md5_input = Vec::with_capacity(32 + 32 + 4 + inputs.id0.len() + 4);
    md5_input.extend_from_slice(&padded);
    md5_input.extend_from_slice(inputs.o);
    md5_input.extend_from_slice(&p_le);
    md5_input.extend_from_slice(inputs.id0);
    // R=4: append 0xFF×4 when encrypt_metadata is false (Algorithm 2, step 3).
    if !inputs.encrypt_metadata {
        md5_input.extend_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
    }
    let mut digest = md5(&md5_input);
    // R=4 ≥ 3: 50 iterations of MD5 on the first n bytes.
    for _ in 0..50 {
        digest = md5(&digest[..n]);
    }
    Ok(digest[..n].to_vec())
}

/// Authenticate a user password for legacy V=5 R=5 and return the 32-byte file key.
///
/// This is the deprecated pre-ISO 32000-2 AES-256 path: authenticate with
/// `SHA-256(password || /U validation_salt)`, then derive the AES key with
/// `SHA-256(password || /U key_salt)` and decrypt `/UE` with AES-256-CBC using
/// a zero IV.
pub(crate) fn check_user_password_r5(
    password: &[u8],
    inputs: &StandardHandlerR5Inputs<'_>,
) -> Result<Vec<u8>> {
    decrypt_r5_file_key(password, inputs.u, inputs.ue, &[])
}

/// Authenticate an owner password for legacy V=5 R=5 and return the 32-byte file key.
///
/// This mirrors [`check_user_password_r5`] using `/O` salts, `/OE`, and the
/// required `/U` entry suffix in the owner-password hash input.
pub(crate) fn check_owner_password_r5(
    password: &[u8],
    inputs: &StandardHandlerR5Inputs<'_>,
) -> Result<Vec<u8>> {
    decrypt_r5_file_key(password, inputs.o, inputs.oe, inputs.u)
}

/// Authenticate a user password for V=5 R=6 and return the 32-byte file key.
///
/// Uses ISO 32000-2 Algorithm 2.B for the validation and file-key hashes, then
/// decrypts `/UE` with AES-256-CBC using a zero IV.
pub(crate) fn check_user_password_r6(
    password: &[u8],
    inputs: &StandardHandlerR5Inputs<'_>,
) -> Result<Vec<u8>> {
    decrypt_r6_file_key(password, inputs.u, inputs.ue, &[])
}

/// Authenticate an owner password for V=5 R=6 and return the 32-byte file key.
///
/// Owner-password hashes include the full 48-byte `/U` entry as the extra input,
/// and the resulting key unwraps `/OE` with AES-256-CBC using a zero IV.
pub(crate) fn check_owner_password_r6(
    password: &[u8],
    inputs: &StandardHandlerR5Inputs<'_>,
) -> Result<Vec<u8>> {
    decrypt_r6_file_key(password, inputs.o, inputs.oe, inputs.u)
}

/// Select the [`CryptFilter`] for a given use-site name from the `/CF` table.
///
/// The name is the value of `/StmF`, `/StrF`, or `/EFF` for the relevant
/// use site.  Resolution rules:
///
/// - `name == None` or `name == Some("/Identity")` → [`CryptFilterRef::Identity`]
/// - `name == Some(n)` and `cf_table` contains `n` → [`CryptFilterRef::Named`]
/// - `name == Some(n)` and `cf_table` does **not** contain `n` →
///   [`EncryptedError::Malformed`]
///
/// `/Identity` is a PDF-specified no-op pass-through and does not need to
/// appear in the `/CF` dictionary.
pub(crate) fn select_crypt_filter<'a>(
    cf_table: &'a std::collections::HashMap<String, CryptFilter>,
    name: Option<&str>,
) -> Result<CryptFilterRef<'a>> {
    match name {
        None | Some("Identity") => Ok(CryptFilterRef::Identity),
        Some(n) => match cf_table.get(n) {
            Some(cf) => Ok(CryptFilterRef::Named(cf)),
            None => Err(EncryptedError::Malformed {
                reason: format!("/CF entry '{}' not found", n),
            }
            .into()),
        },
    }
}

/// Map a [`CryptFilterMethod`] to the [`ObjectKeyAlg`] required by
/// [`per_object_key`].
///
/// Returns `None` for [`CryptFilterMethod::Identity`] because no key derivation
/// is needed — the data is passed through unchanged.
pub(crate) fn cfm_to_object_key_alg(cfm: CryptFilterMethod) -> Option<ObjectKeyAlg> {
    match cfm {
        CryptFilterMethod::V2 => Some(ObjectKeyAlg::Rc4),
        CryptFilterMethod::AesV2 => Some(ObjectKeyAlg::Aes),
        CryptFilterMethod::Identity => None,
    }
}

/// PDF 1.7 §7.6.3.3 Algorithm 6 — Authenticate the user password.
///
/// Returns the file encryption key on success, or
/// [`Error::Encrypted(EncryptedError::BadPassword)`] if the password does not match.
pub(crate) fn check_user_password(
    password: &[u8],
    inputs: &StandardHandlerInputs<'_>,
) -> Result<Vec<u8>> {
    let file_key = compute_file_key(password, inputs)?;

    if inputs.r == 2 {
        // Algorithm 6, step (b) for R=2:
        // Encrypt the padding string with the file key using RC4.
        let mut encrypted = PASSWORD_PADDING;
        rc4(&file_key, &mut encrypted)?;
        // Compare against /U (all 32 bytes).
        if encrypted[..] != inputs.u[..] {
            return Err(EncryptedError::BadPassword.into());
        }
    } else {
        // Algorithm 6, step (b) for R≥3:
        // 1. MD5(PASSWORD_PADDING || /ID[0])
        let mut md5_input = Vec::with_capacity(32 + inputs.id0.len());
        md5_input.extend_from_slice(&PASSWORD_PADDING);
        md5_input.extend_from_slice(inputs.id0);
        let digest = md5(&md5_input);

        // 2. Encrypt that 16-byte digest with the file key.
        let mut data = digest;
        rc4(&file_key, &mut data)?;

        // 3. Apply 19 further RC4 passes with (file_key XOR i) for i = 1..=19.
        for i in 1u8..=19 {
            let xor_key: Vec<u8> = file_key.iter().map(|&b| b ^ i).collect();
            rc4(&xor_key, &mut data)?;
        }

        // 4. Compare the 16-byte result with the first 16 bytes of /U.
        if data[..] != inputs.u[..16] {
            return Err(EncryptedError::BadPassword.into());
        }
    }

    Ok(file_key)
}

/// PDF 1.7 §7.6.3.3 Algorithm 6 for V=4/R=4 Standard handler inputs.
pub(crate) fn check_user_password_v4(
    password: &[u8],
    inputs: &StandardHandlerInputs<'_>,
) -> Result<Vec<u8>> {
    let file_key = compute_file_key_v4(password, inputs)?;

    let mut md5_input = Vec::with_capacity(32 + inputs.id0.len());
    md5_input.extend_from_slice(&PASSWORD_PADDING);
    md5_input.extend_from_slice(inputs.id0);
    let digest = md5(&md5_input);

    let mut data = digest;
    rc4(&file_key, &mut data)?;
    for i in 1u8..=19 {
        let xor_key: Vec<u8> = file_key.iter().map(|&b| b ^ i).collect();
        rc4(&xor_key, &mut data)?;
    }

    if data[..] != inputs.u[..16] {
        return Err(EncryptedError::BadPassword.into());
    }

    Ok(file_key)
}

/// PDF 1.7 §7.6.3.3 Algorithm 7 — Authenticate the owner password.
///
/// Returns the file encryption key on success, or
/// [`Error::Encrypted(EncryptedError::BadPassword)`] if the password does not match.
pub(crate) fn check_owner_password(
    password: &[u8],
    inputs: &StandardHandlerInputs<'_>,
) -> Result<Vec<u8>> {
    let n = validate_inputs(inputs)?;

    // Step 1: Pad/truncate the owner password to 32 bytes.
    let padded_owner = pad_password(password);

    // Step 2: Compute the RC4 key from the owner password.
    //   MD5(padded_owner), then 50× if R≥3, take first n bytes.
    //
    // Per PDF 1.7 §7.6.3.4 Algorithm 3 step 3, the R≥3 loop feeds the full
    // 16-byte MD5 digest back as input (no n-truncation). Truncating to n
    // would break n<16 cases like V=2/R=3/Length∈{40,56,64,...}.
    let mut digest = md5(&padded_owner);
    if inputs.r >= 3 {
        for _ in 0..50 {
            digest = md5(&digest);
        }
    }
    let rc4_key = &digest[..n];

    // Step 3: Use the RC4 key to decrypt /O and recover the (padded) user password.
    let mut candidate = *inputs.o; // 32 bytes

    if inputs.r == 2 {
        // Single RC4 pass.
        rc4(rc4_key, &mut candidate)?;
    } else {
        // 20 passes in DESCENDING order (i = 19..=0).
        for i in (0u8..=19).rev() {
            let xor_key: Vec<u8> = rc4_key.iter().map(|&b| b ^ i).collect();
            rc4(&xor_key, &mut candidate)?;
        }
    }

    // Step 4: Use the recovered candidate as the user password in Algorithm 6.
    check_user_password(&candidate, inputs)
}

/// PDF 1.7 §7.6.3.3 Algorithm 7 for V=4/R=4 Standard handler inputs.
pub(crate) fn check_owner_password_v4(
    password: &[u8],
    inputs: &StandardHandlerInputs<'_>,
) -> Result<Vec<u8>> {
    let n = validate_v4_inputs(inputs)?;
    let padded_owner = pad_password(password);
    let mut digest = md5(&padded_owner);
    for _ in 0..50 {
        digest = md5(&digest);
    }
    let rc4_key = &digest[..n];
    let mut candidate = *inputs.o;
    for i in (0u8..=19).rev() {
        let xor_key: Vec<u8> = rc4_key.iter().map(|&b| b ^ i).collect();
        rc4(&xor_key, &mut candidate)?;
    }
    check_user_password_v4(&candidate, inputs)
}

// ────────────────────────────────────────────────────────────────────────────
// Algorithm 1 — Per-object key derivation (V=1/V=2/V=4)
// ────────────────────────────────────────────────────────────────────────────

/// Selects the cipher variant used for per-object key derivation (Algorithm 1).
///
/// For AES-based crypt filters (V=4, `/CFM /AESV2`), a 4-byte salt `sAlT`
/// (`0x73 0x41 0x6C 0x54`) is appended to the MD5 input.  For all RC4 variants
/// (V=1, V=2, and V=4 `/CFM /V2`) no salt is added.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ObjectKeyAlg {
    /// RC4 variant — no salt appended.
    Rc4,
    /// AES variant — 4-byte salt `sAlT` appended.
    Aes,
}

/// Cipher material selected for decrypting string objects at a given use site.
#[derive(Debug, Clone, Copy)]
pub(crate) enum StringCipher<'a> {
    /// No-op crypt filter.
    Identity,
    /// RC4 with an already-derived object key (V<5) or selected CF key.
    Rc4 { key: &'a [u8] },
    /// AES-128-CBC with an already-derived object key. PDF string bytes include the IV.
    Aes128 { key: &'a [u8; 16] },
    /// AES-256-CBC for V=5. PDF string bytes include the IV.
    Aes256 { key: &'a [u8; 32] },
}

/// Decrypt every string contained in a resolved object graph, in place.
///
/// The caller supplies the cipher material appropriate for `object_ref`: for V<5 this is
/// the Algorithm 1 per-object key; for V=5 it is the file key or selected crypt-filter key.
/// Stream payload bytes are intentionally left untouched, but stream dictionaries are
/// traversed because they can contain ordinary string objects.
pub(crate) fn decrypt_strings_in_object(
    object_ref: ObjectRef,
    object: &mut Object,
    cipher: StringCipher<'_>,
    encrypt_ref: Option<ObjectRef>,
) -> Result<()> {
    if Some(object_ref) == encrypt_ref {
        return Ok(());
    }
    decrypt_strings_in_value(object, cipher)
}

fn decrypt_strings_in_value(object: &mut Object, cipher: StringCipher<'_>) -> Result<()> {
    match object {
        Object::String(bytes) => decrypt_cipher_bytes(bytes, cipher),
        Object::Array(values) => {
            for value in values {
                decrypt_strings_in_value(value, cipher)?;
            }
            Ok(())
        }
        Object::Dictionary(dict) => {
            for value in dict.values_mut() {
                decrypt_strings_in_value(value, cipher)?;
            }
            Ok(())
        }
        Object::Stream(stream) => {
            for value in stream.dict.values_mut() {
                decrypt_strings_in_value(value, cipher)?;
            }
            Ok(())
        }
        Object::Null
        | Object::Boolean(_)
        | Object::Integer(_)
        | Object::Real(_)
        | Object::Name(_)
        | Object::Reference(_) => Ok(()),
    }
}

pub(crate) fn decrypt_cipher_bytes(bytes: &mut Vec<u8>, cipher: StringCipher<'_>) -> Result<()> {
    match cipher {
        StringCipher::Identity => Ok(()),
        StringCipher::Rc4 { key } => rc4(key, bytes).map_err(Into::into),
        StringCipher::Aes128 { key } => {
            let Some((iv, ciphertext)) = bytes.split_first_chunk::<16>() else {
                return Err(EncryptedError::Malformed {
                    reason: "AES string is missing its 16-byte IV".into(),
                }
                .into());
            };
            let mut decrypted = ciphertext.to_vec();
            crate::security::primitives::aes128_cbc_decrypt(key, iv, &mut decrypted)?;
            *bytes = decrypted;
            Ok(())
        }
        StringCipher::Aes256 { key } => {
            let Some((iv, ciphertext)) = bytes.split_first_chunk::<16>() else {
                return Err(EncryptedError::Malformed {
                    reason: "AES string is missing its 16-byte IV".into(),
                }
                .into());
            };
            let mut decrypted = ciphertext.to_vec();
            crate::security::primitives::aes256_cbc_decrypt(key, iv, &mut decrypted)?;
            *bytes = decrypted;
            Ok(())
        }
    }
}

/// PDF 1.7 §7.6.2 Algorithm 1 — derive a per-object key.
///
/// Constructs the object-specific encryption key from the file encryption key,
/// the object number, and the generation number.  Used for non-CF streams and
/// strings (V<5) and for explicit `/Crypt` filter entries when V<5.
///
/// # Arguments
/// - `file_key` — the file encryption key (`n` bytes, `n ∈ [5, 16]`).
/// - `obj`      — the indirect object number.
/// - `gen`      — the object generation number.
/// - `alg`      — [`ObjectKeyAlg::Rc4`] or [`ObjectKeyAlg::Aes`].
///
/// # Algorithm
/// 1. Concatenate: `file_key ‖ obj[0..3] ‖ gen[0..2]`
///    where `obj[0..3]` is the three **low** bytes of `obj` in little-endian
///    order, and `gen[0..2]` is the two **low** bytes of `gen` in little-endian
///    order.
/// 2. If `alg == Aes`, append `0x73 0x41 0x6C 0x54` ("sAlT").
/// 3. Take the MD5 digest.
/// 4. Return the first `min(n + 5, 16)` bytes.
pub(crate) fn per_object_key(file_key: &[u8], obj: u32, gen: u32, alg: ObjectKeyAlg) -> Vec<u8> {
    let n = file_key.len();
    // Capacity: n + 3 (obj) + 2 (gen) + optional 4 (sAlT).
    let mut md5_input = Vec::with_capacity(n + 9);
    md5_input.extend_from_slice(file_key);
    // Low 3 bytes of obj in little-endian order.
    let obj_le = obj.to_le_bytes();
    md5_input.extend_from_slice(&obj_le[..3]);
    // Low 2 bytes of gen in little-endian order.
    let gen_le = gen.to_le_bytes();
    md5_input.extend_from_slice(&gen_le[..2]);
    // AES salt.
    if alg == ObjectKeyAlg::Aes {
        md5_input.extend_from_slice(&[0x73, 0x41, 0x6C, 0x54]);
    }
    let digest = md5(&md5_input);
    let out_len = (n + 5).min(16);
    digest[..out_len].to_vec()
}

// ────────────────────────────────────────────────────────────────────────────
// Tests
// ────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Dictionary, Object, ObjectRef, Stream};

    // ── helpers ──────────────────────────────────────────────────────────────

    fn from_hex(s: &str) -> Vec<u8> {
        assert_eq!(s.len() % 2, 0, "hex string length must be even");
        (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("invalid hex digit"))
            .collect()
    }

    fn hex32(s: &str) -> [u8; 32] {
        let v = from_hex(s);
        assert_eq!(v.len(), 32, "expected 32-byte hex string");
        let mut out = [0u8; 32];
        out.copy_from_slice(&v);
        out
    }

    fn hex48(s: &str) -> [u8; 48] {
        let v = from_hex(s);
        assert_eq!(v.len(), 48, "expected 48-byte hex string");
        let mut out = [0u8; 48];
        out.copy_from_slice(&v);
        out
    }

    fn aes128_string(key: &[u8; 16], iv: &[u8; 16], plaintext: &[u8]) -> Vec<u8> {
        let enc = <Encryptor<Aes128> as KeyIvInit>::new(key.into(), iv.into());
        let mut buf = plaintext.to_vec();
        let msg_len = buf.len();
        buf.resize(msg_len + 16, 0);
        let ciphertext = enc
            .encrypt_padded_mut::<cbc::cipher::block_padding::Pkcs7>(&mut buf, msg_len)
            .unwrap();
        let mut out = iv.to_vec();
        out.extend_from_slice(ciphertext);
        out
    }

    fn aes256_string(key: &[u8; 32], iv: &[u8; 16], plaintext: &[u8]) -> Vec<u8> {
        let enc = <Encryptor<Aes256> as KeyIvInit>::new(key.into(), iv.into());
        let mut buf = plaintext.to_vec();
        let msg_len = buf.len();
        buf.resize(msg_len + 16, 0);
        let ciphertext = enc
            .encrypt_padded_mut::<cbc::cipher::block_padding::Pkcs7>(&mut buf, msg_len)
            .unwrap();
        let mut out = iv.to_vec();
        out.extend_from_slice(ciphertext);
        out
    }

    #[test]
    fn decrypt_strings_descends_into_nested_arrays_dicts_and_stream_dicts() {
        let key = b"Key";
        let mut nested = b"nested".to_vec();
        rc4(key, &mut nested).unwrap();
        let mut in_dict = b"in dict".to_vec();
        rc4(key, &mut in_dict).unwrap();
        let mut in_stream_dict = b"in stream dict".to_vec();
        rc4(key, &mut in_stream_dict).unwrap();

        let mut inner = Dictionary::new();
        inner.insert("S", Object::String(in_dict));
        let mut stream_dict = Dictionary::new();
        stream_dict.insert("Title", Object::String(in_stream_dict));

        let mut object = Object::Array(vec![
            Object::String(nested),
            Object::Dictionary(inner),
            Object::Stream(Stream::new(
                stream_dict,
                b"encrypted stream bytes stay raw".to_vec(),
            )),
        ]);

        decrypt_strings_in_object(
            ObjectRef::new(7, 0),
            &mut object,
            StringCipher::Rc4 { key },
            None,
        )
        .unwrap();

        let Object::Array(values) = object else {
            panic!("expected array");
        };
        assert_eq!(values[0], Object::String(b"nested".to_vec()));
        let Object::Dictionary(dict) = &values[1] else {
            panic!("expected dictionary");
        };
        assert_eq!(dict.get("S"), Some(&Object::String(b"in dict".to_vec())));
        let Object::Stream(stream) = &values[2] else {
            panic!("expected stream");
        };
        assert_eq!(
            stream.dict.get("Title"),
            Some(&Object::String(b"in stream dict".to_vec()))
        );
        assert_eq!(stream.data, b"encrypted stream bytes stay raw");
    }

    #[test]
    fn decrypt_strings_skips_encrypt_object_subtree() {
        let key = b"Key";
        let mut secret = b"must stay encrypted".to_vec();
        rc4(key, &mut secret).unwrap();
        let encrypted = secret.clone();
        let mut dict = Dictionary::new();
        dict.insert("O", Object::String(secret));
        let mut object = Object::Dictionary(dict);

        decrypt_strings_in_object(
            ObjectRef::new(5, 0),
            &mut object,
            StringCipher::Rc4 { key },
            Some(ObjectRef::new(5, 0)),
        )
        .unwrap();

        let Object::Dictionary(dict) = object else {
            panic!("expected dictionary");
        };
        assert_eq!(dict.get("O"), Some(&Object::String(encrypted)));
    }

    #[test]
    fn decrypt_strings_handles_rc4_aes128_and_v5_aes256_string_bytes() {
        let rc4_key = b"Key";
        let mut rc4_string = b"Plaintext".to_vec();
        rc4(rc4_key, &mut rc4_string).unwrap();
        let mut rc4_object = Object::String(rc4_string);
        decrypt_strings_in_object(
            ObjectRef::new(10, 0),
            &mut rc4_object,
            StringCipher::Rc4 { key: rc4_key },
            None,
        )
        .unwrap();
        assert_eq!(rc4_object, Object::String(b"Plaintext".to_vec()));

        let aes128_key = [0x11; 16];
        let aes128_iv = [0x22; 16];
        let mut aes128_object = Object::String(aes128_string(&aes128_key, &aes128_iv, b"AES V2"));
        decrypt_strings_in_object(
            ObjectRef::new(11, 0),
            &mut aes128_object,
            StringCipher::Aes128 { key: &aes128_key },
            None,
        )
        .unwrap();
        assert_eq!(aes128_object, Object::String(b"AES V2".to_vec()));

        let aes256_key = [0x33; 32];
        let aes256_iv = [0x44; 16];
        let mut aes256_object = Object::String(aes256_string(&aes256_key, &aes256_iv, b"AES V5"));
        decrypt_strings_in_object(
            ObjectRef::new(12, 0),
            &mut aes256_object,
            StringCipher::Aes256 { key: &aes256_key },
            None,
        )
        .unwrap();
        assert_eq!(aes256_object, Object::String(b"AES V5".to_vec()));
    }

    // ── Password padding ─────────────────────────────────────────────────────

    #[test]
    fn pad_password_empty() {
        let padded = pad_password(b"");
        assert_eq!(padded, PASSWORD_PADDING);
    }

    #[test]
    fn pad_password_short() {
        let padded = pad_password(b"abc");
        assert_eq!(&padded[..3], b"abc");
        assert_eq!(&padded[3..], &PASSWORD_PADDING[..29]);
    }

    #[test]
    fn pad_password_exactly_32() {
        let pw = [0xAAu8; 32];
        let padded = pad_password(&pw);
        assert_eq!(padded, pw);
    }

    #[test]
    fn pad_password_too_long() {
        let pw = [0xBBu8; 64];
        let padded = pad_password(&pw);
        assert_eq!(padded, [0xBBu8; 32]);
    }

    #[test]
    fn password_padding_constant() {
        // Verify the constant matches the literal values in PDF 1.7 §7.6.3.3.
        assert_eq!(PASSWORD_PADDING[0], 0x28);
        assert_eq!(PASSWORD_PADDING[1], 0xBF);
        assert_eq!(PASSWORD_PADDING[31], 0x7A);
        assert_eq!(PASSWORD_PADDING.len(), 32);
    }

    // ── Test fixture constants ────────────────────────────────────────────────
    // Vectors generated by Python reference implementation (hashlib.md5 + hand-rolled RC4).
    // The same ID[0] is used for all test cases: bytes 0x00..0x0f (16 bytes).

    /// /ID[0] bytes used in all test cases.
    const ID0: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    /// /P value used in all test cases.
    const P: i32 = -3904;

    // TC1: V=1, R=2, 40-bit, empty password.
    const TC1_O: &str = "2055c756c72e1ad702608e8196acad447ad32d17cff583235f6dd15fed7dab67";
    const TC1_U: &str = "d6f15ab11e0082d3e78e9bdd4aa356df7951447b42d3ec90675f8fed125839a0";
    const TC1_KEY: &str = "f784f642f1";

    // TC2: V=2, R=3, 128-bit, empty password.
    const TC2_O: &str = "36451bd39d753b7c1d10922c28e6665aa4f3353fb0348b536893e3b1db5c579b";
    const TC2_U: &str = "09be6cfeca2ef2692f8000d3ca083f2d00000000000000000000000000000000";
    const TC2_KEY: &str = "b452d605afa4725e7ff5cef2d62f9871";

    // TC3: V=1, R=2, 40-bit, user="user", owner="owner".
    const TC3_O: &str = "94e8094419662a774442fb072e3d9f19e9d130ec09a4d0061e78fe920f7ab62f";
    const TC3_U: &str = "13f520c882d052bf57b416b747c13979bded7ea31240fe41928852aca3894c49";
    const TC3_KEY: &str = "7fca5cfcc5";

    // TC4: V=2, R=3, 128-bit, user="secret", owner="ownerpass".
    const TC4_O: &str = "6ef3764ad26cfe3cab68706fc26c9413d80d47b053374529a32e1405eeba42c1";
    const TC4_U: &str = "89ee68e273e9403df45997e914885f0900000000000000000000000000000000";
    const TC4_KEY: &str = "1b7300df56bf77f869b3bf5345aad08c";

    // ── Algorithm 2: compute_file_key ────────────────────────────────────────

    /// TC1: V=1 R=2 40-bit empty password → known 5-byte key.
    #[test]
    fn alg2_v1_r2_empty_password() {
        let o = hex32(TC1_O);
        let u = hex32(TC1_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = compute_file_key(b"", &inputs).unwrap();
        assert_eq!(key, from_hex(TC1_KEY), "TC1: wrong file key");
        assert_eq!(key.len(), 5);
    }

    /// TC2: V=2 R=3 128-bit empty password → known 16-byte key.
    #[test]
    fn alg2_v2_r3_empty_password() {
        let o = hex32(TC2_O);
        let u = hex32(TC2_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = compute_file_key(b"", &inputs).unwrap();
        assert_eq!(key, from_hex(TC2_KEY), "TC2: wrong file key");
        assert_eq!(key.len(), 16);
    }

    /// TC3: V=1 R=2 40-bit, user="user", owner="owner".
    #[test]
    fn alg2_v1_r2_with_password() {
        let o = hex32(TC3_O);
        let u = hex32(TC3_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = compute_file_key(b"user", &inputs).unwrap();
        assert_eq!(key, from_hex(TC3_KEY), "TC3: wrong file key");
    }

    /// TC4: V=2 R=3 128-bit, user="secret", owner="ownerpass".
    #[test]
    fn alg2_v2_r3_with_password() {
        let o = hex32(TC4_O);
        let u = hex32(TC4_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = compute_file_key(b"secret", &inputs).unwrap();
        assert_eq!(key, from_hex(TC4_KEY), "TC4: wrong file key");
    }

    // ── Algorithm 6: check_user_password ────────────────────────────────────

    /// TC1: V=1 R=2, empty user password succeeds and returns the correct key.
    #[test]
    fn alg6_v1_r2_correct_user_password() {
        let o = hex32(TC1_O);
        let u = hex32(TC1_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_user_password(b"", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex(TC1_KEY),
            "TC1 user: wrong key returned on success"
        );
    }

    /// TC1: Wrong user password is rejected.
    #[test]
    fn alg6_v1_r2_wrong_user_password() {
        let o = hex32(TC1_O);
        let u = hex32(TC1_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = check_user_password(b"wrongpassword", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::BadPassword)
            ),
            "expected BadPassword, got: {err:?}"
        );
    }

    /// TC2: V=2 R=3, empty user password succeeds.
    #[test]
    fn alg6_v2_r3_correct_user_password() {
        let o = hex32(TC2_O);
        let u = hex32(TC2_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_user_password(b"", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex(TC2_KEY),
            "TC2 user: wrong key returned on success"
        );
    }

    /// TC2: Wrong user password is rejected for V=2 R=3.
    #[test]
    fn alg6_v2_r3_wrong_user_password() {
        let o = hex32(TC2_O);
        let u = hex32(TC2_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = check_user_password(b"notthepassword", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::BadPassword)
            ),
            "expected BadPassword, got: {err:?}"
        );
    }

    /// TC3: V=1 R=2 with real user password "user".
    #[test]
    fn alg6_v1_r2_named_user_password() {
        let o = hex32(TC3_O);
        let u = hex32(TC3_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_user_password(b"user", &inputs).unwrap();
        assert_eq!(key, from_hex(TC3_KEY));
    }

    /// TC4: V=2 R=3 with user password "secret".
    #[test]
    fn alg6_v2_r3_named_user_password() {
        let o = hex32(TC4_O);
        let u = hex32(TC4_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_user_password(b"secret", &inputs).unwrap();
        assert_eq!(key, from_hex(TC4_KEY));
    }

    // ── Algorithm 7: check_owner_password ───────────────────────────────────

    /// TC1: V=1 R=2, owner == user (empty), succeeds.
    #[test]
    fn alg7_v1_r2_correct_owner_password() {
        let o = hex32(TC1_O);
        let u = hex32(TC1_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_owner_password(b"", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex(TC1_KEY),
            "TC1 owner: wrong key returned on success"
        );
    }

    /// TC1: Wrong owner password is rejected.
    #[test]
    fn alg7_v1_r2_wrong_owner_password() {
        let o = hex32(TC1_O);
        let u = hex32(TC1_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = check_owner_password(b"wrongowner", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::BadPassword)
            ),
            "expected BadPassword, got: {err:?}"
        );
    }

    /// TC2: V=2 R=3, owner == user (empty), succeeds.
    #[test]
    fn alg7_v2_r3_correct_owner_password() {
        let o = hex32(TC2_O);
        let u = hex32(TC2_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_owner_password(b"", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex(TC2_KEY),
            "TC2 owner: wrong key returned on success"
        );
    }

    /// TC2: Wrong owner password is rejected for V=2 R=3.
    #[test]
    fn alg7_v2_r3_wrong_owner_password() {
        let o = hex32(TC2_O);
        let u = hex32(TC2_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = check_owner_password(b"badowner", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::BadPassword)
            ),
            "expected BadPassword, got: {err:?}"
        );
    }

    /// TC3: V=1 R=2, distinct owner password "owner" succeeds.
    #[test]
    fn alg7_v1_r2_named_owner_password() {
        let o = hex32(TC3_O);
        let u = hex32(TC3_U);
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 40,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_owner_password(b"owner", &inputs).unwrap();
        assert_eq!(key, from_hex(TC3_KEY), "TC3 owner: wrong key");
    }

    /// TC4: V=2 R=3, distinct owner password "ownerpass" succeeds.
    #[test]
    fn alg7_v2_r3_named_owner_password() {
        let o = hex32(TC4_O);
        let u = hex32(TC4_U);
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = check_owner_password(b"ownerpass", &inputs).unwrap();
        assert_eq!(key, from_hex(TC4_KEY), "TC4 owner: wrong key");
    }

    // ── Input validation ─────────────────────────────────────────────────────

    #[test]
    fn invalid_v_returns_unsupported() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 3, // not 1 or 2
            r: 3,
            length_bits: 128,
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler, got: {err:?}"
        );
    }

    #[test]
    fn invalid_length_returns_malformed() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 33, // not a multiple of 8
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::Malformed { .. })
            ),
            "expected Malformed, got: {err:?}"
        );
    }

    #[test]
    fn length_out_of_range_returns_malformed() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 3,
            length_bits: 256, // too large
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::Malformed { .. })
            ),
            "expected Malformed for length 256, got: {err:?}"
        );
    }

    /// V=1 with R=3 is rejected — V=1 is fixed at R=2/Length=40 by spec.
    #[test]
    fn v1_with_wrong_r_returns_unsupported() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 3, // V=1 is R=2 only
            length_bits: 40,
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler for V=1/R=3, got: {err:?}"
        );
    }

    /// V=1 with Length=128 is rejected — V=1 is fixed at Length=40.
    #[test]
    fn v1_with_wrong_length_returns_unsupported() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 1,
            r: 2,
            length_bits: 128, // V=1 is 40-bit only
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler for V=1/Length=128, got: {err:?}"
        );
    }

    /// Regression: the R≥3 owner-key 50× MD5 loop must feed back the FULL
    /// 16-byte digest, not the n-truncated prefix. For n<16 (V=2/R=3 with
    /// Length∈{40,…,120}) truncated iteration produces a different key, so
    /// correct owner passwords would be rejected. This test exercises the
    /// iteration mechanics directly to keep the fix from regressing.
    #[test]
    fn alg3_owner_key_iteration_uses_full_digest_for_short_keys() {
        let seed = pad_password(b"");
        let mut full = md5(&seed);
        let mut truncated = full;
        for _ in 0..50 {
            full = md5(&full);
            truncated = md5(&truncated[..5]);
        }
        assert_ne!(
            &full[..5],
            &truncated[..5],
            "owner-key 50× loop must feed full digest back, not digest[..n]; \
             if these match by accident, the regression guard is ineffective"
        );
    }

    /// V=2 with R=2 but Length>40 is rejected — R=2 is fixed at 40-bit by spec.
    #[test]
    fn v2_r2_with_long_length_returns_unsupported() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 2,
            length_bits: 128, // R=2 is 40-bit only
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler for V=2/R=2/Length=128, got: {err:?}"
        );
    }

    /// V=2 with R=4 is out of this module's scope.
    #[test]
    fn r4_returns_unsupported() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 2,
            r: 4, // V=4 handler territory
            length_bits: 128,
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler for R=4, got: {err:?}"
        );
    }

    // ── Algorithm 1: per_object_key ──────────────────────────────────────────
    // Test vectors generated by an independent Python reference implementation
    // (hashlib.md5) that directly follows the PDF 1.7 §7.6.2 Algorithm 1 steps.
    // The vectors exercise: RC4 vs AES salt, output-length cap, obj/gen
    // little-endian 3/2-byte truncation, and key lengths from 5 to 16 bytes.

    /// TC-A1-1: RC4, n=5, obj=1, gen=0.
    /// file_key = [0x01]*5, expected output = 10 bytes (n+5=10).
    #[test]
    fn alg1_rc4_n5_obj1_gen0() {
        let file_key = [0x01u8; 5];
        let got = per_object_key(&file_key, 1, 0, ObjectKeyAlg::Rc4);
        assert_eq!(got, from_hex("95803393a09e46bab004"), "TC-A1-1 mismatch");
        assert_eq!(got.len(), 10);
    }

    /// TC-A1-2: RC4, n=16, obj=12345, gen=0.
    /// file_key = [0xAB]*16, expected output = 16 bytes (capped at 16, n+5=21).
    #[test]
    fn alg1_rc4_n16_obj12345_gen0() {
        let file_key = [0xABu8; 16];
        let got = per_object_key(&file_key, 12345, 0, ObjectKeyAlg::Rc4);
        assert_eq!(
            got,
            from_hex("dbb723a2f63dcced70058cf3059453be"),
            "TC-A1-2 mismatch"
        );
        assert_eq!(got.len(), 16);
    }

    /// TC-A1-3: AES (sAlT appended), n=16, obj=10, gen=0.
    /// file_key = [0x42]*16, expected output = 16 bytes.
    #[test]
    fn alg1_aes_n16_obj10_gen0() {
        let file_key = [0x42u8; 16];
        let got = per_object_key(&file_key, 10, 0, ObjectKeyAlg::Aes);
        assert_eq!(
            got,
            from_hex("4c7ad328a68a6acb8b81f68c86756b7a"),
            "TC-A1-3 mismatch"
        );
        assert_eq!(got.len(), 16);
    }

    /// TC-A1-4: AES (sAlT appended), n=5, obj=10, gen=0.
    /// file_key = [0x42]*5, expected output = 10 bytes (n+5=10).
    #[test]
    fn alg1_aes_n5_obj10_gen0() {
        let file_key = [0x42u8; 5];
        let got = per_object_key(&file_key, 10, 0, ObjectKeyAlg::Aes);
        assert_eq!(got, from_hex("e41cdfa1db13fbaeed00"), "TC-A1-4 mismatch");
        assert_eq!(got.len(), 10);
    }

    /// TC-A1-5: RC4, boundary — obj=0xFFFFFFFF, gen=0xFFFFFFFF.
    /// Low 3 bytes of obj = [0xFF,0xFF,0xFF]; low 2 bytes of gen = [0xFF,0xFF].
    /// file_key = [0x00]*8, expected output = 13 bytes (n+5=13).
    #[test]
    fn alg1_rc4_boundary_max_obj_gen() {
        let file_key = [0x00u8; 8];
        let got = per_object_key(&file_key, 0xFFFF_FFFF, 0xFFFF_FFFF, ObjectKeyAlg::Rc4);
        assert_eq!(
            got,
            from_hex("f828745fe049a5667e00a34f50"),
            "TC-A1-5 mismatch"
        );
        assert_eq!(got.len(), 13);
    }

    /// TC-A1-6: RC4, cap test — n=14, so n+5=19 but output must be capped at 16.
    /// file_key = [0x55]*14, obj=5, gen=2.
    #[test]
    fn alg1_rc4_n14_cap_at_16() {
        let file_key = [0x55u8; 14];
        let got = per_object_key(&file_key, 5, 2, ObjectKeyAlg::Rc4);
        assert_eq!(
            got,
            from_hex("5e17e430553c51ee690d0d37ddc6b33e"),
            "TC-A1-6 mismatch"
        );
        assert_eq!(got.len(), 16, "output must be capped at 16");
    }

    // ── V=4 R=4 key derivation ───────────────────────────────────────────────
    // KAT vectors generated by a Python reference implementation (hashlib.md5)
    // that directly follows PDF 1.7 §7.6.3.3 Algorithm 2 for R=4.
    // All tests use:  ID0 = [0x00..0x0f], P = -3904, O = [0u8; 32].

    /// TC5: V=4 R=4 Length=128, empty password, encrypt_metadata=true.
    /// Expected 16-byte file key (Python reference).
    #[test]
    fn compute_file_key_v4_empty_password_encrypt_metadata_true() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 4,
            r: 4,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = compute_file_key_v4(b"", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex("b6bfe1cc3cede324f6d00115f6f22801"),
            "TC5: wrong file key for V=4 R=4 empty pw encrypt_metadata=true"
        );
        assert_eq!(key.len(), 16);
    }

    /// TC6: V=4 R=4 Length=128, empty password, encrypt_metadata=false.
    /// The 0xFF×4 tail must be appended, changing the key vs TC5.
    #[test]
    fn compute_file_key_v4_empty_password_encrypt_metadata_false() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 4,
            r: 4,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: false,
        };
        let key = compute_file_key_v4(b"", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex("08a258e623f388621234efcf1e86489d"),
            "TC6: wrong file key for V=4 R=4 empty pw encrypt_metadata=false"
        );
        assert_eq!(key.len(), 16);
        // The 0xFF×4 tail must change the key relative to TC5.
        assert_ne!(
            key,
            from_hex("b6bfe1cc3cede324f6d00115f6f22801"),
            "TC6: encrypt_metadata=false must produce a different key than encrypt_metadata=true"
        );
    }

    /// TC7: V=4 R=4 Length=128, password="secret", encrypt_metadata=true.
    #[test]
    fn compute_file_key_v4_with_password() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 4,
            r: 4,
            length_bits: 128,
            p: P,
            id0: &ID0,
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let key = compute_file_key_v4(b"secret", &inputs).unwrap();
        assert_eq!(
            key,
            from_hex("6e3b6cc29b8afaf34a03d43a08d67bbb"),
            "TC7: wrong file key for V=4 R=4 pw='secret'"
        );
    }

    /// validate: V=4 R=3 must be rejected (only R=4 is valid for V=4).
    #[test]
    fn compute_file_key_v4_rejects_r3() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 4,
            r: 3, // wrong revision for V=4
            length_bits: 128,
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key_v4(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler for V=4/R=3, got: {err:?}"
        );
    }

    /// validate: V=4 R=4 but Length != 128 must be rejected.
    #[test]
    fn compute_file_key_v4_rejects_wrong_length() {
        let o = [0u8; 32];
        let u = [0u8; 32];
        let inputs = StandardHandlerInputs {
            v: 4,
            r: 4,
            length_bits: 40, // V=4 requires exactly 128
            p: -1,
            id0: &[],
            u: &u,
            o: &o,
            encrypt_metadata: true,
        };
        let err = compute_file_key_v4(b"", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(
                    crate::error::EncryptedError::UnsupportedHandler { .. }
                )
            ),
            "expected UnsupportedHandler for V=4/R=4/Length=40, got: {err:?}"
        );
    }

    // ── V=5 R=5 key derivation ───────────────────────────────────────────────
    // R=5 is the deprecated pre-ISO 32000-2 AES-256 algorithm. Vectors generated
    // by a Python hashlib SHA-256 reference plus OpenSSL AES-256-CBC -nopad.
    // File key is bytes 0x00..0x1f; IV is all zeroes.

    struct R5Fixture {
        u: [u8; 48],
        o: [u8; 48],
        ue: [u8; 32],
        oe: [u8; 32],
    }

    impl R5Fixture {
        fn inputs(&self) -> StandardHandlerR5Inputs<'_> {
            StandardHandlerR5Inputs {
                u: &self.u,
                o: &self.o,
                ue: &self.ue,
                oe: &self.oe,
            }
        }
    }

    fn r5_fixture() -> R5Fixture {
        R5Fixture {
            u: hex48(
                "97e87734dfa9d2a69a7e7326ce3fabd944a3e718602d1bc4171df8a2736c6cbe\
                 00112233445566778899aabbccddeeff",
            ),
            o: hex48(
                "d95e9aa87833363eccce3e1ba1161b87fcc36c3a2e144b199ddd543db3ad480a\
                 102132435465768798a9bacbdcedfe0f",
            ),
            ue: hex32("08030d6f64d3cf8bc22a9ec592a44da03b019659444bbb14111ea6f021b3bdac"),
            oe: hex32("f8e5af968015e82307b0f2c725cb2641a22dd792ec33c4b104fd5d685f2bba41"),
        }
    }

    fn r5_long_password_fixture() -> R5Fixture {
        R5Fixture {
            u: hex48(
                "d39d90631a68ed50a791f40f2d19d45959b7caa339c4c16b43e01863732d43ee\
                 01020304050607081112131415161718",
            ),
            o: [0u8; 48],
            ue: hex32("74c028775d1a6223f4c4f12f6bd57c325de66fd8eac481d80f7eb5313354db40"),
            oe: [0u8; 32],
        }
    }

    #[test]
    fn check_user_password_r5_returns_file_key() {
        let fixture = r5_fixture();
        let inputs = fixture.inputs();
        let file_key = check_user_password_r5(b"userpass", &inputs).unwrap();
        assert_eq!(
            file_key,
            from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
        );
    }

    #[test]
    fn check_owner_password_r5_returns_file_key() {
        let fixture = r5_fixture();
        let inputs = fixture.inputs();
        let file_key = check_owner_password_r5(b"ownerpass", &inputs).unwrap();
        assert_eq!(
            file_key,
            from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
        );
    }

    #[test]
    fn check_user_password_r5_rejects_wrong_password() {
        let fixture = r5_fixture();
        let inputs = fixture.inputs();
        let err = check_user_password_r5(b"wrong", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::BadPassword)
            ),
            "expected BadPassword for wrong R=5 user password, got: {err:?}"
        );
    }

    #[test]
    fn check_user_password_r5_truncates_password_to_127_bytes() {
        let fixture = r5_long_password_fixture();
        let inputs = fixture.inputs();
        let mut password = vec![b'a'; 127];
        password.extend_from_slice(b"zzz");

        let file_key = check_user_password_r5(&password, &inputs).unwrap();
        assert_eq!(
            file_key,
            from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
        );
    }

    // ── V=5 R=6 key derivation ───────────────────────────────────────────────
    // R=6 vectors generated by a Python hashlib + OpenSSL AES reference that
    // mirrors qpdf's ISO 32000-2 Algorithm 2.B interpretation. File key is
    // bytes 0x00..0x1f; IV is all zeroes for /UE and /OE wrapping.

    struct R6Fixture {
        u: [u8; 48],
        o: [u8; 48],
        ue: [u8; 32],
        oe: [u8; 32],
    }

    impl R6Fixture {
        fn inputs(&self) -> StandardHandlerR5Inputs<'_> {
            StandardHandlerR5Inputs {
                u: &self.u,
                o: &self.o,
                ue: &self.ue,
                oe: &self.oe,
            }
        }
    }

    fn r6_fixture() -> R6Fixture {
        R6Fixture {
            u: hex48(
                "6ce813242d7505a42af6eb24292ac1fe9c8de1a21f598c5205b39d9e9a5ba7bf\
                 00112233445566778899aabbccddeeff",
            ),
            o: hex48(
                "b03bdf6b914364dcdecf182d4cc04bacff9e9a38ea5fd1af31acd59c654495e1\
                 102132435465768798a9bacbdcedfe0f",
            ),
            ue: hex32("4ca56fc060201d966373508e0d5970b65f7581d8f6ff46ee6a3755b623b8379b"),
            oe: hex32("b2ee22084804dbe76635580e7caeb3ba9069d40184ae4ec16eee7aca91d05936"),
        }
    }

    #[test]
    fn r6_password_hash_matches_algorithm_2b_kat() {
        let salt = from_hex("0102030405060708");
        assert_eq!(
            r6_password_hash(b"password", &salt, &[]),
            hex32("22d08d1860cb92edcadda1451a4aebb49c1873722bbfca2aef1a7e5f51e69935")
        );
    }

    #[test]
    fn r6_password_hash_uses_owner_extra_data() {
        let fixture = r6_fixture();
        let salt = from_hex("1021324354657687");
        assert_eq!(
            r6_password_hash(b"ownerpass", &salt, &fixture.u),
            hex32("b03bdf6b914364dcdecf182d4cc04bacff9e9a38ea5fd1af31acd59c654495e1")
        );
    }

    #[test]
    fn r6_password_hash_truncates_password_to_127_bytes() {
        let salt = from_hex("0102030405060708");
        let mut password = vec![b'a'; 127];
        password.extend_from_slice(b"zzz");

        assert_eq!(
            r6_password_hash(&password, &salt, b"extra"),
            hex32("87d58b9b16c2aacf4cb477fa9b5cb57b4b7f6b34d6cfb051b5b35c92a772e723")
        );
    }

    #[test]
    fn check_user_password_r6_returns_file_key() {
        let fixture = r6_fixture();
        let inputs = fixture.inputs();
        let file_key = check_user_password_r6(b"userpass", &inputs).unwrap();
        assert_eq!(
            file_key,
            from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
        );
    }

    #[test]
    fn check_owner_password_r6_returns_file_key() {
        let fixture = r6_fixture();
        let inputs = fixture.inputs();
        let file_key = check_owner_password_r6(b"ownerpass", &inputs).unwrap();
        assert_eq!(
            file_key,
            from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
        );
    }

    #[test]
    fn check_user_password_r6_rejects_wrong_password() {
        let fixture = r6_fixture();
        let inputs = fixture.inputs();
        let err = check_user_password_r6(b"wrong", &inputs).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::BadPassword)
            ),
            "expected BadPassword for wrong R=6 user password, got: {err:?}"
        );
    }

    // ── select_crypt_filter ──────────────────────────────────────────────────

    fn make_cf_table() -> std::collections::HashMap<String, CryptFilter> {
        let mut table = std::collections::HashMap::new();
        table.insert(
            "StdCF".to_string(),
            CryptFilter {
                name: "StdCF".to_string(),
                cfm: CryptFilterMethod::AesV2,
                length_bits: Some(128),
            },
        );
        table.insert(
            "RC4CF".to_string(),
            CryptFilter {
                name: "RC4CF".to_string(),
                cfm: CryptFilterMethod::V2,
                length_bits: None,
            },
        );
        table
    }

    /// name=None → Identity.
    #[test]
    fn select_crypt_filter_none_is_identity() {
        let table = make_cf_table();
        let result = select_crypt_filter(&table, None).unwrap();
        assert_eq!(result, CryptFilterRef::Identity);
    }

    /// name=Some("Identity") → Identity.
    #[test]
    fn select_crypt_filter_identity_name_is_identity() {
        let table = make_cf_table();
        let result = select_crypt_filter(&table, Some("Identity")).unwrap();
        assert_eq!(result, CryptFilterRef::Identity);
    }

    /// name=Some("StdCF") with the entry present → Named.
    #[test]
    fn select_crypt_filter_known_name_returns_named() {
        let table = make_cf_table();
        let result = select_crypt_filter(&table, Some("StdCF")).unwrap();
        match result {
            CryptFilterRef::Named(cf) => {
                assert_eq!(cf.name, "StdCF");
                assert_eq!(cf.cfm, CryptFilterMethod::AesV2);
            }
            CryptFilterRef::Identity => panic!("expected Named, got Identity"),
        }
    }

    /// name=Some("Missing") with no such entry → Malformed.
    #[test]
    fn select_crypt_filter_missing_name_returns_malformed() {
        let table = make_cf_table();
        let err = select_crypt_filter(&table, Some("Missing")).unwrap_err();
        assert!(
            matches!(
                err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::Malformed { .. })
            ),
            "expected Malformed for missing CF entry, got: {err:?}"
        );
    }

    // ── cfm_to_object_key_alg ────────────────────────────────────────────────

    /// CryptFilterMethod::V2 maps to ObjectKeyAlg::Rc4.
    #[test]
    fn cfm_v2_maps_to_rc4() {
        assert_eq!(
            cfm_to_object_key_alg(CryptFilterMethod::V2),
            Some(ObjectKeyAlg::Rc4)
        );
    }

    /// CryptFilterMethod::AesV2 maps to ObjectKeyAlg::Aes.
    #[test]
    fn cfm_aesv2_maps_to_aes() {
        assert_eq!(
            cfm_to_object_key_alg(CryptFilterMethod::AesV2),
            Some(ObjectKeyAlg::Aes)
        );
    }

    /// CryptFilterMethod::Identity maps to None (no cipher needed).
    #[test]
    fn cfm_identity_maps_to_none() {
        assert_eq!(cfm_to_object_key_alg(CryptFilterMethod::Identity), None);
    }

    // ── V4UseSiteSelectors: /StmF /StrF /EFF dispatch ───────────────────────

    /// Demonstrate that /StmF, /StrF, and /EFF resolve correctly via
    /// select_crypt_filter using a V4UseSiteSelectors struct.
    ///
    /// This test confirms the AC requirement that "use site selects the CF":
    /// - stm_f = Some("StdCF") → Named (AesV2)
    /// - str_f = None          → Identity
    /// - eff   = Some("Missing") → Malformed
    #[test]
    fn v4_use_site_selectors_dispatch() {
        let table = make_cf_table();
        let selectors = V4UseSiteSelectors {
            stm_f: Some("StdCF".to_string()),
            str_f: None,
            eff: Some("Missing".to_string()),
        };

        // stm_f resolves to Named(StdCF / AesV2).
        let stm = select_crypt_filter(&table, selectors.stm_f.as_deref()).unwrap();
        match stm {
            CryptFilterRef::Named(cf) => {
                assert_eq!(cf.name, "StdCF");
                assert_eq!(cf.cfm, CryptFilterMethod::AesV2);
            }
            CryptFilterRef::Identity => panic!("stm_f: expected Named, got Identity"),
        }

        // str_f (None) resolves to Identity.
        let str = select_crypt_filter(&table, selectors.str_f.as_deref()).unwrap();
        assert_eq!(
            str,
            CryptFilterRef::Identity,
            "str_f: None must yield Identity"
        );

        // eff with an unknown name resolves to Malformed.
        let eff_err = select_crypt_filter(&table, selectors.eff.as_deref()).unwrap_err();
        assert!(
            matches!(
                eff_err,
                crate::error::Error::Encrypted(crate::error::EncryptedError::Malformed { .. })
            ),
            "eff: missing name must yield Malformed, got: {eff_err:?}"
        );
    }

    /// Per PDF 1.7 §7.6.5.1, `/EFF` absent must fall back to `/StmF`, not
    /// `/Identity`. Resolving eff via `eff_or_stm()` returns the stm_f name
    /// so embedded file streams use the same filter as regular streams.
    #[test]
    fn v4_eff_absent_falls_back_to_stm_f() {
        let table = make_cf_table();
        let selectors = V4UseSiteSelectors {
            stm_f: Some("StdCF".to_string()),
            str_f: Some("StdCF".to_string()),
            eff: None,
        };

        let eff_name = selectors.eff_or_stm();
        assert_eq!(
            eff_name,
            Some("StdCF"),
            "eff absent must fall back to stm_f"
        );

        let resolved = select_crypt_filter(&table, eff_name).unwrap();
        match resolved {
            CryptFilterRef::Named(cf) => {
                assert_eq!(cf.name, "StdCF", "EFF fallback must resolve to StdCF entry");
                assert_eq!(cf.cfm, CryptFilterMethod::AesV2);
            }
            CryptFilterRef::Identity => panic!(
                "EFF=None with StmF=StdCF must NOT short-circuit to Identity; \
                 that would leave encrypted embedded files as plaintext"
            ),
        }
    }

    /// When both `/EFF` and `/StmF` are absent, `eff_or_stm` returns None so
    /// `select_crypt_filter` falls through to `/Identity` — the spec-correct
    /// behavior when neither selector exists in the dictionary.
    #[test]
    fn v4_eff_and_stm_both_absent_resolve_to_identity() {
        let table = make_cf_table();
        let selectors = V4UseSiteSelectors {
            stm_f: None,
            str_f: None,
            eff: None,
        };

        assert_eq!(selectors.eff_or_stm(), None);
        let resolved = select_crypt_filter(&table, selectors.eff_or_stm()).unwrap();
        assert_eq!(resolved, CryptFilterRef::Identity);
    }

    /// When `/EFF` IS present it wins over `/StmF` (no fallback needed).
    #[test]
    fn v4_eff_present_overrides_stm_f() {
        let selectors = V4UseSiteSelectors {
            stm_f: Some("StdCF".to_string()),
            str_f: None,
            eff: Some("OtherCF".to_string()),
        };
        assert_eq!(selectors.eff_or_stm(), Some("OtherCF"));
    }
}