Skip to content

Commit 98476a9

Browse files
author
Jamie C. Driver
committed
messaging: no need to pass buffer when replying with bytestring
The length of the bytestring is passed, so the buffer size can be inferred. Uses a stack buffer for small messages, allocating for larger replies.
1 parent a4998d7 commit 98476a9

11 files changed

+38
-40
lines changed

main/process.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ extern uint8_t macid[6];
4242
// The 'id' we make using that mac-id
4343
static char jade_id[16];
4444

45+
#define JADE_MSG_REPLY_LEN 256
46+
#define JADE_MSG_REPLY_OVERHEAD 64
47+
4548
#ifdef CONFIG_HEAP_TRACING
4649

4750
#include <esp_heap_trace.h>
@@ -603,16 +606,26 @@ void jade_process_reject_message_ex(const cbor_msg_t ctx, int code, const char*
603606
void jade_process_reject_message(jade_process_t* process, int code, const char* message)
604607
{
605608
if (HAS_CURRENT_MESSAGE(process)) {
606-
uint8_t buf[256];
609+
uint8_t buf[JADE_MSG_REPLY_LEN];
607610
jade_process_reject_message_ex(process->ctx, code, message, NULL, 0, buf, sizeof(buf));
608611
} else {
609612
JADE_LOGW("Ignoring attempt to reject 'no-message'");
610613
}
611614
}
612615

613-
void jade_process_reply_to_message_bytes(
614-
cbor_msg_t ctx, const uint8_t* data, const size_t datalen, uint8_t* buffer, const size_t buflen)
616+
void jade_process_reply_to_message_bytes(const cbor_msg_t ctx, const uint8_t* data, const size_t datalen)
615617
{
618+
// Avoid allocating for small replies
619+
uint8_t buf[JADE_MSG_REPLY_LEN];
620+
uint8_t* buffer = buf;
621+
size_t buflen = sizeof(buf);
622+
623+
if (datalen > JADE_MSG_REPLY_LEN - JADE_MSG_REPLY_OVERHEAD) {
624+
buflen = datalen + JADE_MSG_REPLY_OVERHEAD;
625+
JADE_ASSERT(buflen > sizeof(buf));
626+
buffer = JADE_MALLOC(buflen);
627+
}
628+
616629
CborEncoder root_encoder;
617630
cbor_encoder_init(&root_encoder, buffer, buflen, 0);
618631

@@ -631,9 +644,14 @@ void jade_process_reply_to_message_bytes(
631644
cberr = cbor_encoder_close_container(&root_encoder, &root_map_encoder);
632645
JADE_ASSERT(cberr == CborNoError);
633646
jade_process_push_out_message(buffer, cbor_encoder_get_buffer_size(&root_encoder, buffer), ctx.source);
647+
648+
if (buffer != buf) {
649+
// Allocated buffer
650+
free(buffer);
651+
}
634652
}
635653

636-
void jade_process_reply_to_message_bytes_sequence(cbor_msg_t ctx, const size_t seqnum, const size_t seqlen,
654+
void jade_process_reply_to_message_bytes_sequence(const cbor_msg_t ctx, const size_t seqnum, const size_t seqlen,
637655
const uint8_t* data, const size_t datalen, uint8_t* buffer, const size_t buflen)
638656
{
639657
CborEncoder root_encoder;

main/process.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void jade_process_push_out_message(const uint8_t* data, size_t length, jade_msg_
9696
void jade_process_reply_to_message_result_with_id(const char* id, uint8_t* output, size_t output_size,
9797
jade_msg_source_t source, const void* cbctx, cbor_encoder_fn_t cb);
9898
void jade_process_reply_to_message_result(
99-
const cbor_msg_t ctx, uint8_t* output, size_t output_size, const void* cbctx, cbor_encoder_fn_t cb);
99+
cbor_msg_t ctx, uint8_t* output, size_t output_size, const void* cbctx, cbor_encoder_fn_t cb);
100100
void jade_process_reply_to_message_ok(jade_process_t* process);
101101
void jade_process_reply_to_message_fail(jade_process_t* process);
102102
void jade_process_reply_to_message_ex(jade_msg_source_t source, const uint8_t* reply_payload, size_t payload_len);
@@ -116,8 +116,7 @@ void cbor_result_string_cb(const void* ctx, CborEncoder* container);
116116
void cbor_result_boolean_cb(const void* ctx, CborEncoder* container);
117117
void cbor_result_uint64_cb(const void* ctx, CborEncoder* container);
118118

119-
void jade_process_reply_to_message_bytes(
120-
cbor_msg_t ctx, const uint8_t* data, size_t datalen, uint8_t* buffer, size_t buflen);
119+
void jade_process_reply_to_message_bytes(cbor_msg_t ctx, const uint8_t* data, size_t datalen);
121120
void jade_process_reply_to_message_bytes_sequence(cbor_msg_t ctx, const size_t seqnum, const size_t seqlen,
122121
const uint8_t* data, const size_t datalen, uint8_t* buffer, const size_t buflen);
123122

main/process/debug_scan_qr.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#ifdef CONFIG_DEBUG_MODE
1212

13-
static const size_t QR_CBOR_OVERHEAD = 64;
14-
1513
typedef struct {
1614
jade_process_t* process;
1715
bool check_qr; // check captured image is a valid qr code
@@ -96,11 +94,7 @@ static bool return_image_data(const size_t width, const size_t height, const uin
9694
}
9795

9896
// All good, reply with the compressed image data
99-
const size_t buflen = compressed_len + QR_CBOR_OVERHEAD;
100-
uint8_t* buffer = JADE_MALLOC_PREFER_SPIRAM(buflen);
101-
JADE_LOGI("Trying to send compressed captured image data, message buffer len: %u", buflen);
102-
jade_process_reply_to_message_bytes(info->process->ctx, compressed, compressed_len, buffer, buflen);
103-
free(buffer);
97+
jade_process_reply_to_message_bytes(info->process->ctx, compressed, compressed_len);
10498

10599
// Free the input message (to signal that we have been called and sent the reply)
106100
jade_process_free_current_message(info->process);
@@ -184,9 +178,7 @@ void debug_scan_qr_process(void* process_ptr)
184178
}
185179

186180
// Reply with the decoded data (empty if failed)
187-
uint8_t buf[512]; // sufficient for all existing test cases
188-
const bytes_info_t bytes_info = { .data = qr_data.data, .size = qr_data.len };
189-
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &bytes_info, cbor_result_bytes_cb);
181+
jade_process_reply_to_message_bytes(process->ctx, qr_data.data, qr_data.len);
190182
JADE_LOGI("Success");
191183

192184
cleanup:

main/process/get_blinding_factor.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ void get_blinding_factor_process(void* process_ptr)
6464
goto cleanup;
6565
}
6666

67-
uint8_t buffer[256];
68-
jade_process_reply_to_message_bytes(process->ctx, blinding_factor, bf_len, buffer, sizeof(buffer));
67+
jade_process_reply_to_message_bytes(process->ctx, blinding_factor, bf_len);
6968
JADE_LOGI("Success");
7069

7170
cleanup:

main/process/get_blinding_key.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ void get_blinding_key_process(void* process_ptr)
4040
goto cleanup;
4141
}
4242

43-
uint8_t buffer[256];
44-
jade_process_reply_to_message_bytes(
45-
process->ctx, public_blinding_key, sizeof(public_blinding_key), buffer, sizeof(buffer));
43+
jade_process_reply_to_message_bytes(process->ctx, public_blinding_key, sizeof(public_blinding_key));
4644
JADE_LOGI("Success");
4745

4846
cleanup:

main/process/get_identity_pubkey.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ void get_identity_pubkey_process(void* process_ptr)
5959
}
6060

