@@ -240,7 +240,11 @@ static bool serialize_packet(void);
240
240
static bool encrypt_packet (void );
241
241
242
242
/**
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.
244
248
*/
245
249
static bool get_pb_encoded_buffer (
246
250
const inheritance_encrypt_data_with_pin_encrypted_data_structure_t * result ,
@@ -249,7 +253,9 @@ static bool get_pb_encoded_buffer(
249
253
size_t * bytes_written_out );
250
254
251
255
/**
252
- * @brief
256
+ * @brief Sends input buffer to host in chunks.
257
+ *
258
+ * @return true if chunking successful, false otherwise.
253
259
*/
254
260
static bool inheritance_send_in_chunks (inheritance_query_t * query ,
255
261
const uint8_t * buffer ,
@@ -363,20 +369,16 @@ static bool decode_inheritance_plain_data(
363
369
return false;
364
370
}
365
371
366
- // zeroise for safety from garbage in the query reference
367
372
memzero (plain_data ,
368
373
sizeof (inheritance_encrypt_data_with_pin_plain_data_structure_t ));
369
374
370
- /* Create a stream that reads from the buffer. */
371
375
pb_istream_t stream = pb_istream_from_buffer (data , data_size );
372
376
373
- /* Now we are ready to decode the message. */
374
377
bool status =
375
378
pb_decode (& stream ,
376
379
INHERITANCE_ENCRYPT_DATA_WITH_PIN_PLAIN_DATA_STRUCTURE_FIELDS ,
377
380
plain_data );
378
381
379
- /* Send error to host if status is false*/
380
382
if (false == status ) {
381
383
inheritance_send_error (ERROR_COMMON_ERROR_CORRUPT_DATA_TAG ,
382
384
ERROR_DATA_FLOW_DECODING_FAILED );
@@ -386,8 +388,7 @@ static bool decode_inheritance_plain_data(
386
388
}
387
389
388
390
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 };
391
392
inheritance_result_t response =
392
393
init_inheritance_result (INHERITANCE_RESULT_ENCRYPT_TAG );
393
394
const inheritance_encrypt_data_with_pin_plain_data_t * plain_data =
@@ -605,10 +606,8 @@ static bool get_pb_encoded_buffer(
605
606
if (NULL == result || NULL == buffer || NULL == bytes_written_out ) {
606
607
return false;
607
608
}
608
- /* Create a stream that will write to our buffer. */
609
609
pb_ostream_t stream = pb_ostream_from_buffer (buffer , max_buffer_len );
610
610
611
- /* Now we are ready to encode the message! */
612
611
bool status = pb_encode (
613
612
& stream ,
614
613
INHERITANCE_ENCRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_FIELDS ,
@@ -624,19 +623,17 @@ static bool get_pb_encoded_buffer(
624
623
static bool inheritance_send_in_chunks (inheritance_query_t * query ,
625
624
const uint8_t * buffer ,
626
625
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 );
630
627
size_t remaining_size = (size_t )buffer_len ;
631
628
size_t offset = 0 ;
632
629
inheritance_result_t result =
633
630
init_inheritance_result (INHERITANCE_RESULT_ENCRYPT_TAG );
634
631
result .encrypt .which_response =
635
632
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 ;
637
634
result .encrypt .encrypted_data .chunk_payload .total_chunks = total_count ;
638
635
639
- for (int index = 0 ; index < total_count ; index ++ ) {
636
+ for (* index = 0 ; * index < total_count ; ( * index ) ++ ) {
640
637
if (!inheritance_get_query (query , INHERITANCE_QUERY_ENCRYPT_TAG ) ||
641
638
!check_which_request (
642
639
query ,
@@ -645,7 +642,7 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
645
642
}
646
643
// chunk_payload validation checks
647
644
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 ) {
649
646
return false;
650
647
}
651
648
size_t chunk_size = (remaining_size > ENCRYPTED_CHUNK_SIZE )
@@ -660,7 +657,6 @@ static bool inheritance_send_in_chunks(inheritance_query_t *query,
660
657
result .encrypt .encrypted_data .chunk_payload .chunk .size = chunk_size ;
661
658
inheritance_send_result (& result );
662
659
offset += chunk_size ;
663
- result .encrypt .encrypted_data .chunk_payload .chunk_index ++ ;
664
660
if (remaining_size == 0 ) {
665
661
break ;
666
662
}
@@ -676,10 +672,8 @@ static bool send_encrypted_data(inheritance_query_t *query) {
676
672
if (!get_pb_encoded_buffer (& encryption_context -> payload ,
677
673
buffer ,
678
674
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 )) {
683
677
return false;
684
678
}
685
679
return true;
0 commit comments