]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
a7d9ded4 JMF |
2 | # Allow direct execution |
3 | import os | |
4 | import sys | |
5 | import unittest | |
f8271158 | 6 | |
a7d9ded4 JMF |
7 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
8 | ||
f8271158 | 9 | import base64 |
10 | ||
09906f55 | 11 | from yt_dlp.aes import ( |
f8271158 | 12 | BLOCK_SIZE_BYTES, |
09906f55 ÁS |
13 | aes_cbc_decrypt, |
14 | aes_cbc_decrypt_bytes, | |
15 | aes_cbc_encrypt, | |
16 | aes_ctr_decrypt, | |
17 | aes_ctr_encrypt, | |
f8271158 | 18 | aes_decrypt, |
19 | aes_decrypt_text, | |
20 | aes_ecb_decrypt, | |
21 | aes_ecb_encrypt, | |
22 | aes_encrypt, | |
09906f55 ÁS |
23 | aes_gcm_decrypt_and_verify, |
24 | aes_gcm_decrypt_and_verify_bytes, | |
09906f55 ÁS |
25 | ) |
26 | from yt_dlp.compat import compat_pycrypto_AES | |
7a5c1cfe | 27 | from yt_dlp.utils import bytes_to_intlist, intlist_to_bytes |
a7d9ded4 JMF |
28 | |
29 | # the encrypted data can be generate with 'devscripts/generate_aes_testdata.py' | |
30 | ||
31 | ||
32 | class TestAES(unittest.TestCase): | |
33 | def setUp(self): | |
34 | self.key = self.iv = [0x20, 0x15] + 14 * [0] | |
35 | self.secret_msg = b'Secret message goes here' | |
36 | ||
37 | def test_encrypt(self): | |
38 | msg = b'message' | |
39 | key = list(range(16)) | |
40 | encrypted = aes_encrypt(bytes_to_intlist(msg), key) | |
41 | decrypted = intlist_to_bytes(aes_decrypt(encrypted, key)) | |
42 | self.assertEqual(decrypted, msg) | |
43 | ||
44 | def test_cbc_decrypt(self): | |
09906f55 ÁS |
45 | data = b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\x27\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd' |
46 | decrypted = intlist_to_bytes(aes_cbc_decrypt(bytes_to_intlist(data), self.key, self.iv)) | |
a7d9ded4 | 47 | self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) |
09906f55 ÁS |
48 | if compat_pycrypto_AES: |
49 | decrypted = aes_cbc_decrypt_bytes(data, intlist_to_bytes(self.key), intlist_to_bytes(self.iv)) | |
50 | self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | |
a7d9ded4 | 51 | |
c9619f0a YCH |
52 | def test_cbc_encrypt(self): |
53 | data = bytes_to_intlist(self.secret_msg) | |
54 | encrypted = intlist_to_bytes(aes_cbc_encrypt(data, self.key, self.iv)) | |
55 | self.assertEqual( | |
56 | encrypted, | |
09906f55 ÁS |
57 | b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd') |
58 | ||
59 | def test_ctr_decrypt(self): | |
60 | data = bytes_to_intlist(b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08') | |
61 | decrypted = intlist_to_bytes(aes_ctr_decrypt(data, self.key, self.iv)) | |
62 | self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | |
63 | ||
64 | def test_ctr_encrypt(self): | |
65 | data = bytes_to_intlist(self.secret_msg) | |
66 | encrypted = intlist_to_bytes(aes_ctr_encrypt(data, self.key, self.iv)) | |
67 | self.assertEqual( | |
68 | encrypted, | |
69 | b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08') | |
70 | ||
71 | def test_gcm_decrypt(self): | |
72 | data = b'\x159Y\xcf5eud\x90\x9c\x85&]\x14\x1d\x0f.\x08\xb4T\xe4/\x17\xbd' | |
73 | authentication_tag = b'\xe8&I\x80rI\x07\x9d}YWuU@:e' | |
74 | ||
75 | decrypted = intlist_to_bytes(aes_gcm_decrypt_and_verify( | |
76 | bytes_to_intlist(data), self.key, bytes_to_intlist(authentication_tag), self.iv[:12])) | |
77 | self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | |
78 | if compat_pycrypto_AES: | |
79 | decrypted = aes_gcm_decrypt_and_verify_bytes( | |
80 | data, intlist_to_bytes(self.key), authentication_tag, intlist_to_bytes(self.iv[:12])) | |
81 | self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | |
c9619f0a | 82 | |
a7d9ded4 JMF |
83 | def test_decrypt_text(self): |
84 | password = intlist_to_bytes(self.key).decode('utf-8') | |
85 | encrypted = base64.b64encode( | |
3089bc74 S |
86 | intlist_to_bytes(self.iv[:8]) |
87 | + b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae' | |
4c8fea92 | 88 | ).decode('utf-8') |
a7d9ded4 JMF |
89 | decrypted = (aes_decrypt_text(encrypted, password, 16)) |
90 | self.assertEqual(decrypted, self.secret_msg) | |
91 | ||
f9544f6e JMF |
92 | password = intlist_to_bytes(self.key).decode('utf-8') |
93 | encrypted = base64.b64encode( | |
3089bc74 S |
94 | intlist_to_bytes(self.iv[:8]) |
95 | + b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83' | |
4c8fea92 | 96 | ).decode('utf-8') |
f9544f6e JMF |
97 | decrypted = (aes_decrypt_text(encrypted, password, 32)) |
98 | self.assertEqual(decrypted, self.secret_msg) | |
99 | ||
a04e0055 THD |
100 | def test_ecb_encrypt(self): |
101 | data = bytes_to_intlist(self.secret_msg) | |
102 | data += [0x08] * (BLOCK_SIZE_BYTES - len(data) % BLOCK_SIZE_BYTES) | |
103 | encrypted = intlist_to_bytes(aes_ecb_encrypt(data, self.key, self.iv)) | |
104 | self.assertEqual( | |
105 | encrypted, | |
106 | b'\xaa\x86]\x81\x97>\x02\x92\x9d\x1bR[[L/u\xd3&\xd1(h\xde{\x81\x94\xba\x02\xae\xbd\xa6\xd0:') | |
107 | ||
108 | def test_ecb_decrypt(self): | |
109 | data = bytes_to_intlist(b'\xaa\x86]\x81\x97>\x02\x92\x9d\x1bR[[L/u\xd3&\xd1(h\xde{\x81\x94\xba\x02\xae\xbd\xa6\xd0:') | |
110 | decrypted = intlist_to_bytes(aes_ecb_decrypt(data, self.key, self.iv)) | |
111 | self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | |
112 | ||
582be358 | 113 | |
a7d9ded4 JMF |
114 | if __name__ == '__main__': |
115 | unittest.main() |