File: __init__.py

package info (click to toggle)
python-securetar 2026.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,848 kB
  • sloc: python: 2,639; makefile: 12; sh: 6
file content (1863 lines) | stat: -rw-r--r-- 64,726 bytes parent folder | download
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
"""Tarfile fileobject handler for encrypted files."""

from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Callable, Generator, Hashable
import copy
import enum
import hashlib
import hmac
import logging
import os
import struct
import tarfile
import time
from pathlib import Path, PurePath
from types import NoneType, TracebackType
from typing import TYPE_CHECKING, Any, IO, BinaryIO, Literal

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import (
    Cipher,
    algorithms,
    modes,
)
import nacl.bindings.crypto_secretstream as nss
from nacl.bindings.crypto_secretstream import (
    crypto_secretstream_xchacha20poly1305_TAG_FINAL as NSS_TAG_FINAL,
    crypto_secretstream_xchacha20poly1305_TAG_MESSAGE as NSS_TAG_MESSAGE,
)
import nacl.encoding
from nacl.exceptions import CryptoError
from nacl.hash import blake2b
from nacl.pwhash.argon2id import kdf, SALTBYTES as ARGON2_SALT_SIZE
from nacl.utils import random as nacl_random

_LOGGER: logging.Logger = logging.getLogger(__name__)

# Sizes for AES, used in v1 and v2
AES_BLOCK_SIZE = 16
AES_BLOCK_SIZE_BITS = 128
AES_IV_SIZE = AES_BLOCK_SIZE

# Sizes for v3
V3_SECRETSTREAM_ABYTES = nss.crypto_secretstream_xchacha20poly1305_ABYTES
V3_SECRETSTREAM_CHUNK_SIZE = 1024 * 1024  # 1 MiB
V3_KDF_OPSLIMIT = 8
V3_KDF_MEMLIMIT = 16 * 1024 * 1024  # 16 MiB
V3_DERIVED_KEY_SIZE = 32
V3_DERIVED_KEY_SALT_SIZE = 16
V3_CHACHA20_HEADER_SIZE = nss.crypto_secretstream_xchacha20poly1305_HEADERBYTES

DEFAULT_BUFSIZE = 10240

# The initial header consists of 16 bytes of magic, version, reserved.
# The 16 bytes size is chosen to align with v1 which has no magic, just 16 byte AES IV.
SECURETAR_MAGIC = b"SecureTar"
SECURETAR_MAGIC_RESERVED = b"\x00" * 6

# SecureTar v1 header consists of:
# 0 bytes file ID (no magic)
# 0 bytes file metadata (no metadata)
# 16 bytes AES IV
SECURETAR_LEGACY_HEADER_SIZE = AES_IV_SIZE

# Securetar file ID and metadata formats used in v2 and v3:
# 16 bytes file ID: 9 bytes magic + 1 byte version + 6 bytes reserved
# 16 bytes file metadata: 8 bytes plaintext size + 8 bytes reserved
SECURETAR_FILE_ID_FORMAT = "!9sB6s"
SECURETAR_FILE_METADATA_FORMAT = "!Q8x"

# SecureTar v2 header consists of:
# 32 bytes file ID + metadata
# 16 bytes AES IV
# Note: The reserved bytes are currently unused and written as \x00. The reserved
# bytes in the file ID must be zero when reading. The reserved bytes in the file
# metadata are ignored when reading.
SECURETAR_V2_CIPHER_INIT_SIZE = AES_IV_SIZE
SECURETAR_V2_HEADER_SIZE = (
    struct.calcsize(SECURETAR_FILE_ID_FORMAT)
    + struct.calcsize(SECURETAR_FILE_METADATA_FORMAT)
    + SECURETAR_V2_CIPHER_INIT_SIZE
)

# SecureTar v3 header consists of:
# 16 bytes file ID: 9 bytes magic + 1 byte version + 6 bytes reserved
# 16 bytes file metadata: 8 bytes plaintext size + 8 bytes reserved
# 104 bytes cipher initialization:
#  - 16 bytes root salt
#  - 16 bytes validation salt
#  - 32 bytes validation key
#  - 16 bytes validation salt
#  - 24 bytes cipher header + nonce
SECURETAR_V3_CIPHER_INIT_FORMAT = (
    f"!{ARGON2_SALT_SIZE}s"  # Root salt
    f"{V3_DERIVED_KEY_SALT_SIZE}s"  # Validation key salt
    f"{V3_DERIVED_KEY_SIZE}s"  # Validation derived key
    f"{V3_DERIVED_KEY_SALT_SIZE}s"  # Secret stream key salt
    f"{V3_CHACHA20_HEADER_SIZE}s"  # Cipher header + nonce (24 bytes)
)
SECURETAR_V3_CIPHER_INIT_SIZE = struct.calcsize(SECURETAR_V3_CIPHER_INIT_FORMAT)
SECURETAR_V3_HEADER_SIZE = (
    struct.calcsize(SECURETAR_FILE_ID_FORMAT)
    + struct.calcsize(SECURETAR_FILE_METADATA_FORMAT)
    + SECURETAR_V3_CIPHER_INIT_SIZE
)

GZIP_MAGIC_BYTES = b"\x1f\x8b\x08"
TAR_MAGIC_BYTES = b"ustar"
TAR_MAGIC_OFFSET = 257

TAR_BLOCK_SIZE = 512

MOD_EXCLUSIVE = "x"
MOD_READ = "r"
MOD_WRITE = "w"

DEFAULT_CIPHER_VERSION = 3


class CipherMode(enum.Enum):
    """Cipher mode."""

    ENCRYPT = 1
    DECRYPT = 2


class SecureTarHeader:
    """SecureTar header.

    Reads and produces the SecureTar header. Also accepts the magic-less
    format used in earlier releases of SecureTar.
    """

    def __init__(
        self, cipher_initialization: bytes, plaintext_size: int | None, version: int
    ) -> None:
        """Initialize SecureTar header."""
        self.cipher_initialization = cipher_initialization
        self.plaintext_size = plaintext_size
        if version not in (1, 2, 3):
            raise ValueError(f"Unsupported SecureTar version: {version}")
        self.version = version

        if version == 1:
            self.size = SECURETAR_LEGACY_HEADER_SIZE
        elif version == 2:
            self.size = SECURETAR_V2_HEADER_SIZE
        else:
            self.size = SECURETAR_V3_HEADER_SIZE

    @classmethod
    def from_bytes(cls, f: IO[bytes]) -> SecureTarHeader:
        """Create from bytes."""
        # Read magic, version (1 byte), reserved
        header = f.read(struct.calcsize(SECURETAR_FILE_ID_FORMAT))
        plaintext_size: int | None = None
        magic, version, reserved = struct.unpack(SECURETAR_FILE_ID_FORMAT, header)
        if magic == SECURETAR_MAGIC:
            # Magic matches, assume SecureTar v2+
            if version not in (2, 3):
                raise ValueError(f"Unsupported SecureTar version: {version}")
            if reserved != SECURETAR_MAGIC_RESERVED:
                raise ValueError("Invalid reserved bytes in SecureTar header")

            file_metadata = f.read(struct.calcsize(SECURETAR_FILE_METADATA_FORMAT))
            # Valid SecureTar v2+ header, read rest of header: plaintext size + reserved
            (plaintext_size,) = struct.unpack(
                SECURETAR_FILE_METADATA_FORMAT, file_metadata
            )
            if version == 2:
                cipher_initialization = f.read(SECURETAR_V2_CIPHER_INIT_SIZE)
            else:
                cipher_initialization = f.read(SECURETAR_V3_CIPHER_INIT_SIZE)
        else:
            # Assume legacy format without magic
            cipher_initialization = header
            version = 1

        return cls(cipher_initialization, plaintext_size, version)

    def to_bytes(self) -> bytes:
        """Return header bytes."""
        if self.plaintext_size is None:
            raise ValueError("Plaintext size is required")
        # Check version.
        # SecureTar versions writing v1 had bugs related to how the padding was
        # handled, and we don't support creating such archives anymore.
        if self.version not in (2, 3):
            raise ValueError(f"Unsupported SecureTar version: {self.version}")

        return (
            struct.pack(
                SECURETAR_FILE_ID_FORMAT,
                SECURETAR_MAGIC,
                self.version,
                SECURETAR_MAGIC_RESERVED,
            )
            + struct.pack(SECURETAR_FILE_METADATA_FORMAT, self.plaintext_size)
            + self.cipher_initialization
        )


