Skip to content

Use SDK TLV library #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_ASSERT(cx_hash_no_throw(hash_ctx, 0, bytes_ptr, n, NULL, 0));
CX_ASSERT(cx_hash_update(hash_ctx, bytes_ptr, n));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ledger_pki.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int check_signature_with_pubkey(const char *tag,
#ifdef HAVE_LEDGER_PKI
const uint8_t keyUsageExp,
#endif
uint8_t *signature,
const uint8_t *signature,
const uint8_t sigLen) {
UNUSED(tag);
cx_ecfp_public_key_t verif_key = {0};
Expand All @@ -50,7 +50,7 @@ int check_signature_with_pubkey(const char *tag,
KEY_USAGE_STR(key_usage));

// Checking the signature with PKI
if (!os_pki_verify(buffer, bufLen, signature, sigLen)) {
if (!os_pki_verify(buffer, bufLen, (uint8_t *)signature, sigLen)) {
PRINTF("%s: Invalid signature\n", tag);
#ifndef HAVE_BYPASS_SIGNATURES
error = APDU_RESPONSE_INVALID_DATA;
Expand Down
2 changes: 1 addition & 1 deletion src/public_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ int check_signature_with_pubkey(const char *tag,
#ifdef HAVE_LEDGER_PKI
const uint8_t keyUsageExp,
#endif
uint8_t *signature,
const uint8_t *signature,
const uint8_t sigLen);
6 changes: 4 additions & 2 deletions src_features/generic_tx_parser/gtp_data_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ static bool handle_array(const s_tlv_data *data, s_data_path_context *context) {

ctx.args = &context->data_path->elements[context->data_path->size].array;
explicit_bzero(ctx.args, sizeof(*ctx.args));
if (!tlv_parse(data->value, data->length, (f_tlv_data_handler) &handle_array_struct, &ctx))
if (!handle_array_struct(data, &ctx)) {
return false;
}
context->data_path->elements[context->data_path->size].type = ELEMENT_TYPE_ARRAY;
context->data_path->size += 1;
return true;
Expand Down Expand Up @@ -78,8 +79,9 @@ static bool handle_slice(const s_tlv_data *data, s_data_path_context *context) {

ctx.args = &context->data_path->elements[context->data_path->size].slice;
explicit_bzero(ctx.args, sizeof(*ctx.args));
if (!tlv_parse(data->value, data->length, (f_tlv_data_handler) &handle_slice_struct, &ctx))
if (!handle_slice_struct(data, &ctx)) {
return false;
}
context->data_path->elements[context->data_path->size].type = ELEMENT_TYPE_SLICE;
context->data_path->size += 1;
return true;
Expand Down
47 changes: 16 additions & 31 deletions src_features/generic_tx_parser/gtp_path_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@
#include <string.h>
#include "gtp_path_array.h"
#include "read.h"
#include "tlv_library.h"
#include "buffer.h"
#include "os_print.h"
#include "utils.h"

enum {
TAG_WEIGHT = 0x01,
TAG_START = 0x02,
TAG_END = 0x03,
};

static bool handle_weight(const s_tlv_data *data, s_path_array_context *context) {
if (data->length != sizeof(context->args->weight)) {
return false;
}
context->args->weight = data->value[0];
return true;
static bool handle_weight(const tlv_data_t *data, s_path_array_context *context) {
return get_uint8_t_from_tlv_data(data, &context->args->weight);
}

static bool handle_start(const s_tlv_data *data, s_path_array_context *context) {
static bool handle_start(const tlv_data_t *data, s_path_array_context *context) {
if (data->length != sizeof(context->args->start)) {
return false;
}
Expand All @@ -30,7 +22,7 @@ static bool handle_start(const s_tlv_data *data, s_path_array_context *context)
return true;
}

static bool handle_end(const s_tlv_data *data, s_path_array_context *context) {
static bool handle_end(const tlv_data_t *data, s_path_array_context *context) {
if (data->length != sizeof(context->args->end)) {
return false;
}
Expand All @@ -39,24 +31,17 @@ static bool handle_end(const s_tlv_data *data, s_path_array_context *context) {
return true;
}

// clang-format off
#define TLV_TAGS(X) \
X(0x01, TAG_WEIGHT, handle_weight, ALLOW_MULTIPLE_TAG) \
X(0x02, TAG_START, handle_start, ENFORCE_UNIQUE_TAG) \
X(0x03, TAG_END, handle_end, ENFORCE_UNIQUE_TAG)

DEFINE_TLV_PARSER(TLV_TAGS, parse_tlv_array)

bool handle_array_struct(const s_tlv_data *data, s_path_array_context *context) {
bool ret;

switch (data->tag) {
case TAG_WEIGHT:
ret = handle_weight(data, context);
break;
case TAG_START:
ret = handle_start(data, context);
break;
case TAG_END:
ret = handle_end(data, context);
break;
default:
PRINTF(TLV_TAG_ERROR_MSG, data->tag);
ret = false;
}
return ret;
buffer_t payload_buffer = {.ptr = data->value, .size = data->length};
return parse_tlv_array(&payload_buffer, context, NULL);
}

#endif // HAVE_GENERIC_TX_PARSER
29 changes: 10 additions & 19 deletions src_features/generic_tx_parser/gtp_path_slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

#include "gtp_path_slice.h"
#include "os_print.h"
#include "tlv_library.h"
#include "read.h"

enum {
TAG_START = 0x01,
TAG_END = 0x02,
};

static bool handle_start(const s_tlv_data *data, s_path_slice_context *context) {
if (data->length != sizeof(context->args->start)) {
return false;
Expand All @@ -27,21 +23,16 @@ static bool handle_end(const s_tlv_data *data, s_path_slice_context *context) {
return true;
}

bool handle_slice_struct(const s_tlv_data *data, s_path_slice_context *context) {
bool ret;
// clang-format off
#define TLV_TAGS(X) \
X(0x01, TAG_START, handle_start, ENFORCE_UNIQUE_TAG) \
X(0x02, TAG_END, handle_end, ENFORCE_UNIQUE_TAG)

switch (data->tag) {
case TAG_START:
ret = handle_start(data, context);
break;
case TAG_END:
ret = handle_end(data, context);
break;
default:
PRINTF(TLV_TAG_ERROR_MSG, data->tag);
ret = false;
}
return ret;
DEFINE_TLV_PARSER(TLV_TAGS, parse_tlv_slice)

bool handle_slice_struct(const s_tlv_data *data, s_path_slice_context *context) {
buffer_t payload_buffer = {.ptr = data->value, .size = data->length};
return parse_tlv_slice(&payload_buffer, context, NULL);
}

#endif // HAVE_GENERIC_TX_PARSER
18 changes: 1 addition & 17 deletions src_features/provide_trusted_name/cmd_trusted_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@
#include "tlv_apdu.h"
#include "apdu_constants.h"

static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_free) {
s_trusted_name_ctx ctx = {0};
bool parsing_ret;

ctx.trusted_name.name = g_trusted_name;
cx_sha256_init(&ctx.hash_ctx);
parsing_ret = tlv_parse(payload, size, (f_tlv_data_handler) &handle_trusted_name_struct, &ctx);
if (to_free) mem_dealloc(size);
if (!parsing_ret || !verify_trusted_name_struct(&ctx)) {
roll_challenge(); // prevent brute-force guesses
return false;
}
roll_challenge(); // prevent replays
return true;
}

/**
* Handle trusted name APDU
*
Expand All @@ -32,7 +16,7 @@ static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_fr
* @param[in] length payload size
*/
uint16_t handle_trusted_name(uint8_t p1, const uint8_t *data, uint8_t length) {
if (!tlv_from_apdu(p1 == P1_FIRST_CHUNK, length, data, &handle_tlv_payload)) {
if (!tlv_from_apdu(p1 == P1_FIRST_CHUNK, length, data, &handle_tlv_trusted_name_payload)) {
return APDU_RESPONSE_INVALID_DATA;
}
return APDU_RESPONSE_OK;
Expand Down
Loading
Loading