cocoon-tpm-storage 0.1.3

Cocoon TPM project - secure persistent storage
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2025 SUSE LLC
// Author: Nicolai Stange <nstange@suse.de>

//! Encrypt to and decrypt from the various defined encryption entity formats.

extern crate alloc;

use crate::{
    crypto::{
        CryptoDoubleEndedIoSlicesIter, CryptoError, CryptoMutPeekableIoSlicesMutIter, CryptoPeekableIoSlicesIter,
        CryptoWalkableIoSlicesIter, CryptoWalkableIoSlicesMutIter, hash, rng, symcipher,
    },
    fs::{
        NvFsError,
        cocoonfs::{
            FormatError, auth_subject_ids::AuthSubjectDataSuffix, extent_ptr::EncodedExtentPtr,
            extents_layout::ExtentsLayout, layout,
        },
    },
    nvfs_err_internal, tpm2_interface,
    utils_common::{
        bitmanip::BitManip as _,
        fixed_vec::FixedVec,
        io_slices::{
            self, IoSlicesIter as _, IoSlicesIterCommon as _, IoSlicesMutIter as _, WalkableIoSlicesIter as _,
        },
    },
};
use core::{convert, mem, ops::Deref as _};

/// Convert from units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) to units of
/// bytes.
///
/// # Arguments:
///
/// * `allocation_blocks` - The size in units of [Allocation
///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2) to convert
///   from.
/// * `allocation_block_size_128b_log2` - Verbatim copy of
///   [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
fn allocation_blocks_to_len(
    allocation_blocks: layout::AllocBlockCount,
    allocation_block_size_128b_log2: u32,
) -> Result<usize, NvFsError> {
    let allocation_blocks = u64::from(allocation_blocks);
    let len = allocation_blocks << (allocation_block_size_128b_log2 + 7);
    if len >> (allocation_block_size_128b_log2 + 7) != allocation_blocks {
        return Err(NvFsError::DimensionsNotSupported);
    }
    usize::try_from(len).map_err(|_| NvFsError::DimensionsNotSupported)
}

/// Align a length downwards to a multiple of some block cipher block length.
///
/// Returns a pair of the aligned value and padding removed.
///
/// # Arguments:
///
/// * `len` - The length to align.
/// * `block_cipher_alg_block_len` - The block cipher block length.
fn align_len_down_to_block_cipher_alg_block_len(
    len: usize,
    block_cipher_alg_block_len: usize,
) -> Result<(usize, usize), NvFsError> {
    if !block_cipher_alg_block_len.is_pow2() {
        return Err(nvfs_err_internal!());
    }
    let excess = len & (block_cipher_alg_block_len - 1);
    let aligned_len = len - excess;
    Ok((aligned_len, excess))
}

/// Align a length upwards to a multiple of some block cipher block length.
///
/// Returns a pair of the aligned value and padding added.
///
/// # Arguments:
///
/// * `len` - The length to align.
/// * `block_cipher_alg_block_len` - The block cipher block length.
fn align_len_up_to_block_cipher_alg_block_len(
    len: usize,
    block_cipher_alg_block_len: usize,
) -> Result<(usize, usize), NvFsError> {
    if !block_cipher_alg_block_len.is_pow2() {
        return Err(nvfs_err_internal!());
    }
    // Distance to next alignment boundary.
    let required_alignment_padding = len.wrapping_neg() & (block_cipher_alg_block_len - 1);
    Ok((
        len.checked_add(required_alignment_padding)
            .ok_or(NvFsError::DimensionsNotSupported)?,
        required_alignment_padding,
    ))
}

/// Find and validate a PKCS#7 padding.
///
/// Search `decrypted_extents` from the back for the first non-zero byte and
/// confirm it's the last byte of a valid PKCS#7 padding.
///
/// The total number of padding bytes, i.e. the size of the PKCS#7 padding plus
/// any trailing zero bytes will get returned.
///
/// # Arguments:
///
/// * `decrypted_extents` - The decrypted data to find an validate the PKCS#7
///   padding in.
pub fn check_cbc_padding<'a, DI: CryptoDoubleEndedIoSlicesIter<'a>>(
    mut decrypted_extents: DI,
) -> Result<usize, NvFsError> {
    // The used padding is PKCS#7 plus possibly a fillup of zeroes all the way to
    // the extents' end.
    // No constant-time constraints, only authenticated data is getting decrypted.
    let mut trailing_zeroes_len: usize = 0;
    while let Some(tail_slice) = decrypted_extents.next_back_slice(None)? {
        let last_nonzero_pos = match tail_slice.iter().rposition(|b| *b != 0) {
            Some(last_nonzero_pos) => last_nonzero_pos,
            None => {
                trailing_zeroes_len = trailing_zeroes_len
                    .checked_add(tail_slice.len())
                    .ok_or(NvFsError::DimensionsNotSupported)?;
                continue;
            }
        };

        trailing_zeroes_len = trailing_zeroes_len
            .checked_add(tail_slice.len() - last_nonzero_pos - 1)
            .ok_or(NvFsError::DimensionsNotSupported)?;

        let cbc_padding_len = tail_slice[last_nonzero_pos];
        if (cbc_padding_len - 1) as usize > last_nonzero_pos {
            return Err(NvFsError::from(FormatError::InvalidPadding));
        }
        if tail_slice[last_nonzero_pos - (cbc_padding_len - 1) as usize..last_nonzero_pos]
            .iter()
            .any(|b| *b != cbc_padding_len)
        {
            return Err(NvFsError::from(FormatError::InvalidPadding));
        }

        return Ok(trailing_zeroes_len + cbc_padding_len as usize);
    }
    Err(NvFsError::from(FormatError::InvalidPadding))
}

/// Information about a given [block cipher
/// algorithm](symcipher::SymBlockCipherAlg) to be used for the encryption or
/// decryption of some filesystem entity.
#[derive(Clone)]
struct EncryptedEntityBlockCipherAlg {
    /// The block cipher algorithm.
    block_cipher_alg: symcipher::SymBlockCipherAlg,
    /// Block length of [`block_cipher_alg`](Self::block_cipher_alg).
    block_cipher_block_len: usize,
    /// IV length of [`block_cipher_alg`](Self::block_cipher_alg) used in CBC
    /// mode.
    ///
    /// The value is equal to
    /// [`block_cipher_block_len`](Self::block_cipher_block_len), but
    /// maintain it in a dedicated field for a more expressive naming.
    cbc_iv_len: usize,
}

impl convert::From<symcipher::SymBlockCipherAlg> for EncryptedEntityBlockCipherAlg {
    fn from(block_cipher_alg: symcipher::SymBlockCipherAlg) -> Self {
        Self {
            block_cipher_alg,
            block_cipher_block_len: block_cipher_alg.block_len(),
            cbc_iv_len: block_cipher_alg.iv_len_for_mode(tpm2_interface::TpmiAlgCipherMode::Cbc),
        }
    }
}

/// Information about a given [hash algorithm](tpm2_interface::TpmiAlgHash) to
/// be used in a HMAC construction for the the inline authentication of some
/// encrypted filesystem entity.
#[derive(Clone)]
struct EncryptedEntityInlineAuthenticationHmacHashAlg {
    /// The hash algorithm to be used in the HMAC construction.
    hmac_hash_alg: tpm2_interface::TpmiAlgHash,
    /// The length of a digest produced by
    /// [`hmac_hash_alg`](Self::hmac_hash_alg).
    digest_len: usize,
}

impl convert::From<tpm2_interface::TpmiAlgHash> for EncryptedEntityInlineAuthenticationHmacHashAlg {
    fn from(hmac_hash_alg: tpm2_interface::TpmiAlgHash) -> Self {
        Self {
            hmac_hash_alg,
            digest_len: hash::hash_alg_digest_len(hmac_hash_alg) as usize,
        }
    }
}

/// Layout parameters for an "encrypted block" format entity.
#[derive(Clone)]
pub struct EncryptedBlockLayout {
    /// The block cipher algorithm to use for the encryption or decryption
    /// respectively.
    block_cipher_alg: EncryptedEntityBlockCipherAlg,
    /// Base-2 logarithm of the encrypted block's size in units of [Allocation
    /// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    block_allocation_blocks_log2: u8,
    /// Verbatim copy of
    /// [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    allocation_block_size_128b_log2: u8,
}

impl EncryptedBlockLayout {
    /// Create an [`EncryptedBlockLayout`] instance.
    ///
    /// # Arguments:
    ///
    /// * `block_cipher_alg` - The block cipher algorithm to use for the
    ///   encryption or decryption respectively.
    /// * `block_allocation_blocks_log2` - Base-2 logarithm of the encrypted
    ///   block's size in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    /// * `allocation_block_size_128b_log2` - Verbatim copy of
    ///   [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    pub fn new(
        block_cipher_alg: symcipher::SymBlockCipherAlg,
        block_allocation_blocks_log2: u8,
        allocation_block_size_128b_log2: u8,
    ) -> Result<Self, NvFsError> {
        Ok(Self {
            block_cipher_alg: EncryptedEntityBlockCipherAlg::from(block_cipher_alg),
            block_allocation_blocks_log2,
            allocation_block_size_128b_log2,
        })
    }

    /// Get the encrypted block size in units of [Allocation
    /// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    fn block_allocation_blocks(&self) -> layout::AllocBlockCount {
        let block_allocation_blocks_log2 = self.block_allocation_blocks_log2 as u32;
        layout::AllocBlockCount::from(1u64 << block_allocation_blocks_log2)
    }

    /// Get the encrypted block size in units of bytes.
    fn block_len(&self) -> Result<usize, NvFsError> {
        allocation_blocks_to_len(
            self.block_allocation_blocks(),
            self.allocation_block_size_128b_log2 as u32,
        )
    }

    /// Get an encrypted block's maximum payload size in units of bytes.
    pub fn effective_payload_len(&self) -> Result<usize, NvFsError> {
        align_len_down_to_block_cipher_alg_block_len(
            self.block_len()? - self.block_cipher_alg.cbc_iv_len,
            self.block_cipher_alg.block_cipher_block_len,
        )
        .map(|l| l.0)
    }

    /// Get the internal copy of
    /// [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    pub fn get_allocation_block_size_128b_log2(&self) -> u8 {
        self.allocation_block_size_128b_log2
    }

    /// Get the base-2 logarithm of the encrypted block size in units of
    /// [Allocation
    /// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    pub fn get_block_allocation_blocks_log2(&self) -> u8 {
        self.block_allocation_blocks_log2
    }
}

