Skip to content

Commit 8f7912f

Browse files
committed
1)Clean the code
1 parent 227654a commit 8f7912f

File tree

1 file changed

+34
-74
lines changed

1 file changed

+34
-74
lines changed

src/platform/Infineon/crypto/trustm/CHIPCryptoPALHsm_utils_trustm.cpp

+34-74
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ void trustm_Open(void)
147147
optiga_lib_status = OPTIGA_LIB_BUSY;
148148
return_status = optiga_util_open_application(p_local_util, 0); // skip restore
149149
WAIT_FOR_COMPLETION(return_status);
150-
151150
if (OPTIGA_LIB_SUCCESS != return_status)
152151
{
153152
// optiga_util_open_application failed
@@ -159,12 +158,11 @@ void trustm_Open(void)
159158
{
160159
return_status = optiga_util_write_data(p_local_util, dOptigaOID, OPTIGA_UTIL_WRITE_ONLY, 0, &cCurrentLimit, 1);
161160

162-
WAIT_FOR_COMPLETION(return_status);
163-
164161
if (OPTIGA_LIB_SUCCESS != return_status)
165162
{
166163
break;
167164
}
165+
WAIT_FOR_COMPLETION(return_status);
168166
// Set init to true
169167
init = true;
170168
}
@@ -187,13 +185,12 @@ void trustm_close(void)
187185
optiga_lib_status = OPTIGA_LIB_BUSY;
188186
return_status = optiga_util_close_application(p_local_util, 0);
189187
WAIT_FOR_COMPLETION(return_status);
190-
191188
if (OPTIGA_LIB_SUCCESS != return_status)
192189
{
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+
}
197194
// destroy util and crypt instances
198195
optiga_util_destroy(p_local_util);
199196
optiga_crypt_destroy(p_local_crypt);
@@ -233,14 +230,7 @@ void read_certificate_from_optiga(uint16_t optiga_oid, char * cert_pem, uint16_t
233230
break;
234231
}
235232

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);
244234

245235
// convert to PEM format
246236
// 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)
310300
{
311301
optiga_lib_status = OPTIGA_LIB_BUSY;
312302
return_status = optiga_util_write_metadata(p_local_util, optiga_oid, p_data, length);
313-
314-
WAIT_FOR_COMPLETION(return_status);
315-
316303
if (OPTIGA_LIB_SUCCESS != return_status)
317304
{
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);
319306
return_status = optiga_lib_status;
320307
break;
321308
}
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);
326310
} while (0);
327311
}
328312

@@ -336,15 +320,13 @@ optiga_lib_status_t deriveKey_HKDF(const uint8_t * salt, uint16_t salt_length, c
336320
optiga_lib_status = OPTIGA_LIB_BUSY;
337321
return_status = optiga_crypt_hkdf(p_local_crypt, OPTIGA_HKDF_SHA_256, TRUSTM_HKDF_OID_KEY, /* Input secret OID */
338322
salt, salt_length, info, info_length, derived_key_length, TRUE, derived_key);
339-
340-
WAIT_FOR_COMPLETION(return_status);
341-
323+
342324
if (OPTIGA_LIB_SUCCESS != return_status)
343325
{
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);
346327
break;
347328
}
329+
WAIT_FOR_COMPLETION(return_status);
348330
} while (0);
349331

350332
return return_status;
@@ -357,7 +339,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
357339

358340
do
359341
{
360-
return_status = OPTIGA_LIB_BUSY;
342+
optiga_lib_status = OPTIGA_LIB_BUSY;
361343
#if ENABLE_HMAC_MULTI_STEP
362344
// If the size is less than the max length supported
363345
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
370352
optiga_lib_print_message("optiga_crypt_hmac api returns error !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
371353
break;
372354
}
355+
WAIT_FOR_COMPLETION(return_status);
373356
}
374357
else
375358
{
@@ -378,16 +361,14 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
378361
uint32_t remainingLen = input_data_length;
379362
// Start the HMAC Operation
380363
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-
384364
if (OPTIGA_LIB_SUCCESS != return_status)
385365
{
386366
// optiga_crypt_hmac_start api returns error !!!
387367
optiga_lib_print_message("optiga_crypt_hmac_start api returns error !!!", OPTIGA_UTIL_SERVICE,
388368
OPTIGA_UTIL_SERVICE_COLOR);
389369
break;
390370
}
371+
WAIT_FOR_COMPLETION(return_status);
391372
remainingLen = input_data_length - MAX_MAC_DATA_LEN;
392373

393374
while (remainingLen > 0)
@@ -396,54 +377,50 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
396377

397378
if (remainingLen > MAX_MAC_DATA_LEN)
398379
{
399-
return_status = OPTIGA_LIB_BUSY;
380+
optiga_lib_status = OPTIGA_LIB_BUSY;
400381
// printf("HMAC Update\n");
401382
// Continue HMAC operation on input data
402383
return_status =
403384
optiga_crypt_hmac_update(p_local_crypt, (input_data + (input_data_length - remainingLen)), dataLenTemp);
404385
remainingLen = remainingLen - dataLenTemp;
405-
406-
WAIT_FOR_COMPLETION(return_status);
407-
408386
if (OPTIGA_LIB_SUCCESS != return_status)
409387
{
410388
// optiga_crypt_hmac_update api returns error !!!
411389
optiga_lib_print_message("optiga_crypt_hmac_update api returns error !!!", OPTIGA_UTIL_SERVICE,
412390
OPTIGA_UTIL_SERVICE_COLOR);
413391
break;
414392
}
393+
WAIT_FOR_COMPLETION(return_status);
415394
}
416395
else
417396
{
418397
// End HMAC sequence and return the MAC generated
419398
// printf("HMAC Finalize\n");
420-
return_status = OPTIGA_LIB_BUSY;
399+
optiga_lib_status = OPTIGA_LIB_BUSY;
421400
return_status = optiga_crypt_hmac_finalize(p_local_crypt, (input_data + (input_data_length - remainingLen)),
422401
dataLenTemp, mac, mac_length);
423-
WAIT_FOR_COMPLETION(return_status);
424-
425402
if (OPTIGA_LIB_SUCCESS != return_status)
426403
{
427404
// optiga_crypt_hmac_finalize api returns error !!!
428405
optiga_lib_print_message("optiga_crypt_hmac_finalize api returns error !!!", OPTIGA_UTIL_SERVICE,
429406
OPTIGA_UTIL_SERVICE_COLOR);
430407
break;
431408
}
409+
WAIT_FOR_COMPLETION(return_status);
432410
}
433411
}
434412
}
435413
#else
436414