class SecureTarError(Exception):
    """SecureTar error."""


class AddFileError(SecureTarError):
    """Raised when a file could not be added to an archive."""

    def __init__(self, path: Path, *args: Any) -> None:
        """Initialize."""
        self.path = path
        super().__init__(*args)


class InvalidPasswordError(SecureTarError):
    """SecureTar invalid password error."""


class SecureTarReadError(SecureTarError):
    """SecureTar read error."""


def _is_valid_tar_header(data: bytes) -> bool:
    """Check if data looks like a valid tar or gzip header."""
    is_gzip = data[: len(GZIP_MAGIC_BYTES)] == GZIP_MAGIC_BYTES
    is_tar = (
        data[TAR_MAGIC_OFFSET : TAR_MAGIC_OFFSET + len(TAR_MAGIC_BYTES)]
        == TAR_MAGIC_BYTES
    )
    return is_gzip or is_tar


class CipherStream(ABC):
    """Abstract base for cipher stream operations."""

    @abstractmethod
    def close(self) -> None:
        """Close the stream."""


class CipherReader(CipherStream):
    """Abstract base for cipher readers with buffered read."""

    def __init__(self, source: IO[bytes], ciphertext_size: int | None = None) -> None:
        """Initialize cipher reader."""
        self._buffer = b""
        self._ciphertext_size = ciphertext_size
        self._done = False
        self._source = source

    def read(self, size: int = 0) -> bytes:
        """Read data from buffer, filling as needed."""
        if not self._done:
            self._fill_buffer(size)

        data = self._buffer[:size]
        self._buffer = self._buffer[size:]
        return data

    @abstractmethod
    def _fill_buffer(self, size: int) -> None:
        """Fill buffer with at least size bytes if possible."""


class DecryptReader(CipherReader):
    """Abstract base for decryption readers."""

    def __init__(
        self,
        source: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
        ciphertext_size: int | None = None,
        plaintext_size: int | None = None,
    ) -> None:
        """Initialize decryption reader."""
        super().__init__(source, ciphertext_size)
        self._plaintext_size = plaintext_size

    @property
    def plaintext_size(self) -> int | None:
        """Return the total plaintext bytes written."""
        return self._plaintext_size


class EncryptWriter(CipherStream):
    """Abstract base for encryption writers."""

    def __init__(
        self,
        dest: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
    ) -> None:
        """Initialize encryption writer."""
        self._dest = dest
        self._plaintext_size = 0

    def write(self, data: bytes) -> None:
        """Write plaintext data to be encrypted."""
        self._plaintext_size += len(data)
        self._write(data)

    @abstractmethod
    def _write(self, data: bytes) -> None:
        """Write plaintext data to be encrypted."""

    @property
    def plaintext_size(self) -> int:
        """Return the total plaintext bytes written."""
        return self._plaintext_size


class EncryptReader(CipherReader):
    """Abstract base for encryption readers (for streaming encryption)."""

    _ciphertext_size: int

    def __init__(
        self,
        source: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
        plaintext_size: int,
    ) -> None:
        """Initialize encryption reader."""
        super().__init__(source, 0)
        self._plaintext_size = plaintext_size

    @property
    def ciphertext_size(self) -> int:
        """Return the total ciphertext size."""
        return self._ciphertext_size


class CipherStreamFactory(ABC):
    """Abstract factory for creating cipher streams."""

    create_decrypt_reader: type[DecryptReader]
    """Create a decryption reader."""

    create_encrypt_writer: type[EncryptWriter]
    """Create an encryption writer."""

    create_encrypt_reader: type[EncryptReader]
    """Create an encryption reader (for streaming encryption with known size)."""


class _AesCbcDecryptReader(DecryptReader):
    """AES-CBC decryption reader.

    AES-CBC is used in SecureTar v1 and v2.
    """

    def __init__(
        self,
        source: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
        ciphertext_size: int | None = None,
        plaintext_size: int | None = None,
    ) -> None:
        """Initialize."""
        super().__init__(source, key_material, ciphertext_size, plaintext_size)
        self._pos = 0
        self._validated = False

        aes = Cipher(
            algorithms.AES(key_material.key),
            modes.CBC(key_material.iv),
            backend=default_backend(),
        )
        self._cipher = aes.decryptor()

    def _validate(self) -> None:
        """Validate decrypted data looks like tar/gzip."""
        if self._validated:
            return

        # Read first block to validate
        encrypted = self._source.read(TAR_BLOCK_SIZE)
        self._pos = len(encrypted)
        self._buffer = self._cipher.update(encrypted)

        if not _is_valid_tar_header(self._buffer):
            raise SecureTarReadError("The inner tar is not gzip or tar, wrong key?")

        self._validated = True

    def _fill_buffer(self, size: int) -> None:
        """Fill buffer with decrypted data."""
        if not self._validated:
            self._validate()

        # Determine how much more to read
        to_read = max(size, DEFAULT_BUFSIZE)
        if self._ciphertext_size is not None:
            # Bounded: read up to remaining ciphertext
            remaining = self._ciphertext_size - self._pos
            to_read = min(to_read, remaining)

        while len(self._buffer) < size + AES_BLOCK_SIZE and not self._done:
            encrypted_data = self._source.read(to_read)
            if not encrypted_data:
                # EOF - strip padding
                self._strip_padding()
                self._done = True
                break

            self._pos += len(encrypted_data)
            self._buffer += self._cipher.update(encrypted_data)

            # Check if we've read all ciphertext (bounded case)
            if self._ciphertext_size is not None and self._pos >= self._ciphertext_size:
                self._strip_padding()
                self._done = True
                break

    def _strip_padding(self) -> None:
        """Strip PKCS7 padding from buffer."""
        if self._buffer:
            padding_len = self._buffer[-1]
            self._buffer = self._buffer[:-padding_len]

    def close(self) -> None:
        """Close the decrypt reader."""
        # Set to None to help catch any use after close bugs
        self._cipher = None  # type: ignore[assignment]


