test_OpenPGP.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. import unittest
  31. from binascii import unhexlify
  32. from Cryptodome.SelfTest.st_common import list_test_cases
  33. from Cryptodome.Util.py3compat import tobytes
  34. from Cryptodome.Cipher import AES, DES3, DES
  35. from Cryptodome.Hash import SHAKE128
  36. def get_tag_random(tag, length):
  37. return SHAKE128.new(data=tobytes(tag)).read(length)
  38. from Cryptodome.SelfTest.Cipher.test_CBC import BlockChainingTests
  39. class OpenPGPTests(BlockChainingTests):
  40. aes_mode = AES.MODE_OPENPGP
  41. des3_mode = DES3.MODE_OPENPGP
  42. # Redefine test_unaligned_data_128/64
  43. key_128 = get_tag_random("key_128", 16)
  44. key_192 = get_tag_random("key_192", 24)
  45. iv_128 = get_tag_random("iv_128", 16)
  46. iv_64 = get_tag_random("iv_64", 8)
  47. data_128 = get_tag_random("data_128", 16)
  48. def test_loopback_128(self):
  49. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  50. pt = get_tag_random("plaintext", 16 * 100)
  51. ct = cipher.encrypt(pt)
  52. eiv, ct = ct[:18], ct[18:]
  53. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  54. pt2 = cipher.decrypt(ct)
  55. self.assertEqual(pt, pt2)
  56. def test_loopback_64(self):
  57. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
  58. pt = get_tag_random("plaintext", 8 * 100)
  59. ct = cipher.encrypt(pt)
  60. eiv, ct = ct[:10], ct[10:]
  61. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, eiv)
  62. pt2 = cipher.decrypt(ct)
  63. self.assertEqual(pt, pt2)
  64. def test_IV_iv_attributes(self):
  65. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  66. eiv = cipher.encrypt(b"")
  67. self.assertEqual(cipher.iv, self.iv_128)
  68. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  69. self.assertEqual(cipher.iv, self.iv_128)
  70. def test_null_encryption_decryption(self):
  71. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  72. eiv = cipher.encrypt(b"")
  73. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  74. self.assertEqual(cipher.decrypt(b""), b"")
  75. def test_either_encrypt_or_decrypt(self):
  76. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  77. eiv = cipher.encrypt(b"")
  78. self.assertRaises(TypeError, cipher.decrypt, b"")
  79. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  80. cipher.decrypt(b"")
  81. self.assertRaises(TypeError, cipher.encrypt, b"")
  82. def test_unaligned_data_128(self):
  83. plaintexts = [ b"7777777" ] * 100
  84. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  85. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  86. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  87. self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
  88. def test_unaligned_data_64(self):
  89. plaintexts = [ b"7777777" ] * 100
  90. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
  91. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  92. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
  93. self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
  94. def test_output_param(self):
  95. pass
  96. def test_output_param_memoryview(self):
  97. pass
  98. def test_output_param_neg(self):
  99. pass
  100. class TestVectors(unittest.TestCase):
  101. def test_aes(self):
  102. # The following test vectors have been generated with gpg v1.4.0.
  103. # The command line used was:
  104. #
  105. # gpg -c -z 0 --cipher-algo AES --passphrase secret_passphrase \
  106. # --disable-mdc --s2k-mode 0 --output ct pt
  107. #
  108. # As result, the content of the file 'pt' is encrypted with a key derived
  109. # from 'secret_passphrase' and written to file 'ct'.
  110. # Test vectors must be extracted from 'ct', which is a collection of
  111. # TLVs (see RFC4880 for all details):
  112. # - the encrypted data (with the encrypted IV as prefix) is the payload
  113. # of the TLV with tag 9 (Symmetrical Encrypted Data Packet).
  114. # This is the ciphertext in the test vector.
  115. # - inside the encrypted part, there is a further layer of TLVs. One must
  116. # look for tag 11 (Literal Data Packet); in its payload, after a short
  117. # but time dependent header, there is the content of file 'pt'.
  118. # In the test vector, the plaintext is the complete set of TLVs that gets
  119. # encrypted. It is not just the content of 'pt'.
  120. # - the key is the leftmost 16 bytes of the SHA1 digest of the password.
  121. # The test vector contains such shortened digest.
  122. #
  123. # Note that encryption uses a clear IV, and decryption an encrypted IV
  124. plaintext = 'ac18620270744fb4f647426c61636b4361745768697465436174'
  125. ciphertext = 'dc6b9e1f095de609765c59983db5956ae4f63aea7405389d2ebb'
  126. key = '5baa61e4c9b93f3f0682250b6cf8331b'
  127. iv = '3d7d3e62282add7eb203eeba5c800733'
  128. encrypted_iv='fd934601ef49cb58b6d9aebca6056bdb96ef'
  129. plaintext = unhexlify(plaintext)
  130. ciphertext = unhexlify(ciphertext)
  131. key = unhexlify(key)
  132. iv = unhexlify(iv)
  133. encrypted_iv = unhexlify(encrypted_iv)
  134. cipher = AES.new(key, AES.MODE_OPENPGP, iv)
  135. ct = cipher.encrypt(plaintext)
  136. self.assertEqual(ct[:18], encrypted_iv)
  137. self.assertEqual(ct[18:], ciphertext)
  138. cipher = AES.new(key, AES.MODE_OPENPGP, encrypted_iv)
  139. pt = cipher.decrypt(ciphertext)
  140. self.assertEqual(pt, plaintext)
  141. def test_des3(self):
  142. # The following test vectors have been generated with gpg v1.4.0.
  143. # The command line used was:
  144. # gpg -c -z 0 --cipher-algo 3DES --passphrase secret_passphrase \
  145. # --disable-mdc --s2k-mode 0 --output ct pt
  146. # For an explanation, see test_AES.py .
  147. plaintext = 'ac1762037074324fb53ba3596f73656d69746556616c6c6579'
  148. ciphertext = '9979238528357b90e2e0be549cb0b2d5999b9a4a447e5c5c7d'
  149. key = '7ade65b460f5ea9be35f9e14aa883a2048e3824aa616c0b2'
  150. iv='cd47e2afb8b7e4b0'
  151. encrypted_iv='6a7eef0b58050e8b904a'
  152. plaintext = unhexlify(plaintext)
  153. ciphertext = unhexlify(ciphertext)
  154. key = unhexlify(key)
  155. iv = unhexlify(iv)
  156. encrypted_iv = unhexlify(encrypted_iv)
  157. cipher = DES3.new(key, DES3.MODE_OPENPGP, iv)
  158. ct = cipher.encrypt(plaintext)
  159. self.assertEqual(ct[:10], encrypted_iv)
  160. self.assertEqual(ct[10:], ciphertext)
  161. cipher = DES3.new(key, DES3.MODE_OPENPGP, encrypted_iv)
  162. pt = cipher.decrypt(ciphertext)
  163. self.assertEqual(pt, plaintext)
  164. def get_tests(config={}):
  165. tests = []
  166. tests += list_test_cases(OpenPGPTests)
  167. tests += list_test_cases(TestVectors)
  168. return tests
  169. if __name__ == '__main__':
  170. suite = lambda: unittest.TestSuite(get_tests())
  171. unittest.main(defaultTest='suite')