@@ -227,28 +227,30 @@ static void psa_mac_operation(void)
227
227
228
228
switch (psa_crypto .func ) {
229
229
case PSA_MAC_SIGN_SETUP : {
230
- if (!psa_crypto_access_control_is_handle_permitted (psa_crypto .handle ,
231
- msg .client_id )) {
230
+ if (psa_crypto_access_control_is_handle_permitted (psa_crypto .handle , msg .client_id )) {
231
+ status = psa_mac_sign_setup (msg .rhandle , psa_crypto .handle , psa_crypto .alg );
232
+ } else {
232
233
status = PSA_ERROR_INVALID_HANDLE ;
233
- break ;
234
234
}
235
235
236
- status = psa_mac_sign_setup (msg .rhandle ,
237
- psa_crypto .handle ,
238
- psa_crypto .alg );
236
+ if (status != PSA_SUCCESS ) {
237
+ mbedtls_free (msg .rhandle );
238
+ psa_set_rhandle (msg .handle , NULL );
239
+ }
239
240
break ;
240
241
}
241
242
242
243
case PSA_MAC_VERIFY_SETUP : {
243
- if (!psa_crypto_access_control_is_handle_permitted (psa_crypto .handle ,
244
- msg .client_id )) {
244
+ if (psa_crypto_access_control_is_handle_permitted (psa_crypto .handle , msg .client_id )) {
245
+ status = psa_mac_verify_setup (msg .rhandle , psa_crypto .handle , psa_crypto .alg );
246
+ } else {
245
247
status = PSA_ERROR_INVALID_HANDLE ;
246
- break ;
247
248
}
248
249
249
- status = psa_mac_verify_setup (msg .rhandle ,
250
- psa_crypto .handle ,
251
- psa_crypto .alg );
250
+ if (status != PSA_SUCCESS ) {
251
+ mbedtls_free (msg .rhandle );
252
+ psa_set_rhandle (msg .handle , NULL );
253
+ }
252
254
break ;
253
255
}
254
256
@@ -261,32 +263,32 @@ static void psa_mac_operation(void)
261
263
262
264
input_buffer = mbedtls_calloc (1 , allocation_size );
263
265
if (input_buffer == NULL ) {
266
+ psa_mac_abort (msg .rhandle );
264
267
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
265
- break ;
266
- }
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 );
267
272
268
- while (data_remaining > 0 ) {
269
- size_to_read = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
270
- bytes_read = psa_read (msg .handle , 1 , input_buffer ,
271
- size_to_read );
273
+ if (bytes_read != size_to_read ) {
274
+ SPM_PANIC ("SPM read length mismatch" );
275
+ }
272
276
273
- if (bytes_read != size_to_read ) {
274
- SPM_PANIC ("SPM read length mismatch" );
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 ,
278
- input_buffer ,
279
- bytes_read );
280
-
281
- // stop on error
282
- if (status != PSA_SUCCESS ) {
283
- break ;
284
- }
285
- data_remaining = data_remaining - bytes_read ;
285
+ mbedtls_free (input_buffer );
286
286
}
287
287
288
- mbedtls_free (input_buffer );
289
-
288
+ if (status != PSA_SUCCESS ) {
289
+ mbedtls_free (msg .rhandle );
290
+ psa_set_rhandle (msg .handle , NULL );
291
+ }
290
292
break ;
291
293
}
292
294
@@ -301,19 +303,19 @@ static void psa_mac_operation(void)
301
303
size_t mac_length = 0 ;
302
304
uint8_t * mac = mbedtls_calloc (1 , mac_size );
303
305
if (mac == NULL ) {
306
+ psa_mac_abort (msg .rhandle );
304
307
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
305
- break ;
306
- }
307
-
308
- status = psa_mac_sign_finish (msg .rhandle , mac , mac_size ,
309
- & mac_length );
310
- if (status == PSA_SUCCESS ) {
311
- psa_write (msg .handle , 0 , mac , mac_length );
312
- psa_write (msg .handle , 1 , & mac_length ,
313
- sizeof (mac_length ));
308
+ } else {
309
+ status = psa_mac_sign_finish (msg .rhandle , mac , mac_size , & mac_length );
310
+ if (status == PSA_SUCCESS ) {
311
+ psa_write (msg .handle , 0 , mac , mac_length );
312
+ psa_write (msg .handle , 1 , & mac_length , sizeof (mac_length ));
313
+ }
314
+ mbedtls_free (mac );
314
315
}
315
316
316
- mbedtls_free (mac );
317
+ mbedtls_free (msg .rhandle );
318
+ psa_set_rhandle (msg .handle , NULL );
317
319
break ;
318
320
}
319
321
@@ -328,22 +330,27 @@ static void psa_mac_operation(void)
328
330
329
331
uint8_t * mac = mbedtls_calloc (1 , mac_length );
330
332
if (mac == NULL ) {
333
+ psa_mac_abort (msg .rhandle );
331
334
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
332
- break ;
333
- }
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
+ }
334
340
335
- bytes_read = psa_read (msg .handle , 2 , mac , msg .in_size [2 ]);
336
- if (bytes_read != msg .in_size [2 ]) {
337
- SPM_PANIC ("SPM read length mismatch" );
341
+ status = psa_mac_verify_finish (msg .rhandle , mac , mac_length );
342
+ mbedtls_free (mac );
338
343
}
339
344
340
- status = psa_mac_verify_finish (msg .rhandle , mac , mac_length );
341
- mbedtls_free ( mac );
345
+ mbedtls_free (msg .rhandle );
346
+ psa_set_rhandle ( msg . handle , NULL );
342
347
break ;
343
348
}
344
349
345
350
case PSA_MAC_ABORT : {
346
351
status = psa_mac_abort (msg .rhandle );
352
+ mbedtls_free (msg .rhandle );
353
+ psa_set_rhandle (msg .handle , NULL );
347
354
break ;
348
355
}
349
356
@@ -359,8 +366,8 @@ static void psa_mac_operation(void)
359
366
}
360
367
361
368
case PSA_IPC_DISCONNECT : {
362
- psa_mac_abort (msg .rhandle );
363
369
if (msg .rhandle != NULL ) {
370
+ psa_mac_abort (msg .rhandle );
364
371
mbedtls_free (msg .rhandle );
365
372
}
366
373
0 commit comments