test_PBES.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Util.py3compat import *
  36. from Crypto.IO._PBES import PBES2
  37. class TestPBES2(unittest.TestCase):
  38. def setUp(self):
  39. self.ref = b("Test data")
  40. self.passphrase = b("Passphrase")
  41. def test1(self):
  42. ct = PBES2.encrypt(self.ref, self.passphrase,
  43. 'PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC')
  44. pt = PBES2.decrypt(ct, self.passphrase)
  45. self.assertEqual(self.ref, pt)
  46. def test2(self):
  47. ct = PBES2.encrypt(self.ref, self.passphrase,
  48. 'PBKDF2WithHMAC-SHA1AndAES128-CBC')
  49. pt = PBES2.decrypt(ct, self.passphrase)
  50. self.assertEqual(self.ref, pt)
  51. def test3(self):
  52. ct = PBES2.encrypt(self.ref, self.passphrase,
  53. 'PBKDF2WithHMAC-SHA1AndAES192-CBC')
  54. pt = PBES2.decrypt(ct, self.passphrase)
  55. self.assertEqual(self.ref, pt)
  56. def test4(self):
  57. ct = PBES2.encrypt(self.ref, self.passphrase,
  58. 'scryptAndAES128-CBC')
  59. pt = PBES2.decrypt(ct, self.passphrase)
  60. self.assertEqual(self.ref, pt)
  61. def test5(self):
  62. ct = PBES2.encrypt(self.ref, self.passphrase,
  63. 'scryptAndAES192-CBC')
  64. pt = PBES2.decrypt(ct, self.passphrase)
  65. self.assertEqual(self.ref, pt)
  66. def test6(self):
  67. ct = PBES2.encrypt(self.ref, self.passphrase,
  68. 'scryptAndAES256-CBC')
  69. pt = PBES2.decrypt(ct, self.passphrase)
  70. self.assertEqual(self.ref, pt)
  71. def get_tests(config={}):
  72. from Crypto.SelfTest.st_common import list_test_cases
  73. listTests = []
  74. listTests += list_test_cases(TestPBES2)
  75. return listTests
  76. if __name__ == '__main__':
  77. suite = lambda: unittest.TestSuite(get_tests())
  78. unittest.main(defaultTest='suite')