1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

use macros for block/digest sizes

This commit is contained in:
Pavol Rusnak 2016-04-14 17:46:49 +02:00
parent 4c3eb5f015
commit cbea384a79
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 23 additions and 14 deletions

View File

@ -9,6 +9,9 @@
#include "trezor-crypto/ripemd160.h" #include "trezor-crypto/ripemd160.h"
#define HASH_RIPEMD160_BLOCK_SIZE 64
#define HASH_RIPEMD160_DIGEST_SIZE 20
// 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;
@ -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) { 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)); 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_update(&(o->ctx), data, datalen); ripemd160_Update(&(o->ctx), data, datalen);
} else if (n_args != 0) { } 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"));
} }
@ -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_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self);
mp_buffer_info_t databuf; mp_buffer_info_t databuf;
mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ); 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; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Ripemd160_update_obj, mod_TrezorCrypto_Ripemd160_update); 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) { 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, HASH_RIPEMD160_DIGEST_SIZE);
RIPEMD160_CTX ctx; RIPEMD160_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(RIPEMD160_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); 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);
@ -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[] = { 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_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) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(64) }, { 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(20) }, { 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); STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Ripemd160_locals_dict, mod_TrezorCrypto_Ripemd160_locals_dict_table);

View File

@ -9,6 +9,9 @@
#include "trezor-crypto/sha2.h" #include "trezor-crypto/sha2.h"
#define HASH_SHA256_BLOCK_SIZE 64
#define HASH_SHA256_DIGEST_SIZE 32
// class Sha256(object): // class Sha256(object):
typedef struct _mp_obj_Sha256_t { typedef struct _mp_obj_Sha256_t {
mp_obj_base_t base; 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) { STATIC mp_obj_t mod_TrezorCrypto_Sha256_digest(mp_obj_t self) {
mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self); mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self);
vstr_t vstr; vstr_t vstr;
vstr_init_len(&vstr, 32); // 256 bit = 32 bytes vstr_init_len(&vstr, HASH_SHA256_DIGEST_SIZE);
SHA256_CTX ctx; SHA256_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA256_CTX)); memcpy(&ctx, &(o->ctx), sizeof(SHA256_CTX));
sha256_Final((uint8_t *)vstr.buf, &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[] = { 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_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_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_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA256_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(32) }, { 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); STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha256_locals_dict, mod_TrezorCrypto_Sha256_locals_dict_table);

View File

@ -9,6 +9,9 @@
#include "trezor-crypto/sha2.h" #include "trezor-crypto/sha2.h"
#define HASH_SHA512_BLOCK_SIZE 128
#define HASH_SHA512_DIGEST_SIZE 64
// class Sha512(object): // class Sha512(object):
typedef struct _mp_obj_Sha512_t { typedef struct _mp_obj_Sha512_t {
mp_obj_base_t base; 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) { STATIC mp_obj_t mod_TrezorCrypto_Sha512_digest(mp_obj_t self) {
mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self); mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self);
vstr_t vstr; vstr_t vstr;
vstr_init_len(&vstr, 64); // 512 bit = 64 bytes vstr_init_len(&vstr, HASH_SHA512_DIGEST_SIZE);
SHA512_CTX ctx; SHA512_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA512_CTX)); memcpy(&ctx, &(o->ctx), sizeof(SHA512_CTX));
sha512_Final((uint8_t *)vstr.buf, &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[] = { 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_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_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_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA512_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(64) }, { 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); STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha512_locals_dict, mod_TrezorCrypto_Sha512_locals_dict_table);

@ -1 +1 @@
Subproject commit 86d6a0b782cc0d61eb37ead53e5aa37f8de7aa0e Subproject commit f4dd151eb9ef989b88dc79218a2b0115934e4268