Skip to content

Commit f1b345e

Browse files
committed
feat: Added decryption logic
1 parent 0ed1123 commit f1b345e

14 files changed

+1212
-881
lines changed

apps/inheritance_app/inheritance_context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@
2121

2222
#define INHERITANCE_MESSAGES_MAX_COUNT 5
2323
#define INHERITANCE_PACKET_MAX_SIZE 6000
24+
#define INHERITANCE_PIN_TAG 0x50
2425

2526
/*****************************************************************************
2627
* TYPEDEFS
2728
*****************************************************************************/
29+
typedef enum inheritance_message_type {
30+
INHERITANCE_DEFAULT_MESSAGE = 0x00,
31+
INHERITANCE_ONLY_SHOW_ON_DEVICE = 0x50,
32+
} inheritance_message_type_e;
2833

2934
/*****************************************************************************
3035
* EXPORTED VARIABLES
Lines changed: 112 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @file inheritance_decrypt_data.c
32
* @author Cypherock X1 Team
43
* @brief Inheritance message decryption login
54
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
@@ -61,15 +60,23 @@
6160
*****************************************************************************/
6261

6362
#include <stdint.h>
63+
#include <stdio.h>
64+
#include <string.h>
6465

66+
#include "card_fetch_data.h"
67+
#include "card_operation_typedefs.h"
68+
#include "constant_texts.h"
69+
#include "core_session.h"
6570
#include "inheritance/core.pb.h"
6671
#include "inheritance/decrypt_data_with_pin.pb.h"
6772
#include "inheritance_api.h"
73+
#include "inheritance_context.h"
6874
#include "inheritance_priv.h"
6975
#include "reconstruct_wallet_flow.h"
7076
#include "status_api.h"
7177
#include "ui_core_confirm.h"
7278
#include "ui_screens.h"
79+
#include "utils.h"
7380
#include "wallet_list.h"
7481

7582
/*****************************************************************************
@@ -88,90 +95,11 @@
8895
* STATIC FUNCTION PROTOTYPES
8996
*****************************************************************************/
9097

91-
/**
92-
* @brief Checks if the provided query contains expected request.
93-
* @details The function performs the check on the request type and if the check
94-
* fails, then it will send an error to the host inheritance app and return
95-
* false.
96-
*
97-
* @param query Reference to an instance of inheritance_query_t containing query
98-
* received from host app
99-
* @param which_request The expected request type enum
100-
*
101-
* @return bool Indicating if the check succeeded or failed
102-
* @retval true If the query contains the expected request
103-
* @retval false If the query does not contain the expected request
104-
*/
105-
static bool check_which_request(const inheritance_query_t *query,
106-
pb_size_t which_request);
107-
108-
/**
109-
* @brief Validates the derivation path received in the request from host
110-
* @details The function validates the provided account derivation path in the
111-
* request. If invalid path is detected, the function will send an error to the
112-
* host and return false.
113-
*
114-
* @param request Reference to an instance of inheritance_decrypt_data_request_t
115-
* @return bool Indicating if the verification passed or failed
116-
* @retval true If all the derivation path entries are valid
117-
* @retval false If any of the derivation path entries are invalid
118-
*/
119-
static bool validate_request_data(
120-
const inheritance_decrypt_data_with_pin_request_t *request);
121-
122-
/**
123-
* @brief Takes already received and decoded query for the user confirmation.
124-
* @details The function will verify if the query contains the
125-
* INHERITANCE_DECRYPT_DATA type of request. Additionally, the wallet-id is
126-
* validated for sanity and the derivation path for the account is also
127-
* validated. After the validations, user is prompted about the action for
128-
* confirmation. The function returns true indicating all the validation and
129-
* user confirmation was a success. The function also duplicates the data from
130-
* query into the inheritance_txn_context for further processing.
131-
*
132-
* @param query Constant reference to the decoded query received from the host
133-
*
134-
* @return bool Indicating if the function actions succeeded or failed
135-
* @retval true If all the validation and user confirmation was positive
136-
* @retval false If any of the validation or user confirmation was negative
137-
*/
138-
STATIC bool inheritance_handle_initiate_query(const inheritance_query_t *query);
139-
140-
/**
141-
* @brief Aggregates user consent for the decryption info
142-
* @details The function displays the required messages for user to very
143-
*
144-
*
145-
* @return bool Indicating if the user confirmed the messages
146-
* @retval true If user confirmed the messages displayed
147-
* @retval false Immediate return if any of the messages are disapproved
148-
*/
149-
STATIC bool inheritance_get_user_pin_verification(inheritance_query_t *query);
150-
151-
/**
152-
* @brief Sends the decrypted data to the host
153-
* @details The function decrypts the data and sends it to the host
154-
*
155-
* @param query Reference to an instance of inheritance_query_t to store
156-
* transient request from the host
157-
* @return bool Indicating if the decrypted data is sent to the host
158-
* @retval true If the decrypted data was sent to host successfully
159-
* @retval false If the host responded with unknown/wrong query
160-
*/
161-
static bool send_decrypted_data(inheritance_query_t *query);
162-
163-
/**
164-
* @brief The function prepares and sends empty responses
165-
*
166-
* @param which_response Constant value for the response type to be sent
167-
*/
168-
static void send_response(pb_size_t which_response);
169-
17098
/*****************************************************************************
17199
* STATIC VARIABLES
172100
*****************************************************************************/
173101