class _AesCbcEncryptWriter(EncryptWriter):
    """AES-CBC encryption writer.

    AES-CBC is used in SecureTar v1 and v2.
    """

    def __init__(
        self,
        dest: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
    ) -> None:
        """Initialize."""
        super().__init__(dest, key_material)

        aes = Cipher(
            algorithms.AES(key_material.key),
            modes.CBC(key_material.iv),
            backend=default_backend(),
        )
        self._cipher = aes.encryptor()
        self._padder = padding.PKCS7(AES_BLOCK_SIZE_BITS).padder()

    def _write(self, data: bytes) -> None:
        """Write plaintext data to be encrypted."""
        padded = self._padder.update(data)
        if padded:
            self._dest.write(self._cipher.update(padded))

    def close(self) -> None:
        """Close the encrypt writer."""
        if self._padder and self._cipher:
            final_padding = self._padder.finalize()
            self._dest.write(self._cipher.update(final_padding))
        # Set to None to help catch any use after close bugs
        self._padder = None  # type: ignore[assignment]
        self._cipher = None  # type: ignore[assignment]


class _AesCbcEncryptReader(EncryptReader):
    """AES-CBC encryption reader (for streaming encryption).

    AES-CBC is used in SecureTar v1 and v2.
    """

    def __init__(
        self,
        source: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
        plaintext_size: int,
    ) -> None:
        """Initialize."""
        super().__init__(source, key_material, plaintext_size)
        self._ciphertext_size = (
            plaintext_size + AES_BLOCK_SIZE - (plaintext_size % AES_BLOCK_SIZE)
        )
        self._pos = 0

        self._cipher = Cipher(
            algorithms.AES(key_material.key),
            modes.CBC(key_material.iv),
            backend=default_backend(),
        ).encryptor()
        self._padder = padding.PKCS7(AES_BLOCK_SIZE_BITS).padder()

    def _fill_buffer(self, size: int) -> None:
        """Fill buffer with encrypted data."""
        while len(self._buffer) < size and not self._done:
            remaining = self._plaintext_size - self._pos
            to_read = min(max(size, DEFAULT_BUFSIZE), remaining) if remaining > 0 else 0

            if to_read > 0:
                plaintext = self._source.read(to_read)
                self._pos += len(plaintext)
                padded = self._padder.update(plaintext)
                if padded:
                    self._buffer += self._cipher.update(padded)

            if self._pos >= self._plaintext_size:
                # Finalize
                final_padding = self._padder.finalize()
                self._buffer += self._cipher.update(final_padding)
                self._done = True

    def close(self) -> None:
        """Close the encrypt reader."""
        # Set to None to help catch any use after close bugs
        self._cipher = None  # type: ignore[assignment]
        self._padder = None  # type: ignore[assignment]


class AesCbcStreamFactory(CipherStreamFactory):
    """Factory for AES-CBC streams (v1/v2)."""

    create_decrypt_reader = _AesCbcDecryptReader
    create_encrypt_writer = _AesCbcEncryptWriter
    create_encrypt_reader = _AesCbcEncryptReader


class _SecretStreamDecryptReader(DecryptReader):
    """XChaCha20-Poly1305 secretstream decryption reader.

    XChaCha20-Poly1305 is used in SecureTar v3.
    """

    _ciphertext_size: int

    def __init__(
        self,
        source: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
        ciphertext_size: int | None = None,
        plaintext_size: int | None = None,
    ) -> None:
        """Initialize."""
        if plaintext_size is None:
            raise ValueError("Plaintext size is required")

        # Calculate number of chunks using integer ceiling division to avoid
        # rounding issues, and ensure at least 1 chunk for empty plaintext
        num_chunks = (
            plaintext_size + V3_SECRETSTREAM_CHUNK_SIZE - 1
        ) // V3_SECRETSTREAM_CHUNK_SIZE
        if num_chunks == 0:
            num_chunks = 1
        # Calculate ciphertext size based on plaintext size which is always
        # known for v3
        ciphertext_size = plaintext_size + num_chunks * V3_SECRETSTREAM_ABYTES

        super().__init__(source, key_material, ciphertext_size, plaintext_size)
        self._pos = 0

        # Initialize from header stored in cipher_initialization
        self._state = nss.crypto_secretstream_xchacha20poly1305_state()
        # For v3, iv contains the secretstream header
        nss.crypto_secretstream_xchacha20poly1305_init_pull(
            self._state, key_material.iv, key_material.key
        )

    def _fill_buffer(self, size: int) -> None:
        """Fill buffer with decrypted data."""
        while len(self._buffer) < size and not self._done:
            chunk_size = V3_SECRETSTREAM_CHUNK_SIZE + V3_SECRETSTREAM_ABYTES

            # Check bounds
            remaining = self._ciphertext_size - self._pos
            chunk_size = min(chunk_size, max(remaining, 0))

            encrypted = self._source.read(chunk_size)

            self._pos += len(encrypted)
            plaintext, tag = nss.crypto_secretstream_xchacha20poly1305_pull(
                self._state, encrypted
            )
            self._buffer += plaintext

            remaining = self._ciphertext_size - self._pos
            if tag == NSS_TAG_FINAL and remaining != 0:
                raise SecureTarReadError(
                    "Unexpected final tag in secretstream decryption"
                )
            if remaining == 0 and tag != NSS_TAG_FINAL:
                raise SecureTarReadError("Missing final tag in secretstream decryption")

            if tag == NSS_TAG_FINAL:
                self._done = True

    def close(self) -> None:
        """Close the decrypt reader."""
        # Set to None to help catch any use after close bugs
        self._state = None  # type: ignore[assignment]


class _SecretStreamEncryptWriter(EncryptWriter):
    """XChaCha20-Poly1305 secretstream encryption writer.

    XChaCha20-Poly1305 is used in SecureTar v3.
    """

    def __init__(
        self,
        dest: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
    ) -> None:
        """Initialize."""
        super().__init__(dest, key_material)
        self._buffer = b""

        self._state = nss.crypto_secretstream_xchacha20poly1305_state()
        # We use pull init here since the header is already prepared
        nss.crypto_secretstream_xchacha20poly1305_init_pull(
            self._state, key_material.iv, key_material.key
        )

    def _write(self, data: bytes) -> None:
        """Write plaintext data to be encrypted."""
        self._buffer += data

        while len(self._buffer) > V3_SECRETSTREAM_CHUNK_SIZE:
            chunk = self._buffer[:V3_SECRETSTREAM_CHUNK_SIZE]
            self._buffer = self._buffer[V3_SECRETSTREAM_CHUNK_SIZE:]
            encrypted = nss.crypto_secretstream_xchacha20poly1305_push(
                self._state, chunk, None, NSS_TAG_MESSAGE
            )
            self._dest.write(encrypted)

    def close(self) -> None:
        """Close the encrypt writer."""
        if self._state and self._buffer is not None:
            # Write final chunk
            encrypted = nss.crypto_secretstream_xchacha20poly1305_push(
                self._state, self._buffer, None, NSS_TAG_FINAL
            )
            self._dest.write(encrypted)
        # Set to None to help catch any use after close bugs
        self._buffer = None  # type: ignore[assignment]
        self._state = None  # type: ignore[assignment]


