2014-05-22 18:54:58 +00:00
|
|
|
/**
|
2016-04-22 12:48:24 +00:00
|
|
|
* Copyright (c) 2013-2016 Tomas Dzetkulic
|
|
|
|
* Copyright (c) 2013-2016 Pavol Rusnak
|
|
|
|
* Copyright (c) 2015-2016 Jochen Hoenicke
|
2014-05-22 18:54:58 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-09-12 17:21:24 +00:00
|
|
|
#include <string.h>
|
2015-01-26 18:09:00 +00:00
|
|
|
#include <stdbool.h>
|
2013-09-12 17:21:24 +00:00
|
|
|
|
2017-06-11 16:28:07 +00:00
|
|
|
#include "aes.h"
|
2017-03-31 01:38:28 +00:00
|
|
|
#include "address.h"
|
2013-09-12 17:21:24 +00:00
|
|
|
#include "bignum.h"
|
|
|
|
#include "hmac.h"
|
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "bip32.h"
|
2014-01-14 14:29:46 +00:00
|
|
|
#include "sha2.h"
|
2017-05-19 17:33:21 +00:00
|
|
|
#include "sha3.h"
|
2014-01-14 14:29:46 +00:00
|
|
|
#include "ripemd160.h"
|
2014-05-22 19:52:04 +00:00
|
|
|
#include "base58.h"
|
2015-04-13 16:19:33 +00:00
|
|
|
#include "macros.h"
|
2016-04-22 15:47:48 +00:00
|
|
|
#include "curves.h"
|
2015-05-27 14:48:57 +00:00
|
|
|
#include "secp256k1.h"
|
2016-04-22 12:48:24 +00:00
|
|
|
#include "nist256p1.h"
|
2016-04-22 15:47:48 +00:00
|
|
|
#include "ed25519.h"
|
2017-05-19 17:33:21 +00:00
|
|
|
#include "ed25519-sha3.h"
|
|
|
|
#if USE_KECCAK
|
|
|
|
#include "ed25519-keccak.h"
|
2016-08-18 14:21:33 +00:00
|
|
|
#endif
|
2017-05-19 17:17:24 +00:00
|
|
|
#if USE_NEM
|
|
|
|
#include "nem.h"
|
|
|
|
#endif
|
2016-04-22 15:47:48 +00:00
|
|
|
|
|
|
|
const curve_info ed25519_info = {
|
2017-12-09 17:59:44 +00:00
|
|
|
.bip32_name = "ed25519 seed",
|
|
|
|
.params = NULL,
|
|
|
|
.hasher_type = HASHER_SHA2,
|
2016-04-22 15:47:48 +00:00
|
|
|
};
|
2015-05-27 14:48:57 +00:00
|
|
|
|
2017-05-19 17:20:55 +00:00
|
|
|
const curve_info ed25519_sha3_info = {
|
2017-12-09 17:59:44 +00:00
|
|
|
.bip32_name = "ed25519-sha3 seed",
|
|
|
|
.params = NULL,
|
|
|
|
.hasher_type = HASHER_SHA2,
|
2017-05-19 17:20:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if USE_KECCAK
|
|
|
|
const curve_info ed25519_keccak_info = {
|
2017-12-09 17:59:44 +00:00
|
|
|
.bip32_name = "ed25519-keccak seed",
|
|
|
|
.params = NULL,
|
|
|
|
.hasher_type = HASHER_SHA2,
|
2017-05-19 17:20:55 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-10-13 19:24:17 +00:00
|
|
|
const curve_info curve25519_info = {
|
2017-12-09 17:59:44 +00:00
|
|
|
.bip32_name = "curve25519 seed",
|
|
|
|
.params = NULL,
|
|
|
|
.hasher_type = HASHER_SHA2,
|
2016-10-13 19:24:17 +00:00
|
|
|
};
|
|
|
|
|
2016-06-26 14:24:50 +00:00
|
|
|
int hdnode_from_xpub(uint32_t depth, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, const char* curve, HDNode *out)
|
2013-11-25 14:37:49 +00:00
|
|
|
{
|
2016-04-22 12:48:24 +00:00
|
|
|
const curve_info *info = get_curve_by_name(curve);
|
|
|
|
if (info == 0) {
|
2016-04-20 17:54:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-19 23:49:14 +00:00
|
|
|
if (public_key[0] != 0x02 && public_key[0] != 0x03) { // invalid pubkey
|
2014-08-07 12:34:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-04-22 12:48:24 +00:00
|
|
|
out->curve = info;
|
2013-11-25 14:37:49 +00:00
|
|
|
out->depth = depth;
|
|
|
|
out->child_num = child_num;
|
|
|
|
memcpy(out->chain_code, chain_code, 32);
|
2015-04-08 19:07:15 +00:00
|
|
|
MEMSET_BZERO(out->private_key, 32);
|
2013-11-25 14:37:49 +00:00
|
|
|
memcpy(out->public_key, public_key, 33);
|
2014-08-07 12:34:33 +00:00
|
|
|
return 1;
|
2013-11-25 14:37:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 14:24:50 +00:00
|
|
|
int hdnode_from_xprv(uint32_t depth, uint32_t child_num, const uint8_t *chain_code, const uint8_t *private_key, const char* curve, HDNode *out)
|
2013-09-12 17:21:24 +00:00
|
|
|
{
|
2015-04-08 19:07:15 +00:00
|
|
|
bool failed = false;
|
2016-04-22 12:48:24 +00:00
|
|
|
const curve_info *info = get_curve_by_name(curve);
|
|
|
|
if (info == 0) {
|
2015-04-08 19:07:15 +00:00
|
|
|
failed = true;
|
2017-05-27 09:34:21 +00:00
|
|
|
} else if (info->params) {
|
2016-04-22 12:48:24 +00:00
|
|
|
bignum256 a;
|
|
|
|
bn_read_be(private_key, &a);
|
|
|
|
if (bn_is_zero(&a)) { // == 0
|
2015-04-13 16:19:33 +00:00
|
|
|
failed = true;
|
2016-04-22 12:48:24 +00:00
|
|
|
} else {
|
|
|
|
if (!bn_is_less(&a, &info->params->order)) { // >= order
|
|
|
|
failed = true;
|
|
|
|
}
|
2015-04-13 16:19:33 +00:00
|
|
|
}
|
|
|
|
MEMSET_BZERO(&a, sizeof(a));
|
2015-04-08 19:07:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 16:19:33 +00:00
|
|
|
if (failed) {
|
2014-08-07 12:34:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-04-08 19:07:15 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
out->curve = info;
|
2014-01-21 17:38:44 +00:00
|
|
|
out->depth = depth;
|
|
|
|
out->child_num = child_num;
|
|
|
|
memcpy(out->chain_code, chain_code, 32);
|
|
|
|
memcpy(out->private_key, private_key, 32);
|
2016-06-26 14:24:50 +00:00
|
|
|
MEMSET_BZERO(out->public_key, sizeof(out->public_key));
|
2014-08-07 12:34:33 +00:00
|
|
|
return 1;
|
2014-01-21 17:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 16:21:56 +00:00
|
|
|
int hdnode_from_seed(const uint8_t *seed, int seed_len, const char* curve, HDNode *out)
|
2014-01-21 17:38:44 +00:00
|
|
|
{
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL uint8_t I[32 + 32];
|
2014-05-20 22:15:55 +00:00
|
|
|
memset(out, 0, sizeof(HDNode));
|
2013-09-12 17:21:24 +00:00
|
|
|
out->depth = 0;
|
|
|
|
out->child_num = 0;
|
2016-04-19 16:21:56 +00:00
|
|
|
out->curve = get_curve_by_name(curve);
|
2016-04-20 17:54:18 +00:00
|
|
|
if (out->curve == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL HMAC_SHA512_CTX ctx;
|
|
|
|
hmac_sha512_Init(&ctx, (const uint8_t*) out->curve->bip32_name, strlen(out->curve->bip32_name));
|
|
|
|
hmac_sha512_Update(&ctx, seed, seed_len);
|
|
|
|
hmac_sha512_Final(&ctx, I);
|
2015-04-08 19:07:15 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
if (out->curve->params) {
|
|
|
|
bignum256 a;
|
|
|
|
while (true) {
|
|
|
|
bn_read_be(I, &a);
|
|
|
|
if (!bn_is_zero(&a) // != 0
|
|
|
|
&& bn_is_less(&a, &out->curve->params->order)) { // < order
|
|
|
|
break;
|
|
|
|
}
|
2017-08-10 14:31:21 +00:00
|
|
|
hmac_sha512_Init(&ctx, (const uint8_t*) out->curve->bip32_name, strlen(out->curve->bip32_name));
|
|
|
|
hmac_sha512_Update(&ctx, I, sizeof(I));
|
|
|
|
hmac_sha512_Final(&ctx, I);
|
2015-04-09 18:14:37 +00:00
|
|
|
}
|
2015-04-13 16:19:33 +00:00
|
|
|
MEMSET_BZERO(&a, sizeof(a));
|
2015-04-08 19:07:15 +00:00
|
|
|
}
|
2016-04-22 12:48:24 +00:00
|
|
|
memcpy(out->private_key, I, 32);
|
|
|
|
memcpy(out->chain_code, I + 32, 32);
|
2016-10-16 00:26:16 +00:00
|
|
|
MEMSET_BZERO(out->public_key, sizeof(out->public_key));
|
2015-04-13 16:19:33 +00:00
|
|
|
MEMSET_BZERO(I, sizeof(I));
|
2016-04-22 12:48:24 +00:00
|
|
|
return 1;
|
2013-09-12 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 14:24:50 +00:00
|
|
|
uint32_t hdnode_fingerprint(HDNode *node)
|
|
|
|
{
|
|
|
|
uint8_t digest[32];
|
|
|
|
uint32_t fingerprint;
|
|
|
|
|
|
|
|
hdnode_fill_public_key(node);
|
2017-12-09 17:59:44 +00:00
|
|
|
hasher_Raw(node->curve->hasher_type, node->public_key, 33, digest);
|
2016-06-26 14:24:50 +00:00
|
|
|
ripemd160(digest, 32, digest);
|
|
|
|
fingerprint = (digest[0] << 24) + (digest[1] << 16) + (digest[2] << 8) + digest[3];
|
|
|
|
MEMSET_BZERO(digest, sizeof(digest));
|
|
|
|
return fingerprint;
|
|
|
|
}
|
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
int hdnode_private_ckd(HDNode *inout, uint32_t i)
|
2013-09-12 17:21:24 +00:00
|
|
|
{
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL uint8_t data[1 + 32 + 4];
|
|
|
|
static CONFIDENTIAL uint8_t I[32 + 32];
|
|
|
|
static CONFIDENTIAL bignum256 a, b;
|
2013-09-12 17:21:24 +00:00
|
|
|
|
2013-11-25 14:37:49 +00:00
|
|
|
if (i & 0x80000000) { // private derivation
|
2013-09-12 17:21:24 +00:00
|
|
|
data[0] = 0;
|
|
|
|
memcpy(data + 1, inout->private_key, 32);
|
2013-11-25 14:37:49 +00:00
|
|
|
} else { // public derivation
|
2016-04-22 12:48:24 +00:00
|
|
|
if (!inout->curve->params) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-26 14:24:50 +00:00
|
|
|
hdnode_fill_public_key(inout);
|
2013-11-25 14:37:49 +00:00
|
|
|
memcpy(data, inout->public_key, 33);
|
2013-09-12 17:21:24 +00:00
|
|
|
}
|
|
|
|
write_be(data + 33, i);
|
|
|
|
|
|
|
|
bn_read_be(inout->private_key, &a);
|
|
|
|
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL HMAC_SHA512_CTX ctx;
|
|
|
|
hmac_sha512_Init(&ctx, inout->chain_code, 32);
|
|
|
|
hmac_sha512_Update(&ctx, data, sizeof(data));
|
|
|
|
hmac_sha512_Final(&ctx, I);
|
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
if (inout->curve->params) {
|
|
|
|
while (true) {
|
|
|
|
bool failed = false;
|
|
|
|
bn_read_be(I, &b);
|
|
|
|
if (!bn_is_less(&b, &inout->curve->params->order)) { // >= order
|
|
|
|
failed = true;
|
|
|
|
} else {
|
2016-11-05 20:21:48 +00:00
|
|
|
bn_add(&b, &a);
|
2016-04-22 12:48:24 +00:00
|
|
|
bn_mod(&b, &inout->curve->params->order);
|
|
|
|
if (bn_is_zero(&b)) {
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-04-21 11:14:20 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
if (!failed) {
|
|
|
|
bn_write_be(&b, inout->private_key);
|
|
|
|
break;
|
|
|
|
}
|
2015-04-09 18:14:37 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
data[0] = 1;
|
|
|
|
memcpy(data + 1, I + 32, 32);
|
2017-08-10 14:31:21 +00:00
|
|
|
hmac_sha512_Init(&ctx, inout->chain_code, 32);
|
|
|
|
hmac_sha512_Update(&ctx, data, sizeof(data));
|
|
|
|
hmac_sha512_Final(&ctx, I);
|
2015-04-09 18:14:37 +00:00
|
|
|
}
|
2016-04-22 12:48:24 +00:00
|
|
|
} else {
|
|
|
|
memcpy(inout->private_key, I, 32);
|
2014-08-07 12:34:33 +00:00
|
|
|
}
|
2017-04-21 11:14:20 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
memcpy(inout->chain_code, I + 32, 32);
|
|
|
|
inout->depth++;
|
|
|
|
inout->child_num = i;
|
2016-06-26 14:24:50 +00:00
|
|
|
MEMSET_BZERO(inout->public_key, sizeof(inout->public_key));
|
2014-02-19 15:58:48 +00:00
|
|
|
|
2015-04-13 16:19:33 +00:00
|
|
|
// making sure to wipe our memory
|
|
|
|
MEMSET_BZERO(&a, sizeof(a));
|
|
|
|
MEMSET_BZERO(&b, sizeof(b));
|
|
|
|
MEMSET_BZERO(I, sizeof(I));
|
|
|
|
MEMSET_BZERO(data, sizeof(data));
|
2016-04-22 12:48:24 +00:00
|
|
|
return 1;
|
2014-01-14 13:35:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 15:38:49 +00:00
|
|
|
int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent, const uint8_t *parent_chain_code, uint32_t i, curve_point *child, uint8_t *child_chain_code) {
|
2014-01-14 13:35:13 +00:00
|
|
|
uint8_t data[1 + 32 + 4];
|
|
|
|
uint8_t I[32 + 32];
|
|
|
|
bignum256 c;
|
|
|
|
|
|
|
|
if (i & 0x80000000) { // private derivation
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-09 18:14:37 +00:00
|
|
|
|
2017-03-30 15:38:49 +00:00
|
|
|
data[0] = 0x02 | (parent->y.val[0] & 0x01);
|
|
|
|
bn_write_be(&parent->x, data + 1);
|
|
|
|
write_be(data + 33, i);
|
2014-01-14 13:35:13 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
while (true) {
|
2017-03-30 15:38:49 +00:00
|
|
|
hmac_sha512(parent_chain_code, 32, data, sizeof(data), I);
|
2015-04-09 18:14:37 +00:00
|
|
|
bn_read_be(I, &c);
|
2017-03-30 15:38:49 +00:00
|
|
|
if (bn_is_less(&c, &curve->order)) { // < order
|
|
|
|
scalar_multiply(curve, &c, child); // b = c * G
|
|
|
|
point_add(curve, parent, child); // b = a + b
|
|
|
|
if (!point_is_infinity(child)) {
|
|
|
|
if (child_chain_code) {
|
|
|
|
memcpy(child_chain_code, I + 32, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe all stack data.
|
|
|
|
MEMSET_BZERO(data, sizeof(data));
|
|
|
|
MEMSET_BZERO(I, sizeof(I));
|
|
|
|
MEMSET_BZERO(&c, sizeof(c));
|
|
|
|
return 1;
|
2016-04-22 12:48:24 +00:00
|
|
|
}
|
2015-04-09 18:14:37 +00:00
|
|
|
}
|
2017-04-21 11:14:20 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
data[0] = 1;
|
|
|
|
memcpy(data + 1, I + 32, 32);
|
2015-04-09 18:14:37 +00:00
|
|
|
}
|
2017-03-30 15:38:49 +00:00
|
|
|
}
|
2014-01-14 13:35:13 +00:00
|
|
|
|
2017-03-30 15:38:49 +00:00
|
|
|
int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
|
|
|
{
|
|
|
|
curve_point parent, child;
|
|
|
|
|
|
|
|
if (!ecdsa_read_pubkey(inout->curve->params, inout->public_key, &parent)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!hdnode_public_ckd_cp(inout->curve->params, &parent, inout->chain_code, i, &child, inout->chain_code)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(inout->private_key, 0, 32);
|
2016-04-22 12:48:24 +00:00
|
|
|
inout->depth++;
|
|
|
|
inout->child_num = i;
|
2017-03-30 15:38:49 +00:00
|
|
|
inout->public_key[0] = 0x02 | (child.y.val[0] & 0x01);
|
|
|
|
bn_write_be(&child.x, inout->public_key + 1);
|
2016-04-22 12:48:24 +00:00
|
|
|
|
2015-04-09 18:14:37 +00:00
|
|
|
// Wipe all stack data.
|
2017-03-30 15:38:49 +00:00
|
|
|
MEMSET_BZERO(&parent, sizeof(parent));
|
|
|
|
MEMSET_BZERO(&child, sizeof(child));
|
2015-04-09 18:14:37 +00:00
|
|
|
|
2016-04-22 12:48:24 +00:00
|
|
|
return 1;
|
2013-11-08 15:02:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 17:59:44 +00:00
|
|
|
void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, HasherType hasher_type, char *addr, int addrsize, int addrformat)
|
2016-05-12 15:19:42 +00:00
|
|
|
{
|
2016-05-12 17:29:24 +00:00
|
|
|
uint8_t child_pubkey[33];
|
2016-05-12 15:19:42 +00:00
|
|
|
curve_point b;
|
|
|
|
|
2017-03-30 15:38:49 +00:00
|
|
|
hdnode_public_ckd_cp(&secp256k1, pub, chain_code, i, &b, NULL);
|
|
|
|
child_pubkey[0] = 0x02 | (b.y.val[0] & 0x01);
|
|
|
|
bn_write_be(&b.x, child_pubkey + 1);
|
2017-03-31 01:38:28 +00:00
|
|
|
|
2017-07-24 20:57:20 +00:00
|
|
|
switch (addrformat) {
|
|
|
|
case 1: // Segwit-in-P2SH
|
2017-12-09 17:59:44 +00:00
|
|
|
ecdsa_get_address_segwit_p2sh(child_pubkey, version, hasher_type, addr, addrsize);
|
2017-07-24 20:57:20 +00:00
|
|
|
break;
|
|
|
|
default: // normal address
|
2017-12-09 17:59:44 +00:00
|
|
|
ecdsa_get_address(child_pubkey, version, hasher_type, addr, addrsize);
|
2017-07-24 20:57:20 +00:00
|
|
|
break;
|
2017-03-31 01:38:28 +00:00
|
|
|
}
|
2016-05-12 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 18:09:00 +00:00
|
|
|
#if USE_BIP32_CACHE
|
|
|
|
static bool private_ckd_cache_root_set = false;
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL HDNode private_ckd_cache_root;
|
2015-01-26 18:09:00 +00:00
|
|
|
static int private_ckd_cache_index = 0;
|
|
|
|
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL struct {
|
2015-01-26 18:09:00 +00:00
|
|
|
bool set;
|
|
|
|
size_t depth;
|
|
|
|
uint32_t i[BIP32_CACHE_MAXDEPTH];
|
|
|
|
HDNode node;
|
|
|
|
} private_ckd_cache[BIP32_CACHE_SIZE];
|
|
|
|
|
2016-12-12 11:06:42 +00:00
|
|
|
int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count, uint32_t *fingerprint)
|
2015-01-26 18:09:00 +00:00
|
|
|
{
|
|
|
|
if (i_count == 0) {
|
2016-12-12 11:06:42 +00:00
|
|
|
// no way how to compute parent fingerprint
|
2015-01-26 18:09:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (i_count == 1) {
|
2016-12-12 11:06:42 +00:00
|
|
|
if (fingerprint) {
|
|
|
|
*fingerprint = hdnode_fingerprint(inout);
|
|
|
|
}
|
2015-01-26 18:09:00 +00:00
|
|
|
if (hdnode_private_ckd(inout, i[0]) == 0) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
// if root is not set or not the same
|
|
|
|
if (!private_ckd_cache_root_set || memcmp(&private_ckd_cache_root, inout, sizeof(HDNode)) != 0) {
|
|
|
|
// clear the cache
|
|
|
|
private_ckd_cache_index = 0;
|
|
|
|
memset(private_ckd_cache, 0, sizeof(private_ckd_cache));
|
|
|
|
// setup new root
|
|
|
|
memcpy(&private_ckd_cache_root, inout, sizeof(HDNode));
|
|
|
|
private_ckd_cache_root_set = true;
|
|
|
|
} else {
|
|
|
|
// try to find parent
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < BIP32_CACHE_SIZE; j++) {
|
|
|
|
if (private_ckd_cache[j].set &&
|
|
|
|
private_ckd_cache[j].depth == i_count - 1 &&
|
2016-04-19 16:21:56 +00:00
|
|
|
memcmp(private_ckd_cache[j].i, i, (i_count - 1) * sizeof(uint32_t)) == 0 &&
|
|
|
|
private_ckd_cache[j].node.curve == inout->curve) {
|
2015-01-26 18:09:00 +00:00
|
|
|
memcpy(inout, &(private_ckd_cache[j].node), sizeof(HDNode));
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// else derive parent
|
|
|
|
if (!found) {
|
|
|
|
size_t k;
|
|
|
|
for (k = 0; k < i_count - 1; k++) {
|
|
|
|
if (hdnode_private_ckd(inout, i[k]) == 0) return 0;
|
|
|
|
}
|
|
|
|
// and save it
|
2015-03-04 14:43:14 +00:00
|
|
|
memset(&(private_ckd_cache[private_ckd_cache_index]), 0, sizeof(private_ckd_cache[private_ckd_cache_index]));
|
2015-01-26 18:09:00 +00:00
|
|
|
private_ckd_cache[private_ckd_cache_index].set = true;
|
|
|
|
private_ckd_cache[private_ckd_cache_index].depth = i_count - 1;
|
|
|
|
memcpy(private_ckd_cache[private_ckd_cache_index].i, i, (i_count - 1) * sizeof(uint32_t));
|
|
|
|
memcpy(&(private_ckd_cache[private_ckd_cache_index].node), inout, sizeof(HDNode));
|
|
|
|
private_ckd_cache_index = (private_ckd_cache_index + 1) % BIP32_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-12-12 11:06:42 +00:00
|
|
|
if (fingerprint) {
|
|
|
|
*fingerprint = hdnode_fingerprint(inout);
|
|
|
|
}
|
2015-01-26 18:09:00 +00:00
|
|
|
if (hdnode_private_ckd(inout, i[i_count - 1]) == 0) return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-08 15:56:12 +00:00
|
|
|
void hdnode_get_address_raw(HDNode *node, uint32_t version, uint8_t *addr_raw)
|
2016-06-26 14:24:50 +00:00
|
|
|
{
|
|
|
|
hdnode_fill_public_key(node);
|
2017-12-09 17:59:44 +00:00
|
|
|
ecdsa_get_address_raw(node->public_key, version, node->curve->hasher_type, addr_raw);
|
2016-06-26 14:24:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 16:18:32 +00:00
|
|
|
void hdnode_get_address(HDNode *node, uint32_t version, char *addr, int addrsize)
|
|
|
|
{
|
|
|
|
hdnode_fill_public_key(node);
|
2017-12-09 17:59:44 +00:00
|
|
|
ecdsa_get_address(node->public_key, version, node->curve->hasher_type, addr, addrsize);
|
2016-10-08 16:18:32 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 17:38:44 +00:00
|
|
|
void hdnode_fill_public_key(HDNode *node)
|
2013-11-08 15:02:48 +00:00
|
|
|
{
|
2016-06-26 14:24:50 +00:00
|
|
|
if (node->public_key[0] != 0)
|
|
|
|
return;
|
2017-05-19 17:33:21 +00:00
|
|
|
if (node->curve->params) {
|
2016-04-22 15:47:48 +00:00
|
|
|
ecdsa_get_public_key33(node->curve->params, node->private_key, node->public_key);
|
2017-05-19 17:33:21 +00:00
|
|
|
} else {
|
|
|
|
node->public_key[0] = 1;
|
|
|
|
if (node->curve == &ed25519_info) {
|
|
|
|
ed25519_publickey(node->private_key, node->public_key + 1);
|
|
|
|
} else if (node->curve == &ed25519_sha3_info) {
|
|
|
|
ed25519_publickey_sha3(node->private_key, node->public_key + 1);
|
|
|
|
#if USE_KECCAK
|
|
|
|
} else if (node->curve == &ed25519_keccak_info) {
|
|
|
|
ed25519_publickey_keccak(node->private_key, node->public_key + 1);
|
|
|
|
#endif
|
|
|
|
} else if (node->curve == &curve25519_info) {
|
|
|
|
curve25519_scalarmult_basepoint(node->public_key + 1, node->private_key);
|
|
|
|
}
|
2016-04-22 15:47:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 14:21:33 +00:00
|
|
|
#if USE_ETHEREUM
|
|
|
|
int hdnode_get_ethereum_pubkeyhash(const HDNode *node, uint8_t *pubkeyhash)
|
|
|
|
{
|
|
|
|
uint8_t buf[65];
|
|
|
|
SHA3_CTX ctx;
|
|
|
|
|
|
|
|
/* get uncompressed public key */
|
|
|
|
ecdsa_get_public_key65(node->curve->params, node->private_key, buf);
|
|
|
|
|
|
|
|
/* compute sha3 of x and y coordinate without 04 prefix */
|
|
|
|
sha3_256_Init(&ctx);
|
|
|
|
sha3_Update(&ctx, buf + 1, 64);
|
|
|
|
keccak_Final(&ctx, buf);
|
|
|
|
|
|
|
|
/* result are the least significant 160 bits */
|
|
|
|
memcpy(pubkeyhash, buf + 12, 20);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-19 17:17:24 +00:00
|
|
|
#if USE_NEM
|
|
|
|
int hdnode_get_nem_address(HDNode *node, uint8_t version, char *address) {
|
|
|
|
if (node->curve != &ed25519_keccak_info) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdnode_fill_public_key(node);
|
|
|
|
return nem_get_address(&node->public_key[1], version, address);
|
|
|
|
}
|
2017-06-01 15:12:33 +00:00
|
|
|
|
|
|
|
int hdnode_get_nem_shared_key(const HDNode *node, const ed25519_public_key peer_public_key, const uint8_t *salt, ed25519_public_key mul, uint8_t *shared_key) {
|
|
|
|
if (node->curve != &ed25519_keccak_info) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof(ed25519_public_key) == SHA3_256_DIGEST_LENGTH
|
|
|
|
if (mul == NULL) mul = shared_key;
|
|
|
|
|
|
|
|
if (ed25519_scalarmult_keccak(mul, node->private_key, peer_public_key)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 32; i++) {
|
|
|
|
shared_key[i] = mul[i] ^ salt[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
keccak_256(shared_key, 32, shared_key);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-06-11 16:28:07 +00:00
|
|
|
|
|
|
|
int hdnode_nem_encrypt(const HDNode *node, const ed25519_public_key public_key, uint8_t *iv, const uint8_t *salt, const uint8_t *payload, size_t size, uint8_t *buffer) {
|
|
|
|
uint8_t last_block[AES_BLOCK_SIZE];
|
|
|
|
uint8_t remainder = size % AES_BLOCK_SIZE;
|
|
|
|
|
|
|
|
// Round down to last whole block
|
|
|
|
size -= remainder;
|
|
|
|
// Copy old last block
|
|
|
|
memcpy(last_block, &payload[size], remainder);
|
|
|
|
// Pad new last block with number of missing bytes
|
|
|
|
memset(&last_block[remainder], AES_BLOCK_SIZE - remainder, AES_BLOCK_SIZE - remainder);
|
|
|
|
|
|
|
|
uint8_t shared_key[SHA3_256_DIGEST_LENGTH];
|
|
|
|
if (!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
aes_encrypt_ctx ctx;
|
|
|
|
|
|
|
|
int ret = aes_encrypt_key256(shared_key, &ctx);
|
|
|
|
MEMSET_BZERO(shared_key, sizeof(shared_key));
|
|
|
|
|
|
|
|
if (ret != EXIT_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aes_cbc_encrypt(payload, buffer, size, iv, &ctx) != EXIT_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aes_cbc_encrypt(last_block, &buffer[size], sizeof(last_block), iv, &ctx) != EXIT_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hdnode_nem_decrypt(const HDNode *node, const ed25519_public_key public_key, uint8_t *iv, const uint8_t *salt, const uint8_t *payload, size_t size, uint8_t *buffer) {
|
|
|
|
uint8_t shared_key[SHA3_256_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
if (!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
aes_decrypt_ctx ctx;
|
|
|
|
|
|
|
|
int ret = aes_decrypt_key256(shared_key, &ctx);
|
|
|
|
MEMSET_BZERO(shared_key, sizeof(shared_key));
|
|
|
|
|
|
|
|
if (ret != EXIT_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aes_cbc_decrypt(payload, buffer, size, iv, &ctx) != EXIT_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-05-19 17:17:24 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-22 15:47:48 +00:00
|
|
|
// msg is a data to be signed
|
|
|
|
// msg_len is the message length
|
2016-10-06 14:54:07 +00:00
|
|
|
int hdnode_sign(HDNode *node, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
2016-04-22 15:47:48 +00:00
|
|
|
{
|
2017-05-19 17:33:21 +00:00
|
|
|
if (node->curve->params) {
|
2017-12-09 17:59:44 +00:00
|
|
|
return ecdsa_sign(node->curve->params, node->curve->hasher_type, node->private_key, msg, msg_len, sig, pby, is_canonical);
|
2016-10-13 19:24:17 +00:00
|
|
|
} else if (node->curve == &curve25519_info) {
|
|
|
|
return 1; // signatures are not supported
|
2016-04-22 15:47:48 +00:00
|
|
|
} else {
|
2017-05-19 17:33:21 +00:00
|
|
|
hdnode_fill_public_key(node);
|
|
|
|
if (node->curve == &ed25519_info) {
|
|
|
|
ed25519_sign(msg, msg_len, node->private_key, node->public_key + 1, sig);
|
|
|
|
} else if (node->curve == &ed25519_sha3_info) {
|
|
|
|
ed25519_sign_sha3(msg, msg_len, node->private_key, node->public_key + 1, sig);
|
|
|
|
#if USE_KECCAK
|
|
|
|
} else if (node->curve == &ed25519_keccak_info) {
|
|
|
|
ed25519_sign_keccak(msg, msg_len, node->private_key, node->public_key + 1, sig);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 0;
|
2016-04-22 15:47:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:54:07 +00:00
|
|
|
int hdnode_sign_digest(HDNode *node, const uint8_t *digest, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
|
2016-04-22 15:47:48 +00:00
|
|
|
{
|
2017-05-19 17:33:21 +00:00
|
|
|
if (node->curve->params) {
|
|
|
|
return ecdsa_sign_digest(node->curve->params, node->private_key, digest, sig, pby, is_canonical);
|
2016-10-13 19:24:17 +00:00
|
|
|
} else if (node->curve == &curve25519_info) {
|
|
|
|
return 1; // signatures are not supported
|
2016-04-22 15:47:48 +00:00
|
|
|
} else {
|
2017-05-19 17:33:21 +00:00
|
|
|
return hdnode_sign(node, digest, 32, sig, pby, is_canonical);
|
2016-04-22 15:47:48 +00:00
|
|
|
}
|
2013-11-25 14:37:49 +00:00
|
|
|
}
|
2014-05-15 15:36:23 +00:00
|
|
|
|
2016-10-13 19:24:17 +00:00
|
|
|
int hdnode_get_shared_key(const HDNode *node, const uint8_t *peer_public_key, uint8_t *session_key, int *result_size)
|
|
|
|
{
|
|
|
|
// Use elliptic curve Diffie-Helman to compute shared session key
|
2017-05-19 17:33:21 +00:00
|
|
|
if (node->curve->params) {
|
|
|
|
if (ecdh_multiply(node->curve->params, node->private_key, peer_public_key, session_key) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*result_size = 65;
|
|
|
|
return 0;
|
2016-10-13 19:24:17 +00:00
|
|
|
} else if (node->curve == &curve25519_info) {
|
|
|
|
session_key[0] = 0x04;
|
|
|
|
if (peer_public_key[0] != 0x40) {
|
|
|
|
return 1; // Curve25519 public key should start with 0x40 byte.
|
|
|
|
}
|
2017-03-28 21:05:59 +00:00
|
|
|
curve25519_scalarmult(session_key + 1, node->private_key, peer_public_key + 1);
|
2016-10-13 19:24:17 +00:00
|
|
|
*result_size = 33;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2017-05-19 17:33:21 +00:00
|
|
|
*result_size = 0;
|
|
|
|
return 1; // ECDH is not supported
|
2016-10-13 19:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 11:14:20 +00:00
|
|
|
static int hdnode_serialize(const HDNode *node, uint32_t fingerprint, uint32_t version, char use_public, char *str, int strsize)
|
2014-05-15 15:36:23 +00:00
|
|
|
{
|
2014-05-22 19:52:04 +00:00
|
|
|
uint8_t node_data[78];
|
2014-05-15 15:36:23 +00:00
|
|
|
write_be(node_data, version);
|
|
|
|
node_data[4] = node->depth;
|
2016-07-04 15:29:48 +00:00
|
|
|
write_be(node_data + 5, fingerprint);
|
2014-05-15 15:36:23 +00:00
|
|
|
write_be(node_data + 9, node->child_num);
|
|
|
|
memcpy(node_data + 13, node->chain_code, 32);
|
|
|
|
if (use_public) {
|
|
|
|
memcpy(node_data + 45, node->public_key, 33);
|
|
|
|
} else {
|
|
|
|
node_data[45] = 0;
|
|
|
|
memcpy(node_data + 46, node->private_key, 32);
|
|
|
|
}
|
2017-12-09 17:59:44 +00:00
|
|
|
int ret = base58_encode_check(node_data, sizeof(node_data), node->curve->hasher_type, str, strsize);
|
2015-04-09 18:14:37 +00:00
|
|
|
MEMSET_BZERO(node_data, sizeof(node_data));
|
2016-10-06 10:36:23 +00:00
|
|
|
return ret;
|
2014-05-15 15:36:23 +00:00
|
|
|
}
|
2014-05-15 17:35:28 +00:00
|
|
|
|
2017-04-21 11:14:20 +00:00
|
|
|
int hdnode_serialize_public(const HDNode *node, uint32_t fingerprint, uint32_t version, char *str, int strsize)
|
2014-05-15 17:35:28 +00:00
|
|
|
{
|
2017-04-21 11:14:20 +00:00
|
|
|
return hdnode_serialize(node, fingerprint, version, 1, str, strsize);
|
2014-05-15 17:35:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 11:14:20 +00:00
|
|
|
int hdnode_serialize_private(const HDNode *node, uint32_t fingerprint, uint32_t version, char *str, int strsize)
|
2014-05-15 17:35:28 +00:00
|
|
|
{
|
2017-04-21 11:14:20 +00:00
|
|
|
return hdnode_serialize(node, fingerprint, version, 0, str, strsize);
|
2014-05-20 22:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for validity of curve point in case of public data not performed
|
2017-12-10 12:35:02 +00:00
|
|
|
int hdnode_deserialize(const char *str, uint32_t version_public, uint32_t version_private, const char *curve, HDNode *node, uint32_t *fingerprint)
|
2014-05-20 22:15:55 +00:00
|
|
|
{
|
2014-05-22 19:52:04 +00:00
|
|
|
uint8_t node_data[78];
|
2014-05-20 22:15:55 +00:00
|
|
|
memset(node, 0, sizeof(HDNode));
|
2017-12-10 12:35:02 +00:00
|
|
|
node->curve = get_curve_by_name(curve);
|
2017-12-09 17:59:44 +00:00
|
|
|
if (base58_decode_check(str, node->curve->hasher_type, node_data, sizeof(node_data)) != sizeof(node_data)) {
|
2014-05-20 22:15:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t version = read_be(node_data);
|
2017-04-21 11:14:20 +00:00
|
|
|
if (version == version_public) {
|
2016-06-26 14:24:50 +00:00
|
|
|
MEMSET_BZERO(node->private_key, sizeof(node->private_key));
|
2014-05-20 22:15:55 +00:00
|
|
|
memcpy(node->public_key, node_data + 45, 33);
|
2017-04-21 11:14:20 +00:00
|
|
|
} else if (version == version_private) { // private node
|
2014-05-20 22:15:55 +00:00
|
|
|
if (node_data[45]) { // invalid data
|
2014-05-22 19:52:04 +00:00
|
|
|
return -2;
|
2014-05-20 22:15:55 +00:00
|
|
|
}
|
|
|
|
memcpy(node->private_key, node_data + 46, 32);
|
2016-06-26 14:24:50 +00:00
|
|
|
MEMSET_BZERO(node->public_key, sizeof(node->public_key));
|
2014-05-20 22:15:55 +00:00
|
|
|
} else {
|
2014-05-22 19:52:04 +00:00
|
|
|
return -3; // invalid version
|
2014-05-20 22:15:55 +00:00
|
|
|
}
|
|
|
|
node->depth = node_data[4];
|
2016-10-14 12:33:49 +00:00
|
|
|
if (fingerprint) {
|
|
|
|
*fingerprint = read_be(node_data + 5);
|
|
|
|
}
|
2014-05-20 22:15:55 +00:00
|
|
|
node->child_num = read_be(node_data + 9);
|
|
|
|
memcpy(node->chain_code, node_data + 13, 32);
|
|
|
|
return 0;
|
2014-05-15 17:35:28 +00:00
|
|
|
}
|
2016-04-22 12:48:24 +00:00
|
|
|
|
|
|
|
const curve_info *get_curve_by_name(const char *curve_name) {
|
|
|
|
if (curve_name == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (strcmp(curve_name, SECP256K1_NAME) == 0) {
|
|
|
|
return &secp256k1_info;
|
|
|
|
}
|
2017-12-10 12:50:22 +00:00
|
|
|
if (strcmp(curve_name, SECP256K1_DECRED_NAME) == 0) {
|
|
|
|
return &secp256k1_decred_info;
|
|
|
|
}
|
2016-04-22 12:48:24 +00:00
|
|
|
if (strcmp(curve_name, NIST256P1_NAME) == 0) {
|
|
|
|
return &nist256p1_info;
|
|
|
|
}
|
2016-04-22 15:47:48 +00:00
|
|
|
if (strcmp(curve_name, ED25519_NAME) == 0) {
|
|
|
|
return &ed25519_info;
|
|
|
|
}
|
2017-05-19 17:20:55 +00:00
|
|
|
if (strcmp(curve_name, ED25519_SHA3_NAME) == 0) {
|
|
|
|
return &ed25519_sha3_info;
|
|
|
|
}
|
|
|
|
#if USE_KECCAK
|
|
|
|
if (strcmp(curve_name, ED25519_KECCAK_NAME) == 0) {
|
|
|
|
return &ed25519_keccak_info;
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-13 19:24:17 +00:00
|
|
|
if (strcmp(curve_name, CURVE25519_NAME) == 0) {
|
|
|
|
return &curve25519_info;
|
|
|
|
}
|
2016-04-22 12:48:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|