From cbea384a79747609bb30b39060abd4182ed5662d Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 14 Apr 2016 17:46:49 +0200 Subject: [PATCH] use macros for block/digest sizes --- .../modTrezorCrypto/modTrezorCrypto-ripemd160.h | 17 ++++++++++------- extmod/modTrezorCrypto/modTrezorCrypto-sha256.h | 9 ++++++--- extmod/modTrezorCrypto/modTrezorCrypto-sha512.h | 9 ++++++--- vendor/trezor-crypto | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-ripemd160.h b/extmod/modTrezorCrypto/modTrezorCrypto-ripemd160.h index e1f83dfbc6..b4741b79c7 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto-ripemd160.h +++ b/extmod/modTrezorCrypto/modTrezorCrypto-ripemd160.h @@ -9,6 +9,9 @@ #include "trezor-crypto/ripemd160.h" +#define HASH_RIPEMD160_BLOCK_SIZE 64 +#define HASH_RIPEMD160_DIGEST_SIZE 20 + // class Ripemd160(object): typedef struct _mp_obj_Ripemd160_t { mp_obj_base_t base; @@ -19,14 +22,14 @@ typedef struct _mp_obj_Ripemd160_t { 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); o->base.type = type; - ripemd160_init(&(o->ctx)); + ripemd160_Init(&(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); - ripemd160_update(&(o->ctx), data, datalen); + ripemd160_Update(&(o->ctx), data, datalen); } else if (n_args != 0) { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Invalid arguments")); } @@ -38,7 +41,7 @@ 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); + 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); @@ -47,10 +50,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Ripemd160_update_obj, mod_Trez STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_digest(mp_obj_t self) { mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; - vstr_init_len(&vstr, 20); // 160 bit = 20 bytes + vstr_init_len(&vstr, HASH_RIPEMD160_DIGEST_SIZE); RIPEMD160_CTX ctx; memcpy(&ctx, &(o->ctx), sizeof(RIPEMD160_CTX)); - ripemd160_finish(&ctx, (uint8_t *)vstr.buf); + ripemd160_Final((uint8_t *)vstr.buf, &ctx); 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); @@ -60,8 +63,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160_digest_obj, mod_Trez 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_block_size), MP_OBJ_NEW_SMALL_INT(64) }, - { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(20) }, + { 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) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Ripemd160_locals_dict, mod_TrezorCrypto_Ripemd160_locals_dict_table); diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h b/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h index 39b8f63e31..3e7e4b18fe 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h +++ b/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h @@ -9,6 +9,9 @@ #include "trezor-crypto/sha2.h" +#define HASH_SHA256_BLOCK_SIZE 64 +#define HASH_SHA256_DIGEST_SIZE 32 + // class Sha256(object): typedef struct _mp_obj_Sha256_t { mp_obj_base_t base; @@ -47,7 +50,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha256_update_obj, mod_TrezorC STATIC mp_obj_t mod_TrezorCrypto_Sha256_digest(mp_obj_t self) { mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; - vstr_init_len(&vstr, 32); // 256 bit = 32 bytes + vstr_init_len(&vstr, HASH_SHA256_DIGEST_SIZE); SHA256_CTX ctx; memcpy(&ctx, &(o->ctx), sizeof(SHA256_CTX)); sha256_Final((uint8_t *)vstr.buf, &ctx); @@ -60,8 +63,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha256_digest_obj, mod_TrezorC 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_block_size), MP_OBJ_NEW_SMALL_INT(64) }, - { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(32) }, + { 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) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha256_locals_dict, mod_TrezorCrypto_Sha256_locals_dict_table); diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-sha512.h b/extmod/modTrezorCrypto/modTrezorCrypto-sha512.h index 1f2d9dcb21..9102d3e7e0 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto-sha512.h +++ b/extmod/modTrezorCrypto/modTrezorCrypto-sha512.h @@ -9,6 +9,9 @@ #include "trezor-crypto/sha2.h" +#define HASH_SHA512_BLOCK_SIZE 128 +#define HASH_SHA512_DIGEST_SIZE 64 + // class Sha512(object): typedef struct _mp_obj_Sha512_t { mp_obj_base_t base; @@ -47,7 +50,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha512_update_obj, mod_TrezorC STATIC mp_obj_t mod_TrezorCrypto_Sha512_digest(mp_obj_t self) { mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; - vstr_init_len(&vstr, 64); // 512 bit = 64 bytes + vstr_init_len(&vstr, HASH_SHA512_DIGEST_SIZE); SHA512_CTX ctx; memcpy(&ctx, &(o->ctx), sizeof(SHA512_CTX)); sha512_Final((uint8_t *)vstr.buf, &ctx); @@ -60,8 +63,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha512_digest_obj, mod_TrezorC 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_block_size), MP_OBJ_NEW_SMALL_INT(128) }, - { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(64) }, + { 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) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha512_locals_dict, mod_TrezorCrypto_Sha512_locals_dict_table); diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto index 86d6a0b782..f4dd151eb9 160000 --- a/vendor/trezor-crypto +++ b/vendor/trezor-crypto @@ -1 +1 @@ -Subproject commit 86d6a0b782cc0d61eb37ead53e5aa37f8de7aa0e +Subproject commit f4dd151eb9ef989b88dc79218a2b0115934e4268