class _SecretStreamEncryptReader(EncryptReader):
    """XChaCha20-Poly1305 secretstream encryption reader.

    XChaCha20-Poly1305 is used in SecureTar v3.
    """

    def __init__(
        self,
        source: IO[bytes],
        key_material: SecureTarDerivedKeyMaterial,
        plaintext_size: int,
    ) -> None:
        """Initialize."""
        super().__init__(source, key_material, plaintext_size)
        # Calculate number of chunks using integer ceiling division to avoid
        # rounding issues, and ensure at least 1 chunk for empty plaintext
        num_chunks = (
            plaintext_size + V3_SECRETSTREAM_CHUNK_SIZE - 1
        ) // V3_SECRETSTREAM_CHUNK_SIZE
        if num_chunks == 0:
            num_chunks = 1
        # Calculate ciphertext size
        self._ciphertext_size = plaintext_size + num_chunks * V3_SECRETSTREAM_ABYTES

        self._pos = 0

        self._state = nss.crypto_secretstream_xchacha20poly1305_state()
        # We use pull init here since the header is already prepared
        nss.crypto_secretstream_xchacha20poly1305_init_pull(
            self._state, key_material.iv, key_material.key
        )

    def _fill_buffer(self, size: int) -> None:
        """Fill buffer with encrypted data."""
        while len(self._buffer) < size and not self._done:
            remaining = self._plaintext_size - self._pos
            to_read = min(V3_SECRETSTREAM_CHUNK_SIZE, remaining)

            plaintext = self._source.read(to_read)
            self._pos += len(plaintext)

            is_final = self._pos >= self._plaintext_size
            tag = NSS_TAG_FINAL if is_final else NSS_TAG_MESSAGE

            encrypted = nss.crypto_secretstream_xchacha20poly1305_push(
                self._state, plaintext, None, tag
            )
            self._buffer += encrypted

            if is_final:
                self._done = True

    def close(self) -> None:
        """Close the encrypt reader."""
        # Set to None to help catch any use after close bugs
        self._state = None  # type: ignore[assignment]


class SecretStreamFactory(CipherStreamFactory):
    """Factory for XChaCha20-Poly1305 secretstream (v3)."""

    create_decrypt_reader = _SecretStreamDecryptReader
    create_encrypt_writer = _SecretStreamEncryptWriter
    create_encrypt_reader = _SecretStreamEncryptReader


class SecureTarDecryptStream:
    """Decrypts an encrypted tar read from a stream."""

    def __init__(
        self,
        source: IO[bytes],
        *,
        ciphertext_size: int | None = None,
        root_key_context: SecureTarRootKeyContext,
    ) -> None:
        """Initialize."""
        self._source = source
        self._root_key_context = root_key_context
        self._ciphertext_size = ciphertext_size
        self._stream: DecryptReader | None = None
        self._header: SecureTarHeader | None = None

    def __enter__(self) -> DecryptReader:
        self._header = SecureTarHeader.from_bytes(self._source)
        key_material = self._root_key_context.restore_key_material(self._header)

        ciphertext_size = None
        if self._ciphertext_size is not None:
            ciphertext_size = self._ciphertext_size - self._header.size
        self._stream = self._root_key_context.stream_factory.create_decrypt_reader(
            self._source,
            key_material,
            ciphertext_size,
            self._header.plaintext_size,
        )
        return self._stream

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> None:
        if self._stream:
            self._stream.close()
            self._stream = None

    def validate(self, *, basic_validation: bool) -> bool:
        """Validate the stream.

        This will fail if the password is invalid or data is corrupted.
        If the securetar version is 3, it will also validate that the stream
        is not truncated.

        If basic_validation is True, only the beginning of the stream is validated
        to check if it looks like a tar/gzip.

        Note: This consumes the stream. Create a new instance to read data.

        Returns:
            True if password is valid, False otherwise.
        """
        chunk_size = 1 if basic_validation else 1024 * 1024
        try:
            with self as stream:
                while stream.read(chunk_size):
                    if basic_validation:
                        return True
                    pass
        except (InvalidPasswordError, SecureTarReadError, CryptoError):
            return False
        return True


class _FramedEncryptReader:
    """Wraps an encrypt reader with a SecureTar header."""

    def __init__(self, inner: EncryptReader, header: SecureTarHeader) -> None:
        """Initialize."""
        self._inner = inner
        self._header = header
        self._header_bytes: bytes | None = header.to_bytes()
        self._ciphertext_size = header.size + inner.ciphertext_size

    @property
    def ciphertext_size(self) -> int:
        """Return the size of the ciphertext data."""
        return self._ciphertext_size

    def read(self, size: int = 0) -> bytes:
        """Read data from buffer, filling as needed.

        Will first return header bytes, then data from inner stream.
        """
        data = b""

        if self._header_bytes:
            data = self._header_bytes[:size]
            self._header_bytes = (
                self._header_bytes[size:] if len(self._header_bytes) > size else None
            )
            size -= len(data)
            if size == 0:
                return data

        data += self._inner.read(size)
        return data

    def close(self) -> None:
        """Close the stream."""
        self._inner.close()


class SecureTarEncryptStream:
    """Encrypt a plaintext tar read from a stream."""

    def __init__(
        self,
        source: IO[bytes],
        *,
        create_version: int,
        derived_key_id: Hashable | None,
        plaintext_size: int,
        root_key_context: SecureTarRootKeyContext,
    ) -> None:
        """Initialize."""
        self._source = source
        self._create_version = create_version
        self._derived_key_id = derived_key_id
        self._plaintext_size = plaintext_size
        self._root_key_context = root_key_context
        self._stream: _FramedEncryptReader | None = None

    def __enter__(self) -> _FramedEncryptReader:
        key_material = self._root_key_context.derive_key_material(
            self._derived_key_id, self._create_version
        )

        factory = self._root_key_context.stream_factory
        inner_stream = factory.create_encrypt_reader(
            self._source,
            key_material,
            self._plaintext_size,
        )

        header = SecureTarHeader(
            key_material.cipher_initialization,
            self._plaintext_size,
            self._create_version,
        )

        self._stream = _FramedEncryptReader(inner_stream, header)
        return self._stream

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> None:
        if self._stream:
            self._stream.close()
            self._stream = None


