Skip to content

Commit 2087085

Browse files
committed
Code clean up
* renamed p_local_util and p_local_crypt to me_uti and me_crypt * changes to trustm_Open()
1 parent 25b92c5 commit 2087085

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

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

+44-44
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
#include "pal_os_timer.h"
3737
#include <FreeRTOS.h>
3838

39-
optiga_crypt_t * p_local_crypt = NULL;
40-
optiga_util_t * p_local_util = NULL;
39+
optiga_crypt_t * me_crypt = NULL;
40+
optiga_util_t * me_util = NULL;
4141
static bool trustm_isOpen = false;
4242
#define ENABLE_HMAC_MULTI_STEP (0)
4343
#define OPTIGA_UTIL_DER_BITSTRING_TAG (0x03)
@@ -120,37 +120,37 @@ void trustm_Open(void)
120120
do
121121
{
122122
// Create Optiga crypt instance
123-
if (p_local_crypt == NULL)
123+
if (me_crypt == NULL)
124124
{
125-
p_local_crypt = optiga_crypt_create(0, optiga_crypt_callback, NULL);
126-
if (NULL == p_local_crypt)
125+
me_crypt = optiga_crypt_create(0, optiga_crypt_callback, NULL);
126+
if (NULL == me_crypt)
127127
{
128128
break;
129129
}
130130
}
131131
else
132132
{
133-
printf("Error: p_local_crypt already initialised\n");
133+
printf("Error: me_crypt already initialised\n");
134134
}
135135
// Create Optiga Util instance
136-
if (p_local_util == NULL)
136+
if (me_util == NULL)
137137
{
138-
p_local_util = optiga_util_create(0, optiga_util_callback, NULL);
139-
if (NULL == p_local_util)
138+
me_util = optiga_util_create(0, optiga_util_callback, NULL);
139+
if (NULL == me_util)
140140
{
141141
break;
142142
}
143143
}
144144
else
145145
{
146-
printf("Error: p_local_crypt already initialised\n");
146+
printf("Error: me_crypt already initialised\n");
147147
}
148148
/**
149149
* Open the application on OPTIGA which is a precondition to perform any other operations
150150
* using optiga_util_open_application
151151
*/
152152
optiga_lib_status = OPTIGA_LIB_BUSY;
153-
return_status = optiga_util_open_application(p_local_util, 0); // skip restore
153+
return_status = optiga_util_open_application(me_util, 0); // skip restore
154154
if (OPTIGA_LIB_SUCCESS != return_status)
155155
{
156156
printf("optiga_util_open_application api returns error %02X\n", return_status);
@@ -165,15 +165,17 @@ void trustm_Open(void)
165165
printf("optiga_util_open_application failed\n");
166166
break;
167167
}
168+
trustm_isOpen = true;
169+
168170
// Only run once for initialisation
169-
if (init)
170-
{
171-
return_status = optiga_util_write_data(p_local_util, dOptigaOID, OPTIGA_UTIL_WRITE_ONLY, 0, &cCurrentLimit, 1);
172-
if (OPTIGA_LIB_SUCCESS != return_status)
171+
if (!init)
173172
{
174-
printf("optiga_util_write_data api returns error %02X\n", return_status);
175-
break;
176-
}
173+
return_status = optiga_util_write_data(me_util, dOptigaOID, OPTIGA_UTIL_WRITE_ONLY, 0, &cCurrentLimit, 1);
174+
if (OPTIGA_LIB_SUCCESS != return_status)
175+
{
176+
printf("optiga_util_write_data api returns error %02X\n", return_status);
177+
break;
178+
}
177179
WAIT_FOR_COMPLETION(return_status);
178180
if (OPTIGA_LIB_SUCCESS != return_status)
179181
{
@@ -184,8 +186,6 @@ void trustm_Open(void)
184186
init = true;
185187
}
186188
} while (0);
187-
188-
trustm_isOpen = true;
189189
}
190190
}
191191

@@ -200,7 +200,7 @@ void trustm_close(void)
200200
* using optiga_util_close_application
201201
*/
202202
optiga_lib_status = OPTIGA_LIB_BUSY;
203-
return_status = optiga_util_close_application(p_local_util, 0);
203+
return_status = optiga_util_close_application(me_util, 0);
204204
if (OPTIGA_LIB_SUCCESS != return_status)
205205
{
206206
printf("optiga_util_close_application api returns error %02X\n", return_status);
@@ -217,11 +217,11 @@ void trustm_close(void)
217217
}
218218

