From eef147d810290b58baa17dd8727f5ac775f2b9c5 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 3 Apr 2017 21:29:52 +0200 Subject: [PATCH] modtrezorcrypto: add crypto.rfc6979 class --- .../modtrezorcrypto/modtrezorcrypto-random.h | 3 - .../modtrezorcrypto/modtrezorcrypto-rfc6979.h | 57 +++++++++++++++++++ .../extmod/modtrezorcrypto/modtrezorcrypto.c | 2 + src/trezor/crypto/__init__.py | 1 + tests/test_trezor.crypto.rfc6979.py | 38 +++++++++++++ vendor/trezor-crypto | 2 +- 6 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 micropython/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h create mode 100644 tests/test_trezor.crypto.rfc6979.py diff --git a/micropython/extmod/modtrezorcrypto/modtrezorcrypto-random.h b/micropython/extmod/modtrezorcrypto/modtrezorcrypto-random.h index c8853105fd..dc5bbc4513 100644 --- a/micropython/extmod/modtrezorcrypto/modtrezorcrypto-random.h +++ b/micropython/extmod/modtrezorcrypto/modtrezorcrypto-random.h @@ -78,6 +78,3 @@ STATIC const mp_obj_type_t mod_TrezorCrypto_Random_type = { .make_new = mod_TrezorCrypto_Random_make_new, .locals_dict = (void*)&mod_TrezorCrypto_Random_locals_dict, }; - - - diff --git a/micropython/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h b/micropython/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h new file mode 100644 index 0000000000..681481a89b --- /dev/null +++ b/micropython/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#include "py/objstr.h" + +#include "trezor-crypto/rfc6979.h" + +typedef struct _mp_obj_Rfc6979_t { + mp_obj_base_t base; + rfc6979_state rng; +} mp_obj_Rfc6979_t; + +STATIC mp_obj_t mod_TrezorCrypto_Rfc6979_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, 2, 2, false); + mp_obj_Rfc6979_t *o = m_new_obj(mp_obj_Rfc6979_t); + o->base.type = type; + mp_buffer_info_t pkey, hash; + mp_get_buffer_raise(args[0], &pkey, MP_BUFFER_READ); + mp_get_buffer_raise(args[1], &hash, MP_BUFFER_READ); + if (pkey.len != 32) { + mp_raise_ValueError("Private key has to be 32 bytes long"); + } + if (hash.len != 32) { + mp_raise_ValueError("Hash has to be 32 bytes long"); + } + init_rfc6979((const uint8_t *)pkey.buf, (const uint8_t *)hash.buf, &(o->rng)); + return MP_OBJ_FROM_PTR(o); +} + +/// def trezor.crypto.rfc6979.next() -> bytes: +/// ''' +/// Compute next 32-bytes of pseudorandom data +/// ''' +STATIC mp_obj_t mod_TrezorCrypto_Rfc6979_next(mp_obj_t self) { + mp_obj_Rfc6979_t *o = MP_OBJ_TO_PTR(self); + vstr_t vstr; + vstr_init_len(&vstr, 32); + generate_rfc6979((uint8_t *)vstr.buf, &(o->rng)); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Rfc6979_next_obj, mod_TrezorCrypto_Rfc6979_next); + +STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Rfc6979_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&mod_TrezorCrypto_Rfc6979_next_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Rfc6979_locals_dict, mod_TrezorCrypto_Rfc6979_locals_dict_table); + +STATIC const mp_obj_type_t mod_TrezorCrypto_Rfc6979_type = { + { &mp_type_type }, + .name = MP_QSTR_Rfc6979, + .make_new = mod_TrezorCrypto_Rfc6979_make_new, + .locals_dict = (void*)&mod_TrezorCrypto_Rfc6979_locals_dict, +}; diff --git a/micropython/extmod/modtrezorcrypto/modtrezorcrypto.c b/micropython/extmod/modtrezorcrypto/modtrezorcrypto.c index 68459e5351..2c0f19ca3d 100644 --- a/micropython/extmod/modtrezorcrypto/modtrezorcrypto.c +++ b/micropython/extmod/modtrezorcrypto/modtrezorcrypto.c @@ -22,6 +22,7 @@ #include "modtrezorcrypto-ed25519.h" #include "modtrezorcrypto-pbkdf2.h" #include "modtrezorcrypto-random.h" +#include "modtrezorcrypto-rfc6979.h" #include "modtrezorcrypto-ripemd160.h" #include "modtrezorcrypto-nist256p1.h" #include "modtrezorcrypto-secp256k1.h" @@ -44,6 +45,7 @@ STATIC const mp_rom_map_elem_t mp_module_TrezorCrypto_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Nist256p1), MP_ROM_PTR(&mod_TrezorCrypto_Nist256p1_type) }, { MP_ROM_QSTR(MP_QSTR_Pbkdf2), MP_ROM_PTR(&mod_TrezorCrypto_Pbkdf2_type) }, { MP_ROM_QSTR(MP_QSTR_Random), MP_ROM_PTR(&mod_TrezorCrypto_Random_type) }, + { MP_ROM_QSTR(MP_QSTR_Rfc6979), MP_ROM_PTR(&mod_TrezorCrypto_Rfc6979_type) }, { MP_ROM_QSTR(MP_QSTR_Ripemd160), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160_type) }, { MP_ROM_QSTR(MP_QSTR_Secp256k1), MP_ROM_PTR(&mod_TrezorCrypto_Secp256k1_type) }, { MP_ROM_QSTR(MP_QSTR_Sha1), MP_ROM_PTR(&mod_TrezorCrypto_Sha1_type) }, diff --git a/src/trezor/crypto/__init__.py b/src/trezor/crypto/__init__.py index 8562243858..f51a819db4 100644 --- a/src/trezor/crypto/__init__.py +++ b/src/trezor/crypto/__init__.py @@ -1,6 +1,7 @@ from TrezorCrypto import Bip32 from TrezorCrypto import Bip39 from TrezorCrypto import Pbkdf2 as pbkdf2 +from TrezorCrypto import Rfc6979 as rfc6979 from TrezorCrypto import Random from TrezorCrypto import SSSS diff --git a/tests/test_trezor.crypto.rfc6979.py b/tests/test_trezor.crypto.rfc6979.py new file mode 100644 index 0000000000..328d017e64 --- /dev/null +++ b/tests/test_trezor.crypto.rfc6979.py @@ -0,0 +1,38 @@ +from common import * + +from trezor.crypto import rfc6979 +from trezor.crypto.hashlib import sha256 + +class TestCryptoRfc6979(unittest.TestCase): + + def test_vectors(self): + + vectors = [ + ("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721", + "sample", + "a6e3c57dd01abe90086538398355dd4c3b17aa873382b0f24d6129493d8aad60"), + ("cca9fbcc1b41e5a95d369eaa6ddcff73b61a4efaa279cfc6567e8daa39cbaf50", + "sample", + "2df40ca70e639d89528a6b670d9d48d9165fdc0febc0974056bdce192b8e16a3"), + ("0000000000000000000000000000000000000000000000000000000000000001", + "Satoshi Nakamoto", + "8f8a276c19f4149656b280621e358cce24f5f52542772691ee69063b74f15d15"), + ("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", + "Satoshi Nakamoto", + "33a19b60e25fb6f4435af53a3d42d493644827367e6453928554f43e49aa6f90"), + ("f8b8af8ce3c7cca5e300d33939540c10d45ce001b8f252bfbc57ba0342904181", + "Alan Turing", "525a82b70e67874398067543fd84c83d30c175fdc45fdeee082fe13b1d7cfdf1"), + ("0000000000000000000000000000000000000000000000000000000000000001", + "All those moments will be lost in time, like tears in rain. Time to die...", + "38aa22d72376b4dbc472e06c3ba403ee0a394da63fc58d88686c611aba98d6b3"), + ("e91671c46231f833a6406ccbea0e3e392c76c167bac1cb013f6f1013980455c2", + "There is a computer disease that anybody who works with computers knows about. It's a very serious disease and it interferes completely with the work. The trouble with computers is that you 'play' with them!", + "1f4b84c23a86a221d233f2521be018d9318639d5b8bbd6374a8a59232d16ad3d"), + ] + + for key, msg, k in vectors: + rng = rfc6979(unhexlify(key), sha256(msg).digest()) + self.assertEqual(rng.next(), unhexlify(k)) + +if __name__ == '__main__': + unittest.main() diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto index 3d04064384..e15a7bc986 160000 --- a/vendor/trezor-crypto +++ b/vendor/trezor-crypto @@ -1 +1 @@ -Subproject commit 3d04064384b3f4e8331bdcfc96d2c7e5fc04a269 +Subproject commit e15a7bc986667902bf31f2f2cc623e540168f7f0