class SecureTarFile:
    """Handle tar files, optionally wrapped in an encryption layer."""

    _mode: str = "r"

    def __init__(
        self,
        name: Path | None = None,
        *,
        bufsize: int = DEFAULT_BUFSIZE,
        create_version: int | None = None,
        derived_key_id: Hashable | None = None,
        fileobj: IO[bytes] | None = None,
        gzip: bool = True,
        password: str | None = None,
        root_key_context: SecureTarRootKeyContext | None = None,
    ) -> None:
        """Initialize encryption handler.

        Args:
            name: Path to the tar file
            bufsize: Buffer size for tarfile I/O operations, passed on to tarfile.TarFile.
            If set to 0, the default buffer size will be used (10240).
            create_version: SecureTar version to create (2 or 3). If None, defaults to 3
            derived_key_id: Optional derived key ID for deriving key material. Mutually
            exclusive with password.
            fileobj: File object to use instead of opening a file
            gzip: Whether to use gzip compression
            password: Password to derive encryption key from. Mutually exclusive with
            root_key_context and derived_key_id.
            root_key_context: Root key context to use for deriving key material. Mutually
            exclusive with password.
        """
        if self._mode == MOD_READ:
            if create_version is not None:
                raise ValueError("Version must be None when reading a SecureTar file")
        elif create_version is None:
            create_version = DEFAULT_CIPHER_VERSION
        elif create_version not in (2, 3):
            raise ValueError(f"Unsupported SecureTar version: {create_version}")

        if derived_key_id is not None and root_key_context is None:
            raise ValueError(
                "Cannot specify 'derived_key_id' without 'root_key_context'"
            )
        if root_key_context is not None and password is not None:
            raise ValueError("Cannot specify both 'root_key_context' and 'password'")

        if name is None and fileobj is None:
            raise ValueError("Either filename or fileobj must be provided")

        self._file: IO[bytes] | None = None
        self._name: Path | None = name
        self._bufsize: int = bufsize
        self._extra_tar_args: dict[str, Any] = {}
        self._fileobj = fileobj
        self._create_version = create_version
        self._tar: tarfile.TarFile | None = None
        self._derived_key_id = derived_key_id
        self._cipher_stream: CipherStream | None = None
        self._header: SecureTarHeader | None = None

        # Determine if encrypted
        self._encrypted = password is not None or root_key_context is not None

        # Determine the mode for tarfile.open(), streaming (|) for encrypted because
        # we can't seek in the ciphertext, normal (:) for plain
        if self._encrypted:
            self._tar_mode = f"{self._mode}|"
        else:
            self._tar_mode = f"{self._mode}:"
            if gzip:
                self._extra_tar_args["compresslevel"] = 6
        if gzip:
            self._tar_mode += "gz"

        # Set up key context
        self._root_key_context: SecureTarRootKeyContext | None = None
        if self._encrypted:
            if password:
                self._root_key_context = SecureTarRootKeyContext(password)
            else:
                self._root_key_context = root_key_context

    def __enter__(self) -> tarfile.TarFile:
        """Start context manager tarfile."""
        return self.open()

    def open(self) -> tarfile.TarFile:
        """Open SecureTar file.

        Returns tarfile object, data written to is encrypted if key is provided.
        """
        if not self._encrypted:
            # Plain tar, no encryption
            # Ignore mypy because of typing issues with the mode and extra args
            self._tar = tarfile.open(  # type: ignore[call-overload]
                name=str(self._name),
                mode=self._tar_mode,
                dereference=False,
                bufsize=self._bufsize,
                fileobj=self._fileobj,
                **self._extra_tar_args,
            )
            return self._tar

        # When encrypted, we have root key context
        if TYPE_CHECKING:
            assert self._root_key_context is not None

        # Open underlying file or stream
        self._file = self._open_file()

        # Set up cipher layer
        if self._mode == MOD_READ:
            self._header = SecureTarHeader.from_bytes(self._file)
            key_material = self._root_key_context.restore_key_material(self._header)
            factory = self._root_key_context.stream_factory
            self._cipher_stream = factory.create_decrypt_reader(
                self._file, key_material, plaintext_size=self._header.plaintext_size
            )
        else:
            # _create_version set in constructor if encrypted
            if TYPE_CHECKING:
                assert self._create_version is not None
            key_material = self._root_key_context.derive_key_material(
                self._derived_key_id, self._create_version
            )
            self._header = SecureTarHeader(
                key_material.cipher_initialization,
                0,
                self._create_version,
            )
            self._file.write(self._header.to_bytes())
            factory = self._root_key_context.stream_factory
            self._cipher_stream = factory.create_encrypt_writer(
                self._file, key_material
            )

        # Open tar with cipher as fileobj
        # Ignore mypy because of typing issues with the mode and fileobj
        self._tar = tarfile.open(  # type: ignore[call-overload]
            fileobj=self._cipher_stream,
            mode=self._tar_mode,
            dereference=False,
            bufsize=self._bufsize,
        )
        return self._tar

    def _open_file(self) -> IO[bytes] | BinaryIO:
        if self._fileobj:
            # If we have a fileobj, we don't need to open a file
            return self._fileobj
        else:
            # We check in constructor that name and fileobj are not both None
            if TYPE_CHECKING:
                assert self._name is not None
            read_mode = self._mode.startswith("r")
            if read_mode:
                file_mode: int = os.O_RDONLY
            else:
                raise NotImplementedError(
                    "Writing SecureTarFile to a file path is not supported"
                )

            fd = os.open(self._name, file_mode, 0o666)
            return os.fdopen(fd, "rb" if read_mode else "wb")

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> None:
        self.close()

    def close(self) -> None:
        """Close file."""
        if self._tar:
            self._tar.close()
            self._tar = None
        if self._cipher_stream:
            self._cipher_stream.close()
            self._cipher_stream = None
        if self._file:
            if not self._fileobj:
                self._file.close()
            self._file = None

    def _validate(self, *, basic_validation: bool) -> bool:
        """Validate the data.

        If basic_validation is True, only the beginning of the stream is validated
        to check if it looks like a tar/gzip.
        """
        if not self._encrypted:
            raise SecureTarError("File is not encrypted")

        if self._mode != MOD_READ:
            raise SecureTarError("Can only validate password in read mode")

        if self._tar is not None:
            raise SecureTarError("File is already open")

        if TYPE_CHECKING:
            assert self._root_key_context is not None

        file = self._open_file()
        try:
            return SecureTarDecryptStream(
                file,
                root_key_context=self._root_key_context,
            ).validate(basic_validation=basic_validation)
        finally:
            if not self._fileobj:
                file.close()

    def validate_password(self) -> bool:
        """Validate the password by checking if decrypted data looks like a tar.

        Note: If using fileobj instead of a file path, this consumes the stream
        and a new SecureTarFile instance must be created to read data.

        Returns:
            True if password is valid, False otherwise.

        Raises:
            SecureTarError: If file is not encrypted, not in read mode, or already open.
        """
        return self._validate(basic_validation=True)

    def validate(self) -> bool:
        """Validate the data.

        Note: If using fileobj instead of a file path, this consumes the stream
        and a new SecureTarFile instance must be created to read data.

        Returns:
            True if password is valid, False otherwise.

        Raises:
            SecureTarError: If file is not encrypted, not in read mode, or already open.
        """
        return self._validate(basic_validation=False)

    @property
    def path(self) -> Path | None:
        """Return path object of tarfile."""
        return self._name

    @property
    def size(self) -> float:
        """Return backup size."""
        if not self._name or not self._name.is_file():
            return 0
        return round(self._name.stat().st_size / 1_048_576, 2)  # calc mbyte


