Skip to content

Commit ed350dc

Browse files
author
Cruz Monrreal
authored
Merge pull request #9758 from NirSonnenschein/large_buffer_support_for_hash_and_mac
allow hash or mac on large buffers with less memory use
2 parents afeab0f + 1d4c280 commit ed350dc

File tree

1 file changed

+63
-20
lines changed

1 file changed

+63
-20
lines changed

components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#define mbedtls_free free
2424
#endif
2525

26+
// ---------------------------------- Macros -----------------------------------
27+
#if !defined(MIN)
28+
#define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
29+
#endif
30+
2631
// -------------------------------- Structures ---------------------------------
2732
typedef struct psa_spm_hash_clone_s {
2833
int32_t partition_id;
@@ -33,6 +38,12 @@ typedef struct psa_spm_hash_clone_s {
3338
// ---------------------------------- Globals ----------------------------------
3439
static int psa_spm_init_refence_counter = 0;
3540

41+
/* maximal memory allocation for reading large hash or mac input buffers.
42+
the data will be read in chunks of size */
43+
#if !defined (MAX_DATA_CHUNK_SIZE_IN_BYTES)
44+
#define MAX_DATA_CHUNK_SIZE_IN_BYTES 400
45+
#endif
46+
3647
#ifndef MAX_CONCURRENT_HASH_CLONES
3748
#define MAX_CONCURRENT_HASH_CLONES 2
3849
#endif
@@ -221,24 +232,40 @@ static void psa_mac_operation(void)
221232
}
222233

223234
case PSA_MAC_UPDATE: {
224-
uint8_t *input_ptr = mbedtls_calloc(1, msg.in_size[1]);
225-
if (input_ptr == NULL) {
235+
236+
uint8_t *input_buffer = NULL;
237+
size_t data_remaining = msg.in_size[1];
238+
size_t allocation_size = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
239+
size_t size_to_read = 0;
240+
241+
input_buffer = mbedtls_calloc(1, allocation_size);
242+
if (input_buffer == NULL) {
226243
status = PSA_ERROR_INSUFFICIENT_MEMORY;
227244
break;
228245
}
229246

230-
bytes_read = psa_read(msg.handle, 1, input_ptr,
231-
msg.in_size[1]);
247+
while (data_remaining > 0) {
248+
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
249+
bytes_read = psa_read(msg.handle, 1, input_buffer,
250+
size_to_read);
232251

233-
if (bytes_read != msg.in_size[1]) {
234-
SPM_PANIC("SPM read length mismatch");
252+
if (bytes_read != size_to_read) {
253+
SPM_PANIC("SPM read length mismatch");
254+
}
255+
256+
status = psa_mac_update(msg.rhandle,
257+
input_buffer,
258+
bytes_read);
259+
260+
// stop on error
261+
if (status != PSA_SUCCESS) {
262+
break;
263+
}
264+
data_remaining = data_remaining - bytes_read;
235265
}
236266

237-
status = psa_mac_update(msg.rhandle,
238-
input_ptr,
239-
msg.in_size[1]);
267+
mbedtls_free(input_buffer);
240268

241-
mbedtls_free(input_ptr);
242269
break;
243270
}
244271

@@ -368,23 +395,39 @@ static void psa_hash_operation(void)
368395
}
369396

370397
case PSA_HASH_UPDATE: {
371-
uint8_t *input_ptr = mbedtls_calloc(1, msg.in_size[1]);
372-
if (input_ptr == NULL) {
398+
uint8_t *input_buffer = NULL;
399+
size_t data_remaining = msg.in_size[1];
400+
size_t size_to_read = 0;
401+
size_t allocation_size = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
402+
403+
input_buffer = mbedtls_calloc(1, allocation_size);
404+
if (input_buffer == NULL) {
373405
status = PSA_ERROR_INSUFFICIENT_MEMORY;
374406
break;
375407
}
376408

377-
bytes_read = psa_read(msg.handle, 1, input_ptr,
378-
msg.in_size[1]);
409+
while (data_remaining > 0) {
410+
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
411+
bytes_read = psa_read(msg.handle, 1, input_buffer,
412+
size_to_read);
379413

380-
if (bytes_read != msg.in_size[1]) {
381-
SPM_PANIC("SPM read length mismatch");
414+
if (bytes_read != size_to_read) {
415+
SPM_PANIC("SPM read length mismatch");
416+
}
417+
418+
status = psa_hash_update(msg.rhandle,
419+
input_buffer,
420+
bytes_read);
421+
422+
// stop on error
423+
if (status != PSA_SUCCESS) {
424+
break;
425+
}
426+
data_remaining = data_remaining - bytes_read;
382427
}
383428

384-
status = psa_hash_update(msg.rhandle,
385-
input_ptr,
386-
msg.in_size[1]);
387-
mbedtls_free(input_ptr);
429+
mbedtls_free(input_buffer);
430+
388431
break;
389432
}
390433

0 commit comments

Comments
 (0)