Skip to content

Commit 82910c4

Browse files
committed
chore: Review changes
1 parent a93f6f8 commit 82910c4

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

apps/inheritance_app/inheritance_encrypt_data.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ static bool serialize_packet(void);
240240
static bool encrypt_packet(void);
241241

242242
/**
243-
* @brief
243+
* @brief Creates a pb encoded buffer of @ref
244+
* inheritance_encrypt_data_with_pin_encrypted_data_structure_t to be sent to
245+
* host.
246+
*
247+
* @return true if encoding is successful, false otherwise.
244248
*/
245249
static bool get_pb_encoded_buffer(
246250
const inheritance_encrypt_data_with_pin_encrypted_data_structure_t *result,
@@ -249,7 +253,9 @@ static bool get_pb_encoded_buffer(
249253
size_t *bytes_written_out);
250254

251255
/**
252-
* @brief
256+
* @brief Sends input buffer to host in chunks.
257+
*
258+
* @return true if chunking successful, false otherwise.
253259
*/
254260
static bool inheritance_send_in_chunks(inheritance_query_t *query,
255261
const uint8_t *buffer,
@@ -363,20 +369,16 @@ static bool decode_inheritance_plain_data(
363369
return false;
364370
}
365371

366-
// zeroise for safety from garbage in the query reference
367372
memzero(plain_data,
368373
sizeof(inheritance_encrypt_data_with_pin_plain_data_structure_t));
369374

370-
/* Create a stream that reads from the buffer. */
371375
pb_istream_t stream = pb_istream_from_buffer(data, data_size);
372376

373-
/* Now we are ready to decode the message. */
374377
bool status =
375378
pb_decode(&stream,
376379
INHERITANCE_ENCRYPT_DATA_WITH_PIN_PLAIN_DATA_STRUCTURE_FIELDS,
377380
plain_data);
378381

379-
/* Send error to host if status is false*/
380382
if (false == status) {
381383
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
382384
ERROR_DATA_FLOW_DECODING_FAILED);
@@ -386,8 +388,7 @@ static bool decode_inheritance_plain_data(
386388
}
387389

388390
static bool inheritance_get_plain_data(inheritance_query_t *query) {
389-
uint8_t encoded_data[INHERITANCE_PACKET_MAX_SIZE] = {
390-
0}; ///< CONFIRM ENCODED DATA MAX SIZE
391+
uint8_t encoded_data[INHERITANCE_PACKET_MAX_SIZE] = {0};
391392
inheritance_result_t response =
392393
init_inheritance_result(INHERITANCE_RESULT_ENCRYPT_TAG);
393394
const inheritance_encrypt_data_with_pin_plain_data_t *plain_data =
@@ -605,10 +606,8 @@ static bool get_pb_encoded_buffer(
605606
if (NULL == result || NULL == buffer || NULL == bytes_written_out) {
606607
return false;
607608
}
608-
/* Create a stream that will write to our buffer. */
609609
pb_ostream_t stream = pb_ostream_from_buffer(buffer, max_buffer_len);
610610

611-
/* Now we are ready to encode the message! */
612611
bool status = pb_encode(
613612
&stream,
614613
INHERITANCE_ENCRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_FIELDS,
@@ -624,19 +623,17 @@ static bool get_pb_encoded_buffer(
624623
static bool inheritance_send_in_chunks(inheritance_query_t *query,
625624
const uint8_t *buffer,
626625
const size_t buffer_len) {
627-
size_t total_count = ((buffer_len % ENCRYPTED_CHUNK_SIZE) > 0)
628-
? (buffer_len / ENCRYPTED_CHUNK_SIZE) + 1
629-
: (buffer_len / ENCRYPTED_CHUNK_SIZE);
626+
size_t total_count = ((buffer_len + 1) / ENCRYPTED_CHUNK_SIZE);
630627
size_t remaining_size = (size_t)buffer_len;
631628
size_t offset = 0;
632629
inheritance_result_t result =
633630
init_inheritance_result(INHERITANCE_RESULT_ENCRYPT_TAG);
634631
result.encrypt.which_response =
635632
INHERITANCE_ENCRYPT_DATA_WITH_PIN_RESPONSE_ENCRYPTED_DATA_TAG;
636-
result.encrypt.encrypted_data.chunk_payload.chunk_index = 0;
633+
uint32_t *index = &result.encrypt.encrypted_data.chunk_payload.chunk_index;
637634
result.encrypt.encrypted_data.chunk_payload.total_chunks = total_count;
638635

639-
for (int index = 0; index < total_count; index++) {
636+
for (*index = 0; *index < total_count; (*index)++) {
640637
if (!inheritance_get_query(query, INHERITANCE_QUERY_ENCRYPT_TAG) ||
641638
!check_which_request(
642639
query,
@@ -645,7 +642,7 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
645642
}
646643
// chunk_payload validation checks
647644
if (query->encrypt.encrypted_data_request.has_chunk_ack == false ||
648-
query->encrypt.encrypted_data_request.chunk_ack.chunk_index != index) {
645+
query->encrypt.encrypted_data_request.chunk_ack.chunk_index != *index) {
649646
return false;
650647
}
651648
size_t chunk_size = (remaining_size > ENCRYPTED_CHUNK_SIZE)
@@ -660,7 +657,6 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
660657
result.encrypt.encrypted_data.chunk_payload.chunk.size = chunk_size;
661658
inheritance_send_result(&result);
662659
offset += chunk_size;
663-
result.encrypt.encrypted_data.chunk_payload.chunk_index++;
664660
if (remaining_size == 0) {
665661
break;
666662
}
@@ -676,10 +672,8 @@ static bool send_encrypted_data(inheritance_query_t *query) {
676672
if (!get_pb_encoded_buffer(&encryption_context->payload,
677673
buffer,
678674
sizeof(buffer),
679-
&bytes_encoded)) {
680-
return false;
681-
}
682-
if (!inheritance_send_in_chunks(query, buffer, bytes_encoded)) {
675+
&bytes_encoded) ||
676+
!inheritance_send_in_chunks(query, buffer, bytes_encoded)) {
683677
return false;
684678
}
685679
return true;

0 commit comments

Comments
 (0)