1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

tests: simplify hashlib tests, fix trezor.config test

This commit is contained in:
Pavol Rusnak 2017-12-16 01:24:40 +01:00
parent 79a3f84c3c
commit ea404b53ee
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
7 changed files with 138 additions and 146 deletions

View File

@ -1,6 +1,7 @@
from common import * from common import *
from trezor.crypto import random from trezor.crypto import random
from trezor.pin import pin_to_int
from trezor import config from trezor import config
@ -26,7 +27,7 @@ class TestConfig(unittest.TestCase):
def test_wipe(self): def test_wipe(self):
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
config.set(1, 1, b'hello') config.set(1, 1, b'hello')
config.set(1, 2, b'world') config.set(1, 2, b'world')
v0 = config.get(1, 1) v0 = config.get(1, 1)
@ -43,7 +44,7 @@ class TestConfig(unittest.TestCase):
for _ in range(128): for _ in range(128):
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
appid, key = random_entry() appid, key = random_entry()
value = random.bytes(16) value = random.bytes(16)
config.set(appid, key, value) config.set(appid, key, value)
@ -53,30 +54,30 @@ class TestConfig(unittest.TestCase):
config.set(appid, key, bytes()) config.set(appid, key, bytes())
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.change_pin('', 'xxx', None), False) self.assertEqual(config.change_pin(pin_to_int(''), pin_to_int('000'), None), False)
def test_change_pin(self): def test_change_pin(self):
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
config.set(PINAPP, PINKEY, 'xxx') config.set(PINAPP, PINKEY, b'value')
self.assertEqual(config.change_pin('xxx', 'yyy', None), False) self.assertEqual(config.change_pin(pin_to_int('000'), pin_to_int('666'), None), False)
self.assertEqual(config.change_pin('', 'xxx', None), True) self.assertEqual(config.change_pin(pin_to_int(''), pin_to_int('000'), None), True)
self.assertEqual(config.get(PINAPP, PINKEY), bytes()) self.assertEqual(config.get(PINAPP, PINKEY), bytes())
config.set(1, 1, b'value') config.set(1, 1, b'value')
config.init() config.init()
self.assertEqual(config.unlock('xxx', None), True) self.assertEqual(config.unlock(pin_to_int('000'), None), True)
config.change_pin('xxx', '', None) config.change_pin(pin_to_int('000'), pin_to_int(''), None)
config.init() config.init()
self.assertEqual(config.unlock('xxx', None), False) self.assertEqual(config.unlock(pin_to_int('000'), None), False)
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
self.assertEqual(config.get(1, 1), b'value') self.assertEqual(config.get(1, 1), b'value')
def test_set_get(self): def test_set_get(self):
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
for _ in range(32): for _ in range(32):
appid, key = random_entry() appid, key = random_entry()
value = random.bytes(128) value = random.bytes(128)
@ -87,7 +88,7 @@ class TestConfig(unittest.TestCase):
def test_compact(self): def test_compact(self):
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
appid, key = 1, 1 appid, key = 1, 1
for _ in range(259): for _ in range(259):
value = random.bytes(259) value = random.bytes(259)
@ -98,7 +99,7 @@ class TestConfig(unittest.TestCase):
def test_get_default(self): def test_get_default(self):
config.init() config.init()
config.wipe() config.wipe()
self.assertEqual(config.unlock('', None), True) self.assertEqual(config.unlock(pin_to_int(''), None), True)
for _ in range(128): for _ in range(128):
appid, key = random_entry() appid, key = random_entry()
value = config.get(appid, key) value = config.get(appid, key)

View File

