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))
56#define HMAC_LEDGER_MSG_SIZE (1U + (ACCOUNT_NAME_LENGTH - 1U) + 1U + 8U)
81static size_t serialize_hmac_msg(uint8_t *msg,
95 if (offset + GID_SIZE > msg_size) {
98 memmove(&msg[offset], gid, GID_SIZE);
104 uint8_t len = (uint8_t) strlen(str1);
105 if (offset + 1U + (
size_t) len > msg_size) {
110 memmove(&msg[offset], str1, len);
117 if (offset + 1U + (
size_t) raw_len > msg_size) {
120 msg[offset++] = raw_len;
121 memmove(&msg[offset], raw, raw_len);
126 if (include_family) {
127 if (offset + 1U > msg_size) {
130 msg[offset++] = (uint8_t) family;
132 if (offset + 8U > msg_size) {
135 U8BE_ENCODE(msg, offset, chain_id);
147 .path = {0x8000002C, 0x8000003C, 0x80000000, 0x00000000, 0x00000000},
161bool address_book_send_hmac_proof(uint8_t type,
const uint8_t hmac_proof[CX_SHA256_SIZE])
166 G_io_tx_buffer[tx++] = type;
167 memmove(&G_io_tx_buffer[tx], hmac_proof, CX_SHA256_SIZE);
168 tx += CX_SHA256_SIZE;
185bool address_book_send_register_identity_response(
const uint8_t group_handle[GROUP_HANDLE_SIZE],
186 const uint8_t hmac_proof[CX_SHA256_SIZE],
187 const uint8_t hmac_rest[CX_SHA256_SIZE])
191 G_io_tx_buffer[tx++] = TYPE_REGISTER_IDENTITY;
192 memmove(&G_io_tx_buffer[tx], group_handle, GROUP_HANDLE_SIZE);
193 tx += GROUP_HANDLE_SIZE;
194 memmove(&G_io_tx_buffer[tx], hmac_proof, CX_SHA256_SIZE);
195 tx += CX_SHA256_SIZE;
196 memmove(&G_io_tx_buffer[tx], hmac_rest, CX_SHA256_SIZE);
197 tx += CX_SHA256_SIZE;
212bool address_book_generate_group_handle(uint8_t group_handle[GROUP_HANDLE_SIZE])
214 bool success =
false;
215 uint8_t *gid = group_handle;
216 uint8_t *mac = group_handle + GID_SIZE;
218 cx_rng_no_throw(gid, GID_SIZE);
219 if (!sys_address_book_hmac(s_identity_path.
path,
221 ADDRESS_BOOK_SALT_GROUP,
243bool address_book_verify_group_handle(
const uint8_t group_handle[GROUP_HANDLE_SIZE],
244 uint8_t gid_out[GID_SIZE])
246 bool success =
false;
247 const uint8_t *gid = group_handle;
248 const uint8_t *mac = group_handle + GID_SIZE;
250 if (!sys_address_book_hmac_verify(s_identity_path.
path,
252 ADDRESS_BOOK_SALT_GROUP,
256 PRINTF(
"[Address Book] Group handle MAC verification failed\n");
259 memmove(gid_out, gid, GID_SIZE);
277bool address_book_compute_hmac_proof(
const uint8_t gid[GID_SIZE],
279 uint8_t hmac_out[CX_SHA256_SIZE])
281 uint8_t msg[HMAC_PROOF_MSG_SIZE] = {0};
282 bool success =
false;
283 size_t msg_len = serialize_hmac_msg(msg,
sizeof(msg), gid, name, NULL, 0,
false, 0, 0);
285 if (msg_len == SIZE_MAX) {
288 if (!sys_address_book_hmac(s_identity_path.
path,
290 ADDRESS_BOOK_SALT_IDENTITY,
299 explicit_bzero(msg,
sizeof(msg));
311bool address_book_verify_hmac_proof(
const uint8_t gid[GID_SIZE],
313 const uint8_t hmac_expected[CX_SHA256_SIZE])
315 uint8_t msg[HMAC_PROOF_MSG_SIZE] = {0};
316 bool success =
false;
317 size_t msg_len = serialize_hmac_msg(msg,
sizeof(msg), gid, name, NULL, 0,
false, 0, 0);
319 if (msg_len == SIZE_MAX) {
322 if (!sys_address_book_hmac_verify(s_identity_path.
path,
324 ADDRESS_BOOK_SALT_IDENTITY,
328 PRINTF(
"HMAC_PROOF mismatch\n");
334 explicit_bzero(msg,
sizeof(msg));
354bool address_book_compute_hmac_rest(
const uint8_t gid[GID_SIZE],
356 const uint8_t *identifier,
357 uint8_t identifier_len,
360 uint8_t hmac_out[CX_SHA256_SIZE])
362 uint8_t msg[HMAC_MSG_MAX_SIZE] = {0};
363 bool success =
false;
364 size_t msg_len = serialize_hmac_msg(
365 msg,
sizeof(msg), gid, scope, identifier, identifier_len,
true, family, chain_id);
367 if (msg_len == SIZE_MAX) {
370 if (!sys_address_book_hmac(s_identity_path.
path,
372 ADDRESS_BOOK_SALT_IDENTITY,
381 explicit_bzero(msg,
sizeof(msg));
397bool address_book_verify_hmac_rest(
const uint8_t gid[GID_SIZE],
399 const uint8_t *identifier,
400 uint8_t identifier_len,
403 const uint8_t hmac_expected[CX_SHA256_SIZE])
405 uint8_t msg[HMAC_MSG_MAX_SIZE] = {0};
406 bool success =
false;
407 size_t msg_len = serialize_hmac_msg(
408 msg,
sizeof(msg), gid, scope, identifier, identifier_len,
true, family, chain_id);
410 if (msg_len == SIZE_MAX) {
413 if (!sys_address_book_hmac_verify(s_identity_path.
path,
415 ADDRESS_BOOK_SALT_IDENTITY,
419 PRINTF(
"HMAC_REST mismatch\n");
425 explicit_bzero(msg,
sizeof(msg));
429#ifdef HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT
444bool address_book_compute_hmac_proof_ledger_account(
const path_bip32_t *bip32_path,
448 uint8_t hmac_out[CX_SHA256_SIZE])
450 uint8_t msg[HMAC_LEDGER_MSG_SIZE] = {0};
451 bool success =
false;
453 = serialize_hmac_msg(msg,
sizeof(msg), NULL, name, NULL, 0,
true, family, chain_id);
455 if (msg_len == SIZE_MAX) {
458 if (!sys_address_book_hmac(bip32_path->
path,
460 ADDRESS_BOOK_SALT_LEDGER_ACCOUNT,
469 explicit_bzero(msg,
sizeof(msg));
483bool address_book_verify_hmac_proof_ledger_account(
const path_bip32_t *bip32_path,
487 const uint8_t hmac_expected[CX_SHA256_SIZE])
489 uint8_t msg[HMAC_LEDGER_MSG_SIZE] = {0};
490 bool success =
false;
492 = serialize_hmac_msg(msg,
sizeof(msg), NULL, name, NULL, 0,
true, family, chain_id);
494 if (msg_len == SIZE_MAX) {
497 if (!sys_address_book_hmac_verify(bip32_path->
path,
499 ADDRESS_BOOK_SALT_LEDGER_ACCOUNT,
503 PRINTF(
"HMAC proof mismatch\n");
509 explicit_bzero(msg,
sizeof(msg));
Register / Edit Contact Name / Edit Scope / Edit Identifier.
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]