Skip to content

Commit 1c91000

Browse files
Add TLV library and PKI helper
1 parent d3cf494 commit 1c91000

9 files changed

+1395
-0
lines changed

lib_standard_app/ledger_pki.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "os_types.h"
2+
#include "os_pki.h"
3+
#include "ledger_pki.h"
4+
5+
check_signature_with_pki_status_t check_signature_with_pki(const buffer_t hash,
6+
uint8_t expected_key_usage,
7+
cx_curve_t expected_curve,
8+
const buffer_t signature) {
9+
uint8_t key_usage = 0;
10+
size_t certificate_name_len = 0;
11+
uint8_t certificate_name[CERTIFICATE_TRUSTED_NAME_MAXLEN] = {0};
12+
cx_ecfp_384_public_key_t public_key = {0};
13+
bolos_err_t bolos_err;
14+
15+
bolos_err = os_pki_get_info(&key_usage, certificate_name, &certificate_name_len, &public_key);
16+
if (bolos_err != 0x0000) {
17+
PRINTF("Error %x while getting PKI certificate info\n", bolos_err);
18+
return CHECK_SIGNATURE_WITH_PKI_MISSING_CERTIFICATE;
19+
}
20+
21+
if (key_usage != expected_key_usage) {
22+
PRINTF("Wrong usage certificate %d, expected %d\n", key_usage, expected_key_usage);
23+
return CHECK_SIGNATURE_WITH_PKI_WRONG_CERTIFICATE_USAGE;
24+
}
25+
26+
if (public_key.curve != expected_curve) {
27+
PRINTF("Wrong curve %d, expected %d\n", public_key.curve, expected_curve);
28+
return CHECK_SIGNATURE_WITH_PKI_WRONG_CERTIFICATE_CURVE;
29+
}
30+
31+
PRINTF("Certificate '%s' loaded with success\n", certificate_name);
32+
33+
// Checking the signature with PKI
34+
if (!os_pki_verify((uint8_t *) hash.ptr,
35+
hash.size,
36+
(uint8_t *) signature.ptr,
37+
signature.size)) {
38+
PRINTF("Error, '%.*H' is not a signature of hash '%.*H' by the PKI key '%.*H'\n",
39+
signature.size,
40+
signature.ptr,
41+
hash.size,
42+
hash.ptr,
43+
sizeof(public_key),
44+
&public_key);
45+
return CHECK_SIGNATURE_WITH_PKI_WRONG_SIGNATURE;
46+
}
47+
48+
PRINTF("Signature verified successfully\n");
49+
return CHECK_SIGNATURE_WITH_PKI_SUCCESS;
50+
}

lib_standard_app/ledger_pki.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "os.h"
4+
#include "buffer.h"
5+
#include "cx.h"
6+
7+
#define DER_SIGNATURE_MIN_SIZE 64 // Ed25519 size
8+
#define DER_SIGNATURE_MAX_SIZE 72 // ECDSA max size
9+
10+
typedef enum check_signature_with_pki_status_e {
11+
CHECK_SIGNATURE_WITH_PKI_SUCCESS = 0,
12+
CHECK_SIGNATURE_WITH_PKI_MISSING_CERTIFICATE = 1,
13+
CHECK_SIGNATURE_WITH_PKI_WRONG_CERTIFICATE_USAGE = 2,
14+
CHECK_SIGNATURE_WITH_PKI_WRONG_CERTIFICATE_CURVE = 3,
15+
CHECK_SIGNATURE_WITH_PKI_WRONG_SIGNATURE = 4,
16+
} check_signature_with_pki_status_t;
17+
18+
check_signature_with_pki_status_t check_signature_with_pki(const buffer_t hash,
19+
uint8_t expected_key_usage,
20+
cx_curve_t expected_curve,
21+
const buffer_t signature);

lib_standard_app/tlv/tlv_internals.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include <os.h>
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
#include <stdbool.h>
7+
#include "buffer.h"
8+
9+
// ─────────────────────────────────────────────────────────────────────────────
10+
// TLV X macros
11+
// Not intended for library users
12+
// ─────────────────────────────────────────────────────────────────────────────
13+
14+
/**
15+
* @brief Internal macro — assigns the tag's enum value.
16+
* @note Do not use directly.
17+
*/
18+
#define __X_DEFINE_TLV__TAG_ASSIGN(value, name, callback, unicity) name = value,
19+
20+
/**
21+
* @brief Internal macro — creates an index enum for mapping tags to their order in the list.
22+
* @note Do not use directly.
23+
*/
24+
#define __X_DEFINE_TLV__TAG_INDEX(value, name, callback, unicity) name##_INDEX,
25+
26+
/**
27+
* @brief Internal macro — creates a flag enum for mapping tags to their reception flag.
28+
* @note Do not use directly.
29+
*/
30+
#define __X_DEFINE_TLV__TAG_FLAG(value, name, callback, unicity) name##_FLAG = (1U << name##_INDEX),
31+
32+
/**
33+
* @brief Internal macro — expands to a switch case that maps a tag to its flag.
34+
* @note Do not use directly.
35+
*/
36+
#define __X_DEFINE_TLV__TAG_TO_FLAG_CASE(value, name, callback, unicity) \
37+
case name: \
38+
return name##_FLAG;
39+
40+
/**
41+
* @brief Internal macro — expands each tag into an _internal_tlv_handler_t array element
42+
* @note Do not use directly.
43+
*/
44+
#define __X_DEFINE_TLV__TAG_CALLBACKS(value, name, callback, unicity) \
45+
{.tag = name, \
46+
.func = (tlv_handler_cb_t *) callback, \
47+
.is_unique = (unicity == ENFORCE_UNIQUE_TAG)},
48+
49+
// The generated TLV library will give the user a tag_to_flag function of the following type for
50+
// his TLV use case
51+
typedef uint32_t TLV_tag_t;
52+
typedef uint64_t TLV_flag_t;
53+
typedef TLV_flag_t(tag_to_flag_function_t)(TLV_tag_t tag);
54+
55+
// This structure is returned to the TLV parser caller and can be used in conjunction with the
56+
// associated helper functions to get the status of received tags.
57+
typedef struct {
58+
TLV_flag_t flags;
59+
tag_to_flag_function_t *tag_to_flag_function;
60+
} TLV_reception_internal_t;

0 commit comments

Comments
 (0)