mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 12:00:59 +00:00
Some more stack memory wipe before leaving functions.
Note that I preferred to change the multiple returns to multiple checks of a boolean to concentrate the erase into the last part of the functions.
This commit is contained in:
parent
aeefea054a
commit
70dc71c87e
9
base58.c
9
base58.c
@ -26,6 +26,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include "base58.h"
|
#include "base58.h"
|
||||||
#include "sha2.h"
|
#include "sha2.h"
|
||||||
|
#include "macro_utils.h"
|
||||||
|
|
||||||
static const int8_t b58digits_map[] = {
|
static const int8_t b58digits_map[] = {
|
||||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||||
@ -196,10 +197,10 @@ int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize
|
|||||||
sha256_Raw(data, datalen, hash);
|
sha256_Raw(data, datalen, hash);
|
||||||
sha256_Raw(hash, 32, hash);
|
sha256_Raw(hash, 32, hash);
|
||||||
size_t res = strsize;
|
size_t res = strsize;
|
||||||
if (b58enc(str, &res, buf, datalen + 4) != true) {
|
bool fSuccess = b58enc(str, &res, buf, datalen + 4);
|
||||||
return 0;
|
|
||||||
}
|
MEMSET_BZERO(buf, sizeof(buf));
|
||||||
return res;
|
return fSuccess ? res : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int base58_decode_check(const char *str, uint8_t *data, int datalen)
|
int base58_decode_check(const char *str, uint8_t *data, int datalen)
|
||||||
|
100
bip32.c
100
bip32.c
@ -31,8 +31,7 @@
|
|||||||
#include "sha2.h"
|
#include "sha2.h"
|
||||||
#include "ripemd160.h"
|
#include "ripemd160.h"
|
||||||
#include "base58.h"
|
#include "base58.h"
|
||||||
|
#include "macro_utils.h"
|
||||||
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
|
|
||||||
|
|
||||||
int hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, HDNode *out)
|
int hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, HDNode *out)
|
||||||
{
|
{
|
||||||
@ -91,9 +90,14 @@ int hdnode_from_seed(const uint8_t *seed, int seed_len, HDNode *out)
|
|||||||
if (bn_is_zero(&a)) {
|
if (bn_is_zero(&a)) {
|
||||||
failed = true;
|
failed = true;
|
||||||
}
|
}
|
||||||
else if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
|
else
|
||||||
|
{
|
||||||
|
if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Making sure a is wiped.
|
||||||
MEMSET_BZERO(&a,sizeof(a));
|
MEMSET_BZERO(&a,sizeof(a));
|
||||||
failed = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!failed) {
|
if(!failed) {
|
||||||
@ -132,23 +136,36 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
|
|||||||
|
|
||||||
bn_read_be(inout->private_key, &b);
|
bn_read_be(inout->private_key, &b);
|
||||||
|
|
||||||
|
bool failed = false;
|
||||||
|
|
||||||
if (!bn_is_less(&b, &order256k1)) { // >= order
|
if (!bn_is_less(&b, &order256k1)) { // >= order
|
||||||
return 0;
|
failed = true;
|
||||||
|
}
|
||||||
|
if(!failed) {
|
||||||
|
|
||||||
|
bn_addmod(&a, &b, &order256k1);
|
||||||
|
|
||||||
|
if (bn_is_zero(&a)) {
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bn_addmod(&a, &b, &order256k1);
|
if(!failed)
|
||||||
|
{
|
||||||
|
inout->depth++;
|
||||||
|
inout->child_num = i;
|
||||||
|
bn_write_be(&a, inout->private_key);
|
||||||
|
|
||||||
if (bn_is_zero(&a)) {
|
hdnode_fill_public_key(inout);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inout->depth++;
|
// Making sure to wipe our memory!
|
||||||
inout->child_num = i;
|
MEMSET_BZERO(&a,sizeof(a));
|
||||||
bn_write_be(&a, inout->private_key);
|
MEMSET_BZERO(&b,sizeof(b));
|
||||||
|
MEMSET_BZERO(I,sizeof(I));
|
||||||
hdnode_fill_public_key(inout);
|
MEMSET_BZERO(fingerprint,sizeof(fingerprint));
|
||||||
|
MEMSET_BZERO(data,sizeof(data));
|
||||||
return 1;
|
return failed ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
||||||
@ -171,32 +188,51 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
|||||||
inout->fingerprint = (fingerprint[0] << 24) + (fingerprint[1] << 16) + (fingerprint[2] << 8) + fingerprint[3];
|
inout->fingerprint = (fingerprint[0] << 24) + (fingerprint[1] << 16) + (fingerprint[2] << 8) + fingerprint[3];
|
||||||
|
|
||||||
memset(inout->private_key, 0, 32);
|
memset(inout->private_key, 0, 32);
|
||||||
|
|
||||||
|
bool failed = false;
|
||||||
if (!ecdsa_read_pubkey(inout->public_key, &a)) {
|
if (!ecdsa_read_pubkey(inout->public_key, &a)) {
|
||||||
return 0;
|
failed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
|
if(!failed)
|
||||||
memcpy(inout->chain_code, I + 32, 32);
|
{
|
||||||
bn_read_be(I, &c);
|
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
|
||||||
|
memcpy(inout->chain_code, I + 32, 32);
|
||||||
|
bn_read_be(I, &c);
|
||||||
|
|
||||||
if (!bn_is_less(&c, &order256k1)) { // >= order
|
if (!bn_is_less(&c, &order256k1)) { // >= order
|
||||||
return 0;
|
failed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scalar_multiply(&c, &b); // b = c * G
|
if(!failed)
|
||||||
point_add(&a, &b); // b = a + b
|
{
|
||||||
|
scalar_multiply(&c, &b); // b = c * G
|
||||||
|
point_add(&a, &b); // b = a + b
|
||||||
|
|
||||||
if (!ecdsa_validate_pubkey(&b)) {
|
if (!ecdsa_validate_pubkey(&b)) {
|
||||||
return 0;
|
failed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inout->public_key[0] = 0x02 | (b.y.val[0] & 0x01);
|
if(!failed)
|
||||||
bn_write_be(&b.x, inout->public_key + 1);
|
{
|
||||||
|
inout->public_key[0] = 0x02 | (b.y.val[0] & 0x01);
|
||||||
|
bn_write_be(&b.x, inout->public_key + 1);
|
||||||
|
|
||||||
inout->depth++;
|
inout->depth++;
|
||||||
inout->child_num = i;
|
inout->child_num = i;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
// Wipe all stack data.
|
||||||
|
MEMSET_BZERO(data,sizeof(data));
|
||||||
|
MEMSET_BZERO(I,sizeof(I));
|
||||||
|
MEMSET_BZERO(fingerprint,sizeof(fingerprint));
|
||||||
|
MEMSET_BZERO(&a,sizeof(a));
|
||||||
|
MEMSET_BZERO(&b,sizeof(b));
|
||||||
|
MEMSET_BZERO(&c,sizeof(c));
|
||||||
|
|
||||||
|
return failed ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if USE_BIP32_CACHE
|
#if USE_BIP32_CACHE
|
||||||
@ -286,7 +322,9 @@ void hdnode_serialize(const HDNode *node, uint32_t version, char use_public, cha
|
|||||||
node_data[45] = 0;
|
node_data[45] = 0;
|
||||||
memcpy(node_data + 46, node->private_key, 32);
|
memcpy(node_data + 46, node->private_key, 32);
|
||||||
}
|
}
|
||||||
base58_encode_check(node_data, 78, str, strsize);
|
base58_encode_check(node_data, sizeof(node_data), str, strsize);
|
||||||
|
|
||||||
|
MEMSET_BZERO(node_data, sizeof(node_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void hdnode_serialize_public(const HDNode *node, char *str, int strsize)
|
void hdnode_serialize_public(const HDNode *node, char *str, int strsize)
|
||||||
|
8
macro_utils.h
Normal file
8
macro_utils.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
#if !defined( _MACRO_UTILS_H )
|
||||||
|
#define _MACRO_UTILS_H
|
||||||
|
|
||||||
|
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user