Skip to content
75 changes: 62 additions & 13 deletions apps/inheritance_app/inheritance_auth_wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

#include "bip39.h"
#include "card_fetch_data.h"
#include "card_pair.h"
#include "core_error.h"
#include "inheritance/core.pb.h"
#include "inheritance_api.h"
#include "inheritance_priv.h"
#include "reconstruct_wallet_flow.h"
#include "status_api.h"
#include "ui_delay.h"

/*****************************************************************************
* EXTERN VARIABLES
*****************************************************************************/
Expand Down Expand Up @@ -60,8 +61,39 @@ static auth_wallet_config_t *auth = NULL;
static bool verify_auth_wallet_inputs();

/**
* @brief Retrieves encrypted data (entropy) from the card based on the wallet
* ID.
* @brief Retrieves and stores seed-based entropy for authentication.
*
* Generates entropy from the seed based on the wallet ID and stores it in the
* auth structure.
*
* @return true if seed-based entropy is successfully generated and stored,
* false otherwise.
*/
static bool auth_wallet_get_seed_entropy();

/**
* @brief Pairs the card with the device.
*
* Initiates the card pairing process and updates the app flow status upon
* success.
*
* @return true if the card is successfully paired, false otherwise.
*/
static bool auth_wallet_pair_card();

/**
* @brief Retrieves and stores wallet-based entropy from the card.
*
* Fetches encrypted wallet ID data from the card to generate entropy.
*
* @return true if wallet-based entropy is successfully fetched and stored,
* false otherwise.
*/
static bool auth_wallet_get_wallet_entropy();

/**
* @brief Retrieves encrypted data (entropy) from the card based on the type
* requested from the host (seed based and/or wallet_id based)
*
* This function initializes a secure_data_t structure, fetches encrypted data
* from the card, and stores the result in the auth structure. It checks if the
Expand Down Expand Up @@ -126,19 +158,15 @@ static bool verify_auth_wallet_inputs() {
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
return false;
}

return true;
}

static bool auth_wallet_get_entropy() {
static bool auth_wallet_get_seed_entropy() {
if (auth->do_seed_based) {
uint8_t seed[SIZE_SEED] = {0};
if (!reconstruct_seed_without_passphrase(
auth->data.wallet_id, seed, inheritance_send_error)) {
memzero(seed, sizeof(seed));
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_INVALID_QUERY);
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
return false;
}
memcpy((void *)auth->seed_based_data.entropy, seed, SIZE_SEED);
Expand All @@ -148,18 +176,30 @@ static bool auth_wallet_get_entropy() {
// seed generation complete
set_app_flow_status(INHERITANCE_AUTH_WALLET_STATUS_SEED_BASED_CARD_TAPPED);
}
return true;
}

static bool auth_wallet_pair_card() {
// Pair the card first
card_error_type_e status = single_card_pair_operation(
(char *)ui_text_tap_the_card, ui_text_place_card_below);
if (status != CARD_OPERATION_SUCCESS) {
return false;
}
set_app_flow_status(INHERITANCE_AUTH_WALLET_STATUS_PAIRING_CARD_TAPPED);
return true;
}

static bool auth_wallet_get_wallet_entropy() {
if (auth->do_wallet_based) {
secure_data_t msgs[1] = {0};
msgs[0].plain_data_size = WALLET_ID_SIZE;
memcpy(msgs[0].plain_data, auth->data.wallet_id, WALLET_ID_SIZE);

// fetch encrypted wallet_id
card_error_type_e status =
card_fetch_encrypt_data(auth->data.wallet_id, msgs, 1);
if (status != CARD_OPERATION_SUCCESS ||
msgs[0].encrypted_data_size > ENTROPY_SIZE_LIMIT) {
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_INVALID_DATA);
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
return false;
}
memcpy((void *)auth->wallet_based_data.entropy,
Expand All @@ -171,9 +211,18 @@ static bool auth_wallet_get_entropy() {
set_app_flow_status(
INHERITANCE_AUTH_WALLET_STATUS_WALLET_BASED_CARD_TAPPED);
}
return true;
}

static bool auth_wallet_get_entropy() {
if (!auth_wallet_get_seed_entropy() || !auth_wallet_pair_card() ||
!auth_wallet_get_wallet_entropy()) {
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_INVALID_DATA);
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
return false;
}
delay_scr_init(ui_text_inheritance_wallet_authenticating, DELAY_SHORT);

return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/card_operations/card_operation_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ typedef enum card_errors_type {
can be associated to @ref
card_error_status_word_e errors or PN532
errors */

CARD_OPERATION_DEFAULT_INVALID = 0xFF, /** Default invalid value */
} card_error_type_e;

Expand Down
48 changes: 48 additions & 0 deletions src/card_operations/card_pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,54 @@ card_error_type_e card_pair_operation(uint8_t card_number,
break;
}

nfc_deselect_card();
return card_data.error_type;
}

card_error_type_e single_card_pair_operation(char *heading,
const char *message) {
// Need to handle how assign new card its number
card_operation_data_t card_data = {0};
card_pairing_data_t pair_data = {0};

if (SUCCESS != pair_card_preprocess(&pair_data)) {
return CARD_OPERATION_ABORT_OPERATION;
}

instruction_scr_init(message, heading);
card_data.nfc_data.retries = 5;

while (1) {
// Initialize card tap config
card_data.nfc_data.acceptable_cards = ACCEPTABLE_CARDS_ALL;

init_and_pair_card(&card_data, &pair_data);

if (CARD_OPERATION_SUCCESS == card_data.error_type) {
if (SW_NO_ERROR != handle_pairing_success(&card_data, &pair_data)) {
card_data.error_type = CARD_OPERATION_ABORT_OPERATION;
break;
}

buzzer_start(BUZZER_DURATION);
wait_for_card_removal();

break;
}

if (CARD_OPERATION_CARD_REMOVED == card_data.error_type ||
CARD_OPERATION_RETAP_BY_USER_REQUIRED == card_data.error_type) {
const char *error_msg = card_data.error_message;
if (CARD_OPERATION_SUCCESS == indicate_card_error(error_msg)) {
// Re-render the instruction screen
instruction_scr_init(message, heading);
continue;
}
}

break;
}

nfc_deselect_card();
return card_data.error_type;
}
6 changes: 6 additions & 0 deletions src/card_operations/card_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ card_error_type_e card_pair_without_retap(uint8_t card_number,
card_error_type_e card_pair_operation(uint8_t card_number,
char *heading,
const char *message);
// To Improvise:
// Single card pairing operation,
// Should allow single or extra card to be paired if not already,
// for any required flow
card_error_type_e single_card_pair_operation(char *heading,
const char *message);
#endif
1 change: 1 addition & 0 deletions src/constant_texts.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ const char *ui_text_inheritance_wallet_auth_success =
"Wallet\nauthentication\nsuccessfully";
const char *ui_text_inheritance_wallet_auth_fail =
"Wallet\nauthentication\nfailed";
const char *ui_text_tap_the_card = "Tap the card";

const char *ui_text_inheritance_encryption_flow_confirmation =
"Proceed to encrypt data for %s?";
Expand Down
1 change: 1 addition & 0 deletions src/constant_texts.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ extern const char *ui_critical_card_health_migrate_data;
extern const char *ui_text_inheritance_wallet_authenticating;
extern const char *ui_text_inheritance_wallet_auth_success;
extern const char *ui_text_inheritance_wallet_auth_fail;
extern const char *ui_text_tap_the_card;

extern const char *ui_text_inheritance_encryption_flow_confirmation;
extern const char *ui_text_inheritance_encryption_flow_success;
Expand Down
Loading