Skip to content

Commit 8fe6572

Browse files
ldg-github-ciapaillier-ledger
authored andcommitted
[update] Branch develop | Commit 29389646ec61264be5f1af2fb4c32210b9e92c9c
[update] Branch develop | Commit 831eee239c65d80b8e9c294bccab68489b0c9667
1 parent 0a98664 commit 8fe6572

File tree

4 files changed

+35
-75
lines changed

4 files changed

+35
-75
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
| :rotating_light: | Breaks build |
66
| :warning: | Breaks compatibility with app |
77

8-
## [latest](/) - 2024/02/07
8+
## [latest](/) - 2024/03/27
9+
10+
### Changed
11+
12+
* Add new functions `getEthAddressFromRawKey` and `getEthAddressStringFromRawKey`
13+
* Simplify crypto calls in `getEthAddressStringFromBinary`
14+
* Cleanup useless `cx_sha3_t` useless parameter
15+
16+
## [0a98664](/../../commit/0a98664) - 2024/02/07
917

1018
### Removed
1119

src/common_utils.c

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "asset_info.h"
2222
#include "common_utils.h"
23+
#include "lcx_ecfp.h"
24+
#include "lcx_sha3.h"
2325

2426
void array_hexstr(char *strbuf, const void *bin, unsigned int len) {
2527
while (len--) {
@@ -214,55 +216,23 @@ bool amountToString(const uint8_t *amount,
214216
return true;
215217
}
216218

217-
bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) {
218-
uint8_t hashAddress[INT256_LENGTH];
219-
220-
if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) {
221-
return false;
222-
}
223-
224-
if (cx_hash_no_throw((cx_hash_t *) sha3Context,
225-
CX_LAST,
226-
publicKey->W + 1,
227-
64,
228-
hashAddress,
229-
32) != CX_OK) {
230-
return false;
231-
}
232-
233-
memmove(out, hashAddress + 12, 20);
234-
return true;
219+
void getEthAddressFromRawKey(const uint8_t raw_pubkey[static 65],
220+
uint8_t out[static ADDRESS_LENGTH]) {
221+
uint8_t hashAddress[CX_KECCAK_256_SIZE];
222+
CX_ASSERT(cx_keccak_256_hash(raw_pubkey + 1, 64, hashAddress));
223+
memmove(out, hashAddress + 12, ADDRESS_LENGTH);
235224
}
236225

237-
bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
238-
char *out,
239-
cx_sha3_t *sha3Context,
240-
uint64_t chainId) {
241-
uint8_t hashAddress[INT256_LENGTH];
242-
243-
if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) {
244-
return false;
245-
}
246-
247-
if (cx_hash_no_throw((cx_hash_t *) sha3Context,
248-
CX_LAST,
249-
publicKey->W + 1,
250-
64,
251-
hashAddress,
252-
32) != CX_OK) {
253-
return false;
254-
}
255-
256-
if (!getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId)) {
257-
return false;
258-
}
259-
260-
return true;
226+
void getEthAddressStringFromRawKey(const uint8_t raw_pubkey[static 65],
227+
char out[static ADDRESS_LENGTH * 2],
228+
uint64_t chainId) {
229+
uint8_t hashAddress[CX_KECCAK_256_SIZE];
230+
CX_ASSERT(cx_keccak_256_hash(raw_pubkey + 1, 64, hashAddress));
231+
getEthAddressStringFromBinary(hashAddress + 12, out, chainId);
261232
}
262233

