You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
7.5 KiB
178 lines
7.5 KiB
![]()
8 years ago
|
/*
|
||
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
||
|
*
|
||
![]()
8 years ago
|
* Licensed under TREZOR License
|
||
|
* see LICENSE file for details
|
||
![]()
8 years ago
|
*/
|
||
|
|
||
|
#include "py/objstr.h"
|
||
|
|
||
|
#include "trezor-crypto/ecdsa.h"
|
||
![]()
8 years ago
|
#include "trezor-crypto/nist256p1.h"
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def generate_secret() -> bytes:
|
||
![]()
8 years ago
|
/// '''
|
||
|
/// Generate secret key.
|
||
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_nist256p1_generate_secret() {
|
||
![]()
8 years ago
|
vstr_t vstr;
|
||
|
vstr_init_len(&vstr, 32);
|
||
|
for (;;) {
|
||
|
random_buffer((uint8_t *)vstr.buf, 32);
|
||
|
// check whether secret > 0 && secret < curve_order
|
||
|
if (0 == memcmp(vstr.buf, "\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(vstr.buf, "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xBC\xE6\xFA\xAD\xA7\x17\x9E\x84\xF3\xB9\xCA\xC2\xFC\x63\x25\x51", 32)) continue;
|
||
|
break;
|
||
|
}
|
||
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_nist256p1_generate_secret_obj, mod_trezorcrypto_nist256p1_generate_secret);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def publickey(secret_key: bytes, compressed: bool = True) -> bytes:
|
||
![]()
8 years ago
|
/// '''
|
||
|
/// Computes public key from secret key.
|
||
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_nist256p1_publickey(size_t n_args, const mp_obj_t *args) {
|
||
![]()
8 years ago
|
mp_buffer_info_t sk;
|
||
![]()
7 years ago
|
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||
![]()
8 years ago
|
if (sk.len != 32) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Invalid length of secret key");
|
||
![]()
8 years ago
|
}
|
||
![]()
7 years ago
|
bool compressed = n_args < 2 || args[1] == mp_const_true;
|
||
![]()
8 years ago
|
vstr_t vstr;
|
||
|
if (compressed) {
|
||
|
vstr_init_len(&vstr, 33);
|
||
![]()
8 years ago
|
ecdsa_get_public_key33(&nist256p1, (const uint8_t *)sk.buf, (uint8_t *)vstr.buf);
|
||
![]()
8 years ago
|
} else {
|
||
|
vstr_init_len(&vstr, 65);
|
||
![]()
8 years ago
|
ecdsa_get_public_key65(&nist256p1, (const uint8_t *)sk.buf, (uint8_t *)vstr.buf);
|
||
![]()
8 years ago
|
}
|
||
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_nist256p1_publickey_obj, 1, 2, mod_trezorcrypto_nist256p1_publickey);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes:
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
8 years ago
|
/// Uses secret key to produce the signature of the digest.
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_nist256p1_sign(size_t n_args, const mp_obj_t *args) {
|
||
![]()
8 years ago
|
mp_buffer_info_t sk, dig;
|
||
![]()
7 years ago
|
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;
|
||
![]()
8 years ago
|
if (sk.len != 32) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Invalid length of secret key");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
if (dig.len != 32) {
|
||
|
mp_raise_ValueError("Invalid length of digest");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
vstr_t vstr;
|
||
![]()
8 years ago
|
vstr_init_len(&vstr, 65);
|
||
![]()
8 years ago
|
uint8_t pby;
|
||
![]()
8 years ago
|
if (0 != ecdsa_sign_digest(&nist256p1, (const uint8_t *)sk.buf, (const uint8_t *)dig.buf, (uint8_t *)vstr.buf + 1, &pby, NULL)) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Signing failed");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
vstr.buf[0] = 27 + pby + compressed * 4;
|
||
![]()
8 years ago
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_nist256p1_sign_obj, 2, 3, mod_trezorcrypto_nist256p1_sign);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def verify(public_key: bytes, signature: bytes, digest: bytes) -> bool:
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
8 years ago
|
/// Uses public key to verify the signature of the digest.
|
||
![]()
8 years ago
|
/// Returns True on success.
|
||
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_nist256p1_verify(mp_obj_t public_key, mp_obj_t signature, mp_obj_t digest) {
|
||
![]()
8 years ago
|
mp_buffer_info_t pk, sig, dig;
|
||
![]()
7 years ago
|
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);
|
||
![]()
8 years ago
|
if (pk.len != 33 && pk.len != 65) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Invalid length of public key");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
if (sig.len != 64 && sig.len != 65) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Invalid length of signature");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
int offset = sig.len - 64;
|
||
![]()
8 years ago
|
if (dig.len != 32) {
|
||
|
mp_raise_ValueError("Invalid length of digest");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
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));
|
||
![]()
8 years ago
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_nist256p1_verify_obj, mod_trezorcrypto_nist256p1_verify);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def verify_recover(signature: bytes, digest: bytes) -> bytes:
|
||
![]()
8 years ago
|
/// '''
|
||
|
/// Uses signature of the digest to verify the digest and recover the public key.
|
||
|
/// Returns public key on success, None on failure.
|
||
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_nist256p1_verify_recover(mp_obj_t signature, mp_obj_t digest) {
|
||
![]()
8 years ago
|
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;
|
||
|
vstr_t vstr;
|
||
|
vstr_init_len(&vstr, 65);
|
||
|
if (0 == ecdsa_verify_digest_recover(&nist256p1, (uint8_t *)vstr.buf, (const uint8_t *)sig.buf + 1, (const uint8_t *)dig.buf, recid)) {
|
||
|
if (compressed) {
|
||
|
vstr.buf[0] = 0x02 | (vstr.buf[64] & 1);
|
||
|
vstr.len = 33;
|
||
|
}
|
||
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||
|
} else {
|
||
|
return mp_const_none;
|
||
|
}
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nist256p1_verify_recover_obj, mod_trezorcrypto_nist256p1_verify_recover);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def multiply(secret_key: bytes, public_key: bytes) -> bytes:
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
/// Multiplies point defined by public_key with scalar defined by secret_key.
|
||
|
/// Useful for ECDH.
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_nist256p1_multiply(mp_obj_t secret_key, mp_obj_t public_key) {
|
||
![]()
8 years ago
|
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");
|
||
|
}
|
||
|
vstr_t vstr;
|
||
|
vstr_init_len(&vstr, 65);
|
||
|
if (0 != ecdh_multiply(&nist256p1, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf, (uint8_t *)vstr.buf)) {
|
||
|
mp_raise_ValueError("Multiply failed");
|
||
|
}
|
||
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nist256p1_multiply_obj, mod_trezorcrypto_nist256p1_multiply);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
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) },
|
||
![]()
8 years ago
|
};
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_nist256p1_globals, mod_trezorcrypto_nist256p1_globals_table);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
STATIC const mp_obj_module_t mod_trezorcrypto_nist256p1_module = {
|
||
|
.base = { &mp_type_module },
|
||
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_nist256p1_globals,
|
||
![]()
8 years ago
|
};
|