diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h new file mode 100644 index 0000000000..1e09da52fa --- /dev/null +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#include "py/objstr.h" + +#include "blake256.h" + +/// class Blake256: +/// ''' +/// Blake256 context. +/// ''' +typedef struct _mp_obj_Blake256_t { + mp_obj_base_t base; + BLAKE256_CTX ctx; +} mp_obj_Blake256_t; + +STATIC mp_obj_t mod_trezorcrypto_Blake256_update(mp_obj_t self, mp_obj_t data); + +/// def __init__(self, data: bytes = None) -> None: +/// ''' +/// Creates a hash context object. +/// ''' +STATIC mp_obj_t mod_trezorcrypto_Blake256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); + mp_obj_Blake256_t *o = m_new_obj(mp_obj_Blake256_t); + o->base.type = type; + blake256_Init(&(o->ctx)); + // constructor called with bytes/str as first parameter + if (n_args == 1) { + mod_trezorcrypto_Blake256_update(MP_OBJ_FROM_PTR(o), args[0]); + } + return MP_OBJ_FROM_PTR(o); +} + +/// def update(self, data: bytes) -> None: +/// ''' +/// Update the hash context with hashed data. +/// ''' +STATIC mp_obj_t mod_trezorcrypto_Blake256_update(mp_obj_t self, mp_obj_t data) { + mp_obj_Blake256_t *o = MP_OBJ_TO_PTR(self); + mp_buffer_info_t msg; + mp_get_buffer_raise(data, &msg, MP_BUFFER_READ); + if (msg.len > 0) { + blake256_Update(&(o->ctx), msg.buf, msg.len); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Blake256_update_obj, mod_trezorcrypto_Blake256_update); + +/// def digest(self) -> bytes: +/// ''' +/// Returns the digest of hashed data. +/// ''' +STATIC mp_obj_t mod_trezorcrypto_Blake256_digest(mp_obj_t self) { + mp_obj_Blake256_t *o = MP_OBJ_TO_PTR(self); + vstr_t vstr; + vstr_init_len(&vstr, BLAKE256_DIGEST_LENGTH); + BLAKE256_CTX ctx; + memcpy(&ctx, &(o->ctx), sizeof(BLAKE256_CTX)); + blake256_Final(&ctx, (uint8_t *)vstr.buf); + memset(&ctx, 0, sizeof(BLAKE256_CTX)); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake256_digest_obj, mod_trezorcrypto_Blake256_digest); + +STATIC mp_obj_t mod_trezorcrypto_Blake256___del__(mp_obj_t self) { + mp_obj_Blake256_t *o = MP_OBJ_TO_PTR(self); + memset(&(o->ctx), 0, sizeof(BLAKE256_CTX)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake256___del___obj, mod_trezorcrypto_Blake256___del__); + +STATIC const mp_rom_map_elem_t mod_trezorcrypto_Blake256_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_trezorcrypto_Blake256_update_obj) }, + { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_trezorcrypto_Blake256_digest_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_trezorcrypto_Blake256___del___obj) }, + { MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(BLAKE256_BLOCK_LENGTH) }, + { MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(BLAKE256_DIGEST_LENGTH) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Blake256_locals_dict, mod_trezorcrypto_Blake256_locals_dict_table); + +STATIC const mp_obj_type_t mod_trezorcrypto_Blake256_type = { + { &mp_type_type }, + .name = MP_QSTR_Blake256, + .make_new = mod_trezorcrypto_Blake256_make_new, + .locals_dict = (void*)&mod_trezorcrypto_Blake256_locals_dict, +}; diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto.c b/embed/extmod/modtrezorcrypto/modtrezorcrypto.c index 36a4308fbb..c1fcb241d8 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto.c +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto.c @@ -16,6 +16,7 @@ #include "modtrezorcrypto-aes.h" #include "modtrezorcrypto-bip32.h" #include "modtrezorcrypto-bip39.h" +#include "modtrezorcrypto-blake256.h" #include "modtrezorcrypto-blake2b.h" #include "modtrezorcrypto-blake2s.h" #include "modtrezorcrypto-curve25519.h" @@ -36,6 +37,7 @@ 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_AES), MP_ROM_PTR(&mod_trezorcrypto_AES_type) }, + { MP_ROM_QSTR(MP_QSTR_blake256), MP_ROM_PTR(&mod_trezorcrypto_Blake256_type) }, { MP_ROM_QSTR(MP_QSTR_blake2b), MP_ROM_PTR(&mod_trezorcrypto_Blake2b_type) }, { MP_ROM_QSTR(MP_QSTR_blake2s), MP_ROM_PTR(&mod_trezorcrypto_Blake2s_type) }, { MP_ROM_QSTR(MP_QSTR_pbkdf2), MP_ROM_PTR(&mod_trezorcrypto_Pbkdf2_type) }, diff --git a/src/trezor/crypto/hashlib.py b/src/trezor/crypto/hashlib.py index fc36a3fa65..7814fd6ed8 100644 --- a/src/trezor/crypto/hashlib.py +++ b/src/trezor/crypto/hashlib.py @@ -1,3 +1,4 @@ +from trezorcrypto import blake256 from trezorcrypto import blake2b from trezorcrypto import blake2s from trezorcrypto import ripemd160 diff --git a/tests/test_trezor.crypto.hashlib.blake256.py b/tests/test_trezor.crypto.hashlib.blake256.py new file mode 100644 index 0000000000..c338a166f2 --- /dev/null +++ b/tests/test_trezor.crypto.hashlib.blake256.py @@ -0,0 +1,54 @@ +from common import * + +from trezor.crypto import hashlib + + +class TestCryptoBlake256(unittest.TestCase): + + # vectors from https://raw.githubusercontent.com/monero-project/monero/master/tests/hash/tests-extra-blake.txt + + vectors = [ + ('', '716f6e863f744b9ac22c97ec7b76ea5f5908bc5b2f67c61510bfc4751384ea7a'), + ('cc', 'e104256a2bc501f459d03fac96b9014f593e22d30f4de525fa680c3aa189eb4f'), + ('41fb', '8f341148be7e354fdf38b693d8c6b4e0bd57301a734f6fd35cd85b8491c3ddcd'), + ('1f877c', 'bc334d1069099f10c601883ac6f3e7e9787c6aa53171f76a21923cc5ad3ab937'), + ('c1ecfdfc', 'b672a16f53982bab1e77685b71c0a5f6703ffd46a1c834be69f614bd128d658e'), + ('21f134ac57', 'd9134b2899057a7d8d320cc99e3e116982bc99d3c69d260a7f1ed3da8be68d99'), + ('c6f50bb74e29', '637923bd29a35aa3ecbbd2a50549fc32c14cf0fdcaf41c3194dd7414fd224815'), + ('119713cc83eeef', '70c092fd5c8c21e9ef4bbc82a5c7819e262a530a748caf285ff0cba891954f1e'), + ('4a4f202484512526', 'fdf092993edbb7a0dc7ca67f04051bbd14481639da0808947aff8bfab5abed4b'), + ('1f66ab4185ed9b6375', '6f6fc234bf35beae1a366c44c520c59ad5aa70351b5f5085e21e1fe2bfcee709'), + ('eed7422227613b6f53c9', '4fdaf89e2a0e78c000061b59455e0ea93a4445b440e7562c8f0cfa165c93de2e'), + ('eaeed5cdffd89dece455f1', 'd6b780eee9c811f664393dc2c58b5a68c92b3c9fe9ceb70371d33ece63b5787e'), + ('5be43c90f22902e4fe8ed2d3', 'd0015071d3e7ed048c764850d76406eceae52b8e2e6e5a2c3aa92ae880485b34'), + ('a746273228122f381c3b46e4f1', '9b0207902f9932f7a85c24722e93e31f6ed2c75c406509aa0f2f6d1cab046ce4'), + ('3c5871cd619c69a63b540eb5a625', '258020d5b04a814f2b72c1c661e1f5a5c395d9799e5eee8b8519cf7300e90cb1'), + ('fa22874bcc068879e8ef11a69f0722', '4adae3b55baa907fefc253365fdd99d8398befd0551ed6bf9a2a2784d3c304d1'), + ('52a608ab21ccdd8a4457a57ede782176', '6dd10d772f8d5b4a96c3c5d30878cd9a1073fa835bfe6d2b924fa64a1fab1711'), + ('82e192e4043ddcd12ecf52969d0f807eed', '0b8741ddf2259d3af2901eb1ae354f22836442c965556f5c1eb89501191cb46a'), + ('75683dcb556140c522543bb6e9098b21a21e', 'f48a754ca8193a82643150ab94038b5dd170b4ebd1e0751b78cfb0a98fa5076a'), + ('06e4efe45035e61faaf4287b4d8d1f12ca97e5', '5698409ab856b74d9fa5e9b259dfa46001f89041752da424e56e491577b88c86'), + ] + + def test_digest(self): + for b, d in self.vectors: + self.assertEqual(hashlib.blake256(unhexlify(b)).digest(), unhexlify(d)) + + def test_update(self): + + for b, d in self.vectors: + x = hashlib.blake256() + x.update(unhexlify(b)) + self.assertEqual(x.digest(), unhexlify(d)) + + def test_digest_multi(self): + x = hashlib.blake256() + d0 = x.digest() + d1 = x.digest() + d2 = x.digest() + self.assertEqual(d0, d1) + self.assertEqual(d0, d2) + + +if __name__ == '__main__': + unittest.main() diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto index 764cc4c6e8..921b59e1d8 160000 --- a/vendor/trezor-crypto +++ b/vendor/trezor-crypto @@ -1 +1 @@ -Subproject commit 764cc4c6e8ef32e7e1a77f0496ae090f11a36def +Subproject commit 921b59e1d84a21ae3d846779017d839114345e4e