Skip to content

Commit c4b2df3

Browse files
EIP-712 trusted name filtering source check + small refactoring
1 parent c50649f commit c4b2df3

File tree

4 files changed

+41
-56
lines changed

4 files changed

+41
-56
lines changed

src_features/signMessageEIP712/filtering.c

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ bool filtering_trusted_name(const uint8_t *payload,
342342
uint32_t *path_crc) {
343343
uint8_t name_len;
344344
const char *name;
345-
uint8_t types_count;
345+
uint8_t type_count;
346346
e_name_type *types;
347-
uint8_t sources_count;
347+
uint8_t source_count;
348348
e_name_source *sources;
349349
uint8_t sig_len;
350350
const uint8_t *sig;
@@ -365,48 +365,30 @@ bool filtering_trusted_name(const uint8_t *payload,
365365
}
366366
name = (char *) &payload[offset];
367367
offset += name_len;
368-
if ((offset + sizeof(types_count)) > length) {
368+
if ((offset + sizeof(type_count)) > length) {
369369
return false;
370370
}
371-
types_count = payload[offset++];
372-
if ((offset + types_count) > length) {
371+
type_count = payload[offset++];
372+
if (type_count > TN_TYPE_COUNT) {
373+
return false;
374+
}
375+
if ((offset + type_count) > length) {
373376
return false;
374377
}
375378
types = (e_name_type *) &payload[offset];
376-
// sanity check
377-
for (int i = 0; i < types_count; ++i) {
378-
switch (types[i]) {
379-
case TN_TYPE_ACCOUNT:
380-
case TN_TYPE_CONTRACT:
381-
break;
382-
default:
383-
return false;
384-
}
379+
offset += type_count;
380+
if ((offset + sizeof(source_count)) > length) {
381+
return false;
385382
}
386-
offset += types_count;
387-
if ((offset + sizeof(sources_count)) > length) {
383+
source_count = payload[offset++];
384+
if (source_count > TN_SOURCE_COUNT) {
388385
return false;
389386
}
390-
sources_count = payload[offset++];
391-
if ((offset + sources_count) > length) {
387+
if ((offset + source_count) > length) {
392388
return false;
393389
}
394390
sources = (e_name_source *) &payload[offset];
395-
// sanity check
396-
for (int i = 0; i < sources_count; ++i) {
397-
switch (sources[i]) {
398-
case TN_SOURCE_LAB:
399-
case TN_SOURCE_CAL:
400-
case TN_SOURCE_ENS:
401-
case TN_SOURCE_UD:
402-
case TN_SOURCE_FN:
403-
case TN_SOURCE_DNS:
404-
break;
405-
default:
406-
return false;
407-
}
408-
}
409-
offset += sources_count;
391+
offset += source_count;
410392
//
411393
if ((offset + sizeof(sig_len)) > length) {
412394
return false;
@@ -424,8 +406,8 @@ bool filtering_trusted_name(const uint8_t *payload,
424406
}
425407
hash_filtering_path((cx_hash_t *) &hash_ctx, discarded, path_crc);
426408
hash_nbytes((uint8_t *) name, sizeof(char) * name_len, (cx_hash_t *) &hash_ctx);
427-
hash_nbytes(types, types_count, (cx_hash_t *) &hash_ctx);
428-
hash_nbytes(sources, sources_count, (cx_hash_t *) &hash_ctx);
409+
hash_nbytes(types, type_count, (cx_hash_t *) &hash_ctx);
410+
hash_nbytes(sources, source_count, (cx_hash_t *) &hash_ctx);
429411
if (!sig_verif_end(&hash_ctx, sig, sig_len)) {
430412
return false;
431413
}
@@ -438,7 +420,7 @@ bool filtering_trusted_name(const uint8_t *payload,
438420
ui_712_set_title(name, name_len);
439421
}
440422
ui_712_flag_field(true, name_len > 0, false, false, true);
441-
ui_712_set_trusted_name_requirements(types_count, types);
423+
ui_712_set_trusted_name_requirements(type_count, types, source_count, sources);
442424
return true;
443425
}
444426
#endif // HAVE_TRUSTED_NAME

src_features/signMessageEIP712/ui_logic.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ typedef struct {
6565
uint8_t discarded_path_length;
6666
char discarded_path[255];
6767
#ifdef HAVE_TRUSTED_NAME
68-
e_name_type name_types;
68+
uint8_t tn_type_count;
69+
uint8_t tn_source_count;
70+
e_name_type tn_types[TN_TYPE_COUNT];
71+
e_name_source tn_sources[TN_SOURCE_COUNT];
6972
#endif
7073
#ifdef SCREEN_SIZE_WALLET
7174
char ui_pairs_buffer[(SHARED_CTX_FIELD_1_SIZE + SHARED_CTX_FIELD_2_SIZE) * 2];
@@ -530,20 +533,15 @@ static bool update_amount_join(const uint8_t *data, uint8_t length) {
530533
* @return whether it was successful or not
531534
*/
532535
static bool ui_712_format_trusted_name(const uint8_t *data, uint8_t length) {
533-
uint8_t types_count = 0;
534-
e_name_type types[8];
535-
uint8_t types_bak = ui_ctx->name_types;
536-
537536
if (length != ADDRESS_LENGTH) {
538537
return false;
539538
}
540-
for (int i = 0; types_bak > 0; ++i) {
541-
if (types_bak & 1) {
542-
types[types_count++] = i;
543-
}
544-
types_bak >>= 1;
545-
}
546-
if (get_trusted_name(types_count, types, 0, NULL, &eip712_context->chain_id, data) != NULL) {
539+
if (get_trusted_name(ui_ctx->tn_type_count,
540+
ui_ctx->tn_types,
541+
ui_ctx->tn_source_count,
542+
ui_ctx->tn_sources,
543+
&eip712_context->chain_id,
544+
data) != NULL) {
547545
strlcpy(strings.tmp.tmp, g_trusted_name, sizeof(strings.tmp.tmp));
548546
}
549547
return true;
@@ -924,12 +922,14 @@ const char *ui_712_get_discarded_path(uint8_t *length) {
924922
}
925923

926924
#ifdef HAVE_TRUSTED_NAME
927-
void ui_712_set_trusted_name_requirements(uint8_t types_count, const e_name_type *types) {
928-
// pack into one byte to save on space
929-
ui_ctx->name_types = 0;
930-
for (int i = 0; i < types_count; ++i) {
931-
ui_ctx->name_types |= (1 << types[i]);
932-
}
925+
void ui_712_set_trusted_name_requirements(uint8_t type_count,
926+
const e_name_type *types,
927+
uint8_t source_count,
928+
const e_name_source *sources) {
929+
ui_ctx->tn_type_count = type_count;
930+
memcpy(ui_ctx->tn_types, types, type_count);
931+
ui_ctx->tn_source_count = source_count;
932+
memcpy(ui_ctx->tn_sources, sources, source_count);
933933
}
934934
#endif
935935

src_features/signMessageEIP712/ui_logic.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ bool ui_712_push_new_filter_path(uint32_t path_crc);
5252
void ui_712_set_discarded_path(const char *path, uint8_t length);
5353
const char *ui_712_get_discarded_path(uint8_t *length);
5454
#ifdef HAVE_TRUSTED_NAME
55-
void ui_712_set_trusted_name_requirements(uint8_t types_count, const e_name_type *types);
55+
void ui_712_set_trusted_name_requirements(uint8_t type_count,
56+
const e_name_type *types,
57+
uint8_t source_count,
58+
const e_name_source *sources);
5659
#endif
5760
#ifdef SCREEN_SIZE_WALLET
5861
char *get_ui_pairs_buffer(size_t *size);

tests/ragger/test_eip712.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ def test_eip712_advanced_trusted_name(firmware: Firmware,
763763
"type": "trusted_name",
764764
"name": "Validator",
765765
"tn_type": filt_tn_types,
766-
"tn_source": [TrustedNameSource.CAL],
766+
"tn_source": [TrustedNameSource.CAL, TrustedNameSource.ENS],
767767
},
768768
"enable": {
769769
"type": "raw",

0 commit comments

Comments
 (0)