Skip to content

Commit

Permalink
fixed obsoletes.
Browse files Browse the repository at this point in the history
  • Loading branch information
shufps committed Jul 24, 2023
1 parent 43fd3c5 commit a85addd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 89 deletions.
7 changes: 4 additions & 3 deletions src/iota/abstraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ uint64_t get_output_amount(const API_CTX *api, uint8_t index)
return amount;
}

uint8_t address_encode_bech32(const API_CTX *api, const uint8_t *addr_with_type, char *bech32,
uint32_t bech32_max_length)
uint8_t address_encode_bech32(const API_CTX *api, const uint8_t *addr_with_type,
char *bech32, uint32_t bech32_max_length)
{
switch (api->coin) {
case COIN_IOTA: {
Expand All @@ -87,7 +87,8 @@ uint8_t address_encode_bech32(const API_CTX *api, const uint8_t *addr_with_type,
case COIN_SHIMMER: {
MUST(address_encode_bech32_hrp(
addr_with_type, bech32, bech32_max_length,
(api->app_mode & 0x80) ? COIN_HRP_SHIMMER_TESTNET : COIN_HRP_SHIMMER,
(api->app_mode & 0x80) ? COIN_HRP_SHIMMER_TESTNET
: COIN_HRP_SHIMMER,
strlen(COIN_HRP_SHIMMER))); // strlen valid because HRP has the same
// length in testnet
break;
Expand Down
4 changes: 2 additions & 2 deletions src/iota/abstraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const uint8_t *get_output_address_ptr(const API_CTX *api, uint8_t index);

uint64_t get_output_amount(const API_CTX *api, uint8_t index);

uint8_t address_encode_bech32(const API_CTX *api, const uint8_t *addr_with_type, char *bech32,
uint32_t bech32_max_length);
uint8_t address_encode_bech32(const API_CTX *api, const uint8_t *addr_with_type,
char *bech32, uint32_t bech32_max_length);

uint8_t essence_parse_and_validate(API_CTX *api);

Expand Down
15 changes: 10 additions & 5 deletions src/iota/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#pragma GCC diagnostic error "-Wextra"
#pragma GCC diagnostic error "-Wmissing-prototypes"

#include "debugprintf.h"
//#include "debugprintf.h"

uint8_t address_encode_bech32_hrp(const uint8_t *addr_with_type, char *bech32,
uint32_t bech32_max_length, const char *hrp,
Expand Down Expand Up @@ -76,9 +76,14 @@ uint8_t address_generate(uint32_t *bip32_path, uint32_t bip32_path_length,
addr[0] = ADDRESS_TYPE_ED25519;

cx_blake2b_t blake2b;
cx_blake2b_init_no_throw(&blake2b, BLAKE2B_SIZE_BYTES * 8);
cx_hash_no_throw(&blake2b.header, CX_LAST, pubkey_bytes, PUBKEY_SIZE_BYTES, &addr[1],
ADDRESS_SIZE_BYTES);

return 1;
cx_err_t err;
err = cx_blake2b_init_no_throw(&blake2b, BLAKE2B_SIZE_BYTES * 8);
if (err != CX_OK) {
return 0;
}

err = cx_hash_no_throw(&blake2b.header, CX_LAST, pubkey_bytes,
PUBKEY_SIZE_BYTES, &addr[1], ADDRESS_SIZE_BYTES);
return err == CX_OK;
}
88 changes: 31 additions & 57 deletions src/iota/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "os.h"
#include "cx.h"

#include "macros.h"
#include "ed25519.h"

#include "constants.h"
Expand All @@ -13,7 +14,6 @@
#pragma GCC diagnostic error "-Wextra"
#pragma GCC diagnostic error "-Wmissing-prototypes"


// bip-path
// 0x2c'/coin_type'/account'/change'/index'

Expand All @@ -22,40 +22,39 @@ uint8_t ed25519_get_key_pair(uint32_t *bip32_path, uint32_t bip32_path_length,
cx_ecfp_private_key_t *pk,
cx_ecfp_public_key_t *pub)
{
uint8_t keySeed[32];
uint8_t ret = 1;
uint8_t keySeed[64];

// getting the seed to derive and configuring it with SLIP10
os_perso_derive_node_bip32_seed_key(
HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32_path, bip32_path_length,
keySeed, NULL, (unsigned char *)"ed25519 seed", 12);

BEGIN_TRY
{
TRY
{
// initializing the private key and public key instance
// with selected curve ED25519
cx_ecfp_init_private_key(CX_CURVE_Ed25519, keySeed, sizeof(keySeed),
pk);
cx_ecfp_init_public_key(CX_CURVE_Ed25519, NULL, 0, pub);

// generating the key pair
cx_ecfp_generate_pair(CX_CURVE_Ed25519, pub, pk, 1);
cx_err_t err = CX_OK;
do {
err = os_derive_bip32_with_seed_no_throw(
HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32_path, bip32_path_length,
keySeed, NULL, (unsigned char *)"ed25519 seed", 12);
if (err != CX_OK) {
break;
}
CATCH_ALL
{
ret = 0;

// initializing the private key and public key instance
// with selected curve ED25519
err = cx_ecfp_init_private_key_no_throw(CX_CURVE_Ed25519, keySeed, 32,
pk);
if (err != CX_OK) {
break;
}
FINALLY
{
// resetting the variables to avoid leak
explicit_bzero(keySeed, sizeof(keySeed));

err = cx_ecfp_init_public_key_no_throw(CX_CURVE_Ed25519, NULL, 0, pub);
if (err != CX_OK) {
break;
}
}
END_TRY;

return ret;
// generating the key pair
err = cx_ecfp_generate_pair_no_throw(CX_CURVE_Ed25519, pub, pk, 1);
} while (0);

// resetting the variables to avoid leak
explicit_bzero(keySeed, sizeof(keySeed));

return err == CX_OK;
}

// reversing the public key and changing the last byte
Expand All @@ -71,33 +70,8 @@ uint8_t ed25519_public_key_to_bytes(cx_ecfp_public_key_t *pub, uint8_t *output)
}

uint8_t ed25519_sign(cx_ecfp_private_key_t *privateKey, const uint8_t *msg,
uint32_t msg_length, unsigned char *output,
uint32_t *output_length)
uint32_t msg_length, unsigned char *output)
{
uint8_t ret = 1;

BEGIN_TRY
{
TRY
{
*output_length =
cx_eddsa_sign(privateKey, 0, CX_SHA512, msg, msg_length, NULL,
0, output, CX_SHA512_SIZE, NULL);
}
CATCH_ALL
{
ret = 0;
}
FINALLY
{
}
}
END_TRY;


if (*output_length != SIGNATURE_SIZE_BYTES) {
ret = 0;
}

return ret;
return cx_eddsa_sign_no_throw(privateKey, CX_SHA512, msg, msg_length,
output, CX_SHA512_SIZE) == CX_OK;
}
3 changes: 1 addition & 2 deletions src/iota/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ uint8_t ed25519_get_key_pair(uint32_t *bip32_path, uint32_t bip32_path_length,
cx_ecfp_private_key_t *pk,
cx_ecfp_public_key_t *pub);
uint8_t ed25519_sign(cx_ecfp_private_key_t *privateKey, const uint8_t *msg,
uint32_t msg_length, unsigned char *output,
uint32_t *output_length);
uint32_t msg_length, unsigned char *output);
13 changes: 7 additions & 6 deletions src/iota/essence_chrysalis.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#pragma GCC diagnostic error "-Wextra"
#pragma GCC diagnostic error "-Wmissing-prototypes"


static inline uint8_t get_uint32(const uint8_t *data, uint32_t *idx,
uint32_t *v)
{
Expand Down Expand Up @@ -284,17 +283,19 @@ static uint8_t essence_verify_remainder_address(
return 1;
}

static void essence_hash(API_CTX *api)
static uint8_t essence_hash(API_CTX *api)
{
// Block below cannot be fuzzed without going through crypto APIs
#ifndef FUZZING
cx_blake2b_t blake2b;
cx_blake2b_init_no_throw(&blake2b, BLAKE2B_SIZE_BYTES * 8);
cx_hash_no_throw(&blake2b.header, CX_LAST, api->data.buffer, api->essence.length,
api->essence.hash, ADDRESS_SIZE_BYTES);
MUST(cx_blake2b_init_no_throw(&blake2b, BLAKE2B_SIZE_BYTES * 8) == CX_OK);
MUST(cx_hash_no_throw(&blake2b.header, CX_LAST, api->data.buffer,
api->essence.length, api->essence.hash,
ADDRESS_SIZE_BYTES) == CX_OK);
#else
(void)api;
#endif
return 1;
}

uint8_t essence_parse_and_validate_chryslis(API_CTX *api)
Expand Down Expand Up @@ -357,7 +358,7 @@ uint8_t essence_parse_and_validate_chryslis(API_CTX *api)
api->essence.outputs_count));

// everything fine - calculate the hash
essence_hash(api);
MUST(essence_hash(api));

// check if it's a sweeping transaction
if (check_for_internal_transfer(api)) {
Expand Down
12 changes: 7 additions & 5 deletions src/iota/essence_stardust.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,19 @@ static uint8_t essence_verify_remainder_address(
return 1;
}

static void essence_hash(API_CTX *api)
static uint8_t essence_hash(API_CTX *api)
{
// Block below cannot be fuzzed without going through crypto APIs
#ifndef FUZZING
cx_blake2b_t blake2b;
cx_blake2b_init_no_throw(&blake2b, BLAKE2B_SIZE_BYTES * 8);
cx_hash_no_throw(&blake2b.header, CX_LAST, api->data.buffer, api->essence.length,
api->essence.hash, ADDRESS_SIZE_BYTES);
MUST(cx_blake2b_init_no_throw(&blake2b, BLAKE2B_SIZE_BYTES * 8) == CX_OK);
MUST(cx_hash_no_throw(&blake2b.header, CX_LAST, api->data.buffer,
api->essence.length, api->essence.hash,
ADDRESS_SIZE_BYTES) == CX_OK);
#else
(void)api;
#endif
return 1;
}

uint8_t essence_parse_and_validate_stardust(API_CTX *api)
Expand Down Expand Up @@ -324,7 +326,7 @@ uint8_t essence_parse_and_validate_stardust(API_CTX *api)
api->essence.inputs_count));

// everything fine - calculate the hash
essence_hash(api);
MUST(essence_hash(api));

// check if it's a sweeping transaction
if (check_for_internal_transfer(api)) {
Expand Down
8 changes: 1 addition & 7 deletions src/iota/signing.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "iota/ed25519.h"
#endif


#pragma GCC diagnostic error "-Wall"
#pragma GCC diagnostic error "-Wextra"
#pragma GCC diagnostic error "-Wmissing-prototypes"
Expand All @@ -30,23 +29,18 @@ static uint16_t sign_signature(SIGNATURE_BLOCK *pBlock,
bip32_signing_path[BIP32_ADDRESS_INDEX] = input_bip32_index->bip32_index;
bip32_signing_path[BIP32_CHANGE_INDEX] = input_bip32_index->bip32_change;

uint32_t signature_length = 0;

uint8_t ret = 0;
// create key pair and convert pub key to bytes
ret = ed25519_get_key_pair(bip32_signing_path, BIP32_PATH_LEN, &pk, &pub);
ret = ret && ed25519_sign(&pk, essence_hash, BLAKE2B_SIZE_BYTES,
pBlock->signature, &signature_length);
pBlock->signature);

// always delete from stack
explicit_bzero(&pk, sizeof(pk));

// ed25519_get_key_pair and ed25519_sign must succeed
MUST(ret);

// length of signature must not be 0
MUST(signature_length);

MUST(ed25519_public_key_to_bytes(&pub, pBlock->public_key));

return (uint16_t)sizeof(SIGNATURE_BLOCK);
Expand Down
4 changes: 2 additions & 2 deletions src/ui/nano/flow_user_confirm_new_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ static void cb_address_preinit()

// generate bech32 address including the address_type
// we only have a single address in the buffer starting at index 0
address_encode_bech32(&api, flow_data.api->data.buffer, flow_data.scratch[0],
sizeof(flow_data.scratch[0]));
address_encode_bech32(&api, flow_data.api->data.buffer,
flow_data.scratch[0], sizeof(flow_data.scratch[0]));
}

static void cb_bip32_preinit()
Expand Down

0 comments on commit a85addd

Please sign in to comment.