18
18
#define mbedtls_free free
19
19
#endif
20
20
21
+ // ---------------------------------- Macros -----------------------------------
22
+ #if !defined(MIN )
23
+ #define MIN ( a , b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
24
+ #endif
25
+
21
26
// -------------------------------- Structures ---------------------------------
22
27
typedef struct psa_spm_hash_clone_s {
23
28
int32_t partition_id ;
@@ -28,6 +33,12 @@ typedef struct psa_spm_hash_clone_s {
28
33
// ---------------------------------- Globals ----------------------------------
29
34
static int psa_spm_init_refence_counter = 0 ;
30
35
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
+
31
42
#ifndef MAX_CONCURRENT_HASH_CLONES
32
43
#define MAX_CONCURRENT_HASH_CLONES 2
33
44
#endif
@@ -216,24 +227,42 @@ static void psa_mac_operation(void)
216
227
}
217
228
218
229
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 ) {
221
238
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
222
239
break ;
223
240
}
224
241
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 );
227
247
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
+ }
231
251
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 );
235
265
236
- mbedtls_free (input_ptr );
237
266
break ;
238
267
}
239
268
@@ -363,23 +392,41 @@ static void psa_hash_operation(void)
363
392
}
364
393
365
394
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 ) {
368
402
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
369
403
break ;
370
404
}
371
405
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 );
374
411
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 ;
377
426
}
378
427
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
+
383
430
break ;
384
431
}
385
432
0 commit comments