/// Encrypt an "encrypted block" format entity.
///
/// The instance may be used for [encrypting](Self::encrypt_one_block) multiple
/// independent blocks.
pub struct EncryptedBlockEncryptionInstance {
    /// The [`EncryptedBlockLayout`] describing the "encrypted block" format
    /// entity.
    layout: EncryptedBlockLayout,
    /// The the [CBC block cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc)
    /// [`SymBlockCipherModeEncryptionInstance`](symcipher::SymBlockCipherModeEncryptionInstance) to
    /// be used for encryptions.
    block_cipher_instance: symcipher::SymBlockCipherModeEncryptionInstance,
}

impl EncryptedBlockEncryptionInstance {
    /// Create an [`EncryptedBlockEncryptionInstance`].
    ///
    /// # Arguments:
    ///
    /// * `layout` - The [`EncryptedBlockLayout`] describing the "encrypted
    ///   block" format entity.
    /// * `block_cipher_instance` - A
    ///   [`SymBlockCipherModeEncryptionInstance`](symcipher::SymBlockCipherModeEncryptionInstance)
    ///   to be used for encryption. Must be consistent with the
    ///   [`SymBlockCipherAlg`](symcipher::SymBlockCipherAlg) `layout` had been
    ///   created with, and been initialized for operating in the [CBC block
    ///   cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc).
    pub fn new(
        layout: EncryptedBlockLayout,
        block_cipher_instance: symcipher::SymBlockCipherModeEncryptionInstance,
    ) -> Result<Self, NvFsError> {
        if layout.block_cipher_alg.block_cipher_alg != symcipher::SymBlockCipherAlg::from(&block_cipher_instance)
            || tpm2_interface::TpmiAlgCipherMode::from(&block_cipher_instance) != tpm2_interface::TpmiAlgCipherMode::Cbc
        {
            return Err(nvfs_err_internal!());
        }
        Ok(Self {
            layout,
            block_cipher_instance,
        })
    }

    /// Encrypt one "encrypted block" format entity.
    ///
    /// # Arguments:
    ///
    /// * `dst` - Ciphertext destination buffers. Their total size must match
    ///   the length of one encrypted block of the associated
    ///   [`EncryptedBlockLayout`] format.
    /// * `src` - Plaintext source plaintext buffers. Must not exceed
    ///   [`EncryptedBlockLayout::effective_payload_len()`] in length, but may
    ///   be smaller -- any unusued remainder will get filled up with `rng`.
    /// * `rng` - The [random number generator](rng::RngCoreDispatchable) used
    ///   for generating the IV and filling unused padding, if any.
    pub fn encrypt_one_block<'a, 'b, DI: CryptoWalkableIoSlicesMutIter<'a>, SI: CryptoWalkableIoSlicesIter<'b>>(
        &self,
        mut dst: DI,
        src: SI,
        rng: &mut dyn rng::RngCoreDispatchable,
    ) -> Result<(), NvFsError> {
        let block_len = self.layout.block_len()?;
        debug_assert_eq!(dst.total_len()?, block_len);
        let effective_payload_len = self.layout.effective_payload_len()?;
        debug_assert_eq!(
            effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );
        let src_len = src.total_len()?;
        if src_len > effective_payload_len {
            return Err(nvfs_err_internal!());
        }

        let iv_len = self.layout.block_cipher_alg.cbc_iv_len;
        let dst_iv = dst.next_slice_mut(Some(iv_len))?;
        let dst_iv = match dst_iv {
            Some(dst_iv) => dst_iv,
            None => return Err(nvfs_err_internal!()),
        };
        // It is expected that the first slice from dst is large enough to accommodate
        // the IV.
        if dst_iv.len() != iv_len {
            return Err(nvfs_err_internal!());
        }

        // Fill the IV.
        rng::rng_dyn_dispatch_generate(
            rng,
            io_slices::SingletonIoSliceMut::new(dst_iv).map_infallible_err(),
            None,
        )?;

        // Fill any alignment padding inbetween the IV and the beginning of the
        // encrypted data with random bytes. The padding at this location will
        // be empty in practice, but be consistent with the other entity
        // formats.
        rng::rng_dyn_dispatch_generate(
            rng,
            dst.as_ref()
                .take_exact(block_len - iv_len - effective_payload_len)
                .map_err(CryptoError::from),
            None,
        )?;

        // If the to be encrypted source is not a multiple of the block cipher block
        // length, it will get padded with zeroes.
        let src_padding_len =
            align_len_up_to_block_cipher_alg_block_len(src_len, self.block_cipher_instance.block_cipher_block_len())?.1;

        self.block_cipher_instance.encrypt(
            dst_iv,
            dst.as_ref()
                .take_exact(src_len + src_padding_len)
                .map_err(CryptoError::from),
            src.chain(io_slices::ZeroFilledIoSlices::new(src_padding_len).map_infallible_err()),
            None,
        )?;

        // Fill the unused remainder with random bytes.
        rng::rng_dyn_dispatch_generate(rng, dst, None)?;

        Ok(())
    }
}

/// Decrypt an "encrypted block" format entity.
///
/// The instance may be used for [decrypting](Self::decrypt_one_block) multiple
/// independent blocks.
pub struct EncryptedBlockDecryptionInstance {
    /// The [`EncryptedBlockLayout`] describing the "encrypted block" format
    /// entity.
    layout: EncryptedBlockLayout,
    /// The the [CBC block cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc)
    /// [`SymBlockCipherModeDecryptionInstance`](symcipher::SymBlockCipherModeDecryptionInstance) to
    /// be used for encryptions.
    block_cipher_instance: symcipher::SymBlockCipherModeDecryptionInstance,
}

impl EncryptedBlockDecryptionInstance {
    /// Create an [`EncryptedBlockEncryptionInstance`].
    ///
    /// # Arguments:
    ///
    /// * `layout` - The [`EncryptedBlockLayout`] describing the "encrypted
    ///   block" format entity.
    /// * `block_cipher_instance` - A
    ///   [`SymBlockCipherModeDecryptionInstance`](symcipher::SymBlockCipherModeDecryptionInstance)
    ///   to be used for encryption. Must be consistent with the
    ///   [`SymBlockCipherAlg`](symcipher::SymBlockCipherAlg) `layout` had been
    ///   created with, and been initialized for operating in the [CBC block
    ///   cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc).
    pub fn new(
        layout: EncryptedBlockLayout,
        block_cipher_instance: symcipher::SymBlockCipherModeDecryptionInstance,
    ) -> Result<Self, NvFsError> {
        if layout.block_cipher_alg.block_cipher_alg != symcipher::SymBlockCipherAlg::from(&block_cipher_instance)
            || tpm2_interface::TpmiAlgCipherMode::from(&block_cipher_instance) != tpm2_interface::TpmiAlgCipherMode::Cbc
        {
            return Err(nvfs_err_internal!());
        }
        Ok(Self {
            layout,
            block_cipher_instance,
        })
    }

    /// Decrypt one "encrypted block" format entity.
    ///
    /// # Arguments:
    ///
    /// * `dst` - Plaintext destination buffers. Must not exceed
    ///   [`EncryptedBlockLayout::effective_payload_len()`] in length, but may
    ///   be smaller -- any remainder will get discarded.
    /// * `src` - Ciphertext source buffers. Their total size must match the
    ///   length of one encrypted block of the associated
    ///   [`EncryptedBlockLayout`] format.
    pub fn decrypt_one_block<'a, 'b, DI: CryptoWalkableIoSlicesMutIter<'a>, SI: CryptoWalkableIoSlicesIter<'b>>(
        &self,
        mut dst: DI,
        mut src: SI,
    ) -> Result<(), NvFsError> {
        let block_len = self.layout.block_len()?;
        debug_assert_eq!(src.total_len()?, block_len);
        let effective_payload_len = self.layout.effective_payload_len()?;
        debug_assert_eq!(
            effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );
        let dst_len = dst.total_len()?;
        if dst_len > effective_payload_len {
            return Err(nvfs_err_internal!());
        }

        let iv_len = self.layout.block_cipher_alg.cbc_iv_len;
        let src_iv = src.next_slice(Some(iv_len))?;
        let src_iv = match src_iv {
            Some(src_iv) => src_iv,
            None => return Err(nvfs_err_internal!()),
        };
        // It is expected that the first slice from src is large enough to accommodate
        // the IV.
        if src_iv.len() != iv_len {
            return Err(nvfs_err_internal!());
        }

        // Skip over padding after the IV for aligning the encrypted data. It will be
        // empty in practice, but be consistent with the other formats.
        src.skip(block_len - iv_len - effective_payload_len)
            .map_err(CryptoError::from)?;

        // If the destination to be decrypted to is not a multiple of the block cipher
        // block length, provide a scratch buffer to receive the padding.
        let dst_padding_len =
            align_len_up_to_block_cipher_alg_block_len(dst_len, self.block_cipher_instance.block_cipher_block_len())?.1;
        let mut dst_padding_scratch = FixedVec::<u8, 0>::new_with_default(dst_padding_len)?;

        self.block_cipher_instance.decrypt(
            src_iv,
            dst.as_ref()
                .chain(io_slices::SingletonIoSliceMut::new(&mut dst_padding_scratch).map_infallible_err()),
            src.take_exact(dst_len + dst_padding_len).map_err(CryptoError::from),
            None,
        )?;

        Ok(())
    }
}

