Skip to content

Commit 5df44ab

Browse files
committed
fix(app): Minor bugs
1 parent 785ef09 commit 5df44ab

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

apps/inheritance_app/inheritance_decrypt_data.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
#include "inheritance_api.h"
7373
#include "inheritance_context.h"
7474
#include "inheritance_priv.h"
75+
#include "memzero.h"
76+
#include "pb.h"
7577
#include "pb_decode.h"
7678
#include "pb_encode.h"
7779
#include "reconstruct_wallet_flow.h"
@@ -234,7 +236,7 @@ STATIC bool inheritance_handle_initiate_query(inheritance_query_t *query) {
234236
query->decrypt.initiate.wallet_id,
235237
WALLET_ID_SIZE);
236238
inheritance_result_t result =
237-
init_inheritance_result(INHERITANCE_RESULT_ENCRYPT_TAG);
239+
init_inheritance_result(INHERITANCE_RESULT_DECRYPT_TAG);
238240
result.decrypt.which_response =
239241
INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_CONFIRMATION_TAG;
240242
inheritance_send_result(&result);
@@ -254,15 +256,15 @@ static bool decode_inheritance_encrypted_data(
254256

255257
// zeroise for safety from garbage in the query reference
256258
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));
258260

259261
/* Create a stream that reads from the buffer. */
260262
pb_istream_t stream = pb_istream_from_buffer(data, data_size);
261263

262264
/* Now we are ready to decode the message. */
263265
bool status = pb_decode(
264266
&stream,
265-
INHERITANCE_DECRYPT_DATA_WITH_PIN_DECRYPTED_DATA_STRUCTURE_FIELDS,
267+
INHERITANCE_DECRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_FIELDS,
266268
encrypted_data);
267269

268270
/* Send error to host if status is false*/
@@ -275,8 +277,7 @@ static bool decode_inheritance_encrypted_data(
275277
}
276278

277279
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};
280281
inheritance_result_t response =
281282
init_inheritance_result(INHERITANCE_RESULT_DECRYPT_TAG);
282283
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) {
309310
size += chunk->size;
310311

311312
// Send chunk ack to host
312-
response.encrypt.which_response =
313+
response.decrypt.which_response =
313314
INHERITANCE_DECRYPT_DATA_WITH_PIN_RESPONSE_DATA_ACCEPTED_TAG;
314315
response.decrypt.data_accepted.has_chunk_ack = true;
315316
response.decrypt.data_accepted.chunk_ack.chunk_index = payload->chunk_index;
@@ -336,10 +337,8 @@ static bool get_pb_encoded_buffer(
336337
if (NULL == result || NULL == buffer || NULL == bytes_written_out) {
337338
return false;
338339
}
339-
/* Create a stream that will write to our buffer. */
340340
pb_ostream_t stream = pb_ostream_from_buffer(buffer, max_buffer_len);
341341

342-
/* Now we are ready to encode the message! */
343342
bool status = pb_encode(
344343
&stream,
345344
INHERITANCE_DECRYPT_DATA_WITH_PIN_DECRYPTED_DATA_STRUCTURE_FIELDS,
@@ -355,19 +354,17 @@ static bool get_pb_encoded_buffer(
355354
static bool inheritance_send_in_chunks(inheritance_query_t *query,
356355
const uint8_t *buffer,
357356
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);
361358
size_t remaining_size = (size_t)buffer_len;
362359
size_t offset = 0;
363360
inheritance_result_t result =
364361
init_inheritance_result(INHERITANCE_RESULT_DECRYPT_TAG);
365362
result.decrypt.which_response =
366363
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;
368365
result.decrypt.decrypted_data.chunk_payload.total_chunks = total_count;
369366

370-
for (int index = 0; index < total_count; index++) {
367+
for (*index = 0; *index < total_count; (*index)++) {
371368
if (!inheritance_get_query(query, INHERITANCE_QUERY_DECRYPT_TAG) ||
372369
!check_which_request(
373370
query,
@@ -376,7 +373,7 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
376373
}
377374
// chunk_payload validation checks
378375
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) {
380377
return false;
381378
}
382379
size_t chunk_size = (remaining_size > DECRYPTED_CHUNK_SIZE)
@@ -391,7 +388,6 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
391388
result.decrypt.decrypted_data.chunk_payload.chunk.size = chunk_size;
392389
inheritance_send_result(&result);
393390
offset += chunk_size;
394-
result.decrypt.decrypted_data.chunk_payload.chunk_index++;
395391
if (remaining_size == 0) {
396392
break;
397393
}
@@ -401,27 +397,20 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
401397

402398
static bool send_decrypted_data(inheritance_query_t *query) {
403399
uint8_t
404-
buffer[INHERITANCE_DECRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_SIZE] =
400+
buffer[INHERITANCE_DECRYPT_DATA_WITH_PIN_DECRYPTED_DATA_STRUCTURE_SIZE] =
405401
{0};
406402
size_t bytes_encoded = 0;
407403
if (!get_pb_encoded_buffer(&decryption_context->response_payload,
408404
buffer,
409405
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)) {
414408
return false;
415409
}
416410
return true;
417411
}
418412

419413
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);
425414
return session_aes_decrypt(decryption_context->encrypted_data.data.bytes,
426415
&decryption_context->encrypted_data.data.size) ==
427416
SESSION_DECRYPT_PACKET_SUCCESS;

0 commit comments

Comments
 (0)