Skip to content

Commit 4a4af57

Browse files
author
Nir Sonnenschein
committed
allow calling hash or mac on large buffers without large memory allocation
use a fixed size buffer for large inputs to process the input in multiple smaller pieces.
1 parent 31da50e commit 4a4af57

File tree

1 file changed

+68
-21
lines changed

1 file changed

+68
-21
lines changed

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

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#define mbedtls_free free
1919
#endif
2020

21+
// ---------------------------------- Macros -----------------------------------
22+
#if !defined(MIN)
23+
#define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
24+
#endif
25+
2126
// -------------------------------- Structures ---------------------------------
2227
typedef struct psa_spm_hash_clone_s {
2328
int32_t partition_id;
@@ -28,6 +33,12 @@ typedef struct psa_spm_hash_clone_s {
2833
// ---------------------------------- Globals ----------------------------------
2934
static int psa_spm_init_refence_counter = 0;
3035

36+
/* maximal memoty allocation for reading large hash ort mac input buffers.
37+
the data will be read in chunks of size */
38+
#if !defined (MAX_DATA_CHUNK_SIZE_IN_BYTES)
39+
#define MAX_DATA_CHUNK_SIZE_IN_BYTES 400
40+
#endif
41+
3142
#ifndef MAX_CONCURRENT_HASH_CLONES
3243
#define MAX_CONCURRENT_HASH_CLONES 2
3344
#endif
@@ -216,24 +227,42 @@ static void psa_mac_operation(void)
216227
}
217228

218229
case PSA_MAC_UPDATE: {
219-
uint8_t *input_ptr = mbedtls_calloc(1, msg.in_size[1]);
220-
if (input_ptr == NULL) {
230+
231+
uint8_t * input_buffer = NULL;
232+
size_t data_remaining = msg.in_size[1];
233+
size_t allocation_size = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
234+
size_t size_to_read = 0;
235+
236+
input_buffer = mbedtls_calloc(1, allocation_size);
237+
if (input_buffer == NULL) {
221238
status = PSA_ERROR_INSUFFICIENT_MEMORY;
222239
break;
223240
}
224241

225-
bytes_read = psa_read(msg.handle, 1, input_ptr,
226-
msg.in_size[1]);
242+
while (data_remaining > 0)
243+
{
244+
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
245+
bytes_read = psa_read(msg.handle, 1, input_buffer,
246+
size_to_read);
227247

228-
if (bytes_read != msg.in_size[1]) {
229-
SPM_PANIC("SPM read length mismatch");
230-
}
248+
if (bytes_read != size_to_read) {
249+
SPM_PANIC("SPM read length mismatch");
250+
}
231251

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

236-
mbedtls_free(input_ptr);
237266
break;
238267
}
239268

@@ -363,23 +392,41 @@ static void psa_hash_operation(void)
363392
}
364393

365394
case PSA_HASH_UPDATE: {
366-
uint8_t *input_ptr = mbedtls_calloc(1, msg.in_size[1]);
367-
if (input_ptr == NULL) {
395+
uint8_t * input_buffer = NULL;
396+
size_t data_remaining = msg.in_size[1];
397+
size_t size_to_read = 0;
398+
size_t allocation_size = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
399+
400+
input_buffer = mbedtls_calloc(1, allocation_size);
401+
if (input_buffer == NULL) {
368402
status = PSA_ERROR_INSUFFICIENT_MEMORY;
369403
break;
370404
}
371405

372-
bytes_read = psa_read(msg.handle, 1, input_ptr,
373-
msg.in_size[1]);
406+
while (data_remaining > 0)
407+
{
408+
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
409+
bytes_read = psa_read(msg.handle, 1, input_buffer,
410+
size_to_read);
374411

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

379-
status = psa_hash_update(msg.rhandle,
380-
input_ptr,
381-
msg.in_size[1]);
382-
mbedtls_free(input_ptr);
428+
mbedtls_free(input_buffer);
429+
383430
break;
384431
}
385432

0 commit comments

Comments
 (0)