174-
STATIC inheritance_decryption_context_t *inheritance_decryption_context = NULL;
102+
STATIC inheritance_decryption_context_t *context = NULL;
175103

176104
/*****************************************************************************
177105
* GLOBAL VARIABLES
@@ -196,13 +124,12 @@ static bool validate_request_data(
196124
const inheritance_decrypt_data_with_pin_request_t *request) {
197125
bool status = true;
198126

199-
// TODO: check the current request and session validity here
127+
// TODO: check the current session validity here
200128

201129
return status;
202130
}
203131

204-
STATIC bool inheritance_handle_initiate_query(
205-
const inheritance_query_t *query) {
132+
STATIC bool inheritance_handle_initiate_query(inheritance_query_t *query) {
206133
char wallet_name[NAME_SIZE] = "";
207134
char msg[100] = "";
208135

@@ -215,30 +142,21 @@ STATIC bool inheritance_handle_initiate_query(
215142
return false;
216143
}
217144

218-
snprintf(msg, sizeof(msg), "Test %s", wallet_name); // TODO: update message
219-
//
220-
// Take user consent to sign the transaction for the wallet
145+
snprintf(msg,
146+
sizeof(msg),
147+
ui_text_inheritance_decryption_flow_failure,
148+
wallet_name);
149+
221150
if (!core_confirmation(msg, inheritance_send_error)) {
222151
return false;
223152
}
224153

225154
set_app_flow_status(INHERITANCE_DECRYPT_DATA_STATUS_USER_CONFIRMED);
226155

227-
// TODO: copy data to local context;
228-
229-
// show processing screen for a minimum duration (additional time will add due
230-
// to actual processing)
231-
delay_scr_init(ui_text_processing, DELAY_SHORT);
156+
context->request_pointer = &(query->decrypt.initiate);
232157
return true;
233158
}
234159

235-
static void send_response(const pb_size_t which_response) {
236-
inheritance_result_t result =
237-
init_inheritance_result(INHERITANCE_RESULT_DECRYPT_TAG);
238-
result.decrypt.which_response = which_response;
239-
inheritance_send_result(&result);
240-
}
241-
242160
static bool send_decrypted_data(inheritance_query_t *query) {
243161
inheritance_result_t result =
244162
init_inheritance_result(INHERITANCE_RESULT_DECRYPT_TAG);
@@ -250,34 +168,101 @@ static bool send_decrypted_data(inheritance_query_t *query) {
250168
return false;
251169
}
252170

253-
inheritance_decrypt_data_with_pin_messages_response_t dummy = {0};
254-
255-
dummy.plain_data_count = 1;
256-
dummy.plain_data[0].message.size = 1;
257-
dummy.plain_data[0].message.bytes[0] = 97;
258-
259171
memcpy(&result.decrypt.messages,
260-
&dummy,
172+
&context->response,
261173
sizeof(inheritance_decrypt_data_with_pin_messages_response_t));
262174

263-
set_app_flow_status(INHERITANCE_DECRYPT_DATA_STATUS_MESSAGE_DECRYPTED);
264175
inheritance_send_result(&result);
265176
return true;
266177
}
267178

268-
STATIC bool inheritance_get_user_pin_verification(inheritance_query_t *query) {
269-
if (!inheritance_get_query(query, INHERITANCE_QUERY_DECRYPT_TAG) ||
270-
!check_which_request(query,
271-
INHERITANCE_DECRYPT_DATA_WITH_PIN_REQUEST_ACK_TAG)) {
272-
return false;
179+
static bool decrypt_packet(void) {
180+
context->packet_size = context->request_pointer->encrypted_data.size;
181+
memcpy(context->packet,
182+
context->request_pointer->encrypted_data.bytes,
183+
context->packet_size);
184+
return session_aes_decrypt(context->packet, &context->packet_size) ==
185+
SESSION_DECRYPT_PACKET_SUCCESS;
186+
}
187+
188+
static bool deserialize_packet(void) {
189+
uint16_t packet_index = 0;
190+
context->data_count = context->packet[packet_index++];
191+
for (uint8_t index = 0; index < context->data_count; index++) {
192+
packet_index++; ///< Skip tag
193+
194+
context->data[index].encrypted_data_size =
195+
U16_READ_BE_ARRAY(&context->packet[packet_index]);
196+
packet_index += 2; ///< Read length
197+
198+
memcpy(context->data[index].encrypted_data,
199+
&context->packet[packet_index],
200+
context->data[index].encrypted_data_size);
201+
packet_index += context->data[index].encrypted_data_size;
273202
}
274-
// TODO: Show user the pin
275-
if (!core_scroll_page("Your PIN", "1234", inheritance_send_error)) {
276-
return false;
203+
204+
return packet_index <= context->packet_size;
205+
}
206+
207+
static bool decrypt_message_data(void) {
208+
return card_fetch_decrypt_data(context->request_pointer->wallet_id,
209+
context->data,
210+
context->data_count) == CARD_OPERATION_SUCCESS;
211+
}
212+
213+
static bool decrypt_data(void) {
214+
bool status = true;
215+
216+
do {
217+
if (!decrypt_packet()) {
218+
// TODO: Throw packet decryption error
219+
status = false;
220+
break;
221+
}
222+
223+
if (!deserialize_packet()) {
224+
// TODO: Throw packet serialization error
225+
status = false;
226+
break;
227+
}
228+
if (!decrypt_message_data()) {
229+
// TODO: Throw decryption failed
230+
status = false;
231+
break;
232+
}
233+
234+
} while (0);
235+
set_app_flow_status(INHERITANCE_DECRYPT_DATA_STATUS_MESSAGE_DECRYPTED);
236+
return status;
237+
}
238+
239+
static bool show_data(void) {
240+
pb_size_t response_count = 0;
241+
242+
for (uint8_t i = 0; i < context->data_count; i++) {
243+
uint8_t tag = context->data[i].plain_data[0];
244+
245+
if (tag == INHERITANCE_ONLY_SHOW_ON_DEVICE) {
246+
if (!core_scroll_page(
247+
UI_TEXT_VERIFY_MESSAGE,
248+
(const char *)&context->data[i]
249+
.plain_data[3], ///> sizeof (tag) + sizeof (length) = 3
250+
inheritance_send_error)) {
251+
return false;
252+
}
253+
} else {
254+
uint16_t offset = 1; // Skip tag
255+
context->response.plain_data[response_count].message.size =
256+
U16_READ_BE_ARRAY(context->data[i].plain_data + offset);
257+
offset += 2; // Skip length
258+
memcpy(context->response.plain_data[response_count].message.bytes,
259+
context->data[i].plain_data + offset,
260+
context->response.plain_data[response_count].message.size);
261+
context->response.plain_data_count = ++response_count;
262+
}
277263
}
278264

279265
set_app_flow_status(INHERITANCE_DECRYPT_DATA_STATUS_PIN_VERIFIED);
280-
send_response(INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_ACK_TAG);
281266
return true;
282267
}
283268

@@ -286,17 +271,19 @@ STATIC bool inheritance_get_user_pin_verification(inheritance_query_t *query) {
286271
*****************************************************************************/
287272

