mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-13 19:18:56 +00:00
modtrezorcrypto: add Blake2b
This commit is contained in:
parent
10b687c318
commit
9c3c19959b
97
micropython/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h
Normal file
97
micropython/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
||||
*
|
||||
* Licensed under TREZOR License
|
||||
* see LICENSE file for details
|
||||
*/
|
||||
|
||||
#include "py/objstr.h"
|
||||
|
||||
#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;
|
||||
} mp_obj_Blake2b_t;
|
||||
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Blake2b_update(mp_obj_t self, mp_obj_t data);
|
||||
|
||||
/// def trezor.crypto.hashlib.blake2b(data: bytes=None, key: bytes=None) -> Blake2b:
|
||||
/// '''
|
||||
/// Creates a hash context object.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Blake2b_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, 2, false);
|
||||
mp_obj_Blake2b_t *o = m_new_obj(mp_obj_Blake2b_t);
|
||||
o->base.type = type;
|
||||
// constructor called with key argument set
|
||||
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);
|
||||
} else {
|
||||
blake2b_Init(&(o->ctx), BLAKE2B_OUTBYTES);
|
||||
}
|
||||
// constructor called with data argument set
|
||||
if (n_args >= 1) {
|
||||
mod_TrezorCrypto_Blake2b_update(MP_OBJ_FROM_PTR(o), args[0]);
|
||||
}
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def trezor.crypto.hashlib.blake2b.update(self, data: bytes) -> None:
|
||||
/// '''
|
||||
/// Update the hash context with hashed data.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Blake2b_update(mp_obj_t self, mp_obj_t data) {
|
||||
mp_obj_Blake2b_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) {
|
||||
blake2b_Update(&(o->ctx), msg.buf, msg.len);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Blake2b_update_obj, mod_TrezorCrypto_Blake2b_update);
|
||||
|
||||
/// def trezor.crypto.hashlib.blake2b.digest(self) -> bytes:
|
||||
/// '''
|
||||
/// Returns the digest of hashed data.
|
||||
/// '''
|
||||
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);
|
||||
BLAKE2B_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(BLAKE2B_CTX));
|
||||
blake2b_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2B_OUTBYTES);
|
||||
memset(&ctx, 0, sizeof(BLAKE2B_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Blake2b_digest_obj, mod_TrezorCrypto_Blake2b_digest);
|
||||
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Blake2b___del__(mp_obj_t self) {
|
||||
mp_obj_Blake2b_t *o = MP_OBJ_TO_PTR(self);
|
||||
memset(&(o->ctx), 0, sizeof(BLAKE2B_CTX));
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Blake2b___del___obj, mod_TrezorCrypto_Blake2b___del__);
|
||||
|
||||
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) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Blake2b_locals_dict, mod_TrezorCrypto_Blake2b_locals_dict_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_TrezorCrypto_Blake2b_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Blake2b,
|
||||
.make_new = mod_TrezorCrypto_Blake2b_make_new,
|
||||
.locals_dict = (void*)&mod_TrezorCrypto_Blake2b_locals_dict,
|
||||
};
|
@ -16,6 +16,7 @@
|
||||
#include "modtrezorcrypto-aes.h"
|
||||
#include "modtrezorcrypto-bip32.h"
|
||||
#include "modtrezorcrypto-bip39.h"
|
||||
#include "modtrezorcrypto-blake2b.h"
|
||||
#include "modtrezorcrypto-blake2s.h"
|
||||
#include "modtrezorcrypto-curve25519.h"
|
||||
#include "modtrezorcrypto-ed25519.h"
|
||||
@ -36,6 +37,7 @@ STATIC const mp_rom_map_elem_t mp_module_TrezorCrypto_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_AES), MP_ROM_PTR(&mod_TrezorCrypto_AES_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Bip32), MP_ROM_PTR(&mod_TrezorCrypto_Bip32_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Bip39), MP_ROM_PTR(&mod_TrezorCrypto_Bip39_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_Curve25519), MP_ROM_PTR(&mod_TrezorCrypto_Curve25519_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Ed25519), MP_ROM_PTR(&mod_TrezorCrypto_Ed25519_type) },
|
||||
|
@ -1,3 +1,4 @@
|
||||
from TrezorCrypto import Blake2b as blake2b
|
||||
from TrezorCrypto import Blake2s as blake2s
|
||||
from TrezorCrypto import Ripemd160 as ripemd160
|
||||
from TrezorCrypto import Sha1 as sha1
|
||||
|
48
tests/test_trezor.crypto.hashlib.blake2b.py
Normal file
48
tests/test_trezor.crypto.hashlib.blake2b.py
Normal file
@ -0,0 +1,48 @@
|
||||
from common import *
|
||||
|
||||
from trezor.crypto import hashlib
|
||||
|
||||
class TestCryptoBlake2b(unittest.TestCase):
|
||||
|
||||
# vectors from https://raw.githubusercontent.com/BLAKE2/BLAKE2/master/testvectors/blake2b-kat.txt
|
||||
|
||||
vectors = [
|
||||
('', '10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568'),
|
||||
('00', '961f6dd1e4dd30f63901690c512e78e4b45e4742ed197c3c5e45c549fd25f2e4187b0bc9fe30492b16b0d0bc4ef9b0f34c7003fac09a5ef1532e69430234cebd'),
|
||||
('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e', 'eba51acffb4cea31db4b8d87e9bf7dd48fe97b0253ae67aa580f9ac4a9d941f2bea518ee286818cc9f633f2a3b9fb68e594b48cdd6d515bf1d52ba6c85a203a7'),
|
||||
('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', '5595e05c13a7ec4dc8f41fb70cb50a71bce17c024ff6de7af618d0cc4e9c32d9570d6d3ea45b86525491030c0d8f2b1836d5778c1ce735c17707df364d054347'),
|
||||
('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4041', 'c516541701863f91005f314108ceece3c643e04fc8c42fd2ff556220e616aaa6a48aeb97a84bad74782e8dff96a1a2fa949339d722edcaa32b57067041df88cc'),
|
||||
('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60', '31fc79738b8772b3f55cd8178813b3b52d0db5a419d30ba9495c4b9da0219fac6df8e7c23a811551a62b827f256ecdb8124ac8a6792ccfecc3b3012722e94463'),
|
||||
]
|
||||
|
||||
def test_digest(self):
|
||||
key = unhexlify('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f')
|
||||
for d, h in self.vectors:
|
||||
self.assertEqual(hashlib.blake2b(unhexlify(d), key).digest(), unhexlify(h))
|
||||
|
||||
def test_update(self):
|
||||
key = unhexlify('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f')
|
||||
x = hashlib.blake2b(b'', key)
|
||||
x.update(bytes(range(10)))
|
||||
self.assertEqual(x.digest(), unhexlify('4fe181f54ad63a2983feaaf77d1e7235c2beb17fa328b6d9505bda327df19fc37f02c4b6f0368ce23147313a8e5738b5fa2a95b29de1c7f8264eb77b69f585cd'))
|
||||
x.update(bytes(range(10, 30)))
|
||||
self.assertEqual(x.digest(), unhexlify('c6dbc61dec6eaeac81e3d5f755203c8e220551534a0b2fd105a91889945a638550204f44093dd998c076205dffad703a0e5cd3c7f438a7e634cd59fededb539e'))
|
||||
x.update(bytes(range(30, 80)))
|
||||
self.assertEqual(x.digest(), unhexlify('fa1549c9796cd4d303dcf452c1fbd5744fd9b9b47003d920b92de34839d07ef2a29ded68f6fc9e6c45e071a2e48bd50c5084e96b657dd0404045a1ddefe282ed'))
|
||||
x.update(bytes(range(80, 111)))
|
||||
self.assertEqual(x.digest(), unhexlify('2620f687e8625f6a412460b42e2cef67634208ce10a0cbd4dff7044a41b7880077e9f8dc3b8d1216d3376a21e015b58fb279b521d83f9388c7382c8505590b9b'))
|
||||
x.update(bytes(range(111, 127)))
|
||||
self.assertEqual(x.digest(), unhexlify('76d2d819c92bce55fa8e092ab1bf9b9eab237a25267986cacf2b8ee14d214d730dc9a5aa2d7b596e86a1fd8fa0804c77402d2fcd45083688b218b1cdfa0dcbcb'))
|
||||
x.update(bytes(range(127, 255)))
|
||||
self.assertEqual(x.digest(), unhexlify('142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e92484be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461'))
|
||||
|
||||
def test_digest_multi(self):
|
||||
x = hashlib.blake2b()
|
||||
d0 = x.digest()
|
||||
d1 = x.digest()
|
||||
d2 = x.digest()
|
||||
self.assertEqual(d0, d1)
|
||||
self.assertEqual(d0, d2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -4,7 +4,7 @@ from trezor.crypto import hashlib
|
||||
|
||||
class TestCryptoBlake2s(unittest.TestCase):
|
||||
|
||||
# vectors from http://www.di-mgt.com.au/sha_testvectors.html
|
||||
# vectors from https://raw.githubusercontent.com/BLAKE2/BLAKE2/master/testvectors/blake2s-kat.txt
|
||||
|
||||
vectors = [
|
||||
('', '48a8997da407876b3d79c0d92325ad3b89cbb754d86ab71aee047ad345fd2c49'),
|
||||
|
2
vendor/trezor-crypto
vendored
2
vendor/trezor-crypto
vendored
@ -1 +1 @@
|
||||
Subproject commit 3b97a8b34cd6f5cdf522fbf7a497d5441dcb9c08
|
||||
Subproject commit 9a2310fc53a4d03c19591cbb55d85436f7573971
|
Loading…
Reference in New Issue
Block a user