6161
// Return pubkey to caller
62-
uint8_t buffer[256];
63-
jade_process_reply_to_message_bytes(process->ctx, pubkey, sizeof(pubkey), buffer, sizeof(buffer));
62+
jade_process_reply_to_message_bytes(process->ctx, pubkey, sizeof(pubkey));
6463

6564
JADE_LOGI("Success");
6665

main/process/get_identity_shared_key.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ void get_identity_shared_key_process(void* process_ptr)
5858
}
5959

6060
// Return pubkey to caller
61-
uint8_t buffer[256];
62-
jade_process_reply_to_message_bytes(process->ctx, shared_key, sizeof(shared_key), buffer, sizeof(buffer));
61+
jade_process_reply_to_message_bytes(process->ctx, shared_key, sizeof(shared_key));
6362

6463
JADE_LOGI("Success");
6564

main/process/get_master_blinding_key.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ void get_master_blinding_key_process(void* process_ptr)
4343
// So we only return the relevant slice of the data.
4444
JADE_STATIC_ASSERT(sizeof(keychain_get()->master_unblinding_key) == HMAC_SHA512_LEN);
4545

46-
uint8_t buffer[256];
47-
jade_process_reply_to_message_bytes(process->ctx, keychain_get()->master_unblinding_key + HMAC_SHA512_LEN / 2,
48-
HMAC_SHA512_LEN / 2, buffer, sizeof(buffer));
46+
jade_process_reply_to_message_bytes(
47+
process->ctx, keychain_get()->master_unblinding_key + HMAC_SHA512_LEN / 2, HMAC_SHA512_LEN / 2);
4948
JADE_LOGI("Success");
5049