class InnerSecureTarFile(SecureTarFile):
    """Handle encrypted files for tarfile library inside another tarfile."""

    _header_length: int
    _header_position: int
    _mode: str = "w"
    _tar_info: tarfile.TarInfo

    def __init__(
        self,
        outer_tar: tarfile.TarFile,
        name: Path,
        *,
        bufsize: int,
        derived_key_id: Hashable | None,
        create_version: int | None,
        gzip: bool,
        root_key_context: SecureTarRootKeyContext | None,
    ) -> None:
        """Initialize inner handler."""
        super().__init__(
            name=name,
            gzip=gzip,
            bufsize=bufsize,
            derived_key_id=derived_key_id,
            # https://github.com/python/typeshed/issues/15365
            fileobj=outer_tar.fileobj,  # type: ignore[arg-type]
            create_version=create_version,
            root_key_context=root_key_context,
        )
        self.outer_tar = outer_tar
        if self.outer_tar.format != tarfile.PAX_FORMAT:
            raise ValueError("Outer tarfile must be in PAX format")

    def __enter__(self) -> tarfile.TarFile:
        """Start context manager tarfile."""
        self._tar_info = tarfile.TarInfo(name=str(self._name))
        # Ensure we always set mtime as a float to force
        # a PAX header to be written.
        #
        # This is necessary to
        # handle large files as TarInfo.tobuf will try to
        # use a shorter ustar header if we do not have at
        # least one float in the tarinfo.
        # https://github.com/python/cpython/blob/53b84e772cac6e4a55cebf908d6bb9c48fe254dc/Lib/tarfile.py#L1066
        self._tar_info.mtime = time.time()

        fileobj = self.outer_tar.fileobj
        if TYPE_CHECKING:
            # tarfile.TarFile.fileobj is incorrectly typed, see
            # https://github.com/python/typeshed/issues/15365
            assert fileobj is not None

        self._header_position = fileobj.tell()

        # Write an empty header for the inner tar file in the outer tar file
        # We'll seek back to this position later to update the header with the correct size
        outer_tar = self.outer_tar
        tar_info_header = self._tar_info.tobuf(
            outer_tar.format, outer_tar.encoding, outer_tar.errors
        )
        self._header_length = len(tar_info_header)
        fileobj.write(tar_info_header)

        return super().__enter__()

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> None:
        """Close file."""
        # This class is only used for writing inner tar files, so we know
        # that if _cipher_stream is set, it is an EncryptWriter.
        if TYPE_CHECKING:
            assert isinstance(self._cipher_stream, (EncryptWriter, NoneType))
        # Capture encrypt_writer before super().__exit__ sets it to None
        encrypt_writer: EncryptWriter | None = self._cipher_stream
        super().__exit__(exc_type, exc_value, traceback)
        self._finalize_tar_entry(encrypt_writer)

    def _finalize_tar_entry(self, encrypt_writer: EncryptWriter | None) -> None:
        """Update tar header and securetar header with final sizes."""
        outer_tar = self.outer_tar
        fileobj = self.outer_tar.fileobj
        if TYPE_CHECKING:
            # tarfile.TarFile.fileobj is incorrectly typed, see
            # https://github.com/python/typeshed/issues/15365
            assert fileobj is not None

        end_position = fileobj.tell()

        size_of_inner_tar = end_position - self._header_position - self._header_length

        # Pad to tar block boundary
        blocks, remainder = divmod(size_of_inner_tar, tarfile.BLOCKSIZE)
        padding_size = 0
        if remainder > 0:
            padding_size = tarfile.BLOCKSIZE - remainder
            fileobj.write(tarfile.NUL * padding_size)
        outer_tar.offset += size_of_inner_tar + padding_size

        # Update securetar header with plaintext size if encrypted
        if self._encrypted and self._header and encrypt_writer:
            self._header.plaintext_size = encrypt_writer.plaintext_size
            fileobj.seek(self._header_position + self._header_length)
            fileobj.write(self._header.to_bytes())

        # Now that we know the size of the inner tar, we seek back
        # to where we started and re-write the header with the correct size
        self._tar_info.size = size_of_inner_tar
        fileobj.seek(self._header_position)

        # We can't call tar.addfile here because it doesn't allow a non-zero
        # size to be set without passing a fileobj. Instead we manually write
        # the header. https://github.com/python/cpython/pull/117988
        buf = self._tar_info.tobuf(
            outer_tar.format, outer_tar.encoding, outer_tar.errors
        )
        fileobj.write(buf)
        outer_tar.offset += len(buf)
        # Tarfile.members is not specified in the type stubs and we don't
        # want to use the getter Tarfile.getmembers().
        outer_tar.members.append(self._tar_info)  # type: ignore[attr-defined]

        # Finally return to the end of the outer tar file
        fileobj.seek(end_position + padding_size)


class SecureTarArchive:
    """Manage a plain tar archive containing encrypted inner tar files."""

    def __init__(
        self,
        name: Path | None = None,
        mode: Literal["r", "w"] = "r",
        *,
        bufsize: int = DEFAULT_BUFSIZE,
        create_version: int | None = None,
        fileobj: IO[bytes] | None = None,
        password: str | None = None,
        root_key_context: SecureTarRootKeyContext | None = None,
        streaming: bool = False,
    ) -> None:
        """Initialize archive handler.

        Args:
            name: Path to the tar file
            mode: File mode ('r' for read, 'w' for write, 'x' for exclusive create)
            bufsize: Buffer size for tarfile I/O operations, passed on to tarfile.TarFile.
            If set to 0, the default buffer size will be used (10240).
            create_version: SecureTar version to create (2 or 3). If None, defaults to 3
            fileobj: File object to use instead of opening a file
            password: Password for encryption/decryption of inner tar files. Mutually
            exclusive with root_key_context.
            root_key_context: Root key context to use for deriving key material. Mutually
            exclusive with password.
            streaming: Whether to use streaming mode for tarfile (no seeking)
        """
        if mode == MOD_READ:
            if create_version is not None:
                raise ValueError("Version must be None when reading a SecureTar file")
        elif create_version is None:
            create_version = DEFAULT_CIPHER_VERSION
        elif create_version not in (2, 3):
            raise ValueError(f"Unsupported SecureTar version: {create_version}")
        if root_key_context is not None and password is not None:
            raise ValueError("Cannot specify both 'root_key_context' and 'password'")
        if name is None and fileobj is None:
            raise ValueError("Either name or fileobj must be provided")

        if mode not in (MOD_EXCLUSIVE, MOD_READ, MOD_WRITE):
            raise ValueError(
                f"Mode must be '{MOD_EXCLUSIVE}', '{MOD_READ}', or '{MOD_WRITE}'"
            )

        self._create_version = create_version
        self._name = name
        self._mode = mode
        self._bufsize = bufsize
        self._fileobj = fileobj
        self._streaming = streaming
        self._tar: tarfile.TarFile | None = None

        # Set up key context
        self._root_key_context = root_key_context
        if password:
            self._root_key_context = SecureTarRootKeyContext(password)

    def __enter__(self) -> SecureTarArchive:
        """Open the archive."""
        return self.open()

    def open(self) -> SecureTarArchive:
        """Open the archive."""
        mode = f"{self._mode}{'|' if self._streaming else ''}"
        # Ignore mypy because of typing issues with the mode and fileobj
        self._tar = tarfile.open(  # type: ignore[call-overload]
            name=str(self._name) if self._name else None,
            mode=mode,
            fileobj=self._fileobj,
            bufsize=self._bufsize,
        )
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> None:
        """Close the archive."""
        self.close()

    def close(self) -> None:
        """Close the archive."""
        if self._tar:
            self._tar.close()
            self._tar = None

    @property
    def tar(self) -> tarfile.TarFile:
        """Return the underlying tar file."""
        if not self._tar:
            raise SecureTarError("Archive not open")
        return self._tar

    def create_tar(
        self,
        name: str,
        *,
        derived_key_id: Hashable | None = None,
        gzip: bool = True,
    ) -> InnerSecureTarFile:
        """Create a new tar file within the archive.

        Returns a context manager that yields a TarFile for adding files.
        The tar file will be encrypted if a password was provided to the archive.

        Args:
            name: Name of the inner tar file in the archive
            derived_key_id: Optional derived key ID for deriving key material.
            gzip: Whether to use gzip compression
        """
        if not self._tar:
            raise SecureTarError("Archive not open")
        if self._mode == MOD_READ:
            raise SecureTarError("Archive not open for writing")
        if self._streaming:
            raise SecureTarError("create_tar not supported in streaming mode")
        if derived_key_id is not None and self._root_key_context is None:
            raise ValueError(
                "Cannot specify 'derived_key_id' when encryption is disabled"
            )

        return InnerSecureTarFile(
            self._tar,
            bufsize=self._bufsize,
            gzip=gzip,
            name=Path(name),
            create_version=self._create_version,
            derived_key_id=derived_key_id,
            root_key_context=self._root_key_context,
        )

    def extract_tar(
        self,
        member: tarfile.TarInfo,
    ) -> SecureTarDecryptStream:
        """Extract and decrypt a tar file from the archive.

        Returns a context manager that yields a readable stream of decrypted bytes.

        Args:
            member: TarInfo of the tar file to extract
        """
        if not self._tar:
            raise SecureTarError("Archive not open")

        if self._mode != MOD_READ:
            raise SecureTarError("Archive not open for reading")

        if not self._root_key_context:
            raise SecureTarError("No password provided")

        fileobj = self._tar.extractfile(member)
        if fileobj is None:
            raise SecureTarError(f"Cannot extract {member.name}")

        return SecureTarDecryptStream(
            fileobj,
            ciphertext_size=member.size,
            root_key_context=self._root_key_context,
        )

    def import_tar(
        self,
        source: IO[bytes],
        member: tarfile.TarInfo,
        *,
        derived_key_id: Hashable | None = None,
    ) -> None:
        """Import an existing tar into the archive, encrypting it.

        The member.size must be set to the size of the source stream.

        Args:
            source: Source tar stream to encrypt and add
            member: TarInfo for the tar file (size must be set)
            derived_key_id: Optional derived key ID for deriving key material.
        """
        if not self._tar:
            raise SecureTarError("Archive not open")

        if self._mode == MOD_READ:
            raise SecureTarError("Archive not open for writing")

        if not self._root_key_context:
            raise SecureTarError("No password provided")

        if TYPE_CHECKING:
            # create_version is set in constructor if in write mode
            assert self._create_version is not None

        with SecureTarEncryptStream(
            source,
            create_version=self._create_version,
            derived_key_id=derived_key_id,
            plaintext_size=member.size,
            root_key_context=self._root_key_context,
        ) as encrypted:
            encrypted_tar_info = copy.deepcopy(member)
            encrypted_tar_info.size = encrypted.ciphertext_size
            self._tar.addfile(encrypted_tar_info, encrypted)

    def _validate(self, member: tarfile.TarInfo, *, basic_validation: bool) -> bool:
        """Validate an encrypted inner tar."""
        if not self._tar:
            raise SecureTarError("Archive not open")

        if self._mode != MOD_READ:
            raise SecureTarError("Archive not open for reading")

        if not self._root_key_context:
            raise SecureTarError("No password provided")

        return self.extract_tar(member).validate(basic_validation=basic_validation)

    def validate_password(self, member: tarfile.TarInfo) -> bool:
        """Validate the password against an encrypted inner tar.

        Note: This consumes the stream. Create a new instance to read data.

        Args:
            member: TarInfo of an encrypted tar file to validate against
        """
        return self._validate(member, basic_validation=True)

    def validate(self, member: tarfile.TarInfo) -> bool:
        """Validate an encrypted inner tar.

        Note: This consumes the stream. Create a new instance to read data.

        Args:
            member: TarInfo of an encrypted tar file to validate against
        """
        return self._validate(member, basic_validation=False)


