2016-04-27 11:53:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
2016-05-28 12:37:32 +00:00
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
2016-04-27 11:53:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
2017-12-12 19:31:03 +00:00
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "secp256k1.h"
|
2016-04-27 11:53:37 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def generate_secret() -> bytes:
|
2016-10-24 10:08:12 +00:00
|
|
|
/// '''
|
|
|
|
/// Generate secret key.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_secp256k1_generate_secret() {
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[32];
|
2016-10-24 10:08:12 +00:00
|
|
|
for (;;) {
|
2018-01-03 20:53:58 +00:00
|
|
|
random_buffer(out, 32);
|
2016-10-24 10:08:12 +00:00
|
|
|
// check whether secret > 0 && secret < curve_order
|
2018-01-03 20:53:58 +00:00
|
|
|
if (0 == memcmp(out, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) continue;
|
|
|
|
if (0 <= memcmp(out, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xBA\xAE\xDC\xE6\xAF\x48\xA0\x3B\xBF\xD2\x5E\x8C\xD0\x36\x41\x41", 32)) continue;
|
2016-10-24 10:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-10-24 10:08:12 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_secp256k1_generate_secret_obj, mod_trezorcrypto_secp256k1_generate_secret);
|
2016-10-24 10:08:12 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def publickey(secret_key: bytes, compressed: bool = True) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
|
|
|
/// Computes public key from secret key.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_secp256k1_publickey(size_t n_args, const mp_obj_t *args) {
|
2016-05-02 16:55:32 +00:00
|
|
|
mp_buffer_info_t sk;
|
2017-06-20 15:45:27 +00:00
|
|
|
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
2016-05-02 16:55:32 +00:00
|
|
|
if (sk.len != 32) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
bool compressed = n_args < 2 || args[1] == mp_const_true;
|
2016-04-27 11:53:37 +00:00
|
|
|
if (compressed) {
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[33];
|
|
|
|
ecdsa_get_public_key33(&secp256k1, (const uint8_t *)sk.buf, out);
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-04-27 11:53:37 +00:00
|
|
|
} else {
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[65];
|
|
|
|
ecdsa_get_public_key65(&secp256k1, (const uint8_t *)sk.buf, out);
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_publickey_obj, 1, 2, mod_trezorcrypto_secp256k1_publickey);
|
2016-04-27 11:53:37 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2016-11-08 14:37:48 +00:00
|
|
|
/// Uses secret key to produce the signature of the digest.
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_secp256k1_sign(size_t n_args, const mp_obj_t *args) {
|
2016-11-08 14:37:48 +00:00
|
|
|
mp_buffer_info_t sk, dig;
|
2017-06-20 15:45:27 +00:00
|
|
|
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;
|
2016-05-02 16:55:32 +00:00
|
|
|
if (sk.len != 32) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2016-11-08 14:37:48 +00:00
|
|
|
if (dig.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of digest");
|
2016-10-04 16:53:32 +00:00
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[65], pby;
|
|
|
|
if (0 != ecdsa_sign_digest(&secp256k1, (const uint8_t *)sk.buf, (const uint8_t *)dig.buf, out + 1, &pby, NULL)) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Signing failed");
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
out[0] = 27 + pby + compressed * 4;
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_sign_obj, 2, 3, mod_trezorcrypto_secp256k1_sign);
|
2016-04-27 11:53:37 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def verify(public_key: bytes, signature: bytes, digest: bytes) -> bool:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2016-11-08 14:37:48 +00:00
|
|
|
/// Uses public key to verify the signature of the digest.
|
2016-06-06 08:18:55 +00:00
|
|
|
/// Returns True on success.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_secp256k1_verify(mp_obj_t public_key, mp_obj_t signature, mp_obj_t digest) {
|
2016-11-08 14:37:48 +00:00
|
|
|
mp_buffer_info_t pk, sig, dig;
|
2017-06-20 15:45:27 +00:00
|
|
|
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);
|
2016-05-02 16:55:32 +00:00
|
|
|
if (pk.len != 33 && pk.len != 65) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of public key");
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2016-11-08 22:10:31 +00:00
|
|
|
if (sig.len != 64 && sig.len != 65) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of signature");
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2016-11-08 22:10:31 +00:00
|
|
|
int offset = sig.len - 64;
|
2016-11-08 14:37:48 +00:00
|
|
|
if (dig.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of digest");
|
2016-10-04 16:53:32 +00:00
|
|
|
}
|
2016-11-08 22:10:31 +00:00
|
|
|
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));
|
2016-04-27 11:53:37 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_secp256k1_verify_obj, mod_trezorcrypto_secp256k1_verify);
|
2016-04-27 11:53:37 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def verify_recover(signature: bytes, digest: bytes) -> bytes:
|
2016-11-08 22:52:04 +00:00
|
|
|
/// '''
|
|
|
|
/// Uses signature of the digest to verify the digest and recover the public key.
|
|
|
|
/// Returns public key on success, None on failure.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_secp256k1_verify_recover(mp_obj_t signature, mp_obj_t digest) {
|
2016-11-08 22:52:04 +00:00
|
|
|
mp_buffer_info_t sig, dig;
|
|
|
|
mp_get_buffer_raise(signature, &sig, MP_BUFFER_READ);
|
|
|
|
mp_get_buffer_raise(digest, &dig, MP_BUFFER_READ);
|
|
|
|
if (sig.len != 65) {
|
|
|
|
mp_raise_ValueError("Invalid length of signature");
|
|
|
|
}
|
|
|
|
if (dig.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of digest");
|
|
|
|
}
|
|
|
|
uint8_t recid = ((const uint8_t *)sig.buf)[0] - 27;
|
|
|
|
if (recid >= 8) {
|
|
|
|
mp_raise_ValueError("Invalid recid in signature");
|
|
|
|
}
|
|
|
|
bool compressed = (recid >= 4);
|
|
|
|
recid &= 3;
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[65];
|
|
|
|
if (0 == ecdsa_verify_digest_recover(&secp256k1, out, (const uint8_t *)sig.buf + 1, (const uint8_t *)dig.buf, recid)) {
|
2016-11-08 22:52:04 +00:00
|
|
|
if (compressed) {
|
2018-01-03 20:53:58 +00:00
|
|
|
out[0] = 0x02 | (out[64] & 1);
|
|
|
|
return mp_obj_new_bytes(out, 33);
|
2016-11-08 22:52:04 +00:00
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-11-08 22:52:04 +00:00
|
|
|
} else {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_secp256k1_verify_recover_obj, mod_trezorcrypto_secp256k1_verify_recover);
|
2016-11-08 22:52:04 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def multiply(secret_key: bytes, public_key: bytes) -> bytes:
|
2016-10-21 22:51:52 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Multiplies point defined by public_key with scalar defined by secret_key.
|
|
|
|
/// Useful for ECDH.
|
2016-10-21 22:51:52 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_secp256k1_multiply(mp_obj_t secret_key, mp_obj_t public_key) {
|
2016-10-21 22:51:52 +00:00
|
|
|
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);
|
|
|
|
if (sk.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
|
|
|
}
|
|
|
|
if (pk.len != 33 && pk.len != 65) {
|
|
|
|
mp_raise_ValueError("Invalid length of public key");
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[65];
|
|
|
|
if (0 != ecdh_multiply(&secp256k1, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf, out)) {
|
2016-10-21 22:51:52 +00:00
|
|
|
mp_raise_ValueError("Multiply failed");
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-10-21 22:51:52 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_secp256k1_multiply_obj, mod_trezorcrypto_secp256k1_multiply);
|
2016-10-21 22:51:52 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
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) },
|
2016-04-27 11:53:37 +00:00
|
|
|
};
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_secp256k1_globals, mod_trezorcrypto_secp256k1_globals_table);
|
2016-04-27 11:53:37 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_obj_module_t mod_trezorcrypto_secp256k1_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_secp256k1_globals,
|
2016-04-27 11:53:37 +00:00
|
|
|
};
|