diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-base58.h b/extmod/modtrezorcrypto/modtrezorcrypto-base58.h index 334fb2100f..dbf8410aac 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-base58.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-base58.h @@ -14,6 +14,7 @@ typedef struct _mp_obj_Base58_t { // def Base58.__init__(self) STATIC mp_obj_t mod_TrezorCrypto_Base58_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Base58_t *o = m_new_obj(mp_obj_Base58_t); o->base.type = type; return MP_OBJ_FROM_PTR(o); diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h b/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h index 632a7404c9..88182a78fb 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h @@ -18,21 +18,18 @@ typedef struct _mp_obj_Ripemd160_t { mbedtls_ripemd160_context ctx; } mp_obj_Ripemd160_t; +STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_update(mp_obj_t self, mp_obj_t data); + // def Ripemd160.__init__(self, data: bytes = None) STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Ripemd160_t *o = m_new_obj(mp_obj_Ripemd160_t); o->base.type = type; mbedtls_ripemd160_init(&(o->ctx)); mbedtls_ripemd160_starts(&(o->ctx)); // constructor called with bytes/str as first parameter if (n_args == 1) { - if (!MP_OBJ_IS_STR_OR_BYTES(args[0])) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid argument")); - } - GET_STR_DATA_LEN(args[0], data, datalen); - mbedtls_ripemd160_update(&(o->ctx), data, datalen); - } else if (n_args != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid arguments")); + mod_TrezorCrypto_Ripemd160_update(MP_OBJ_FROM_PTR(o), args[0]); } return MP_OBJ_FROM_PTR(o); } @@ -60,11 +57,20 @@ STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160_digest_obj, mod_TrezorCrypto_Ripemd160_digest); +// def Ripemd160.__del__(self) -> None +STATIC mp_obj_t mod_TrezorCrypto_Ripemd160___del__(mp_obj_t self) { + mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self); + mbedtls_ripemd160_free(&(o->ctx)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160___del___obj, mod_TrezorCrypto_Ripemd160___del__); + // Ripemd160 stuff STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Ripemd160_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160_digest_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160___del___obj) }, { MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_RIPEMD160_BLOCK_SIZE) }, { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_RIPEMD160_DIGEST_SIZE) }, }; diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h index 16f66a35b2..b9e8e139a7 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h @@ -18,21 +18,18 @@ typedef struct _mp_obj_Sha256_t { mbedtls_sha256_context ctx; } mp_obj_Sha256_t; +STATIC mp_obj_t mod_TrezorCrypto_Sha256_update(mp_obj_t self, mp_obj_t data); + // def Sha256.__init__(self, data: bytes = None) STATIC mp_obj_t mod_TrezorCrypto_Sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Sha256_t *o = m_new_obj(mp_obj_Sha256_t); o->base.type = type; mbedtls_sha256_init(&(o->ctx)); mbedtls_sha256_starts(&(o->ctx), 0); // constructor called with bytes/str as first parameter if (n_args == 1) { - if (!MP_OBJ_IS_STR_OR_BYTES(args[0])) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid argument")); - } - GET_STR_DATA_LEN(args[0], data, datalen); - mbedtls_sha256_update(&(o->ctx), data, datalen); - } else if (n_args != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid arguments")); + mod_TrezorCrypto_Sha256_update(MP_OBJ_FROM_PTR(o), args[0]); } return MP_OBJ_FROM_PTR(o); } @@ -60,11 +57,21 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha256_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha256_digest_obj, mod_TrezorCrypto_Sha256_digest); +// def Sha256.__del__(self) -> None +STATIC mp_obj_t mod_TrezorCrypto_Sha256___del__(mp_obj_t self) { + mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self); + mbedtls_sha256_free(&(o->ctx)); + printf("yay!\n"); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha256___del___obj, mod_TrezorCrypto_Sha256___del__); + // Sha256 stuff STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha256_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_digest_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Sha256___del___obj) }, { MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA256_BLOCK_SIZE) }, { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA256_DIGEST_SIZE) }, }; diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h index e9f43b1a41..6aff396be0 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h @@ -18,21 +18,17 @@ typedef struct _mp_obj_Sha512_t { mbedtls_sha512_context ctx; } mp_obj_Sha512_t; +STATIC mp_obj_t mod_TrezorCrypto_Sha512_update(mp_obj_t self, mp_obj_t data); + // def Sha512.__init__(self, data: bytes = None) STATIC mp_obj_t mod_TrezorCrypto_Sha512_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Sha512_t *o = m_new_obj(mp_obj_Sha512_t); o->base.type = type; mbedtls_sha512_init(&(o->ctx)); mbedtls_sha512_starts(&(o->ctx), 0); - // constructor called with bytes/str as first parameter if (n_args == 1) { - if (!MP_OBJ_IS_STR_OR_BYTES(args[0])) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid argument")); - } - GET_STR_DATA_LEN(args[0], data, datalen); - mbedtls_sha512_update(&(o->ctx), data, datalen); - } else if (n_args != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid arguments")); + mod_TrezorCrypto_Sha512_update(MP_OBJ_FROM_PTR(o), args[0]); } return MP_OBJ_FROM_PTR(o); } @@ -60,11 +56,20 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha512_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha512_digest_obj, mod_TrezorCrypto_Sha512_digest); +// def Sha512.__del__(self) -> None +STATIC mp_obj_t mod_TrezorCrypto_Sha512___del__(mp_obj_t self) { + mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self); + mbedtls_sha512_free(&(o->ctx)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha512___del___obj, mod_TrezorCrypto_Sha512___del__); + // Sha512 stuff STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha512_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha512_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha512_digest_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Sha512___del___obj) }, { MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA512_BLOCK_SIZE) }, { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA512_DIGEST_SIZE) }, }; diff --git a/extmod/modtrezormsg/modtrezormsg.c b/extmod/modtrezormsg/modtrezormsg.c index f798aed989..7f17f17171 100644 --- a/extmod/modtrezormsg/modtrezormsg.c +++ b/extmod/modtrezormsg/modtrezormsg.c @@ -34,6 +34,7 @@ typedef struct _mp_obj_Msg_t { // def Msg.__init__(self) STATIC mp_obj_t mod_TrezorMsg_Msg_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Msg_t *o = m_new_obj(mp_obj_Msg_t); o->base.type = type; return MP_OBJ_FROM_PTR(o); diff --git a/extmod/modtrezorprotobuf/modtrezorprotobuf.c b/extmod/modtrezorprotobuf/modtrezorprotobuf.c index 61c6e7d0f7..69ba62e2a3 100644 --- a/extmod/modtrezorprotobuf/modtrezorprotobuf.c +++ b/extmod/modtrezorprotobuf/modtrezorprotobuf.c @@ -22,6 +22,7 @@ typedef struct _mp_obj_Protobuf_t { // def Protobuf.__init__(self) STATIC mp_obj_t mod_TrezorProtobuf_Protobuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Protobuf_t *o = m_new_obj(mp_obj_Protobuf_t); o->base.type = type; return MP_OBJ_FROM_PTR(o); diff --git a/extmod/modtrezorui/modtrezorui-display.h b/extmod/modtrezorui/modtrezorui-display.h index f87f7e5456..65e55e7ee6 100644 --- a/extmod/modtrezorui/modtrezorui-display.h +++ b/extmod/modtrezorui/modtrezorui-display.h @@ -158,6 +158,7 @@ typedef struct _mp_obj_Display_t { // def Display.__init__(self) STATIC mp_obj_t mod_TrezorUi_Display_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); display_init(); mp_obj_Display_t *o = m_new_obj(mp_obj_Display_t); o->base.type = type; diff --git a/extmod/modtrezorui/modtrezorui-touch.h b/extmod/modtrezorui/modtrezorui-touch.h index 0a3c206e79..eaa0fc38e8 100644 --- a/extmod/modtrezorui/modtrezorui-touch.h +++ b/extmod/modtrezorui/modtrezorui-touch.h @@ -38,6 +38,7 @@ typedef struct _mp_obj_Touch_t { // def Touch.__init__(self) STATIC mp_obj_t mod_TrezorUi_Touch_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Touch_t *o = m_new_obj(mp_obj_Touch_t); o->base.type = type; return MP_OBJ_FROM_PTR(o); diff --git a/src/tests/test_crypto_base58.py b/src/tests/test_crypto_base58.py index bc29c11d2d..6544b848de 100644 --- a/src/tests/test_crypto_base58.py +++ b/src/tests/test_crypto_base58.py @@ -4,7 +4,7 @@ sys.path.append('../lib') import unittest import trezor.utils -import trezor.crypto.base58 +from trezor.crypto import base58 class TestCryptoBase58(unittest.TestCase): @@ -64,11 +64,11 @@ class TestCryptoBase58(unittest.TestCase): def test_decode_check(self): for a, b in self.vectors: - self.assertEqual(trezor.crypto.base58.decode_check(b), trezor.utils.unhexlify(a)) + self.assertEqual(base58.decode_check(b), trezor.utils.unhexlify(a)) def test_encode_check(self): for a, b in self.vectors: - self.assertEqual(trezor.crypto.base58.encode_check(trezor.utils.unhexlify(a)), b) + self.assertEqual(base58.encode_check(trezor.utils.unhexlify(a)), b) if __name__ == '__main__': unittest.main() diff --git a/src/tests/test_crypto_hmac.py b/src/tests/test_crypto_hmac.py index 33457579da..e8ebba1987 100644 --- a/src/tests/test_crypto_hmac.py +++ b/src/tests/test_crypto_hmac.py @@ -4,8 +4,8 @@ sys.path.append('../lib') import unittest import trezor.utils -import trezor.crypto.hash -import trezor.crypto.hmac +from trezor.crypto import hashlib +from trezor.crypto import hmac class TestCryptoHmac(unittest.TestCase): @@ -16,65 +16,65 @@ class TestCryptoHmac(unittest.TestCase): # case 1 key = b'\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b' msg = b'Hi There' - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha256).digest(), trezor.utils.unhexlify('b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7')) - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha512).digest(), trezor.utils.unhexlify('87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854')) + self.assertEqual(hmac.new(key, msg, hashlib.sha256).digest(), trezor.utils.unhexlify('b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7')) + self.assertEqual(hmac.new(key, msg, hashlib.sha512).digest(), trezor.utils.unhexlify('87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854')) # case 2 key = b'Jefe' msg = b'what do ya want for nothing?' - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha256).digest(), trezor.utils.unhexlify('5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843')) - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha512).digest(), trezor.utils.unhexlify('164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737')) + self.assertEqual(hmac.new(key, msg, hashlib.sha256).digest(), trezor.utils.unhexlify('5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843')) + self.assertEqual(hmac.new(key, msg, hashlib.sha512).digest(), trezor.utils.unhexlify('164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737')) # case 3 key = b'\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa' msg = b'\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd' - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha256).digest(), trezor.utils.unhexlify('773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe')) - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha512).digest(), trezor.utils.unhexlify('fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb')) + self.assertEqual(hmac.new(key, msg, hashlib.sha256).digest(), trezor.utils.unhexlify('773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe')) + self.assertEqual(hmac.new(key, msg, hashlib.sha512).digest(), trezor.utils.unhexlify('fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb')) # case 4 key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19' msg = b'\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd' - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha256).digest(), trezor.utils.unhexlify('82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b')) - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha512).digest(), trezor.utils.unhexlify('b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd')) + self.assertEqual(hmac.new(key, msg, hashlib.sha256).digest(), trezor.utils.unhexlify('82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b')) + self.assertEqual(hmac.new(key, msg, hashlib.sha512).digest(), trezor.utils.unhexlify('b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd')) # case 6 key = bytes([0xAA] * 131) msg = b'Test Using Larger Than Block-Size Key - Hash Key First' - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha256).digest(), trezor.utils.unhexlify('60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54')) - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha512).digest(), trezor.utils.unhexlify('80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598')) + self.assertEqual(hmac.new(key, msg, hashlib.sha256).digest(), trezor.utils.unhexlify('60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54')) + self.assertEqual(hmac.new(key, msg, hashlib.sha512).digest(), trezor.utils.unhexlify('80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598')) # case 7 key = bytes([0xAA] * 131) msg = b'This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.' - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha256).digest(), trezor.utils.unhexlify('9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2')) - self.assertEqual(trezor.crypto.hmac.new(key, msg, trezor.crypto.hash.sha512).digest(), trezor.utils.unhexlify('e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58')) + self.assertEqual(hmac.new(key, msg, hashlib.sha256).digest(), trezor.utils.unhexlify('9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2')) + self.assertEqual(hmac.new(key, msg, hashlib.sha512).digest(), trezor.utils.unhexlify('e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58')) def test_update(self): # case 3 key = b'\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa' - x = trezor.crypto.hmac.new(key, b'', trezor.crypto.hash.sha256) + x = hmac.new(key, b'', hashlib.sha256) for i in range(50): x.update(b'\xdd') self.assertEqual(x.digest(), trezor.utils.unhexlify('773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe')) - x = trezor.crypto.hmac.new(key, b'', trezor.crypto.hash.sha512) + x = hmac.new(key, b'', hashlib.sha512) for i in range(50): x.update(b'\xdd') self.assertEqual(x.digest(), trezor.utils.unhexlify('fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb')) # case 4 key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19' - x = trezor.crypto.hmac.new(key, b'', trezor.crypto.hash.sha256) + x = hmac.new(key, b'', hashlib.sha256) for i in range(50): x.update(b'\xcd') self.assertEqual(x.digest(), trezor.utils.unhexlify('82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b')) - x = trezor.crypto.hmac.new(key, b'', trezor.crypto.hash.sha512) + x = hmac.new(key, b'', hashlib.sha512) for i in range(50): x.update(b'\xcd') self.assertEqual(x.digest(), trezor.utils.unhexlify('b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd')) def test_digest_multi(self): - x = trezor.crypto.hmac.new(b'', b'', trezor.crypto.hash.sha256) + x = hmac.new(b'', b'', hashlib.sha256) d0 = x.digest() d1 = x.digest() d2 = x.digest() diff --git a/src/tests/test_crypto_pbkdf2.py b/src/tests/test_crypto_pbkdf2.py index 197a8baeee..a465a4236c 100644 --- a/src/tests/test_crypto_pbkdf2.py +++ b/src/tests/test_crypto_pbkdf2.py @@ -4,9 +4,8 @@ sys.path.append('../lib') import unittest import trezor.utils -import trezor.crypto.hash -import trezor.crypto.hmac -import trezor.crypto.pbkdf2 +from trezor.crypto import hashlib +from trezor.crypto import hmac class TestCryptoPbkdf2(unittest.TestCase): @@ -14,31 +13,31 @@ class TestCryptoPbkdf2(unittest.TestCase): def test_hmac_sha256(self): P = b'password' S = b'salt' - k = trezor.crypto.pbkdf2.new(P, S, 1, trezor.crypto.hash.sha256, trezor.crypto.hmac).read(32) - self.assertEqual(k, trezor.utils.unhexlify('120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b')) - k = trezor.crypto.pbkdf2.new(P, S, 2, trezor.crypto.hash.sha256, trezor.crypto.hmac).read(32) - self.assertEqual(k, trezor.utils.unhexlify('ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43')) - k = trezor.crypto.pbkdf2.new(P, S, 4096, trezor.crypto.hash.sha256, trezor.crypto.hmac).read(32) - self.assertEqual(k, trezor.utils.unhexlify('c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a')) + dk = hashlib.pbkdf2_hmac('sha256', P, S, 1) + self.assertEqual(dk, trezor.utils.unhexlify('120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b')) + dk = hashlib.pbkdf2_hmac('sha256', P, S, 2) + self.assertEqual(dk, trezor.utils.unhexlify('ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43')) + dk = hashlib.pbkdf2_hmac('sha256', P, S, 4096) + self.assertEqual(dk, trezor.utils.unhexlify('c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a')) P = b'passwordPASSWORDpassword' S = b'saltSALTsaltSALTsaltSALTsaltSALTsalt' - k = trezor.crypto.pbkdf2.new(P, S, 4096, trezor.crypto.hash.sha256, trezor.crypto.hmac).read(40) - self.assertEqual(k, trezor.utils.unhexlify('348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9')) + dk = hashlib.pbkdf2_hmac('sha256', P, S, 4096, 40) + self.assertEqual(dk, trezor.utils.unhexlify('348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9')) # vectors from https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors def test_hmac_sha512(self): P = b'password' S = b'salt' - k = trezor.crypto.pbkdf2.new(P, S, 1, trezor.crypto.hash.sha512, trezor.crypto.hmac).read(64) - self.assertEqual(k, trezor.utils.unhexlify('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce')) - k = trezor.crypto.pbkdf2.new(P, S, 2, trezor.crypto.hash.sha512, trezor.crypto.hmac).read(64) - self.assertEqual(k, trezor.utils.unhexlify('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e')) - k = trezor.crypto.pbkdf2.new(P, S, 4096, trezor.crypto.hash.sha512, trezor.crypto.hmac).read(64) - self.assertEqual(k, trezor.utils.unhexlify('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5')) + dk = hashlib.pbkdf2_hmac('sha512', P, S, 1) + self.assertEqual(dk, trezor.utils.unhexlify('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce')) + dk = hashlib.pbkdf2_hmac('sha512', P, S, 2) + self.assertEqual(dk, trezor.utils.unhexlify('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e')) + dk = hashlib.pbkdf2_hmac('sha512', P, S, 4096) + self.assertEqual(dk, trezor.utils.unhexlify('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5')) P = b'passwordPASSWORDpassword' S = b'saltSALTsaltSALTsaltSALTsaltSALTsalt' - k = trezor.crypto.pbkdf2.new(P, S, 4096, trezor.crypto.hash.sha512, trezor.crypto.hmac).read(64) - self.assertEqual(k, trezor.utils.unhexlify('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8')) + dk = hashlib.pbkdf2_hmac('sha512', P, S, 4096) + self.assertEqual(dk, trezor.utils.unhexlify('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8')) if __name__ == '__main__': unittest.main() diff --git a/src/tests/test_crypto_ripemd160.py b/src/tests/test_crypto_ripemd160.py index 359b088cab..f562abd194 100644 --- a/src/tests/test_crypto_ripemd160.py +++ b/src/tests/test_crypto_ripemd160.py @@ -4,57 +4,57 @@ sys.path.append('../lib') import unittest import trezor.utils -import trezor.crypto.hash +from trezor.crypto import hashlib class TestCryptoRipemd160(unittest.TestCase): # vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html def test_digest(self): - self.assertEqual(trezor.crypto.hash.ripemd160(b'').digest(), trezor.utils.unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'a').digest(), trezor.utils.unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'abc').digest(), trezor.utils.unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'message digest').digest(), trezor.utils.unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'abcdefghijklmnopqrstuvwxyz').digest(), trezor.utils.unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), trezor.utils.unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').digest(), trezor.utils.unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189')) - self.assertEqual(trezor.crypto.hash.ripemd160(b'12345678901234567890123456789012345678901234567890123456789012345678901234567890').digest(), trezor.utils.unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb')) + self.assertEqual(hashlib.ripemd160(b'').digest(), trezor.utils.unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31')) + self.assertEqual(hashlib.ripemd160(b'a').digest(), trezor.utils.unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe')) + self.assertEqual(hashlib.ripemd160(b'abc').digest(), trezor.utils.unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc')) + self.assertEqual(hashlib.ripemd160(b'message digest').digest(), trezor.utils.unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36')) + self.assertEqual(hashlib.ripemd160(b'abcdefghijklmnopqrstuvwxyz').digest(), trezor.utils.unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc')) + self.assertEqual(hashlib.ripemd160(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), trezor.utils.unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b')) + self.assertEqual(hashlib.ripemd160(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').digest(), trezor.utils.unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189')) + self.assertEqual(hashlib.ripemd160(b'12345678901234567890123456789012345678901234567890123456789012345678901234567890').digest(), trezor.utils.unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb')) def test_update(self): - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() self.assertEqual(x.digest(), trezor.utils.unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() x.update(b'a') self.assertEqual(x.digest(), trezor.utils.unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() x.update(b'abc') self.assertEqual(x.digest(), trezor.utils.unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() x.update(b'message digest') self.assertEqual(x.digest(), trezor.utils.unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() x.update(b'abcdefghijklmnopqrstuvwxyz') self.assertEqual(x.digest(), trezor.utils.unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') self.assertEqual(x.digest(), trezor.utils.unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() x.update(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') self.assertEqual(x.digest(), trezor.utils.unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189')) - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() for i in range(8): x.update(b'1234567890') self.assertEqual(x.digest(), trezor.utils.unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb')) def test_digest_multi(self): - x = trezor.crypto.hash.ripemd160() + x = hashlib.ripemd160() d0 = x.digest() d1 = x.digest() d2 = x.digest() diff --git a/src/tests/test_crypto_sha256.py b/src/tests/test_crypto_sha256.py index 5500817ec1..307e5586c1 100644 --- a/src/tests/test_crypto_sha256.py +++ b/src/tests/test_crypto_sha256.py @@ -4,48 +4,48 @@ sys.path.append('../lib') import unittest import trezor.utils -import trezor.crypto.hash +from trezor.crypto import hashlib class TestCryptoSha256(unittest.TestCase): # vectors from http://www.di-mgt.com.au/sha_testvectors.html def test_digest(self): - self.assertEqual(trezor.crypto.hash.sha256(b'').digest(), trezor.utils.unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')) - self.assertEqual(trezor.crypto.hash.sha256(b'abc').digest(), trezor.utils.unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')) - self.assertEqual(trezor.crypto.hash.sha256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), trezor.utils.unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1')) - self.assertEqual(trezor.crypto.hash.sha256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), trezor.utils.unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1')) + self.assertEqual(hashlib.sha256(b'').digest(), trezor.utils.unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')) + self.assertEqual(hashlib.sha256(b'abc').digest(), trezor.utils.unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')) + self.assertEqual(hashlib.sha256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), trezor.utils.unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1')) + self.assertEqual(hashlib.sha256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), trezor.utils.unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1')) def test_update(self): - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() self.assertEqual(x.digest(), trezor.utils.unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')) - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() x.update(b'abc') self.assertEqual(x.digest(), trezor.utils.unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')) - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') self.assertEqual(x.digest(), trezor.utils.unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1')) - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu') self.assertEqual(x.digest(), trezor.utils.unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1')) - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() for i in range(1000000): x.update(b'a') self.assertEqual(x.digest(), trezor.utils.unhexlify('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0')) ''' - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() for i in range(16777216): x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno') self.assertEqual(x.digest(), trezor.utils.unhexlify('50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e')) ''' def test_digest_multi(self): - x = trezor.crypto.hash.sha256() + x = hashlib.sha256() d0 = x.digest() d1 = x.digest() d2 = x.digest() diff --git a/src/tests/test_crypto_sha512.py b/src/tests/test_crypto_sha512.py index b1e0659cd3..14b452f97d 100644 --- a/src/tests/test_crypto_sha512.py +++ b/src/tests/test_crypto_sha512.py @@ -4,48 +4,48 @@ sys.path.append('../lib') import unittest import trezor.utils -import trezor.crypto.hash +from trezor.crypto import hashlib class TestCryptoSha512(unittest.TestCase): # vectors from http://www.di-mgt.com.au/sha_testvectors.html def test_digest(self): - self.assertEqual(trezor.crypto.hash.sha512(b'').digest(), trezor.utils.unhexlify('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')) - self.assertEqual(trezor.crypto.hash.sha512(b'abc').digest(), trezor.utils.unhexlify('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f')) - self.assertEqual(trezor.crypto.hash.sha512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), trezor.utils.unhexlify('204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445')) - self.assertEqual(trezor.crypto.hash.sha512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), trezor.utils.unhexlify('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909')) + self.assertEqual(hashlib.sha512(b'').digest(), trezor.utils.unhexlify('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')) + self.assertEqual(hashlib.sha512(b'abc').digest(), trezor.utils.unhexlify('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f')) + self.assertEqual(hashlib.sha512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), trezor.utils.unhexlify('204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445')) + self.assertEqual(hashlib.sha512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), trezor.utils.unhexlify('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909')) def test_update(self): - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() self.assertEqual(x.digest(), trezor.utils.unhexlify('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')) - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() x.update(b'abc') self.assertEqual(x.digest(), trezor.utils.unhexlify('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f')) - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') self.assertEqual(x.digest(), trezor.utils.unhexlify('204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445')) - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu') self.assertEqual(x.digest(), trezor.utils.unhexlify('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909')) - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() for i in range(1000000): x.update(b'a') self.assertEqual(x.digest(), trezor.utils.unhexlify('e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b')) ''' - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() for i in range(16777216): x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno') self.assertEqual(x.digest(), trezor.utils.unhexlify('b47c933421ea2db149ad6e10fce6c7f93d0752380180ffd7f4629a712134831d77be6091b819ed352c2967a2e2d4fa5050723c9630691f1a05a7281dbe6c1086')) ''' def test_digest_multi(self): - x = trezor.crypto.hash.sha512() + x = hashlib.sha512() d0 = x.digest() d1 = x.digest() d2 = x.digest() diff --git a/src/trezor/crypto/base58.py b/src/trezor/crypto/base58.py index bc61f8c7f9..04a2c2a287 100644 --- a/src/trezor/crypto/base58.py +++ b/src/trezor/crypto/base58.py @@ -1,5 +1,5 @@ from TrezorCrypto import Base58 -from .hash import sha256 +from .hashlib import sha256 _base58 = Base58() diff --git a/src/trezor/crypto/hash.py b/src/trezor/crypto/hash.py deleted file mode 100644 index 3c4d35264d..0000000000 --- a/src/trezor/crypto/hash.py +++ /dev/null @@ -1,3 +0,0 @@ -from TrezorCrypto import Ripemd160 as ripemd160 -from TrezorCrypto import Sha256 as sha256 -from TrezorCrypto import Sha512 as sha512 diff --git a/src/trezor/crypto/pbkdf2.py b/src/trezor/crypto/hashlib.py similarity index 82% rename from src/trezor/crypto/pbkdf2.py rename to src/trezor/crypto/hashlib.py index 3e2f37921e..862500c769 100644 --- a/src/trezor/crypto/pbkdf2.py +++ b/src/trezor/crypto/hashlib.py @@ -1,3 +1,26 @@ +from TrezorCrypto import Ripemd160 as ripemd160 +from TrezorCrypto import Sha256 as sha256 +from TrezorCrypto import Sha512 as sha512 + +from . import hmac + +def pbkdf2_hmac(name, password, salt, rounds, dklen=None): + if name == 'sha256': + digestmod = sha256 + elif name == 'sha512': + digestmod = sha512 + elif name == 'ripemd160': + digestmod = ripemd160 + else: + raise ValueError('unknown digest', name) + if dklen is None: + dklen = digestmod.digest_size + + p = Pbkdf2(password, salt, rounds, digestmod, hmac) + k = p.read(dklen) + p.close() + return k + # # Copyright (C) 2007-2011 Dwayne C. Litzenberger # Copyright (C) 2016 Pavol Rusnak @@ -70,6 +93,3 @@ class Pbkdf2(object): del self.__blockNum del self.__buf self.closed = True - -def new(passphrase, salt, iterations, digestmodule, macmodule): - return Pbkdf2(passphrase, salt, iterations, digestmodule, macmodule)