Skip to content

Commit a93f6f8

Browse files
committed
fix(app): Generic local send in chunks
1 parent 8367c76 commit a93f6f8

File tree

4 files changed

+46
-44
lines changed

4 files changed

+46
-44
lines changed

apps/inheritance_app/inheritance_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void inheritance_send_error(pb_size_t which_error, uint32_t error_code) {
172172

173173
void inheritance_send_result(const inheritance_result_t *result) {
174174
// TODO: Set the options file for all
175-
uint8_t buffer[1700] = {0};
175+
uint8_t buffer[INHERITANCE_RESULT_SIZE] = {0};
176176
size_t bytes_encoded = 0;
177177
ASSERT(encode_inheritance_result(
178178
result, buffer, sizeof(buffer), &bytes_encoded));

apps/inheritance_app/inheritance_encrypt_data.c

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,9 @@ static bool get_pb_encoded_buffer(
251251
/**
252252
* @brief
253253
*/
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);
258257
/**
259258
* @brief Encrypts the data and sends the result.
260259
*
@@ -527,22 +526,23 @@ static bool encrypt_message_data(void) {
527526
}
528527

529528
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++] =
532532
encryption_context->data_count;
533533
pb_size_t index = 0;
534534

535535
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,
538538
0x00, ///< TODO: take this from sdk
539539
encryption_context->data[index].encrypted_data_size,
540540
encryption_context->data[index].encrypted_data);
541541
}
542542

543543
// 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,
546546
INHERITANCE_PIN_TAG,
547547
encryption_context->data[index].encrypted_data_size,
548548
encryption_context->data[index].encrypted_data);
@@ -552,8 +552,8 @@ static bool serialize_packet(void) {
552552

553553
static bool encrypt_packet(void) {
554554
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)) {
557557
return false;
558558
}
559559

@@ -621,30 +621,13 @@ static bool get_pb_encoded_buffer(
621621
return status;
622622
}
623623

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;
648631
size_t offset = 0;
649632
inheritance_result_t result =
650633
init_inheritance_result(INHERITANCE_RESULT_ENCRYPT_TAG);
@@ -660,8 +643,9 @@ static bool send_encrypted_data(inheritance_query_t *query) {
660643
INHERITANCE_ENCRYPT_DATA_WITH_PIN_REQUEST_ENCRYPTED_DATA_REQUEST_TAG)) {
661644
return false;
662645
}
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) {
665649
return false;
666650
}
667651
size_t chunk_size = (remaining_size > ENCRYPTED_CHUNK_SIZE)
@@ -677,7 +661,26 @@ static bool send_encrypted_data(inheritance_query_t *query) {
677661
inheritance_send_result(&result);
678662
offset += chunk_size;
679663
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;
681684
}
682685
return true;
683686
}

apps/inheritance_app/inheritance_priv.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ typedef struct {
8080
uint8_t wallet_id[WALLET_ID_SIZE];
8181
inheritance_encrypt_data_with_pin_plain_data_structure_t plain_data;
8282
uint8_t pin_value[MAX_PIN_SIZE];
83-
uint8_t packet[INHERITANCE_PACKET_MAX_SIZE];
84-
uint16_t packet_size;
83+
inheritance_encrypt_data_with_pin_encrypted_data_structure_t payload;
8584
} inheritance_encryption_context_t;
8685

8786
typedef struct {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
inheritance.DecryptDataWithPinMessagesResponse.plain_data type:FT_STATIC max_count:5 fixed_length:false
2-
inheritance.DecryptDataWithPinInitiateRequest.encrypted_data type:FT_STATIC max_size:6000 fixed_length:false
1+
inheritance.DecryptDataWithPinMessagesResponse.plain_data type:FT_STATIC max_count:1 fixed_length:false
2+
inheritance.DecryptDataWithPinInitiateRequest.encrypted_data type:FT_STATIC max_size:60 fixed_length:false
33
inheritance.DecryptDataWithPinInitiateRequest.wallet_id type:FT_STATIC max_size:32 fixed_length:true

0 commit comments

Comments
 (0)