Skip to content

Commit f492a30

Browse files
committed
getting-started: Reduce RSA sign stack usage
On targets with limited stack available to the main thread, our example can run out of stack space when doing RSA encryption. This can lead to stack overflows which cause undefined behavior. Move the RSA key and signature to static RAM instead of stack to reduce the stack footprint of message signing done by the example.
1 parent 58ee0a9 commit f492a30

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

getting-started/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ static void sign_a_message_using_rsa(void)
142142
{
143143
psa_status_t status;
144144
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
145-
uint8_t key[] = RSA_KEY;
145+
static const uint8_t key[] = RSA_KEY;
146146
uint8_t hash[] = "INPUT_FOR_SIGN";
147-
uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
147+
/* 'signature' is static to reduce stack usage. */
148+
static uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
148149
size_t signature_length;
149150
psa_key_handle_t handle;
150151

0 commit comments

Comments
 (0)