36
36
#include " pal_os_timer.h"
37
37
#include < FreeRTOS.h>
38
38
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 ;
41
41
static bool trustm_isOpen = false ;
42
42
#define ENABLE_HMAC_MULTI_STEP (0 )
43
43
#define OPTIGA_UTIL_DER_BITSTRING_TAG (0x03 )
@@ -57,7 +57,6 @@ void vApplicationTickHook(void);
57
57
58
58
void vApplicationTickHook (void )
59
59
{
60
- pal_os_event_trigger_registered_callback ();
61
60
}
62
61
63
62
#define WAIT_FOR_COMPLETION (ret ) \
@@ -112,45 +111,47 @@ void trustm_Open(void)
112
111
{
113
112
uint16_t dOptigaOID = 0xE0C4 ;
114
113
// Maximum Power, Minimum Current limitation
115
- uint8_t cCurrentLimit = 15 ;
114
+ const uint8_t current_limit [] = {
115
+ 0x0F ,
116
+ };
116
117
117
118
if (!trustm_isOpen)
118
119
{
119
120
optiga_lib_status_t return_status;
120
121
do
121
122
{
122
123
// Create Optiga crypt instance
123
- if (p_local_crypt == NULL )
124
+ if (me_crypt == NULL )
124
125
{
125
- p_local_crypt = optiga_crypt_create (0 , optiga_crypt_callback, NULL );
126
- if (NULL == p_local_crypt )
126
+ me_crypt = optiga_crypt_create (0 , optiga_crypt_callback, NULL );
127
+ if (NULL == me_crypt )
127
128
{
128
129
break ;
129
130
}
130
131
}
131
132
else
132
133
{
133
- printf (" Error: p_local_crypt already initialised\n " );
134
+ printf (" Error: me_crypt already initialised\n " );
134
135
}
135
136
// Create Optiga Util instance
136
- if (p_local_util == NULL )
137
+ if (me_util == NULL )
137
138
{
138
- p_local_util = optiga_util_create (0 , optiga_util_callback, NULL );
139
- if (NULL == p_local_util )
139
+ me_util = optiga_util_create (0 , optiga_util_callback, NULL );
140
+ if (NULL == me_util )
140
141
{
141
142
break ;
142
143
}
143
144
}
144
145
else
145
146
{
146
- printf (" Error: p_local_crypt already initialised\n " );
147
+ printf (" Error: me_crypt already initialised\n " );
147
148
}
148
149
/* *
149
150
* Open the application on OPTIGA which is a precondition to perform any other operations
150
151
* using optiga_util_open_application
151
152
*/
152
153
optiga_lib_status = OPTIGA_LIB_BUSY;
153
- return_status = optiga_util_open_application (p_local_util , 0 ); // skip restore
154
+ return_status = optiga_util_open_application (me_util , 0 ); // skip restore
154
155
if (OPTIGA_LIB_SUCCESS != return_status)
155
156
{
156
157
printf (" optiga_util_open_application api returns error %02X\n " , return_status);
@@ -165,27 +166,17 @@ void trustm_Open(void)
165
166
printf (" optiga_util_open_application failed\n " );
166
167
break ;
167
168
}
169
+ trustm_isOpen = true ;
170
+
168
171
// Only run once for initialisation
169
- if (init)
172
+ if (! init)
170
173
{
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)
173
- {
174
- printf (" optiga_util_write_data api returns error %02X\n " , return_status);
175
- break ;
176
- }
177
- WAIT_FOR_COMPLETION (return_status);
178
- if (OPTIGA_LIB_SUCCESS != return_status)
179
- {
180
- printf (" optiga_util_write_data returns error\n " );
181
- break ;
182
- }
174
+ // Set current limit for high performance
175
+ write_data (dOptigaOID, current_limit, sizeof (current_limit));
183
176
// Set init to true
184
177
init = true ;
185
178
}
186
179
} while (0 );
187
-
188
- trustm_isOpen = true ;
189
180
}
190
181
}
191
182
@@ -200,7 +191,7 @@ void trustm_close(void)
200
191
* using optiga_util_close_application
201
192
*/
202
193
optiga_lib_status = OPTIGA_LIB_BUSY;
203
- return_status = optiga_util_close_application (p_local_util , 0 );
194
+ return_status = optiga_util_close_application (me_util , 0 );
204
195
if (OPTIGA_LIB_SUCCESS != return_status)
205
196
{
206
197
printf (" optiga_util_close_application api returns error %02X\n " , return_status);
@@ -217,11 +208,11 @@ void trustm_close(void)
217
208
}
218
209
219
210
// destroy util and crypt instances
220
- optiga_util_destroy (p_local_util );
221
- optiga_crypt_destroy (p_local_crypt );
211
+ optiga_util_destroy (me_util );
212
+ optiga_crypt_destroy (me_crypt );
222
213
pal_os_event_destroy (NULL );
223
- p_local_util = NULL ;
224
- p_local_crypt = NULL ;
214
+ me_util = NULL ;
215
+ me_crypt = NULL ;
225
216
trustm_isOpen = false ;
226
217
return_status = OPTIGA_LIB_SUCCESS;
227
218
} while (0 );
@@ -241,7 +232,7 @@ void read_certificate_from_optiga(uint16_t optiga_oid, char * cert_pem, uint16_t
241
232
do
242
233
{
243
234
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);
235
+ return_status = optiga_util_read_data (me_util , optiga_oid, 0 , ifx_cert_hex, &ifx_cert_hex_len);
245
236
if (OPTIGA_LIB_SUCCESS != return_status)
246
237
{
247
238
printf (" optiga_util_read_data api returns error %02X\n " , return_status);
@@ -292,7 +283,7 @@ void write_data(uint16_t optiga_oid, const uint8_t * p_data, uint16_t length)
292
283
do
293
284
{
294
285
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);
286
+ return_status = optiga_util_write_data (me_util , optiga_oid, OPTIGA_UTIL_ERASE_AND_WRITE, 0 , p_data, length);
296
287
if (OPTIGA_LIB_SUCCESS != return_status)
297
288
{
298
289
printf (" optiga_util_write_data api returns error %02X\n " , return_status);
@@ -316,7 +307,7 @@ void write_metadata(uint16_t optiga_oid, const uint8_t * p_data, uint8_t length)
316
307
do
317
308
{
318
309
optiga_lib_status = OPTIGA_LIB_BUSY;
319
- return_status = optiga_util_write_metadata (p_local_util , optiga_oid, p_data, length);
310
+ return_status = optiga_util_write_metadata (me_util , optiga_oid, p_data, length);
320
311
if (OPTIGA_LIB_SUCCESS != return_status)
321
312
{
322
313
printf (" optiga_util_write_metadata api returns error %02X\n " , return_status);
@@ -339,7 +330,7 @@ optiga_lib_status_t deriveKey_HKDF(const uint8_t * salt, uint16_t salt_length, c
339
330
do
340
331
{
341
332
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 */
333
+ return_status = optiga_crypt_hkdf (me_crypt , OPTIGA_HKDF_SHA_256, TRUSTM_HKDF_OID_KEY, /* Input secret OID */
343
334
salt, salt_length, info, info_length, derived_key_length, TRUE , derived_key);
344
335
if (OPTIGA_LIB_SUCCESS != return_status)
345
336
{
@@ -369,7 +360,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
369
360
// If the size is less than the max length supported
370
361
if (input_data_length <= MAX_MAC_DATA_LEN)
371
362
{
372
- return_status = optiga_crypt_hmac (p_local_crypt , type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
363
+ return_status = optiga_crypt_hmac (me_crypt , type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
373
364
if (OPTIGA_LIB_SUCCESS != return_status)
374
365
{
375
366
// optiga_crypt_hmac returns error !!!
@@ -391,7 +382,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
391
382
uint32_t dataLenTemp = 0 ;
392
383
uint32_t remainingLen = input_data_length;
393
384
// 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);
385
+ return_status = optiga_crypt_hmac_start (me_crypt , type, TRUSTM_HMAC_OID_KEY, input_data, MAX_MAC_DATA_LEN);
395
386
if (OPTIGA_LIB_SUCCESS != return_status)
396
387
{
397
388
// optiga_crypt_hmac returns error !!!
@@ -415,7 +406,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
415
406
{
416
407
optiga_lib_status = OPTIGA_LIB_BUSY;
417
408
return_status =
418
- optiga_crypt_hmac_update (p_local_crypt , (input_data + (input_data_length - remainingLen)), dataLenTemp);
409
+ optiga_crypt_hmac_update (me_crypt , (input_data + (input_data_length - remainingLen)), dataLenTemp);
419
410
if (OPTIGA_LIB_SUCCESS != return_status)
420
411
{
421
412
// optiga_crypt_hmac_update returns error !!!
@@ -436,7 +427,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
436
427
// End HMAC sequence and return the MAC generated
437
428
// printf("HMAC Finalize\n");
438
429
optiga_lib_status = OPTIGA_LIB_BUSY;
439
- return_status = optiga_crypt_hmac_finalize (p_local_crypt , (input_data + (input_data_length - remainingLen)),
430
+ return_status = optiga_crypt_hmac_finalize (me_crypt , (input_data + (input_data_length - remainingLen)),
440
431
dataLenTemp, mac, mac_length);
441
432
if (OPTIGA_LIB_SUCCESS != return_status)
442
433
{
@@ -456,7 +447,7 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
456
447
}
457
448
#else
458
449
459
- return_status = optiga_crypt_hmac (p_local_crypt , type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
450
+ return_status = optiga_crypt_hmac (me_crypt , type, TRUSTM_HMAC_OID_KEY, input_data, input_data_length, mac, mac_length);
460
451
if (OPTIGA_LIB_SUCCESS != return_status)
461
452
{
462
453
// optiga_crypt_hmac returns error !!!
@@ -482,7 +473,7 @@ optiga_lib_status_t optiga_crypt_rng(uint8_t * random_data, uint16_t random_data
482
473
do
483
474
{
484
475
optiga_lib_status = OPTIGA_LIB_BUSY;
485
- return_status = optiga_crypt_random (p_local_crypt , OPTIGA_RNG_TYPE_DRNG, random_data, random_data_length);
476
+ return_status = optiga_crypt_random (me_crypt , OPTIGA_RNG_TYPE_DRNG, random_data, random_data_length);
486
477
if (OPTIGA_LIB_SUCCESS != return_status)
487
478
{
488
479
// optiga_crypt_random returns error !!!
@@ -514,7 +505,7 @@ optiga_lib_status_t trustm_ecc_keygen(uint16_t optiga_key_id, uint8_t key_type,
514
505
do
515
506
{
516
507
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),
508
+ return_status = optiga_crypt_ecc_generate_keypair (me_crypt , curve_id, key_type, FALSE , &optiga_key_id, (pubkey + i),
518
509
pubkey_length);
519
510
if (OPTIGA_LIB_SUCCESS != return_status)
520
511
{
@@ -541,7 +532,7 @@ void trustmGetKey(uint16_t optiga_oid, uint8_t * pubkey, uint16_t * pubkeyLen)
541
532
do
542
533
{
543
534
optiga_lib_status = OPTIGA_LIB_BUSY;
544
- return_status = optiga_util_read_data (p_local_util , optiga_oid, offset, pubkey, pubkeyLen);
535
+ return_status = optiga_util_read_data (me_util , optiga_oid, offset, pubkey, pubkeyLen);
545
536
if (OPTIGA_LIB_SUCCESS != return_status)
546
537
{
547
538
// optiga_util_read_data api returns error !!!
@@ -566,7 +557,7 @@ optiga_lib_status_t trustm_hash(uint8_t * msg, uint16_t msg_length, uint8_t * di
566
557
hash_data_host.buffer = msg;
567
558
hash_data_host.length = msg_length;
568
559
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);
560
+ return_status = optiga_crypt_hash (me_crypt , OPTIGA_HASH_TYPE_SHA_256, OPTIGA_CRYPT_HOST_DATA, &hash_data_host, digest);
570
561
if (OPTIGA_LIB_SUCCESS != return_status)
571
562
{
572
563
// optiga_crypt_hash api returns error !!!
@@ -592,7 +583,7 @@ optiga_lib_status_t trustm_ecdsa_sign(optiga_key_id_t optiga_key_id, uint8_t * d
592
583
do
593
584
{
594
585
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);
586
+ return_status = optiga_crypt_ecdsa_sign (me_crypt , digest, digest_length, optiga_key_id, signature, signature_length);
596
587
if (OPTIGA_LIB_SUCCESS != return_status)
597
588
{
598
589
// optiga_crypt_ecdsa_sign api returns error !!!
@@ -657,7 +648,7 @@ optiga_lib_status_t trustm_ecdsa_verify(uint8_t * digest, uint8_t digest_length,
657
648
}
658
649
659
650
optiga_lib_status = OPTIGA_LIB_BUSY;
660
- return_status = optiga_crypt_ecdsa_verify (p_local_crypt , digest, digest_length, signature, signature_length,
651
+ return_status = optiga_crypt_ecdsa_verify (me_crypt , digest, digest_length, signature, signature_length,
661
652
OPTIGA_CRYPT_HOST_DATA, &public_key_details);
662
653
if (OPTIGA_LIB_SUCCESS != return_status)
663
654
{
@@ -690,7 +681,7 @@ CHIP_ERROR trustmGetCertificate(uint16_t optiga_oid, uint8_t * buf, uint16_t * b
690
681
do
691
682
{
692
683
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);
684
+ return_status = optiga_util_read_data (me_util , optiga_oid, 0 , ifx_cert_hex, &ifx_cert_hex_len);
694
685
if (OPTIGA_LIB_SUCCESS != return_status)
695
686
{
696
687
// optiga_util_read_data api returns error !!!
@@ -730,7 +721,7 @@ optiga_lib_status_t trustm_ecdh_derive_secret(optiga_key_id_t optiga_key_id, uin
730
721
do
731
722
{
732
723
optiga_lib_status = OPTIGA_LIB_BUSY;
733
- return_status = optiga_crypt_ecdh (p_local_crypt , optiga_key_id, &public_key_details, TRUE , shared_secret);
724
+ return_status = optiga_crypt_ecdh (me_crypt , optiga_key_id, &public_key_details, TRUE , shared_secret);
734
725
if (OPTIGA_LIB_SUCCESS != return_status)
735
726
{
736
727
// optiga_util_read_data api returns error !!!
@@ -764,7 +755,7 @@ optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen,
764
755
// Calculate U1, U1 ends up in work
765
756
optiga_lib_status = OPTIGA_LIB_BUSY;
766
757
return_status =
767
- optiga_crypt_hmac (p_local_crypt , OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, salt, (uint32_t ) slen, work, &work_len);
758
+ optiga_crypt_hmac (me_crypt , OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, salt, (uint32_t ) slen, work, &work_len);
768
759
if (OPTIGA_LIB_SUCCESS != return_status)
769
760
{
770
761
// optiga_crypt_hmac api returns error !!!
@@ -782,7 +773,7 @@ optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen,
782
773
{
783
774
optiga_lib_status = OPTIGA_LIB_BUSY;
784
775
// 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);
776
+ return_status = optiga_crypt_hmac (me_crypt , OPTIGA_HMAC_SHA_256, TRUSTM_HMAC_OID_KEY, md1, md1_len, md1, &md1_len);
786
777
if (OPTIGA_LIB_SUCCESS != return_status)
787
778
{
788
779
// optiga_crypt_hmac api returns error !!!
0 commit comments