test_Crypto.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import nose
  2. import os.path
  3. import OpenPGP
  4. import OpenPGP.Crypto
  5. import Crypto.Util
  6. import Crypto.PublicKey.RSA
  7. class TestMessageVerification:
  8. def oneMessage(self, pkey, path):
  9. pkeyM = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/' + pkey, 'rb').read())
  10. m = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/' + path, 'rb').read())
  11. verify = OpenPGP.Crypto.Wrapper(pkeyM)
  12. nose.tools.assert_equal(verify.verify(m), m.signatures())
  13. def testUncompressedOpsRSA(self):
  14. self.oneMessage('pubring.gpg', 'uncompressed-ops-rsa.gpg')
  15. def testCompressedSig(self):
  16. self.oneMessage('pubring.gpg', 'compressedsig.gpg')
  17. def testCompressedSigZLIB(self):
  18. self.oneMessage('pubring.gpg', 'compressedsig-zlib.gpg')
  19. def testCompressedSigBzip2(self):
  20. self.oneMessage('pubring.gpg', 'compressedsig-bzip2.gpg')
  21. def testSigningMessagesRSA(self):
  22. wkey = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/helloKey.gpg', 'rb').read())
  23. data = OpenPGP.LiteralDataPacket('This is text.', 'u', 'stuff.txt')
  24. sign = OpenPGP.Crypto.Wrapper(wkey)
  25. m = sign.sign(data).to_bytes()
  26. reparsedM = OpenPGP.Message.parse(m)
  27. nose.tools.assert_equal(sign.verify(reparsedM), reparsedM.signatures())
  28. def testSigningMessagesDSA(self):
  29. wkey = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/secring.gpg', 'rb').read())
  30. data = OpenPGP.LiteralDataPacket('This is text.', 'u', 'stuff.txt')
  31. dsa = OpenPGP.Crypto.Wrapper(wkey).private_key('7F69FA376B020509')
  32. m = OpenPGP.Crypto.Wrapper(data).sign(dsa, 'SHA512', '7F69FA376B020509').to_bytes()
  33. reparsedM = OpenPGP.Message.parse(m)
  34. nose.tools.assert_equal(OpenPGP.Crypto.Wrapper(wkey).verify(reparsedM), reparsedM.signatures())
  35. def testUncompressedOpsDSA(self):
  36. self.oneMessage('pubring.gpg', 'uncompressed-ops-dsa.gpg')
  37. def testUncompressedOpsDSAsha384(self):
  38. self.oneMessage('pubring.gpg', 'uncompressed-ops-dsa-sha384.txt.gpg')
  39. class TestKeyVerification:
  40. def oneKeyRSA(self, path):
  41. m = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/' + path, 'rb').read())
  42. verify = OpenPGP.Crypto.Wrapper(m)
  43. nose.tools.assert_equal(verify.verify(m), m.signatures())
  44. def testSigningKeysRSA(self):
  45. k = Crypto.PublicKey.RSA.generate(1024)
  46. nkey = OpenPGP.SecretKeyPacket((
  47. Crypto.Util.number.long_to_bytes(k.n),
  48. Crypto.Util.number.long_to_bytes(k.e),
  49. Crypto.Util.number.long_to_bytes(k.d),
  50. Crypto.Util.number.long_to_bytes(k.p),
  51. Crypto.Util.number.long_to_bytes(k.q),
  52. Crypto.Util.number.long_to_bytes(k.u)
  53. ))
  54. uid = OpenPGP.UserIDPacket('Test <test@example.com>')
  55. wkey = OpenPGP.Crypto.Wrapper(nkey)
  56. m = wkey.sign_key_userid([nkey, uid]).to_bytes()
  57. reparsedM = OpenPGP.Message.parse(m)
  58. nose.tools.assert_equal(wkey.verify(reparsedM), reparsedM.signatures())
  59. def testHelloKey(self):
  60. self.oneKeyRSA("helloKey.gpg")
  61. class TestDecryption:
  62. def oneSymmetric(self, pss, cnt, path):
  63. m = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/' + path, 'rb').read())
  64. m2 = OpenPGP.Crypto.Wrapper(m).decrypt_symmetric(pss)
  65. while(isinstance(m2[0], OpenPGP.CompressedDataPacket)):
  66. m2 = m2[0].data
  67. for p in m2:
  68. if(isinstance(p,OpenPGP.LiteralDataPacket)):
  69. nose.tools.assert_equal(p.data, cnt)
  70. def testDecryptAES(self):
  71. self.oneSymmetric("hello", b"PGP\n", "symmetric-aes.gpg")
  72. def testDecryptNoMDC(self):
  73. self.oneSymmetric("hello", b"PGP\n", "symmetric-no-mdc.gpg")
  74. def testDecrypt3DES(self):
  75. self.oneSymmetric("hello", b"PGP\n", "symmetric-3des.gpg")
  76. def testDecryptBlowfish(self):
  77. self.oneSymmetric("hello", b"PGP\n", "symmetric-blowfish.gpg")
  78. def testDecryptCAST5(self):
  79. self.oneSymmetric("hello", b"PGP\n", "symmetric-cast5.gpg")
  80. def testDecryptSessionKey(self):
  81. self.oneSymmetric("hello", b"PGP\n", "symmetric-with-session-key.gpg")
  82. def testDecryptSecretKey(self):
  83. key = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/encryptedSecretKey.gpg', 'rb').read())
  84. skey = OpenPGP.Crypto.Wrapper(key[0]).decrypt_secret_key("hello")
  85. nose.tools.assert_equal(not (not skey), True)
  86. def testDecryptAsymmetric(self):
  87. m = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/hello.gpg', 'rb').read())
  88. key = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/helloKey.gpg', 'rb').read())
  89. m2 = OpenPGP.Crypto.Wrapper(key).decrypt(m)
  90. while(isinstance(m2[0], OpenPGP.CompressedDataPacket)):
  91. m2 = m2[0].data
  92. for p in m2:
  93. if(isinstance(p,OpenPGP.LiteralDataPacket)):
  94. nose.tools.assert_equal(p.data, b"hello\n")
  95. class TestEncryption:
  96. def testEncryptSymmetric(self):
  97. data = OpenPGP.LiteralDataPacket('This is text.', 'u', 'stuff.txt')
  98. encrypted = OpenPGP.Crypto.Wrapper(OpenPGP.Message([data])).encrypt('secret')
  99. decrypted = OpenPGP.Crypto.Wrapper(encrypted).decrypt_symmetric('secret')
  100. nose.tools.assert_equal(decrypted[0].data, b'This is text.')
  101. def testEncryptAsymmetric(self):
  102. key = OpenPGP.Message.parse(open(os.path.dirname(__file__) + '/data/helloKey.gpg', 'rb').read())
  103. data = OpenPGP.LiteralDataPacket('This is text.', 'u', 'stuff.txt')
  104. encrypted = OpenPGP.Crypto.Wrapper(OpenPGP.Message([data])).encrypt(key)
  105. decryptor = OpenPGP.Crypto.Wrapper(key)
  106. decrypted = decryptor.decrypt(encrypted)
  107. nose.tools.assert_equal(decrypted[0].data, b'This is text.')