@@ -147,7 +147,6 @@ void trustm_Open(void)
147
147
optiga_lib_status = OPTIGA_LIB_BUSY;
148
148
return_status = optiga_util_open_application (p_local_util, 0 ); // skip restore
149
149
WAIT_FOR_COMPLETION (return_status);
150
-
151
150
if (OPTIGA_LIB_SUCCESS != return_status)
152
151
{
153
152
// optiga_util_open_application failed
@@ -159,12 +158,11 @@ void trustm_Open(void)
159
158
{
160
159
return_status = optiga_util_write_data (p_local_util, dOptigaOID, OPTIGA_UTIL_WRITE_ONLY, 0 , &cCurrentLimit, 1 );
161
160
162
- WAIT_FOR_COMPLETION (return_status);
163
-
164
161
if (OPTIGA_LIB_SUCCESS != return_status)
165
162
{
166
163
break ;
167
164
}
165
+ WAIT_FOR_COMPLETION (return_status);
168
166
// Set init to true
169
167
init = true ;
170
168
}
@@ -187,13 +185,12 @@ void trustm_close(void)
187
185
optiga_lib_status = OPTIGA_LIB_BUSY;
188
186
return_status = optiga_util_close_application (p_local_util, 0 );
189
187
WAIT_FOR_COMPLETION (return_status);
190
-
191
188
if (OPTIGA_LIB_SUCCESS != return_status)
192
189
{
193
- // optiga_util_open_application failed
194
- printf (" optiga_util_open_application failed\n " );
195
- break ;
196
- }
190
+ // optiga_util_close_application failed
191
+ printf (" optiga_util_close_application failed\n " );
192
+ break ;
193
+ }
197
194
// destroy util and crypt instances
198
195
optiga_util_destroy (p_local_util);
199
196
optiga_crypt_destroy (p_local_crypt);
@@ -233,14 +230,7 @@ void read_certificate_from_optiga(uint16_t optiga_oid, char * cert_pem, uint16_t
233
230
break ;
234
231
}
235
232
236
- while (optiga_lib_status == OPTIGA_LIB_BUSY)
237
- ;
238
- if (OPTIGA_LIB_SUCCESS != optiga_lib_status)
239
- {
240
- // optiga_util_read_data failed
241
- optiga_lib_print_message (" optiga_util_read_data failed" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
242
- break ;
243
- }
233
+ WAIT_FOR_COMPLETION (return_status);
244
234
245
235
// convert to PEM format
246
236
// If the first byte is TLS Identity Tag, than we need to skip 9 first bytes
@@ -310,19 +300,13 @@ void write_metadata(uint16_t optiga_oid, const uint8_t * p_data, uint8_t length)
310
300
{
311
301
optiga_lib_status = OPTIGA_LIB_BUSY;
312
302
return_status = optiga_util_write_metadata (p_local_util, optiga_oid, p_data, length);
313
-
314
- WAIT_FOR_COMPLETION (return_status);
315
-
316
303
if (OPTIGA_LIB_SUCCESS != return_status)
317
304
{
318
- optiga_lib_print_message (" optiga_util_write_metadata failed" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
305
+ optiga_lib_print_message (" optiga_util_write_metadata API failed" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
319
306
return_status = optiga_lib_status;
320
307
break ;
321
308
}
322
- else
323
- {
324
- optiga_lib_print_message (" optiga_util_write_metadata successful" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
325
- }
309
+ WAIT_FOR_COMPLETION (return_status);
326
310
} while (0 );
327
311
}
328
312
@@ -336,15 +320,13 @@ optiga_lib_status_t deriveKey_HKDF(const uint8_t * salt, uint16_t salt_length, c
336
320
optiga_lib_status = OPTIGA_LIB_BUSY;
337
321
return_status = optiga_crypt_hkdf (p_local_crypt, OPTIGA_HKDF_SHA_256, TRUSTM_HKDF_OID_KEY, /* Input secret OID */
338
322
salt, salt_length, info, info_length, derived_key_length, TRUE , derived_key);
339
-
340
- WAIT_FOR_COMPLETION (return_status);
341
-
323
+
342
324
if (OPTIGA_LIB_SUCCESS != return_status)
343
325
{
344
- // optiga_crypt_hkdf failed
345
- optiga_lib_print_message (" optiga_crypt_hkdf failed" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
326
+ optiga_lib_print_message (" optiga_crypt_hkdf API failed" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
346
327
break ;
347
328
}
329
+ WAIT_FOR_COMPLETION (return_status);
348
330
} while (0 );
349
331
350
332
return return_status;
@@ -357,7 +339,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
357
339
358
340
do
359
341
{
360
- return_status = OPTIGA_LIB_BUSY;
342
+ optiga_lib_status = OPTIGA_LIB_BUSY;
361
343
#if ENABLE_HMAC_MULTI_STEP
362
344
// If the size is less than the max length supported
363
345
if (input_data_length <= MAX_MAC_DATA_LEN)
@@ -370,6 +352,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
370
352
optiga_lib_print_message (" optiga_crypt_hmac api returns error !!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
371
353
break ;
372
354
}
355
+ WAIT_FOR_COMPLETION (return_status);
373
356
}
374
357
else
375
358
{
@@ -378,16 +361,14 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
378
361
uint32_t remainingLen = input_data_length;
379
362
// Start the HMAC Operation
380
363
return_status = optiga_crypt_hmac_start (p_local_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, MAX_MAC_DATA_LEN);
381
-
382
- WAIT_FOR_COMPLETION (return_status);
383
-
384
364
if (OPTIGA_LIB_SUCCESS != return_status)
385
365
{
386
366
// optiga_crypt_hmac_start api returns error !!!
387
367
optiga_lib_print_message (" optiga_crypt_hmac_start api returns error !!!" , OPTIGA_UTIL_SERVICE,
388
368
OPTIGA_UTIL_SERVICE_COLOR);
389
369
break ;
390
370
}
371
+ WAIT_FOR_COMPLETION (return_status);
391
372
remainingLen = input_data_length - MAX_MAC_DATA_LEN;
392
373
393
374
while (remainingLen > 0 )
@@ -396,54 +377,50 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
396
377
397
378
if (remainingLen > MAX_MAC_DATA_LEN)
398
379
{
399
- return_status = OPTIGA_LIB_BUSY;
380
+ optiga_lib_status = OPTIGA_LIB_BUSY;
400
381
// printf("HMAC Update\n");
401
382
// Continue HMAC operation on input data
402
383
return_status =
403
384
optiga_crypt_hmac_update (p_local_crypt, (input_data + (input_data_length - remainingLen)), dataLenTemp);
404
385
remainingLen = remainingLen - dataLenTemp;
405
-
406
- WAIT_FOR_COMPLETION (return_status);
407
-
408
386
if (OPTIGA_LIB_SUCCESS != return_status)
409
387
{
410
388
// optiga_crypt_hmac_update api returns error !!!
411
389
optiga_lib_print_message (" optiga_crypt_hmac_update api returns error !!!" , OPTIGA_UTIL_SERVICE,
412
390
OPTIGA_UTIL_SERVICE_COLOR);
413
391
break ;
414
392
}
393
+ WAIT_FOR_COMPLETION (return_status);
415
394
}
416
395
else
417
396
{
418
397
// End HMAC sequence and return the MAC generated
419
398
// printf("HMAC Finalize\n");
420
- return_status = OPTIGA_LIB_BUSY;
399
+ optiga_lib_status = OPTIGA_LIB_BUSY;
421
400
return_status = optiga_crypt_hmac_finalize (p_local_crypt, (input_data + (input_data_length - remainingLen)),
422
401
dataLenTemp, mac, mac_length);
423
- WAIT_FOR_COMPLETION (return_status);
424
-
425
402
if (OPTIGA_LIB_SUCCESS != return_status)
426
403
{
427
404
// optiga_crypt_hmac_finalize api returns error !!!
428
405
optiga_lib_print_message (" optiga_crypt_hmac_finalize api returns error !!!" , OPTIGA_UTIL_SERVICE,
429
406
OPTIGA_UTIL_SERVICE_COLOR);
430
407
break ;
431
408
}
409
+ WAIT_FOR_COMPLETION (return_status);
432
410
}
433
411
}
434
412
}
435
413
#else
436
414
437
415
return_status = optiga_crypt_hmac (p_local_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
438
416
// printf("Output Length %ld Input Length %ld \n", *mac_length, input_data_length);
439
- WAIT_FOR_COMPLETION (return_status);
440
-
441
417
if (OPTIGA_LIB_SUCCESS != return_status)
442
418
{
443
419
// optiga_crypt_hmac api returns error !!!
444
420
optiga_lib_print_message (" optiga_crypt_hmac api returns error !!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
445
421
break ;
446
422
}
423
+ WAIT_FOR_COMPLETION (return_status);
447
424
#endif
448
425
} while (0 );
449
426
@@ -454,17 +431,15 @@ optiga_lib_status_t optiga_crypt_rng(uint8_t * random_data, uint16_t random_data
454
431
optiga_lib_status_t return_status;
455
432
do
456
433
{
457
- return_status = OPTIGA_LIB_BUSY;
434
+ optiga_lib_status = OPTIGA_LIB_BUSY;
458
435
return_status = optiga_crypt_random (p_local_crypt, OPTIGA_RNG_TYPE_DRNG, random_data, random_data_length);
459
-
460
- WAIT_FOR_COMPLETION (return_status);
461
-
462
436
if (OPTIGA_LIB_SUCCESS != return_status)
463
437
{
464
438
// optiga_crypt_random failed
465
- optiga_lib_print_message (" optiga_crypt_random failed " , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
439
+ optiga_lib_print_message (" optiga_crypt_random api returns error !!! " , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
466
440
break ;
467
441
}
442
+ WAIT_FOR_COMPLETION (return_status);
468
443
} while (0 );
469
444
470
445
return return_status;
@@ -485,16 +460,14 @@ optiga_lib_status_t trustm_ecc_keygen(uint16_t optiga_key_id, uint8_t key_type,
485
460
optiga_lib_status = OPTIGA_LIB_BUSY;
486
461
return_status = optiga_crypt_ecc_generate_keypair (p_local_crypt, curve_id, key_type, FALSE , &optiga_key_id, (pubkey + i),
487
462
pubkey_length);
488
-
489
- WAIT_FOR_COMPLETION (return_status);
490
-
491
463
if (OPTIGA_LIB_SUCCESS != return_status)
492
464
{
493
465
// optiga_crypt_ecc_generate_keypair api returns error !!!
494
466
optiga_lib_print_message (" optiga_crypt_ecc_generate_keypair api returns error !!!" , OPTIGA_UTIL_SERVICE,
495
467
OPTIGA_UTIL_SERVICE_COLOR);
496
468
break ;
497
469
}
470
+ WAIT_FOR_COMPLETION (return_status);
498
471
} while (0 );
499
472
500
473
*pubkey_length += sizeof (header256);
@@ -508,15 +481,13 @@ void trustmGetKey(uint16_t optiga_oid, uint8_t * pubkey, uint16_t * pubkeyLen)
508
481
{
509
482
optiga_lib_status = OPTIGA_LIB_BUSY;
510
483
return_status = optiga_util_read_data (p_local_util, optiga_oid, offset, pubkey, pubkeyLen);
511
-
512
- WAIT_FOR_COMPLETION (return_status);
513
-
514
484
if (OPTIGA_LIB_SUCCESS != return_status)
515
485
{
516
486
// optiga_util_read_pubkey api returns error !!!
517
487
optiga_lib_print_message (" optiga_util_read_pubkey returns error !!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
518
488
break ;
519
489
}
490
+ WAIT_FOR_COMPLETION (return_status);
520
491
521
492
} while (0 );
522
493
}
@@ -530,15 +501,13 @@ optiga_lib_status_t trustm_hash(uint8_t * msg, uint16_t msg_length, uint8_t * di
530
501
hash_data_host.length = msg_length;
531
502
optiga_lib_status = OPTIGA_LIB_BUSY;
532
503
return_status = optiga_crypt_hash (p_local_crypt, OPTIGA_HASH_TYPE_SHA_256, OPTIGA_CRYPT_HOST_DATA, &hash_data_host, digest);
533
-
534
- WAIT_FOR_COMPLETION (return_status);
535
-
536
504
if (OPTIGA_LIB_SUCCESS != return_status)
537
505
{
538
506
// optiga_crypt_hash api returns error !!!
539
507
optiga_lib_print_message (" optiga_crypt_hash api returns error !!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
540
508
break ;
541
509
}
510
+ WAIT_FOR_COMPLETION (return_status);
542
511
} while (0 );
543
512
544
513
return return_status;
@@ -553,15 +522,14 @@ optiga_lib_status_t trustm_ecdsa_sign(optiga_key_id_t optiga_key_id, uint8_t * d
553
522
optiga_lib_status = OPTIGA_LIB_BUSY;
554
523
return_status = optiga_crypt_ecdsa_sign (p_local_crypt, digest, digest_length, optiga_key_id, signature, signature_length);
555
524
556
- WAIT_FOR_COMPLETION (return_status);
557
-
558
525
if (OPTIGA_LIB_SUCCESS != return_status)
559
526
{
560
527
// optiga_crypt_ecdsa_sign api returns error !!!
561
528
optiga_lib_print_message (" optiga_crypt_ecdsa_sign api returns error !!!" , OPTIGA_UTIL_SERVICE,
562
529
OPTIGA_UTIL_SERVICE_COLOR);
563
530
break ;
564
531
}
532
+ WAIT_FOR_COMPLETION (return_status);
565
533
566
534
for (i = (*signature_length - 1 ); i >= 0 ; i--)
567
535
{
@@ -616,16 +584,14 @@ optiga_lib_status_t trustm_ecdsa_verify(uint8_t * digest, uint8_t digest_length,
616
584
optiga_lib_status = OPTIGA_LIB_BUSY;
617
585
return_status = optiga_crypt_ecdsa_verify (p_local_crypt, digest, digest_length, signature, signature_length,
618
586
OPTIGA_CRYPT_HOST_DATA, &public_key_details);
619
-
620
- WAIT_FOR_COMPLETION (return_status);
621
-
622
587
if (OPTIGA_LIB_SUCCESS != return_status)
623
588
{
624
589
// optiga_crypt_ecdsa_verify api returns error !!!
625
590
optiga_lib_print_message (" optiga_crypt_ecdsa_verify api returns error !!!" , OPTIGA_UTIL_SERVICE,
626
591
OPTIGA_UTIL_SERVICE_COLOR);
627
592
break ;
628
593
}
594
+ WAIT_FOR_COMPLETION (return_status);
629
595
} while (0 );
630
596
631
597
return return_status;
@@ -646,15 +612,13 @@ CHIP_ERROR trustmGetCertificate(uint16_t optiga_oid, uint8_t * buf, uint16_t * b
646
612
optiga_lib_status = OPTIGA_LIB_BUSY;
647
613
return_status = optiga_util_read_data (p_local_util, optiga_oid, 0 , ifx_cert_hex, &ifx_cert_hex_len);
648
614
649
- WAIT_FOR_COMPLETION (return_status);
650
-
651
615
if (OPTIGA_LIB_SUCCESS != return_status)
652
616
{
653
617
// optiga_util_read_data failed
654
618
optiga_lib_print_message (" optiga_util_read_data failed" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
655
619
break ;
656
620
}
657
-
621
+ WAIT_FOR_COMPLETION (return_status);
658
622
memcpy (buf, ifx_cert_hex, ifx_cert_hex_len);
659
623
*buflen = ifx_cert_hex_len;
660
624
} while (0 );
@@ -674,15 +638,13 @@ optiga_lib_status_t trustm_ecdh_derive_secret(optiga_key_id_t optiga_key_id, uin
674
638
{
675
639
optiga_lib_status = OPTIGA_LIB_BUSY;
676
640
return_status = optiga_crypt_ecdh (p_local_crypt, optiga_key_id, &public_key_details, TRUE , shared_secret);
677
-
678
- WAIT_FOR_COMPLETION (return_status);
679
-
680
641
if (OPTIGA_LIB_SUCCESS != return_status)
681
642
{
682
643
// optiga_crypt_ecdh api returns error !!!
683
644
optiga_lib_print_message (" optiga_crypt_ecdh api returns error !!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
684
645
break ;
685
646
}
647
+ WAIT_FOR_COMPLETION (return_status);
686
648
} while (0 );
687
649
688
650
return return_status;
@@ -701,30 +663,28 @@ optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen,
701
663
do
702
664
{
703
665
// Calculate U1, U1 ends up in work
666
+ optiga_lib_status = OPTIGA_LIB_BUSY;
704
667
return_status =
705
668
optiga_crypt_hmac (p_local_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, salt, (uint32_t ) slen, work, &work_len);
706
669
707
- WAIT_FOR_COMPLETION (return_status);
708
-
709
670
if (OPTIGA_LIB_SUCCESS != return_status)
710
671
{
711
672
optiga_lib_print_message (" optiga_crypt_hmac api returns error!!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
712
673
break ;
713
674
}
675
+ WAIT_FOR_COMPLETION (return_status);
714
676
memcpy (md1, work, md1_len);
715
677
for (unsigned int i = 1 ; i < iteration_count; i++)
716
678
{
717
- return_status = OPTIGA_LIB_BUSY;
679
+ optiga_lib_status = OPTIGA_LIB_BUSY;
718
680
// Calculated subsequent U, which ends up in md1
719
- return_status = optiga_crypt_hmac (p_local_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, md1, md1_len, md1, &md1_len);
720
-
721
- WAIT_FOR_COMPLETION (return_status);
722
-
681
+ return_status = optiga_crypt_hmac (p_local_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, md1, md1_len, md1, &md1_len);
723
682
if (OPTIGA_LIB_SUCCESS != return_status)
724
683
{
725
684
optiga_lib_print_message (" optiga_crypt_hmac api returns error!!!" , OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
726
685
break ;
727
686
}
687
+ WAIT_FOR_COMPLETION (return_status);
728
688
// U1 xor U2
729
689
for (int j = 0 ; j < (int ) md1_len; j++)
730
690
{
0 commit comments