2016-10-21 22:51:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
2017-12-12 19:31:03 +00:00
|
|
|
#include "ed25519-donna/ed25519.h"
|
2016-10-21 22:51:52 +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_curve25519_generate_secret() {
|
2016-10-24 10:08:12 +00:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, 32);
|
|
|
|
random_buffer((uint8_t *)vstr.buf, 32);
|
|
|
|
// taken from https://cr.yp.to/ecdh.html
|
|
|
|
vstr.buf[0] &= 248;
|
|
|
|
vstr.buf[31] &= 127;
|
|
|
|
vstr.buf[31] |= 64;
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_curve25519_generate_secret_obj, mod_trezorcrypto_curve25519_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-10-23 20:39:11 +00:00
|
|
|
/// '''
|
|
|
|
/// Computes public key from secret key.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_curve25519_publickey(mp_obj_t secret_key) {
|
2016-10-23 20:39:11 +00:00
|
|
|
mp_buffer_info_t sk;
|
|
|
|
mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ);
|
|
|
|
if (sk.len != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of secret key");
|
|
|
|
}
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, 32);
|
2017-03-28 21:09:17 +00:00
|
|
|
curve25519_scalarmult_basepoint((uint8_t *)vstr.buf, (const uint8_t *)sk.buf);
|
2016-10-23 20:39:11 +00:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_curve25519_publickey_obj, mod_trezorcrypto_curve25519_publickey);
|
2016-10-23 20:39:11 +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_curve25519_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 != 32) {
|
|
|
|
mp_raise_ValueError("Invalid length of public key");
|
|
|
|
}
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, 32);
|
2017-03-28 21:09:17 +00:00
|
|
|
curve25519_scalarmult((uint8_t *)vstr.buf, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf);
|
2016-10-21 22:51:52 +00:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_curve25519_multiply_obj, mod_trezorcrypto_curve25519_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_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) },
|
2016-10-21 22:51:52 +00:00
|
|
|
};
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_curve25519_globals, mod_trezorcrypto_curve25519_globals_table);
|
2016-10-21 22:51:52 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_obj_module_t mod_trezorcrypto_curve25519_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_curve25519_globals,
|
2016-10-21 22:51:52 +00:00
|
|
|
};
|