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);
154bool address_book_send_hmac_proof(uint8_t type,
const uint8_t hmac_proof[CX_SHA256_SIZE])
159 G_io_tx_buffer[tx++] = type;
160 memmove(&G_io_tx_buffer[tx], hmac_proof, CX_SHA256_SIZE);
161 tx += CX_SHA256_SIZE;
178bool address_book_send_register_identity_response(
const uint8_t group_handle[GROUP_HANDLE_SIZE],
179 const uint8_t hmac_proof[CX_SHA256_SIZE],
180 const uint8_t hmac_rest[CX_SHA256_SIZE])
184 G_io_tx_buffer[tx++] = TYPE_REGISTER_IDENTITY;
185 memmove(&G_io_tx_buffer[tx], group_handle, GROUP_HANDLE_SIZE);
186 tx += GROUP_HANDLE_SIZE;
187 memmove(&G_io_tx_buffer[tx], hmac_proof, CX_SHA256_SIZE);
188 tx += CX_SHA256_SIZE;
189 memmove(&G_io_tx_buffer[tx], hmac_rest, CX_SHA256_SIZE);
190 tx += CX_SHA256_SIZE;
205bool address_book_generate_group_handle(uint8_t group_handle[GROUP_HANDLE_SIZE])
207 bool success =
false;
208 uint8_t *gid = group_handle;
209 uint8_t *mac = group_handle + GID_SIZE;
211 cx_rng_no_throw(gid, GID_SIZE);
212 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_GROUP, gid, GID_SIZE, mac)) {
231bool address_book_verify_group_handle(
const uint8_t group_handle[GROUP_HANDLE_SIZE],
232 uint8_t gid_out[GID_SIZE])
234 bool success =
false;
235 const uint8_t *gid = group_handle;
236 const uint8_t *mac = group_handle + GID_SIZE;
238 if (!sys_address_book_hmac_verify(ADDRESS_BOOK_SALT_GROUP, gid, GID_SIZE, mac)) {
239 PRINTF(
"[Address Book] Group handle MAC verification failed\n");
242 memmove(gid_out, gid, GID_SIZE);
260bool address_book_compute_hmac_proof(
const uint8_t gid[GID_SIZE],
262 uint8_t hmac_out[CX_SHA256_SIZE])
264 uint8_t msg[HMAC_PROOF_MSG_SIZE] = {0};
265 bool success =
false;
266 size_t msg_len = serialize_hmac_msg(msg,
sizeof(msg), gid, name, NULL, 0,
false, 0, 0);
268 if (msg_len == SIZE_MAX) {
271 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_out)) {
277 explicit_bzero(msg,
sizeof(msg));
289bool address_book_verify_hmac_proof(
const uint8_t gid[GID_SIZE],
291 const uint8_t hmac_expected[CX_SHA256_SIZE])
293 uint8_t msg[HMAC_PROOF_MSG_SIZE] = {0};
294 bool success =
false;
295 size_t msg_len = serialize_hmac_msg(msg,
sizeof(msg), gid, name, NULL, 0,
false, 0, 0);
297 if (msg_len == SIZE_MAX) {
300 if (!sys_address_book_hmac_verify(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_expected)) {
301 PRINTF(
"HMAC_PROOF mismatch\n");
307 explicit_bzero(msg,
sizeof(msg));
327bool address_book_compute_hmac_rest(
const uint8_t gid[GID_SIZE],
329 const uint8_t *identifier,
330 uint8_t identifier_len,
333 uint8_t hmac_out[CX_SHA256_SIZE])
335 uint8_t msg[HMAC_MSG_MAX_SIZE] = {0};
336 bool success =
false;
337 size_t msg_len = serialize_hmac_msg(
338 msg,
sizeof(msg), gid, scope, identifier, identifier_len,
true, family, chain_id);
340 if (msg_len == SIZE_MAX) {
343 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_out)) {
349 explicit_bzero(msg,
sizeof(msg));
365bool address_book_verify_hmac_rest(
const uint8_t gid[GID_SIZE],
367 const uint8_t *identifier,
368 uint8_t identifier_len,
371 const uint8_t hmac_expected[CX_SHA256_SIZE])
373 uint8_t msg[HMAC_MSG_MAX_SIZE] = {0};
374 bool success =
false;
375 size_t msg_len = serialize_hmac_msg(
376 msg,
sizeof(msg), gid, scope, identifier, identifier_len,
true, family, chain_id);
378 if (msg_len == SIZE_MAX) {
381 if (!sys_address_book_hmac_verify(ADDRESS_BOOK_SALT_IDENTITY, msg, msg_len, hmac_expected)) {
382 PRINTF(
"HMAC_REST mismatch\n");
388 explicit_bzero(msg,
sizeof(msg));
392#ifdef HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT
407bool address_book_compute_hmac_proof_ledger_account(
const path_bip32_t *bip32_path,
411 uint8_t hmac_out[CX_SHA256_SIZE])
413 uint8_t msg[HMAC_LEDGER_MSG_SIZE] = {0};
414 bool success =
false;
416 = serialize_hmac_msg(msg,
sizeof(msg), NULL, name, NULL, 0,
true, family, chain_id);
418 if (msg_len == SIZE_MAX) {
421 if (!sys_address_book_hmac(ADDRESS_BOOK_SALT_LEDGER_ACCOUNT, msg, msg_len, hmac_out)) {
427 explicit_bzero(msg,
sizeof(msg));
441bool address_book_verify_hmac_proof_ledger_account(
const path_bip32_t *bip32_path,
445 const uint8_t hmac_expected[CX_SHA256_SIZE])
447 uint8_t msg[HMAC_LEDGER_MSG_SIZE] = {0};
448 bool success =
false;
450 = serialize_hmac_msg(msg,
sizeof(msg), NULL, name, NULL, 0,
true, family, chain_id);
452 if (msg_len == SIZE_MAX) {
455 if (!sys_address_book_hmac_verify(
456 ADDRESS_BOOK_SALT_LEDGER_ACCOUNT, msg, msg_len, hmac_expected)) {
457 PRINTF(
"HMAC proof mismatch\n");
463 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)