2017-04-03 19:29:52 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2017-04-03 19:29:52 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-03 19:29:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
2017-12-12 19:31:03 +00:00
|
|
|
#include "rfc6979.h"
|
2017-04-03 19:29:52 +00:00
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// class Rfc6979:
|
|
|
|
/// '''
|
|
|
|
/// RFC6979 context.
|
|
|
|
/// '''
|
2017-04-03 19:29:52 +00:00
|
|
|
typedef struct _mp_obj_Rfc6979_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
rfc6979_state rng;
|
|
|
|
} mp_obj_Rfc6979_t;
|
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// def __init__(self, secret_key: bytes, hash: bytes) -> None:
|
|
|
|
/// '''
|
|
|
|
/// Initialize RFC6979 context from secret key and a hash.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
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) {
|
2017-04-03 19:29:52 +00:00
|
|
|
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) {
|
2017-06-14 15:40:50 +00:00
|
|
|
mp_raise_ValueError("Secret key has to be 32 bytes long");
|
2017-04-03 19:29:52 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// def next(self) -> bytes:
|
2017-04-03 19:29:52 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Compute next 32-bytes of pseudorandom data.
|
2017-04-03 19:29:52 +00:00
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_Rfc6979_next(mp_obj_t self) {
|
2017-04-03 19:29:52 +00:00
|
|
|
mp_obj_Rfc6979_t *o = MP_OBJ_TO_PTR(self);
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[32];
|
|
|
|
generate_rfc6979(out, &(o->rng));
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2017-04-03 19:29:52 +00:00
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Rfc6979_next_obj, mod_trezorcrypto_Rfc6979_next);
|
2017-04-03 19:29:52 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
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) },
|
2017-04-03 19:29:52 +00:00
|
|
|
};
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Rfc6979_locals_dict, mod_trezorcrypto_Rfc6979_locals_dict_table);
|
2017-04-03 19:29:52 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC const mp_obj_type_t mod_trezorcrypto_Rfc6979_type = {
|
2017-04-03 19:29:52 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Rfc6979,
|
2017-06-14 16:47:38 +00:00
|
|
|
.make_new = mod_trezorcrypto_Rfc6979_make_new,
|
|
|
|
.locals_dict = (void*)&mod_trezorcrypto_Rfc6979_locals_dict,
|
2017-04-03 19:29:52 +00:00
|
|
|
};
|