OLD | NEW |
(Empty) | |
| 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 |
| 23 __revision__ = "$Id$" |
| 24 |
| 25 import unittest |
| 26 from binascii import unhexlify |
| 27 |
| 28 from Crypto.SelfTest.st_common import list_test_cases |
| 29 from Crypto.Hash import SHA as SHA1,HMAC |
| 30 |
| 31 from Crypto.Protocol.KDF import * |
| 32 |
| 33 def t2b(t): return unhexlify(b(t)) |
| 34 |
| 35 class PBKDF1_Tests(unittest.TestCase): |
| 36 |
| 37 # List of tuples with test data. |
| 38 # Each tuple is made up by: |
| 39 # Item #0: a pass phrase |
| 40 # Item #1: salt (8 bytes encoded in hex) |
| 41 # Item #2: output key length |
| 42 # Item #3: iterations to use |
| 43 # Item #4: expected result (encoded in hex) |
| 44 _testData = ( |
| 45 # From http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf |
| 46 ("password","78578E5A5D63CB06",16,1000,"DC19847E05C64D2FAF10EBFB4A3D
2A20"), |
| 47 ) |
| 48 |
| 49 def test1(self): |
| 50 v = self._testData[0] |
| 51 res = PBKDF1(v[0], t2b(v[1]), v[2], v[3], SHA1) |
| 52 self.assertEqual(res, t2b(v[4])) |
| 53 |
| 54 class PBKDF2_Tests(unittest.TestCase): |
| 55 |
| 56 # List of tuples with test data. |
| 57 # Each tuple is made up by: |
| 58 # Item #0: a pass phrase |
| 59 # Item #1: salt (encoded in hex) |
| 60 # Item #2: output key length |
| 61 # Item #3: iterations to use |
| 62 # Item #4: expected result (encoded in hex) |
| 63 _testData = ( |
| 64 # From http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf |
| 65 ("password","78578E5A5D63CB06",24,2048,"BFDE6BE94DF7E11DD409BCE20A02
55EC327CB936FFE93643"), |
| 66 # From RFC 6050 |
| 67 ("password","73616c74", 20, 1, "0c60c80f961f0e71f3a9b524af6
012062fe037a6"), |
| 68 ("password","73616c74", 20, 2, "ea6c014dc72d6f8ccd1ed92ace1
d41f0d8de8957"), |
| 69 ("password","73616c74", 20, 4096, "4b007901b765489abead49d926f
721d065a429c1"), |
| 70 ("passwordPASSWORDpassword","73616c7453414c5473616c7453414c5473616c7
453414c5473616c7453414c5473616c74", |
| 71 25, 4096, "3d2eec4fe41c849b80c8d83662c
0e44a8b291a964cf2f07038"), |
| 72 ( 'pass\x00word',"7361006c74",16,4096, "56fa6aa75548099dcc37d7f0342
5e0c3"), |
| 73 ) |
| 74 |
| 75 def test1(self): |
| 76 # Test only for HMAC-SHA1 as PRF |
| 77 |
| 78 def prf(p,s): |
| 79 return HMAC.new(p,s,SHA1).digest() |
| 80 |
| 81 for i in xrange(len(self._testData)): |
| 82 v = self._testData[i] |
| 83 res = PBKDF2(v[0], t2b(v[1]), v[2], v[3]) |
| 84 res2 = PBKDF2(v[0], t2b(v[1]), v[2], v[3], prf) |
| 85 self.assertEqual(res, t2b(v[4])) |
| 86 self.assertEqual(res, res2) |
| 87 |
| 88 def get_tests(config={}): |
| 89 tests = [] |
| 90 tests += list_test_cases(PBKDF1_Tests) |
| 91 tests += list_test_cases(PBKDF2_Tests) |
| 92 return tests |
| 93 |
| 94 if __name__ == '__main__': |
| 95 suite = lambda: unittest.TestSuite(get_tests()) |
| 96 unittest.main(defaultTest='suite') |
| 97 |
| 98 # vim:set ts=4 sw=4 sts=4 |
OLD | NEW |