Skip to content

Commit

Permalink
Use CX_CHECK macro in compare_recovery_phrase()
Browse files Browse the repository at this point in the history
  • Loading branch information
aido committed Dec 9, 2023
1 parent a681e47 commit 77b29fc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# Change log

## [1.5.5] - 2023-12-10
### Added
- Use CX_CHECK macro in compare_recovery_phrase()

### Changed
-

### Fixed
-

## [1.5.4] - 2023-11-30
### Added
- Added mandatory Ledger embedded application manifest file

### Changed
- Combined BIP39 wordlist and SSKR wordlist unit tests
- Combined BIP39 wordlist and SSKR wordlist unit tests

### Fixed
-
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ all: default
APPNAME = "Seed Tool"
APPVERSION_M = 1
APPVERSION_N = 5
APPVERSION_P = 4
APPVERSION_P = 5
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

APP_LOAD_PARAMS = --appFlags 0x10 $(COMMON_LOAD_PARAMS) --curve secp256k1 --path ""
Expand Down
36 changes: 20 additions & 16 deletions src/nano/nanos_enter_phrase.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,11 @@ void compare_recovery_phrase(void) {
G_bolos_ux_context.processing = PROCESSING_COMPLETE;
io_seproxyhal_general_status();

// convert mnemonic to hex-seed
cx_err_t error = CX_OK; // By default, until some error occurs
uint8_t buffer[64] = {0};
uint8_t buffer_device[64] = {0};

// convert mnemonic to hex-seed
if (G_bolos_ux_context.onboarding_type == ONBOARDING_TYPE_BIP39) {
bolos_ux_bip39_mnemonic_to_seed((unsigned char*) G_bolos_ux_context.words_buffer,
G_bolos_ux_context.words_buffer_length,
Expand All @@ -432,32 +435,33 @@ void compare_recovery_phrase(void) {
cx_hmac_sha512_t ctx;
const char key[] = "Bitcoin seed";

cx_hmac_sha512_init_no_throw(&ctx, (const uint8_t*) key, strlen(key));
cx_hmac_no_throw((cx_hmac_t*) &ctx, CX_LAST, buffer, 64, buffer, 64);
CX_CHECK(cx_hmac_sha512_init_no_throw(&ctx, (const uint8_t*) key, strlen(key)));
CX_CHECK(cx_hmac_no_throw((cx_hmac_t*) &ctx, CX_LAST, buffer, 64, buffer, 64));
PRINTF("Root key from input:\n%.*H\n", 64, buffer);

// get rootkey from device's seed
uint8_t buffer_device[64];

// os_derive_bip32* do not accept NULL path, even with a size of 0, so we provide an empty path
const unsigned int empty_path = 0;

if (os_derive_bip32_no_throw(CX_CURVE_256K1,
&empty_path,
0,
buffer_device,
buffer_device + 32) != CX_OK) {
PRINTF("An error occurred while comparing the recovery phrase\n");
return;
}
CX_CHECK(os_derive_bip32_no_throw(CX_CURVE_256K1,
&empty_path,
0,
buffer_device,
buffer_device + 32));
PRINTF("Root key from device: \n%.*H\n", 64, buffer_device);

bool memcmp_ret = (os_secure_memcmp(buffer, buffer_device, 64) == 0) ? 0 : 1;
// compare both rootkey
CX_CHECK(os_secure_memcmp(buffer, buffer_device, 64));

end:
memzero(buffer, 64);
memzero(buffer_device, 64);

// compare both rootkey
if (memcmp_ret) {
if ((error == CX_INVALID_PARAMETER) || (error == CX_INTERNAL_ERROR)) {
PRINTF("ERROR: compare_recovery_phrase(): %d\n", error);
}

if (error != CX_OK) {
(G_bolos_ux_context.onboarding_type == ONBOARDING_TYPE_BIP39)
? ux_flow_init(0, ux_bip39_nomatch_flow, NULL)
: ux_flow_init(0, ux_sskr_nomatch_flow, NULL);
Expand Down
39 changes: 24 additions & 15 deletions src/nano/nanox_enter_phrase.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,11 @@ const bagl_element_t* screen_onboarding_restore_word_before_element_display_call
}

static uint8_t compare_recovery_phrase(void) {
// convert mnemonic to hex-seed
cx_err_t error = CX_OK; // By default, until some error occurs
uint8_t buffer[64] = {0};
uint8_t buffer_device[64] = {0};

// convert mnemonic to hex-seed
if (G_bolos_ux_context.onboarding_type == ONBOARDING_TYPE_BIP39) {
bolos_ux_bip39_mnemonic_to_seed((unsigned char*) G_bolos_ux_context.words_buffer,
G_bolos_ux_context.words_buffer_length,
Expand All @@ -480,31 +483,37 @@ static uint8_t compare_recovery_phrase(void) {
cx_hmac_sha512_t ctx;
const char key[] = "Bitcoin seed";

cx_hmac_sha512_init_no_throw(&ctx, (const uint8_t*) key, strlen(key));
cx_hmac_no_throw((cx_hmac_t*) &ctx, CX_LAST, buffer, 64, buffer, 64);
CX_CHECK(cx_hmac_sha512_init_no_throw(&ctx, (const uint8_t*) key, strlen(key)));
CX_CHECK(cx_hmac_no_throw((cx_hmac_t*) &ctx, CX_LAST, buffer, 64, buffer, 64));
PRINTF("Root key from BIP39 input:\n%.*H\n", 64, buffer);

// get rootkey from device's seed
uint8_t buffer_device[64];

// os_derive_bip32* do not accept NULL path, even with a size of 0, so we provide an empty path
const unsigned int empty_path = 0;

if (os_derive_bip32_no_throw(CX_CURVE_256K1,
&empty_path,
0,
buffer_device,
buffer_device + 32) != CX_OK) {
PRINTF("An error occurred while comparing the recovery phrase\n");
return 0;
}
CX_CHECK(os_derive_bip32_no_throw(CX_CURVE_256K1,
&empty_path,
0,
buffer_device,
buffer_device + 32));
PRINTF("Root key from device: \n%.*H\n", 64, buffer_device);

// compare both rootkey
uint8_t ret = (os_secure_memcmp(buffer, buffer_device, 64) != 0) ? 0 : 1;
CX_CHECK(os_secure_memcmp(buffer, buffer_device, 64));

end:
memzero(buffer, 64);
memzero(buffer_device, 64);
return ret;

if ((error == CX_INVALID_PARAMETER) || (error == CX_INTERNAL_ERROR)) {
PRINTF("ERROR: compare_recovery_phrase(): %d\n", error);
}

if (error != CX_OK) {
return 0;
} else {
return 1;
}
}

void screen_onboarding_restore_word_validate(void) {
Expand Down

0 comments on commit 77b29fc

Please sign in to comment.