437415
return_status = optiga_crypt_hmac(p_local_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
438416
// printf("Output Length %ld Input Length %ld \n", *mac_length, input_data_length);
439-
WAIT_FOR_COMPLETION(return_status);
440-
441417
if (OPTIGA_LIB_SUCCESS != return_status)
442418
{
443419
// optiga_crypt_hmac api returns error !!!
444420
optiga_lib_print_message("optiga_crypt_hmac api returns error !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
445421
break;
446422
}
423+
WAIT_FOR_COMPLETION(return_status);
447424
#endif
448425
} while (0);
449426

@@ -454,17 +431,15 @@ optiga_lib_status_t optiga_crypt_rng(uint8_t * random_data, uint16_t random_data
454431
optiga_lib_status_t return_status;
455432
do
456433
{
457-
return_status = OPTIGA_LIB_BUSY;
434+
optiga_lib_status = OPTIGA_LIB_BUSY;
458435
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-
462436
if (OPTIGA_LIB_SUCCESS != return_status)
463437
{
464438
// 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);
466440
break;
467441
}
442+
WAIT_FOR_COMPLETION(return_status);
468443
} while (0);
469444

470445
return return_status;
@@ -485,16 +460,14 @@ optiga_lib_status_t trustm_ecc_keygen(uint16_t optiga_key_id, uint8_t key_type,
485460
optiga_lib_status = OPTIGA_LIB_BUSY;
486461
return_status = optiga_crypt_ecc_generate_keypair(p_local_crypt, curve_id, key_type, FALSE, &optiga_key_id, (pubkey + i),
487462
pubkey_length);
488-
489-
WAIT_FOR_COMPLETION(return_status);
490-
491463
if (OPTIGA_LIB_SUCCESS != return_status)
492464
{
493465
// optiga_crypt_ecc_generate_keypair api returns error !!!
494466
optiga_lib_print_message("optiga_crypt_ecc_generate_keypair api returns error !!!", OPTIGA_UTIL_SERVICE,
495467
OPTIGA_UTIL_SERVICE_COLOR);
496468
break;
497469
}
470+
WAIT_FOR_COMPLETION(return_status);
498471
} while (0);
499472

500473
*pubkey_length += sizeof(header256);
@@ -508,15 +481,13 @@ void trustmGetKey(uint16_t optiga_oid, uint8_t * pubkey, uint16_t * pubkeyLen)
508481
{
509482
optiga_lib_status = OPTIGA_LIB_BUSY;
510483
return_status = optiga_util_read_data(p_local_util, optiga_oid, offset, pubkey, pubkeyLen);
511-
512-
WAIT_FOR_COMPLETION(return_status);
513-
514484
if (OPTIGA_LIB_SUCCESS != return_status)
515485
{
516486
// optiga_util_read_pubkey api returns error !!!
517487
optiga_lib_print_message("optiga_util_read_pubkey returns error !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
518488
break;
519489
}
490+
WAIT_FOR_COMPLETION(return_status);
520491

521492
} while (0);
522493
}
@@ -530,15 +501,13 @@ optiga_lib_status_t trustm_hash(uint8_t * msg, uint16_t msg_length, uint8_t * di
530501
hash_data_host.length = msg_length;
531502
optiga_lib_status = OPTIGA_LIB_BUSY;
532503
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-
536504
if (OPTIGA_LIB_SUCCESS != return_status)
537505
{
538506
// optiga_crypt_hash api returns error !!!
539507
optiga_lib_print_message("optiga_crypt_hash api returns error !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
540508
break;
541509
}
510+
WAIT_FOR_COMPLETION(return_status);
542511
} while (0);
543512

544513
return return_status;
@@ -553,15 +522,14 @@ optiga_lib_status_t trustm_ecdsa_sign(optiga_key_id_t optiga_key_id, uint8_t * d
553522
optiga_lib_status = OPTIGA_LIB_BUSY;
554523
return_status = optiga_crypt_ecdsa_sign(p_local_crypt, digest, digest_length, optiga_key_id, signature, signature_length);
555524

556-
WAIT_FOR_COMPLETION(return_status);
557-
558525
if (OPTIGA_LIB_SUCCESS != return_status)
559526
{
560527
// optiga_crypt_ecdsa_sign api returns error !!!
561528
optiga_lib_print_message("optiga_crypt_ecdsa_sign api returns error !!!", OPTIGA_UTIL_SERVICE,
562529
OPTIGA_UTIL_SERVICE_COLOR);
563530
break;
564531
}
532+
WAIT_FOR_COMPLETION(return_status);
565533

566534
for (i = (*signature_length - 1); i >= 0; i--)
567535
{
@@ -616,16 +584,14 @@ optiga_lib_status_t trustm_ecdsa_verify(uint8_t * digest, uint8_t digest_length,
616584
optiga_lib_status = OPTIGA_LIB_BUSY;
617585
return_status = optiga_crypt_ecdsa_verify(p_local_crypt, digest, digest_length, signature, signature_length,
618586
OPTIGA_CRYPT_HOST_DATA, &public_key_details);
619-
620-
WAIT_FOR_COMPLETION(return_status);
621-
622587
if (OPTIGA_LIB_SUCCESS != return_status)
623588
{
624589
// optiga_crypt_ecdsa_verify api returns error !!!
625590
optiga_lib_print_message("optiga_crypt_ecdsa_verify api returns error !!!", OPTIGA_UTIL_SERVICE,
626591
OPTIGA_UTIL_SERVICE_COLOR);
627592
break;
628593
}
594+
WAIT_FOR_COMPLETION(return_status);
629595
} while (0);
630596

631597
return return_status;
@@ -646,15 +612,13 @@ CHIP_ERROR trustmGetCertificate(uint16_t optiga_oid, uint8_t * buf, uint16_t * b
646612
optiga_lib_status = OPTIGA_LIB_BUSY;
647613
return_status = optiga_util_read_data(p_local_util, optiga_oid, 0, ifx_cert_hex, &ifx_cert_hex_len);
648614

649-
WAIT_FOR_COMPLETION(return_status);
650-
651615
if (OPTIGA_LIB_SUCCESS != return_status)
652616
{
653617
// optiga_util_read_data failed
654618
optiga_lib_print_message("optiga_util_read_data failed", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
655619
break;
656620
}
657-
621+
WAIT_FOR_COMPLETION(return_status);
658622
memcpy(buf, ifx_cert_hex, ifx_cert_hex_len);
659623
*buflen = ifx_cert_hex_len;
660624
} while (0);
@@ -674,15 +638,13 @@ optiga_lib_status_t trustm_ecdh_derive_secret(optiga_key_id_t optiga_key_id, uin
674638
{
675639
optiga_lib_status = OPTIGA_LIB_BUSY;
676640
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-
680641
if (OPTIGA_LIB_SUCCESS != return_status)
681642
{
682643
// optiga_crypt_ecdh api returns error !!!
683644
optiga_lib_print_message("optiga_crypt_ecdh api returns error !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
684645
break;
685646
}
647+
WAIT_FOR_COMPLETION(return_status);
686648
} while (0);
687649

688650
return return_status;
@@ -701,30 +663,28 @@ optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen,
701663
do
702664
{
703665
// Calculate U1, U1 ends up in work
666+
optiga_lib_status = OPTIGA_LIB_BUSY;
704667
return_status =
705668
optiga_crypt_hmac(p_local_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, salt, (uint32_t) slen, work, &work_len);
706669

707-
WAIT_FOR_COMPLETION(return_status);
708-
709670
if (OPTIGA_LIB_SUCCESS != return_status)
710671
{
711672
optiga_lib_print_message("optiga_crypt_hmac api returns error!!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
712673
break;
713674
}
675+
WAIT_FOR_COMPLETION(return_status);
714676
memcpy(md1, work, md1_len);
715677
for (unsigned int i = 1; i < iteration_count; i++)
716678
{
717-
return_status = OPTIGA_LIB_BUSY;
679+
optiga_lib_status = OPTIGA_LIB_BUSY;
718680
// 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);
723682
if (OPTIGA_LIB_SUCCESS != return_status)
724683
{
725684
optiga_lib_print_message("optiga_crypt_hmac api returns error!!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
726685
break;
727686
}
687+
WAIT_FOR_COMPLETION(return_status);
728688
// U1 xor U2
729689
for (int j = 0; j < (int) md1_len; j++)
730690
{

0 commit comments

Comments
 (0)