/// Layout parameters for an "encrypted extents" format entity.
#[derive(Clone)]
pub struct EncryptedExtentsLayout {
    /// The block cipher algorithm to use for the encryption or decryption
    /// respectively.
    block_cipher_alg: EncryptedEntityBlockCipherAlg,
    /// Verbatim copy of
    /// [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    allocation_block_size_128b_log2: u8,
}

impl EncryptedExtentsLayout {
    /// Create an [`EncryptedExtentsLayout`] instance.
    ///
    /// # Arguments:
    ///
    /// * `block_cipher_alg` - The block cipher algorithm to use for the
    ///   encryption or decryption respectively.
    /// * `allocation_block_size_128b_log2` - Verbatim copy of
    ///   [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    pub fn new(
        block_cipher_alg: symcipher::SymBlockCipherAlg,
        allocation_block_size_128b_log2: u8,
    ) -> Result<Self, NvFsError> {
        Ok(Self {
            block_cipher_alg: EncryptedEntityBlockCipherAlg::from(block_cipher_alg),
            allocation_block_size_128b_log2,
        })
    }

    /// Obtain an [`ExtentsLayout`] instance for allocating extents suitable
    /// for an "encrypted extents" format entity described by the
    /// [`EncryptedExtentsLayout`].
    pub fn get_extents_layout(&self) -> Result<ExtentsLayout, NvFsError> {
        ExtentsLayout::new(
            None,
            0,
            self.block_cipher_alg.cbc_iv_len as u32,
            0,
            0,
            u8::try_from(self.block_cipher_alg.block_cipher_block_len).map_err(|_| nvfs_err_internal!())?,
            self.allocation_block_size_128b_log2,
        )
    }

    /// Compute the length of one extent in units of bytes.
    ///
    /// # Arguments:
    ///
    /// * `extent_allocation_blocks` - Extent length in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    fn extent_len(&self, extent_allocation_blocks: layout::AllocBlockCount) -> Result<usize, NvFsError> {
        allocation_blocks_to_len(extent_allocation_blocks, self.allocation_block_size_128b_log2 as u32)
    }

    /// Compute the effective payload length in units of bytes that can be
    /// stored in a given extent.
    ///
    /// Note that the final PKCS#7 padding is not being accounted for, i.e. it's
    /// being considered as part of the (total) payload length.
    ///
    /// # Arguments:
    ///
    /// * `extent_allocation_blocks` - Extent length in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    /// * `is_first` - Whether or not the extent is the first one in an
    ///   "encrypted extents" format entity.
    fn effective_payload_len(
        &self,
        extent_allocation_blocks: layout::AllocBlockCount,
        is_first: bool,
    ) -> Result<usize, NvFsError> {
        let iv_len = if is_first { self.block_cipher_alg.cbc_iv_len } else { 0 };

        align_len_down_to_block_cipher_alg_block_len(
            self.extent_len(extent_allocation_blocks)? - iv_len,
            self.block_cipher_alg.block_cipher_block_len,
        )
        .map(|l| l.0)
    }
}

/// Encrypt an "encrypted extents" format entity.
///
/// The entity's extents are to be [encrypted](Self::encrypt_one_extent) one
/// after another with the instance.
pub struct EncryptedExtentsEncryptionInstance {
    /// The [`EncryptedExtentsLayout`] describing the "encrypted extents" format
    /// entity.
    layout: EncryptedExtentsLayout,
    /// The the [CBC block cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc)
    /// [`SymBlockCipherModeEncryptionInstance`](symcipher::SymBlockCipherModeEncryptionInstance) to
    /// be used for encryptions.
    block_cipher_instance: symcipher::SymBlockCipherModeEncryptionInstance,
    /// The "carry-over" CBC extent to be used for the next extent in the
    /// sequence.
    next_iv: FixedVec<u8, 4>,
}

impl EncryptedExtentsEncryptionInstance {
    /// Create an [`EncryptedExtentsEncryptionInstance`].
    ///
    /// # Arguments:
    ///
    /// * `layout` - The [`EncryptedExtentsLayout`] describing the "encrypted
    ///   extents" format entity.
    /// * `block_cipher_instance` - A
    ///   [`SymBlockCipherModeEncryptionInstance`](symcipher::SymBlockCipherModeEncryptionInstance)
    ///   to be used for encryption. Must be consistent with the
    ///   [`SymBlockCipherAlg`](symcipher::SymBlockCipherAlg) `layout` had been
    ///   created with, and been initialized for operating in the [CBC block
    ///   cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc).
    pub fn new(
        layout: &EncryptedExtentsLayout,
        block_cipher_instance: symcipher::SymBlockCipherModeEncryptionInstance,
    ) -> Result<Self, NvFsError> {
        if layout.block_cipher_alg.block_cipher_alg != symcipher::SymBlockCipherAlg::from(&block_cipher_instance)
            || tpm2_interface::TpmiAlgCipherMode::from(&block_cipher_instance) != tpm2_interface::TpmiAlgCipherMode::Cbc
        {
            return Err(nvfs_err_internal!());
        }
        Ok(Self {
            layout: layout.clone(),
            block_cipher_instance,
            next_iv: FixedVec::new_empty(),
        })
    }

    /// Encrypt one extent in a sequence of an "encrypted extents" format
    /// entity's extents.
    ///
    /// Encrypt as much of `src` into `dst` as fits into the extent's payload.
    /// `src` will get advanced by the consumed amount, so that a `mut`
    /// reference to a single [iterator](CryptoWalkableIoSlicesIter)
    /// instance over all of the entity's plaintext can get conveniently
    /// passed to a series of `encrypt_one_extent()` invocations.
    ///
    /// Once the plaintext remaining in `src` becomes less than the payload that
    /// fits the destination extent, indicating that this is the "encrypted
    /// extents" format entity's last extent, the payload will get amended
    /// by a PKCS#7 padding. It is a logic error to invoke
    /// `encrypt_one_extent()` more than once with the remaining `src`
    /// length being less than the respective extent payload length.
    ///
    /// # Arguments:
    ///
    /// * `dst` - The current extent's ciphertext destination buffers.
    /// * `src` - The plaintext source buffers. Will get advanced by the amount
    ///   consumed.
    /// * `extent_allocation_blocks` - Size of the current extent in units of
    ///   [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must be
    ///   consistent with `dst`'s total length.
    /// * `rng` - The [random number generator](rng::RngCoreDispatchable) used
    ///   for generating the IV and filling padding, if any.
    pub fn encrypt_one_extent<'a, 'b, DI: CryptoWalkableIoSlicesMutIter<'a>, SI: CryptoWalkableIoSlicesIter<'b>>(
        &mut self,
        mut dst: DI,
        mut src: SI,
        extent_allocation_blocks: layout::AllocBlockCount,
        rng: &mut dyn rng::RngCoreDispatchable,
    ) -> Result<(), NvFsError> {
        let is_first = self.next_iv.is_empty();
        let extent_len = self.layout.extent_len(extent_allocation_blocks)?;
        debug_assert_eq!(dst.total_len()?, extent_len);
        let effective_payload_len = self.layout.effective_payload_len(extent_allocation_blocks, is_first)?;
        debug_assert_eq!(
            effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );

        let total_src_len = src.total_len()?;
        // Apply PKCS#7 padding if at the last extent, otherwise use all the extent's
        // available (and aligned) payload.
        let (src_len, src_padding_len) = if total_src_len < effective_payload_len {
            let mut src_padding_len = align_len_up_to_block_cipher_alg_block_len(
                total_src_len,
                self.block_cipher_instance.block_cipher_block_len(),
            )?
            .1;
            if src_padding_len == 0 {
                src_padding_len = self.block_cipher_instance.block_cipher_block_len();
            }
            // effective_payload_len is aligned to the block cipher block length.
            debug_assert!(total_src_len + src_padding_len <= effective_payload_len);
            (total_src_len, src_padding_len)
        } else {
            (effective_payload_len, 0)
        };
        let src_padding = FixedVec::<u8, 0>::new_with_value(src_padding_len, src_padding_len as u8)?;

        let iv_len = self.layout.block_cipher_alg.cbc_iv_len;
        if is_first {
            self.next_iv = FixedVec::new_with_default(iv_len)?;
            rng::rng_dyn_dispatch_generate(
                rng,
                io_slices::SingletonIoSliceMut::new(&mut self.next_iv).map_infallible_err(),
                None,
            )?;

            // Copy the IV to the destination.
            dst.as_ref()
                .take_exact(iv_len)
                .copy_from_iter_exhaustive(io_slices::SingletonIoSlice::new(&self.next_iv).map_infallible_err())
                .map_err(CryptoError::from)?;

            // Fill any alignment padding inbetween the IV and the beginning of the
            // encrypted data with random bytes. The padding at this location will
            // be empty in practice, but be consistent with the other entity
            // formats.
            rng::rng_dyn_dispatch_generate(
                rng,
                dst.as_ref()
                    .take_exact(extent_len - iv_len - effective_payload_len)
                    .map_err(CryptoError::from),
                None,
            )?;
        } else {
            // Fill any alignment padding inbetween the start of the destination buffer and
            // the beginning of the encrypted data with random bytes. The
            // padding at this location will be empty in practice, but be
            // consistent with the other entity formats.
            rng::rng_dyn_dispatch_generate(
                rng,
                dst.as_ref()
                    .take_exact(extent_len - effective_payload_len)
                    .map_err(CryptoError::from),
                None,
            )?;
        }
        debug_assert_eq!(dst.total_len()?, effective_payload_len);

        let mut iv_out = FixedVec::new_with_default(iv_len)?;
        self.block_cipher_instance.encrypt(
            &self.next_iv,
            dst,
            src.as_ref()
                .take_exact(src_len)
                .map_err(CryptoError::from)
                .chain(io_slices::SingletonIoSlice::new(&src_padding).map_infallible_err())
                .chain(
                    io_slices::ZeroFilledIoSlices::new(effective_payload_len - src_len - src_padding_len)
                        .map_infallible_err(),
                ),
            Some(&mut iv_out),
        )?;
        self.next_iv = iv_out;

        Ok(())
    }
}

