mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
use streamed version of ripemd160
This commit is contained in:
parent
0ab2170cc6
commit
21db5a6ff7
@ -12,34 +12,43 @@
|
|||||||
// class Ripemd160(object):
|
// class Ripemd160(object):
|
||||||
typedef struct _mp_obj_Ripemd160_t {
|
typedef struct _mp_obj_Ripemd160_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
uint8_t hash[20];
|
RIPEMD160_CTX ctx;
|
||||||
} mp_obj_Ripemd160_t;
|
} mp_obj_Ripemd160_t;
|
||||||
|
|
||||||
// def Ripemd160.__init__(self, data: bytes = None)
|
// 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) {
|
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_obj_Ripemd160_t *o = m_new_obj(mp_obj_Ripemd160_t);
|
mp_obj_Ripemd160_t *o = m_new_obj(mp_obj_Ripemd160_t);
|
||||||
o->base.type = type;
|
o->base.type = type;
|
||||||
|
ripemd160_init(&(o->ctx));
|
||||||
// constructor called with bytes/str as first parameter
|
// constructor called with bytes/str as first parameter
|
||||||
if (n_args == 1) {
|
if (n_args == 1) {
|
||||||
if (!MP_OBJ_IS_STR_OR_BYTES(args[0])) {
|
if (!MP_OBJ_IS_STR_OR_BYTES(args[0])) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid argument"));
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid argument"));
|
||||||
}
|
}
|
||||||
GET_STR_DATA_LEN(args[0], data, datalen);
|
GET_STR_DATA_LEN(args[0], data, datalen);
|
||||||
ripemd160(data, datalen, o->hash);
|
ripemd160_update(&(o->ctx), data, datalen);
|
||||||
} else {
|
} else if (n_args != 0) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid arguments"));
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid arguments"));
|
||||||
}
|
}
|
||||||
return MP_OBJ_FROM_PTR(o);
|
return MP_OBJ_FROM_PTR(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
// no update for now ...
|
// def Ripemd160.update(self, data: bytes) -> None
|
||||||
|
STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_update(mp_obj_t self, mp_obj_t data) {
|
||||||
|
mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self);
|
||||||
|
mp_buffer_info_t databuf;
|
||||||
|
mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ);
|
||||||
|
ripemd160_update(&(o->ctx), databuf.buf, databuf.len);
|
||||||
|
return mp_const_none;
|
||||||
|
}
|
||||||
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Ripemd160_update_obj, mod_TrezorCrypto_Ripemd160_update);
|
||||||
|
|
||||||
// def Ripemd160.digest(self) -> bytes
|
// def Ripemd160.digest(self) -> bytes
|
||||||
STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_digest(mp_obj_t self) {
|
STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_digest(mp_obj_t self) {
|
||||||
mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self);
|
mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self);
|
||||||
vstr_t vstr;
|
vstr_t vstr;
|
||||||
vstr_init_len(&vstr, 20); // 160 bit = 20 bytes
|
vstr_init_len(&vstr, 20); // 160 bit = 20 bytes
|
||||||
memcpy(vstr.buf, o->hash, 20);
|
ripemd160_finish(&(o->ctx), (uint8_t *)vstr.buf);
|
||||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160_digest_obj, mod_TrezorCrypto_Ripemd160_digest);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160_digest_obj, mod_TrezorCrypto_Ripemd160_digest);
|
||||||
@ -47,6 +56,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160_digest_obj, mod_Trez
|
|||||||
// Ripemd160 stuff
|
// Ripemd160 stuff
|
||||||
|
|
||||||
STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Ripemd160_locals_dict_table[] = {
|
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_digest), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160_digest_obj) },
|
||||||
};
|
};
|
||||||
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Ripemd160_locals_dict, mod_TrezorCrypto_Ripemd160_locals_dict_table);
|
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Ripemd160_locals_dict, mod_TrezorCrypto_Ripemd160_locals_dict_table);
|
||||||
|
@ -20,5 +20,38 @@ class TestCryptoRipemd160(unittest.TestCase):
|
|||||||
self.assertEqual(trezor.crypto.hash.ripemd160(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').digest(), trezor.utils.unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189'))
|
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(trezor.crypto.hash.ripemd160(b'12345678901234567890123456789012345678901234567890123456789012345678901234567890').digest(), trezor.utils.unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb'))
|
||||||
|
|
||||||
|
def test_update(self):
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
x.update(b'a')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
x.update(b'abc')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
x.update(b'message digest')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
x.update(b'abcdefghijklmnopqrstuvwxyz')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
x.update(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189'))
|
||||||
|
|
||||||
|
x = trezor.crypto.hash.ripemd160()
|
||||||
|
for i in range(8):
|
||||||
|
x.update(b'1234567890')
|
||||||
|
self.assertEqual(x.digest(), trezor.utils.unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb'))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
2
vendor/trezor-crypto
vendored
2
vendor/trezor-crypto
vendored
@ -1 +1 @@
|
|||||||
Subproject commit bb52cb4ac93dd36e22bac7fef003f5b5488f2e08
|
Subproject commit 86d6a0b782cc0d61eb37ead53e5aa37f8de7aa0e
|
Loading…
Reference in New Issue
Block a user