Skip to content

Commit

Permalink
Fix deprecated warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cedelavergne-ledger committed Mar 18, 2024
1 parent 1ac2d2b commit b817997
Show file tree
Hide file tree
Showing 27 changed files with 316 additions and 396 deletions.
7 changes: 4 additions & 3 deletions src/ethUstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void initTx(txContext_t *context,
context->customProcessor = customProcessor;
context->extra = extra;
context->currentField = RLP_NONE + 1;
cx_keccak_init(context->sha3, 256);
CX_ASSERT(cx_keccak_init_no_throw(context->sha3, 256));
}

uint8_t readTxByte(txContext_t *context) {
Expand All @@ -52,7 +52,7 @@ uint8_t readTxByte(txContext_t *context) {
context->currentFieldPos++;
}
if (!(context->processingField && context->fieldSingleByte)) {
cx_hash((cx_hash_t *) context->sha3, 0, &data, 1, NULL, 0);
CX_ASSERT(cx_hash_no_throw((cx_hash_t *) context->sha3, 0, &data, 1, NULL, 0));
}
return data;
}
Expand All @@ -66,7 +66,8 @@ void copyTxData(txContext_t *context, uint8_t *out, uint32_t length) {
memmove(out, context->workBuffer, length);
}
if (!(context->processingField && context->fieldSingleByte)) {
cx_hash((cx_hash_t *) context->sha3, 0, context->workBuffer, length, NULL, 0);
CX_ASSERT(
cx_hash_no_throw((cx_hash_t *) context->sha3, 0, context->workBuffer, length, NULL, 0));
}
context->workBuffer += length;
context->commandLength -= length;
Expand Down
70 changes: 20 additions & 50 deletions src/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "os.h"
#include "shared_context.h"
#include "string.h"
#include "lib_standard_app/crypto_helpers.h"

#define ZERO(x) explicit_bzero(&x, sizeof(x))

Expand All @@ -18,76 +19,45 @@ void handle_check_address(check_address_parameters_t* params, chain_config_t* ch
const uint8_t* bip32_path_ptr = params->address_parameters;
uint8_t bip32PathLength = *(bip32_path_ptr++);
cx_sha3_t local_sha3;

// Common memory is used for locals that are not used concurrently
union group1 {
uint32_t bip32Path[MAX_BIP32_PATH];
cx_ecfp_private_key_t privateKey;
char address[51];
} locals_union1;
union group2 {
uint8_t privateKeyData[64];
cx_ecfp_public_key_t publicKey;
} locals_union2;
uint32_t bip32Path[MAX_BIP32_PATH];
char address[51];
cx_ecfp_256_private_key_t priv;
cx_ecfp_256_public_key_t pub;

if ((bip32PathLength < 0x01) || (bip32PathLength > MAX_BIP32_PATH) ||
(bip32PathLength * 4 != params->address_parameters_length - 1)) {
PRINTF("Invalid path\n");
return;
}
for (uint8_t i = 0; i < bip32PathLength; i++) {
locals_union1.bip32Path[i] = U4BE(bip32_path_ptr, 0);
bip32Path[i] = U4BE(bip32_path_ptr, 0);
bip32_path_ptr += 4;
}
if (os_derive_bip32_no_throw(CX_CURVE_256K1,
locals_union1.bip32Path,
bip32PathLength,
locals_union2.privateKeyData,
NULL) != CX_OK) {
ZERO(locals_union1);
ZERO(locals_union2);
return;
}

ZERO(locals_union1);
if (cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1,
locals_union2.privateKeyData,
32,
&locals_union1.privateKey) != CX_OK) {
ZERO(locals_union1);
ZERO(locals_union2);
return;
}
ZERO(locals_union2);
if (cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1,
&locals_union2.publicKey,
&locals_union1.privateKey,
1) != CX_OK) {
ZERO(locals_union1);
ZERO(locals_union2);
return;
}
ZERO(locals_union1);
if (!getEthAddressStringFromKey(&locals_union2.publicKey,
locals_union1.address,
&local_sha3,
chain_config->chainId)) {
ZERO(locals_union1);
ZERO(locals_union2);
CX_ASSERT(bip32_derive_with_seed_init_privkey_256(HDW_NORMAL,
CX_CURVE_256K1,
bip32Path,
bip32PathLength,
&priv,
NULL,
NULL,
0));
if (cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &pub, &priv, 1) != CX_OK) {
return;
}
ZERO(locals_union2);
getEthAddressStringFromRawKey((const uint8_t*) &pub.W,
address,
&local_sha3,
chain_config->chainId);

uint8_t offset_0x = 0;
if (memcmp(params->address_to_check, "0x", 2) == 0) {
offset_0x = 2;
}

