test_OpenPGP.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 Crypto.SelfTest.st_common import list_test_cases
  32. from Crypto.Util.py3compat import tobytes, b, unhexlify
  33. from Crypto.Cipher import AES, DES3, DES
  34. from Crypto.Hash import SHAKE128
  35. def get_tag_random(tag, length):
  36. return SHAKE128.new(data=tobytes(tag)).read(length)
  37. from Crypto.SelfTest.Cipher.test_CBC import BlockChainingTests
  38. class OpenPGPTests(BlockChainingTests):
  39. aes_mode = AES.MODE_OPENPGP
  40. des3_mode = DES3.MODE_OPENPGP
  41. # Redefine test_unaligned_data_128/64
  42. key_128 = get_tag_random("key_128", 16)
  43. key_192 = get_tag_random("key_192", 24)
  44. iv_128 = get_tag_random("iv_128", 16)
  45. iv_64 = get_tag_random("iv_64", 8)
  46. data_128 = get_tag_random("data_128", 16)
  47. def test_loopback_128(self):
  48. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  49. pt = get_tag_random("plaintext", 16 * 100)
  50. ct = cipher.encrypt(pt)
  51. eiv, ct = ct[:18], ct[18:]
  52. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  53. pt2 = cipher.decrypt(ct)
  54. self.assertEqual(pt, pt2)
  55. def test_loopback_64(self):
  56. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
  57. pt = get_tag_random("plaintext", 8 * 100)
  58. ct = cipher.encrypt(pt)
  59. eiv, ct = ct[:10], ct[10:]
  60. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, eiv)
  61. pt2 = cipher.decrypt(ct)
  62. self.assertEqual(pt, pt2)
  63. def test_IV_iv_attributes(self):
  64. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  65. eiv = cipher.encrypt(b(""))
  66. self.assertEqual(cipher.iv, self.iv_128)
  67. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  68. self.assertEqual(cipher.iv, self.iv_128)
  69. def test_null_encryption_decryption(self):
  70. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  71. eiv = cipher.encrypt(b(""))
  72. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  73. self.assertEqual(cipher.decrypt(b("")), b(""))
  74. def test_either_encrypt_or_decrypt(self):
  75. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  76. eiv = cipher.encrypt(b(""))
  77. self.assertRaises(TypeError, cipher.decrypt, b(""))
  78. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
  79. cipher.decrypt(b(""))
  80. self.assertRaises(TypeError, cipher.encrypt, b(""))
  81. def test_unaligned_data_128(self):
  82. plaintexts = [ b("7777777") ] * 100
  83. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  84. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  85. cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
  86. self.assertEqual(b("").join(ciphertexts), cipher.encrypt(b("").join(plaintexts)))
  87. def test_unaligned_data_64(self):
  88. plaintexts = [ b("7777777") ] * 100
  89. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
  90. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  91. cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
  92. self.assertEqual(b("").join(ciphertexts), cipher.encrypt(b("").join(plaintexts)))
  93. class TestVectors(unittest.TestCase):
  94. def test_aes(self):
  95. # The following test vectors have been generated with gpg v1.4.0.
  96. # The command line used was:
  97. #
  98. # gpg -c -z 0 --cipher-algo AES --passphrase secret_passphrase \
  99. # --disable-mdc --s2k-mode 0 --output ct pt
  100. #
  101. # As result, the content of the file 'pt' is encrypted with a key derived
  102. # from 'secret_passphrase' and written to file 'ct'.
  103. # Test vectors must be extracted from 'ct', which is a collection of
  104. # TLVs (see RFC4880 for all details):
  105. # - the encrypted data (with the encrypted IV as prefix) is the payload
  106. # of the TLV with tag 9 (Symmetrical Encrypted Data Packet).
  107. # This is the ciphertext in the test vector.
  108. # - inside the encrypted part, there is a further layer of TLVs. One must
  109. # look for tag 11 (Literal Data Packet); in its payload, after a short
  110. # but time dependent header, there is the content of file 'pt'.
  111. # In the test vector, the plaintext is the complete set of TLVs that gets
  112. # encrypted. It is not just the content of 'pt'.
  113. # - the key is the leftmost 16 bytes of the SHA1 digest of the password.
  114. # The test vector contains such shortened digest.
  115. #
  116. # Note that encryption uses a clear IV, and decryption an encrypted IV
  117. plaintext = 'ac18620270744fb4f647426c61636b4361745768697465436174'
  118. ciphertext = 'dc6b9e1f095de609765c59983db5956ae4f63aea7405389d2ebb'
  119. key = '5baa61e4c9b93f3f0682250b6cf8331b'
  120. iv = '3d7d3e62282add7eb203eeba5c800733'
  121. encrypted_iv='fd934601ef49cb58b6d9aebca6056bdb96ef'
  122. plaintext = unhexlify(plaintext)
  123. ciphertext = unhexlify(ciphertext)
  124. key = unhexlify(key)
  125. iv = unhexlify(iv)
  126. encrypted_iv = unhexlify(encrypted_iv)
  127. cipher = AES.new(key, AES.MODE_OPENPGP, iv)
  128. ct = cipher.encrypt(plaintext)
  129. self.assertEqual(ct[:18], encrypted_iv)
  130. self.assertEqual(ct[18:], ciphertext)
  131. cipher = AES.new(key, AES.MODE_OPENPGP, encrypted_iv)
  132. pt = cipher.decrypt(ciphertext)
  133. self.assertEqual(pt, plaintext)
  134. def test_des3(self):
  135. # The following test vectors have been generated with gpg v1.4.0.
  136. # The command line used was:
  137. # gpg -c -z 0 --cipher-algo 3DES --passphrase secret_passphrase \
  138. # --disable-mdc --s2k-mode 0 --output ct pt
  139. # For an explanation, see test_AES.py .
  140. plaintext = 'ac1762037074324fb53ba3596f73656d69746556616c6c6579'
  141. ciphertext = '9979238528357b90e2e0be549cb0b2d5999b9a4a447e5c5c7d'
  142. key = '7ade65b460f5ea9be35f9e14aa883a2048e3824aa616c0b2'
  143. iv='cd47e2afb8b7e4b0'
  144. encrypted_iv='6a7eef0b58050e8b904a'
  145. plaintext = unhexlify(plaintext)
  146. ciphertext = unhexlify(ciphertext)
  147. key = unhexlify(key)
  148. iv = unhexlify(iv)
  149. encrypted_iv = unhexlify(encrypted_iv)
  150. cipher = DES3.new(key, DES3.MODE_OPENPGP, iv)
  151. ct = cipher.encrypt(plaintext)
  152. self.assertEqual(ct[:10], encrypted_iv)
  153. self.assertEqual(ct[10:], ciphertext)
  154. cipher = DES3.new(key, DES3.MODE_OPENPGP, encrypted_iv)
  155. pt = cipher.decrypt(ciphertext)
  156. self.assertEqual(pt, plaintext)
  157. def get_tests(config={}):
  158. tests = []
  159. tests += list_test_cases(OpenPGPTests)
  160. tests += list_test_cases(TestVectors)
  161. return tests
  162. if __name__ == '__main__':
  163. suite = lambda: unittest.TestSuite(get_tests())
  164. unittest.main(defaultTest='suite')