Skip to content

Commit

Permalink
builds clean
Browse files Browse the repository at this point in the history
  • Loading branch information
ProofOfKeags committed Nov 13, 2019
2 parents f9e3a40 + 695cbdc commit 66c7e6e
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 88 deletions.
1 change: 1 addition & 0 deletions deps/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(sources
trezor-firmware/crypto/bip39.c
trezor-firmware/crypto/hmac.c
trezor-firmware/crypto/hmac_drbg.c
trezor-firmware/crypto/sha2.c
trezor-firmware/crypto/base32.c
trezor-firmware/crypto/hasher.c
Expand Down
2 changes: 1 addition & 1 deletion deps/crypto/trezor-firmware
50 changes: 50 additions & 0 deletions include/keepkey/board/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* 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/>.
*/

#ifndef __TREZORHAL_COMMON_H__
#define __TREZORHAL_COMMON_H__

#include <stddef.h>
#include <stdint.h>

#define HW_ENTROPY_LEN (12 + 32)
extern uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];

void __attribute__((noreturn))
__fatal_error(const char *expr, const char *msg, const char *file, int line,
const char *func);
void __attribute__((noreturn))
error_shutdown(const char *line1, const char *line2, const char *line3,
const char *line4);

#define ensure(expr, msg) \
(((expr) == sectrue) \
? (void)0 \
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))

void hal_delay(uint32_t ms);

void wait_random(void);

void drbg_init(void);
void drbg_reseed(const uint8_t *entropy, size_t len);
void drbg_generate(uint8_t *buf, size_t len);
uint32_t drbg_random32(void);

#endif
71 changes: 37 additions & 34 deletions include/keepkey/firmware/coins.def

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/keepkey/firmware/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define NA 0xFFFF /*etherum does not use P2PH or P2SH */
#define ETHEREUM "Ethereum"
#define ETHEREUM_CLS "ETH Classic"
#define ETHEREUM_TST "ETH Testnet"

