basic functionalty
This commit is contained in:
53
modules/Crypto/SelfTest/Hash/__init__.py
Normal file
53
modules/Crypto/SelfTest/Hash/__init__.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/__init__.py: Self-test for hash modules
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test for hash modules"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
def get_tests(config={}):
|
||||
tests = []
|
||||
from Crypto.SelfTest.Hash import test_HMAC; tests += test_HMAC.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_CMAC; tests += test_CMAC.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_MD2; tests += test_MD2.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_MD4; tests += test_MD4.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_MD5; tests += test_MD5.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_RIPEMD160; tests += test_RIPEMD160.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_SHA1; tests += test_SHA1.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_SHA256; tests += test_SHA256.get_tests(config=config)
|
||||
try:
|
||||
from Crypto.SelfTest.Hash import test_SHA224; tests += test_SHA224.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_SHA384; tests += test_SHA384.get_tests(config=config)
|
||||
from Crypto.SelfTest.Hash import test_SHA512; tests += test_SHA512.get_tests(config=config)
|
||||
except ImportError:
|
||||
import sys
|
||||
sys.stderr.write("SelfTest: warning: not testing SHA224/SHA384/SHA512 modules (not available)\n")
|
||||
return tests
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
259
modules/Crypto/SelfTest/Hash/common.py
Normal file
259
modules/Crypto/SelfTest/Hash/common.py
Normal file
@@ -0,0 +1,259 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/common.py: Common code for Crypto.SelfTest.Hash
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-testing for PyCrypto hash modules"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
import binascii
|
||||
import Crypto.Hash
|
||||
from Crypto.Util.py3compat import *
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
|
||||
# For compatibility with Python 2.1 and Python 2.2
|
||||
if sys.hexversion < 0x02030000:
|
||||
# Python 2.1 doesn't have a dict() function
|
||||
# Python 2.2 dict() function raises TypeError if you do dict(MD5='blah')
|
||||
def dict(**kwargs):
|
||||
return kwargs.copy()
|
||||
else:
|
||||
dict = dict
|
||||
|
||||
from Crypto.Util.strxor import strxor_c
|
||||
|
||||
class HashDigestSizeSelfTest(unittest.TestCase):
|
||||
|
||||
def __init__(self, hashmod, description, expected):
|
||||
unittest.TestCase.__init__(self)
|
||||
self.hashmod = hashmod
|
||||
self.expected = expected
|
||||
self.description = description
|
||||
|
||||
def shortDescription(self):
|
||||
return self.description
|
||||
|
||||
def runTest(self):
|
||||
self.failUnless(hasattr(self.hashmod, "digest_size"))
|
||||
self.assertEquals(self.hashmod.digest_size, self.expected)
|
||||
h = self.hashmod.new()
|
||||
self.failUnless(hasattr(h, "digest_size"))
|
||||
self.assertEquals(h.digest_size, self.expected)
|
||||
|
||||
|
||||
class HashSelfTest(unittest.TestCase):
|
||||
|
||||
def __init__(self, hashmod, description, expected, input):
|
||||
unittest.TestCase.__init__(self)
|
||||
self.hashmod = hashmod
|
||||
self.expected = expected
|
||||
self.input = input
|
||||
self.description = description
|
||||
|
||||
def shortDescription(self):
|
||||
return self.description
|
||||
|
||||
def runTest(self):
|
||||
h = self.hashmod.new()
|
||||
h.update(self.input)
|
||||
|
||||
out1 = binascii.b2a_hex(h.digest())
|
||||
out2 = h.hexdigest()
|
||||
|
||||
h = self.hashmod.new(self.input)
|
||||
|
||||
out3 = h.hexdigest()
|
||||
out4 = binascii.b2a_hex(h.digest())
|
||||
|
||||
# PY3K: hexdigest() should return str(), and digest() bytes
|
||||
self.assertEqual(self.expected, out1) # h = .new(); h.update(data); h.digest()
|
||||
if sys.version_info[0] == 2:
|
||||
self.assertEqual(self.expected, out2) # h = .new(); h.update(data); h.hexdigest()
|
||||
self.assertEqual(self.expected, out3) # h = .new(data); h.hexdigest()
|
||||
else:
|
||||
self.assertEqual(self.expected.decode(), out2) # h = .new(); h.update(data); h.hexdigest()
|
||||
self.assertEqual(self.expected.decode(), out3) # h = .new(data); h.hexdigest()
|
||||
self.assertEqual(self.expected, out4) # h = .new(data); h.digest()
|
||||
|
||||
# Verify that the .new() method produces a fresh hash object, except
|
||||
# for MD5 and SHA1, which are hashlib objects. (But test any .new()
|
||||
# method that does exist.)
|
||||
if self.hashmod.__name__ not in ('Crypto.Hash.MD5', 'Crypto.Hash.SHA1') or hasattr(h, 'new'):
|
||||
h2 = h.new()
|
||||
h2.update(self.input)
|
||||
out5 = binascii.b2a_hex(h2.digest())
|
||||
self.assertEqual(self.expected, out5)
|
||||
|
||||
# Verify that Crypto.Hash.new(h) produces a fresh hash object
|
||||
h3 = Crypto.Hash.new(h)
|
||||
h3.update(self.input)
|
||||
out6 = binascii.b2a_hex(h3.digest())
|
||||
self.assertEqual(self.expected, out6)
|
||||
|
||||
if hasattr(h, 'name'):
|
||||
# Verify that Crypto.Hash.new(h.name) produces a fresh hash object
|
||||
h4 = Crypto.Hash.new(h.name)
|
||||
h4.update(self.input)
|
||||
out7 = binascii.b2a_hex(h4.digest())
|
||||
self.assertEqual(self.expected, out7)
|
||||
|
||||
class HashTestOID(unittest.TestCase):
|
||||
def __init__(self, hashmod, oid):
|
||||
unittest.TestCase.__init__(self)
|
||||
self.hashmod = hashmod
|
||||
self.oid = oid
|
||||
|
||||
def runTest(self):
|
||||
from Crypto.Signature import PKCS1_v1_5
|
||||
h = self.hashmod.new()
|
||||
self.assertEqual(PKCS1_v1_5._HASH_OIDS[h.name], self.oid)
|
||||
|
||||
class HashDocStringTest(unittest.TestCase):
|
||||
def __init__(self, hashmod):
|
||||
unittest.TestCase.__init__(self)
|
||||
self.hashmod = hashmod
|
||||
|
||||
def runTest(self):
|
||||
docstring = self.hashmod.__doc__
|
||||
self.assert_(hasattr(self.hashmod, '__doc__'))
|
||||
self.assert_(isinstance(self.hashmod.__doc__, str))
|
||||
|
||||
class GenericHashConstructorTest(unittest.TestCase):
|
||||
def __init__(self, hashmod):
|
||||
unittest.TestCase.__init__(self)
|
||||
self.hashmod = hashmod
|
||||
|
||||
def runTest(self):
|
||||
obj1 = self.hashmod.new("foo")
|
||||
obj2 = self.hashmod.new()
|
||||
obj3 = Crypto.Hash.new(obj1.name, "foo")
|
||||
obj4 = Crypto.Hash.new(obj1.name)
|
||||
obj5 = Crypto.Hash.new(obj1, "foo")
|
||||
obj6 = Crypto.Hash.new(obj1)
|
||||
self.assert_(isinstance(self.hashmod, obj1))
|
||||
self.assert_(isinstance(self.hashmod, obj2))
|
||||
self.assert_(isinstance(self.hashmod, obj3))
|
||||
self.assert_(isinstance(self.hashmod, obj4))
|
||||
self.assert_(isinstance(self.hashmod, obj5))
|
||||
self.assert_(isinstance(self.hashmod, obj6))
|
||||
|
||||
class MACSelfTest(unittest.TestCase):
|
||||
|
||||
def __init__(self, module, description, result, input, key, params):
|
||||
unittest.TestCase.__init__(self)
|
||||
self.module = module
|
||||
self.result = result
|
||||
self.input = input
|
||||
self.key = key
|
||||
self.params = params
|
||||
self.description = description
|
||||
|
||||
def shortDescription(self):
|
||||
return self.description
|
||||
|
||||
def runTest(self):
|
||||
key = binascii.a2b_hex(b(self.key))
|
||||
data = binascii.a2b_hex(b(self.input))
|
||||
|
||||
# Strip whitespace from the expected string (which should be in lowercase-hex)
|
||||
expected = b("".join(self.result.split()))
|
||||
|
||||
h = self.module.new(key, **self.params)
|
||||
h.update(data)
|
||||
out1_bin = h.digest()
|
||||
out1 = binascii.b2a_hex(h.digest())
|
||||
out2 = h.hexdigest()
|
||||
|
||||
# Verify that correct MAC does not raise any exception
|
||||
h.hexverify(out1)
|
||||
h.verify(out1_bin)
|
||||
|
||||
# Verify that incorrect MAC does raise ValueError exception
|
||||
wrong_mac = strxor_c(out1_bin, 255)
|
||||
self.assertRaises(ValueError, h.verify, wrong_mac)
|
||||
self.assertRaises(ValueError, h.hexverify, "4556")
|
||||
|
||||
h = self.module.new(key, data, **self.params)
|
||||
|
||||
out3 = h.hexdigest()
|
||||
out4 = binascii.b2a_hex(h.digest())
|
||||
|
||||
# Test .copy()
|
||||
h2 = h.copy()
|
||||
h.update(b("blah blah blah")) # Corrupt the original hash object
|
||||
out5 = binascii.b2a_hex(h2.digest()) # The copied hash object should return the correct result
|
||||
|
||||
# PY3K: Check that hexdigest() returns str and digest() returns bytes
|
||||
if sys.version_info[0] > 2:
|
||||
self.assertTrue(isinstance(h.digest(), type(b(""))))
|
||||
self.assertTrue(isinstance(h.hexdigest(), type("")))
|
||||
|
||||
# PY3K: Check that .hexverify() accepts bytes or str
|
||||
if sys.version_info[0] > 2:
|
||||
h.hexverify(h.hexdigest())
|
||||
h.hexverify(h.hexdigest().encode('ascii'))
|
||||
|
||||
# PY3K: hexdigest() should return str, and digest() should return bytes
|
||||
self.assertEqual(expected, out1)
|
||||
if sys.version_info[0] == 2:
|
||||
self.assertEqual(expected, out2)
|
||||
self.assertEqual(expected, out3)
|
||||
else:
|
||||
self.assertEqual(expected.decode(), out2)
|
||||
self.assertEqual(expected.decode(), out3)
|
||||
self.assertEqual(expected, out4)
|
||||
self.assertEqual(expected, out5)
|
||||
|
||||
def make_hash_tests(module, module_name, test_data, digest_size, oid=None):
|
||||
tests = []
|
||||
for i in range(len(test_data)):
|
||||
row = test_data[i]
|
||||
(expected, input) = map(b,row[0:2])
|
||||
if len(row) < 3:
|
||||
description = repr(input)
|
||||
else:
|
||||
description = row[2].encode('latin-1')
|
||||
name = "%s #%d: %s" % (module_name, i+1, description)
|
||||
tests.append(HashSelfTest(module, name, expected, input))
|
||||
name = "%s #%d: digest_size" % (module_name, i+1)
|
||||
tests.append(HashDigestSizeSelfTest(module, name, digest_size))
|
||||
if oid is not None:
|
||||
tests.append(HashTestOID(module, oid))
|
||||
tests.append(HashDocStringTest(module))
|
||||
if getattr(module, 'name', None) is not None:
|
||||
tests.append(GenericHashConstructorTest(module))
|
||||
return tests
|
||||
|
||||
def make_mac_tests(module, module_name, test_data):
|
||||
tests = []
|
||||
for i in range(len(test_data)):
|
||||
row = test_data[i]
|
||||
(key, data, results, description, params) = row
|
||||
name = "%s #%d: %s" % (module_name, i+1, description)
|
||||
tests.append(MACSelfTest(module, name, results, data, key, params))
|
||||
return tests
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
249
modules/Crypto/SelfTest/Hash/test_CMAC.py
Normal file
249
modules/Crypto/SelfTest/Hash/test_CMAC.py
Normal file
@@ -0,0 +1,249 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/CMAC.py: Self-test for the CMAC module
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.CMAC"""
|
||||
|
||||
import sys
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
from common import dict
|
||||
|
||||
from Crypto.Hash import CMAC
|
||||
from Crypto.Cipher import AES, DES3
|
||||
|
||||
# This is a list of (key, data, result, description, module) tuples.
|
||||
test_data = [
|
||||
|
||||
## Test vectors from RFC 4493 ##
|
||||
## The are also in NIST SP 800 38B D.2 ##
|
||||
( '2b7e151628aed2a6abf7158809cf4f3c',
|
||||
'',
|
||||
'bb1d6929e95937287fa37d129b756746',
|
||||
'RFC 4493 #1',
|
||||
AES
|
||||
),
|
||||
|
||||
( '2b7e151628aed2a6abf7158809cf4f3c',
|
||||
'6bc1bee22e409f96e93d7e117393172a',
|
||||
'070a16b46b4d4144f79bdd9dd04a287c',
|
||||
'RFC 4493 #2',
|
||||
AES
|
||||
),
|
||||
|
||||
( '2b7e151628aed2a6abf7158809cf4f3c',
|
||||
'6bc1bee22e409f96e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c9eb76fac45af8e51'+
|
||||
'30c81c46a35ce411',
|
||||
'dfa66747de9ae63030ca32611497c827',
|
||||
'RFC 4493 #3',
|
||||
AES
|
||||
),
|
||||
|
||||
( '2b7e151628aed2a6abf7158809cf4f3c',
|
||||
'6bc1bee22e409f96e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c9eb76fac45af8e51'+
|
||||
'30c81c46a35ce411e5fbc1191a0a52ef'+
|
||||
'f69f2445df4f9b17ad2b417be66c3710',
|
||||
'51f0bebf7e3b9d92fc49741779363cfe',
|
||||
'RFC 4493 #4',
|
||||
AES
|
||||
),
|
||||
|
||||
## The rest of Appendix D of NIST SP 800 38B
|
||||
## was not totally correct.
|
||||
## Values in Examples 14, 15, 18, and 19 were wrong.
|
||||
## The updated test values are published in:
|
||||
## http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
|
||||
|
||||
( '8e73b0f7da0e6452c810f32b809079e5'+
|
||||
'62f8ead2522c6b7b',
|
||||
'',
|
||||
'd17ddf46adaacde531cac483de7a9367',
|
||||
'NIST SP 800 38B D.2 Example 5',
|
||||
AES
|
||||
),
|
||||
|
||||
( '8e73b0f7da0e6452c810f32b809079e5'+
|
||||
'62f8ead2522c6b7b',
|
||||
'6bc1bee22e409f96e93d7e117393172a',
|
||||
'9e99a7bf31e710900662f65e617c5184',
|
||||
'NIST SP 800 38B D.2 Example 6',
|
||||
AES
|
||||
),
|
||||
|
||||
( '8e73b0f7da0e6452c810f32b809079e5'+
|
||||
'62f8ead2522c6b7b',
|
||||
'6bc1bee22e409f96e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c9eb76fac45af8e51'+
|
||||
'30c81c46a35ce411',
|
||||
'8a1de5be2eb31aad089a82e6ee908b0e',
|
||||
'NIST SP 800 38B D.2 Example 7',
|
||||
AES
|
||||
),
|
||||
|
||||
( '8e73b0f7da0e6452c810f32b809079e5'+
|
||||
'62f8ead2522c6b7b',
|
||||
'6bc1bee22e409f96e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c9eb76fac45af8e51'+
|
||||
'30c81c46a35ce411e5fbc1191a0a52ef'+
|
||||
'f69f2445df4f9b17ad2b417be66c3710',
|
||||
'a1d5df0eed790f794d77589659f39a11',
|
||||
'NIST SP 800 38B D.2 Example 8',
|
||||
AES
|
||||
),
|
||||
|
||||
( '603deb1015ca71be2b73aef0857d7781'+
|
||||
'1f352c073b6108d72d9810a30914dff4',
|
||||
'',
|
||||
'028962f61b7bf89efc6b551f4667d983',
|
||||
'NIST SP 800 38B D.3 Example 9',
|
||||
AES
|
||||
),
|
||||
|
||||
( '603deb1015ca71be2b73aef0857d7781'+
|
||||
'1f352c073b6108d72d9810a30914dff4',
|
||||
'6bc1bee22e409f96e93d7e117393172a',
|
||||
'28a7023f452e8f82bd4bf28d8c37c35c',
|
||||
'NIST SP 800 38B D.3 Example 10',
|
||||
AES
|
||||
),
|
||||
|
||||
( '603deb1015ca71be2b73aef0857d7781'+
|
||||
'1f352c073b6108d72d9810a30914dff4',
|
||||
'6bc1bee22e409f96e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c9eb76fac45af8e51'+
|
||||
'30c81c46a35ce411',
|
||||
'aaf3d8f1de5640c232f5b169b9c911e6',
|
||||
'NIST SP 800 38B D.3 Example 11',
|
||||
AES
|
||||
),
|
||||
|
||||
( '603deb1015ca71be2b73aef0857d7781'+
|
||||
'1f352c073b6108d72d9810a30914dff4',
|
||||
'6bc1bee22e409f96e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c9eb76fac45af8e51'+
|
||||
'30c81c46a35ce411e5fbc1191a0a52ef'+
|
||||
'f69f2445df4f9b17ad2b417be66c3710',
|
||||
'e1992190549f6ed5696a2c056c315410',
|
||||
'NIST SP 800 38B D.3 Example 12',
|
||||
AES
|
||||
),
|
||||
|
||||
( '8aa83bf8cbda1062'+
|
||||
'0bc1bf19fbb6cd58'+
|
||||
'bc313d4a371ca8b5',
|
||||
'',
|
||||
'b7a688e122ffaf95',
|
||||
'NIST SP 800 38B D.4 Example 13',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '8aa83bf8cbda1062'+
|
||||
'0bc1bf19fbb6cd58'+
|
||||
'bc313d4a371ca8b5',
|
||||
'6bc1bee22e409f96',
|
||||
'8e8f293136283797',
|
||||
'NIST SP 800 38B D.4 Example 14',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '8aa83bf8cbda1062'+
|
||||
'0bc1bf19fbb6cd58'+
|
||||
'bc313d4a371ca8b5',
|
||||
'6bc1bee22e409f96'+
|
||||
'e93d7e117393172a'+
|
||||
'ae2d8a57',
|
||||
'743ddbe0ce2dc2ed',
|
||||
'NIST SP 800 38B D.4 Example 15',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '8aa83bf8cbda1062'+
|
||||
'0bc1bf19fbb6cd58'+
|
||||
'bc313d4a371ca8b5',
|
||||
'6bc1bee22e409f96'+
|
||||
'e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c'+
|
||||
'9eb76fac45af8e51',
|
||||
'33e6b1092400eae5',
|
||||
'NIST SP 800 38B D.4 Example 16',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '4cf15134a2850dd5'+
|
||||
'8a3d10ba80570d38',
|
||||
'',
|
||||
'bd2ebf9a3ba00361',
|
||||
'NIST SP 800 38B D.7 Example 17',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '4cf15134a2850dd5'+
|
||||
'8a3d10ba80570d38',
|
||||
'6bc1bee22e409f96',
|
||||
'4ff2ab813c53ce83',
|
||||
'NIST SP 800 38B D.7 Example 18',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '4cf15134a2850dd5'+
|
||||
'8a3d10ba80570d38',
|
||||
'6bc1bee22e409f96'+
|
||||
'e93d7e117393172a'+
|
||||
'ae2d8a57',
|
||||
'62dd1b471902bd4e',
|
||||
'NIST SP 800 38B D.7 Example 19',
|
||||
DES3
|
||||
),
|
||||
|
||||
( '4cf15134a2850dd5'+
|
||||
'8a3d10ba80570d38',
|
||||
'6bc1bee22e409f96'+
|
||||
'e93d7e117393172a'+
|
||||
'ae2d8a571e03ac9c'+
|
||||
'9eb76fac45af8e51',
|
||||
'31b1e431dabc4eb8',
|
||||
'NIST SP 800 38B D.7 Example 20',
|
||||
DES3
|
||||
),
|
||||
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
global test_data
|
||||
from common import make_mac_tests
|
||||
|
||||
# Add new() parameters to the back of each test vector
|
||||
params_test_data = []
|
||||
for row in test_data:
|
||||
t = list(row)
|
||||
t[4] = dict(ciphermod=t[4])
|
||||
params_test_data.append(t)
|
||||
|
||||
return make_mac_tests(CMAC, "CMAC", params_test_data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
233
modules/Crypto/SelfTest/Hash/test_HMAC.py
Normal file
233
modules/Crypto/SelfTest/Hash/test_HMAC.py
Normal file
@@ -0,0 +1,233 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/HMAC.py: Self-test for the HMAC module
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.HMAC"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from common import dict # For compatibility with Python 2.1 and 2.2
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
from Crypto.Hash import MD5, SHA1, SHA224, SHA256, SHA384, SHA512, HMAC
|
||||
|
||||
default_hash = None
|
||||
|
||||
# This is a list of (key, data, results, description) tuples.
|
||||
test_data = [
|
||||
## Test vectors from RFC 2202 ##
|
||||
# Test that the default hashmod is MD5
|
||||
('0b' * 16,
|
||||
'4869205468657265',
|
||||
dict(default_hash='9294727a3638bb1c13f48ef8158bfc9d'),
|
||||
'default-is-MD5'),
|
||||
|
||||
# Test case 1 (MD5)
|
||||
('0b' * 16,
|
||||
'4869205468657265',
|
||||
dict(MD5='9294727a3638bb1c13f48ef8158bfc9d'),
|
||||
'RFC 2202 #1-MD5 (HMAC-MD5)'),
|
||||
|
||||
# Test case 1 (SHA1)
|
||||
('0b' * 20,
|
||||
'4869205468657265',
|
||||
dict(SHA1='b617318655057264e28bc0b6fb378c8ef146be00'),
|
||||
'RFC 2202 #1-SHA1 (HMAC-SHA1)'),
|
||||
|
||||
# Test case 2
|
||||
('4a656665',
|
||||
'7768617420646f2079612077616e7420666f72206e6f7468696e673f',
|
||||
dict(MD5='750c783e6ab0b503eaa86e310a5db738',
|
||||
SHA1='effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'),
|
||||
'RFC 2202 #2 (HMAC-MD5/SHA1)'),
|
||||
|
||||
# Test case 3 (MD5)
|
||||
('aa' * 16,
|
||||
'dd' * 50,
|
||||
dict(MD5='56be34521d144c88dbb8c733f0e8b3f6'),
|
||||
'RFC 2202 #3-MD5 (HMAC-MD5)'),
|
||||
|
||||
# Test case 3 (SHA1)
|
||||
('aa' * 20,
|
||||
'dd' * 50,
|
||||
dict(SHA1='125d7342b9ac11cd91a39af48aa17b4f63f175d3'),
|
||||
'RFC 2202 #3-SHA1 (HMAC-SHA1)'),
|
||||
|
||||
# Test case 4
|
||||
('0102030405060708090a0b0c0d0e0f10111213141516171819',
|
||||
'cd' * 50,
|
||||
dict(MD5='697eaf0aca3a3aea3a75164746ffaa79',
|
||||
SHA1='4c9007f4026250c6bc8414f9bf50c86c2d7235da'),
|
||||
'RFC 2202 #4 (HMAC-MD5/SHA1)'),
|
||||
|
||||
# Test case 5 (MD5)
|
||||
('0c' * 16,
|
||||
'546573742057697468205472756e636174696f6e',
|
||||
dict(MD5='56461ef2342edc00f9bab995690efd4c'),
|
||||
'RFC 2202 #5-MD5 (HMAC-MD5)'),
|
||||
|
||||
# Test case 5 (SHA1)
|
||||
# NB: We do not implement hash truncation, so we only test the full hash here.
|
||||
('0c' * 20,
|
||||
'546573742057697468205472756e636174696f6e',
|
||||
dict(SHA1='4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'),
|
||||
'RFC 2202 #5-SHA1 (HMAC-SHA1)'),
|
||||
|
||||
# Test case 6
|
||||
('aa' * 80,
|
||||
'54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
|
||||
+ '65204b6579202d2048617368204b6579204669727374',
|
||||
dict(MD5='6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd',
|
||||
SHA1='aa4ae5e15272d00e95705637ce8a3b55ed402112'),
|
||||
'RFC 2202 #6 (HMAC-MD5/SHA1)'),
|
||||
|
||||
# Test case 7
|
||||
('aa' * 80,
|
||||
'54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
|
||||
+ '65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d'
|
||||
+ '53697a652044617461',
|
||||
dict(MD5='6f630fad67cda0ee1fb1f562db3aa53e',
|
||||
SHA1='e8e99d0f45237d786d6bbaa7965c7808bbff1a91'),
|
||||
'RFC 2202 #7 (HMAC-MD5/SHA1)'),
|
||||
|
||||
## Test vectors from RFC 4231 ##
|
||||
# 4.2. Test Case 1
|
||||
('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b',
|
||||
'4869205468657265',
|
||||
dict(SHA256='''
|
||||
b0344c61d8db38535ca8afceaf0bf12b
|
||||
881dc200c9833da726e9376c2e32cff7
|
||||
'''),
|
||||
'RFC 4231 #1 (HMAC-SHA256)'),
|
||||
|
||||
# 4.3. Test Case 2 - Test with a key shorter than the length of the HMAC
|
||||
# output.
|
||||
('4a656665',
|
||||
'7768617420646f2079612077616e7420666f72206e6f7468696e673f',
|
||||
dict(SHA256='''
|
||||
5bdcc146bf60754e6a042426089575c7
|
||||
5a003f089d2739839dec58b964ec3843
|
||||
'''),
|
||||
'RFC 4231 #2 (HMAC-SHA256)'),
|
||||
|
||||
# 4.4. Test Case 3 - Test with a combined length of key and data that is
|
||||
# larger than 64 bytes (= block-size of SHA-224 and SHA-256).
|
||||
('aa' * 20,
|
||||
'dd' * 50,
|
||||
dict(SHA256='''
|
||||
773ea91e36800e46854db8ebd09181a7
|
||||
2959098b3ef8c122d9635514ced565fe
|
||||
'''),
|
||||
'RFC 4231 #3 (HMAC-SHA256)'),
|
||||
|
||||
# 4.5. Test Case 4 - Test with a combined length of key and data that is
|
||||
# larger than 64 bytes (= block-size of SHA-224 and SHA-256).
|
||||
('0102030405060708090a0b0c0d0e0f10111213141516171819',
|
||||
'cd' * 50,
|
||||
dict(SHA256='''
|
||||
82558a389a443c0ea4cc819899f2083a
|
||||
85f0faa3e578f8077a2e3ff46729665b
|
||||
'''),
|
||||
'RFC 4231 #4 (HMAC-SHA256)'),
|
||||
|
||||
# 4.6. Test Case 5 - Test with a truncation of output to 128 bits.
|
||||
#
|
||||
# Not included because we do not implement hash truncation.
|
||||
#
|
||||
|
||||
# 4.7. Test Case 6 - Test with a key larger than 128 bytes (= block-size of
|
||||
# SHA-384 and SHA-512).
|
||||
('aa' * 131,
|
||||
'54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
|
||||
+ '65204b6579202d2048617368204b6579204669727374',
|
||||
dict(SHA256='''
|
||||
60e431591ee0b67f0d8a26aacbf5b77f
|
||||
8e0bc6213728c5140546040f0ee37f54
|
||||
'''),
|
||||
'RFC 4231 #6 (HMAC-SHA256)'),
|
||||
|
||||
# 4.8. Test Case 7 - Test with a key and data that is larger than 128 bytes
|
||||
# (= block-size of SHA-384 and SHA-512).
|
||||
('aa' * 131,
|
||||
'5468697320697320612074657374207573696e672061206c6172676572207468'
|
||||
+ '616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074'
|
||||
+ '68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565'
|
||||
+ '647320746f20626520686173686564206265666f7265206265696e6720757365'
|
||||
+ '642062792074686520484d414320616c676f726974686d2e',
|
||||
dict(SHA256='''
|
||||
9b09ffa71b942fcb27635fbcd5b0e944
|
||||
bfdc63644f0713938a7f51535c3a35e2
|
||||
'''),
|
||||
'RFC 4231 #7 (HMAC-SHA256)'),
|
||||
|
||||
# Test case 8 (SHA224)
|
||||
('4a656665',
|
||||
'7768617420646f2079612077616e74'
|
||||
+ '20666f72206e6f7468696e673f',
|
||||
dict(SHA224='a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44'),
|
||||
'RFC 4634 8.4 SHA224 (HMAC-SHA224)'),
|
||||
|
||||
# Test case 9 (SHA384)
|
||||
('4a656665',
|
||||
'7768617420646f2079612077616e74'
|
||||
+ '20666f72206e6f7468696e673f',
|
||||
dict(SHA384='af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649'),
|
||||
'RFC 4634 8.4 SHA384 (HMAC-SHA384)'),
|
||||
|
||||
# Test case 10 (SHA512)
|
||||
('4a656665',
|
||||
'7768617420646f2079612077616e74'
|
||||
+ '20666f72206e6f7468696e673f',
|
||||
dict(SHA512='164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'),
|
||||
'RFC 4634 8.4 SHA512 (HMAC-SHA512)'),
|
||||
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
global test_data
|
||||
from common import make_mac_tests
|
||||
|
||||
# A test vector contains multiple results, each one for a
|
||||
# different hash algorithm.
|
||||
# Here we expand each test vector into multiple ones,
|
||||
# and add the relevant parameters that will be passed to new()
|
||||
exp_test_data = []
|
||||
for row in test_data:
|
||||
for modname in row[2].keys():
|
||||
t = list(row)
|
||||
t[2] = row[2][modname]
|
||||
try:
|
||||
t.append(dict(digestmod=globals()[modname]))
|
||||
exp_test_data.append(t)
|
||||
except AttributeError:
|
||||
import sys
|
||||
sys.stderr.write("SelfTest: warning: not testing HMAC-%s (not available)\n" % modname)
|
||||
|
||||
return make_mac_tests(HMAC, "HMAC", exp_test_data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
64
modules/Crypto/SelfTest/Hash/test_MD2.py
Normal file
64
modules/Crypto/SelfTest/Hash/test_MD2.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/MD2.py: Self-test for the MD2 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.MD2"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
# Test vectors from RFC 1319
|
||||
('8350e5a3e24c153df2275c9f80692773', '', "'' (empty string)"),
|
||||
('32ec01ec4a6dac72c0ab96fb34c0b5d1', 'a'),
|
||||
('da853b0d3f88d99b30283a69e6ded6bb', 'abc'),
|
||||
('ab4f496bfb2a530b219ff33031fe06b0', 'message digest'),
|
||||
|
||||
('4e8ddff3650292ab5a4108c3aa47940b', 'abcdefghijklmnopqrstuvwxyz',
|
||||
'a-z'),
|
||||
|
||||
('da33def2a42df13975352846c30338cd',
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
||||
'A-Z, a-z, 0-9'),
|
||||
|
||||
('d5976f79d83d3a0dc9806c3c66f3efd8',
|
||||
'1234567890123456789012345678901234567890123456'
|
||||
+ '7890123456789012345678901234567890',
|
||||
"'1234567890' * 8"),
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import MD2
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(MD2, "MD2", test_data,
|
||||
digest_size=16,
|
||||
oid="1.2.840.113549.2.2")
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
64
modules/Crypto/SelfTest/Hash/test_MD4.py
Normal file
64
modules/Crypto/SelfTest/Hash/test_MD4.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/MD4.py: Self-test for the MD4 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.MD4"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
# Test vectors from RFC 1320
|
||||
('31d6cfe0d16ae931b73c59d7e0c089c0', '', "'' (empty string)"),
|
||||
('bde52cb31de33e46245e05fbdbd6fb24', 'a'),
|
||||
('a448017aaf21d8525fc10ae87aa6729d', 'abc'),
|
||||
('d9130a8164549fe818874806e1c7014b', 'message digest'),
|
||||
|
||||
('d79e1c308aa5bbcdeea8ed63df412da9', 'abcdefghijklmnopqrstuvwxyz',
|
||||
'a-z'),
|
||||
|
||||
('043f8582f241db351ce627e153e7f0e4',
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
||||
'A-Z, a-z, 0-9'),
|
||||
|
||||
('e33b4ddc9c38f2199c3e7b164fcc0536',
|
||||
'1234567890123456789012345678901234567890123456'
|
||||
+ '7890123456789012345678901234567890',
|
||||
"'1234567890' * 8"),
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import MD4
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(MD4, "MD4", test_data,
|
||||
digest_size=16,
|
||||
oid="1.2.840.113549.2.4")
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
64
modules/Crypto/SelfTest/Hash/test_MD5.py
Normal file
64
modules/Crypto/SelfTest/Hash/test_MD5.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/MD5.py: Self-test for the MD5 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.MD5"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
# Test vectors from RFC 1321
|
||||
('d41d8cd98f00b204e9800998ecf8427e', '', "'' (empty string)"),
|
||||
('0cc175b9c0f1b6a831c399e269772661', 'a'),
|
||||
('900150983cd24fb0d6963f7d28e17f72', 'abc'),
|
||||
('f96b697d7cb7938d525a2f31aaf161d0', 'message digest'),
|
||||
|
||||
('c3fcd3d76192e4007dfb496cca67e13b', 'abcdefghijklmnopqrstuvwxyz',
|
||||
'a-z'),
|
||||
|
||||
('d174ab98d277d9f5a5611c2c9f419d9f',
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
||||
'A-Z, a-z, 0-9'),
|
||||
|
||||
('57edf4a22be3c955ac49da2e2107b67a',
|
||||
'1234567890123456789012345678901234567890123456'
|
||||
+ '7890123456789012345678901234567890',
|
||||
"'1234567890' * 8"),
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import MD5
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(MD5, "MD5", test_data,
|
||||
digest_size=16,
|
||||
oid="1.2.840.113549.2.5")
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
73
modules/Crypto/SelfTest/Hash/test_RIPEMD160.py
Normal file
73
modules/Crypto/SelfTest/Hash/test_RIPEMD160.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/test_RIPEMD160.py: Self-test for the RIPEMD-160 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
#"""Self-test suite for Crypto.Hash.RIPEMD160"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
# Test vectors downloaded 2008-09-12 from
|
||||
# http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
||||
('9c1185a5c5e9fc54612808977ee8f548b2258d31', '', "'' (empty string)"),
|
||||
('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe', 'a'),
|
||||
('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc', 'abc'),
|
||||
('5d0689ef49d2fae572b881b123a85ffa21595f36', 'message digest'),
|
||||
|
||||
('f71c27109c692c1b56bbdceb5b9d2865b3708dbc',
|
||||
'abcdefghijklmnopqrstuvwxyz',
|
||||
'a-z'),
|
||||
|
||||
('12a053384a9c0c88e405a06c27dcf49ada62eb2b',
|
||||
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
|
||||
'abcdbcd...pnopq'),
|
||||
|
||||
('b0e20b6e3116640286ed3a87a5713079b21f5189',
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
||||
'A-Z, a-z, 0-9'),
|
||||
|
||||
('9b752e45573d4b39f4dbd3323cab82bf63326bfb',
|
||||
'1234567890' * 8,
|
||||
"'1234567890' * 8"),
|
||||
|
||||
('52783243c1697bdbe16d37f97f68f08325dc1528',
|
||||
'a' * 10**6,
|
||||
'"a" * 10**6'),
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import RIPEMD160
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(RIPEMD160, "RIPEMD160", test_data,
|
||||
digest_size=20,
|
||||
oid="1.3.36.3.2.1")
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
64
modules/Crypto/SelfTest/Hash/test_SHA1.py
Normal file
64
modules/Crypto/SelfTest/Hash/test_SHA1.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/SHA1.py: Self-test for the SHA-1 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.SHA"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
# Test vectors from various sources
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
# FIPS PUB 180-2, A.1 - "One-Block Message"
|
||||
('a9993e364706816aba3e25717850c26c9cd0d89d', 'abc'),
|
||||
|
||||
# FIPS PUB 180-2, A.2 - "Multi-Block Message"
|
||||
('84983e441c3bd26ebaae4aa1f95129e5e54670f1',
|
||||
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
|
||||
|
||||
# FIPS PUB 180-2, A.3 - "Long Message"
|
||||
# ('34aa973cd4c4daa4f61eeb2bdbad27316534016f',
|
||||
# 'a' * 10**6,
|
||||
# '"a" * 10**6'),
|
||||
|
||||
# RFC 3174: Section 7.3, "TEST4" (multiple of 512 bits)
|
||||
('dea356a2cddd90c7a7ecedc5ebb563934f460452',
|
||||
'01234567' * 80,
|
||||
'"01234567" * 80'),
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import SHA1
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(SHA1, "SHA1", test_data,
|
||||
digest_size=20,
|
||||
oid="1.3.14.3.2.26")
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
65
modules/Crypto/SelfTest/Hash/test_SHA224.py
Normal file
65
modules/Crypto/SelfTest/Hash/test_SHA224.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/test_SHA224.py: Self-test for the SHA-224 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.SHA224"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
# Test vectors from various sources
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
|
||||
# RFC 3874: Section 3.1, "Test Vector #1
|
||||
('23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7', 'abc'),
|
||||
|
||||
# RFC 3874: Section 3.2, "Test Vector #2
|
||||
('75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
|
||||
|
||||
# RFC 3874: Section 3.3, "Test Vector #3
|
||||
('20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67', 'a' * 10**6, "'a' * 10**6"),
|
||||
|
||||
# Examples from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
|
||||
('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', ''),
|
||||
|
||||
('49b08defa65e644cbf8a2dd9270bdededabc741997d1dadd42026d7b',
|
||||
'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
|
||||
|
||||
('58911e7fccf2971a7d07f93162d8bd13568e71aa8fc86fc1fe9043d1',
|
||||
'Frank jagt im komplett verwahrlosten Taxi quer durch Bayern'),
|
||||
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import SHA224
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(SHA224, "SHA224", test_data,
|
||||
digest_size=28,
|
||||
oid='2.16.840.1.101.3.4.2.4')
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
96
modules/Crypto/SelfTest/Hash/test_SHA256.py
Normal file
96
modules/Crypto/SelfTest/Hash/test_SHA256.py
Normal file
@@ -0,0 +1,96 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/test_SHA256.py: Self-test for the SHA-256 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.SHA256"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
import unittest
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
class LargeSHA256Test(unittest.TestCase):
|
||||
def runTest(self):
|
||||
"""SHA256: 512/520 MiB test"""
|
||||
from Crypto.Hash import SHA256
|
||||
zeros = bchr(0x00) * (1024*1024)
|
||||
|
||||
h = SHA256.new(zeros)
|
||||
for i in xrange(511):
|
||||
h.update(zeros)
|
||||
|
||||
# This test vector is from PyCrypto's old testdata.py file.
|
||||
self.assertEqual('9acca8e8c22201155389f65abbf6bc9723edc7384ead80503839f49dcc56d767', h.hexdigest()) # 512 MiB
|
||||
|
||||
for i in xrange(8):
|
||||
h.update(zeros)
|
||||
|
||||
# This test vector is from PyCrypto's old testdata.py file.
|
||||
self.assertEqual('abf51ad954b246009dfe5a50ecd582fd5b8f1b8b27f30393853c3ef721e7fa6e', h.hexdigest()) # 520 MiB
|
||||
|
||||
def get_tests(config={}):
|
||||
# Test vectors from FIPS PUB 180-2
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
# FIPS PUB 180-2, B.1 - "One-Block Message"
|
||||
('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
|
||||
'abc'),
|
||||
|
||||
# FIPS PUB 180-2, B.2 - "Multi-Block Message"
|
||||
('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
|
||||
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
|
||||
|
||||
# FIPS PUB 180-2, B.3 - "Long Message"
|
||||
('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0',
|
||||
'a' * 10**6,
|
||||
'"a" * 10**6'),
|
||||
|
||||
# Test for an old PyCrypto bug.
|
||||
('f7fd017a3c721ce7ff03f3552c0813adcc48b7f33f07e5e2ba71e23ea393d103',
|
||||
'This message is precisely 55 bytes long, to test a bug.',
|
||||
'Length = 55 (mod 64)'),
|
||||
|
||||
# Example from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
|
||||
('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', ''),
|
||||
|
||||
('d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8',
|
||||
'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
|
||||
]
|
||||
|
||||
from Crypto.Hash import SHA256
|
||||
from common import make_hash_tests
|
||||
tests = make_hash_tests(SHA256, "SHA256", test_data,
|
||||
digest_size=32,
|
||||
oid="2.16.840.1.101.3.4.2.1")
|
||||
|
||||
if config.get('slow_tests'):
|
||||
tests += [LargeSHA256Test()]
|
||||
|
||||
return tests
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
63
modules/Crypto/SelfTest/Hash/test_SHA384.py
Normal file
63
modules/Crypto/SelfTest/Hash/test_SHA384.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/test_SHA.py: Self-test for the SHA-384 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.SHA384"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
# Test vectors from various sources
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
|
||||
# RFC 4634: Section Page 8.4, "Test 1"
|
||||
('cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7', 'abc'),
|
||||
|
||||
# RFC 4634: Section Page 8.4, "Test 2.2"
|
||||
('09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'),
|
||||
|
||||
# RFC 4634: Section Page 8.4, "Test 3"
|
||||
('9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985', 'a' * 10**6, "'a' * 10**6"),
|
||||
|
||||
# Taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
|
||||
('38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', ''),
|
||||
|
||||
# Example from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
|
||||
('71e8383a4cea32d6fd6877495db2ee353542f46fa44bc23100bca48f3366b84e809f0708e81041f427c6d5219a286677',
|
||||
'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
|
||||
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import SHA384
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(SHA384, "SHA384", test_data,
|
||||
digest_size=48,
|
||||
oid='2.16.840.1.101.3.4.2.2')
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
60
modules/Crypto/SelfTest/Hash/test_SHA512.py
Normal file
60
modules/Crypto/SelfTest/Hash/test_SHA512.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# SelfTest/Hash/test_SHA512.py: Self-test for the SHA-512 hash function
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Self-test suite for Crypto.Hash.SHA512"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
# Test vectors from various sources
|
||||
# This is a list of (expected_result, input[, description]) tuples.
|
||||
test_data = [
|
||||
|
||||
# RFC 4634: Section Page 8.4, "Test 1"
|
||||
('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'abc'),
|
||||
|
||||
# RFC 4634: Section Page 8.4, "Test 2.1"
|
||||
('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'),
|
||||
|
||||
# RFC 4634: Section Page 8.4, "Test 3"
|
||||
('e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b', 'a' * 10**6, "'a' * 10**6"),
|
||||
|
||||
# Taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
|
||||
('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', ''),
|
||||
|
||||
('af9ed2de700433b803240a552b41b5a472a6ef3fe1431a722b2063c75e9f07451f67a28e37d09cde769424c96aea6f8971389db9e1993d6c565c3c71b855723c', 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
|
||||
]
|
||||
|
||||
def get_tests(config={}):
|
||||
from Crypto.Hash import SHA512
|
||||
from common import make_hash_tests
|
||||
return make_hash_tests(SHA512, "SHA512", test_data,
|
||||
digest_size=64,
|
||||
oid="2.16.840.1.101.3.4.2.3")
|
||||
|
||||
if __name__ == '__main__':
|
||||
import unittest
|
||||
suite = lambda: unittest.TestSuite(get_tests())
|
||||
unittest.main(defaultTest='suite')
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
Reference in New Issue
Block a user