/// Decrypt an "encrypted extents" format entity.
///
/// The entity's extents are to be [decrypted](Self::decrypt_one_extent) one
/// after another in encryption order with the instance.
pub struct EncryptedExtentsDecryptionInstance {
    /// The [`EncryptedExtentsLayout`] describing the "encrypted extents" format
    /// entity.
    layout: EncryptedExtentsLayout,
    /// The the [CBC block cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc)
    /// [`SymBlockCipherModeDecryptionInstance`](symcipher::SymBlockCipherModeDecryptionInstance) to
    /// be used for decryptions.
    block_cipher_instance: symcipher::SymBlockCipherModeDecryptionInstance,
    /// The "carry-over" CBC extent to be used for the next extent in the
    /// sequence.
    next_iv: FixedVec<u8, 4>,
}

impl EncryptedExtentsDecryptionInstance {
    /// Create an [`EncryptedExtentsDecryptionInstance`].
    ///
    /// # Arguments:
    ///
    /// * `layout` - The [`EncryptedExtentsLayout`] describing the "encrypted
    ///   extents" format entity.
    /// * `block_cipher_instance` - A
    ///   [`SymBlockCipherModeDecryptionInstance`](symcipher::SymBlockCipherModeDecryptionInstance)
    ///   to be used for decryption. Must be consistent with the
    ///   [`SymBlockCipherAlg`](symcipher::SymBlockCipherAlg) `layout` had been
    ///   created with, and been initialized for operating in the [CBC block
    ///   cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc).
    pub fn new(
        layout: EncryptedExtentsLayout,
        block_cipher_instance: symcipher::SymBlockCipherModeDecryptionInstance,
    ) -> Result<Self, NvFsError> {
        if layout.block_cipher_alg.block_cipher_alg != symcipher::SymBlockCipherAlg::from(&block_cipher_instance)
            || tpm2_interface::TpmiAlgCipherMode::from(&block_cipher_instance) != tpm2_interface::TpmiAlgCipherMode::Cbc
        {
            return Err(nvfs_err_internal!());
        }
        Ok(Self {
            layout,
            block_cipher_instance,
            next_iv: FixedVec::new_empty(),
        })
    }

    /// Determine the maximum plaintext length the ciphertext in an extent of
    /// given length would decrypt to.
    ///
    /// Note that the extent of the specified length would in fact decrypt to a
    /// length exactly equal to the returned value. The actual payload
    /// obtained after stripping off the terminating PKCS#7 padding will be
    /// shorter in length though.
    ///
    /// # Arguments:
    ///
    /// * `extent_allocation_blocks` - The extent size in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    /// * `is_first` - Whether or not the extent is the first in the entity's
    ///   sequence of extents.
    pub fn max_extent_decrypted_len(
        &self,
        extent_allocation_blocks: layout::AllocBlockCount,
        is_first: bool,
    ) -> Result<usize, NvFsError> {
        let total_effective_payload_len = self.layout.effective_payload_len(extent_allocation_blocks, is_first)?;
        debug_assert_eq!(
            total_effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );
        Ok(total_effective_payload_len)
    }

    /// Determine the maximum plaintext length the ciphertext stored
    /// collectively in an "encrypted extents" format entity's extent would
    /// decrypt to.
    ///
    /// Note that the extents of the specified length would in fact collectively
    /// decrypt to a length exactly equal to the returned value. The actual
    /// payload obtained after stripping off the terminating PKCS#7 padding
    /// will be shorter in length though.
    ///
    /// # Arguments:
    ///
    /// * `extents` - [`Iterator`] yielding the entity's extents' lengths.
    pub fn max_extents_decrypted_len<EI: Iterator<Item = layout::PhysicalAllocBlockRange>>(
        &self,
        extents: EI,
    ) -> Result<usize, NvFsError> {
        let mut is_first = true;
        let mut total_effective_payload_len = 0usize;
        for extent in extents {
            total_effective_payload_len = match total_effective_payload_len
                .checked_add(self.max_extent_decrypted_len(extent.block_count(), is_first)?)
            {
                Some(total_effective_payload_len) => total_effective_payload_len,
                None => return Err(NvFsError::DimensionsNotSupported),
            };
            is_first = false;
        }
        Ok(total_effective_payload_len)
    }

    /// Decrypt one extent in a sequence of an "encrypted extents" format
    /// entity's extents.
    ///
    /// Decrypt `src` into `dst`. `dst` will get advanced by the decrypted
    /// plaintext's length, so that a `mut` reference to a single
    /// [iterator](CryptoWalkableIoSlicesMutIter) instance wrapping some
    /// destination buffers to eventually receive all of the entity's
    /// plaintext can get conveniently passed to a series of
    /// `decrypt_one_extent()` invocations.
    ///
    /// The entity's total decrypted plaintext, i.e. the concatention of all its
    /// extents' individual plaintexts, will have a PKCS#7 padding
    /// plus possibly some trailing zeros left and `decrypt_one_extent()` would
    /// not strip those. It's the caller's responsibility to eventually find
    /// the payload plaintext's end by means of [`check_cbc_padding()`].
    ///
    /// # Arguments:
    ///
    /// * `dst` - The plaintext destination buffers. Must have at least enough
    ///   capacity left to accomodate for the current extent's decrypted
    ///   payload, as determined by
    ///   [`max_extent_decrypted_len()`](Self::max_extent_decrypted_len).
    /// * `src` - The extent's ciphertext source buffers.
    /// * `extent_allocation_blocks` - Size of the current extent in units of
    ///   [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must be
    ///   consistent with `src`'s total length.
    pub fn decrypt_one_extent<'a, 'b, DI: CryptoWalkableIoSlicesMutIter<'a>, SI: CryptoWalkableIoSlicesIter<'b>>(
        &mut self,
        mut dst: DI,
        mut src: SI,
        extent_allocation_blocks: layout::AllocBlockCount,
    ) -> Result<(), NvFsError> {
        let is_first = self.next_iv.is_empty();
        let extent_len = self.layout.extent_len(extent_allocation_blocks)?;
        debug_assert_eq!(src.total_len()?, extent_len);
        let effective_payload_len = self.layout.effective_payload_len(extent_allocation_blocks, is_first)?;
        debug_assert_eq!(
            effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );
        let dst_len = dst.total_len()?;
        if dst_len < effective_payload_len {
            return Err(nvfs_err_internal!());
        }

        let iv_len = self.layout.block_cipher_alg.cbc_iv_len;
        if is_first {
            // Copy the IV from the source.
            self.next_iv = FixedVec::new_with_default(iv_len)?;
            io_slices::SingletonIoSliceMut::new(&mut self.next_iv)
                .map_infallible_err()
                .copy_from_iter_exhaustive(src.as_ref().take_exact(iv_len))
                .map_err(CryptoError::from)?;

            // Skip over padding after the IV for aligning the encrypted data. It will be
            // empty in practice, but be consistent with the other formats.
            src.skip(extent_len - iv_len - effective_payload_len)
                .map_err(CryptoError::from)?
        } else {
            // Skip any alignment padding inbetween the start of the destination buffer and
            // the beginning of the encrypted data. The padding at this location
            // will be empty in practice, but be consistent with the other
            // entity formats.
            src.skip(extent_len - effective_payload_len)
                .map_err(CryptoError::from)?
        }
        debug_assert_eq!(src.total_len()?, effective_payload_len);

        let mut iv_out = FixedVec::new_with_default(iv_len)?;
        self.block_cipher_instance.decrypt(
            &self.next_iv,
            dst.as_ref()
                .take_exact(effective_payload_len)
                .map_err(CryptoError::from),
            src,
            Some(&mut iv_out),
        )?;
        self.next_iv = iv_out;

        Ok(())
    }
}

/// Layout parameters for an "encrypted chained extents" format entity.
#[derive(Clone)]
pub struct EncryptedChainedExtentsLayout {
    /// Length of a header to be stored in plain at the beginning of the
    /// entity's first extent, like e.g. a magic.
    plain_data_extents_hdr_len: usize,
    /// The block cipher algorithm to use for the encryption or decryption
    /// respectively.
    block_cipher_alg: EncryptedEntityBlockCipherAlg,
    /// The HMAC hash algorithm to use for the entity's inline authentication,
    /// if any.
    inline_authentication_hmac_hash_alg: Option<EncryptedEntityInlineAuthenticationHmacHashAlg>,
    /// Alignment constraint on the entity's extents.
    extent_alignment_allocation_blocks_log2: u8,
    /// Verbatim copy of
    /// [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    allocation_block_size_128b_log2: u8,
}