enum {
#define X(\
Expand Down
5 changes: 4 additions & 1 deletion include/keepkey/firmware/cosmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include "messages.pb.h"
#include "trezor/crypto/bip32.h"

bool cosmos_getAddress(const uint8_t *public_key, char *address);
bool cosmos_path_mismatched(const CoinType *_coin,
const uint32_t *address_n,
const uint32_t address_n_count);
bool cosmos_getAddress(const HDNode *node, char *address);
bool cosmos_signTx(const uint8_t* private_key,
const uint64_t account_number,
const char* chain_id,
Expand Down
1 change: 1 addition & 0 deletions lib/board/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include(CheckSymbolExists)

set(sources
check_bootloader.c
common.c
confirm_sm.c
draw.c
font.c
Expand Down
50 changes: 50 additions & 0 deletions lib/board/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* 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/>.
*/

#include "keepkey/board/common.h"

#include "keepkey/rand/rng.h"
#include "trezor/crypto/hmac_drbg.h"
#include "trezor/crypto/rand.h"

#include <stdio.h>

uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];

static HMAC_DRBG_CTX drbg_ctx;

void drbg_init() {
uint8_t entropy[48] = {0};
random_buffer(entropy, sizeof(entropy));
hmac_drbg_init(&drbg_ctx, entropy, sizeof(entropy), NULL, 0);
}

void drbg_reseed(const uint8_t *entropy, size_t len) {
hmac_drbg_reseed(&drbg_ctx, entropy, len, NULL, 0);
}

void drbg_generate(uint8_t *buf, size_t len) {
hmac_drbg_generate(&drbg_ctx, buf, len);
}

uint32_t drbg_random32(void) {
uint32_t value = 0;
drbg_generate((uint8_t *)&value, sizeof(value));
return value;
}
4 changes: 4 additions & 0 deletions lib/firmware/coins.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ static bool path_mismatched(const CoinType *coin, const uint32_t *address_n,
if (address_n_count == 5 &&
(strncmp(coin->coin_name, ETHEREUM, strlen(ETHEREUM)) == 0 ||
strncmp(coin->coin_name, ETHEREUM_CLS, sizeof(ETHEREUM_CLS)) == 0 ||
strncmp(coin->coin_name, ETHEREUM_TST, sizeof(ETHEREUM_TST)) == 0 ||
coin->has_contract_address)) {
if (whole_account)
return true;
Expand Down Expand Up @@ -422,6 +423,9 @@ bool isEthereumLike(const char *coin_name)
if (strcmp(coin_name, ETHEREUM_CLS) == 0)
return true;

if (strcmp(coin_name, ETHEREUM_TST) == 0)
return true;

return false;
}

Expand Down
58 changes: 28 additions & 30 deletions lib/firmware/cosmos.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,34 @@ bool cosmos_path_mismatched(const CoinType *_coin,
* need guidance on the way you want to handle syncing the locked commit,
* keepkey@HEAD, and the new commit.
*/
static int convert_bits(uint8_t *out, size_t *outlen, int outbits, const uint8_t *in, size_t inlen, int inbits, int pad)
{
uint32_t val = 0;
int bits = 0;
uint32_t maxv = (((uint32_t)1) << outbits) - 1;
while (inlen--)
{
val = (val << inbits) | *(in++);
bits += inbits;
while (bits >= outbits)
{
bits -= outbits;
out[(*outlen)++] = (val >> bits) & maxv;
}
}
if (pad)
{
if (bits)
{
out[(*outlen)++] = (val << (outbits - bits)) & maxv;
}
}
else if (((val << (outbits - bits)) & maxv) || bits >= inbits)
{
return 0;
}
return 1;
}
// static int convert_bits(uint8_t *out, size_t *outlen, int outbits, const uint8_t *in, size_t inlen, int inbits, int pad)
// {
// uint32_t val = 0;
// int bits = 0;
// uint32_t maxv = (((uint32_t)1) << outbits) - 1;
// while (inlen--)
// {
// val = (val << inbits) | *(in++);
// bits += inbits;
// while (bits >= outbits)
// {
// bits -= outbits;
// out[(*outlen)++] = (val >> bits) & maxv;
// }
// }
// if (pad)
// {
// if (bits)
// {
// out[(*outlen)++] = (val << (outbits - bits)) & maxv;
// }
// }
// else if (((val << (outbits - bits)) & maxv) || bits >= inbits)
// {
// return 0;
// }
// return 1;
// }

/*
* Gets the address
Expand All @@ -69,8 +69,6 @@ static int convert_bits(uint8_t *out, size_t *outlen, int outbits, const uint8_t
*/
bool cosmos_getAddress(const HDNode *node, char *address)
{
hdnode_fill_public_key(node);

uint8_t hash160Buf[RIPEMD160_DIGEST_LENGTH];
ecdsa_get_pubkeyhash(node->public_key, HASHER_SHA2_RIPEMD, hash160Buf);

Expand Down
9 changes: 9 additions & 0 deletions lib/firmware/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ int cryptoGetECDHSessionKey(const HDNode *node, const uint8_t *peer_public_key,
return 0;
}

_Static_assert(sizeof(((CoinType*)0)->signed_message_header) < 256, "Message header too long");

static void cryptoMessageHash(const CoinType *coin, const curve_info *curve, const uint8_t *message, size_t message_len, uint8_t hash[HASHER_DIGEST_LENGTH]) {
Hasher hasher;
hasher_Init(&hasher, curve->hasher_sign);
uint8_t header_len = strlen(coin->signed_message_header);
hasher_Update(&hasher, &header_len, 1);
hasher_Update(&hasher, (const uint8_t *)coin->signed_message_header, strlen(coin->signed_message_header));
uint8_t varint[5];
uint32_t l = ser_length(message_len, varint);
Expand All @@ -147,6 +151,9 @@ int cryptoMessageSign(const CoinType *coin, HDNode *node, InputScriptType script
{
const curve_info *curve = get_curve_by_name(coin->curve_name);
if (!curve) return 1;

if (!coin->has_signed_message_header) return 1;

uint8_t hash[HASHER_DIGEST_LENGTH];
cryptoMessageHash(coin, curve, message, message_len, hash);

Expand Down Expand Up @@ -181,6 +188,8 @@ int cryptoMessageVerify(const CoinType *coin, const uint8_t *message, size_t mes
const curve_info *curve = get_curve_by_name(coin->curve_name);
if (!curve) return 1;

if (!coin->has_signed_message_header) return 1;

uint8_t hash[HASHER_DIGEST_LENGTH];
cryptoMessageHash(coin, curve, message, message_len, hash);

Expand Down
11 changes: 8 additions & 3 deletions lib/firmware/fsm_msg_cosmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ void fsm_msgCosmosGetAddress(const CosmosGetAddress *msg)

CHECK_PIN

const char *coin_name = msg->has_coin_name ? msg->coin_name : "Cosmos";
const char *coin_name = "Cosmos";
const CoinType *coin = fsm_getCoin(true, coin_name);
if (!coin) { return; }
HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL);
if (!node) { return; }

if (!cosmos_getAddress(node, resp->address) {
hdnode_fill_public_key(node);

if (!cosmos_getAddress(node, resp->address)) {
fsm_sendFailure(FailureType_Failure_Other, _("Can't encode address"));
layoutHome();
return;
Expand All @@ -38,7 +40,7 @@ void fsm_msgCosmosGetAddress(const CosmosGetAddress *msg)
}
}

if(!confirm_address(node_str, address)) {
if(!confirm_address(node_str, resp->address)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, "Show address cancelled");
layoutHome();
return;
Expand All @@ -56,6 +58,9 @@ void fsm_msgCosmosSignTx(const CosmosSignTx *msg)
CHECK_INITIALIZED
CHECK_PIN

const char *coin_name = "Cosmos";
const CoinType *coin = fsm_getCoin(true, coin_name);
if (!coin) { return; }
HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL);
if (!node)
{
Expand Down
7 changes: 4 additions & 3 deletions lib/firmware/recovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "keepkey/firmware/storage.h"
#include "keepkey/rand/rng.h"
#include "trezor/crypto/bip39.h"
#include "trezor/crypto/bip39_english.h"
#include "trezor/crypto/memzero.h"
#include "trezor/crypto/rand.h"

Expand Down Expand Up @@ -61,8 +62,8 @@ void next_word(void) {
snprintf(title_formatted, SMALL_STR_BUF, "Device Recovery Step %lu/24", (unsigned long)(word_index + 1));

if (word_pos == 0) {
const char * const *wl = mnemonic_wordlist();
strlcpy(fake_word, wl[random_uniform(2048)], sizeof(fake_word));
const char * const *wl = wordlist;
strlcpy(fake_word, wl[random_uniform(2048)], sizeof(fake_word));

/* Format body for fake word */
/* snprintf: 18 + 12 (fake_word) + 1 (NULL) = 31 */
Expand Down Expand Up @@ -158,7 +159,7 @@ void recovery_init(uint32_t _word_count, bool passphrase_protection,
}

static bool isInWordList(const char *word) {
const char * const *wl = mnemonic_wordlist();
const char * const *wl = wordlist;
while (*wl)
{
if (strcmp(word, *wl) == 0)
Expand Down
Loading

0 comments on commit 66c7e6e

Please sign in to comment.