1
1
/**
2
- * @file inheritance_decrypt_data.c
3
2
* @author Cypherock X1 Team
4
3
* @brief Inheritance message decryption login
5
4
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
61
60
*****************************************************************************/
62
61
63
62
#include <stdint.h>
63
+ #include <stdio.h>
64
+ #include <string.h>
64
65
66
+ #include "card_fetch_data.h"
67
+ #include "card_operation_typedefs.h"
68
+ #include "constant_texts.h"
69
+ #include "core_session.h"
65
70
#include "inheritance/core.pb.h"
66
71
#include "inheritance/decrypt_data_with_pin.pb.h"
67
72
#include "inheritance_api.h"
73
+ #include "inheritance_context.h"
68
74
#include "inheritance_priv.h"
69
75
#include "reconstruct_wallet_flow.h"
70
76
#include "status_api.h"
71
77
#include "ui_core_confirm.h"
72
78
#include "ui_screens.h"
79
+ #include "utils.h"
73
80
#include "wallet_list.h"
74
81
75
82
/*****************************************************************************
88
95
* STATIC FUNCTION PROTOTYPES
89
96
*****************************************************************************/
90
97
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
-
170
98
/*****************************************************************************
171
99
* STATIC VARIABLES
172
100
*****************************************************************************/
173
101
174
- STATIC inheritance_decryption_context_t * inheritance_decryption_context = NULL ;
102
+ STATIC inheritance_decryption_context_t * context = NULL ;
175
103
176
104
/*****************************************************************************
177
105
* GLOBAL VARIABLES
@@ -196,13 +124,12 @@ static bool validate_request_data(
196
124
const inheritance_decrypt_data_with_pin_request_t * request ) {
197
125
bool status = true;
198
126
199
- // TODO: check the current request and session validity here
127
+ // TODO: check the current session validity here
200
128
201
129
return status ;
202
130
}
203
131
204
- STATIC bool inheritance_handle_initiate_query (
205
- const inheritance_query_t * query ) {
132
+ STATIC bool inheritance_handle_initiate_query (inheritance_query_t * query ) {
206
133
char wallet_name [NAME_SIZE ] = "" ;
207
134
char msg [100 ] = "" ;
208
135
@@ -215,30 +142,21 @@ STATIC bool inheritance_handle_initiate_query(
215
142
return false;
216
143
}
217
144
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
+
221
150
if (!core_confirmation (msg , inheritance_send_error )) {
222
151
return false;
223
152
}
224
153
225
154
set_app_flow_status (INHERITANCE_DECRYPT_DATA_STATUS_USER_CONFIRMED );
226
155
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 );
232
157
return true;
233
158
}
234
159
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
-
242
160
static bool send_decrypted_data (inheritance_query_t * query ) {
243
161
inheritance_result_t result =
244
162
init_inheritance_result (INHERITANCE_RESULT_DECRYPT_TAG );
@@ -250,34 +168,101 @@ static bool send_decrypted_data(inheritance_query_t *query) {
250
168
return false;
251
169
}
252
170
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
-
259
171
memcpy (& result .decrypt .messages ,
260
- & dummy ,
172
+ & context -> response ,
261
173
sizeof (inheritance_decrypt_data_with_pin_messages_response_t ));
262
174
263
- set_app_flow_status (INHERITANCE_DECRYPT_DATA_STATUS_MESSAGE_DECRYPTED );
264
175
inheritance_send_result (& result );
265
176
return true;
266
177
}
267
178
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 ;
273
202
}
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
+ }
277
263
}
278
264
279
265
set_app_flow_status (INHERITANCE_DECRYPT_DATA_STATUS_PIN_VERIFIED );
280
- send_response (INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_ACK_TAG );
281
266
return true;
282
267
}
283
268
@@ -286,17 +271,19 @@ STATIC bool inheritance_get_user_pin_verification(inheritance_query_t *query) {
286
271
*****************************************************************************/
287
272
288
273
void inheritance_decrypt_data (inheritance_query_t * query ) {
289
- inheritance_decryption_context = (inheritance_decryption_context_t * )malloc (
274
+ context = (inheritance_decryption_context_t * )malloc (
290
275
sizeof (inheritance_decryption_context_t ));
291
- memzero (inheritance_decryption_context ,
292
- sizeof (inheritance_decryption_context_t ));
276
+ memzero (context , sizeof (inheritance_decryption_context_t ));
293
277
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 );
298
283
}
299
284
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 ;
302
289
}
0 commit comments