impl EncryptedChainedExtentsLayout {
    /// Create an [`EncryptedChainedExtentsLayout`] instance.
    ///
    /// # Arguments:
    ///
    /// * `plain_data_extents_hdr_len` - Length of a header to be stored in
    ///   plain at the beginning of the entity's first extent, like e.g. a
    ///   magic.
    /// * `block_cipher_alg` - The block cipher algorithm to use for the
    ///   encryption or decryption respectively.
    /// * `inline_authentication_hmac_hash_alg` - The HMAC hash algorithm to use
    ///   for the entity's inline authentication, if any.
    /// * `extent_alignment_allocation_blocks_log2` - Alignment constraint on
    ///   the entity's extents.
    /// * `allocation_block_size_128b_log2` - Verbatim copy of
    ///   [`ImageLayout::allocation_block_size_128b_log2`](layout::ImageLayout::allocation_block_size_128b_log2).
    pub fn new(
        plain_data_extents_hdr_len: usize,
        block_cipher_alg: symcipher::SymBlockCipherAlg,
        inline_authentication_hmac_hash_alg: Option<tpm2_interface::TpmiAlgHash>,
        extent_alignment_allocation_blocks_log2: u8,
        allocation_block_size_128b_log2: u8,
    ) -> Result<Self, NvFsError> {
        if extent_alignment_allocation_blocks_log2 as u32 > u64::BITS
            || 1u64 << extent_alignment_allocation_blocks_log2 > EncodedExtentPtr::MAX_EXTENT_ALLOCATION_BLOCKS
        {
            return Err(nvfs_err_internal!());
        }

        let block_cipher_alg = EncryptedEntityBlockCipherAlg::from(block_cipher_alg);

        if plain_data_extents_hdr_len
            .checked_add(block_cipher_alg.cbc_iv_len)
            .is_none()
        {
            return Err(NvFsError::DimensionsNotSupported);
        }

        Ok(Self {
            plain_data_extents_hdr_len,
            block_cipher_alg,
            inline_authentication_hmac_hash_alg: inline_authentication_hmac_hash_alg.map(
                |inline_authentication_hmac_hash_alg| {
                    EncryptedEntityInlineAuthenticationHmacHashAlg::from(inline_authentication_hmac_hash_alg)
                },
            ),
            extent_alignment_allocation_blocks_log2,
            allocation_block_size_128b_log2,
        })
    }

    /// Obtain an [`ExtentsLayout`] instance for allocating extents suitable
    /// for an "encrypted chained extents" format entity described by the
    /// [`EncryptedChainedExtentsLayout`].
    pub fn get_extents_layout(&self) -> Result<ExtentsLayout, NvFsError> {
        let extents_hdr_len = u32::try_from(self.plain_data_extents_hdr_len + self.block_cipher_alg.cbc_iv_len)
            .map_err(|_| NvFsError::DimensionsNotSupported)?;
        ExtentsLayout::new(
            Some(layout::AllocBlockCount::from(
                EncodedExtentPtr::MAX_EXTENT_ALLOCATION_BLOCKS,
            )),
            self.extent_alignment_allocation_blocks_log2,
            extents_hdr_len,
            self.inline_authentication_hmac_hash_alg
                .as_ref()
                .map(|inline_authentication_hmac_hash_alg| inline_authentication_hmac_hash_alg.digest_len as u32)
                .unwrap_or(0),
            EncodedExtentPtr::ENCODED_SIZE,
            u8::try_from(self.block_cipher_alg.block_cipher_block_len).map_err(|_| nvfs_err_internal!())?,
            self.allocation_block_size_128b_log2,
        )
    }

    /// Compute the length of one extent in units of bytes.
    ///
    /// # Arguments:
    ///
    /// * `extent_allocation_blocks` - Extent length in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    fn extent_len(&self, extent_allocation_blocks: layout::AllocBlockCount) -> Result<usize, NvFsError> {
        allocation_blocks_to_len(extent_allocation_blocks, self.allocation_block_size_128b_log2 as u32)
    }

    /// Compute the effective payload length in units of bytes that can be
    /// stored in a given extent.
    ///
    /// Note that the final PKCS#7 padding is not being accounted for, i.e. it's
    /// being considered as part of the (total) payload length.
    ///
    /// # Arguments:
    ///
    /// * `extent_allocation_blocks` - Extent length in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    /// * `is_first` - Whether or not the extent is the first one in an
    ///   "encrypted chained extents" format entity.
    fn effective_payload_len(
        &self,
        extent_allocation_blocks: layout::AllocBlockCount,
        is_first: bool,
    ) -> Result<usize, NvFsError> {
        let extents_hdr_len = if is_first {
            self.plain_data_extents_hdr_len + self.block_cipher_alg.cbc_iv_len
        } else {
            0
        };

        let inline_authentication_digest_len = self
            .inline_authentication_hmac_hash_alg
            .as_ref()
            .map(|inline_authentication_hmac_hash_alg| inline_authentication_hmac_hash_alg.digest_len)
            .unwrap_or(0);

        let total_hdr_len = extents_hdr_len
            .checked_add(inline_authentication_digest_len)
            .ok_or(NvFsError::DimensionsNotSupported)?;

        let extent_len = self.extent_len(extent_allocation_blocks)?;
        if total_hdr_len > extent_len {
            return Err(nvfs_err_internal!());
        }

        align_len_down_to_block_cipher_alg_block_len(
            extent_len - total_hdr_len,
            self.block_cipher_alg.block_cipher_block_len,
        )
        .map(|l| l.0)
    }
}

/// Authentication subject identifiers to be used for associated data to be
/// authenticated with "encrypted chained extents" format entities' inline
/// authentication, if any.
#[repr(u8)]
pub enum EncryptedChainedExtentsAssociatedDataAuthSubjectDataSuffix {
    /// The entity in "encrypted chained extents" format is the journal log.
    JournalLog = 1,
    /// The entity in "encrypted chained extents" format is some (special)
    /// inode's associated extents list.
    InodeExtentsListPreauthCcaProtection = 2,
}

/// Encrypt an "encrypted chained extents" format entity.
///
/// The entity's extents are to be [encrypted](Self::encrypt_one_extent) one
/// after another with the instance, supplying the link to the next extent in
/// the linked list for each.
pub struct EncryptedChainedExtentsEncryptionInstance {
    /// The [`EncryptedChainedExtentsLayout`] describing the "encrypted chained
    /// extents" format entity.
    layout: EncryptedChainedExtentsLayout,
    /// The the [CBC block cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc)
    /// [`SymBlockCipherModeEncryptionInstance`](symcipher::SymBlockCipherModeEncryptionInstance) to
    /// be used for encryptions.
    block_cipher_instance: symcipher::SymBlockCipherModeEncryptionInstance,
    /// The "carry-over" CBC extent to be used for the next extent in the
    /// sequence.
    next_iv: FixedVec<u8, 4>,
    /// If inline authentication is enabled: a pair of a
    /// [`HmacInstance`](hash::HmacInstance) in its initial state and the
    /// entity's previous extents' associated digest, if any.
    inline_authentication: Option<(hash::HmacInstance, FixedVec<u8, 5>)>,
}

impl EncryptedChainedExtentsEncryptionInstance {
    /// Create an [`EncryptedChainedExtentsEncryptionInstance`].
    ///
    /// # Arguments:
    ///
    /// * `layout` - The [`EncryptedChainedExtentsLayout`] describing the
    ///   "encrypted chained extents" format entity.
    /// * `block_cipher_instance` - A
    ///   [`SymBlockCipherModeEncryptionInstance`](symcipher::SymBlockCipherModeEncryptionInstance)
    ///   to be used for encryption. Must be consistent with the
    ///   [`SymBlockCipherAlg`](symcipher::SymBlockCipherAlg) `layout` had been
    ///   created with, and been initialized for operating in the [CBC block
    ///   cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc).
    /// * `inline_authentication_hmac_instance` - Optional
    ///   [`HmacInstance`](hash::HmacInstance) in its initial state. Must be
    ///   provided if and only if some [`hash
    ///   algorithm`](tpm2_interface::TpmiAlgHash) to be used for the inline
    ///   authentication had been specified at the creation of `layout`, and
    ///   must be consistent with that if so.
    pub fn new(
        layout: &EncryptedChainedExtentsLayout,
        block_cipher_instance: symcipher::SymBlockCipherModeEncryptionInstance,
        inline_authentication_hmac_instance: Option<hash::HmacInstance>,
    ) -> Result<Self, NvFsError> {
        if layout.block_cipher_alg.block_cipher_alg != symcipher::SymBlockCipherAlg::from(&block_cipher_instance)
            || tpm2_interface::TpmiAlgCipherMode::from(&block_cipher_instance) != tpm2_interface::TpmiAlgCipherMode::Cbc
        {
            return Err(nvfs_err_internal!());
        }

        match layout.inline_authentication_hmac_hash_alg.as_ref() {
            Some(inline_authentication_hmac_hash_alg) => {
                match inline_authentication_hmac_instance.as_ref() {
                    Some(inline_authentication_hmac_instance) => {
                        if inline_authentication_hmac_hash_alg.hmac_hash_alg
                            != tpm2_interface::TpmiAlgHash::from(inline_authentication_hmac_instance)
                        {
                            return Err(nvfs_err_internal!());
                        }
                    }
                    None => {
                        // The authentication digest must always get updated at encryption.
                        return Err(nvfs_err_internal!());
                    }
                }
            }
            None => {
                // Inline authentication not specifed for the layout, yet an instance has been
                // provided.
                if inline_authentication_hmac_instance.is_some() {
                    return Err(nvfs_err_internal!());
                }
            }
        }

        Ok(Self {
            layout: layout.clone(),
            block_cipher_instance,
            next_iv: FixedVec::new_empty(),
            inline_authentication: inline_authentication_hmac_instance.map(|inline_authentication_hmac_instance| {
                (inline_authentication_hmac_instance, FixedVec::new_empty())
            }),
        })
    }