class KeyDerivationStrategy(ABC):
    """Abstract base for version-specific key derivation."""

    stream_factory: type[CipherStreamFactory]
    """The cipher stream factory for this version."""

    @abstractmethod
    def get_key_material(
        self, cipher_initialization: bytes | None = None
    ) -> SecureTarDerivedKeyMaterial:
        """Get key material, deriving new or restoring from cipher_initialization."""


class KeyDerivationV2(KeyDerivationStrategy):
    """Key derivation for SecureTar v1 and v2."""

    stream_factory = AesCbcStreamFactory

    def __init__(self, password: str) -> None:
        """Initialize."""
        self._root_key = self._password_to_key(password)

    @staticmethod
    def _password_to_key(password: str) -> bytes:
        """Generate AES key from password using 100 rounds of SHA256."""
        key: bytes = password.encode()
        for _ in range(100):
            key = hashlib.sha256(key).digest()
        return key[:16]

    def get_key_material(
        self, cipher_initialization: bytes | None = None
    ) -> SecureTarDerivedKeyMaterialV2:
        """Get key material, deriving new or restoring from cipher_initialization."""
        salt = (
            cipher_initialization
            if cipher_initialization is not None
            else nacl_random(AES_IV_SIZE)
        )
        return SecureTarDerivedKeyMaterialV2(root_key=self._root_key, salt=salt)


class KeyDerivationV3(KeyDerivationStrategy):
    """Key derivation for SecureTar v3."""

    stream_factory = SecretStreamFactory

    def __init__(
        self,
        root_key: bytes,
        root_salt: bytes,
        validation_salt: bytes,
        validation_key: bytes,
    ) -> None:
        """Initialize."""
        self._root_key = root_key
        self._root_salt = root_salt
        self._validation_salt = validation_salt
        self._validation_key = validation_key

    @classmethod
    def create(cls, password: str) -> KeyDerivationV3:
        """Create strategy for new files."""
        root_salt = nacl_random(ARGON2_SALT_SIZE)
        validation_salt = nacl_random(V3_DERIVED_KEY_SALT_SIZE)
        root_key, validation_key = cls._derive_keys(
            password, root_salt, validation_salt
        )
        return cls(root_key, root_salt, validation_salt, validation_key)

    @classmethod
    def from_header(cls, password: str, header: SecureTarHeader) -> KeyDerivationV3:
        """Create strategy from existing header."""
        root_salt, validation_salt, stored_validation_key, _, _ = struct.unpack(
            SECURETAR_V3_CIPHER_INIT_FORMAT, header.cipher_initialization
        )

        root_key, validation_key = cls._derive_keys(
            password, root_salt, validation_salt
        )

        # Use hmac.compare_digest to prevent timing attacks. Note that the
        # validation key is stored in plaintext in the header, so this is
        # primarily to avoid false positives from code analysis tools.
        if not hmac.compare_digest(validation_key, stored_validation_key):
            raise InvalidPasswordError("Invalid password")

        return cls(root_key, root_salt, validation_salt, validation_key)

    @staticmethod
    def _derive_keys(
        password: str, root_salt: bytes, validation_salt: bytes
    ) -> tuple[bytes, bytes]:
        """Derive root key and validation key from password and salts."""
        root_key = kdf(
            nss.crypto_secretstream_xchacha20poly1305_KEYBYTES,
            password.encode(),
            root_salt,
            opslimit=V3_KDF_OPSLIMIT,
            memlimit=V3_KDF_MEMLIMIT,
        )
        validation_key = blake2b(
            b"",
            key=root_key,
            salt=validation_salt,
            person=b"SecureTarv3",
            encoder=nacl.encoding.RawEncoder,
        )
        return root_key, validation_key

    def get_key_material(
        self, cipher_initialization: bytes | None = None
    ) -> SecureTarDerivedKeyMaterialV3:
        """Get key material, deriving new or restoring from cipher_initialization."""
        if cipher_initialization is not None:
            _, _, _, derivation_salt, secretstream_header = struct.unpack(
                SECURETAR_V3_CIPHER_INIT_FORMAT, cipher_initialization
            )
        else:
            derivation_salt = nacl_random(V3_DERIVED_KEY_SALT_SIZE)
            secretstream_header = None  # Will be generated

        return SecureTarDerivedKeyMaterialV3(
            root_key=self._root_key,
            root_salt=self._root_salt,
            validation_salt=self._validation_salt,
            validation_key=self._validation_key,
            derivation_salt=derivation_salt,
            secretstream_header=secretstream_header,
        )


