37#include "os_address_book.h"
40#ifdef HAVE_ADDRESS_BOOK
49#define HMAC_MSG_MAX_SIZE \
50 (GID_SIZE + 1U + (SCOPE_LENGTH - 1U) + 1U + IDENTIFIER_MAX_LENGTH + 1U + 8U)
53#define HMAC_PROOF_MSG_SIZE (GID_SIZE + 1U + (CONTACT_NAME_LENGTH - 1U))
59#define HMAC_LEDGER_MSG_SIZE \
60 (1U + (ACCOUNT_NAME_LENGTH - 1U) + 1U + (MAX_BIP32_PATH * 4U) + 1U + 8U)
102 memmove(&msg[offset], gid,
GID_SIZE);
108 uint8_t len = (uint8_t) strlen(str1);
109 if (offset + 1U + (
size_t) len > msg_size) {
114 memmove(&msg[offset], str1, len);
121 if (offset + 1U + (
size_t) raw_len > msg_size) {
124 msg[offset++] = raw_len;
125 memmove(&msg[offset], raw, raw_len);
130 if (include_family) {
131 if (offset + 1U > msg_size) {
134 msg[offset++] = (uint8_t) family;
136 if (offset + 8U > msg_size) {
139 U8BE_ENCODE(msg, offset, chain_id);
163 G_io_tx_buffer[tx++] = type;
164 memmove(&G_io_tx_buffer[tx], hmac_proof, CX_SHA256_SIZE);
165 tx += CX_SHA256_SIZE;
183 const uint8_t hmac_proof[CX_SHA256_SIZE],
184 const uint8_t hmac_rest[CX_SHA256_SIZE])
191 memmove(&G_io_tx_buffer[tx], hmac_proof, CX_SHA256_SIZE);
192 tx += CX_SHA256_SIZE;
193 memmove(&G_io_tx_buffer[tx], hmac_rest, CX_SHA256_SIZE);
194 tx += CX_SHA256_SIZE;
211 bool success =
false;
212 uint8_t *gid = group_handle;
213 uint8_t *mac = group_handle +
GID_SIZE;
216 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_GROUP, gid,
GID_SIZE, mac)) {
238 bool success =
false;
239 const uint8_t *gid = group_handle;
240 const uint8_t *mac = group_handle +
GID_SIZE;
242 if (!sys_address_book_hmac_verify(ADDRESS_BOOK_SALT_GROUP, gid,
GID_SIZE, mac)) {
243 PRINTF(
"[Address Book] Group handle MAC verification failed\n");
266 uint8_t hmac_out[CX_SHA256_SIZE])
269 bool success =
false;
270 size_t msg_len =
serialize_hmac_msg(msg,
sizeof(msg), gid, name, NULL, 0,
false, 0, 0);
272 if (msg_len == SIZE_MAX) {
275 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_out)) {
281 explicit_bzero(msg,
sizeof(msg));
295 const uint8_t hmac_expected[CX_SHA256_SIZE])
298 bool success =
false;
299 size_t msg_len =
serialize_hmac_msg(msg,
sizeof(msg), gid, name, NULL, 0,
false, 0, 0);
301 if (msg_len == SIZE_MAX) {
304 if (!sys_address_book_hmac_verify(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_expected)) {
305 PRINTF(
"HMAC_PROOF mismatch\n");
311 explicit_bzero(msg,
sizeof(msg));
333 const uint8_t *identifier,
334 uint8_t identifier_len,
337 uint8_t hmac_out[CX_SHA256_SIZE])
340 bool success =
false;
342 msg,
sizeof(msg), gid, scope, identifier, identifier_len,
true, family, chain_id);
344 if (msg_len == SIZE_MAX) {
347 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_out)) {
353 explicit_bzero(msg,
sizeof(msg));
371 const uint8_t *identifier,
372 uint8_t identifier_len,
375 const uint8_t hmac_expected[CX_SHA256_SIZE])
378 bool success =
false;
380 msg,
sizeof(msg), gid, scope, identifier, identifier_len,
true, family, chain_id);
382 if (msg_len == SIZE_MAX) {
385 if (!sys_address_book_hmac_verify(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_expected)) {
386 PRINTF(
"HMAC_REST mismatch\n");
392 explicit_bzero(msg,
sizeof(msg));
396#ifdef HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT
421 if ((offset == SIZE_MAX) || (bip32_path == NULL) || (bip32_path->
length == 0)
423 || (offset + 1U + (bip32_path->
length * 4U) > msg_size)) {
426 msg[offset++] = bip32_path->
length;
427 for (uint8_t i = 0; i < bip32_path->
length; i++) {
428 U4BE_ENCODE(msg, offset, bip32_path->
path[i]);
432 &msg[offset], msg_size - offset, NULL, NULL, NULL, 0,
true, family, chain_id);
433 if (tail_len == SIZE_MAX) {
436 return offset + tail_len;
456 uint8_t hmac_out[CX_SHA256_SIZE])
459 bool success =
false;
463 if (msg_len == SIZE_MAX) {
466 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_LEDGER_ACCOUNT, msg, msg_len, hmac_out)) {
472 explicit_bzero(msg,
sizeof(msg));
490 const uint8_t hmac_expected[CX_SHA256_SIZE])
493 bool success =
false;
497 if (msg_len == SIZE_MAX) {
500 if (!sys_address_book_hmac_verify(
501 ADDRESS_BOOK_SALT_LEDGER_ACCOUNT, msg, msg_len, hmac_expected)) {
502 PRINTF(
"HMAC proof mismatch\n");
508 explicit_bzero(msg,
sizeof(msg));
bool address_book_send_hmac_proof(uint8_t type, const uint8_t hmac_proof[CX_SHA256_SIZE])
Send an HMAC proof response for Edit operations.
#define HMAC_PROOF_MSG_SIZE
bool address_book_send_register_identity_response(const uint8_t group_handle[GROUP_HANDLE_SIZE], const uint8_t hmac_proof[CX_SHA256_SIZE], const uint8_t hmac_rest[CX_SHA256_SIZE])
Send the Register Identity response carrying the group handle and both HMACs.
bool address_book_compute_hmac_rest(const uint8_t gid[GID_SIZE], const char *scope, const uint8_t *identifier, uint8_t identifier_len, blockchain_family_e family, uint64_t chain_id, uint8_t hmac_out[CX_SHA256_SIZE])
Compute HMAC_REST for an Identity contact.
bool address_book_compute_hmac_proof_ledger_account(const char *name, const path_bip32_t *bip32_path, blockchain_family_e family, uint64_t chain_id, uint8_t hmac_out[CX_SHA256_SIZE])
Compute an HMAC-SHA256 Proof of Registration for a Ledger Account.
static size_t serialize_ledger_account_msg(uint8_t *msg, size_t msg_size, const char *name, const path_bip32_t *bip32_path, blockchain_family_e family, uint64_t chain_id)
Serialize the Ledger Account HMAC message.
bool address_book_compute_hmac_proof(const uint8_t gid[GID_SIZE], const char *name, uint8_t hmac_out[CX_SHA256_SIZE])
Compute HMAC_PROOF for an Identity contact.
bool address_book_verify_hmac_proof(const uint8_t gid[GID_SIZE], const char *name, const uint8_t hmac_expected[CX_SHA256_SIZE])
Verify HMAC_PROOF for an Identity contact.
#define HMAC_MSG_MAX_SIZE
Maximum serialized HMAC message size.
bool address_book_generate_group_handle(uint8_t group_handle[GROUP_HANDLE_SIZE])
Generate a device-authenticated group handle.
bool address_book_verify_group_handle(const uint8_t group_handle[GROUP_HANDLE_SIZE], uint8_t gid_out[GID_SIZE])
Verify a group handle and extract the gid.
bool address_book_verify_hmac_proof_ledger_account(const char *name, const path_bip32_t *bip32_path, blockchain_family_e family, uint64_t chain_id, const uint8_t hmac_expected[CX_SHA256_SIZE])
Verify an HMAC Proof of Registration for a Ledger Account.
static size_t serialize_hmac_msg(uint8_t *msg, size_t msg_size, const uint8_t *gid, const char *str1, const uint8_t *raw, uint8_t raw_len, bool include_family, blockchain_family_e family, uint64_t chain_id)
Serialize an HMAC message from optional fields.
bool address_book_verify_hmac_rest(const uint8_t gid[GID_SIZE], const char *scope, const uint8_t *identifier, uint8_t identifier_len, blockchain_family_e family, uint64_t chain_id, const uint8_t hmac_expected[CX_SHA256_SIZE])
Verify HMAC_REST for an Identity contact.
#define HMAC_LEDGER_MSG_SIZE
Register / Edit Contact Name / Edit Scope / Edit Identifier.
#define GID_SIZE
32-byte random group ID generated by the device
#define TYPE_REGISTER_IDENTITY
#define GROUP_HANDLE_SIZE
group_handle = gid(32) | MAC(K_group, gid)(32)
static int io_send_response_pointer(const uint8_t *ptr, size_t size, uint16_t sw)
Random Number Generation.
SHA-2 (Secure Hash Algorithm 2).
uint32_t path[MAX_BIP32_PATH]