test_KDF.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Protocol/test_KDF.py: Self-test for key derivation functions
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. __revision__ = "$Id$"
  23. import unittest
  24. from binascii import unhexlify
  25. from Crypto.SelfTest.st_common import list_test_cases
  26. from Crypto.Hash import SHA as SHA1,HMAC
  27. from Crypto.Protocol.KDF import *
  28. def t2b(t): return unhexlify(b(t))
  29. class PBKDF1_Tests(unittest.TestCase):
  30. # List of tuples with test data.
  31. # Each tuple is made up by:
  32. # Item #0: a pass phrase
  33. # Item #1: salt (8 bytes encoded in hex)
  34. # Item #2: output key length
  35. # Item #3: iterations to use
  36. # Item #4: expected result (encoded in hex)
  37. _testData = (
  38. # From http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf
  39. ("password","78578E5A5D63CB06",16,1000,"DC19847E05C64D2FAF10EBFB4A3D2A20"),
  40. )
  41. def test1(self):
  42. v = self._testData[0]
  43. res = PBKDF1(v[0], t2b(v[1]), v[2], v[3], SHA1)
  44. self.assertEqual(res, t2b(v[4]))
  45. class PBKDF2_Tests(unittest.TestCase):
  46. # List of tuples with test data.
  47. # Each tuple is made up by:
  48. # Item #0: a pass phrase
  49. # Item #1: salt (encoded in hex)
  50. # Item #2: output key length
  51. # Item #3: iterations to use
  52. # Item #4: expected result (encoded in hex)
  53. _testData = (
  54. # From http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf
  55. ("password","78578E5A5D63CB06",24,2048,"BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643"),
  56. # From RFC 6050
  57. ("password","73616c74", 20, 1, "0c60c80f961f0e71f3a9b524af6012062fe037a6"),
  58. ("password","73616c74", 20, 2, "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"),
  59. ("password","73616c74", 20, 4096, "4b007901b765489abead49d926f721d065a429c1"),
  60. ("passwordPASSWORDpassword","73616c7453414c5473616c7453414c5473616c7453414c5473616c7453414c5473616c74",
  61. 25, 4096, "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"),
  62. ( 'pass\x00word',"7361006c74",16,4096, "56fa6aa75548099dcc37d7f03425e0c3"),
  63. )
  64. def test1(self):
  65. # Test only for HMAC-SHA1 as PRF
  66. def prf(p,s):
  67. return HMAC.new(p,s,SHA1).digest()
  68. for i in xrange(len(self._testData)):
  69. v = self._testData[i]
  70. res = PBKDF2(v[0], t2b(v[1]), v[2], v[3])
  71. res2 = PBKDF2(v[0], t2b(v[1]), v[2], v[3], prf)
  72. self.assertEqual(res, t2b(v[4]))
  73. self.assertEqual(res, res2)
  74. def get_tests(config={}):
  75. tests = []
  76. tests += list_test_cases(PBKDF1_Tests)
  77. tests += list_test_cases(PBKDF2_Tests)
  78. return tests
  79. if __name__ == '__main__':
  80. suite = lambda: unittest.TestSuite(get_tests())
  81. unittest.main(defaultTest='suite')
  82. # vim:set ts=4 sw=4 sts=4