@@ -255,34 +255,37 @@ static void psa_mac_operation(void)
255
255
}
256
256
257
257
case PSA_MAC_UPDATE : {
258
-
259
258
uint8_t * input_buffer = NULL ;
260
259
size_t data_remaining = msg .in_size [1 ];
261
260
size_t allocation_size = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
262
261
size_t size_to_read = 0 ;
263
262
264
- input_buffer = mbedtls_calloc (1 , allocation_size );
265
- if (input_buffer == NULL ) {
266
- psa_mac_abort (msg .rhandle );
267
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
268
- } else {
269
- while (data_remaining > 0 ) {
270
- size_to_read = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
271
- bytes_read = psa_read (msg .handle , 1 , input_buffer , size_to_read );
272
-
273
- if (bytes_read != size_to_read ) {
274
- SPM_PANIC ("SPM read length mismatch" );
263
+ if (allocation_size > 0 ) {
264
+ input_buffer = mbedtls_calloc (1 , allocation_size );
265
+ if (input_buffer == NULL ) {
266
+ psa_mac_abort (msg .rhandle );
267
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
268
+ } else {
269
+ while (data_remaining > 0 ) {
270
+ size_to_read = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
271
+
272
+ bytes_read = psa_read (msg .handle , 1 , input_buffer , size_to_read );
273
+ if (bytes_read != size_to_read ) {
274
+ SPM_PANIC ("SPM read length mismatch" );
275
+ }
276
+
277
+ status = psa_mac_update (msg .rhandle , input_buffer , bytes_read );
278
+ // stop on error
279
+ if (status != PSA_SUCCESS ) {
280
+ break ;
281
+ }
282
+ data_remaining = data_remaining - bytes_read ;
275
283
}
276
284
277
- status = psa_mac_update (msg .rhandle , input_buffer , bytes_read );
278
- // stop on error
279
- if (status != PSA_SUCCESS ) {
280
- break ;
281
- }
282
- data_remaining = data_remaining - bytes_read ;
285
+ mbedtls_free (input_buffer );
283
286
}
284
-
285
- mbedtls_free ( input_buffer );
287
+ } else {
288
+ status = psa_mac_update ( msg . rhandle , input_buffer , allocation_size );
286
289
}
287
290
288
291
if (status != PSA_SUCCESS ) {
@@ -293,25 +296,30 @@ static void psa_mac_operation(void)
293
296
}
294
297
295
298
case PSA_MAC_SIGN_FINISH : {
296
- size_t mac_size = 0 ;
297
- bytes_read = psa_read (msg .handle , 1 , & mac_size ,
298
- msg .in_size [1 ]);
299
+ uint8_t * mac = NULL ;
300
+ size_t mac_size = 0 , mac_length = 0 ;
301
+
302
+ bytes_read = psa_read (msg .handle , 1 , & mac_size , msg .in_size [1 ]);
299
303
if (bytes_read != msg .in_size [1 ]) {
300
304
SPM_PANIC ("SPM read length mismatch" );
301
305
}
302
306
303
- size_t mac_length = 0 ;
304
- uint8_t * mac = mbedtls_calloc (1 , mac_size );
305
- if (mac == NULL ) {
306
- psa_mac_abort (msg .rhandle );
307
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
308
- } else {
307
+ if (mac_size > 0 ) {
308
+ mac = mbedtls_calloc (1 , mac_size );
309
+ if (mac == NULL ) {
310
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
311
+ }
312
+ }
313
+
314
+ if (status == PSA_SUCCESS ) {
309
315
status = psa_mac_sign_finish (msg .rhandle , mac , mac_size , & mac_length );
310
316
if (status == PSA_SUCCESS ) {
311
317
psa_write (msg .handle , 0 , mac , mac_length );
312
318
psa_write (msg .handle , 1 , & mac_length , sizeof (mac_length ));
313
319
}
314
320
mbedtls_free (mac );
321
+ } else {
322
+ psa_mac_abort (msg .rhandle );
315
323
}
316
324
317
325
mbedtls_free (msg .rhandle );
@@ -320,26 +328,31 @@ static void psa_mac_operation(void)
320
328
}
321
329
322
330
case PSA_MAC_VERIFY_FINISH : {
331
+ uint8_t * mac = NULL ;
323
332
size_t mac_length = 0 ;
324
- bytes_read = psa_read (msg .handle , 1 , & mac_length ,
325
- msg .in_size [1 ]);
326
- if (bytes_read != msg .in_size [1 ] ||
327
- mac_length != msg .in_size [2 ]) {
333
+
334
+ bytes_read = psa_read (msg .handle , 1 , & mac_length , msg .in_size [1 ]);
335
+ if (bytes_read != msg .in_size [1 ] || mac_length != msg .in_size [2 ]) {
328
336
SPM_PANIC ("SPM read length mismatch" );
329
337
}
330
338
331
- uint8_t * mac = mbedtls_calloc (1 , mac_length );
332
- if (mac == NULL ) {
333
- psa_mac_abort (msg .rhandle );
334
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
335
- } else {
336
- bytes_read = psa_read (msg .handle , 2 , mac , msg .in_size [2 ]);
337
- if (bytes_read != msg .in_size [2 ]) {
338
- SPM_PANIC ("SPM read length mismatch" );
339
+ if (mac_length > 0 ) {
340
+ mac = mbedtls_calloc (1 , mac_length );
341
+ if (mac == NULL ) {
342
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
343
+ } else {
344
+ bytes_read = psa_read (msg .handle , 2 , mac , mac_length );
345
+ if (bytes_read != mac_length ) {
346
+ SPM_PANIC ("SPM read length mismatch" );
347
+ }
339
348
}
349
+ }
340
350
351
+ if (status == PSA_SUCCESS ) {
341
352
status = psa_mac_verify_finish (msg .rhandle , mac , mac_length );
342
353
mbedtls_free (mac );
354
+ } else {
355
+ psa_mac_abort (msg .rhandle );
343
356
}
344
357
345
358
mbedtls_free (msg .rhandle );
0 commit comments