@ -5,44 +5,26 @@ from trezor.crypto import hashlib
class TestCryptoRipemd160(unittest.TestCase): class TestCryptoRipemd160(unittest.TestCase):
# vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html # vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
vectors = [
(b'', '9c1185a5c5e9fc54612808977ee8f548b2258d31'),
(b'a', '0bdc9d2d256b3ee9daae347be6f4dc835a467ffe'),
(b'abc', '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'),
(b'message digest', '5d0689ef49d2fae572b881b123a85ffa21595f36'),
(b'abcdefghijklmnopqrstuvwxyz', 'f71c27109c692c1b56bbdceb5b9d2865b3708dbc'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '12a053384a9c0c88e405a06c27dcf49ada62eb2b'),
(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 'b0e20b6e3116640286ed3a87a5713079b21f5189'),
(b'12345678901234567890123456789012345678901234567890123456789012345678901234567890', '9b752e45573d4b39f4dbd3323cab82bf63326bfb'),
]
def test_digest(self): def test_digest(self):
self.assertEqual(hashlib.ripemd160(b'').digest(), unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31')) for b, d in self.vectors:
self.assertEqual(hashlib.ripemd160(b'a').digest(), unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe')) self.assertEqual(hashlib.ripemd160(b).digest(), unhexlify(d))
self.assertEqual(hashlib.ripemd160(b'abc').digest(), unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'))
self.assertEqual(hashlib.ripemd160(b'message digest').digest(), unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36'))
self.assertEqual(hashlib.ripemd160(b'abcdefghijklmnopqrstuvwxyz').digest(), unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc'))
self.assertEqual(hashlib.ripemd160(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b'))
self.assertEqual(hashlib.ripemd160(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').digest(), unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189'))
self.assertEqual(hashlib.ripemd160(b'12345678901234567890123456789012345678901234567890123456789012345678901234567890').digest(), unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb'))
def test_update(self): def test_update(self):
x = hashlib.ripemd160() for b, d in self.vectors:
self.assertEqual(x.digest(), unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31')) x = hashlib.ripemd160()
x.update(b)
x = hashlib.ripemd160() self.assertEqual(x.digest(), unhexlify(d))
x.update(b'a')
self.assertEqual(x.digest(), unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe'))
x = hashlib.ripemd160()
x.update(b'abc')
self.assertEqual(x.digest(), unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'))
x = hashlib.ripemd160()
x.update(b'message digest')
self.assertEqual(x.digest(), unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36'))
x = hashlib.ripemd160()
x.update(b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(x.digest(), unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc'))
x = hashlib.ripemd160()
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
self.assertEqual(x.digest(), unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b'))
x = hashlib.ripemd160()
x.update(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
self.assertEqual(x.digest(), unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189'))
x = hashlib.ripemd160() x = hashlib.ripemd160()
for i in range(8): for i in range(8):

View File

@ -5,28 +5,23 @@ from trezor.crypto import hashlib
class TestCryptoSha1(unittest.TestCase): class TestCryptoSha1(unittest.TestCase):
# vectors from http://www.di-mgt.com.au/sha_testvectors.html # vectors from http://www.di-mgt.com.au/sha_testvectors.html
vectors = [
(b'', 'da39a3ee5e6b4b0d3255bfef95601890afd80709'),
(b'abc', 'a9993e364706816aba3e25717850c26c9cd0d89d'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '84983e441c3bd26ebaae4aa1f95129e5e54670f1'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', 'a49b2446a02c645bf419f995b67091253a04a259')
]
def test_digest(self): def test_digest(self):
self.assertEqual(hashlib.sha1(b'').digest(), unhexlify('da39a3ee5e6b4b0d3255bfef95601890afd80709')) for b, d in self.vectors:
self.assertEqual(hashlib.sha1(b'abc').digest(), unhexlify('a9993e364706816aba3e25717850c26c9cd0d89d')) self.assertEqual(hashlib.sha1(b).digest(), unhexlify(d))
self.assertEqual(hashlib.sha1(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('84983e441c3bd26ebaae4aa1f95129e5e54670f1'))
self.assertEqual(hashlib.sha1(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('a49b2446a02c645bf419f995b67091253a04a259'))
def test_update(self): def test_update(self):
x = hashlib.sha1() for b, d in self.vectors:
self.assertEqual(x.digest(), unhexlify('da39a3ee5e6b4b0d3255bfef95601890afd80709')) x = hashlib.sha1()
x.update(b)
x = hashlib.sha1() self.assertEqual(x.digest(), unhexlify(d))
x.update(b'abc')
self.assertEqual(x.digest(), unhexlify('a9993e364706816aba3e25717850c26c9cd0d89d'))
x = hashlib.sha1()
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
self.assertEqual(x.digest(), unhexlify('84983e441c3bd26ebaae4aa1f95129e5e54670f1'))
x = hashlib.sha1()
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu')
self.assertEqual(x.digest(), unhexlify('a49b2446a02c645bf419f995b67091253a04a259'))
x = hashlib.sha1() x = hashlib.sha1()
for i in range(1000000): for i in range(1000000):

View File

@ -5,28 +5,22 @@ from trezor.crypto import hashlib
class TestCryptoSha256(unittest.TestCase): class TestCryptoSha256(unittest.TestCase):
# vectors from http://www.di-mgt.com.au/sha_testvectors.html # vectors from http://www.di-mgt.com.au/sha_testvectors.html
vectors = [
(b'', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'),
(b'abc', 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', 'cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1'),
]
def test_digest(self): def test_digest(self):
self.assertEqual(hashlib.sha256(b'').digest(), unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')) for b, d in self.vectors:
self.assertEqual(hashlib.sha256(b'abc').digest(), unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')) self.assertEqual(hashlib.sha256(b).digest(), unhexlify(d))
self.assertEqual(hashlib.sha256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'))
self.assertEqual(hashlib.sha256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1'))
def test_update(self): def test_update(self):
x = hashlib.sha256() for b, d in self.vectors:
self.assertEqual(x.digest(), unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')) x = hashlib.sha256()
x.update(b)
x = hashlib.sha256() self.assertEqual(x.digest(), unhexlify(d))
x.update(b'abc')
self.assertEqual(x.digest(), unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'))
x = hashlib.sha256()
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
self.assertEqual(x.digest(), unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'))
x = hashlib.sha256()
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu')
self.assertEqual(x.digest(), unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1'))
x = hashlib.sha256() x = hashlib.sha256()
for i in range(1000000): for i in range(1000000):

View File

@ -5,34 +5,33 @@ from trezor.crypto import hashlib
class TestCryptoSha3_256(unittest.TestCase): class TestCryptoSha3_256(unittest.TestCase):
# vectors from http://www.di-mgt.com.au/sha_testvectors.html # vectors from http://www.di-mgt.com.au/sha_testvectors.html
vectors = [
(b'', 'a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'),
(b'abc', '3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', '916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18'),
]
vectors_keccak = [
(b'', 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'),
(b'abc', '4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '45d3b367a6904e6e8d502ee04999a7c27647f91fa845d456525fd352ae3d7371'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', 'f519747ed599024f3882238e5ab43960132572b7345fbeb9a90769dafd21ad67'),
]
def test_digest(self): def test_digest(self):
self.assertEqual(hashlib.sha3_256(b'').digest(), unhexlify('a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a')) for b, d in self.vectors:
self.assertEqual(hashlib.sha3_256(b'abc').digest(), unhexlify('3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532')) self.assertEqual(hashlib.sha3_256(b).digest(), unhexlify(d))
self.assertEqual(hashlib.sha3_256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376'))
self.assertEqual(hashlib.sha3_256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18'))
def test_digest_keccak(self): def test_digest_keccak(self):
self.assertEqual(hashlib.sha3_256(b'').digest(True), unhexlify('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')) for b, d in self.vectors_keccak:
self.assertEqual(hashlib.sha3_256(b'abc').digest(True), unhexlify('4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45')) self.assertEqual(hashlib.sha3_256(b).digest(True), unhexlify(d))
self.assertEqual(hashlib.sha3_256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(True), unhexlify('45d3b367a6904e6e8d502ee04999a7c27647f91fa845d456525fd352ae3d7371'))
self.assertEqual(hashlib.sha3_256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(True), unhexlify('f519747ed599024f3882238e5ab43960132572b7345fbeb9a90769dafd21ad67'))
def test_update(self): def test_update(self):
x = hashlib.sha3_256() for b, d in self.vectors:
self.assertEqual(x.digest(), unhexlify('a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a')) x = hashlib.sha3_256()
x.update(b)
x = hashlib.sha3_256() self.assertEqual(x.digest(), unhexlify(d))
x.update(b'abc')
self.assertEqual(x.digest(), unhexlify('3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532'))
x = hashlib.sha3_256()
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
self.assertEqual(x.digest(), unhexlify('41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376'))
x = hashlib.sha3_256()
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu')
self.assertEqual(x.digest(), unhexlify('916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18'))
x = hashlib.sha3_256() x = hashlib.sha3_256()
for i in range(1000000): for i in range(1000000):
@ -46,6 +45,12 @@ class TestCryptoSha3_256(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify('ecbbc42cbf296603acb2c6bc0410ef4378bafb24b710357f12df607758b33e2b')) self.assertEqual(x.digest(), unhexlify('ecbbc42cbf296603acb2c6bc0410ef4378bafb24b710357f12df607758b33e2b'))
''' '''
def test_update_keccak(self):
for b, d in self.vectors_keccak:
x = hashlib.sha3_256()
x.update(b)
self.assertEqual(x.digest(True), unhexlify(d))
def test_digest_multi(self): def test_digest_multi(self):
x = hashlib.sha3_256() x = hashlib.sha3_256()
d0 = x.digest() d0 = x.digest()
@ -54,5 +59,13 @@ class TestCryptoSha3_256(unittest.TestCase):
self.assertEqual(d0, d1) self.assertEqual(d0, d1)
self.assertEqual(d0, d2) self.assertEqual(d0, d2)
def test_digest_multi(self):
x = hashlib.sha3_256()
d0 = x.digest(True)
d1 = x.digest(True)
d2 = x.digest(True)
self.assertEqual(d0, d1)
self.assertEqual(d0, d2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -5,34 +5,33 @@ from trezor.crypto import hashlib
class TestCryptoSha3_512(unittest.TestCase): class TestCryptoSha3_512(unittest.TestCase):
# vectors from http://www.di-mgt.com.au/sha_testvectors.html # vectors from http://www.di-mgt.com.au/sha_testvectors.html
vectors = [
(b'', 'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'),
(b'abc', 'b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '04a371e84ecfb5b8b77cb48610fca8182dd457ce6f326a0fd3d7ec2f1e91636dee691fbe0c985302ba1b0d8dc78c086346b533b49c030d99a27daf1139d6e75e'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', 'afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185'),
]
vectors_keccak = [
(b'', '0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'),
(b'abc', '18587dc2ea106b9a1563e32b3312421ca164c7f1f07bc922a9c83d77cea3a1e5d0c69910739025372dc14ac9642629379540c17e2a65b19d77aa511a9d00bb96'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '6aa6d3669597df6d5a007b00d09c20795b5c4218234e1698a944757a488ecdc09965435d97ca32c3cfed7201ff30e070cd947f1fc12b9d9214c467d342bcba5d'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', 'ac2fb35251825d3aa48468a9948c0a91b8256f6d97d8fa4160faff2dd9dfcc24f3f1db7a983dad13d53439ccac0b37e24037e7b95f80f59f37a2f683c4ba4682'),
]
def test_digest(self): def test_digest(self):
self.assertEqual(hashlib.sha3_512(b'').digest(), unhexlify('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26')) for b, d in self.vectors:
self.assertEqual(hashlib.sha3_512(b'abc').digest(), unhexlify('b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0')) self.assertEqual(hashlib.sha3_512(b).digest(), unhexlify(d))
self.assertEqual(hashlib.sha3_512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('04a371e84ecfb5b8b77cb48610fca8182dd457ce6f326a0fd3d7ec2f1e91636dee691fbe0c985302ba1b0d8dc78c086346b533b49c030d99a27daf1139d6e75e'))
self.assertEqual(hashlib.sha3_512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185'))
def test_digest_keccak(self): def test_digest_keccak(self):
self.assertEqual(hashlib.sha3_512(b'').digest(True), unhexlify('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e')) for b, d in self.vectors_keccak:
self.assertEqual(hashlib.sha3_512(b'abc').digest(True), unhexlify('18587dc2ea106b9a1563e32b3312421ca164c7f1f07bc922a9c83d77cea3a1e5d0c69910739025372dc14ac9642629379540c17e2a65b19d77aa511a9d00bb96')) self.assertEqual(hashlib.sha3_512(b).digest(True), unhexlify(d))
self.assertEqual(hashlib.sha3_512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(True), unhexlify('6aa6d3669597df6d5a007b00d09c20795b5c4218234e1698a944757a488ecdc09965435d97ca32c3cfed7201ff30e070cd947f1fc12b9d9214c467d342bcba5d'))
self.assertEqual(hashlib.sha3_512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(True), unhexlify('ac2fb35251825d3aa48468a9948c0a91b8256f6d97d8fa4160faff2dd9dfcc24f3f1db7a983dad13d53439ccac0b37e24037e7b95f80f59f37a2f683c4ba4682'))
def test_update(self): def test_update(self):
x = hashlib.sha3_512() for b, d in self.vectors:
self.assertEqual(x.digest(), unhexlify('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26')) x = hashlib.sha3_512()
x.update(b)
x = hashlib.sha3_512() self.assertEqual(x.digest(), unhexlify(d))
x.update(b'abc')
self.assertEqual(x.digest(), unhexlify('b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0'))
x = hashlib.sha3_512()
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
self.assertEqual(x.digest(), unhexlify('04a371e84ecfb5b8b77cb48610fca8182dd457ce6f326a0fd3d7ec2f1e91636dee691fbe0c985302ba1b0d8dc78c086346b533b49c030d99a27daf1139d6e75e'))
x = hashlib.sha3_512()
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu')
self.assertEqual(x.digest(), unhexlify('afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185'))
x = hashlib.sha3_512() x = hashlib.sha3_512()
for i in range(1000000): for i in range(1000000):
@ -46,6 +45,12 @@ class TestCryptoSha3_512(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify('235ffd53504ef836a1342b488f483b396eabbfe642cf78ee0d31feec788b23d0d18d5c339550dd5958a500d4b95363da1b5fa18affc1bab2292dc63b7d85097c')) self.assertEqual(x.digest(), unhexlify('235ffd53504ef836a1342b488f483b396eabbfe642cf78ee0d31feec788b23d0d18d5c339550dd5958a500d4b95363da1b5fa18affc1bab2292dc63b7d85097c'))
''' '''
def test_update_keccak(self):
for b, d in self.vectors_keccak:
x = hashlib.sha3_512()
x.update(b)
self.assertEqual(x.digest(True), unhexlify(d))
def test_digest_multi(self): def test_digest_multi(self):
x = hashlib.sha3_512() x = hashlib.sha3_512()
d0 = x.digest() d0 = x.digest()
@ -54,5 +59,13 @@ class TestCryptoSha3_512(unittest.TestCase):
self.assertEqual(d0, d1) self.assertEqual(d0, d1)
self.assertEqual(d0, d2) self.assertEqual(d0, d2)
def test_digest_multi(self):
x = hashlib.sha3_512()
d0 = x.digest(True)
d1 = x.digest(True)
d2 = x.digest(True)
self.assertEqual(d0, d1)
self.assertEqual(d0, d2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -5,28 +5,22 @@ from trezor.crypto import hashlib
class TestCryptoSha512(unittest.TestCase): class TestCryptoSha512(unittest.TestCase):
# vectors from http://www.di-mgt.com.au/sha_testvectors.html # vectors from http://www.di-mgt.com.au/sha_testvectors.html
vectors = [
(b'', 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
(b'abc', 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f'),
(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445'),
(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'),
]
def test_digest(self): def test_digest(self):
self.assertEqual(hashlib.sha512(b'').digest(), unhexlify('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')) for b, d in self.vectors:
self.assertEqual(hashlib.sha512(b'abc').digest(), unhexlify('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f')) self.assertEqual(hashlib.sha512(b).digest(), unhexlify(d))
self.assertEqual(hashlib.sha512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445'))
self.assertEqual(hashlib.sha512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'))
def test_update(self): def test_update(self):
x = hashlib.sha512() for b, d in self.vectors:
self.assertEqual(x.digest(), unhexlify('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')) x = hashlib.sha512()
x.update(b)
x = hashlib.sha512() self.assertEqual(x.digest(), unhexlify(d))
x.update(b'abc')
self.assertEqual(x.digest(), unhexlify('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f'))
x = hashlib.sha512()
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
self.assertEqual(x.digest(), unhexlify('204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445'))
x = hashlib.sha512()
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu')
self.assertEqual(x.digest(), unhexlify('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'))
x = hashlib.sha512() x = hashlib.sha512()
for i in range(1000000): for i in range(1000000):