2021-06-03 03:43:42 -06:00
|
|
|
#!/usr/bin/env python3
|
2015-03-21 05:07:23 -06:00
|
|
|
# Allow direct execution
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2022-04-11 16:32:57 -06:00
|
|
|
|
2015-03-21 05:07:23 -06:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
2022-04-11 16:32:57 -06:00
|
|
|
import base64
|
|
|
|
|
2021-09-19 06:22:31 -06:00
|
|
|
from yt_dlp.aes import (
|
2022-04-11 16:32:57 -06:00
|
|
|
BLOCK_SIZE_BYTES,
|
2021-09-19 06:22:31 -06:00
|
|
|
aes_cbc_decrypt,
|
|
|
|
aes_cbc_decrypt_bytes,
|
|
|
|
aes_cbc_encrypt,
|
|
|
|
aes_ctr_decrypt,
|
|
|
|
aes_ctr_encrypt,
|
2022-04-11 16:32:57 -06:00
|
|
|
aes_decrypt,
|
|
|
|
aes_decrypt_text,
|
|
|
|
aes_ecb_decrypt,
|
|
|
|
aes_ecb_encrypt,
|
|
|
|
aes_encrypt,
|
2021-09-19 06:22:31 -06:00
|
|
|
aes_gcm_decrypt_and_verify,
|
|
|
|
aes_gcm_decrypt_and_verify_bytes,
|
|
|
|
)
|
2022-04-20 13:05:57 -06:00
|
|
|
from yt_dlp.dependencies import Cryptodome_AES
|
2021-02-24 11:45:56 -07:00
|
|
|
from yt_dlp.utils import bytes_to_intlist, intlist_to_bytes
|
2015-03-21 05:07:23 -06:00
|
|
|
|
|
|
|
# the encrypted data can be generate with 'devscripts/generate_aes_testdata.py'
|
|
|
|
|
|
|
|
|
|
|
|
class TestAES(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.key = self.iv = [0x20, 0x15] + 14 * [0]
|
|
|
|
self.secret_msg = b'Secret message goes here'
|
|
|
|
|
|
|
|
def test_encrypt(self):
|
|
|
|
msg = b'message'
|
|
|
|
key = list(range(16))
|
|
|
|
encrypted = aes_encrypt(bytes_to_intlist(msg), key)
|
|
|
|
decrypted = intlist_to_bytes(aes_decrypt(encrypted, key))
|
|
|
|
self.assertEqual(decrypted, msg)
|
|
|
|
|
|
|
|
def test_cbc_decrypt(self):
|
2021-09-19 06:22:31 -06:00
|
|
|
data = b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\x27\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd'
|
|
|
|
decrypted = intlist_to_bytes(aes_cbc_decrypt(bytes_to_intlist(data), self.key, self.iv))
|
2015-03-21 05:07:23 -06:00
|
|
|
self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
|
2022-04-20 13:05:57 -06:00
|
|
|
if Cryptodome_AES:
|
2021-09-19 06:22:31 -06:00
|
|
|
decrypted = aes_cbc_decrypt_bytes(data, intlist_to_bytes(self.key), intlist_to_bytes(self.iv))
|
|
|
|
self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
|
2015-03-21 05:07:23 -06:00
|
|
|
|
2017-02-27 03:47:47 -07:00
|
|
|
def test_cbc_encrypt(self):
|
|
|
|
data = bytes_to_intlist(self.secret_msg)
|
|
|
|
encrypted = intlist_to_bytes(aes_cbc_encrypt(data, self.key, self.iv))
|
|
|
|
self.assertEqual(
|
|
|
|
encrypted,
|
2021-09-19 06:22:31 -06:00
|
|
|
b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd')
|
|
|
|
|
|
|
|
def test_ctr_decrypt(self):
|
|
|
|
data = bytes_to_intlist(b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08')
|
|
|
|
decrypted = intlist_to_bytes(aes_ctr_decrypt(data, self.key, self.iv))
|
|
|
|
self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
|
|
|
|
|
|
|
|
def test_ctr_encrypt(self):
|
|
|
|
data = bytes_to_intlist(self.secret_msg)
|
|
|
|
encrypted = intlist_to_bytes(aes_ctr_encrypt(data, self.key, self.iv))
|
|
|
|
self.assertEqual(
|
|
|
|
encrypted,
|
|
|
|
b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08')
|
|
|
|
|
|
|
|
def test_gcm_decrypt(self):
|
|
|
|
data = b'\x159Y\xcf5eud\x90\x9c\x85&]\x14\x1d\x0f.\x08\xb4T\xe4/\x17\xbd'
|
|
|
|
authentication_tag = b'\xe8&I\x80rI\x07\x9d}YWuU@:e'
|
|
|
|
|
|
|
|
decrypted = intlist_to_bytes(aes_gcm_decrypt_and_verify(
|
|
|
|
bytes_to_intlist(data), self.key, bytes_to_intlist(authentication_tag), self.iv[:12]))
|
|
|
|
self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
|
2022-04-20 13:05:57 -06:00
|
|
|
if Cryptodome_AES:
|
2021-09-19 06:22:31 -06:00
|
|
|
decrypted = aes_gcm_decrypt_and_verify_bytes(
|
|
|
|
data, intlist_to_bytes(self.key), authentication_tag, intlist_to_bytes(self.iv[:12]))
|
|
|
|
self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
|
2017-02-27 03:47:47 -07:00
|
|
|
|
2015-03-21 05:07:23 -06:00
|
|
|
def test_decrypt_text(self):
|
|
|
|
password = intlist_to_bytes(self.key).decode('utf-8')
|
|
|
|
encrypted = base64.b64encode(
|
2019-05-10 14:56:22 -06:00
|
|
|
intlist_to_bytes(self.iv[:8])
|
|
|
|
+ b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae'
|
2015-06-03 15:50:38 -06:00
|
|
|
).decode('utf-8')
|
2015-03-21 05:07:23 -06:00
|
|
|
decrypted = (aes_decrypt_text(encrypted, password, 16))
|
|
|
|
self.assertEqual(decrypted, self.secret_msg)
|
|
|
|
|
2015-03-22 05:09:58 -06:00
|
|
|
password = intlist_to_bytes(self.key).decode('utf-8')
|
|
|
|
encrypted = base64.b64encode(
|
2019-05-10 14:56:22 -06:00
|
|
|
intlist_to_bytes(self.iv[:8])
|
|
|
|
+ b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83'
|
2015-06-03 15:50:38 -06:00
|
|
|
).decode('utf-8')
|
2015-03-22 05:09:58 -06:00
|
|
|
decrypted = (aes_decrypt_text(encrypted, password, 32))
|
|
|
|
self.assertEqual(decrypted, self.secret_msg)
|
|
|
|
|
2021-11-18 18:54:10 -07:00
|
|
|
def test_ecb_encrypt(self):
|
|
|
|
data = bytes_to_intlist(self.secret_msg)
|
|
|
|
data += [0x08] * (BLOCK_SIZE_BYTES - len(data) % BLOCK_SIZE_BYTES)
|
|
|
|
encrypted = intlist_to_bytes(aes_ecb_encrypt(data, self.key, self.iv))
|
|
|
|
self.assertEqual(
|
|
|
|
encrypted,
|
|
|
|
b'\xaa\x86]\x81\x97>\x02\x92\x9d\x1bR[[L/u\xd3&\xd1(h\xde{\x81\x94\xba\x02\xae\xbd\xa6\xd0:')
|
|
|
|
|
|
|
|
def test_ecb_decrypt(self):
|
|
|
|
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:')
|
|
|
|
decrypted = intlist_to_bytes(aes_ecb_decrypt(data, self.key, self.iv))
|
|
|
|
self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
|
|
|
|
|
2016-11-17 04:42:56 -07:00
|
|
|
|
2015-03-21 05:07:23 -06:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|