    /// Encrypt one extent in a sequence of an "encrypted chained extents"
    /// format entity's extents.
    ///
    /// Encrypt as much of `src` into `dst` as fits into the extent's payload.
    /// `src` will get advanced by the consumed amount, so that a `mut`
    /// reference to a single [iterator](CryptoWalkableIoSlicesIter)
    /// instance over all of the entity's plaintext can get conveniently
    /// passed to a series of `encrypt_one_extent()` invocations.
    ///
    /// Once the plaintext remaining in `src` becomes less than the payload that
    /// fits the destination extent, indicating that this is the "encrypted
    /// extents" format entity's last extent, the payload will get amended
    /// by a PKCS#7 padding. It is a logic error to invoke
    /// `encrypt_one_extent()` more than once with the remaining `src`
    /// length being less than the respective extent payload length.
    ///
    /// If some non-zero plain header length had been specified at the creation
    /// of the associated [`EncryptedChainedExtentsLayout`], then it is
    /// expected that a header of that length has been written to the
    /// beginning of the entity's first extent's `dst` already. If inline
    /// authentication is enabled for the entity, then that header will be
    /// considered in the authentication.
    ///
    /// # Arguments:
    ///
    /// * `dst` - The current extent's ciphertext destination buffers.
    /// * `src` - The plaintext source buffers. Will get advanced by the amount
    ///   consumed.
    /// * `authenticated_associated_data` - Additional data to be considered for
    ///   the inline authentication if enabled for the entity, ignored
    ///   otherwise.
    /// * `extent_allocation_blocks` - Size of the current extent in units of
    ///   [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must be
    ///   consistent with `dst`'s total length.
    /// * `next_chained_extent` - The location of the next extent in the
    ///   sequence of the "encryted chained extents" format entity's extents.
    /// * `rng` - The [random number generator](rng::RngCoreDispatchable) used
    ///   for generating the IV and filling padding, if any.
    pub fn encrypt_one_extent<
        'a,
        'b,
        'c,
        DI: CryptoMutPeekableIoSlicesMutIter<'a>,
        SI: CryptoWalkableIoSlicesIter<'b>,
        AI: CryptoWalkableIoSlicesIter<'c>,
    >(
        &mut self,
        mut dst: DI,
        mut src: SI,
        authenticated_associated_data: AI,
        extent_allocation_blocks: layout::AllocBlockCount,
        next_chained_extent: Option<&layout::PhysicalAllocBlockRange>,
        rng: &mut dyn rng::RngCoreDispatchable,
    ) -> Result<(), NvFsError> {
        let is_first = self.next_iv.is_empty();
        let extent_len = self.layout.extent_len(extent_allocation_blocks)?;
        debug_assert_eq!(dst.total_len()?, extent_len);
        let total_effective_payload_len = self.layout.effective_payload_len(extent_allocation_blocks, is_first)?;
        debug_assert_eq!(
            total_effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );

        if total_effective_payload_len < EncodedExtentPtr::ENCODED_SIZE as usize
            || !is_first && total_effective_payload_len == EncodedExtentPtr::ENCODED_SIZE as usize
        {
            return Err(nvfs_err_internal!());
        }
        let available_effective_payload_len = total_effective_payload_len - EncodedExtentPtr::ENCODED_SIZE as usize;

        let total_src_len = src.total_len()?;
        // Apply PKCS#7 padding if at the last extent, otherwise use all the extent's
        // available payload.
        let (src_len, src_padding_len) = if total_src_len < available_effective_payload_len {
            if next_chained_extent.is_some() {
                return Err(nvfs_err_internal!());
            }
            let mut src_padding_len = align_len_up_to_block_cipher_alg_block_len(
                total_src_len + EncodedExtentPtr::ENCODED_SIZE as usize,
                self.block_cipher_instance.block_cipher_block_len(),
            )?
            .1;
            if src_padding_len == 0 {
                src_padding_len = self.block_cipher_instance.block_cipher_block_len();
            }
            // total_effective_payload_len is aligned to the block cipher block length.
            debug_assert!(total_src_len + src_padding_len <= available_effective_payload_len);
            (total_src_len, src_padding_len)
        } else {
            if next_chained_extent.is_none() {
                return Err(nvfs_err_internal!());
            }
            (available_effective_payload_len, 0)
        };
        let src_padding = FixedVec::<u8, 0>::new_with_value(src_padding_len, src_padding_len as u8)?;

        let inline_authentication_digest_len = self
            .inline_authentication
            .as_ref()
            .map(|inline_authentication| inline_authentication.0.digest_len())
            .unwrap_or(0);

        // Decouple from the dst iterator for encryption, a second pass will
        // be needed for the inline authentication, if any.
        let mut dst_encrypt = dst.decoupled_borrow_mut();
        if is_first {
            dst_encrypt
                .skip(self.layout.plain_data_extents_hdr_len)
                .map_err(CryptoError::from)?;
        }
        dst_encrypt
            .skip(inline_authentication_digest_len)
            .map_err(CryptoError::from)?;

        let iv_len = self.layout.block_cipher_alg.cbc_iv_len;
        if is_first {
            self.next_iv = FixedVec::new_with_default(iv_len)?;
            rng::rng_dyn_dispatch_generate(
                rng,
                io_slices::SingletonIoSliceMut::new(&mut self.next_iv).map_infallible_err(),
                None,
            )?;

            // Copy the IV to the destination.
            dst_encrypt
                .as_ref()
                .take_exact(iv_len)
                .copy_from_iter_exhaustive(io_slices::SingletonIoSlice::new(&self.next_iv).map_infallible_err())
                .map_err(CryptoError::from)?;

            // Fill any alignment padding inbetween the IV and the beginning of the
            // encrypted data with random bytes.
            rng::rng_dyn_dispatch_generate(
                rng,
                dst_encrypt
                    .as_ref()
                    .take_exact(
                        extent_len
                            - self.layout.plain_data_extents_hdr_len
                            - inline_authentication_digest_len
                            - iv_len
                            - total_effective_payload_len,
                    )
                    .map_err(CryptoError::from),
                None,
            )?;
        } else {
            // Fill any alignment padding inbetween the start of the destination buffer and
            // the beginning of the encrypted data with random bytes.
            rng::rng_dyn_dispatch_generate(
                rng,
                dst_encrypt
                    .as_ref()
                    .take_exact(extent_len - inline_authentication_digest_len - total_effective_payload_len)
                    .map_err(CryptoError::from),
                None,
            )?;
        }
        debug_assert_eq!(dst_encrypt.total_len()?, total_effective_payload_len);

        let encoded_extent_ptr = EncodedExtentPtr::encode(next_chained_extent, false)?;

        let mut iv_out = FixedVec::new_with_default(iv_len)?;
        self.block_cipher_instance.encrypt(
            &self.next_iv,
            dst_encrypt,
            io_slices::SingletonIoSlice::new(encoded_extent_ptr.deref())
                .map_infallible_err()
                .chain(src.as_ref().take_exact(src_len).map_err(CryptoError::from))
                .chain(io_slices::SingletonIoSlice::new(&src_padding).map_infallible_err())
                .chain(
                    io_slices::ZeroFilledIoSlices::new(available_effective_payload_len - src_len - src_padding_len)
                        .map_infallible_err(),
                ),
            Some(&mut iv_out),
        )?;
        // Don't update self.next_iv with iv_out until after the authentication digest
        // has been computed below.

        // Now compute and serialize the inline authentication digest, if requested.
        if let Some(inline_authentication) = self.inline_authentication.as_mut() {
            let (inline_authentication_hmac_instance, prev_extent_inline_authentication_digest) = inline_authentication;

            let mut inline_authentication_hmac_instance = inline_authentication_hmac_instance.try_clone()?;
            let mut dst_auth = dst.decoupled_borrow();
            if is_first {
                inline_authentication_hmac_instance.update(
                    dst_auth
                        .as_ref()
                        .take_exact(self.layout.plain_data_extents_hdr_len)
                        .map_err(CryptoError::from),
                )?;

                // The first extent has virtual zeroes filled into the authentication digest
                // region.
                inline_authentication_hmac_instance.update(
                    io_slices::ZeroFilledIoSlices::new(inline_authentication_digest_len).map_infallible_err(),
                )?;
                dst_auth
                    .skip(inline_authentication_digest_len)
                    .map_err(CryptoError::from)?;
                // The position is now at the IV serialized above.
            } else {
                // With respect to CCA-protection, an extent within the extent
                // chain is interpreted as a single, isolated
                // ciphertext. So for that, authentication of
                // the IV in combination with the extent's to be
                // decrypted contents is needed.
                //
                // Including the previous extent's authentication digest
                // provides integrity protection, but *not*
                // authentication of the overall extent chain as a whole (or
                // it does, but only at the security level of collision
                // resistance rather than that of the MAC, i.e.
                // at approx. a half). For the Journal Record Area integrity
                // protection is highly desired, and we're getting it almost for
                // free by implementing this hash chain on top
                // of the authentication needed anyway. For
                // simplicity, chain the digests unconditionally, not only when
                // integrity protection is desired.
                inline_authentication_hmac_instance.update(
                    io_slices::SingletonIoSlice::new(prev_extent_inline_authentication_digest).map_infallible_err(),
                )?;
                dst_auth
                    .skip(inline_authentication_digest_len)
                    .map_err(CryptoError::from)?;
            }

            // Digest the encrypted data, including any randomized alignment padding. For
            // the first extent, this also includes the IV.
            inline_authentication_hmac_instance.update(dst_auth)?;

            // Now digest the context suffix.
            // For continuation extents, this includes the IV.
            if !is_first {
                inline_authentication_hmac_instance
                    .update(io_slices::SingletonIoSlice::new(&self.next_iv).map_infallible_err())?;
            }

            // Then comes the associated data, if any.
            let authenticated_associated_data_len = u64::try_from(authenticated_associated_data.total_len()?)
                .map_err(|_| nvfs_err_internal!())?
                .to_le_bytes();
            inline_authentication_hmac_instance.update(authenticated_associated_data)?;

            // The remainder authentication context suffix uniquely encodes the semantics of
            // the rest.
            let auth_context_subject_id_suffix = [
                !is_first as u8,
                0u8, // Version of the authenticated data's format.
                AuthSubjectDataSuffix::EncryptionEntityChainedExtents as u8,
            ];
            let auth_context_enc_params = {
                let block_cipher_alg = symcipher::SymBlockCipherAlg::from(&self.block_cipher_instance);
                let (block_cipher_alg_id, block_cipher_key_size) =
                    <(tpm2_interface::TpmiAlgSymObject, u16)>::from(&block_cipher_alg);
                let mut auth_context_enc_params =
                    [0u8; tpm2_interface::TpmiAlgSymObject::marshalled_size() as usize + mem::size_of::<u16>()];
                let context_buf = block_cipher_alg_id
                    .marshal(&mut auth_context_enc_params)
                    .map_err(|_| nvfs_err_internal!())?;
                tpm2_interface::marshal_u16(context_buf, block_cipher_key_size).map_err(|_| nvfs_err_internal!())?;
                auth_context_enc_params
            };
            inline_authentication_hmac_instance.update(
                io_slices::BuffersSliceIoSlicesIter::new(&[
                    authenticated_associated_data_len.as_slice(),
                    auth_context_enc_params.as_slice(),
                    auth_context_subject_id_suffix.as_slice(),
                ])
                .map_infallible_err(),
            )?;

            if prev_extent_inline_authentication_digest.is_empty() {
                *prev_extent_inline_authentication_digest =
                    FixedVec::new_with_default(inline_authentication_digest_len)?;
            }
            // Produce the digest and copy it to the destination.
            inline_authentication_hmac_instance
                .finalize_into(prev_extent_inline_authentication_digest.as_mut_slice())?;
            if is_first {
                dst.skip(self.layout.plain_data_extents_hdr_len)
                    .map_err(CryptoError::from)?;
            }
            dst.as_ref()
                .take_exact(inline_authentication_digest_len)
                .map_err(CryptoError::from)
                .copy_from_iter_exhaustive(
                    io_slices::SingletonIoSlice::new(prev_extent_inline_authentication_digest).map_infallible_err(),
                )
                .map_err(CryptoError::from)?;
        }
        debug_assert_eq!(
            dst.total_len()?,
            extent_len
                - if is_first {
                    self.layout.plain_data_extents_hdr_len
                } else {
                    0
                }
                - inline_authentication_digest_len
        );
        // Now that the authentication digest has been computed, the IV to be used for
        // the next continuation extent can get stored away.
        self.next_iv = iv_out;

        // Finally, advance the original iterator to past the processed position in case
        // the user somehow relies on it.
        dst.skip(
            extent_len
                - if is_first {
                    self.layout.plain_data_extents_hdr_len
                } else {
                    0
                }
                - inline_authentication_digest_len,
        )
        .map_err(CryptoError::from)?;
        debug_assert!(dst.is_empty()?);

        Ok(())
    }
}