if (strcmp(locals_union1.address, params->address_to_check + offset_0x) != 0) {
if (strcmp(address, params->address_to_check + offset_0x) != 0) {
PRINTF("Addresses don't match\n");
} else {
PRINTF("Addresses match\n");
params->result = 1;
}
ZERO(locals_union1);
}
2 changes: 2 additions & 0 deletions src/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* conf
BLE_power(1, NULL);
#endif // HAVE_BLE
app_main();
// Failsafe
os_sched_exit(-1);
}
2 changes: 1 addition & 1 deletion src/hash_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param[in] hash_ctx pointer to the hashing context
*/
void hash_nbytes(const uint8_t *bytes_ptr, size_t n, cx_hash_t *hash_ctx) {
cx_hash(hash_ctx, 0, bytes_ptr, n, NULL, 0);
CX_ASSERT(cx_hash_no_throw(hash_ctx, 0, bytes_ptr, n, NULL, 0));
}

/**
Expand Down
40 changes: 11 additions & 29 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "commands_712.h"
#include "challenge.h"
#include "domain_name.h"
#include "lib_standard_app/crypto_helpers.h"

unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B];

Expand Down Expand Up @@ -88,28 +89,6 @@ void io_seproxyhal_send_status(uint32_t sw) {
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}

void format_signature_out(const uint8_t *signature) {
memset(G_io_apdu_buffer + 1, 0x00, 64);
uint8_t offset = 1;
uint8_t xoffset = 4; // point to r value
// copy r
uint8_t xlength = signature[xoffset - 1];
if (xlength == 33) {
xlength = 32;
xoffset++;
}
memmove(G_io_apdu_buffer + offset + 32 - xlength, signature + xoffset, xlength);
offset += 32;
xoffset += xlength + 2; // move over rvalue and TagLEn
// copy s value
xlength = signature[xoffset - 1];
if (xlength == 33) {
xlength = 32;
xoffset++;
}
memmove(G_io_apdu_buffer + offset + 32 - xlength, signature + xoffset, xlength);
}

unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) {
switch (channel & ~(IO_FLAGS)) {
case CHANNEL_KEYBOARD:
Expand Down Expand Up @@ -162,11 +141,17 @@ void handleGetWalletId(volatile unsigned int *tx) {
unsigned char t[64];
cx_ecfp_256_private_key_t priv;
cx_ecfp_256_public_key_t pub;
// seed => priv key
os_perso_derive_node_bip32(CX_CURVE_256K1, U_os_perso_seed_cookie, 2, t, NULL);
// seed => pubkey
CX_ASSERT(bip32_derive_with_seed_init_privkey_256(HDW_NORMAL,
CX_CURVE_256K1,
U_os_perso_seed_cookie,
2,
&priv,
NULL,
NULL,
0));
// priv key => pubkey
cx_ecdsa_init_private_key(CX_CURVE_256K1, t, 32, &priv);
cx_ecfp_generate_pair(CX_CURVE_256K1, &pub, &priv, 1);
CX_ASSERT(cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &pub, &priv, 1));
// pubkey -> sha512
cx_hash_sha512(pub.W, sizeof(pub.W), t, sizeof(t));
// ! cookie !
Expand Down Expand Up @@ -508,9 +493,6 @@ void app_main(void) {
}
END_TRY;
}

// return_to_dashboard:
return;
}

// override point, but nothing more to do
Expand Down
1 change: 0 additions & 1 deletion src/ui_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ unsigned int io_seproxyhal_touch_privacy_cancel(const bagl_element_t *e);
void ui_warning_contract_data(void);

void io_seproxyhal_send_status(uint32_t sw);
void format_signature_out(const uint8_t *signature);
void finalizeParsing(bool direct);
extraInfo_t *getKnownToken(uint8_t *contractAddress);

Expand Down
2 changes: 1 addition & 1 deletion src/uint256.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void mul256(const uint256_t *const number1,
write_u64_be(num1 + i * sizeof(uint64_t), number1->elements[i / 2].elements[i % 2]);
write_u64_be(num2 + i * sizeof(uint64_t), number2->elements[i / 2].elements[i % 2]);
}
cx_math_mult(result, num1, num2, sizeof(num1));
CX_ASSERT(cx_math_mult_no_throw(result, num1, num2, sizeof(num1)));
for (uint8_t i = 0; i < 4; i++) {
read_u64_be(result + 32 + i * sizeof(uint64_t), &target->elements[i / 2].elements[i % 2]);
}
Expand Down
53 changes: 14 additions & 39 deletions src_common/common_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,50 +214,25 @@ bool amountToString(const uint8_t *amount,
return true;
}

bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) {
void getEthAddressFromRawKey(const uint8_t raw_pubkey[static 65],
uint8_t *out,
cx_sha3_t *sha3Context) {
uint8_t hashAddress[INT256_LENGTH];

if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) {
return false;
}

if (cx_hash_no_throw((cx_hash_t *) sha3Context,
CX_LAST,
publicKey->W + 1,
64,
hashAddress,
32) != CX_OK) {
return false;
}

CX_ASSERT(cx_keccak_init_no_throw(sha3Context, 256));
CX_ASSERT(
cx_hash_no_throw((cx_hash_t *) sha3Context, CX_LAST, raw_pubkey + 1, 64, hashAddress, 32));
memmove(out, hashAddress + 12, 20);
return true;
}

bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId) {
void getEthAddressStringFromRawKey(const uint8_t raw_pubkey[static 65],
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId) {
uint8_t hashAddress[INT256_LENGTH];

if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) {
return false;
}

if (cx_hash_no_throw((cx_hash_t *) sha3Context,
CX_LAST,
publicKey->W + 1,
64,
hashAddress,
32) != CX_OK) {
return false;
}

if (!getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId)) {
return false;
}

return true;
CX_ASSERT(cx_keccak_init_no_throw(sha3Context, 256));
CX_ASSERT(
cx_hash_no_throw((cx_hash_t *) sha3Context, CX_LAST, raw_pubkey + 1, 64, hashAddress, 32));
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId);
}

bool getEthAddressStringFromBinary(uint8_t *address,
Expand Down
16 changes: 11 additions & 5 deletions src_common/common_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ static const char HEXDIGITS[] = "0123456789abcdef";

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

extern size_t strlcpy(char *dst, const char *src, size_t size);

extern size_t strlcat(char *dst, const char *src, size_t size);

void array_hexstr(char *strbuf, const void *bin, unsigned int len);

uint64_t u64_from_BE(const uint8_t *in, uint8_t size);
Expand All @@ -56,12 +60,14 @@ bool adjustDecimals(const char *src,
size_t targetLength,
uint8_t decimals);

bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context);
void getEthAddressFromRawKey(const uint8_t raw_pubkey[static 65],
uint8_t *out,
cx_sha3_t *sha3Context);

bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId);
void getEthAddressStringFromRawKey(const uint8_t raw_pubkey[static 65],
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId);

bool getEthAddressStringFromBinary(uint8_t *address,
char *out,
Expand Down
26 changes: 17 additions & 9 deletions src_features/getEth2PublicKey/cmd_getEth2PublicKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,35 @@ static const uint8_t BLS12_381_FIELD_MODULUS[] = {
0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab};

void getEth2PublicKey(uint32_t *bip32Path, uint8_t bip32PathLength, uint8_t *out) {
uint8_t privateKeyData[INT256_LENGTH];
uint8_t privateKeyData[64];
cx_ecfp_256_extended_private_key_t privateKey;
cx_ecfp_384_public_key_t publicKey;
uint8_t yFlag = 0;
uint8_t tmp[96];
int diff;

io_seproxyhal_io_heartbeat();
os_perso_derive_eip2333(CX_CURVE_BLS12_381_G1, bip32Path, bip32PathLength, privateKeyData);
CX_ASSERT(os_derive_eip2333_no_throw(CX_CURVE_BLS12_381_G1,
bip32Path,
bip32PathLength,
privateKeyData));
io_seproxyhal_io_heartbeat();
memset(tmp, 0, 48);
memmove(tmp + 16, privateKeyData, 32);
cx_ecfp_init_private_key(CX_CURVE_BLS12_381_G1, tmp, 48, (cx_ecfp_private_key_t *) &privateKey);
cx_ecfp_generate_pair(CX_CURVE_BLS12_381_G1,
(cx_ecfp_public_key_t *) &publicKey,
(cx_ecfp_private_key_t *) &privateKey,
1);
CX_ASSERT(cx_ecfp_init_private_key_no_throw(CX_CURVE_BLS12_381_G1,
tmp,
48,
(cx_ecfp_private_key_t *) &privateKey));
CX_ASSERT(cx_ecfp_generate_pair_no_throw(CX_CURVE_BLS12_381_G1,
(cx_ecfp_public_key_t *) &publicKey,
(cx_ecfp_private_key_t *) &privateKey,
1));
explicit_bzero(tmp, 96);
explicit_bzero((void *) &privateKey, sizeof(cx_ecfp_256_extended_private_key_t));
tmp[47] = 2;
cx_math_mult(tmp, publicKey.W + 1 + 48, tmp, 48);
if (cx_math_cmp(tmp + 48, BLS12_381_FIELD_MODULUS, 48) > 0) {
CX_ASSERT(cx_math_mult_no_throw(tmp, publicKey.W + 1 + 48, tmp, 48));
CX_ASSERT(cx_math_cmp_no_throw(tmp + 48, BLS12_381_FIELD_MODULUS, 48, &diff));
if (diff > 0) {
yFlag = 0x20;
}
publicKey.W[1] &= 0x1f;
Expand Down
Loading

0 comments on commit b817997

Please sign in to comment.