1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-21 23:18:13 +00:00

add nist256p1 and secp256k1 to trezor.crypto.curve

This commit is contained in:
Pavol Rusnak 2016-04-27 13:53:37 +02:00
parent f3f44b408d
commit b7013437ee
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
7 changed files with 356 additions and 1 deletions

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under Microsoft Reference Source License (Ms-RSL)
* see LICENSE.md file for details
*/
#include "py/objstr.h"
#include "trezor-crypto/ecdsa.h"
#include "trezor-crypto/nist256p1.h"
// class Nist256p1(object):
typedef struct _mp_obj_Nist256p1_t {
mp_obj_base_t base;
} mp_obj_Nist256p1_t;
// def Nist256p1.__init__(self)
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 Nist256p1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes
STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_publickey(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t skbuf;
mp_get_buffer_raise(args[1], &skbuf, MP_BUFFER_READ);
if (skbuf.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
bool compressed = n_args < 3 || args[2] == mp_const_true;
vstr_t vstr;
if (compressed) {
vstr_init_len(&vstr, 33);
ecdsa_get_public_key33(&nist256p1, (const uint8_t *)skbuf.buf, (uint8_t *)vstr.buf);
} else {
vstr_init_len(&vstr, 65);
ecdsa_get_public_key65(&nist256p1, (const uint8_t *)skbuf.buf, (uint8_t *)vstr.buf);
}
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);
// def Nist256p1.sign(self, secret_key: bytes, message: bytes) -> bytes
STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_sign(mp_obj_t self, mp_obj_t secret_key, mp_obj_t message) {
mp_buffer_info_t skbuf, messagebuf;
mp_get_buffer_raise(secret_key, &skbuf, MP_BUFFER_READ);
mp_get_buffer_raise(message, &messagebuf, MP_BUFFER_READ);
if (skbuf.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
vstr_t vstr;
vstr_init_len(&vstr, 65);
uint8_t pby;
if (0 != ecdsa_sign(&nist256p1, (const uint8_t *)skbuf.buf, (const uint8_t *)messagebuf.buf, messagebuf.len, (uint8_t *)vstr.buf, &pby)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Signing failed"));
}
(void)pby;
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorCrypto_Nist256p1_sign_obj, mod_TrezorCrypto_Nist256p1_sign);
// def Nist256p1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool
STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_verify(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t pkbuf, sigbuf, messagebuf;
mp_get_buffer_raise(args[1], &pkbuf, MP_BUFFER_READ);
mp_get_buffer_raise(args[2], &sigbuf, MP_BUFFER_READ);
mp_get_buffer_raise(args[3], &messagebuf, MP_BUFFER_READ);
if (pkbuf.len != 33 && pkbuf.len != 65) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of public key"));
}
if (sigbuf.len != 65) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of signature"));
}
return mp_obj_new_bool(0 == ecdsa_verify(&nist256p1, (const uint8_t *)pkbuf.buf, (const uint8_t *)sigbuf.buf, (const uint8_t *)messagebuf.buf, messagebuf.len));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Nist256p1_verify_obj, 4, 4, mod_TrezorCrypto_Nist256p1_verify);
// Nist256p1 stuff
STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Nist256p1_locals_dict_table[] = {
{ 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) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Nist256p1_locals_dict, mod_TrezorCrypto_Nist256p1_locals_dict_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,
};

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under Microsoft Reference Source License (Ms-RSL)
* see LICENSE.md file for details
*/
#include "py/objstr.h"
#include "trezor-crypto/ecdsa.h"
#include "trezor-crypto/secp256k1.h"
// class Secp256k1(object):
typedef struct _mp_obj_Secp256k1_t {
mp_obj_base_t base;
} mp_obj_Secp256k1_t;
// def Secp256k1.__init__(self)
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 Secp256k1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes
STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_publickey(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t skbuf;
mp_get_buffer_raise(args[1], &skbuf, MP_BUFFER_READ);
if (skbuf.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
bool compressed = n_args < 3 || args[2] == mp_const_true;
vstr_t vstr;
if (compressed) {
vstr_init_len(&vstr, 33);
ecdsa_get_public_key33(&secp256k1, (const uint8_t *)skbuf.buf, (uint8_t *)vstr.buf);
} else {
vstr_init_len(&vstr, 65);
ecdsa_get_public_key65(&secp256k1, (const uint8_t *)skbuf.buf, (uint8_t *)vstr.buf);
}
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);
// def Secp256k1.sign(self, secret_key: bytes, message: bytes) -> bytes
STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_sign(mp_obj_t self, mp_obj_t secret_key, mp_obj_t message) {
mp_buffer_info_t skbuf, messagebuf;
mp_get_buffer_raise(secret_key, &skbuf, MP_BUFFER_READ);
mp_get_buffer_raise(message, &messagebuf, MP_BUFFER_READ);
if (skbuf.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
vstr_t vstr;
vstr_init_len(&vstr, 65);
uint8_t pby;
if (0 != ecdsa_sign(&secp256k1, (const uint8_t *)skbuf.buf, (const uint8_t *)messagebuf.buf, messagebuf.len, (uint8_t *)vstr.buf, &pby)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Signing failed"));
}
(void)pby;
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorCrypto_Secp256k1_sign_obj, mod_TrezorCrypto_Secp256k1_sign);
// def Secp256k1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool
STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_verify(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t pkbuf, sigbuf, messagebuf;
mp_get_buffer_raise(args[1], &pkbuf, MP_BUFFER_READ);
mp_get_buffer_raise(args[2], &sigbuf, MP_BUFFER_READ);
mp_get_buffer_raise(args[3], &messagebuf, MP_BUFFER_READ);
if (pkbuf.len != 33 && pkbuf.len != 65) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of public key"));
}
if (sigbuf.len != 65) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of signature"));
}
return mp_obj_new_bool(0 == ecdsa_verify(&secp256k1, (const uint8_t *)pkbuf.buf, (const uint8_t *)sigbuf.buf, (const uint8_t *)messagebuf.buf, messagebuf.len));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Secp256k1_verify_obj, 4, 4, mod_TrezorCrypto_Secp256k1_verify);
// Secp256k1 stuff
STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Secp256k1_locals_dict_table[] = {
{ 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) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorCrypto_Secp256k1_locals_dict, mod_TrezorCrypto_Secp256k1_locals_dict_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,
};