263234
bool getEthAddressStringFromBinary(uint8_t *address,
264-
char *out,
265-
cx_sha3_t *sha3Context,
235+
char out[static ADDRESS_LENGTH * 2],
266236
uint64_t chainId) {
267237
// save some precious stack space
268238
union locals_union {
@@ -292,18 +262,10 @@ bool getEthAddressStringFromBinary(uint8_t *address,
292262
locals_union.tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f];
293263
locals_union.tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f];
294264
}
295-
if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) {
265+
if (cx_keccak_256_hash(locals_union.tmp, offset + 40, locals_union.hashChecksum) != CX_OK) {
296266
return false;
297267
}
298268

299-
if (cx_hash_no_throw((cx_hash_t *) sha3Context,
300-
CX_LAST,
301-
locals_union.tmp,
302-
offset + 40,
303-
locals_union.hashChecksum,
304-
32) != CX_OK) {
305-
return false;
306-
}
307269
for (i = 0; i < 40; i++) {
308270
uint8_t digit = address[i / 2];
309271
if ((i % 2) == 0) {
@@ -329,20 +291,15 @@ bool getEthAddressStringFromBinary(uint8_t *address,
329291

330292
/* Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary
331293
format by `in`. (eg: uint8_t*:0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB ->
332-
char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" )
333-
`sha3` context doesn't have have to be initialized prior to call.*/
334-
bool getEthDisplayableAddress(uint8_t *in,
335-
char *out,
336-
size_t out_len,
337-
cx_sha3_t *sha3,
338-
uint64_t chainId) {
294+
char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" ).*/
295+
bool getEthDisplayableAddress(uint8_t *in, char *out, size_t out_len, uint64_t chainId) {
339296
if (out_len < 43) {
340297
strlcpy(out, "ERROR", out_len);
341298
return false;
342299
}
343300
out[0] = '0';
344301
out[1] = 'x';
345-
if (!getEthAddressStringFromBinary(in, out + 2, sha3, chainId)) {
302+
if (!getEthAddressStringFromBinary(in, out + 2, chainId)) {
346303
strlcpy(out, "ERROR", out_len);
347304
return false;
348305
}

src/common_utils.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,18 @@ bool adjustDecimals(const char *src,
5656
size_t targetLength,
5757
uint8_t decimals);
5858

59-
bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context);
59+
void getEthAddressFromRawKey(const uint8_t raw_pubkey[static 65],
60+
uint8_t out[static ADDRESS_LENGTH]);
6061

61-
bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
62-
char *out,
63-
cx_sha3_t *sha3Context,
64-
uint64_t chainId);
62+
void getEthAddressStringFromRawKey(const uint8_t raw_pubkey[static 65],
63+
char out[static ADDRESS_LENGTH * 2],
64+
uint64_t chainId);
6565

6666
bool getEthAddressStringFromBinary(uint8_t *address,
67-
char *out,
68-
cx_sha3_t *sha3Context,
67+
char out[static ADDRESS_LENGTH * 2],
6968
uint64_t chainId);
7069

71-
bool getEthDisplayableAddress(uint8_t *in,
72-
char *out,
73-
size_t out_len,
74-
cx_sha3_t *sha3,
75-
uint64_t chainId);
70+
bool getEthDisplayableAddress(uint8_t *in, char *out, size_t out_len, uint64_t chainId);
7671

7772
static __attribute__((no_instrument_function)) inline int allzeroes(const void *buf, size_t n) {
7873
uint8_t *p = (uint8_t *) buf;

src/eth_plugin_interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ typedef struct ethPluginSharedRO_s {
9090

9191
// Plugin-only memory allocated by the Ethereum application and used by the plugin.
9292
#define PLUGIN_CONTEXT_SIZE (5 * INT256_LENGTH)
93-
// It is recommended to cast the raw uin8_t array to a structure meaningfull for your plugin
93+
// It is recommended to cast the raw uin8_t array to a structure meaningful for your plugin
9494
// Helper to check that the actual plugin context structure is not bigger than the allocated memory
9595
#define ASSERT_SIZEOF_PLUGIN_CONTEXT(s) \
9696
_Static_assert(sizeof(s) <= PLUGIN_CONTEXT_SIZE, "Plugin context structure is too big.")

0 commit comments

Comments
 (0)