|
| 1 | +/** |
| 2 | + * @file inheritance_auth_wallet.c |
| 3 | + * @author Cypherock X1 Team |
| 4 | + * @brief |
| 5 | + * @details |
| 6 | +
|
| 7 | + * @copyright Copyright (c) 2023 HODL TECH PTE LTD |
| 8 | + * <br/> You may obtain a copy of license at <a href="https://mitcc.org/" |
| 9 | + * target=_blank>https://mitcc.org/</a> |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +/***************************************************************************** |
| 14 | + * INCLUDES |
| 15 | + *****************************************************************************/ |
| 16 | + |
| 17 | +#include <stdbool.h> |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#include "bip39.h" |
| 21 | +#include "card_fetch_data.h" |
| 22 | +#include "inheritance/core.pb.h" |
| 23 | +#include "inheritance_api.h" |
| 24 | +#include "inheritance_priv.h" |
| 25 | +#include "status_api.h" |
| 26 | +#include "ui_delay.h" |
| 27 | + |
| 28 | +/***************************************************************************** |
| 29 | + * EXTERN VARIABLES |
| 30 | + *****************************************************************************/ |
| 31 | + |
| 32 | +/***************************************************************************** |
| 33 | + * PRIVATE MACROS AND DEFINES |
| 34 | + *****************************************************************************/ |
| 35 | + |
| 36 | +/***************************************************************************** |
| 37 | + * STATIC VARIABLES |
| 38 | + *****************************************************************************/ |
| 39 | +static auth_wallet_config_t *auth = NULL; |
| 40 | + |
| 41 | +/***************************************************************************** |
| 42 | + * GLOBAL VARIABLES |
| 43 | + *****************************************************************************/ |
| 44 | + |
| 45 | +/***************************************************************************** |
| 46 | + * STATIC FUNCTION PROTOTYPES |
| 47 | + *****************************************************************************/ |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Verifies the integrity and validity of the wallet authentication |
| 51 | + * inputs. |
| 52 | + * |
| 53 | + * This static function checks if the challenge, wallet ID, and challenge size |
| 54 | + * are non-zero and within the expected range. It ensures the authentication |
| 55 | + * inputs are valid. |
| 56 | + * |
| 57 | + * @return true Always returns true if all assertions pass. |
| 58 | + */ |
| 59 | +static bool verify_auth_wallet_inputs(); |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Retrieves encrypted data (entropy) from the card based on the wallet |
| 63 | + * ID. |
| 64 | + * |
| 65 | + * This function initializes a secure_data_t structure, fetches encrypted data |
| 66 | + * from the card, and stores the result in the auth structure. It checks if the |
| 67 | + * operation was successful and if the encrypted data size is within the allowed |
| 68 | + * limit. |
| 69 | + * |
| 70 | + * @return true If the entropy was successfully fetched and stored. |
| 71 | + * @return false If there was an error in fetching the encrypted data or if the |
| 72 | + * data size exceeds the limit. |
| 73 | + */ |
| 74 | +static bool auth_wallet_get_entropy(); |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Generates the public and private key pairs based on the entropy. |
| 78 | + * |
| 79 | + * This function derives a seed from the entropy and generates an Ed25519 key |
| 80 | + * pair (private and public keys). It stores the keys in the auth structure. |
| 81 | + * |
| 82 | + * @return true Always returns true. |
| 83 | + */ |
| 84 | +static bool auth_wallet_get_pairs(); |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Generates and verifies a digital signature for the wallet |
| 88 | + * authentication. |
| 89 | + * |
| 90 | + * This function creates an unsigned transaction by concatenating the challenge |
| 91 | + * and wallet ID. It then signs the transaction using the private key and |
| 92 | + * verifies the signature using the public key. |
| 93 | + * |
| 94 | + * @return true If the signature was successfully generated and verified. |
| 95 | + * @return false If the signature verification failed. |
| 96 | + */ |
| 97 | +static bool auth_wallet_get_signature(); |
| 98 | + |
| 99 | +/***************************************************************************** |
| 100 | + * STATIC FUNCTIONS |
| 101 | + *****************************************************************************/ |
| 102 | + |
| 103 | +static bool verify_auth_wallet_inputs() { |
| 104 | + if (NULL == auth->challenge || NULL == auth->wallet_id || |
| 105 | + auth->challenge_size < CHALLENGE_SIZE_MIN || |
| 106 | + auth->challenge_size > CHALLENGE_SIZE_MAX) { |
| 107 | + inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG, |
| 108 | + ERROR_DATA_FLOW_INVALID_QUERY); |
| 109 | + delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +static bool auth_wallet_get_entropy() { |
| 117 | + secure_data_t msgs[1] = {0}; |
| 118 | + msgs[0].plain_data_size = WALLET_ID_SIZE; |
| 119 | + memcpy(msgs[0].plain_data, auth->wallet_id, WALLET_ID_SIZE); |
| 120 | + |
| 121 | + card_error_type_e status = card_fetch_encrypt_data(auth->wallet_id, msgs, 1); |
| 122 | + |
| 123 | + delay_scr_init(ui_text_inheritance_wallet_authenticating, DELAY_SHORT); |
| 124 | + |
| 125 | + if (status != CARD_OPERATION_SUCCESS || |
| 126 | + msgs[0].encrypted_data_size > ENTROPY_SIZE_LIMIT) { |
| 127 | + inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG, |
| 128 | + ERROR_DATA_FLOW_INVALID_QUERY); |
| 129 | + delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME); |
| 130 | + return false; |
| 131 | + } |
| 132 | + set_app_flow_status(INHERITANCE_AUTH_WALLET_STATUS_CARD_TAPPED); |
| 133 | + |
| 134 | + memcpy((void *)auth->entropy, |
| 135 | + msgs[0].encrypted_data, |
| 136 | + msgs[0].encrypted_data_size); |
| 137 | + auth->entropy_size = msgs[0].encrypted_data_size; |
| 138 | + |
| 139 | + return true; |
| 140 | +} |
| 141 | + |
| 142 | +static bool auth_wallet_get_pairs() { |
| 143 | + mnemonic_to_seed((char *)auth->entropy, "", auth->private_key, NULL); |
| 144 | + ed25519_publickey(auth->private_key, auth->public_key); |
| 145 | + |
| 146 | + return true; |
| 147 | +} |
| 148 | + |
| 149 | +static bool auth_wallet_get_signature() { |
| 150 | + const size_t unsigned_txn_size = auth->challenge_size + WALLET_ID_SIZE; |
| 151 | + uint8_t unsigned_txn[unsigned_txn_size]; |
| 152 | + |
| 153 | + memcpy(unsigned_txn, auth->challenge, auth->challenge_size); |
| 154 | + memcpy(unsigned_txn + auth->challenge_size, auth->wallet_id, WALLET_ID_SIZE); |
| 155 | + |
| 156 | + ed25519_sign(unsigned_txn, |
| 157 | + unsigned_txn_size, |
| 158 | + auth->private_key, |
| 159 | + auth->public_key, |
| 160 | + auth->signature); |
| 161 | + |
| 162 | + int valid = ed25519_sign_open( |
| 163 | + unsigned_txn, unsigned_txn_size, auth->public_key, auth->signature); |
| 164 | + |
| 165 | + if (0 != valid) { |
| 166 | + inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG, |
| 167 | + ERROR_DATA_FLOW_INVALID_DATA); |
| 168 | + delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME); |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + return true; |
| 173 | +} |
| 174 | + |
| 175 | +static bool send_result() { |
| 176 | + inheritance_result_t result = INHERITANCE_RESULT_INIT_ZERO; |
| 177 | + result.which_response = INHERITANCE_RESULT_AUTH_WALLET_TAG; |
| 178 | + result.auth_wallet.which_response = |
| 179 | + INHERITANCE_AUTH_WALLET_RESPONSE_RESULT_TAG; |
| 180 | + memcpy(result.auth_wallet.result.signature, |
| 181 | + auth->signature, |
| 182 | + sizeof(ed25519_signature)); |
| 183 | + |
| 184 | + if (auth->is_setup) { |
| 185 | + memcpy(result.auth_wallet.result.public_key, |
| 186 | + auth->public_key, |
| 187 | + sizeof(ed25519_public_key)); |
| 188 | + } |
| 189 | + |
| 190 | + inheritance_send_result(&result); |
| 191 | + return true; |
| 192 | +} |
| 193 | +/***************************************************************************** |
| 194 | + * GLOBAL FUNCTIONS |
| 195 | + *****************************************************************************/ |
| 196 | + |
| 197 | +void inheritance_auth_wallet(inheritance_query_t *query) { |
| 198 | + auth = (auth_wallet_config_t *)malloc(sizeof(auth_wallet_config_t)); |
| 199 | + ASSERT(auth != NULL); |
| 200 | + memzero(auth, sizeof(auth_wallet_config_t)); |
| 201 | + |
| 202 | + memcpy( |
| 203 | + auth->wallet_id, query->auth_wallet.initiate.wallet_id, WALLET_ID_SIZE); |
| 204 | + auth->challenge_size = query->auth_wallet.initiate.challenge.size; |
| 205 | + memcpy(auth->challenge, |
| 206 | + query->auth_wallet.initiate.challenge.bytes, |
| 207 | + auth->challenge_size); |
| 208 | + auth->is_setup = query->auth_wallet.initiate.is_public_key; |
| 209 | + |
| 210 | + set_app_flow_status(INHERITANCE_AUTH_WALLET_STATUS_INIT); |
| 211 | + if (verify_auth_wallet_inputs() && auth_wallet_get_entropy() && |
| 212 | + auth_wallet_get_pairs() && auth_wallet_get_signature() && send_result()) { |
| 213 | + delay_scr_init(ui_text_inheritance_wallet_auth_success, DELAY_TIME); |
| 214 | + } |
| 215 | + |
| 216 | + memzero(auth, sizeof(auth_wallet_config_t)); |
| 217 | + free(auth); |
| 218 | + auth = NULL; |
| 219 | +} |
0 commit comments