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