Skip to content

Commit

Permalink
assertion fixes and use raw buffer for public key
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Dec 13, 2023
1 parent 8b5082e commit ba7a42e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ ENABLE_BLUETOOTH = 1
#DISABLE_STANDARD_WEBUSB = 1
#DISABLE_STANDARD_BAGL_UX_FLOW = 1
DEFINES += HAVE_LEGACY_PID

ifeq ($(DEBUG), 1)
DEFINES += HAVE_LEDGER_ASSERT_DISPLAY LEDGER_ASSERT_CONFIG_MESSAGE_INFO
endif

include $(BOLOS_SDK)/Makefile.standard_app
17 changes: 4 additions & 13 deletions src/blake2b.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@
#include "sia.h"

void blake2b_init(cx_blake2b_t *S) {
if (cx_blake2b_init_no_throw(S, 256) != CX_OK) {
ASSERT_DISPLAY_MESSAGE("blake2b_init failed");
return;
}
LEDGER_ASSERT(CX_OK == cx_blake2b_init_no_throw(S, 256), "blake2b_init failed");
}

void blake2b_update(cx_blake2b_t *S, const uint8_t *in, uint64_t inlen) {
if (cx_hash_no_throw((cx_hash_t *)S, 0, in, inlen, NULL, 0) != CX_OK) {
ASSERT_DISPLAY_MESSAGE("blake2b_update failed");
return;
}
LEDGER_ASSERT(CX_OK == cx_hash_no_throw((cx_hash_t *)S, 0, in, inlen, NULL, 0), "blake2b_update failed");
}

void blake2b_final(cx_blake2b_t *S, uint8_t *out, uint64_t outlen) {
uint8_t buf[32];
if (cx_hash_no_throw((cx_hash_t *)S, CX_LAST, NULL, 0, buf, sizeof(buf)) != CX_OK) {
ASSERT_DISPLAY_MESSAGE("blake2b_final failed");
return;
}
uint8_t buf[32] = {0};
LEDGER_ASSERT(CX_OK == cx_hash_no_throw((cx_hash_t *)S, CX_LAST, NULL, 0, buf, sizeof(buf)), "blake2b_final failed");
memmove(out, buf, outlen);
}