288273
void inheritance_decrypt_data(inheritance_query_t *query) {
289-
inheritance_decryption_context = (inheritance_decryption_context_t *)malloc(
274+
context = (inheritance_decryption_context_t *)malloc(
290275
sizeof(inheritance_decryption_context_t));
291-
memzero(inheritance_decryption_context,
292-
sizeof(inheritance_decryption_context_t));
276+
memzero(context, sizeof(inheritance_decryption_context_t));
293277

294-
// TODO: add actual decryption and decrypiton function
295-
if (inheritance_handle_initiate_query(query) && send_decrypted_data(query) &&
296-
inheritance_get_user_pin_verification(query)) {
297-
delay_scr_init(ui_text_check_cysync, DELAY_TIME);
278+
if (inheritance_handle_initiate_query(query) && decrypt_data() &&
279+
show_data() && send_decrypted_data(query)) {
280+
delay_scr_init(ui_text_inheritance_decryption_flow_success, DELAY_TIME);
281+
} else {
282+
delay_scr_init(ui_text_inheritance_decryption_flow_failure, DELAY_TIME);
298283
}
299284

300-
free(inheritance_decryption_context);
301-
inheritance_decryption_context = NULL;
285+
delay_scr_init(ui_text_check_cysync, DELAY_TIME);
286+
memzero(context, sizeof(inheritance_decryption_context_t));
287+
free(context);
288+
context = NULL;
302289
}

0 commit comments

Comments
 (0)