@@ -251,10 +251,9 @@ static bool get_pb_encoded_buffer(
251
251
/**
252
252
* @brief
253
253
*/
254
- static bool encrypted_struct_to_byte_array (uint8_t * buffer ,
255
- size_t buffer_size ,
256
- size_t * bytes_encoded );
257
-
254
+ static bool inheritance_send_in_chunks (inheritance_query_t * query ,
255
+ const uint8_t * buffer ,
256
+ const size_t buffer_len );
258
257
/**
259
258
* @brief Encrypts the data and sends the result.
260
259
*
@@ -527,22 +526,23 @@ static bool encrypt_message_data(void) {
527
526
}
528
527
529
528
static bool serialize_packet (void ) {
530
- encryption_context -> packet_size = 0 ;
531
- encryption_context -> packet [encryption_context -> packet_size ++ ] =
529
+ encryption_context -> payload .encrypted_data .size = 0 ;
530
+ encryption_context -> payload .encrypted_data
531
+ .bytes [encryption_context -> payload .encrypted_data .size ++ ] =
532
532
encryption_context -> data_count ;
533
533
pb_size_t index = 0 ;
534
534
535
535
for (index = 0 ; index < encryption_context -> data_count - 1 ; index ++ ) {
536
- inheritance_fill_tlv (encryption_context -> packet ,
537
- & encryption_context -> packet_size ,
536
+ inheritance_fill_tlv (encryption_context -> payload . encrypted_data . bytes ,
537
+ & encryption_context -> payload . encrypted_data . size ,
538
538
0x00 , ///< TODO: take this from sdk
539
539
encryption_context -> data [index ].encrypted_data_size ,
540
540
encryption_context -> data [index ].encrypted_data );
541
541
}
542
542
543
543
// The last encrypted message is the PIN
544
- inheritance_fill_tlv (encryption_context -> packet ,
545
- & encryption_context -> packet_size ,
544
+ inheritance_fill_tlv (encryption_context -> payload . encrypted_data . bytes ,
545
+ & encryption_context -> payload . encrypted_data . size ,
546
546
INHERITANCE_PIN_TAG ,
547
547
encryption_context -> data [index ].encrypted_data_size ,
548
548
encryption_context -> data [index ].encrypted_data );
@@ -552,8 +552,8 @@ static bool serialize_packet(void) {
552
552
553
553
static bool encrypt_packet (void ) {
554
554
if (SESSION_ENCRYPT_PACKET_SUCCESS !=
555
- session_aes_encrypt (encryption_context -> packet ,
556
- & encryption_context -> packet_size )) {
555
+ session_aes_encrypt (encryption_context -> payload . encrypted_data . bytes ,
556
+ & encryption_context -> payload . encrypted_data . size )) {
557
557
return false;
558
558
}
559
559
@@ -621,30 +621,13 @@ static bool get_pb_encoded_buffer(
621
621
return status ;
622
622
}
623
623
624
- static bool encrypted_struct_to_byte_array (uint8_t * buffer ,
625
- size_t buffer_size ,
626
- size_t * bytes_encoded ) {
627
- inheritance_encrypt_data_with_pin_encrypted_data_structure_t encrypted_data =
628
- INHERITANCE_ENCRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_INIT_DEFAULT ;
629
- memcpy (encrypted_data .encrypted_data .bytes ,
630
- encryption_context -> packet ,
631
- encryption_context -> packet_size );
632
- encrypted_data .encrypted_data .size = encryption_context -> packet_size ;
633
- bool status = get_pb_encoded_buffer (
634
- & encrypted_data , buffer , buffer_size , bytes_encoded );
635
- return status ;
636
- }
637
-
638
- static bool send_encrypted_data (inheritance_query_t * query ) {
639
- uint8_t buffer [1700 ] = {0 };
640
- size_t bytes_encoded = 0 ;
641
- if (!encrypted_struct_to_byte_array (buffer , 1700 , & bytes_encoded )) {
642
- return false;
643
- }
644
- size_t total_count = ((bytes_encoded % ENCRYPTED_CHUNK_SIZE ) > 0 )
645
- ? (bytes_encoded / ENCRYPTED_CHUNK_SIZE ) + 1
646
- : (bytes_encoded / ENCRYPTED_CHUNK_SIZE );
647
- size_t remaining_size = (size_t )bytes_encoded ;
624
+ static bool inheritance_send_in_chunks (inheritance_query_t * query ,
625
+ const uint8_t * buffer ,
626
+ 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 );
630
+ size_t remaining_size = (size_t )buffer_len ;
648
631
size_t offset = 0 ;
649
632
inheritance_result_t result =
650
633
init_inheritance_result (INHERITANCE_RESULT_ENCRYPT_TAG );
@@ -660,8 +643,9 @@ static bool send_encrypted_data(inheritance_query_t *query) {
660
643
INHERITANCE_ENCRYPT_DATA_WITH_PIN_REQUEST_ENCRYPTED_DATA_REQUEST_TAG )) {
661
644
return false;
662
645
}
663
- // Add chunk_payload validation checks
664
- if (query -> encrypt .encrypted_data_request .has_chunk_ack == false) {
646
+ // chunk_payload validation checks
647
+ if (query -> encrypt .encrypted_data_request .has_chunk_ack == false ||
648
+ query -> encrypt .encrypted_data_request .chunk_ack .chunk_index != index ) {
665
649
return false;
666
650
}
667
651
size_t chunk_size = (remaining_size > ENCRYPTED_CHUNK_SIZE )
@@ -677,7 +661,26 @@ static bool send_encrypted_data(inheritance_query_t *query) {
677
661
inheritance_send_result (& result );
678
662
offset += chunk_size ;
679
663
result .encrypt .encrypted_data .chunk_payload .chunk_index ++ ;
680
- result .encrypt .encrypted_data .chunk_payload .total_chunks -- ;
664
+ if (remaining_size == 0 ) {
665
+ break ;
666
+ }
667
+ }
668
+ return true;
669
+ }
670
+
671
+ static bool send_encrypted_data (inheritance_query_t * query ) {
672
+ uint8_t
673
+ buffer [INHERITANCE_ENCRYPT_DATA_WITH_PIN_ENCRYPTED_DATA_STRUCTURE_SIZE ] =
674
+ {0 };
675
+ size_t bytes_encoded = 0 ;
676
+ if (!get_pb_encoded_buffer (& encryption_context -> payload ,
677
+ buffer ,
678
+ sizeof (buffer ),
679
+ & bytes_encoded )) {
680
+ return false;
681
+ }
682
+ if (!inheritance_send_in_chunks (query , buffer , bytes_encoded )) {
683
+ return false;
681
684
}
682
685
return true;
683
686
}
0 commit comments