Expand Down
8 changes: 4 additions & 4 deletions src/getPublicKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ static void continue_review(void) {
#endif

static unsigned int io_seproxyhal_touch_pk_ok(void) {
cx_ecfp_public_key_t publicKey = {0};
uint8_t publicKey[65] = {0};

// The response APDU will contain multiple objects, which means we need to
// remember our offset within G_io_apdu_buffer. By convention, the offset
// variable is named 'tx'.
uint8_t tx = 0;

deriveSiaPublicKey(ctx->keyIndex, publicKey.W);
extractPubkeyBytes(G_io_apdu_buffer + tx, &publicKey);
deriveSiaPublicKey(ctx->keyIndex, publicKey);
extractPubkeyBytes(G_io_apdu_buffer + tx, publicKey);
tx += 32;
pubkeyToSiaAddress((char*)G_io_apdu_buffer + tx, &publicKey);
pubkeyToSiaAddress((char*)G_io_apdu_buffer + tx, publicKey);
tx += 76;

// Flush the APDU buffer, sending the response.
Expand Down
22 changes: 7 additions & 15 deletions src/sia.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@ static void siaSetPath(uint32_t index, uint32_t path[static 5]) {
path[4] = 0x80000000;
}

void deriveSiaPublicKey(uint32_t index, uint8_t raw_pubkey[static 64]) {
void deriveSiaPublicKey(uint32_t index, uint8_t publicKey[static 65]) {
uint32_t bip32Path[5];
siaSetPath(index, bip32Path);

if (raw_pubkey) {
if (bip32_derive_with_seed_get_pubkey_256(HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32Path, 5, raw_pubkey, NULL, CX_SHA512, NULL, 0) != CX_OK) {
ASSERT_DISPLAY_MESSAGE("get pubkey failed");
return;
}
}
LEDGER_ASSERT(CX_OK == bip32_derive_with_seed_get_pubkey_256(HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32Path, 5, publicKey, NULL, CX_SHA512, NULL, 0), "get pubkey failed");
}

void extractPubkeyBytes(unsigned char *dst, const cx_ecfp_public_key_t *publicKey) {
void extractPubkeyBytes(unsigned char *dst, const uint8_t publicKey[static 65]) {
for (int i = 0; i < 32; i++) {
dst[i] = publicKey->W[64 - i];
dst[i] = publicKey[64 - i];
}
if (publicKey->W[32] & 1) {
if (publicKey[32] & 1) {
dst[31] |= 0x80;
}
}
Expand All @@ -45,10 +40,7 @@ void deriveAndSign(uint8_t *dst, uint32_t index, const uint8_t *hash) {
siaSetPath(index, bip32Path);

size_t signatureLength = 64;
if (bip32_derive_with_seed_eddsa_sign_hash_256(HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32Path, 5, CX_SHA512, hash, 32, dst, &signatureLength, NULL, 0) != CX_OK) {
ASSERT_DISPLAY_MESSAGE("signing txn failed");
return;
}
LEDGER_ASSERT(CX_OK == bip32_derive_with_seed_eddsa_sign_hash_256(HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32Path, 5, CX_SHA512, hash, 32, dst, &signatureLength, NULL, 0), "signing txn failed");
}

void bin2hex(char *dst, const uint8_t *data, uint64_t inlen) {
Expand All @@ -60,7 +52,7 @@ void bin2hex(char *dst, const uint8_t *data, uint64_t inlen) {
dst[2 * inlen] = '\0';
}

void pubkeyToSiaAddress(char *dst, const cx_ecfp_public_key_t *publicKey) {
void pubkeyToSiaAddress(char *dst, const uint8_t publicKey[static 65]) {
// A Sia address is the Merkle root of a set of unlock conditions.
// For a "standard" address, the unlock conditions are:
//
Expand Down
6 changes: 3 additions & 3 deletions src/sia.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ int formatSC(char *buf, uint8_t decLen);

// extractPubkeyBytes converts a Ledger-style public key to a Sia-friendly
// 32-byte array.
void extractPubkeyBytes(unsigned char *dst, const cx_ecfp_public_key_t *publicKey);
void extractPubkeyBytes(unsigned char *dst, const uint8_t publicKey[static 65]);

// pubkeyToSiaAddress converts a Ledger pubkey to a Sia wallet address.
void pubkeyToSiaAddress(char *dst, const cx_ecfp_public_key_t *publicKey);
void pubkeyToSiaAddress(char *dst, const uint8_t publicKey[static 65]);

// deriveSiaPublicKey derives an Ed25519 public key from an index and the
// Ledger seed.
void deriveSiaPublicKey(uint32_t index, uint8_t raw_pubkey[static 64]);
void deriveSiaPublicKey(uint32_t index, uint8_t publicKey[static 64]);

// deriveAndSign derives an Ed25519 private key from an index and the
// Ledger seed, and uses it to produce a 64-byte signature of the provided
Expand Down
6 changes: 3 additions & 3 deletions src/txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ void txn_init(txn_state_t *txn, uint16_t sigIndex, uint32_t changeIndex) {
txn->elementIndex = 0;
txn->elements[txn->elementIndex].elemType = -1; // first increment brings it to SC_INPUT

cx_ecfp_public_key_t publicKey = {0};
deriveSiaPublicKey(changeIndex, publicKey.W);
pubkeyToSiaAddress((char *)&txn->changeAddr, &publicKey);
uint8_t publicKey[65] = {0};
deriveSiaPublicKey(changeIndex, publicKey);
pubkeyToSiaAddress((char *)&txn->changeAddr, publicKey);

// initialize hash state
blake2b_init(&txn->blake);
Expand Down

0 comments on commit ba7a42e

Please sign in to comment.