test_PBES.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #
  2. # SelfTest/IO/test_PBES.py: Self-test for the _PBES module
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. """Self-tests for Crypto.IO._PBES module"""
  34. import unittest
  35. from Crypto.IO._PBES import PBES2
  36. class TestPBES2(unittest.TestCase):
  37. def setUp(self):
  38. self.ref = b"Test data"
  39. self.passphrase = b"Passphrase"
  40. def test1(self):
  41. ct = PBES2.encrypt(self.ref, self.passphrase,
  42. 'PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC')
  43. pt = PBES2.decrypt(ct, self.passphrase)
  44. self.assertEqual(self.ref, pt)
  45. def test2(self):
  46. ct = PBES2.encrypt(self.ref, self.passphrase,
  47. 'PBKDF2WithHMAC-SHA224AndAES128-CBC')
  48. pt = PBES2.decrypt(ct, self.passphrase)
  49. self.assertEqual(self.ref, pt)
  50. def test3(self):
  51. ct = PBES2.encrypt(self.ref, self.passphrase,
  52. 'PBKDF2WithHMAC-SHA256AndAES192-CBC')
  53. pt = PBES2.decrypt(ct, self.passphrase)
  54. self.assertEqual(self.ref, pt)
  55. def test4(self):
  56. ct = PBES2.encrypt(self.ref, self.passphrase,
  57. 'PBKDF2WithHMAC-SHA384AndAES256-CBC')
  58. pt = PBES2.decrypt(ct, self.passphrase)
  59. self.assertEqual(self.ref, pt)
  60. def test5(self):
  61. ct = PBES2.encrypt(self.ref, self.passphrase,
  62. 'PBKDF2WithHMAC-SHA512AndAES128-GCM')
  63. pt = PBES2.decrypt(ct, self.passphrase)
  64. self.assertEqual(self.ref, pt)
  65. def test6(self):
  66. ct = PBES2.encrypt(self.ref, self.passphrase,
  67. 'PBKDF2WithHMAC-SHA512-224AndAES192-GCM')
  68. pt = PBES2.decrypt(ct, self.passphrase)
  69. self.assertEqual(self.ref, pt)
  70. def test7(self):
  71. ct = PBES2.encrypt(self.ref, self.passphrase,
  72. 'PBKDF2WithHMAC-SHA3-256AndAES256-GCM')
  73. pt = PBES2.decrypt(ct, self.passphrase)
  74. self.assertEqual(self.ref, pt)
  75. def test8(self):
  76. ct = PBES2.encrypt(self.ref, self.passphrase,
  77. 'scryptAndAES128-CBC')
  78. pt = PBES2.decrypt(ct, self.passphrase)
  79. self.assertEqual(self.ref, pt)
  80. def test9(self):
  81. ct = PBES2.encrypt(self.ref, self.passphrase,
  82. 'scryptAndAES192-CBC')
  83. pt = PBES2.decrypt(ct, self.passphrase)
  84. self.assertEqual(self.ref, pt)
  85. def test10(self):
  86. ct = PBES2.encrypt(self.ref, self.passphrase,
  87. 'scryptAndAES256-CBC')
  88. pt = PBES2.decrypt(ct, self.passphrase)
  89. self.assertEqual(self.ref, pt)
  90. def get_tests(config={}):
  91. from Crypto.SelfTest.st_common import list_test_cases
  92. listTests = []
  93. listTests += list_test_cases(TestPBES2)
  94. return listTests
  95. if __name__ == '__main__':
  96. def suite():
  97. return unittest.TestSuite(get_tests())
  98. unittest.main(defaultTest='suite')