team-ljyzipelo

공개
104줄4.5 KB
1"""공개 시험 암호로 독립 AES 표본 생성과 검증을 수행함."""
2
3import argparse
4import hashlib
5import hmac
6import io
7from pathlib import Path
8import struct
9import subprocess
10import tempfile
11import zipfile
12import zlib
13
14ROOT = Path(__file__).resolve().parent
15PASSWORD = b"fixture-password"
16PAYLOAD = b"Zipelo encrypted interoperability\n" * 8
17
18
19def crypt(data, key):
20 counters = b"".join(i.to_bytes(16, "little") for i in range(1, (len(data) + 15) // 16 + 1))
21 if not counters:
22 return b""
23 mask = subprocess.run(["openssl", "enc", "-aes-256-ecb", "-nopad", "-K", key.hex()],
24 input=counters, check=True, stdout=subprocess.PIPE).stdout
25 return bytes(a ^ b for a, b in zip(data, mask))
26
27
28def aes_zip(version, method):
29 data = PAYLOAD
30 packed = data if method == 0 else zlib.compress(data, wbits=-15)
31 salt = bytes(range(16))
32 keys = hashlib.pbkdf2_hmac("sha1", PASSWORD, salt, 1000, 66)
33 cipher = crypt(packed, keys[:32])
34 encrypted = salt + keys[64:] + cipher + hmac.digest(keys[32:64], cipher, "sha1")[:10]
35 name = b"payload.bin"
36 crc = zlib.crc32(data) if version == 1 else 0
37 extra = struct.pack("<HHH2sBH", 0x9901, 7, version, b"AE", 3, method)
38 local = struct.pack("<IHHHHHIIIHH", 0x04034B50, 51, 0x801, 99, 0, 33,
39 crc, len(encrypted), len(data), len(name), len(extra)) + name + extra + encrypted
40 central = struct.pack("<IHHHHHHIIIHHHHHII", 0x02014B50, 0x333, 51, 0x801, 99, 0, 33,
41 crc, len(encrypted), len(data), len(name), len(extra), 0, 0, 0,
42 0o100600 << 16, 0) + name + extra
43 return local + central + struct.pack("<IHHHHIIH", 0x06054B50, 0, 0, 1, 1,
44 len(central), len(local), 0)
45
46
47def verify(path):
48 raw = Path(path).read_bytes()
49 with zipfile.ZipFile(io.BytesIO(raw)) as archive:
50 assert archive.infolist()
51 for info in archive.infolist():
52 assert info.compress_type == 99 and info.flag_bits & 1
53 fields = struct.unpack_from("<HHHHHIIIHH", raw, info.header_offset + 4)
54 name_len, extra_len = fields[-2:]
55 extra_at = info.header_offset + 30 + name_len
56 assert raw[extra_at:extra_at + extra_len] == info.extra
57 tag, length, version, vendor, strength, method = struct.unpack("<HHH2sBH", info.extra)
58 assert (tag, length, vendor, strength) == (0x9901, 7, b"AE", 3)
59 assert version in (1, 2)
60 start = extra_at + extra_len
61 encrypted = raw[start:start + info.compress_size]
62 keys = hashlib.pbkdf2_hmac("sha1", PASSWORD, encrypted[:16], 1000, 66)
63 assert hmac.compare_digest(keys[64:], encrypted[16:18])
64 cipher = encrypted[18:-10]
65 assert hmac.compare_digest(hmac.digest(keys[32:64], cipher, "sha1")[:10], encrypted[-10:])
66 packed = crypt(cipher, keys[:32])
67 data = packed if method == 0 else zlib.decompress(packed, wbits=-15)
68 assert len(data) == info.file_size
69 assert info.CRC == (0 if version == 2 else zlib.crc32(data))
70 assert data == PAYLOAD
71
72
73def generate():
74 directory = ROOT / "fixtures"
75 for version in (1, 2):
76 for method, name in ((0, "store"), (8, "deflate")):
77 path = directory / f"aes{version}-{name}.zip"
78 path.write_bytes(aes_zip(version, method))
79 verify(path)
80 runs = ROOT / "runs"
81 runs.mkdir(exist_ok=True)
82 with tempfile.TemporaryDirectory(dir=runs) as scratch:
83 source = Path(scratch) / "payload.bin"
84 source.write_bytes(PAYLOAD)
85 for level, name in (("-0", "store"), ("-9", "deflate")):
86 result = subprocess.run(["zip", "-q", "-X", "-P", PASSWORD.decode(), level, "-", source.name],
87 cwd=scratch, check=True, stdout=subprocess.PIPE).stdout
88 with zipfile.ZipFile(io.BytesIO(result)) as archive:
89 assert archive.read("payload.bin", pwd=PASSWORD) == PAYLOAD
90 assert archive.infolist()[0].flag_bits & 8
91 (directory / f"zipcrypto-{name}.zip").write_bytes(result)
92
93
94if __name__ == "__main__":
95 parser = argparse.ArgumentParser(description=__doc__)
96 parser.add_argument("--generate", action="store_true")
97 parser.add_argument("--verify", type=Path)
98 args = parser.parse_args()
99 if args.generate:
100 generate()
101 elif args.verify:
102 verify(args.verify)
103 else:
104 parser.error("--generate 또는 --verify가 필요함")