Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cosmos #188

Merged
merged 51 commits into from
Dec 3, 2019
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7d084d9
boil your plates
ProofOfKeags Nov 10, 2019
c8be2d2
partial
ProofOfKeags Nov 10, 2019
aa3c082
works on emulator
ProofOfKeags Nov 11, 2019
bbb31d9
tests passing
ProofOfKeags Nov 11, 2019
6f74447
fixes submodule deps for pr
ProofOfKeags Nov 12, 2019
c866112
missed conflict header in rebase
ProofOfKeags Nov 13, 2019
06a31e5
Update lib/firmware/cosmos.c
ProofOfKeags Nov 13, 2019
8c5f92e
moves import
ProofOfKeags Nov 13, 2019
0d493b8
Merge branch 'feature/cosmos' of github.com:Start9Labs/keepkey-firmwa…
ProofOfKeags Nov 13, 2019
0f2fcb9
puts responsibility on getAddress to fill pubkey
ProofOfKeags Nov 13, 2019
702b185
fixes confirmation dialogs
ProofOfKeags Nov 13, 2019
720a7ba
displays node_str instead of from address
ProofOfKeags Nov 13, 2019
e3048b2
fixes committed conflict
ProofOfKeags Nov 13, 2019
200efcd
fixes mismatch logic, adds EOS to account based calls
ProofOfKeags Nov 13, 2019
1ad286c
changes to rebased cosmos commit
ProofOfKeags Nov 13, 2019
3062856
temp change to start9 fork of trezor firmware
ProofOfKeags Nov 13, 2019
e22364d
changes trezor-firmware commit to include base conversion functionality
ProofOfKeags Nov 13, 2019
c4063bf
Update lib/firmware/coins.c
ProofOfKeags Nov 13, 2019
f9e3a40
Update lib/firmware/coins.c
ProofOfKeags Nov 13, 2019
66c7e6e
builds clean
ProofOfKeags Nov 13, 2019
605c587
removes commented code
ProofOfKeags Nov 13, 2019
aa3826d
changes back to keepkey remotes on submodules
ProofOfKeags Nov 13, 2019
2b3dac7
escaping
dr-bonez Nov 13, 2019
975b46b
Merge branch 'feature/cosmos' of github.com:Start9Labs/keepkey-firmwa…
dr-bonez Nov 13, 2019
a21af26
tests pass again
ProofOfKeags Nov 13, 2019
5b3680a
WIP: refactor message protocol for interactivity
ProofOfKeags Nov 14, 2019
5fd3abb
updates device protocol dependency
ProofOfKeags Nov 14, 2019
59d5358
WIP: trying to get unit tests to pass with refactor
ProofOfKeags Nov 14, 2019
17bd691
WIP: phase 2 works?
ProofOfKeags Nov 14, 2019
6e7c701
Unit Tests Pass For Real™
ProofOfKeags Nov 14, 2019
fe51ffc
switched to sizeof
ProofOfKeags Nov 14, 2019
5c2fa36
fix optionals
dr-bonez Nov 14, 2019
12bcaa2
Integration tests work
ProofOfKeags Nov 14, 2019
a10fa44
Update lib/firmware/cosmos.c
ProofOfKeags Nov 21, 2019
e3c7686
Update lib/firmware/fsm_msg_cosmos.h
ProofOfKeags Nov 21, 2019
cf90b53
dead code removal, macro inlining
ProofOfKeags Nov 21, 2019
979abbd
Merge branch 'feature/cosmos' of github.com:Start9Labs/keepkey-firmwa…
ProofOfKeags Nov 21, 2019
fa8473c
removes requirement of from_address, calculates it dynamically
ProofOfKeags Nov 21, 2019
291ac60
removes has_from_address check
ProofOfKeags Nov 21, 2019
c96ff1f
fix for bip32 derivation
ProofOfKeags Nov 22, 2019
4ad5419
Update trezor-firmware dep
keepkeyjon Nov 22, 2019
f56d76c
cosmos: rearrange msg signing for clarity
keepkeyjon Nov 22, 2019
223075e
cosmos: avoid UB in testcase
keepkeyjon Nov 22, 2019
de09ac8
fixes UB issue with copying
ProofOfKeags Nov 22, 2019
ac9393c
aborts and proper string formatting
ProofOfKeags Nov 22, 2019
6d571f8
Merge pull request #2 from keepkey/feature/cosmos
ProofOfKeags Nov 22, 2019
f6f663d
Merge branch 'master' into feature/cosmos
keepkeyjon Nov 22, 2019
be49036
cosmos: whole_account = false
keepkeyjon Nov 22, 2019
ec716cf
update device-protocol
keepkeyjon Nov 22, 2019
cf06bc4
Merge pull request #3 from keepkey/feature/cosmos
ProofOfKeags Nov 22, 2019
91dc4f4
Update lib/firmware/fsm_msg_cosmos.h
ProofOfKeags Nov 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
builds clean
ProofOfKeags committed Nov 13, 2019

Verified

This commit was signed with the committer’s verified signature.
jdaugherty James Daugherty
commit 66c7e6e2d1005d541c86c0182da08c3b6ff8b228
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
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
@@ -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(\
5 changes: 4 additions & 1 deletion include/keepkey/firmware/cosmos.h
Original file line number Diff line number Diff line change
@@ -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,
1 change: 1 addition & 0 deletions lib/board/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ include(CheckSymbolExists)

set(sources
check_bootloader.c
common.c
confirm_sm.c
draw.c
font.c
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
@@ -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;
@@ -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;
}

58 changes: 28 additions & 30 deletions lib/firmware/cosmos.c
Original file line number Diff line number Diff line change
@@ -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
@@ -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);

9 changes: 9 additions & 0 deletions lib/firmware/crypto.c
Original file line number Diff line number Diff line change
@@ -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);
@@ -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);

@@ -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);

11 changes: 8 additions & 3 deletions lib/firmware/fsm_msg_cosmos.h
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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)
{
7 changes: 4 additions & 3 deletions lib/firmware/recovery.c
Original file line number Diff line number Diff line change
@@ -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"

@@ -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 */
@@ -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)
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.