class SecureTarDerivedKeyMaterial(ABC):
    """Abstract base for derived key material."""

    key: bytes
    """The derived encryption key."""

    iv: bytes
    """The initialization vector."""

    @property
    @abstractmethod
    def cipher_initialization(self) -> bytes:
        """Return cipher initialization bytes for the header."""


class SecureTarDerivedKeyMaterialV2(SecureTarDerivedKeyMaterial):
    """Key material for SecureTar v1 and v2."""

    def __init__(self, root_key: bytes, salt: bytes) -> None:
        """Initialize."""
        self.key = root_key
        self._salt = salt
        self.iv = self._generate_iv(root_key, salt)

    @staticmethod
    def _generate_iv(key: bytes, salt: bytes) -> bytes:
        """Generate an IV from key and salt."""
        temp_iv = key + salt
        for _ in range(100):
            temp_iv = hashlib.sha256(temp_iv).digest()
        return temp_iv[:AES_IV_SIZE]

    @property
    def cipher_initialization(self) -> bytes:
        """Return cipher initialization bytes for the header."""
        return self._salt


class SecureTarDerivedKeyMaterialV3(SecureTarDerivedKeyMaterial):
    """Key material for SecureTar v3."""

    def __init__(
        self,
        root_key: bytes,
        root_salt: bytes,
        validation_salt: bytes,
        validation_key: bytes,
        derivation_salt: bytes,
        secretstream_header: bytes | None = None,
    ) -> None:
        """Initialize."""
        self._root_salt = root_salt
        self._validation_salt = validation_salt
        self._validation_key = validation_key
        self._derivation_salt = derivation_salt

        self.key = blake2b(
            b"",
            key=root_key,
            salt=derivation_salt,
            person=b"SecureTarv3",
            encoder=nacl.encoding.RawEncoder,
        )

        if secretstream_header is not None:
            self.iv = secretstream_header
        else:
            # Generate new header
            self.iv = nss.crypto_secretstream_xchacha20poly1305_init_push(
                nss.crypto_secretstream_xchacha20poly1305_state(), self.key
            )

    @property
    def cipher_initialization(self) -> bytes:
        """Return cipher initialization bytes for the header."""
        return (
            self._root_salt
            + self._validation_salt
            + self._validation_key
            + self._derivation_salt
            + self.iv
        )


class SecureTarRootKeyContext:
    """Handle cipher contexts for multiple inner SecureTar files."""

    def __init__(self, password: str) -> None:
        self._password = password
        self._strategy: KeyDerivationStrategy | None = None
        self._version: int | None = None
        self._derived_keys: dict[Hashable, SecureTarDerivedKeyMaterial] = {}

    @property
    def stream_factory(self) -> type[CipherStreamFactory]:
        """Return the cipher stream factory."""
        if self._strategy is None:
            raise SecureTarError("Context not initialized")
        return self._strategy.stream_factory

    def _ensure_strategy(
        self, version: int, header: SecureTarHeader | None = None
    ) -> KeyDerivationStrategy:
        """Ensure strategy is initialized for the given version."""
        if self._strategy is not None:
            if self._version != version:
                raise SecureTarError(
                    f"Context already initialized for version {self._version}, "
                    f"cannot use for version {version}"
                )
            return self._strategy

        if version in (1, 2):
            self._strategy = KeyDerivationV2(self._password)
        elif version == 3:
            if header is not None:
                self._strategy = KeyDerivationV3.from_header(self._password, header)
            else:
                self._strategy = KeyDerivationV3.create(self._password)
        else:
            raise ValueError(f"Unsupported SecureTar version: {version}")

        self._version = version
        return self._strategy

    def derive_key_material(
        self, key_id: Hashable | None, version: int
    ) -> SecureTarDerivedKeyMaterial:
        """Derive per-entry key material from the root key."""
        strategy = self._ensure_strategy(version)
        if key_id is None:
            return strategy.get_key_material()
        if key_id not in self._derived_keys:
            self._derived_keys[key_id] = strategy.get_key_material()
        return self._derived_keys[key_id]

    def restore_key_material(
        self, header: SecureTarHeader
    ) -> SecureTarDerivedKeyMaterial:
        """Reconstruct key material from existing header fields."""
        strategy = self._ensure_strategy(header.version, header)
        return strategy.get_key_material(header.cipher_initialization)


def secure_path(tar: tarfile.TarFile) -> Generator[tarfile.TarInfo, None, None]:
    """Security safe check of path.
    Prevent ../ or absolut paths
    """
    for member in tar:
        file_path = Path(member.name)
        try:
            if file_path.is_absolute():
                raise ValueError()
            Path("/fake", file_path).resolve().relative_to("/fake")
        except (ValueError, RuntimeError):
            _LOGGER.warning("Found issue with file %s", file_path)
            continue
        else:
            yield member


def atomic_contents_add(
    tar_file: tarfile.TarFile,
    origin_path: Path,
    file_filter: Callable[[PurePath], bool],
    arcname: str = ".",
) -> None:
    """Append directories and/or files to the TarFile if file_filter returns False.

    :param file_filter: A filter function, should return True if the item should
    be excluded from the archive. The function should take a single argument, a
    pathlib.PurePath object representing the relative path of the item to be archived.
    """

    if file_filter(PurePath(arcname)):
        return None
    return _atomic_contents_add(tar_file, origin_path, file_filter, arcname)


def _atomic_contents_add(
    tar_file: tarfile.TarFile,
    origin_path: Path,
    file_filter: Callable[[PurePath], bool],
    arcname: str,
) -> None:
    """Append directories and/or files to the TarFile if file_filter returns False."""

    # Add directory only (recursive=False) to ensure we also archive empty directories
    try:
        tar_file.add(origin_path.as_posix(), arcname=arcname, recursive=False)
    except (OSError, tarfile.TarError) as err:
        raise AddFileError(
            origin_path,
            f"Error adding {origin_path} to tarfile: {err} ({err.__class__.__name__})",
        ) from err

    try:
        dir_iterator = origin_path.iterdir()
    except OSError as err:
        raise AddFileError(
            origin_path,
            f"Error iterating over {origin_path}: {err} ({err.__class__.__name__})",
        ) from err

    for directory_item in dir_iterator:
        try:
            item_arcpath = PurePath(arcname, directory_item.name)
            if file_filter(PurePath(item_arcpath)):
                continue

            item_arcname = item_arcpath.as_posix()
            if directory_item.is_dir() and not directory_item.is_symlink():
                _atomic_contents_add(
                    tar_file, directory_item, file_filter, item_arcname
                )
                continue

            tar_file.add(
                directory_item.as_posix(), arcname=item_arcname, recursive=False
            )
        except (OSError, tarfile.TarError) as err:
            raise AddFileError(
                directory_item,
                (
                    f"Error adding {directory_item} to tarfile: "
                    f"{err} ({err.__class__.__name__})"
                ),
            ) from err

    return None