Skip to content

Commit

Permalink
src: Improve crypto code using SDK crypto_helpers and LEDGER_ASSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Chapron committed Dec 12, 2023
1 parent 1a17b33 commit 5762e49
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 113 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ include $(BOLOS_SDK)/Makefile.glyphs
APP_SOURCE_PATH += src
SDK_SOURCE_PATH += lib_stusb lib_stusb_impl

# Allow usage of function from lib_standard_app/crypto_helpers.c
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c

ifneq ($(TARGET_NAME),TARGET_STAX)
SDK_SOURCE_PATH += lib_ux
endif
Expand Down
137 changes: 33 additions & 104 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
#include "cx.h"
#include <stdbool.h>
#include <stdlib.h>

#include "lib_standard_app/crypto_helpers.h"
#include "ledger_assert.h"

#include "utils.h"

void get_public_key(uint8_t *publicKeyArray, const uint32_t *derivationPath, size_t pathLength) {
cx_ecfp_private_key_t privateKey;
cx_ecfp_public_key_t publicKey;
void get_public_key(uint8_t publicKeyArray[static PUBKEY_LENGTH], const uint32_t *derivationPath, size_t pathLength) {
uint8_t raw_pubkey[65];

get_private_key(&privateKey, derivationPath, pathLength);
BEGIN_TRY {
TRY {
cx_ecfp_generate_pair(CX_CURVE_Ed25519, &publicKey, &privateKey, 1);
}
CATCH_OTHER(e) {
MEMCLEAR(privateKey);
THROW(e);
}
FINALLY {
MEMCLEAR(privateKey);
}
if (CX_OK != bip32_derive_with_seed_get_pubkey_256(HDW_ED25519_SLIP10,
CX_CURVE_Ed25519,
derivationPath,
pathLength,
raw_pubkey,
NULL,
CX_SHA512,
NULL,
0)) {
LEDGER_ASSERT(false, "Fail to get pub key");
}
END_TRY;

for (int i = 0; i < PUBKEY_LENGTH; i++) {
publicKeyArray[i] = publicKey.W[PUBKEY_LENGTH + PRIVATEKEY_LENGTH - i];
publicKeyArray[i] = raw_pubkey[PUBKEY_LENGTH + PRIVATEKEY_LENGTH - i];
}
if ((publicKey.W[PUBKEY_LENGTH] & 1) != 0) {
if ((raw_pubkey[PUBKEY_LENGTH] & 1) != 0) {
publicKeyArray[PUBKEY_LENGTH - 1] |= 0x80;
}
}
Expand All @@ -35,66 +35,6 @@ uint32_t readUint32BE(uint8_t *buffer) {
return ((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]));
}

void get_private_key(cx_ecfp_private_key_t *privateKey,
const uint32_t *derivationPath,
size_t pathLength) {
uint8_t privateKeyData[PRIVATEKEY_LENGTH];
BEGIN_TRY {
TRY {
os_perso_derive_node_bip32_seed_key(HDW_ED25519_SLIP10,
CX_CURVE_Ed25519,
derivationPath,
pathLength,
privateKeyData,
NULL,
NULL,
0);
cx_ecfp_init_private_key(CX_CURVE_Ed25519,
privateKeyData,
PRIVATEKEY_LENGTH,
privateKey);
}
CATCH_OTHER(e) {
MEMCLEAR(privateKeyData);
THROW(e);
}
FINALLY {
MEMCLEAR(privateKeyData);
}
}
END_TRY;
}

void get_private_key_with_seed(cx_ecfp_private_key_t *privateKey,
const uint32_t *derivationPath,
uint8_t pathLength) {
uint8_t privateKeyData[PRIVATEKEY_LENGTH];
BEGIN_TRY {
TRY {
os_perso_derive_node_bip32_seed_key(HDW_ED25519_SLIP10,
CX_CURVE_Ed25519,
derivationPath,
pathLength,
privateKeyData,
NULL,
(unsigned char *) "ed25519 seed",
12);
cx_ecfp_init_private_key(CX_CURVE_Ed25519,
privateKeyData,
PRIVATEKEY_LENGTH,
privateKey);
}
CATCH_OTHER(e) {
MEMCLEAR(privateKeyData);
THROW(e);
}
FINALLY {
MEMCLEAR(privateKeyData);
}
}
END_TRY;
}

int read_derivation_path(const uint8_t *data_buffer,
size_t data_size,
uint32_t *derivation_path,
Expand Down Expand Up @@ -126,32 +66,21 @@ int read_derivation_path(const uint8_t *data_buffer,
}

uint8_t set_result_sign_message(void) {
uint8_t signature[SIGNATURE_LENGTH];
cx_ecfp_private_key_t privateKey;
BEGIN_TRY {
TRY {
get_private_key_with_seed(&privateKey,
G_command.derivation_path,
G_command.derivation_path_length);
cx_eddsa_sign(&privateKey,
CX_LAST,
CX_SHA512,
G_command.message,
G_command.message_length,
NULL,
0,
signature,
SIGNATURE_LENGTH,
NULL);
memcpy(G_io_apdu_buffer, signature, SIGNATURE_LENGTH);
}
CATCH_OTHER(e) {
THROW(e);
}
FINALLY {
MEMCLEAR(privateKey);
}
size_t sig_len = SIGNATURE_LENGTH;

if (CX_OK != bip32_derive_with_seed_eddsa_sign_hash_256(HDW_ED25519_SLIP10,
CX_CURVE_Ed25519,
G_command.derivation_path,
G_command.derivation_path_length,
CX_SHA512,
G_command.message,
G_command.message_length,
G_io_apdu_buffer,
&sig_len,
NULL,
0)) {
LEDGER_ASSERT(false, "Fail to sign");
}
END_TRY;

return SIGNATURE_LENGTH;
}
10 changes: 1 addition & 9 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ typedef enum rlpTxType {
TX_FEE
} rlpTxType;

void get_public_key(uint8_t *publicKeyArray, const uint32_t *derivationPath, size_t pathLength);
void get_public_key(uint8_t publicKeyArray[static PUBKEY_LENGTH], const uint32_t *derivationPath, size_t pathLength);

uint32_t readUint32BE(uint8_t *buffer);

void get_private_key(cx_ecfp_private_key_t *privateKey,
const uint32_t *derivationPath,
size_t pathLength);

void get_private_key_with_seed(cx_ecfp_private_key_t *privateKey,
const uint32_t *derivationPath,
uint8_t pathLength);

/**
* Deserialize derivation path from raw bytes.
*
Expand Down

0 comments on commit 5762e49

Please sign in to comment.