219219
// destroy util and crypt instances
220-
optiga_util_destroy(p_local_util);
221-
optiga_crypt_destroy(p_local_crypt);
220+
optiga_util_destroy(me_util);
221+
optiga_crypt_destroy(me_crypt);
222222
pal_os_event_destroy(NULL);
223-
p_local_util = NULL;
224-
p_local_crypt = NULL;
223+
me_util = NULL;
224+
me_crypt = NULL;
225225
trustm_isOpen = false;
226226
return_status = OPTIGA_LIB_SUCCESS;
227227
} while (0);
@@ -241,7 +241,7 @@ void read_certificate_from_optiga(uint16_t optiga_oid, char * cert_pem, uint16_t
241241
do
242242
{
243243
optiga_lib_status = OPTIGA_LIB_BUSY;
244-
return_status = optiga_util_read_data(p_local_util, optiga_oid, 0, ifx_cert_hex, &ifx_cert_hex_len);
244+
return_status = optiga_util_read_data(me_util, optiga_oid, 0, ifx_cert_hex, &ifx_cert_hex_len);
245245
if (OPTIGA_LIB_SUCCESS != return_status)
246246
{
247247
printf("optiga_util_read_data api returns error %02X\n", return_status);
@@ -292,7 +292,7 @@ void write_data(uint16_t optiga_oid, const uint8_t * p_data, uint16_t length)
292292
do
293293
{
294294
optiga_lib_status = OPTIGA_LIB_BUSY;
295-
return_status = optiga_util_write_data(p_local_util, optiga_oid, OPTIGA_UTIL_ERASE_AND_WRITE, 0, p_data, length);
295+
return_status = optiga_util_write_data(me_util, optiga_oid, OPTIGA_UTIL_ERASE_AND_WRITE, 0, p_data, length);
296296
if (OPTIGA_LIB_SUCCESS != return_status)
297297
{
298298
printf("optiga_util_write_data api returns error %02X\n", return_status);
@@ -316,7 +316,7 @@ void write_metadata(uint16_t optiga_oid, const uint8_t * p_data, uint8_t length)
316316
do
317317
{
318318
optiga_lib_status = OPTIGA_LIB_BUSY;
319-
return_status = optiga_util_write_metadata(p_local_util, optiga_oid, p_data, length);
319+
return_status = optiga_util_write_metadata(me_util, optiga_oid, p_data, length);
320320
if (OPTIGA_LIB_SUCCESS != return_status)
321321
{
322322
printf("optiga_util_write_metadata api returns error %02X\n", return_status);
@@ -339,7 +339,7 @@ optiga_lib_status_t deriveKey_HKDF(const uint8_t * salt, uint16_t salt_length, c
339339
do
340340
{
341341
optiga_lib_status = OPTIGA_LIB_BUSY;
342-
return_status = optiga_crypt_hkdf(p_local_crypt, OPTIGA_HKDF_SHA_256, TRUSTM_HKDF_OID_KEY, /* Input secret OID */
342+
return_status = optiga_crypt_hkdf(me_crypt, OPTIGA_HKDF_SHA_256, TRUSTM_HKDF_OID_KEY, /* Input secret OID */
343343
salt, salt_length, info, info_length, derived_key_length, TRUE, derived_key);
344344
if (OPTIGA_LIB_SUCCESS != return_status)
345345
{
@@ -369,7 +369,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
369369
// If the size is less than the max length supported
370370
if (input_data_length <= MAX_MAC_DATA_LEN)
371371
{
372-
return_status = optiga_crypt_hmac(p_local_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
372+
return_status = optiga_crypt_hmac(me_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
373373
if (OPTIGA_LIB_SUCCESS != return_status)
374374
{
375375
// optiga_crypt_hmac returns error !!!
@@ -391,7 +391,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
391391
uint32_t dataLenTemp = 0;
392392
uint32_t remainingLen = input_data_length;
393393
// Start the HMAC Operation
394-
return_status = optiga_crypt_hmac_start(p_local_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, MAX_MAC_DATA_LEN);
394+
return_status = optiga_crypt_hmac_start(me_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, MAX_MAC_DATA_LEN);
395395
if (OPTIGA_LIB_SUCCESS != return_status)
396396
{
397397
// optiga_crypt_hmac returns error !!!
@@ -415,7 +415,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
415415
{
416416
optiga_lib_status = OPTIGA_LIB_BUSY;
417417
return_status =
418-
optiga_crypt_hmac_update(p_local_crypt, (input_data + (input_data_length - remainingLen)), dataLenTemp);
418+
optiga_crypt_hmac_update(me_crypt, (input_data + (input_data_length - remainingLen)), dataLenTemp);
419419
if (OPTIGA_LIB_SUCCESS != return_status)
420420
{
421421
// optiga_crypt_hmac_update returns error !!!
@@ -436,7 +436,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
436436
// End HMAC sequence and return the MAC generated
437437
// printf("HMAC Finalize\n");
438438
optiga_lib_status = OPTIGA_LIB_BUSY;
439-
return_status = optiga_crypt_hmac_finalize(p_local_crypt, (input_data + (input_data_length - remainingLen)),
439+
return_status = optiga_crypt_hmac_finalize(me_crypt, (input_data + (input_data_length - remainingLen)),
440440
dataLenTemp, mac, mac_length);
441441
if (OPTIGA_LIB_SUCCESS != return_status)
442442
{
@@ -456,7 +456,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
456456
}
457457
#else
458458

459-
return_status = optiga_crypt_hmac(p_local_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
459+
return_status = optiga_crypt_hmac(me_crypt, type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
460460
if (OPTIGA_LIB_SUCCESS != return_status)
461461
{
462462
// optiga_crypt_hmac returns error !!!
@@ -482,7 +482,7 @@ optiga_lib_status_t optiga_crypt_rng(uint8_t * random_data, uint16_t random_data
482482
do
483483
{
484484
optiga_lib_status = OPTIGA_LIB_BUSY;
485-
return_status = optiga_crypt_random(p_local_crypt, OPTIGA_RNG_TYPE_DRNG, random_data, random_data_length);
485+
return_status = optiga_crypt_random(me_crypt, OPTIGA_RNG_TYPE_DRNG, random_data, random_data_length);
486486
if (OPTIGA_LIB_SUCCESS != return_status)
487487
{
488488
// optiga_crypt_random returns error !!!
@@ -514,7 +514,7 @@ optiga_lib_status_t trustm_ecc_keygen(uint16_t optiga_key_id, uint8_t key_type,
514514
do
515515
{
516516
optiga_lib_status = OPTIGA_LIB_BUSY;
517-
return_status = optiga_crypt_ecc_generate_keypair(p_local_crypt, curve_id, key_type, FALSE, &optiga_key_id, (pubkey + i),
517+
return_status = optiga_crypt_ecc_generate_keypair(me_crypt, curve_id, key_type, FALSE, &optiga_key_id, (pubkey + i),
518518
pubkey_length);
519519
if (OPTIGA_LIB_SUCCESS != return_status)
520520
{
@@ -541,7 +541,7 @@ void trustmGetKey(uint16_t optiga_oid, uint8_t * pubkey, uint16_t * pubkeyLen)
541541
do
542542
{
543543
optiga_lib_status = OPTIGA_LIB_BUSY;
544-
return_status = optiga_util_read_data(p_local_util, optiga_oid, offset, pubkey, pubkeyLen);
544+
return_status = optiga_util_read_data(me_util, optiga_oid, offset, pubkey, pubkeyLen);
545545
if (OPTIGA_LIB_SUCCESS != return_status)
546546
{
547547
// optiga_util_read_data api returns error !!!
@@ -566,7 +566,7 @@ optiga_lib_status_t trustm_hash(uint8_t * msg, uint16_t msg_length, uint8_t * di
566566
hash_data_host.buffer = msg;
567567
hash_data_host.length = msg_length;
568568
optiga_lib_status = OPTIGA_LIB_BUSY;
569-
return_status = optiga_crypt_hash(p_local_crypt, OPTIGA_HASH_TYPE_SHA_256, OPTIGA_CRYPT_HOST_DATA, &hash_data_host, digest);
569+
return_status = optiga_crypt_hash(me_crypt, OPTIGA_HASH_TYPE_SHA_256, OPTIGA_CRYPT_HOST_DATA, &hash_data_host, digest);
570570
if (OPTIGA_LIB_SUCCESS != return_status)
571571
{
572572
// optiga_crypt_hash api returns error !!!
@@ -592,7 +592,7 @@ optiga_lib_status_t trustm_ecdsa_sign(optiga_key_id_t optiga_key_id, uint8_t * d
592592
do
593593
{
594594
optiga_lib_status = OPTIGA_LIB_BUSY;
595-
return_status = optiga_crypt_ecdsa_sign(p_local_crypt, digest, digest_length, optiga_key_id, signature, signature_length);
595+
return_status = optiga_crypt_ecdsa_sign(me_crypt, digest, digest_length, optiga_key_id, signature, signature_length);
596596
if (OPTIGA_LIB_SUCCESS != return_status)
597597
{
598598
// optiga_crypt_ecdsa_sign api returns error !!!
@@ -657,7 +657,7 @@ optiga_lib_status_t trustm_ecdsa_verify(uint8_t * digest, uint8_t digest_length,
657657
}
658658

659659
optiga_lib_status = OPTIGA_LIB_BUSY;
660-
return_status = optiga_crypt_ecdsa_verify(p_local_crypt, digest, digest_length, signature, signature_length,
660+
return_status = optiga_crypt_ecdsa_verify(me_crypt, digest, digest_length, signature, signature_length,
661661
OPTIGA_CRYPT_HOST_DATA, &public_key_details);
662662
if (OPTIGA_LIB_SUCCESS != return_status)
663663
{
@@ -690,7 +690,7 @@ CHIP_ERROR trustmGetCertificate(uint16_t optiga_oid, uint8_t * buf, uint16_t * b
690690
do
691691
{
692692
optiga_lib_status = OPTIGA_LIB_BUSY;
693-
return_status = optiga_util_read_data(p_local_util, optiga_oid, 0, ifx_cert_hex, &ifx_cert_hex_len);
693+
return_status = optiga_util_read_data(me_util, optiga_oid, 0, ifx_cert_hex, &ifx_cert_hex_len);
694694
if (OPTIGA_LIB_SUCCESS != return_status)
695695
{
696696
// optiga_util_read_data api returns error !!!
@@ -730,7 +730,7 @@ optiga_lib_status_t trustm_ecdh_derive_secret(optiga_key_id_t optiga_key_id, uin
730730
do
731731
{
732732
optiga_lib_status = OPTIGA_LIB_BUSY;
733-
return_status = optiga_crypt_ecdh(p_local_crypt, optiga_key_id, &public_key_details, TRUE, shared_secret);
733+
return_status = optiga_crypt_ecdh(me_crypt, optiga_key_id, &public_key_details, TRUE, shared_secret);
734734
if (OPTIGA_LIB_SUCCESS != return_status)
735735
{
736736
// optiga_util_read_data api returns error !!!
@@ -764,7 +764,7 @@ optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen,
764764
// Calculate U1, U1 ends up in work
765765
optiga_lib_status = OPTIGA_LIB_BUSY;
766766
return_status =
767-
optiga_crypt_hmac(p_local_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, salt, (uint32_t) slen, work, &work_len);
767+
optiga_crypt_hmac(me_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, salt, (uint32_t) slen, work, &work_len);
768768
if (OPTIGA_LIB_SUCCESS != return_status)
769769
{
770770
// optiga_crypt_hmac api returns error !!!
@@ -782,7 +782,7 @@ optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen,
782782
{
783783
optiga_lib_status = OPTIGA_LIB_BUSY;
784784
// Calculated subsequent U, which ends up in md1
785-
return_status = optiga_crypt_hmac(p_local_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, md1, md1_len, md1, &md1_len);
785+
return_status = optiga_crypt_hmac(me_crypt, OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, md1, md1_len, md1, &md1_len);
786786
if (OPTIGA_LIB_SUCCESS != return_status)
787787
{
788788
// optiga_crypt_hmac api returns error !!!

0 commit comments

Comments
 (0)