/// Decrypt an "encrypted chained extents" format entity.
///
/// The entity's extents are to be [decrypted](Self::decrypt_one_extent) one
/// after another in encryption order with the instance. Note that the
/// decryption of one extents yields the pointer to the next, if any.
pub struct EncryptedChainedExtentsDecryptionInstance {
    layout: EncryptedChainedExtentsLayout,
    block_cipher_instance: symcipher::SymBlockCipherModeDecryptionInstance,
    next_iv: FixedVec<u8, 4>,
    inline_authentication: Option<(hash::HmacInstance, FixedVec<u8, 5>)>,
}

impl EncryptedChainedExtentsDecryptionInstance {
    /// Create an [`EncryptedChainedExtentsDecryptionInstance`].
    ///
    /// # Arguments:
    ///
    /// * `layout` - The [`EncryptedChainedExtentsLayout`] describing the
    ///   "encrypted chained extents" format entity.
    /// * `block_cipher_instance` - A
    ///   [`SymBlockCipherModeDecryptionInstance`](symcipher::SymBlockCipherModeDecryptionInstance)
    ///   to be used for decryption. Must be consistent with the
    ///   [`SymBlockCipherAlg`](symcipher::SymBlockCipherAlg) `layout` had been
    ///   created with, and been initialized for operating in the [CBC block
    ///   cipher mode](tpm2_interface::TpmiAlgCipherMode::Cbc).
    /// * `inline_authentication_hmac_instance` - Optional
    ///   [`HmacInstance`](hash::HmacInstance) in its initial state. Must be
    ///   provided only if some [`hash algorithm`](tpm2_interface::TpmiAlgHash)
    ///   to be used for the inline authentication had been specified at the
    ///   creation of `layout`, and must be consistent with that. The entity's
    ///   inline authentication will get verified in the course of decryption if
    ///   provided.
    pub fn new(
        layout: &EncryptedChainedExtentsLayout,
        block_cipher_instance: symcipher::SymBlockCipherModeDecryptionInstance,
        inline_authentication_hmac_instance: Option<hash::HmacInstance>,
    ) -> Result<Self, NvFsError> {
        if layout.block_cipher_alg.block_cipher_alg != symcipher::SymBlockCipherAlg::from(&block_cipher_instance)
            || tpm2_interface::TpmiAlgCipherMode::from(&block_cipher_instance) != tpm2_interface::TpmiAlgCipherMode::Cbc
        {
            return Err(nvfs_err_internal!());
        }

        match layout.inline_authentication_hmac_hash_alg.as_ref() {
            Some(inline_authentication_hmac_hash_alg) => {
                if let Some(inline_authentication_hmac_instance) = inline_authentication_hmac_instance.as_ref() {
                    if inline_authentication_hmac_hash_alg.hmac_hash_alg
                        != tpm2_interface::TpmiAlgHash::from(inline_authentication_hmac_instance)
                    {
                        return Err(nvfs_err_internal!());
                    }
                }
            }
            None => {
                // Inline authentication not specifed for the layout, yet an instance has been
                // provided.
                if inline_authentication_hmac_instance.is_some() {
                    return Err(nvfs_err_internal!());
                }
            }
        }

        Ok(Self {
            layout: layout.clone(),
            block_cipher_instance,
            next_iv: FixedVec::new_empty(),
            inline_authentication: inline_authentication_hmac_instance.map(|inline_authentication_hmac_instance| {
                (inline_authentication_hmac_instance, FixedVec::new_empty())
            }),
        })
    }

    /// Determine the maximum plaintext length the ciphertext in an extent of
    /// given length would decrypt to.
    ///
    /// Note that the extent of the specified length would in fact decrypt to a
    /// length exactly equal to the returned value. The actual payload
    /// obtained after stripping off the terminating PKCS#7 padding will be
    /// shorter in length though.
    ///
    /// # Arguments:
    ///
    /// * `extent_allocation_blocks` - The extent size in units of [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
    /// * `is_first` - Whether or not the extent is the first in the entity's
    ///   sequence of extents.
    pub fn max_extent_decrypted_len(
        &self,
        extent_allocation_blocks: layout::AllocBlockCount,
        is_first: bool,
    ) -> Result<usize, NvFsError> {
        let total_effective_payload_len = self.layout.effective_payload_len(extent_allocation_blocks, is_first)?;
        debug_assert_eq!(
            total_effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );

        if total_effective_payload_len < EncodedExtentPtr::ENCODED_SIZE as usize
            || !is_first && total_effective_payload_len == EncodedExtentPtr::ENCODED_SIZE as usize
        {
            return Err(nvfs_err_internal!());
        }
        Ok(total_effective_payload_len - EncodedExtentPtr::ENCODED_SIZE as usize)
    }

