mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 14:28:07 +00:00
trezor.crypto: rework for new structure
This commit is contained in:
parent
c94b4f68df
commit
afd65833cb
@ -235,38 +235,21 @@ STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type = {
|
||||
.locals_dict = (void*)&mod_trezorcrypto_HDNode_locals_dict,
|
||||
};
|
||||
|
||||
/// class Bip32:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_Bip32_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Bip32_t;
|
||||
|
||||
/// def __init__(self):
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip32_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, 0, false);
|
||||
mp_obj_Bip32_t *o = m_new_obj(mp_obj_Bip32_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def deserialize(self, value: str, version_public: int, version_private: int) -> HDNode:
|
||||
/// '''
|
||||
/// Construct a BIP0032 HD node from a base58-serialized value.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip32_deserialize(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t value;
|
||||
mp_get_buffer_raise(args[1], &value, MP_BUFFER_READ);
|
||||
if (value.len == 0) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip32_deserialize(mp_obj_t value, mp_obj_t version_public, mp_obj_t version_private) {
|
||||
mp_buffer_info_t valueb;
|
||||
mp_get_buffer_raise(value, &valueb, MP_BUFFER_READ);
|
||||
if (valueb.len == 0) {
|
||||
mp_raise_ValueError("Invalid value");
|
||||
}
|
||||
uint32_t version_public = mp_obj_get_int_truncated(args[2]);
|
||||
uint32_t version_private = mp_obj_get_int_truncated(args[3]);
|
||||
uint32_t vpub = mp_obj_get_int_truncated(version_public);
|
||||
uint32_t vpriv = mp_obj_get_int_truncated(version_private);
|
||||
HDNode hdnode;
|
||||
uint32_t fingerprint;
|
||||
if (hdnode_deserialize(value.buf, version_public, version_private, &hdnode, &fingerprint) < 0) {
|
||||
if (hdnode_deserialize(valueb.buf, vpub, vpriv, &hdnode, &fingerprint) < 0) {
|
||||
mp_raise_ValueError("Failed to deserialize");
|
||||
}
|
||||
|
||||
@ -276,13 +259,13 @@ STATIC mp_obj_t mod_trezorcrypto_Bip32_deserialize(size_t n_args, const mp_obj_t
|
||||
o->fingerprint = fingerprint;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Bip32_deserialize_obj, 4, 4, mod_trezorcrypto_Bip32_deserialize);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_bip32_deserialize_obj, mod_trezorcrypto_bip32_deserialize);
|
||||
|
||||
/// def from_seed(self, seed: bytes, curve_name: str) -> HDNode:
|
||||
/// def from_seed(seed: bytes, curve_name: str) -> HDNode:
|
||||
/// '''
|
||||
/// Construct a BIP0032 HD node from a BIP0039 seed value.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip32_from_seed(mp_obj_t self, mp_obj_t seed, mp_obj_t curve_name) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed, mp_obj_t curve_name) {
|
||||
mp_buffer_info_t seedb;
|
||||
mp_get_buffer_raise(seed, &seedb, MP_BUFFER_READ);
|
||||
if (seedb.len == 0) {
|
||||
@ -302,17 +285,17 @@ STATIC mp_obj_t mod_trezorcrypto_Bip32_from_seed(mp_obj_t self, mp_obj_t seed, m
|
||||
o->hdnode = hdnode;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Bip32_from_seed_obj, mod_trezorcrypto_Bip32_from_seed);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_seed_obj, mod_trezorcrypto_bip32_from_seed);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Bip32_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_deserialize), MP_ROM_PTR(&mod_trezorcrypto_Bip32_deserialize_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_from_seed), MP_ROM_PTR(&mod_trezorcrypto_Bip32_from_seed_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip32_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip32) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_HDNode), MP_ROM_PTR(&mod_trezorcrypto_HDNode_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_deserialize), MP_ROM_PTR(&mod_trezorcrypto_bip32_deserialize_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_from_seed), MP_ROM_PTR(&mod_trezorcrypto_bip32_from_seed_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Bip32_locals_dict, mod_trezorcrypto_Bip32_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip32_globals, mod_trezorcrypto_bip32_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Bip32_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Bip32,
|
||||
.make_new = mod_trezorcrypto_Bip32_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Bip32_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_bip32_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_bip32_globals,
|
||||
};
|
||||
|
@ -9,28 +9,11 @@
|
||||
|
||||
#include "trezor-crypto/bip39.h"
|
||||
|
||||
/// class Bip39:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_Bip39_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Bip39_t;
|
||||
|
||||
/// def __init__(self):
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_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, 0, false);
|
||||
mp_obj_Bip39_t *o = m_new_obj(mp_obj_Bip39_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def find_word(self, prefix: str) -> Optional[str]:
|
||||
/// def find_word(prefix: str) -> Optional[str]:
|
||||
/// '''
|
||||
/// Return the first word from the wordlist starting with prefix.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_find_word(mp_obj_t self, mp_obj_t prefix)
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip39_find_word(mp_obj_t prefix)
|
||||
{
|
||||
mp_buffer_info_t pfx;
|
||||
mp_get_buffer_raise(prefix, &pfx, MP_BUFFER_READ);
|
||||
@ -44,14 +27,14 @@ STATIC mp_obj_t mod_trezorcrypto_Bip39_find_word(mp_obj_t self, mp_obj_t prefix)
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Bip39_find_word_obj, mod_trezorcrypto_Bip39_find_word);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_find_word_obj, mod_trezorcrypto_bip39_find_word);
|
||||
|
||||
/// def complete_word(self, prefix: str) -> int:
|
||||
/// def complete_word(prefix: str) -> int:
|
||||
/// '''
|
||||
/// Return possible 1-letter suffixes for given word prefix.
|
||||
/// Result is a bitmask, with 'a' on the lowest bit, 'b' on the second lowest, etc.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_complete_word(mp_obj_t self, mp_obj_t prefix)
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip39_complete_word(mp_obj_t prefix)
|
||||
{
|
||||
mp_buffer_info_t pfx;
|
||||
mp_get_buffer_raise(prefix, &pfx, MP_BUFFER_READ);
|
||||
@ -71,13 +54,13 @@ STATIC mp_obj_t mod_trezorcrypto_Bip39_complete_word(mp_obj_t self, mp_obj_t pre
|
||||
}
|
||||
return mp_obj_new_int(res);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Bip39_complete_word_obj, mod_trezorcrypto_Bip39_complete_word);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_complete_word_obj, mod_trezorcrypto_bip39_complete_word);
|
||||
|
||||
/// def generate(self, strength: int) -> str:
|
||||
/// def generate(strength: int) -> str:
|
||||
/// '''
|
||||
/// Generate a mnemonic of given strength (128, 160, 192, 224 and 256 bits).
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_generate(mp_obj_t self, mp_obj_t strength) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip39_generate(mp_obj_t strength) {
|
||||
int bits = mp_obj_get_int(strength);
|
||||
if (bits % 32 || bits < 128 || bits > 256) {
|
||||
mp_raise_ValueError("Invalid bit strength (only 128, 160, 192, 224 and 256 values are allowed)");
|
||||
@ -85,13 +68,13 @@ STATIC mp_obj_t mod_trezorcrypto_Bip39_generate(mp_obj_t self, mp_obj_t strength
|
||||
const char *mnemo = mnemonic_generate(bits);
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (uint8_t *)mnemo, strlen(mnemo));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Bip39_generate_obj, mod_trezorcrypto_Bip39_generate);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_generate_obj, mod_trezorcrypto_bip39_generate);
|
||||
|
||||
/// def from_data(self, data: bytes) -> str:
|
||||
/// def from_data(data: bytes) -> str:
|
||||
/// '''
|
||||
/// Generate a mnemonic from given data (of 16, 20, 24, 28 and 32 bytes).
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_from_data(mp_obj_t self, mp_obj_t data) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip39_from_data(mp_obj_t data) {
|
||||
mp_buffer_info_t bin;
|
||||
mp_get_buffer_raise(data, &bin, MP_BUFFER_READ);
|
||||
if (bin.len % 4 || bin.len < 16 || bin.len > 32) {
|
||||
@ -100,24 +83,24 @@ STATIC mp_obj_t mod_trezorcrypto_Bip39_from_data(mp_obj_t self, mp_obj_t data) {
|
||||
const char *mnemo = mnemonic_from_data(bin.buf, bin.len);
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (uint8_t *)mnemo, strlen(mnemo));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Bip39_from_data_obj, mod_trezorcrypto_Bip39_from_data);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_from_data_obj, mod_trezorcrypto_bip39_from_data);
|
||||
|
||||
/// def check(self, mnemonic: str) -> bool:
|
||||
/// def check(mnemonic: str) -> bool:
|
||||
/// '''
|
||||
/// Check whether given mnemonic is valid.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_check(mp_obj_t self, mp_obj_t mnemonic) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip39_check(mp_obj_t mnemonic) {
|
||||
mp_buffer_info_t text;
|
||||
mp_get_buffer_raise(mnemonic, &text, MP_BUFFER_READ);
|
||||
return (text.len > 0 && mnemonic_check(text.buf)) ? mp_const_true : mp_const_false;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Bip39_check_obj, mod_trezorcrypto_Bip39_check);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_check_obj, mod_trezorcrypto_bip39_check);
|
||||
|
||||
/// def seed(self, mnemonic: str, passphrase: str) -> bytes:
|
||||
/// def seed(mnemonic: str, passphrase: str) -> bytes:
|
||||
/// '''
|
||||
/// Generate seed from mnemonic and passphrase.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Bip39_seed(mp_obj_t self, mp_obj_t mnemonic, mp_obj_t passphrase) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_bip39_seed(mp_obj_t mnemonic, mp_obj_t passphrase) {
|
||||
mp_buffer_info_t mnemo;
|
||||
mp_buffer_info_t phrase;
|
||||
mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ);
|
||||
@ -129,21 +112,20 @@ STATIC mp_obj_t mod_trezorcrypto_Bip39_seed(mp_obj_t self, mp_obj_t mnemonic, mp
|
||||
mnemonic_to_seed(pmnemonic, ppassphrase, (uint8_t *)vstr.buf, NULL); // no callback for now
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Bip39_seed_obj, mod_trezorcrypto_Bip39_seed);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip39_seed_obj, mod_trezorcrypto_bip39_seed);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Bip39_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_find_word), MP_ROM_PTR(&mod_trezorcrypto_Bip39_find_word_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_complete_word), MP_ROM_PTR(&mod_trezorcrypto_Bip39_complete_word_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate), MP_ROM_PTR(&mod_trezorcrypto_Bip39_generate_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_from_data), MP_ROM_PTR(&mod_trezorcrypto_Bip39_from_data_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_check), MP_ROM_PTR(&mod_trezorcrypto_Bip39_check_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_trezorcrypto_Bip39_seed_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip39_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip39) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_find_word), MP_ROM_PTR(&mod_trezorcrypto_bip39_find_word_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_complete_word), MP_ROM_PTR(&mod_trezorcrypto_bip39_complete_word_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate), MP_ROM_PTR(&mod_trezorcrypto_bip39_generate_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_from_data), MP_ROM_PTR(&mod_trezorcrypto_bip39_from_data_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_check), MP_ROM_PTR(&mod_trezorcrypto_bip39_check_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_trezorcrypto_bip39_seed_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Bip39_locals_dict, mod_trezorcrypto_Bip39_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip39_globals, mod_trezorcrypto_bip39_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Bip39_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Bip39,
|
||||
.make_new = mod_trezorcrypto_Bip39_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Bip39_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_bip39_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_bip39_globals,
|
||||
};
|
||||
|
@ -11,28 +11,11 @@
|
||||
|
||||
#include "rand.h"
|
||||
|
||||
/// class Curve25519:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_Curve25519_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Curve25519_t;
|
||||
|
||||
/// def __init__(self) -> None:
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Curve25519_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, 0, false);
|
||||
mp_obj_Curve25519_t *o = m_new_obj(mp_obj_Curve25519_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def generate_secret(self) -> bytes:
|
||||
/// def generate_secret() -> bytes:
|
||||
/// '''
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Curve25519_generate_secret(mp_obj_t self) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_curve25519_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
random_buffer((uint8_t *)vstr.buf, 32);
|
||||
@ -42,13 +25,13 @@ STATIC mp_obj_t mod_trezorcrypto_Curve25519_generate_secret(mp_obj_t self) {
|
||||
vstr.buf[31] |= 64;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Curve25519_generate_secret_obj, mod_trezorcrypto_Curve25519_generate_secret);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_curve25519_generate_secret_obj, mod_trezorcrypto_curve25519_generate_secret);
|
||||
|
||||
/// def publickey(self, secret_key: bytes) -> bytes:
|
||||
/// def publickey(secret_key: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Computes public key from secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Curve25519_publickey(mp_obj_t self, mp_obj_t secret_key) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_curve25519_publickey(mp_obj_t secret_key) {
|
||||
mp_buffer_info_t sk;
|
||||
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
||||
if (sk.len != 32) {
|
||||
@ -59,14 +42,14 @@ STATIC mp_obj_t mod_trezorcrypto_Curve25519_publickey(mp_obj_t self, mp_obj_t se
|
||||
curve25519_scalarmult_basepoint((uint8_t *)vstr.buf, (const uint8_t *)sk.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Curve25519_publickey_obj, mod_trezorcrypto_Curve25519_publickey);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_curve25519_publickey_obj, mod_trezorcrypto_curve25519_publickey);
|
||||
|
||||
/// def multiply(self, secret_key: bytes, public_key: bytes) -> bytes:
|
||||
/// def multiply(secret_key: bytes, public_key: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Multiplies point defined by public_key with scalar defined by secret_key.
|
||||
/// Useful for ECDH.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Curve25519_multiply(mp_obj_t self, mp_obj_t secret_key, mp_obj_t public_key) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_curve25519_multiply(mp_obj_t secret_key, mp_obj_t public_key) {
|
||||
mp_buffer_info_t sk, pk;
|
||||
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(public_key, &pk, MP_BUFFER_READ);
|
||||
@ -81,18 +64,17 @@ STATIC mp_obj_t mod_trezorcrypto_Curve25519_multiply(mp_obj_t self, mp_obj_t sec
|
||||
curve25519_scalarmult((uint8_t *)vstr.buf, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Curve25519_multiply_obj, mod_trezorcrypto_Curve25519_multiply);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_curve25519_multiply_obj, mod_trezorcrypto_curve25519_multiply);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Curve25519_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_Curve25519_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_Curve25519_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_Curve25519_multiply_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_curve25519_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_curve25519) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_curve25519_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_curve25519_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_curve25519_multiply_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Curve25519_locals_dict, mod_trezorcrypto_Curve25519_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_curve25519_globals, mod_trezorcrypto_curve25519_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Curve25519_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Curve25519,
|
||||
.make_new = mod_trezorcrypto_Curve25519_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Curve25519_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_curve25519_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_curve25519_globals,
|
||||
};
|
||||
|
@ -11,28 +11,11 @@
|
||||
|
||||
#include "rand.h"
|
||||
|
||||
/// class Ed25519:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_Ed25519_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Ed25519_t;
|
||||
|
||||
/// def __init__(self) -> None:
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_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, 0, false);
|
||||
mp_obj_Ed25519_t *o = m_new_obj(mp_obj_Ed25519_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def generate_secret(self) -> bytes:
|
||||
/// def generate_secret() -> bytes:
|
||||
/// '''
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_generate_secret(mp_obj_t self) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
random_buffer((uint8_t *)vstr.buf, 32);
|
||||
@ -42,13 +25,13 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_generate_secret(mp_obj_t self) {
|
||||
vstr.buf[31] |= 64;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Ed25519_generate_secret_obj, mod_trezorcrypto_Ed25519_generate_secret);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_ed25519_generate_secret_obj, mod_trezorcrypto_ed25519_generate_secret);
|
||||
|
||||
/// def publickey(self, secret_key: bytes) -> bytes:
|
||||
/// def publickey(secret_key: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Computes public key from secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_publickey(mp_obj_t self, mp_obj_t secret_key) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_publickey(mp_obj_t secret_key) {
|
||||
mp_buffer_info_t sk;
|
||||
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
||||
if (sk.len != 32) {
|
||||
@ -59,13 +42,13 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_publickey(mp_obj_t self, mp_obj_t secre
|
||||
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, *(ed25519_public_key *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Ed25519_publickey_obj, mod_trezorcrypto_Ed25519_publickey);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ed25519_publickey_obj, mod_trezorcrypto_ed25519_publickey);
|
||||
|
||||
/// def sign(self, secret_key: bytes, message: bytes) -> bytes:
|
||||
/// def sign(secret_key: bytes, message: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Uses secret key to produce the signature of message.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_sign(mp_obj_t self, mp_obj_t secret_key, mp_obj_t message) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_sign(mp_obj_t secret_key, mp_obj_t message) {
|
||||
mp_buffer_info_t sk, msg;
|
||||
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(message, &msg, MP_BUFFER_READ);
|
||||
@ -82,18 +65,18 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_sign(mp_obj_t self, mp_obj_t secret_key
|
||||
ed25519_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, pk, *(ed25519_signature *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Ed25519_sign_obj, mod_trezorcrypto_Ed25519_sign);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ed25519_sign_obj, mod_trezorcrypto_ed25519_sign);
|
||||
|
||||
/// def verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool:
|
||||
/// def verify(public_key: bytes, signature: bytes, message: bytes) -> bool:
|
||||
/// '''
|
||||
/// Uses public key to verify the signature of the message.
|
||||
/// Returns True on success.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_verify(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_verify(mp_obj_t public_key, mp_obj_t signature, mp_obj_t message) {
|
||||
mp_buffer_info_t pk, sig, msg;
|
||||
mp_get_buffer_raise(args[1], &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[3], &msg, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(public_key, &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(signature, &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(message, &msg, MP_BUFFER_READ);
|
||||
if (pk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of public key");
|
||||
}
|
||||
@ -105,13 +88,13 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_verify(size_t n_args, const mp_obj_t *a
|
||||
}
|
||||
return (0 == ed25519_sign_open(msg.buf, msg.len, *(const ed25519_public_key *)pk.buf, *(const ed25519_signature *)sig.buf)) ? mp_const_true : mp_const_false;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Ed25519_verify_obj, 4, 4, mod_trezorcrypto_Ed25519_verify);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_ed25519_verify_obj, mod_trezorcrypto_ed25519_verify);
|
||||
|
||||
/// def cosi_combine_publickeys(self, public_keys: List[bytes]) -> bytes:
|
||||
/// def cosi_combine_publickeys(public_keys: List[bytes]) -> bytes:
|
||||
/// '''
|
||||
/// Combines a list of public keys used in COSI cosigning scheme.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_cosi_combine_publickeys(mp_obj_t self, mp_obj_t public_keys) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_combine_publickeys(mp_obj_t public_keys) {
|
||||
size_t pklen;
|
||||
mp_obj_t *pkitems;
|
||||
mp_obj_get_array(public_keys, &pklen, &pkitems);
|
||||
@ -134,13 +117,13 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_cosi_combine_publickeys(mp_obj_t self,
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Ed25519_cosi_combine_publickeys_obj, mod_trezorcrypto_Ed25519_cosi_combine_publickeys);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ed25519_cosi_combine_publickeys_obj, mod_trezorcrypto_ed25519_cosi_combine_publickeys);
|
||||
|
||||
/// def cosi_combine_signatures(self, R: bytes, signatures: List[bytes]) -> bytes:
|
||||
/// def cosi_combine_signatures(R: bytes, signatures: List[bytes]) -> bytes:
|
||||
/// '''
|
||||
/// Combines a list of signatures used in COSI cosigning scheme.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_cosi_combine_signatures(mp_obj_t self, mp_obj_t R, mp_obj_t signatures) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_combine_signatures(mp_obj_t R, mp_obj_t signatures) {
|
||||
mp_buffer_info_t sigR;
|
||||
mp_get_buffer_raise(R, &sigR, MP_BUFFER_READ);
|
||||
if (sigR.len != 32) {
|
||||
@ -166,19 +149,19 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_cosi_combine_signatures(mp_obj_t self,
|
||||
ed25519_cosi_combine_signatures(*(ed25519_signature *)vstr.buf, *(const ed25519_public_key *)sigR.buf, sigs, siglen);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Ed25519_cosi_combine_signatures_obj, mod_trezorcrypto_Ed25519_cosi_combine_signatures);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ed25519_cosi_combine_signatures_obj, mod_trezorcrypto_ed25519_cosi_combine_signatures);
|
||||
|
||||
/// def cosi_sign(self, secret_key: bytes, message: bytes, nonce: bytes, sigR: bytes, combined_pubkey: bytes) -> bytes:
|
||||
/// def cosi_sign(secret_key: bytes, message: bytes, nonce: bytes, sigR: bytes, combined_pubkey: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Produce signature of message using COSI cosigning scheme.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ed25519_cosi_sign(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_sign(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t sk, msg, nonce, sigR, pk;
|
||||
mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &msg, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[3], &nonce, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[4], &sigR, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[5], &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[1], &msg, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &nonce, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[3], &sigR, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[4], &pk, MP_BUFFER_READ);
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
@ -196,22 +179,21 @@ STATIC mp_obj_t mod_trezorcrypto_Ed25519_cosi_sign(size_t n_args, const mp_obj_t
|
||||
ed25519_cosi_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, *(const ed25519_secret_key *)nonce.buf, *(const ed25519_public_key *)sigR.buf, *(const ed25519_secret_key *)pk.buf, *(ed25519_cosi_signature *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Ed25519_cosi_sign_obj, 6, 6, mod_trezorcrypto_Ed25519_cosi_sign);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_ed25519_cosi_sign_obj, 5, 5, mod_trezorcrypto_ed25519_cosi_sign);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Ed25519_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_sign_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_verify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cosi_combine_publickeys), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_cosi_combine_publickeys_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cosi_combine_signatures), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_cosi_combine_signatures_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cosi_sign), MP_ROM_PTR(&mod_trezorcrypto_Ed25519_cosi_sign_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_ed25519_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ed25519) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_ed25519_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_ed25519_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_ed25519_sign_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_ed25519_verify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cosi_combine_publickeys), MP_ROM_PTR(&mod_trezorcrypto_ed25519_cosi_combine_publickeys_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cosi_combine_signatures), MP_ROM_PTR(&mod_trezorcrypto_ed25519_cosi_combine_signatures_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cosi_sign), MP_ROM_PTR(&mod_trezorcrypto_ed25519_cosi_sign_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Ed25519_locals_dict, mod_trezorcrypto_Ed25519_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_ed25519_globals, mod_trezorcrypto_ed25519_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Ed25519_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Ed25519,
|
||||
.make_new = mod_trezorcrypto_Ed25519_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Ed25519_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_ed25519_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_ed25519_globals,
|
||||
};
|
||||
|
@ -10,28 +10,11 @@
|
||||
#include "trezor-crypto/ecdsa.h"
|
||||
#include "trezor-crypto/nist256p1.h"
|
||||
|
||||
/// class Nist256p1:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_Nist256p1_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Nist256p1_t;
|
||||
|
||||
/// def __init__(self) -> None:
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_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, 0, false);
|
||||
mp_obj_Nist256p1_t *o = m_new_obj(mp_obj_Nist256p1_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def generate_secret(self) -> bytes:
|
||||
/// def generate_secret() -> bytes:
|
||||
/// '''
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_generate_secret(mp_obj_t self) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
for (;;) {
|
||||
@ -43,19 +26,19 @@ STATIC mp_obj_t mod_trezorcrypto_Nist256p1_generate_secret(mp_obj_t self) {
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Nist256p1_generate_secret_obj, mod_trezorcrypto_Nist256p1_generate_secret);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_nist256p1_generate_secret_obj, mod_trezorcrypto_nist256p1_generate_secret);
|
||||
|
||||
/// def publickey(self, secret_key: bytes, compressed: bool = True) -> bytes:
|
||||
/// def publickey(secret_key: bytes, compressed: bool = True) -> bytes:
|
||||
/// '''
|
||||
/// Computes public key from secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_publickey(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_publickey(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t sk;
|
||||
mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
bool compressed = n_args < 3 || args[2] == mp_const_true;
|
||||
bool compressed = n_args < 2 || args[1] == mp_const_true;
|
||||
vstr_t vstr;
|
||||
if (compressed) {
|
||||
vstr_init_len(&vstr, 33);
|
||||
@ -66,17 +49,17 @@ STATIC mp_obj_t mod_trezorcrypto_Nist256p1_publickey(size_t n_args, const mp_obj
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Nist256p1_publickey_obj, 2, 3, mod_trezorcrypto_Nist256p1_publickey);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_nist256p1_publickey_obj, 1, 2, mod_trezorcrypto_nist256p1_publickey);
|
||||
|
||||
/// def sign(self, secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes:
|
||||
/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes:
|
||||
/// '''
|
||||
/// Uses secret key to produce the signature of the digest.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_sign(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_sign(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t sk, dig;
|
||||
mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &dig, MP_BUFFER_READ);
|
||||
bool compressed = n_args < 4 || args[3] == mp_const_true;
|
||||
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[1], &dig, MP_BUFFER_READ);
|
||||
bool compressed = n_args < 3 || args[2] == mp_const_true;
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
@ -92,18 +75,18 @@ STATIC mp_obj_t mod_trezorcrypto_Nist256p1_sign(size_t n_args, const mp_obj_t *a
|
||||
vstr.buf[0] = 27 + pby + compressed * 4;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Nist256p1_sign_obj, 3, 4, mod_trezorcrypto_Nist256p1_sign);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_nist256p1_sign_obj, 2, 3, mod_trezorcrypto_nist256p1_sign);
|
||||
|
||||
/// def verify(self, public_key: bytes, signature: bytes, digest: bytes) -> bool:
|
||||
/// def verify(public_key: bytes, signature: bytes, digest: bytes) -> bool:
|
||||
/// '''
|
||||
/// Uses public key to verify the signature of the digest.
|
||||
/// Returns True on success.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_verify(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_verify(mp_obj_t public_key, mp_obj_t signature, mp_obj_t digest) {
|
||||
mp_buffer_info_t pk, sig, dig;
|
||||
mp_get_buffer_raise(args[1], &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[3], &dig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(public_key, &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(signature, &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(digest, &dig, MP_BUFFER_READ);
|
||||
if (pk.len != 33 && pk.len != 65) {
|
||||
mp_raise_ValueError("Invalid length of public key");
|
||||
}
|
||||
@ -116,14 +99,14 @@ STATIC mp_obj_t mod_trezorcrypto_Nist256p1_verify(size_t n_args, const mp_obj_t
|
||||
}
|
||||
return mp_obj_new_bool(0 == ecdsa_verify_digest(&nist256p1, (const uint8_t *)pk.buf, (const uint8_t *)sig.buf + offset, (const uint8_t *)dig.buf));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Nist256p1_verify_obj, 4, 4, mod_trezorcrypto_Nist256p1_verify);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_nist256p1_verify_obj, mod_trezorcrypto_nist256p1_verify);
|
||||
|
||||
/// def verify_recover(self, signature: bytes, digest: bytes) -> bytes:
|
||||
/// def verify_recover(signature: bytes, digest: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Uses signature of the digest to verify the digest and recover the public key.
|
||||
/// Returns public key on success, None on failure.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_verify_recover(mp_obj_t self, mp_obj_t signature, mp_obj_t digest) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_verify_recover(mp_obj_t signature, mp_obj_t digest) {
|
||||
mp_buffer_info_t sig, dig;
|
||||
mp_get_buffer_raise(signature, &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(digest, &dig, MP_BUFFER_READ);
|
||||
@ -151,14 +134,14 @@ STATIC mp_obj_t mod_trezorcrypto_Nist256p1_verify_recover(mp_obj_t self, mp_obj_
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Nist256p1_verify_recover_obj, mod_trezorcrypto_Nist256p1_verify_recover);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nist256p1_verify_recover_obj, mod_trezorcrypto_nist256p1_verify_recover);
|
||||
|
||||
/// def multiply(self, secret_key: bytes, public_key: bytes) -> bytes:
|
||||
/// def multiply(secret_key: bytes, public_key: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Multiplies point defined by public_key with scalar defined by secret_key
|
||||
/// Useful for ECDH
|
||||
/// Multiplies point defined by public_key with scalar defined by secret_key.
|
||||
/// Useful for ECDH.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Nist256p1_multiply(mp_obj_t self, mp_obj_t secret_key, mp_obj_t public_key) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_multiply(mp_obj_t secret_key, mp_obj_t public_key) {
|
||||
mp_buffer_info_t sk, pk;
|
||||
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(public_key, &pk, MP_BUFFER_READ);
|
||||
@ -175,21 +158,20 @@ STATIC mp_obj_t mod_trezorcrypto_Nist256p1_multiply(mp_obj_t self, mp_obj_t secr
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Nist256p1_multiply_obj, mod_trezorcrypto_Nist256p1_multiply);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nist256p1_multiply_obj, mod_trezorcrypto_nist256p1_multiply);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Nist256p1_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_Nist256p1_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_Nist256p1_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_Nist256p1_sign_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_Nist256p1_verify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_Nist256p1_verify_recover_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_Nist256p1_multiply_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_nist256p1_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_nist256p1) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_sign_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_verify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_verify_recover_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_multiply_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Nist256p1_locals_dict, mod_trezorcrypto_Nist256p1_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_nist256p1_globals, mod_trezorcrypto_nist256p1_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Nist256p1_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Nist256p1,
|
||||
.make_new = mod_trezorcrypto_Nist256p1_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Nist256p1_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_nist256p1_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_nist256p1_globals,
|
||||
};
|
||||
|
@ -9,41 +9,24 @@
|
||||
|
||||
#include "rand.h"
|
||||
|
||||
/// class Random:
|
||||
/// def uniform(n: int) -> int:
|
||||
/// '''
|
||||
/// Compute uniform random number from interval 0 ... n - 1.
|
||||
/// '''
|
||||
typedef struct _mp_obj_Random_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Random_t;
|
||||
|
||||
/// def __init__(self) -> None:
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Random_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, 0, false);
|
||||
mp_obj_Random_t *o = m_new_obj(mp_obj_Random_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def uniform(self, n: int) -> int:
|
||||
/// '''
|
||||
/// Compute uniform random number from interval 0 ... n - 1
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Random_uniform(mp_obj_t self, mp_obj_t n) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_random_uniform(mp_obj_t n) {
|
||||
uint32_t nn = mp_obj_get_int_truncated(n);
|
||||
if (nn == 0) {
|
||||
mp_raise_ValueError("Maximum can't be zero");
|
||||
}
|
||||
return mp_obj_new_int_from_uint(random_uniform(nn));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Random_uniform_obj, mod_trezorcrypto_Random_uniform);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_uniform_obj, mod_trezorcrypto_random_uniform);
|
||||
|
||||
/// def bytes(self, len: int) -> bytes:
|
||||
/// def bytes(len: int) -> bytes:
|
||||
/// '''
|
||||
/// Generate random bytes sequence of length len
|
||||
/// Generate random bytes sequence of length len.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Random_bytes(mp_obj_t self, mp_obj_t len) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_random_bytes(mp_obj_t len) {
|
||||
uint32_t l = mp_obj_get_int(len);
|
||||
if (l > 8192) {
|
||||
mp_raise_ValueError("Maximum requested size is 8192");
|
||||
@ -53,13 +36,13 @@ STATIC mp_obj_t mod_trezorcrypto_Random_bytes(mp_obj_t self, mp_obj_t len) {
|
||||
random_buffer((uint8_t *)vstr.buf, l);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Random_bytes_obj, mod_trezorcrypto_Random_bytes);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_bytes_obj, mod_trezorcrypto_random_bytes);
|
||||
|
||||
/// def shuffle(self, data: list) -> None:
|
||||
/// def shuffle(data: list) -> None:
|
||||
/// '''
|
||||
/// Shuffles items of given list (in-place)
|
||||
/// Shuffles items of given list (in-place).
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Random_shuffle(mp_obj_t self, mp_obj_t data) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_random_shuffle(mp_obj_t data) {
|
||||
size_t item_cnt;
|
||||
mp_obj_t *items;
|
||||
mp_obj_get_array(data, &item_cnt, &items);
|
||||
@ -69,18 +52,17 @@ STATIC mp_obj_t mod_trezorcrypto_Random_shuffle(mp_obj_t self, mp_obj_t data) {
|
||||
random_permute(items, sizeof(mp_obj_t *), item_cnt);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Random_shuffle_obj, mod_trezorcrypto_Random_shuffle);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_shuffle_obj, mod_trezorcrypto_random_shuffle);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Random_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_trezorcrypto_Random_uniform_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_bytes), MP_ROM_PTR(&mod_trezorcrypto_Random_bytes_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_shuffle), MP_ROM_PTR(&mod_trezorcrypto_Random_shuffle_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_random_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_random) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_trezorcrypto_random_uniform_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_bytes), MP_ROM_PTR(&mod_trezorcrypto_random_bytes_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_shuffle), MP_ROM_PTR(&mod_trezorcrypto_random_shuffle_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Random_locals_dict, mod_trezorcrypto_Random_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_random_globals, mod_trezorcrypto_random_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Random_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Random,
|
||||
.make_new = mod_trezorcrypto_Random_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Random_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_random_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_random_globals,
|
||||
};
|
||||
|
@ -10,28 +10,11 @@
|
||||
#include "trezor-crypto/ecdsa.h"
|
||||
#include "trezor-crypto/secp256k1.h"
|
||||
|
||||
/// class Secp256k1:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_Secp256k1_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_Secp256k1_t;
|
||||
|
||||
/// def __init__(self) -> None:
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_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, 0, false);
|
||||
mp_obj_Secp256k1_t *o = m_new_obj(mp_obj_Secp256k1_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def generate_secret(self, ) -> bytes:
|
||||
/// def generate_secret() -> bytes:
|
||||
/// '''
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_generate_secret(mp_obj_t self) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
for (;;) {
|
||||
@ -43,19 +26,19 @@ STATIC mp_obj_t mod_trezorcrypto_Secp256k1_generate_secret(mp_obj_t self) {
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Secp256k1_generate_secret_obj, mod_trezorcrypto_Secp256k1_generate_secret);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_secp256k1_generate_secret_obj, mod_trezorcrypto_secp256k1_generate_secret);
|
||||
|
||||
/// def publickey(self, secret_key: bytes, compressed: bool = True) -> bytes:
|
||||
/// def publickey(secret_key: bytes, compressed: bool = True) -> bytes:
|
||||
/// '''
|
||||
/// Computes public key from secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_publickey(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_publickey(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t sk;
|
||||
mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
bool compressed = n_args < 3 || args[2] == mp_const_true;
|
||||
bool compressed = n_args < 2 || args[1] == mp_const_true;
|
||||
vstr_t vstr;
|
||||
if (compressed) {
|
||||
vstr_init_len(&vstr, 33);
|
||||
@ -66,17 +49,17 @@ STATIC mp_obj_t mod_trezorcrypto_Secp256k1_publickey(size_t n_args, const mp_obj
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Secp256k1_publickey_obj, 2, 3, mod_trezorcrypto_Secp256k1_publickey);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_publickey_obj, 1, 2, mod_trezorcrypto_secp256k1_publickey);
|
||||
|
||||
/// def sign(self, secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes:
|
||||
/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes:
|
||||
/// '''
|
||||
/// Uses secret key to produce the signature of the digest.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_sign(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_sign(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t sk, dig;
|
||||
mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &dig, MP_BUFFER_READ);
|
||||
bool compressed = n_args < 4 || args[3] == mp_const_true;
|
||||
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[1], &dig, MP_BUFFER_READ);
|
||||
bool compressed = n_args < 3 || args[2] == mp_const_true;
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
@ -92,18 +75,18 @@ STATIC mp_obj_t mod_trezorcrypto_Secp256k1_sign(size_t n_args, const mp_obj_t *a
|
||||
vstr.buf[0] = 27 + pby + compressed * 4;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Secp256k1_sign_obj, 3, 4, mod_trezorcrypto_Secp256k1_sign);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_sign_obj, 2, 3, mod_trezorcrypto_secp256k1_sign);
|
||||
|
||||
/// def verify(self, public_key: bytes, signature: bytes, digest: bytes) -> bool:
|
||||
/// def verify(public_key: bytes, signature: bytes, digest: bytes) -> bool:
|
||||
/// '''
|
||||
/// Uses public key to verify the signature of the digest.
|
||||
/// Returns True on success.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_verify(size_t n_args, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_verify(mp_obj_t public_key, mp_obj_t signature, mp_obj_t digest) {
|
||||
mp_buffer_info_t pk, sig, dig;
|
||||
mp_get_buffer_raise(args[1], &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[2], &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(args[3], &dig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(public_key, &pk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(signature, &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(digest, &dig, MP_BUFFER_READ);
|
||||
if (pk.len != 33 && pk.len != 65) {
|
||||
mp_raise_ValueError("Invalid length of public key");
|
||||
}
|
||||
@ -116,14 +99,14 @@ STATIC mp_obj_t mod_trezorcrypto_Secp256k1_verify(size_t n_args, const mp_obj_t
|
||||
}
|
||||
return mp_obj_new_bool(0 == ecdsa_verify_digest(&secp256k1, (const uint8_t *)pk.buf, (const uint8_t *)sig.buf + offset, (const uint8_t *)dig.buf));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Secp256k1_verify_obj, 4, 4, mod_trezorcrypto_Secp256k1_verify);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_secp256k1_verify_obj, mod_trezorcrypto_secp256k1_verify);
|
||||
|
||||
/// def verify_recover(self, signature: bytes, digest: bytes) -> bytes:
|
||||
/// def verify_recover(signature: bytes, digest: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Uses signature of the digest to verify the digest and recover the public key.
|
||||
/// Returns public key on success, None on failure.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_verify_recover(mp_obj_t self, mp_obj_t signature, mp_obj_t digest) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_verify_recover(mp_obj_t signature, mp_obj_t digest) {
|
||||
mp_buffer_info_t sig, dig;
|
||||
mp_get_buffer_raise(signature, &sig, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(digest, &dig, MP_BUFFER_READ);
|
||||
@ -151,14 +134,14 @@ STATIC mp_obj_t mod_trezorcrypto_Secp256k1_verify_recover(mp_obj_t self, mp_obj_
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Secp256k1_verify_recover_obj, mod_trezorcrypto_Secp256k1_verify_recover);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_secp256k1_verify_recover_obj, mod_trezorcrypto_secp256k1_verify_recover);
|
||||
|
||||
/// def multiply(self, secret_key: bytes, public_key: bytes) -> bytes:
|
||||
/// def multiply(secret_key: bytes, public_key: bytes) -> bytes:
|
||||
/// '''
|
||||
/// Multiplies point defined by public_key with scalar defined by secret_key.
|
||||
/// Useful for ECDH.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Secp256k1_multiply(mp_obj_t self, mp_obj_t secret_key, mp_obj_t public_key) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_multiply(mp_obj_t secret_key, mp_obj_t public_key) {
|
||||
mp_buffer_info_t sk, pk;
|
||||
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(public_key, &pk, MP_BUFFER_READ);
|
||||
@ -175,21 +158,20 @@ STATIC mp_obj_t mod_trezorcrypto_Secp256k1_multiply(mp_obj_t self, mp_obj_t secr
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_Secp256k1_multiply_obj, mod_trezorcrypto_Secp256k1_multiply);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_secp256k1_multiply_obj, mod_trezorcrypto_secp256k1_multiply);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Secp256k1_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_Secp256k1_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_Secp256k1_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_Secp256k1_sign_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_Secp256k1_verify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_Secp256k1_verify_recover_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_Secp256k1_multiply_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_secp256k1_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_secp256k1) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_generate_secret), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_generate_secret_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_publickey_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_sign_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_recover_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_multiply_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Secp256k1_locals_dict, mod_trezorcrypto_Secp256k1_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_secp256k1_globals, mod_trezorcrypto_secp256k1_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_Secp256k1_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Secp256k1,
|
||||
.make_new = mod_trezorcrypto_Secp256k1_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_Secp256k1_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_secp256k1_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_secp256k1_globals,
|
||||
};
|
||||
|
@ -10,32 +10,15 @@
|
||||
#include "trezor-crypto/bignum.h"
|
||||
#include "ssss.h"
|
||||
|
||||
/// class SSSS:
|
||||
/// '''
|
||||
/// '''
|
||||
typedef struct _mp_obj_SSSS_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_SSSS_t;
|
||||
|
||||
/// def __init__(self) -> None:
|
||||
/// '''
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_SSSS_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, 0, false);
|
||||
mp_obj_SSSS_t *o = m_new_obj(mp_obj_SSSS_t);
|
||||
o->base.type = type;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
/// def split(self, m: int, n: int, secret: bytes) -> tuple:
|
||||
/// def split(m: int, n: int, secret: bytes) -> tuple:
|
||||
/// '''
|
||||
/// Split secret to (M of N) shares using Shamir's Secret Sharing Scheme.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_SSSS_split(size_t n_args, const mp_obj_t *args) {
|
||||
mp_int_t m = mp_obj_get_int(args[1]);
|
||||
mp_int_t n = mp_obj_get_int(args[2]);
|
||||
STATIC mp_obj_t mod_trezorcrypto_ssss_split(mp_obj_t m_obj, mp_obj_t n_obj, mp_obj_t secret_obj) {
|
||||
mp_int_t m = mp_obj_get_int(m_obj);
|
||||
mp_int_t n = mp_obj_get_int(n_obj);
|
||||
mp_buffer_info_t secret;
|
||||
mp_get_buffer_raise(args[3], &secret, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(secret_obj, &secret, MP_BUFFER_READ);
|
||||
if (secret.len != 32) {
|
||||
mp_raise_ValueError("Length of the secret has to be 256 bits");
|
||||
}
|
||||
@ -57,16 +40,16 @@ STATIC mp_obj_t mod_trezorcrypto_SSSS_split(size_t n_args, const mp_obj_t *args)
|
||||
}
|
||||
return MP_OBJ_FROM_PTR(tuple);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_SSSS_split_obj, 4, 4, mod_trezorcrypto_SSSS_split);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_ssss_split_obj, mod_trezorcrypto_ssss_split);
|
||||
|
||||
/// def combine(self, shares: tuple) -> bytes:
|
||||
/// def combine(shares: tuple) -> bytes:
|
||||
/// '''
|
||||
/// Combine M shares of Shamir's Secret Sharing Scheme into secret.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_SSSS_combine(mp_obj_t self, mp_obj_t shares) {
|
||||
STATIC mp_obj_t mod_trezorcrypto_ssss_combine(mp_obj_t shares_obj) {
|
||||
size_t n;
|
||||
mp_obj_t *share;
|
||||
mp_obj_get_array(shares, &n, &share);
|
||||
mp_obj_get_array(shares_obj, &n, &share);
|
||||
if (n < 1 || n > 15) {
|
||||
mp_raise_ValueError("Invalid number of shares");
|
||||
}
|
||||
@ -92,17 +75,16 @@ STATIC mp_obj_t mod_trezorcrypto_SSSS_combine(mp_obj_t self, mp_obj_t shares) {
|
||||
bn_write_be(&sk, (uint8_t *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_SSSS_combine_obj, mod_trezorcrypto_SSSS_combine);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ssss_combine_obj, mod_trezorcrypto_ssss_combine);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_SSSS_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&mod_trezorcrypto_SSSS_split_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_combine), MP_ROM_PTR(&mod_trezorcrypto_SSSS_combine_obj) },
|
||||
STATIC const mp_rom_map_elem_t mod_trezorcrypto_ssss_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ssss) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&mod_trezorcrypto_ssss_split_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_combine), MP_ROM_PTR(&mod_trezorcrypto_ssss_combine_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_SSSS_locals_dict, mod_trezorcrypto_SSSS_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_ssss_globals, mod_trezorcrypto_ssss_globals_table);
|
||||
|
||||
STATIC const mp_obj_type_t mod_trezorcrypto_SSSS_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_SSSS,
|
||||
.make_new = mod_trezorcrypto_SSSS_make_new,
|
||||
.locals_dict = (void*)&mod_trezorcrypto_SSSS_locals_dict,
|
||||
STATIC const mp_obj_module_t mod_trezorcrypto_ssss_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_ssss_globals,
|
||||
};
|
||||
|
@ -36,24 +36,24 @@
|
||||
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_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) },
|
||||
{ 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) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Sha256), MP_ROM_PTR(&mod_trezorcrypto_Sha256_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Sha512), MP_ROM_PTR(&mod_trezorcrypto_Sha512_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Sha3_256), MP_ROM_PTR(&mod_trezorcrypto_Sha3_256_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Sha3_512), MP_ROM_PTR(&mod_trezorcrypto_Sha3_512_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_SSSS), MP_ROM_PTR(&mod_trezorcrypto_SSSS_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_bip32), MP_ROM_PTR(&mod_trezorcrypto_bip32_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_bip39), MP_ROM_PTR(&mod_trezorcrypto_bip39_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_curve25519), MP_ROM_PTR(&mod_trezorcrypto_curve25519_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ed25519), MP_ROM_PTR(&mod_trezorcrypto_ed25519_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_nist256p1), MP_ROM_PTR(&mod_trezorcrypto_nist256p1_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_trezorcrypto_random_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_secp256k1), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ssss), MP_ROM_PTR(&mod_trezorcrypto_ssss_module) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_trezorcrypto_globals, mp_module_trezorcrypto_globals_table);
|
||||
|
||||
|
@ -1,11 +1,6 @@
|
||||
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
|
||||
|
||||
bip32 = Bip32()
|
||||
bip39 = Bip39()
|
||||
random = Random()
|
||||
ssss = SSSS()
|
||||
from trezorcrypto import bip32
|
||||
from trezorcrypto import bip39
|
||||
from trezorcrypto import random
|
||||
from trezorcrypto import ssss
|
||||
|
@ -1,9 +1,4 @@
|
||||
from trezorcrypto import Curve25519
|
||||
from trezorcrypto import Ed25519
|
||||
from trezorcrypto import Nist256p1
|
||||
from trezorcrypto import Secp256k1
|
||||
|
||||
curve25519 = Curve25519()
|
||||
ed25519 = Ed25519()
|
||||
nist256p1 = Nist256p1()
|
||||
secp256k1 = Secp256k1()
|
||||
from trezorcrypto import curve25519
|
||||
from trezorcrypto import ed25519
|
||||
from trezorcrypto import nist256p1
|
||||
from trezorcrypto import secp256k1
|
||||
|
Loading…
Reference in New Issue
Block a user