23
23
#define mbedtls_free free
24
24
#endif
25
25
26
+ // ---------------------------------- Macros -----------------------------------
27
+ #if !defined(MIN )
28
+ #define MIN ( a , b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
29
+ #endif
30
+
26
31
// -------------------------------- Structures ---------------------------------
27
32
typedef struct psa_spm_hash_clone_s {
28
33
int32_t partition_id ;
@@ -33,6 +38,12 @@ typedef struct psa_spm_hash_clone_s {
33
38
// ---------------------------------- Globals ----------------------------------
34
39
static int psa_spm_init_refence_counter = 0 ;
35
40
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
+
36
47
#ifndef MAX_CONCURRENT_HASH_CLONES
37
48
#define MAX_CONCURRENT_HASH_CLONES 2
38
49
#endif
@@ -221,24 +232,40 @@ static void psa_mac_operation(void)
221
232
}
222
233
223
234
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 ) {
226
243
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
227
244
break ;
228
245
}
229
246
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 );
232
251
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 ;
235
265
}
236
266
237
- status = psa_mac_update (msg .rhandle ,
238
- input_ptr ,
239
- msg .in_size [1 ]);
267
+ mbedtls_free (input_buffer );
240
268
241
- mbedtls_free (input_ptr );
242
269
break ;
243
270
}
244
271
@@ -368,23 +395,39 @@ static void psa_hash_operation(void)
368
395
}
369
396
370
397
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 ) {
373
405
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
374
406
break ;
375
407
}
376
408
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 );
379
413
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 ;
382
427
}
383
428
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
+
388
431
break ;
389
432
}
390
433
0 commit comments