2016-04-27 00:36:59 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2016-04-27 00:36:59 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-04-27 00:36:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
2017-12-12 19:31:03 +00:00
|
|
|
#include "ed25519-donna/ed25519.h"
|
2016-04-27 00:36:59 +00:00
|
|
|
|
2016-10-24 10:08:12 +00:00
|
|
|
#include "rand.h"
|
|
|
|
|
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_ed25519_generate_secret() {
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[32];
|
|
|
|
random_buffer(out, 32);
|
2016-10-24 10:08:12 +00:00
|
|
|
// taken from https://cr.yp.to/ecdh.html
|
2018-01-03 20:53:58 +00:00
|
|
|
out[0] &= 248;
|
|
|
|
out[31] &= 127;
|
|
|
|
out[31] |= 64;
|
|
|
|
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_ed25519_generate_secret_obj, mod_trezorcrypto_ed25519_generate_secret);
|
2016-10-24 10:08:12 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def publickey(secret_key: bytes) -> 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_ed25519_publickey(mp_obj_t secret_key) {
|
2016-05-02 16:55:32 +00:00
|
|
|
mp_buffer_info_t sk;
|
|
|
|
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
|
|
|
if (sk.len != 32) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[32];
|
|
|
|
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, *(ed25519_public_key *)out);
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ed25519_publickey_obj, mod_trezorcrypto_ed25519_publickey);
|
2016-04-27 00:36:59 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def sign(secret_key: bytes, message: bytes) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
|
|
|
/// Uses secret key to produce the signature of message.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_ed25519_sign(mp_obj_t secret_key, mp_obj_t message) {
|
2016-05-02 16:55:32 +00:00
|
|
|
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);
|
|
|
|
if (sk.len != 32) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2016-10-04 16:53:32 +00:00
|
|
|
if (msg.len == 0) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Empty data to sign");
|
2016-10-04 16:53:32 +00:00
|
|
|
}
|
2016-04-27 00:36:59 +00:00
|
|
|
ed25519_public_key pk;
|
2016-05-02 16:55:32 +00:00
|
|
|
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, pk);
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[64];
|
|
|
|
ed25519_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, pk, *(ed25519_signature *)out);
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ed25519_sign_obj, mod_trezorcrypto_ed25519_sign);
|
2016-04-27 00:36:59 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def verify(public_key: bytes, signature: bytes, message: bytes) -> bool:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
|
|
|
/// Uses public key to verify the signature of the message.
|
|
|
|
/// Returns True on success.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_ed25519_verify(mp_obj_t public_key, mp_obj_t signature, mp_obj_t message) {
|
2016-05-02 16:55:32 +00:00
|
|
|
mp_buffer_info_t pk, sig, msg;
|
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(message, &msg, MP_BUFFER_READ);
|
2016-05-02 16:55:32 +00:00
|
|
|
if (pk.len != 32) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of public key");
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2016-05-02 16:55:32 +00:00
|
|
|
if (sig.len != 64) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid length of signature");
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2016-10-04 16:53:32 +00:00
|
|
|
if (msg.len == 0) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Empty data to verify");
|
2016-10-04 16:53:32 +00:00
|
|
|
}
|
2016-05-02 16:55:32 +00:00
|
|
|
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;
|
2016-04-27 00:36:59 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_ed25519_verify_obj, mod_trezorcrypto_ed25519_verify);
|
2016-04-27 00:36:59 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def cosi_combine_publickeys(public_keys: List[bytes]) -> bytes:
|
2017-04-03 20:26:21 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Combines a list of public keys used in COSI cosigning scheme.
|
2017-04-03 20:26:21 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_combine_publickeys(mp_obj_t public_keys) {
|
2017-04-06 16:36:30 +00:00
|
|
|
size_t pklen;
|
2017-04-03 20:26:21 +00:00
|
|
|
mp_obj_t *pkitems;
|
|
|
|
mp_obj_get_array(public_keys, &pklen, &pkitems);
|
|
|
|
if (pklen > 15) {
|
|
|
|
mp_raise_ValueError("Can't combine more than 15 public keys");
|
|
|
|
}
|
|
|
|
mp_buffer_info_t buf;
|
|
|
|
ed25519_public_key pks[pklen];
|
|
|
|
for (int i = 0; i < pklen; i++) {
|
|
|
|
mp_get_buffer_raise(pkitems[i], &buf, MP_BUFFER_READ);
|
|
|
|
if (buf.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of public key");
|
|
|
|
}
|
|
|
|
memcpy(pks[i], buf.buf, buf.len);
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[32];
|
|
|
|
if (0 != ed25519_cosi_combine_publickeys(*(ed25519_public_key *)out, pks, pklen)) {
|
2017-04-03 20:26:21 +00:00
|
|
|
mp_raise_ValueError("Error combining public keys");
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2017-04-03 20:26:21 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ed25519_cosi_combine_publickeys_obj, mod_trezorcrypto_ed25519_cosi_combine_publickeys);
|
2017-04-03 20:26:21 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def cosi_combine_signatures(R: bytes, signatures: List[bytes]) -> bytes:
|
2017-04-03 20:26:21 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Combines a list of signatures used in COSI cosigning scheme.
|
2017-04-03 20:26:21 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_combine_signatures(mp_obj_t R, mp_obj_t signatures) {
|
2017-04-03 20:26:21 +00:00
|
|
|
mp_buffer_info_t sigR;
|
|
|
|
mp_get_buffer_raise(R, &sigR, MP_BUFFER_READ);
|
|
|
|
if (sigR.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of R");
|
|
|
|
}
|
2017-04-06 16:36:30 +00:00
|
|
|
size_t siglen;
|
2017-04-03 20:26:21 +00:00
|
|
|
mp_obj_t *sigitems;
|
|
|
|
mp_obj_get_array(signatures, &siglen, &sigitems);
|
|
|
|
if (siglen > 15) {
|
|
|
|
mp_raise_ValueError("Can't combine more than 15 COSI signatures");
|
|
|
|
}
|
|
|
|
mp_buffer_info_t buf;
|
|
|
|
ed25519_cosi_signature sigs[siglen];
|
|
|
|
for (int i = 0; i < siglen; i++) {
|
|
|
|
mp_get_buffer_raise(sigitems[i], &buf, MP_BUFFER_READ);
|
|
|
|
if (buf.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of COSI signature");
|
|
|
|
}
|
|
|
|
memcpy(sigs[i], buf.buf, buf.len);
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[64];
|
|
|
|
ed25519_cosi_combine_signatures(*(ed25519_signature *)out, *(const ed25519_public_key *)sigR.buf, sigs, siglen);
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2017-04-03 20:26:21 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ed25519_cosi_combine_signatures_obj, mod_trezorcrypto_ed25519_cosi_combine_signatures);
|
2017-04-03 20:26:21 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def cosi_sign(secret_key: bytes, message: bytes, nonce: bytes, sigR: bytes, combined_pubkey: bytes) -> bytes:
|
2017-04-03 20:26:21 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Produce signature of message using COSI cosigning scheme.
|
2017-04-03 20:26:21 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_sign(size_t n_args, const mp_obj_t *args) {
|
2017-04-03 20:26:21 +00:00
|
|
|
mp_buffer_info_t sk, msg, nonce, sigR, pk;
|
2017-06-20 15:45:27 +00:00
|
|
|
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);
|
2017-04-03 20:26:21 +00:00
|
|
|
if (sk.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
|
|
|
}
|
|
|
|
if (nonce.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of nonce");
|
|
|
|
}
|
|
|
|
if (sigR.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of R");
|
|
|
|
}
|
|
|
|
if (pk.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of aggregated public key");
|
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t out[32];
|
|
|
|
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 *)out);
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
2017-04-03 20:26:21 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_ed25519_cosi_sign_obj, 5, 5, mod_trezorcrypto_ed25519_cosi_sign);
|
2017-04-03 20:26:21 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
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) },
|
2016-04-27 00:36:59 +00:00
|
|
|
};
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_ed25519_globals, mod_trezorcrypto_ed25519_globals_table);
|
2016-04-27 00:36:59 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_obj_module_t mod_trezorcrypto_ed25519_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_ed25519_globals,
|
2016-04-27 00:36:59 +00:00
|
|
|
};
|