View File

@ -18,6 +18,8 @@
#include "modtrezorcrypto-ed25519.h"
#include "modtrezorcrypto-pbkdf2_hmac.h"
#include "modtrezorcrypto-ripemd160.h"
#include "modtrezorcrypto-nist256p1.h"
#include "modtrezorcrypto-secp256k1.h"
#include "modtrezorcrypto-sha256.h"
#include "modtrezorcrypto-sha512.h"
#include "modtrezorcrypto-sha3-256.h"
@ -28,7 +30,9 @@
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_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_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_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) },

View File

@ -0,0 +1,82 @@
import sys
sys.path.append('..')
sys.path.append('../lib')
import unittest
from ubinascii import hexlify, unhexlify
from trezor.crypto.curve import nist256p1
class TestCryptoNist256p1(unittest.TestCase):
# vectors from http://point-at-infinity.org/ecc/nisttv
vectors = [
(1, '6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5'),
(2, '7CF27B188D034F7E8A52380304B51AC3C08969E277F21B35A60B48FC4766997807775510DB8ED040293D9AC69F7430DBBA7DADE63CE982299E04B79D227873D1'),
(3, '5ECBE4D1A6330A44C8F7EF951D4BF165E6C6B721EFADA985FB41661BC6E7FD6C8734640C4998FF7E374B06CE1A64A2ECD82AB036384FB83D9A79B127A27D5032'),
(4, 'E2534A3532D08FBBA02DDE659EE62BD0031FE2DB785596EF509302446B030852E0F1575A4C633CC719DFEE5FDA862D764EFC96C3F30EE0055C42C23F184ED8C6'),
(5, '51590B7A515140D2D784C85608668FDFEF8C82FD1F5BE52421554A0DC3D033EDE0C17DA8904A727D8AE1BF36BF8A79260D012F00D4D80888D1D0BB44FDA16DA4'),
(6, 'B01A172A76A4602C92D3242CB897DDE3024C740DEBB215B4C6B0AAE93C2291A9E85C10743237DAD56FEC0E2DFBA703791C00F7701C7E16BDFD7C48538FC77FE2'),
(7, '8E533B6FA0BF7B4625BB30667C01FB607EF9F8B8A80FEF5B300628703187B2A373EB1DBDE03318366D069F83A6F5900053C73633CB041B21C55E1A86C1F400B4'),
(8, '62D9779DBEE9B0534042742D3AB54CADC1D238980FCE97DBB4DD9DC1DB6FB393AD5ACCBD91E9D8244FF15D771167CEE0A2ED51F6BBE76A78DA540A6A0F09957E'),
(9, 'EA68D7B6FEDF0B71878938D51D71F8729E0ACB8C2C6DF8B3D79E8A4B90949EE02A2744C972C9FCE787014A964A8EA0C84D714FEAA4DE823FE85A224A4DD048FA'),
(10, 'CEF66D6B2A3A993E591214D1EA223FB545CA6C471C48306E4C36069404C5723F878662A229AAAE906E123CDD9D3B4C10590DED29FE751EEECA34BBAA44AF0773'),
(11, '3ED113B7883B4C590638379DB0C21CDA16742ED0255048BF433391D374BC21D19099209ACCC4C8A224C843AFA4F4C68A090D04DA5E9889DAE2F8EEFCE82A3740'),
(12, '741DD5BDA817D95E4626537320E5D55179983028B2F82C99D500C5EE8624E3C40770B46A9C385FDC567383554887B1548EEB912C35BA5CA71995FF22CD4481D3'),
(13, '177C837AE0AC495A61805DF2D85EE2FC792E284B65EAD58A98E15D9D46072C0163BB58CD4EBEA558A24091ADB40F4E7226EE14C3A1FB4DF39C43BBE2EFC7BFD8'),
(14, '54E77A001C3862B97A76647F4336DF3CF126ACBE7A069C5E5709277324D2920BF599F1BB29F4317542121F8C05A2E7C37171EA77735090081BA7C82F60D0B375'),
(15, 'F0454DC6971ABAE7ADFB378999888265AE03AF92DE3A0EF163668C63E59B9D5FB5B93EE3592E2D1F4E6594E51F9643E62A3B21CE75B5FA3F47E59CDE0D034F36'),
(16, '76A94D138A6B41858B821C629836315FCD28392EFF6CA038A5EB4787E1277C6EA985FE61341F260E6CB0A1B5E11E87208599A0040FC78BAA0E9DDD724B8C5110'),
(17, '47776904C0F1CC3A9C0984B66F75301A5FA68678F0D64AF8BA1ABCE34738A73EAA005EE6B5B957286231856577648E8381B2804428D5733F32F787FF71F1FCDC'),
(18, '1057E0AB5780F470DEFC9378D1C7C87437BB4C6F9EA55C63D936266DBD781FDAF6F1645A15CBE5DC9FA9B7DFD96EE5A7DCC11B5C5EF4F1F78D83B3393C6A45A2'),
(19, 'CB6D2861102C0C25CE39B7C17108C507782C452257884895C1FC7B74AB03ED8358D7614B24D9EF515C35E7100D6D6CE4A496716E30FA3E03E39150752BCECDAA'),
(20, '83A01A9378395BAB9BCD6A0AD03CC56D56E6B19250465A94A234DC4C6B28DA9A76E49B6DE2F73234AE6A5EB9D612B75C9F2202BB6923F54FF8240AAA86F640B8'),
(112233445566778899, '339150844EC15234807FE862A86BE77977DBFB3AE3D96F4C22795513AEAAB82FB1C14DDFDC8EC1B2583F51E85A5EB3A155840F2034730E9B5ADA38B674336A21'),
(112233445566778899112233445566778899, '1B7E046A076CC25E6D7FA5003F6729F665CC3241B5ADAB12B498CD32F2803264BFEA79BE2B666B073DB69A2A241ADAB0738FE9D2DD28B5604EB8C8CF097C457B'),
(29852220098221261079183923314599206100666902414330245206392788703677545185283, '9EACE8F4B071E677C5350B02F2BB2B384AAE89D58AA72CA97A170572E0FB222F1BBDAEC2430B09B93F7CB08678636CE12EAAFD58390699B5FD2F6E1188FC2A78'),
(57896042899961394862005778464643882389978449576758748073725983489954366354431, '878F22CC6DB6048D2B767268F22FFAD8E56AB8E2DC615F7BD89F1E350500DD8D714A5D7BB901C9C5853400D12341A892EF45D87FC553786756C4F0C9391D763E'),
(1766845392945710151501889105729049882997660004824848915955419660366636031, '659A379625AB122F2512B8DADA02C6348D53B54452DFF67AC7ACE4E8856295CA49D81AB97B648464D0B4A288BD7818FAB41A16426E943527C4FED8736C53D0F6'),
(28948025760307534517734791687894775804466072615242963443097661355606862201087, 'CBCEAAA8A4DD44BBCE58E8DB7740A5510EC2CB7EA8DA8D8F036B3FB04CDA4DE44BD7AA301A80D7F59FD983FEDBE59BB7B2863FE46494935E3745B360E32332FA'),
(113078210460870548944811695960290644973229224625838436424477095834645696384, 'F0C4A0576154FF3A33A3460D42EAED806E854DFA37125221D37935124BA462A45B392FA964434D29EEC6C9DBC261CF116796864AA2FAADB984A2DF38D1AEF7A3'),
(12078056106883488161242983286051341125085761470677906721917479268909056, '5E6C8524B6369530B12C62D31EC53E0288173BD662BDF680B53A41ECBCAD00CC447FE742C2BFEF4D0DB14B5B83A2682309B5618E0064A94804E9282179FE089F'),
(57782969857385448082319957860328652998540760998293976083718804450708503920639, '03792E541BC209076A3D7920A915021ECD396A6EB5C3960024BE5575F3223484FC774AE092403101563B712F68170312304F20C80B40C06282063DB25F268DE4'),
(57896017119460046759583662757090100341435943767777707906455551163257755533312, '2379FF85AB693CDF901D6CE6F2473F39C04A2FE3DCD842CE7AAB0E002095BCF8F8B476530A634589D5129E46F322B02FBC610A703D80875EE70D7CE1877436A1'),
(452312848374287284681282171017647412726433684238464212999305864837160993279, 'C1E4072C529BF2F44DA769EFC934472848003B3AF2C0F5AA8F8DDBD53E12ED7C39A6EE77812BB37E8079CD01ED649D3830FCA46F718C1D3993E4A591824ABCDB'),
(904571339174065134293634407946054000774746055866917729876676367558469746684, '34DFBC09404C21E250A9B40FA8772897AC63A094877DB65862B61BD1507B34F3CF6F8A876C6F99CEAEC87148F18C7E1E0DA6E165FFC8ED82ABB65955215F77D3'),
(115792089210356248762697446949407573529996955224135760342422259061068512044349, '83A01A9378395BAB9BCD6A0AD03CC56D56E6B19250465A94A234DC4C6B28DA9A891B64911D08CDCC5195A14629ED48A360DDFD4596DC0AB007DBF5557909BF47'),
(115792089210356248762697446949407573529996955224135760342422259061068512044350, 'CB6D2861102C0C25CE39B7C17108C507782C452257884895C1FC7B74AB03ED83A7289EB3DB2610AFA3CA18EFF292931B5B698E92CF05C1FC1C6EAF8AD4313255'),
(115792089210356248762697446949407573529996955224135760342422259061068512044351, '1057E0AB5780F470DEFC9378D1C7C87437BB4C6F9EA55C63D936266DBD781FDA090E9BA4EA341A246056482026911A58233EE4A4A10B0E08727C4CC6C395BA5D'),
(115792089210356248762697446949407573529996955224135760342422259061068512044352, '47776904C0F1CC3A9C0984B66F75301A5FA68678F0D64AF8BA1ABCE34738A73E55FFA1184A46A8D89DCE7A9A889B717C7E4D7FBCD72A8CC0CD0878008E0E0323'),
(115792089210356248762697446949407573529996955224135760342422259061068512044353, '76A94D138A6B41858B821C629836315FCD28392EFF6CA038A5EB4787E1277C6E567A019DCBE0D9F2934F5E4A1EE178DF7A665FFCF0387455F162228DB473AEEF'),
(115792089210356248762697446949407573529996955224135760342422259061068512044354, 'F0454DC6971ABAE7ADFB378999888265AE03AF92DE3A0EF163668C63E59B9D5F4A46C11BA6D1D2E1B19A6B1AE069BC19D5C4DE328A4A05C0B81A6321F2FCB0C9'),
(115792089210356248762697446949407573529996955224135760342422259061068512044355, '54E77A001C3862B97A76647F4336DF3CF126ACBE7A069C5E5709277324D2920B0A660E43D60BCE8BBDEDE073FA5D183C8E8E15898CAF6FF7E45837D09F2F4C8A'),
(115792089210356248762697446949407573529996955224135760342422259061068512044356, '177C837AE0AC495A61805DF2D85EE2FC792E284B65EAD58A98E15D9D46072C019C44A731B1415AA85DBF6E524BF0B18DD911EB3D5E04B20C63BC441D10384027'),
(115792089210356248762697446949407573529996955224135760342422259061068512044357, '741DD5BDA817D95E4626537320E5D55179983028B2F82C99D500C5EE8624E3C4F88F4B9463C7A024A98C7CAAB7784EAB71146ED4CA45A358E66A00DD32BB7E2C'),
(115792089210356248762697446949407573529996955224135760342422259061068512044358, '3ED113B7883B4C590638379DB0C21CDA16742ED0255048BF433391D374BC21D16F66DF64333B375EDB37BC505B0B3975F6F2FB26A16776251D07110317D5C8BF'),
(115792089210356248762697446949407573529996955224135760342422259061068512044359, 'CEF66D6B2A3A993E591214D1EA223FB545CA6C471C48306E4C36069404C5723F78799D5CD655517091EDC32262C4B3EFA6F212D7018AE11135CB4455BB50F88C'),
(115792089210356248762697446949407573529996955224135760342422259061068512044360, 'EA68D7B6FEDF0B71878938D51D71F8729E0ACB8C2C6DF8B3D79E8A4B90949EE0D5D8BB358D36031978FEB569B5715F37B28EB0165B217DC017A5DDB5B22FB705'),
(115792089210356248762697446949407573529996955224135760342422259061068512044361, '62D9779DBEE9B0534042742D3AB54CADC1D238980FCE97DBB4DD9DC1DB6FB39352A533416E1627DCB00EA288EE98311F5D12AE0A4418958725ABF595F0F66A81'),
(115792089210356248762697446949407573529996955224135760342422259061068512044362, '8E533B6FA0BF7B4625BB30667C01FB607EF9F8B8A80FEF5B300628703187B2A38C14E2411FCCE7CA92F9607C590A6FFFAC38C9CD34FBE4DE3AA1E5793E0BFF4B'),
(115792089210356248762697446949407573529996955224135760342422259061068512044363, 'B01A172A76A4602C92D3242CB897DDE3024C740DEBB215B4C6B0AAE93C2291A917A3EF8ACDC8252B9013F1D20458FC86E3FF0890E381E9420283B7AC7038801D'),
(115792089210356248762697446949407573529996955224135760342422259061068512044364, '51590B7A515140D2D784C85608668FDFEF8C82FD1F5BE52421554A0DC3D033ED1F3E82566FB58D83751E40C9407586D9F2FED1002B27F7772E2F44BB025E925B'),
(115792089210356248762697446949407573529996955224135760342422259061068512044365, 'E2534A3532D08FBBA02DDE659EE62BD0031FE2DB785596EF509302446B0308521F0EA8A4B39CC339E62011A02579D289B103693D0CF11FFAA3BD3DC0E7B12739'),
(115792089210356248762697446949407573529996955224135760342422259061068512044366, '5ECBE4D1A6330A44C8F7EF951D4BF165E6C6B721EFADA985FB41661BC6E7FD6C78CB9BF2B6670082C8B4F931E59B5D1327D54FCAC7B047C265864ED85D82AFCD'),
(115792089210356248762697446949407573529996955224135760342422259061068512044367, '7CF27B188D034F7E8A52380304B51AC3C08969E277F21B35A60B48FC47669978F888AAEE24712FC0D6C26539608BCF244582521AC3167DD661FB4862DD878C2E'),
(115792089210356248762697446949407573529996955224135760342422259061068512044368, '6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296B01CBD1C01E58065711814B583F061E9D431CCA994CEA1313449BF97C840AE0A'),
]
def test_publickey(self):
for sk, pk in self.vectors:
sk = hex(sk)[2:]
if len(sk) < 64:
sk = '0' * (64 - len(sk)) + sk
pk = pk.lower()
pk65 = hexlify(nist256p1.publickey(unhexlify(sk), False)).decode('ascii') # uncompressed
self.assertEqual(str(pk65), '04' + pk)
pk33 = hexlify(nist256p1.publickey(unhexlify(sk))).decode('ascii')
if pk[-1] in '02468ace':
self.assertEqual(pk33, '02' + pk[:64])
else:
self.assertEqual(pk33, '03' + pk[:64])
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,75 @@
import sys
sys.path.append('..')
sys.path.append('../lib')
import unittest
from ubinascii import hexlify, unhexlify
from trezor.crypto.curve import secp256k1
class TestCryptoSecp256k1(unittest.TestCase):
# vectors from https://crypto.stackexchange.com/questions/784/are-there-any-secp256k1-ecdsa-test-examples-available
vectors = [
(1, '79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8'),
(2, 'C6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE51AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A'),
(3, 'F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9388F7B0F632DE8140FE337E62A37F3566500A99934C2231B6CB9FD7584B8E672'),
(4, 'E493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD1351ED993EA0D455B75642E2098EA51448D967AE33BFBDFE40CFE97BDC47739922'),
(5, '2F8BDE4D1A07209355B4A7250A5C5128E88B84BDDC619AB7CBA8D569B240EFE4D8AC222636E5E3D6D4DBA9DDA6C9C426F788271BAB0D6840DCA87D3AA6AC62D6'),
(6, 'FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A1460297556AE12777AACFBB620F3BE96017F45C560DE80F0F6518FE4A03C870C36B075F297'),
(7, '5CBDF0646E5DB4EAA398F365F2EA7A0E3D419B7E0330E39CE92BDDEDCAC4F9BC6AEBCA40BA255960A3178D6D861A54DBA813D0B813FDE7B5A5082628087264DA'),
(8, '2F01E5E15CCA351DAFF3843FB70F3C2F0A1BDD05E5AF888A67784EF3E10A2A015C4DA8A741539949293D082A132D13B4C2E213D6BA5B7617B5DA2CB76CBDE904'),
(9, 'ACD484E2F0C7F65309AD178A9F559ABDE09796974C57E714C35F110DFC27CCBECC338921B0A7D9FD64380971763B61E9ADD888A4375F8E0F05CC262AC64F9C37'),
(10, 'A0434D9E47F3C86235477C7B1AE6AE5D3442D49B1943C2B752A68E2A47E247C7893ABA425419BC27A3B6C7E693A24C696F794C2ED877A1593CBEE53B037368D7'),
(11, '774AE7F858A9411E5EF4246B70C65AAC5649980BE5C17891BBEC17895DA008CBD984A032EB6B5E190243DD56D7B7B365372DB1E2DFF9D6A8301D74C9C953C61B'),
(12, 'D01115D548E7561B15C38F004D734633687CF4419620095BC5B0F47070AFE85AA9F34FFDC815E0D7A8B64537E17BD81579238C5DD9A86D526B051B13F4062327'),
(13, 'F28773C2D975288BC7D1D205C3748651B075FBC6610E58CDDEEDDF8F19405AA80AB0902E8D880A89758212EB65CDAF473A1A06DA521FA91F29B5CB52DB03ED81'),
(14, '499FDF9E895E719CFD64E67F07D38E3226AA7B63678949E6E49B241A60E823E4CAC2F6C4B54E855190F044E4A7B3D464464279C27A3F95BCC65F40D403A13F5B'),
(15, 'D7924D4F7D43EA965A465AE3095FF41131E5946F3C85F79E44ADBCF8E27E080E581E2872A86C72A683842EC228CC6DEFEA40AF2BD896D3A5C504DC9FF6A26B58'),
(16, 'E60FCE93B59E9EC53011AABC21C23E97B2A31369B87A5AE9C44EE89E2A6DEC0AF7E3507399E595929DB99F34F57937101296891E44D23F0BE1F32CCE69616821'),
(17, 'DEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A344211AB0694635168E997B0EAD2A93DAECED1F4A04A95C0F6CFB199F69E56EB77'),
(18, '5601570CB47F238D2B0286DB4A990FA0F3BA28D1A319F5E7CF55C2A2444DA7CCC136C1DC0CBEB930E9E298043589351D81D8E0BC736AE2A1F5192E5E8B061D58'),
(19, '2B4EA0A797A443D293EF5CFF444F4979F06ACFEBD7E86D277475656138385B6C85E89BC037945D93B343083B5A1C86131A01F60C50269763B570C854E5C09B7A'),
(20, '4CE119C96E2FA357200B559B2F7DD5A5F02D5290AFF74B03F3E471B273211C9712BA26DCB10EC1625DA61FA10A844C676162948271D96967450288EE9233DC3A'),
(112233445566778899, 'A90CC3D3F3E146DAADFC74CA1372207CB4B725AE708CEF713A98EDD73D99EF295A79D6B289610C68BC3B47F3D72F9788A26A06868B4D8E433E1E2AD76FB7DC76'),
(112233445566778899112233445566778899, 'E5A2636BCFD412EBF36EC45B19BFB68A1BC5F8632E678132B885F7DF99C5E9B3736C1CE161AE27B405CAFD2A7520370153C2C861AC51D6C1D5985D9606B45F39'),
(28948022309329048855892746252171976963209391069768726095651290785379540373584, 'A6B594B38FB3E77C6EDF78161FADE2041F4E09FD8497DB776E546C41567FEB3C71444009192228730CD8237A490FEBA2AFE3D27D7CC1136BC97E439D13330D55'),
(57896044618658097711785492504343953926418782139537452191302581570759080747168, '00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C633F3979BF72AE8202983DC989AEC7F2FF2ED91BDD69CE02FC0700CA100E59DDF3'),
(86844066927987146567678238756515930889628173209306178286953872356138621120752, 'E24CE4BEEE294AA6350FAA67512B99D388693AE4E7F53D19882A6EA169FC1CE18B71E83545FC2B5872589F99D948C03108D36797C4DE363EBD3FF6A9E1A95B10'),
(115792089237316195423570985008687907852837564279074904382605163141518161494317, '4CE119C96E2FA357200B559B2F7DD5A5F02D5290AFF74B03F3E471B273211C97ED45D9234EF13E9DA259E05EF57BB3989E9D6B7D8E269698BAFD77106DCC1FF5'),
(115792089237316195423570985008687907852837564279074904382605163141518161494318, '2B4EA0A797A443D293EF5CFF444F4979F06ACFEBD7E86D277475656138385B6C7A17643FC86BA26C4CBCF7C4A5E379ECE5FE09F3AFD9689C4A8F37AA1A3F60B5'),
(115792089237316195423570985008687907852837564279074904382605163141518161494319, '5601570CB47F238D2B0286DB4A990FA0F3BA28D1A319F5E7CF55C2A2444DA7CC3EC93E23F34146CF161D67FBCA76CAE27E271F438C951D5E0AE6D1A074F9DED7'),
(115792089237316195423570985008687907852837564279074904382605163141518161494320, 'DEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34BDEE54F96B9CAE9716684F152D56C251312E0B5FB56A3F09304E660861A910B8'),
(115792089237316195423570985008687907852837564279074904382605163141518161494321, 'E60FCE93B59E9EC53011AABC21C23E97B2A31369B87A5AE9C44EE89E2A6DEC0A081CAF8C661A6A6D624660CB0A86C8EFED6976E1BB2DC0F41E0CD330969E940E'),
(115792089237316195423570985008687907852837564279074904382605163141518161494322, 'D7924D4F7D43EA965A465AE3095FF41131E5946F3C85F79E44ADBCF8E27E080EA7E1D78D57938D597C7BD13DD733921015BF50D427692C5A3AFB235F095D90D7'),
(115792089237316195423570985008687907852837564279074904382605163141518161494323, '499FDF9E895E719CFD64E67F07D38E3226AA7B63678949E6E49B241A60E823E4353D093B4AB17AAE6F0FBB1B584C2B9BB9BD863D85C06A4339A0BF2AFC5EBCD4'),
(115792089237316195423570985008687907852837564279074904382605163141518161494324, 'F28773C2D975288BC7D1D205C3748651B075FBC6610E58CDDEEDDF8F19405AA8F54F6FD17277F5768A7DED149A3250B8C5E5F925ADE056E0D64A34AC24FC0EAE'),
(115792089237316195423570985008687907852837564279074904382605163141518161494325, 'D01115D548E7561B15C38F004D734633687CF4419620095BC5B0F47070AFE85A560CB00237EA1F285749BAC81E8427EA86DC73A2265792AD94FAE4EB0BF9D908'),
(115792089237316195423570985008687907852837564279074904382605163141518161494326, '774AE7F858A9411E5EF4246B70C65AAC5649980BE5C17891BBEC17895DA008CB267B5FCD1494A1E6FDBC22A928484C9AC8D24E1D20062957CFE28B3536AC3614'),
(115792089237316195423570985008687907852837564279074904382605163141518161494327, 'A0434D9E47F3C86235477C7B1AE6AE5D3442D49B1943C2B752A68E2A47E247C776C545BDABE643D85C4938196C5DB3969086B3D127885EA6C3411AC3FC8C9358'),
(115792089237316195423570985008687907852837564279074904382605163141518161494328, 'ACD484E2F0C7F65309AD178A9F559ABDE09796974C57E714C35F110DFC27CCBE33CC76DE4F5826029BC7F68E89C49E165227775BC8A071F0FA33D9D439B05FF8'),
(115792089237316195423570985008687907852837564279074904382605163141518161494329, '2F01E5E15CCA351DAFF3843FB70F3C2F0A1BDD05E5AF888A67784EF3E10A2A01A3B25758BEAC66B6D6C2F7D5ECD2EC4B3D1DEC2945A489E84A25D3479342132B'),
(115792089237316195423570985008687907852837564279074904382605163141518161494330, '5CBDF0646E5DB4EAA398F365F2EA7A0E3D419B7E0330E39CE92BDDEDCAC4F9BC951435BF45DAA69F5CE8729279E5AB2457EC2F47EC02184A5AF7D9D6F78D9755'),
(115792089237316195423570985008687907852837564279074904382605163141518161494331, 'FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A146029755651ED8885530449DF0C4169FE80BA3A9F217F0F09AE701B5FC378F3C84F8A0998'),
(115792089237316195423570985008687907852837564279074904382605163141518161494332, '2F8BDE4D1A07209355B4A7250A5C5128E88B84BDDC619AB7CBA8D569B240EFE42753DDD9C91A1C292B24562259363BD90877D8E454F297BF235782C459539959'),
(115792089237316195423570985008687907852837564279074904382605163141518161494333, 'E493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13AE1266C15F2BAA48A9BD1DF6715AEBB7269851CC404201BF30168422B88C630D'),
(115792089237316195423570985008687907852837564279074904382605163141518161494334, 'F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9C77084F09CD217EBF01CC819D5C80CA99AFF5666CB3DDCE4934602897B4715BD'),
(115792089237316195423570985008687907852837564279074904382605163141518161494335, 'C6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5E51E970159C23CC65C3A7BE6B99315110809CD9ACD992F1EDC9BCE55AF301705'),
(115792089237316195423570985008687907852837564279074904382605163141518161494336, '79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798B7C52588D95C3B9AA25B0403F1EEF75702E84BB7597AABE663B82F6F04EF2777'),
]
def test_publickey(self):
for sk, pk in self.vectors:
sk = hex(sk)[2:]
if len(sk) < 64:
sk = '0' * (64 - len(sk)) + sk
pk = pk.lower()
pk65 = hexlify(secp256k1.publickey(unhexlify(sk), False)).decode('ascii') # uncompressed
self.assertEqual(str(pk65), '04' + pk)
pk33 = hexlify(secp256k1.publickey(unhexlify(sk))).decode('ascii')
if pk[-1] in '02468ace':
self.assertEqual(pk33, '02' + pk[:64])
else:
self.assertEqual(pk33, '03' + pk[:64])
if __name__ == '__main__':
unittest.main()

View File

@ -1,3 +1,7 @@
from TrezorCrypto import Ed25519
from TrezorCrypto import Nist256p1
from TrezorCrypto import Secp256k1
ed25519 = Ed25519()
nist256p1 = Nist256p1()
secp256k1 = Secp256k1()

2
vendor/micropython vendored

@ -1 +1 @@
Subproject commit e864044e3d5b92cda68be4325708565458e3e864
Subproject commit 8678e9d6158fd0fb3b564266aa718334f5522d52