    /// Decrypt one extent in a sequence of an "encrypted chained extents"
    /// format entity's extents.
    ///
    /// Decrypt `src` into `dst` and return the next chained extent's location,
    /// if any.
    ///
    /// `dst` will get advanced by the decrypted
    /// plaintext's length, so that a `mut` reference to a single
    /// [iterator](CryptoWalkableIoSlicesMutIter) instance wrapping some
    /// destination buffers to eventually receive all of the entity's
    /// plaintext can get conveniently passed to a series of
    /// `decrypt_one_extent()` invocations.
    ///
    /// The entity's total decrypted plaintext, i.e. the concatention of all its
    /// extents' individual plaintexts, will have a PKCS#7 padding
    /// plus possibly some trailing zeros left and `decrypt_one_extent()` would
    /// not strip those. It's the caller's responsibility to eventually find
    /// the payload plaintext's end by means of [`check_cbc_padding()`].
    ///
    /// If some non-zero plain header length had been specified at the creation
    /// of the associated [`EncryptedChainedExtentsLayout`], then it is
    /// expected that a header of that length is stored to the beginning of
    /// the entity's first extent's `src`. If inline authentication is
    /// enabled, then that header will be considered for verifying the
    /// authentication.
    ///
    /// # Arguments:
    ///
    /// * `dst` - The plaintext destination buffers. Must have at least enough
    ///   capacity left to accomodate for the current extent's decrypted
    ///   payload, as determined by
    ///   [`max_extent_decrypted_len()`](Self::max_extent_decrypted_len).
    /// * `src` - The extent's ciphertext source buffers.
    /// * `authenticated_associated_data` - Additional data to be considered for
    ///   the inline authentication if enabled for the entity, ignored
    ///   otherwise.
    /// * `extent_allocation_blocks` - Size of the current extent in units of
    ///   [Allocation
    ///   Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must be
    ///   consistent with `src`'s total length.
    pub fn decrypt_one_extent<
        'a,
        'b,
        'c,
        DI: CryptoMutPeekableIoSlicesMutIter<'a>,
        SI: CryptoPeekableIoSlicesIter<'b>,
        AI: CryptoWalkableIoSlicesIter<'c>,
    >(
        &mut self,
        mut dst: DI,
        mut src: SI,
        authenticated_associated_data: AI,
        extent_allocation_blocks: layout::AllocBlockCount,
    ) -> Result<Option<layout::PhysicalAllocBlockRange>, NvFsError> {
        let is_first = self.next_iv.is_empty();
        let extent_len = self.layout.extent_len(extent_allocation_blocks)?;
        debug_assert_eq!(src.total_len()?, extent_len);
        let total_effective_payload_len = self.layout.effective_payload_len(extent_allocation_blocks, is_first)?;
        debug_assert_eq!(
            total_effective_payload_len % self.block_cipher_instance.block_cipher_block_len(),
            0
        );

        if total_effective_payload_len < EncodedExtentPtr::ENCODED_SIZE as usize
            || !is_first && total_effective_payload_len == EncodedExtentPtr::ENCODED_SIZE as usize
        {
            return Err(nvfs_err_internal!());
        }
        let available_effective_payload_len = total_effective_payload_len - EncodedExtentPtr::ENCODED_SIZE as usize;

        let dst_len = dst.total_len()?;
        if dst_len < available_effective_payload_len {
            return Err(nvfs_err_internal!());
        }

        // Unconditionally obtain the inline authentication digest length from the
        // layout in case verification is not enabled for this decryption
        // request (like for pre-auth CCA protection when proper authentication
        // is available).
        let inline_authentication_digest_len = self
            .layout
            .inline_authentication_hmac_hash_alg
            .as_ref()
            .map(|inline_authentication_hmac_hash_alg| inline_authentication_hmac_hash_alg.digest_len)
            .unwrap_or(0);

        // Verify the inline authentication digest, if requested.
        if let Some(inline_authentication) = self.inline_authentication.as_mut() {
            let (inline_authentication_hmac_instance, prev_extent_inline_authentication_digest) = inline_authentication;

            let mut inline_authentication_hmac_instance = inline_authentication_hmac_instance.try_clone()?;

            let mut src_auth = src.decoupled_borrow();
            if is_first {
                inline_authentication_hmac_instance.update(
                    src_auth
                        .as_ref()
                        .take_exact(self.layout.plain_data_extents_hdr_len)
                        .map_err(CryptoError::from),
                )?;

                // The first extent has virtual zeroes filled into the authentication digest
                // region.
                inline_authentication_hmac_instance.update(
                    io_slices::ZeroFilledIoSlices::new(inline_authentication_digest_len).map_infallible_err(),
                )?;
                src_auth
                    .skip(inline_authentication_digest_len)
                    .map_err(CryptoError::from)?;
                // The position is now at the IV.
            } else {
                // C.f. the comment in the encryption routine about the rationale for including
                // the preceding extent's authentication digest.
                inline_authentication_hmac_instance.update(
                    io_slices::SingletonIoSlice::new(prev_extent_inline_authentication_digest).map_infallible_err(),
                )?;
                src_auth
                    .skip(inline_authentication_digest_len)
                    .map_err(CryptoError::from)?;
            }

            // Digest the encrypted data, including any randomized alignment padding. For
            // the first extent, this also includes the IV.
            inline_authentication_hmac_instance.update(src_auth)?;

            // Now digest the context suffix.
            // For continuation extents, this includes the IV.
            if !is_first {
                inline_authentication_hmac_instance
                    .update(io_slices::SingletonIoSlice::new(&self.next_iv).map_infallible_err())?;
            }

            // Then comes the associated data, if any.
            let authenticated_associated_data_len = u64::try_from(authenticated_associated_data.total_len()?)
                .map_err(|_| nvfs_err_internal!())?
                .to_le_bytes();
            inline_authentication_hmac_instance.update(authenticated_associated_data)?;

            // The remainder authentication context suffix uniquely encodes the semantics of
            // the rest.
            let auth_context_subject_id_suffix = [
                !is_first as u8,
                0u8, // Version of the authenticated data's format.
                AuthSubjectDataSuffix::EncryptionEntityChainedExtents as u8,
            ];
            let auth_context_enc_params = {
                let block_cipher_alg = symcipher::SymBlockCipherAlg::from(&self.block_cipher_instance);
                let (block_cipher_alg_id, block_cipher_key_size) =
                    <(tpm2_interface::TpmiAlgSymObject, u16)>::from(&block_cipher_alg);
                let mut auth_context_enc_params =
                    [0u8; tpm2_interface::TpmiAlgSymObject::marshalled_size() as usize + mem::size_of::<u16>()];
                let context_buf = block_cipher_alg_id
                    .marshal(&mut auth_context_enc_params)
                    .map_err(|_| nvfs_err_internal!())?;
                tpm2_interface::marshal_u16(context_buf, block_cipher_key_size).map_err(|_| nvfs_err_internal!())?;
                auth_context_enc_params
            };
            inline_authentication_hmac_instance.update(
                io_slices::BuffersSliceIoSlicesIter::new(&[
                    authenticated_associated_data_len.as_slice(),
                    auth_context_enc_params.as_slice(),
                    auth_context_subject_id_suffix.as_slice(),
                ])
                .map_infallible_err(),
            )?;

            if prev_extent_inline_authentication_digest.is_empty() {
                *prev_extent_inline_authentication_digest =
                    FixedVec::new_with_default(inline_authentication_digest_len)?;
            }
            // Produce the digest and compare with what's expected.
            inline_authentication_hmac_instance
                .finalize_into(prev_extent_inline_authentication_digest.as_mut_slice())?;

            if is_first {
                src.skip(self.layout.plain_data_extents_hdr_len)
                    .map_err(CryptoError::from)?;
            }
            if src
                .as_ref()
                .take_exact(inline_authentication_digest_len)
                .map_err(CryptoError::from)
                .ct_eq_with_iter(
                    io_slices::SingletonIoSlice::new(prev_extent_inline_authentication_digest).map_infallible_err(),
                )?
                .unwrap()
                == 0
            {
                return Err(NvFsError::AuthenticationFailure);
            }
        } else {
            // Don't authenticate, but skip the iterator over the inline authentication
            // digest.
            if is_first {
                src.skip(self.layout.plain_data_extents_hdr_len)
                    .map_err(CryptoError::from)?;
            }
            src.skip(inline_authentication_digest_len).map_err(CryptoError::from)?;
        }
        debug_assert_eq!(
            src.total_len()?,
            extent_len
                - inline_authentication_digest_len
                - if is_first {
                    self.layout.plain_data_extents_hdr_len
                } else {
                    0
                }
        );

        let iv_len = self.layout.block_cipher_alg.cbc_iv_len;
        if is_first {
            // Copy the IV out from the first source extent.
            self.next_iv = FixedVec::new_with_default(iv_len)?;
            io_slices::SingletonIoSliceMut::new(&mut self.next_iv)
                .map_infallible_err()
                .copy_from_iter_exhaustive(src.as_ref().take_exact(iv_len).map_err(CryptoError::from))
                .map_err(CryptoError::from)?;
        }

        // Skip over any alignment inserted before the encrypted data.
        src.skip(
            extent_len
                - inline_authentication_digest_len
                - if is_first {
                    self.layout.plain_data_extents_hdr_len + iv_len
                } else {
                    0
                }
                - total_effective_payload_len,
        )
        .map_err(CryptoError::from)?;
        debug_assert_eq!(src.total_len()?, total_effective_payload_len);

        // And decrypt.
        let mut encoded_next_chained_extent = [0u8; EncodedExtentPtr::ENCODED_SIZE as usize];

        let mut iv_out = FixedVec::new_with_default(iv_len)?;
        self.block_cipher_instance.decrypt(
            &self.next_iv,
            io_slices::SingletonIoSliceMut::new(&mut encoded_next_chained_extent)
                .map_infallible_err()
                .chain(
                    dst.as_ref()
                        .take_exact(available_effective_payload_len)
                        .map_err(CryptoError::from),
                ),
            src,
            Some(&mut iv_out),
        )?;
        self.next_iv = iv_out;

        let next_chained_extent = EncodedExtentPtr::from(encoded_next_chained_extent)
            .decode(self.layout.allocation_block_size_128b_log2 as u32)?;
        let next_chained_extent = match next_chained_extent {
            Some((next_chained_extent, is_indirect)) => {
                // Chained continuation extents are always direct.
                if is_indirect {
                    return Err(NvFsError::from(FormatError::InvalidExtents));
                }
                Some(next_chained_extent)
            }
            None => None,
        };

        Ok(next_chained_extent)
    }
}