72
72
#include "inheritance_api.h"
73
73
#include "inheritance_context.h"
74
74
#include "inheritance_priv.h"
75
+ #include "memzero.h"
76
+ #include "pb.h"
75
77
#include "pb_decode.h"
76
78
#include "pb_encode.h"
77
79
#include "reconstruct_wallet_flow.h"
@@ -234,7 +236,7 @@ STATIC bool inheritance_handle_initiate_query(inheritance_query_t *query) {
234
236
query -> decrypt .initiate .wallet_id ,
235
237
WALLET_ID_SIZE );
236
238
inheritance_result_t result =
237
- init_inheritance_result (INHERITANCE_RESULT_ENCRYPT_TAG );
239
+ init_inheritance_result (INHERITANCE_RESULT_DECRYPT_TAG );
238
240
result .decrypt .which_response =
239
241
INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_CONFIRMATION_TAG ;
240
242
inheritance_send_result (& result );
@@ -254,15 +256,15 @@ static bool decode_inheritance_encrypted_data(
254
256
255
257
// zeroise for safety from garbage in the query reference
256
258
memzero (encrypted_data ,
257
- sizeof (inheritance_decrypt_data_with_pin_decrypted_data_structure_t ));
259
+ sizeof (inheritance_decrypt_data_with_pin_encrypted_data_structure_t ));
258
260
259
261
/* Create a stream that reads from the buffer. */
260
262
pb_istream_t stream = pb_istream_from_buffer (data , data_size );
261
263
262
264
/* Now we are ready to decode the message. */
263
265
bool status = pb_decode (
264
266
& stream ,
265
- INHERITANCE_DECRYPT_DATA_WITH_PIN_DECRYPTED_DATA_STRUCTURE_FIELDS ,
267
+ INHERITANCE_DECRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_FIELDS ,
266
268
encrypted_data );
267
269
268
270
/* Send error to host if status is false*/
@@ -275,8 +277,7 @@ static bool decode_inheritance_encrypted_data(
275
277
}
276
278
277
279
static bool inheritance_get_encrypted_data (inheritance_query_t * query ) {
278
- uint8_t encoded_data [INHERITANCE_PACKET_MAX_SIZE ] = {
279
- 0 }; ///< CONFIRM ENCODED DATA MAX SIZE
280
+ uint8_t encoded_data [INHERITANCE_PACKET_MAX_SIZE ] = {0 };
280
281
inheritance_result_t response =
281
282
init_inheritance_result (INHERITANCE_RESULT_DECRYPT_TAG );
282
283
const inheritance_decrypt_data_with_pin_encrypted_data_t * encrypted_data =
@@ -309,7 +310,7 @@ static bool inheritance_get_encrypted_data(inheritance_query_t *query) {
309
310
size += chunk -> size ;
310
311
311
312
// Send chunk ack to host
312
- response .encrypt .which_response =
313
+ response .decrypt .which_response =
313
314
INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_DATA_ACCEPTED_TAG ;
314
315
response .decrypt .data_accepted .has_chunk_ack = true;
315
316
response .decrypt .data_accepted .chunk_ack .chunk_index = payload -> chunk_index ;
@@ -336,10 +337,8 @@ static bool get_pb_encoded_buffer(
336
337
if (NULL == result || NULL == buffer || NULL == bytes_written_out ) {
337
338
return false;
338
339
}
339
- /* Create a stream that will write to our buffer. */
340
340
pb_ostream_t stream = pb_ostream_from_buffer (buffer , max_buffer_len );
341
341
342
- /* Now we are ready to encode the message! */
343
342
bool status = pb_encode (
344
343
& stream ,
345
344
INHERITANCE_DECRYPT_DATA_WITH_PIN_DECRYPTED_DATA_STRUCTURE_FIELDS ,
@@ -355,19 +354,17 @@ static bool get_pb_encoded_buffer(
355
354
static bool inheritance_send_in_chunks (inheritance_query_t * query ,
356
355
const uint8_t * buffer ,
357
356
const size_t buffer_len ) {
358
- size_t total_count = ((buffer_len % DECRYPTED_CHUNK_SIZE ) > 0 )
359
- ? (buffer_len / DECRYPTED_CHUNK_SIZE ) + 1
360
- : (buffer_len / DECRYPTED_CHUNK_SIZE );
357
+ size_t total_count = ((buffer_len + 1 ) / DECRYPTED_CHUNK_SIZE );
361
358
size_t remaining_size = (size_t )buffer_len ;
362
359
size_t offset = 0 ;
363
360
inheritance_result_t result =
364
361
init_inheritance_result (INHERITANCE_RESULT_DECRYPT_TAG );
365
362
result .decrypt .which_response =
366
363
INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_DECRYPTED_DATA_TAG ;
367
- result .decrypt .decrypted_data .chunk_payload .chunk_index = 0 ;
364
+ uint32_t * index = & result .decrypt .decrypted_data .chunk_payload .chunk_index ;
368
365
result .decrypt .decrypted_data .chunk_payload .total_chunks = total_count ;
369
366
370
- for (int index = 0 ; index < total_count ; index ++ ) {
367
+ for (* index = 0 ; * index < total_count ; ( * index ) ++ ) {
371
368
if (!inheritance_get_query (query , INHERITANCE_QUERY_DECRYPT_TAG ) ||
372
369
!check_which_request (
373
370
query ,
@@ -376,7 +373,7 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
376
373
}
377
374
// chunk_payload validation checks
378
375
if (query -> decrypt .decrypted_data_request .has_chunk_ack == false ||
379
- query -> decrypt .decrypted_data_request .chunk_ack .chunk_index != index ) {
376
+ query -> decrypt .decrypted_data_request .chunk_ack .chunk_index != * index ) {
380
377
return false;
381
378
}
382
379
size_t chunk_size = (remaining_size > DECRYPTED_CHUNK_SIZE )
@@ -391,7 +388,6 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
391
388
result .decrypt .decrypted_data .chunk_payload .chunk .size = chunk_size ;
392
389
inheritance_send_result (& result );
393
390
offset += chunk_size ;
394
- result .decrypt .decrypted_data .chunk_payload .chunk_index ++ ;
395
391
if (remaining_size == 0 ) {
396
392
break ;
397
393
}
@@ -401,27 +397,20 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
401
397
402
398
static bool send_decrypted_data (inheritance_query_t * query ) {
403
399
uint8_t
404
- buffer [INHERITANCE_DECRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_SIZE ] =
400
+ buffer [INHERITANCE_DECRYPT_DATA_WITH_PIN_DECRYPTED_DATA_STRUCTURE_SIZE ] =
405
401
{0 };
406
402
size_t bytes_encoded = 0 ;
407
403
if (!get_pb_encoded_buffer (& decryption_context -> response_payload ,
408
404
buffer ,
409
405
sizeof (buffer ),
410
- & bytes_encoded )) {
411
- return false;
412
- }
413
- if (!inheritance_send_in_chunks (query , buffer , bytes_encoded )) {
406
+ & bytes_encoded ) ||
407
+ !inheritance_send_in_chunks (query , buffer , bytes_encoded )) {
414
408
return false;
415
409
}
416
410
return true;
417
411
}
418
412
419
413
static bool decrypt_packet (void ) {
420
- // decryption_context->packet_size =
421
- // decryption_context->request_pointer->encrypted_data.size;
422
- // memcpy(decryption_context->packet,
423
- // decryption_context->request_pointer->encrypted_data.bytes,
424
- // decryption_context->packet_size);
425
414
return session_aes_decrypt (decryption_context -> encrypted_data .data .bytes ,
426
415
& decryption_context -> encrypted_data .data .size ) ==
427
416
SESSION_DECRYPT_PACKET_SUCCESS ;
0 commit comments