5150
cleanup:

main/process/get_shared_nonce.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ void get_shared_nonce_process(void* process_ptr)
107107
jade_process_reply_to_message_result(process->ctx, buf, sizeof(buf), &data, reply_nonce_and_pubkey);
108108
} else {
109109
// Just shared blinding nonce alone (default/legacy behaviour)
110-
uint8_t buf[128];
111-
jade_process_reply_to_message_bytes(process->ctx, shared_nonce, sizeof(shared_nonce), buf, sizeof(buf));
110+
jade_process_reply_to_message_bytes(process->ctx, shared_nonce, sizeof(shared_nonce));
112111
}
113112
JADE_LOGI("Success");
114113

main/process/sign_message.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ void sign_message_process(void* process_ptr)
253253
// convert normal EC signatures to use the new/improved message flow.
254254
size_t ae_host_entropy_len = 0;
255255
const uint8_t* ae_host_entropy = NULL;
256-
uint8_t buf[256];
257256
if (use_ae_signatures) {
258257
JADE_ASSERT(ae_host_commitment);
259258
JADE_ASSERT(ae_host_commitment_len == WALLY_HOST_COMMITMENT_LEN);
@@ -271,8 +270,7 @@ void sign_message_process(void* process_ptr)
271270
}
272271

273272
// Return signer commitment to caller
274-
jade_process_reply_to_message_bytes(
275-
process->ctx, ae_signer_commitment, sizeof(ae_signer_commitment), buf, sizeof(buf));
273+
jade_process_reply_to_message_bytes(process->ctx, ae_signer_commitment, sizeof(ae_signer_commitment));
276274

277275
// Await 'get_signature' message containing host entropy
278276
jade_process_load_in_message(process, true);
@@ -303,6 +301,8 @@ void sign_message_process(void* process_ptr)
303301
}
304302
JADE_ASSERT(written < sizeof(sig_output));
305303
JADE_ASSERT(sig_output[written - 1] == '\0');
304+
305+
uint8_t buf[256];
306306
jade_process_reply_to_message_result(
307307
process->ctx, buf, sizeof(buf), (const char*)sig_output, cbor_result_string_cb);
308308

0 commit comments

Comments
 (0)