test_aes.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import base64
  8. from yt_dlp.aes import (
  9. aes_cbc_decrypt,
  10. aes_cbc_decrypt_bytes,
  11. aes_cbc_encrypt,
  12. aes_ctr_decrypt,
  13. aes_ctr_encrypt,
  14. aes_decrypt,
  15. aes_decrypt_text,
  16. aes_ecb_decrypt,
  17. aes_ecb_encrypt,
  18. aes_encrypt,
  19. aes_gcm_decrypt_and_verify,
  20. aes_gcm_decrypt_and_verify_bytes,
  21. key_expansion,
  22. pad_block,
  23. )
  24. from yt_dlp.dependencies import Cryptodome
  25. # the encrypted data can be generate with 'devscripts/generate_aes_testdata.py'
  26. class TestAES(unittest.TestCase):
  27. def setUp(self):
  28. self.key = self.iv = [0x20, 0x15] + 14 * [0]
  29. self.secret_msg = b'Secret message goes here'
  30. def test_encrypt(self):
  31. msg = b'message'
  32. key = list(range(16))
  33. encrypted = aes_encrypt(list(msg), key)
  34. decrypted = bytes(aes_decrypt(encrypted, key))
  35. self.assertEqual(decrypted, msg)
  36. def test_cbc_decrypt(self):
  37. data = b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\x27\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd'
  38. decrypted = bytes(aes_cbc_decrypt(list(data), self.key, self.iv))
  39. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  40. if Cryptodome.AES:
  41. decrypted = aes_cbc_decrypt_bytes(data, bytes(self.key), bytes(self.iv))
  42. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  43. def test_cbc_encrypt(self):
  44. data = list(self.secret_msg)
  45. encrypted = bytes(aes_cbc_encrypt(data, self.key, self.iv))
  46. self.assertEqual(
  47. encrypted,
  48. b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd')
  49. def test_ctr_decrypt(self):
  50. data = list(b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08')
  51. decrypted = bytes(aes_ctr_decrypt(data, self.key, self.iv))
  52. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  53. def test_ctr_encrypt(self):
  54. data = list(self.secret_msg)
  55. encrypted = bytes(aes_ctr_encrypt(data, self.key, self.iv))
  56. self.assertEqual(
  57. encrypted,
  58. b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08')
  59. def test_gcm_decrypt(self):
  60. data = b'\x159Y\xcf5eud\x90\x9c\x85&]\x14\x1d\x0f.\x08\xb4T\xe4/\x17\xbd'
  61. authentication_tag = b'\xe8&I\x80rI\x07\x9d}YWuU@:e'
  62. decrypted = bytes(aes_gcm_decrypt_and_verify(
  63. list(data), self.key, list(authentication_tag), self.iv[:12]))
  64. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  65. if Cryptodome.AES:
  66. decrypted = aes_gcm_decrypt_and_verify_bytes(
  67. data, bytes(self.key), authentication_tag, bytes(self.iv[:12]))
  68. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  69. def test_gcm_aligned_decrypt(self):
  70. data = b'\x159Y\xcf5eud\x90\x9c\x85&]\x14\x1d\x0f'
  71. authentication_tag = b'\x08\xb1\x9d!&\x98\xd0\xeaRq\x90\xe6;\xb5]\xd8'
  72. decrypted = bytes(aes_gcm_decrypt_and_verify(
  73. list(data), self.key, list(authentication_tag), self.iv[:12]))
  74. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg[:16])
  75. if Cryptodome.AES:
  76. decrypted = aes_gcm_decrypt_and_verify_bytes(
  77. data, bytes(self.key), authentication_tag, bytes(self.iv[:12]))
  78. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg[:16])
  79. def test_decrypt_text(self):
  80. password = bytes(self.key).decode()
  81. encrypted = base64.b64encode(
  82. bytes(self.iv[:8])
  83. + b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae',
  84. ).decode()
  85. decrypted = (aes_decrypt_text(encrypted, password, 16))
  86. self.assertEqual(decrypted, self.secret_msg)
  87. password = bytes(self.key).decode()
  88. encrypted = base64.b64encode(
  89. bytes(self.iv[:8])
  90. + b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83',
  91. ).decode()
  92. decrypted = (aes_decrypt_text(encrypted, password, 32))
  93. self.assertEqual(decrypted, self.secret_msg)
  94. def test_ecb_encrypt(self):
  95. data = list(self.secret_msg)
  96. encrypted = bytes(aes_ecb_encrypt(data, self.key))
  97. self.assertEqual(
  98. encrypted,
  99. b'\xaa\x86]\x81\x97>\x02\x92\x9d\x1bR[[L/u\xd3&\xd1(h\xde{\x81\x94\xba\x02\xae\xbd\xa6\xd0:')
  100. def test_ecb_decrypt(self):
  101. data = list(b'\xaa\x86]\x81\x97>\x02\x92\x9d\x1bR[[L/u\xd3&\xd1(h\xde{\x81\x94\xba\x02\xae\xbd\xa6\xd0:')
  102. decrypted = bytes(aes_ecb_decrypt(data, self.key, self.iv))
  103. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  104. def test_key_expansion(self):
  105. key = '4f6bdaa39e2f8cb07f5e722d9edef314'
  106. self.assertEqual(key_expansion(list(bytearray.fromhex(key))), [
  107. 0x4F, 0x6B, 0xDA, 0xA3, 0x9E, 0x2F, 0x8C, 0xB0, 0x7F, 0x5E, 0x72, 0x2D, 0x9E, 0xDE, 0xF3, 0x14,
  108. 0x53, 0x66, 0x20, 0xA8, 0xCD, 0x49, 0xAC, 0x18, 0xB2, 0x17, 0xDE, 0x35, 0x2C, 0xC9, 0x2D, 0x21,
  109. 0x8C, 0xBE, 0xDD, 0xD9, 0x41, 0xF7, 0x71, 0xC1, 0xF3, 0xE0, 0xAF, 0xF4, 0xDF, 0x29, 0x82, 0xD5,
  110. 0x2D, 0xAD, 0xDE, 0x47, 0x6C, 0x5A, 0xAF, 0x86, 0x9F, 0xBA, 0x00, 0x72, 0x40, 0x93, 0x82, 0xA7,
  111. 0xF9, 0xBE, 0x82, 0x4E, 0x95, 0xE4, 0x2D, 0xC8, 0x0A, 0x5E, 0x2D, 0xBA, 0x4A, 0xCD, 0xAF, 0x1D,
  112. 0x54, 0xC7, 0x26, 0x98, 0xC1, 0x23, 0x0B, 0x50, 0xCB, 0x7D, 0x26, 0xEA, 0x81, 0xB0, 0x89, 0xF7,
  113. 0x93, 0x60, 0x4E, 0x94, 0x52, 0x43, 0x45, 0xC4, 0x99, 0x3E, 0x63, 0x2E, 0x18, 0x8E, 0xEA, 0xD9,
  114. 0xCA, 0xE7, 0x7B, 0x39, 0x98, 0xA4, 0x3E, 0xFD, 0x01, 0x9A, 0x5D, 0xD3, 0x19, 0x14, 0xB7, 0x0A,
  115. 0xB0, 0x4E, 0x1C, 0xED, 0x28, 0xEA, 0x22, 0x10, 0x29, 0x70, 0x7F, 0xC3, 0x30, 0x64, 0xC8, 0xC9,
  116. 0xE8, 0xA6, 0xC1, 0xE9, 0xC0, 0x4C, 0xE3, 0xF9, 0xE9, 0x3C, 0x9C, 0x3A, 0xD9, 0x58, 0x54, 0xF3,
  117. 0xB4, 0x86, 0xCC, 0xDC, 0x74, 0xCA, 0x2F, 0x25, 0x9D, 0xF6, 0xB3, 0x1F, 0x44, 0xAE, 0xE7, 0xEC])
  118. def test_pad_block(self):
  119. block = [0x21, 0xA0, 0x43, 0xFF]
  120. self.assertEqual(pad_block(block, 'pkcs7'),
  121. [*block, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C])
  122. self.assertEqual(pad_block(block, 'iso7816'),
  123. [*block, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  124. self.assertEqual(pad_block(block, 'whitespace'),
  125. [*block, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20])
  126. self.assertEqual(pad_block(block, 'zero'),
  127. [*block, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  128. block = list(range(16))
  129. for mode in ('pkcs7', 'iso7816', 'whitespace', 'zero'):
  130. self.assertEqual(pad_block(block, mode), block, mode)
  131. if __name__ == '__main__':
  132. unittest.main()