1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

modtrezorcrypto: use BLOCK_LENGTH and DIGEST_LENGTH across all hash functions

This commit is contained in:
Pavol Rusnak 2017-03-20 21:07:20 +01:00
parent d23860ed67
commit 420516c5de
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
9 changed files with 31 additions and 55 deletions

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/blake2b.h"
#define HASH_BLAKE2B_BLOCK_SIZE BLAKE2B_BLOCKBYTES
#define HASH_BLAKE2B_DIGEST_SIZE BLAKE2B_OUTBYTES
typedef struct _mp_obj_Blake2b_t {
mp_obj_base_t base;
BLAKE2B_CTX ctx;
@ -31,9 +28,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Blake2b_make_new(const mp_obj_type_t *type, siz
if (n_args == 2) {
mp_buffer_info_t key;
mp_get_buffer_raise(args[1], &key, MP_BUFFER_READ);
blake2b_InitKey(&(o->ctx), BLAKE2B_OUTBYTES, key.buf, key.len);
blake2b_InitKey(&(o->ctx), BLAKE2B_DIGEST_LENGTH, key.buf, key.len);
} else {
blake2b_Init(&(o->ctx), BLAKE2B_OUTBYTES);
blake2b_Init(&(o->ctx), BLAKE2B_DIGEST_LENGTH);
}
// constructor called with data argument set
if (n_args >= 1) {
@ -64,10 +61,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Blake2b_update_obj, mod_Trezor
STATIC mp_obj_t mod_TrezorCrypto_Blake2b_digest(mp_obj_t self) {
mp_obj_Blake2b_t *o = MP_OBJ_TO_PTR(self);
vstr_t vstr;
vstr_init_len(&vstr, HASH_BLAKE2B_DIGEST_SIZE);
vstr_init_len(&vstr, BLAKE2B_DIGEST_LENGTH);
BLAKE2B_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(BLAKE2B_CTX));
blake2b_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2B_OUTBYTES);
blake2b_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2B_DIGEST_LENGTH);
memset(&ctx, 0, sizeof(BLAKE2B_CTX));
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
@ -84,8 +81,8 @@ STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Blake2b_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Blake2b_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Blake2b_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Blake2b___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_BLAKE2B_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_BLAKE2B_DIGEST_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(BLAKE2B_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(BLAKE2B_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Blake2b_locals_dict, mod_TrezorCrypto_Blake2b_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/blake2s.h"
#define HASH_BLAKE2S_BLOCK_SIZE BLAKE2S_BLOCKBYTES
#define HASH_BLAKE2S_DIGEST_SIZE BLAKE2S_OUTBYTES
typedef struct _mp_obj_Blake2s_t {
mp_obj_base_t base;
BLAKE2S_CTX ctx;
@ -31,9 +28,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Blake2s_make_new(const mp_obj_type_t *type, siz
if (n_args == 2) {
mp_buffer_info_t key;
mp_get_buffer_raise(args[1], &key, MP_BUFFER_READ);
blake2s_InitKey(&(o->ctx), BLAKE2S_OUTBYTES, key.buf, key.len);
blake2s_InitKey(&(o->ctx), BLAKE2S_DIGEST_LENGTH, key.buf, key.len);
} else {
blake2s_Init(&(o->ctx), BLAKE2S_OUTBYTES);
blake2s_Init(&(o->ctx), BLAKE2S_DIGEST_LENGTH);
}
// constructor called with data argument set
if (n_args >= 1) {
@ -64,10 +61,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Blake2s_update_obj, mod_Trezor
STATIC mp_obj_t mod_TrezorCrypto_Blake2s_digest(mp_obj_t self) {
mp_obj_Blake2s_t *o = MP_OBJ_TO_PTR(self);
vstr_t vstr;
vstr_init_len(&vstr, HASH_BLAKE2S_DIGEST_SIZE);
vstr_init_len(&vstr, BLAKE2S_DIGEST_LENGTH);
BLAKE2S_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(BLAKE2S_CTX));
blake2s_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2S_OUTBYTES);
blake2s_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2S_DIGEST_LENGTH);
memset(&ctx, 0, sizeof(BLAKE2S_CTX));
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
@ -84,8 +81,8 @@ STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Blake2s_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Blake2s_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Blake2s_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Blake2s___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_BLAKE2S_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_BLAKE2S_DIGEST_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(BLAKE2S_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(BLAKE2S_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Blake2s_locals_dict, mod_TrezorCrypto_Blake2s_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/ripemd160.h"
#define HASH_RIPEMD160_BLOCK_SIZE RIPEMD160_BLOCK_LENGTH
#define HASH_RIPEMD160_DIGEST_SIZE RIPEMD160_DIGEST_LENGTH
typedef struct _mp_obj_Ripemd160_t {
mp_obj_base_t base;
RIPEMD160_CTX ctx;
@ -57,7 +54,7 @@ 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, HASH_RIPEMD160_DIGEST_SIZE);
vstr_init_len(&vstr, RIPEMD160_DIGEST_LENGTH);
RIPEMD160_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(RIPEMD160_CTX));
ripemd160_Final(&ctx, (uint8_t *)vstr.buf);
@ -77,8 +74,8 @@ 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) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(RIPEMD160_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(RIPEMD160_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Ripemd160_locals_dict, mod_TrezorCrypto_Ripemd160_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/sha2.h"
#define HASH_SHA1_BLOCK_SIZE SHA1_BLOCK_LENGTH
#define HASH_SHA1_DIGEST_SIZE SHA1_DIGEST_LENGTH
typedef struct _mp_obj_Sha1_t {
mp_obj_base_t base;
SHA1_CTX ctx;
@ -57,7 +54,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha1_update_obj, mod_TrezorCry
STATIC mp_obj_t mod_TrezorCrypto_Sha1_digest(mp_obj_t self) {
mp_obj_Sha1_t *o = MP_OBJ_TO_PTR(self);
vstr_t vstr;
vstr_init_len(&vstr, HASH_SHA1_DIGEST_SIZE);
vstr_init_len(&vstr, SHA1_DIGEST_LENGTH);
SHA1_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA1_CTX));
sha1_Final(&ctx, (uint8_t *)vstr.buf);
@ -77,8 +74,8 @@ STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha1_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha1_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha1_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Sha1___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA1_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA1_DIGEST_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SHA1_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(SHA1_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha1_locals_dict, mod_TrezorCrypto_Sha1_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/sha2.h"
#define HASH_SHA256_BLOCK_SIZE SHA256_BLOCK_LENGTH
#define HASH_SHA256_DIGEST_SIZE SHA256_DIGEST_LENGTH
typedef struct _mp_obj_Sha256_t {
mp_obj_base_t base;
SHA256_CTX ctx;
@ -57,7 +54,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, HASH_SHA256_DIGEST_SIZE);
vstr_init_len(&vstr, SHA256_DIGEST_LENGTH);
SHA256_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA256_CTX));
sha256_Final(&ctx, (uint8_t *)vstr.buf);
@ -77,8 +74,8 @@ 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) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SHA256_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(SHA256_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha256_locals_dict, mod_TrezorCrypto_Sha256_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/sha3.h"
#define HASH_SHA3_256_BLOCK_SIZE 64
#define HASH_SHA3_256_DIGEST_SIZE sha3_256_hash_size
typedef struct _mp_obj_Sha3_256_t {
mp_obj_base_t base;
SHA3_CTX ctx;
@ -57,7 +54,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha3_256_update_obj, mod_Trezo
STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_digest(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(args[0]);
vstr_t vstr;
vstr_init_len(&vstr, HASH_SHA3_256_DIGEST_SIZE);
vstr_init_len(&vstr, SHA3_256_DIGEST_LENGTH);
SHA3_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
if (n_args >= 1 && args[1] == mp_const_true) {
@ -81,8 +78,8 @@ STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha3_256_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_256_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_256_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_256___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA3_256_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA3_256_DIGEST_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SHA3_256_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(SHA3_256_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha3_256_locals_dict, mod_TrezorCrypto_Sha3_256_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/sha3.h"
#define HASH_SHA3_512_BLOCK_SIZE 128
#define HASH_SHA3_512_DIGEST_SIZE sha3_512_hash_size
typedef struct _mp_obj_Sha3_512_t {
mp_obj_base_t base;
SHA3_CTX ctx;
@ -57,7 +54,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha3_512_update_obj, mod_Trezo
STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_digest(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(args[0]);
vstr_t vstr;
vstr_init_len(&vstr, HASH_SHA3_512_DIGEST_SIZE);
vstr_init_len(&vstr, SHA3_512_DIGEST_LENGTH);
SHA3_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
if (n_args >= 1 && args[1] == mp_const_true) {
@ -81,8 +78,8 @@ STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha3_512_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_512_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_512_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_512___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA3_512_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(HASH_SHA3_512_DIGEST_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SHA3_512_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(SHA3_512_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha3_512_locals_dict, mod_TrezorCrypto_Sha3_512_locals_dict_table);

View File

@ -9,9 +9,6 @@
#include "trezor-crypto/sha2.h"
#define HASH_SHA512_BLOCK_SIZE SHA512_BLOCK_LENGTH
#define HASH_SHA512_DIGEST_SIZE SHA512_DIGEST_LENGTH
typedef struct _mp_obj_Sha512_t {
mp_obj_base_t base;
SHA512_CTX ctx;
@ -56,7 +53,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, HASH_SHA512_DIGEST_SIZE);
vstr_init_len(&vstr, SHA512_DIGEST_LENGTH);
SHA512_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA512_CTX));
sha512_Final(&ctx, (uint8_t *)vstr.buf);
@ -76,8 +73,8 @@ 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) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SHA512_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(SHA512_DIGEST_LENGTH) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha512_locals_dict, mod_TrezorCrypto_Sha512_locals_dict_table);

@ -1 +1 @@
Subproject commit 9a2310fc53a4d03c19591cbb55d85436f7573971
Subproject commit 87c920a7e747f7ed40b6ae841327868ab914435b