From ed39c2001edb4191cf5b9c21c19d676de15dcb55 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 11 Apr 2016 10:12:05 +0200 Subject: [PATCH] remove t.c.base58.*_check functions from module (this is app logic), add t.c.sha256 module --- emu.sh | 3 +- .../modTrezorCrypto/modTrezorCrypto-base58.h | 25 ----------- .../modTrezorCrypto/modTrezorCrypto-sha256.h | 45 +++++++++++++++++++ extmod/modTrezorCrypto/modTrezorCrypto.c | 2 + src/trezor/crypto.py | 3 +- 5 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 extmod/modTrezorCrypto/modTrezorCrypto-sha256.h diff --git a/emu.sh b/emu.sh index dac981a10..b111f3a8f 100755 --- a/emu.sh +++ b/emu.sh @@ -1,5 +1,4 @@ #!/bin/bash cd `dirname $0`/src - rm -f ../pipe.* -../vendor/micropython/unix/micropython -O0 -X heapsize=100000 main.py +../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-base58.h b/extmod/modTrezorCrypto/modTrezorCrypto-base58.h index 0747598f0..f78f15d3c 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto-base58.h +++ b/extmod/modTrezorCrypto/modTrezorCrypto-base58.h @@ -31,18 +31,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Base58_encode(mp_obj_t self, mp_obj_t data) { } MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_encode_obj, mod_TrezorCrypto_Base58_encode); -// def Base58.encode_check(self, data: bytes) -> str -STATIC mp_obj_t mod_TrezorCrypto_Base58_encode_check(mp_obj_t self, mp_obj_t data) { - mp_buffer_info_t databuf; - mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ); - vstr_t vstr; - vstr_init(&vstr, (databuf.len + 4) * 8000 / 5857 + 1); // 256 = 2^8 ; 58 > 2^5.857 - vstr.len = base58_encode_check(databuf.buf, databuf.len, vstr.buf, vstr.alloc); - vstr.len--; // base58_encode_check returns length including the trailing zero - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); -} -MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_encode_check_obj, mod_TrezorCrypto_Base58_encode_check); - // def Base58.decode(self, string: str) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Base58_decode(mp_obj_t self, mp_obj_t string) { mp_buffer_info_t stringbuf; @@ -54,24 +42,11 @@ STATIC mp_obj_t mod_TrezorCrypto_Base58_decode(mp_obj_t self, mp_obj_t string) { } MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_decode_obj, mod_TrezorCrypto_Base58_decode); -// def Base58.decode_check(self, string: str) -> bytes -STATIC mp_obj_t mod_TrezorCrypto_Base58_decode_check(mp_obj_t self, mp_obj_t string) { - mp_buffer_info_t stringbuf; - mp_get_buffer_raise(string, &stringbuf, MP_BUFFER_READ); - vstr_t vstr; - vstr_init(&vstr, stringbuf.len * 5858 / 8000 + 1); // 256 = 2^8 ; 58 < 2^5.858 - vstr.len = base58_decode_check(stringbuf.buf, (uint8_t *)vstr.buf, vstr.alloc); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); -} -MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Base58_decode_check_obj, mod_TrezorCrypto_Base58_decode_check); - // Base58 stuff STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Base58_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&mod_TrezorCrypto_Base58_encode_obj) }, - { MP_ROM_QSTR(MP_QSTR_encode_check), MP_ROM_PTR(&mod_TrezorCrypto_Base58_encode_check_obj) }, { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&mod_TrezorCrypto_Base58_decode_obj) }, - { MP_ROM_QSTR(MP_QSTR_decode_check), MP_ROM_PTR(&mod_TrezorCrypto_Base58_decode_check_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Base58_locals_dict, mod_TrezorCrypto_Base58_locals_dict_table); diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h b/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h new file mode 100644 index 000000000..ef4734bbe --- /dev/null +++ b/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under Microsoft Reference Source License (Ms-RSL) + * see LICENSE.md file for details + */ + +#include "trezor-crypto/sha2.h" + +// class Sha256(object): +typedef struct _mp_obj_Sha256_t { + mp_obj_base_t base; +} mp_obj_Sha256_t; + +// def Sha256.__init__(self): +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_obj_Sha256_t *o = m_new_obj(mp_obj_Sha256_t); + o->base.type = type; + return MP_OBJ_FROM_PTR(o); +} + +// def Sha256.hash(self, data: bytes) -> bytes +STATIC mp_obj_t mod_TrezorCrypto_Sha256_hash(mp_obj_t self, mp_obj_t data) { + mp_buffer_info_t databuf; + mp_get_buffer_raise(data, &databuf, MP_BUFFER_READ); + vstr_t vstr; + vstr_init_len(&vstr, 32); // 256 bit = 32 bytes + sha256_Raw(databuf.buf, databuf.len, (uint8_t *)vstr.buf); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +} +MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha256_hash_obj, mod_TrezorCrypto_Sha256_hash); + +// Sha256 stuff + +STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha256_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_hash), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_hash_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Sha256_locals_dict, mod_TrezorCrypto_Sha256_locals_dict_table); + +STATIC const mp_obj_type_t mod_TrezorCrypto_Sha256_type = { + { &mp_type_type }, + .name = MP_QSTR_Sha256, + .make_new = mod_TrezorCrypto_Sha256_make_new, + .locals_dict = (void*)&mod_TrezorCrypto_Sha256_locals_dict, +}; diff --git a/extmod/modTrezorCrypto/modTrezorCrypto.c b/extmod/modTrezorCrypto/modTrezorCrypto.c index 38ee2b631..75f67c616 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto.c +++ b/extmod/modTrezorCrypto/modTrezorCrypto.c @@ -16,12 +16,14 @@ #if MICROPY_PY_TREZORCRYPTO #include "modTrezorCrypto-base58.h" +#include "modTrezorCrypto-sha256.h" // module stuff STATIC const mp_rom_map_elem_t mp_module_TrezorCrypto_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorCrypto) }, { MP_ROM_QSTR(MP_QSTR_Base58), MP_ROM_PTR(&mod_TrezorCrypto_Base58_type) }, + { MP_ROM_QSTR(MP_QSTR_Sha256), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_type) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorCrypto_globals, mp_module_TrezorCrypto_globals_table); diff --git a/src/trezor/crypto.py b/src/trezor/crypto.py index 80ab35d81..50dcfe176 100644 --- a/src/trezor/crypto.py +++ b/src/trezor/crypto.py @@ -1,3 +1,4 @@ -from TrezorCrypto import Base58 +from TrezorCrypto import Base58, Sha256 base58 = Base58() +sha256 = Sha256()