Skip to content

Commit 3b198ba

Browse files
committed
Add Tropic01_Deinit call in wolfCrypt_Cleanup for proper resource management
1 parent 172728b commit 3b198ba

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

wolfcrypt/src/port/tropicsquare/tropic01.c

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
#include <config.h>
2525
#endif
2626

27+
#include <wolfssl/wolfcrypt/settings.h>
28+
2729
#ifdef WOLFSSL_TROPIC01
2830

2931
#include <wolfssl/wolfcrypt/types.h>
30-
#include <wolfssl/wolfcrypt/settings.h>
3132
#include <wolfssl/wolfcrypt/cryptocb.h>
3233
#include <wolfssl/wolfcrypt/error-crypt.h>
3334
#include <wolfssl/wolfcrypt/logging.h>
@@ -60,7 +61,6 @@ static int Tropic01_GetRandom(byte* out, word32 sz)
6061
ret = lt_random_get(&g_h, out, sz);
6162
if (ret != LT_OK) {
6263
WOLFSSL_MSG_EX("TROPIC01: GetKey: Failed to retrieve key, ret=%d", ret);
63-
Tropic01_Deinit();
6464
return WC_HW_E;
6565
}
6666

@@ -82,24 +82,21 @@ static int Tropic01_GenerateKeyED25519(byte* pubkey, int keySlot, word32 sz)
8282
return BAD_FUNC_ARG;
8383

8484
ret = lt_ecc_key_erase(&g_h, keySlot);
85-
if(ret != LT_OK) {
85+
if (ret != LT_OK) {
8686
WOLFSSL_MSG_EX("TROPIC01: GetKey: Failed to erase key, ret=%d", ret);
87-
Tropic01_Deinit();
8887
return WC_HW_E;
8988
}
9089

9190
ret = lt_ecc_key_generate(&g_h, keySlot, CURVE_ED25519);
92-
if(ret != LT_OK) {
91+
if (ret != LT_OK) {
9392
WOLFSSL_MSG_EX("TROPIC01: GetKey: Failed to generate key, ret=%d", ret);
94-
Tropic01_Deinit();
9593
return WC_HW_E;
9694
}
9795
lt_ecc_curve_type_t curve = CURVE_ED25519;
9896
ecc_key_origin_t origin = CURVE_GENERATED;
9997
ret = lt_ecc_key_read(&g_h, keySlot, pubkey, sz, &curve, &origin);
100-
if(ret != LT_OK) {
98+
if (ret != LT_OK) {
10199
WOLFSSL_MSG_EX("TROPIC01: GetKey: Failed to read pub key, ret=%d", ret);
102-
Tropic01_Deinit();
103100
return WC_HW_E;
104101
}
105102

@@ -138,12 +135,11 @@ static int Tropic01_GetKeyAES(Aes* aes, int keySlot, word32 keySz)
138135
/* Retrieve key from TROPIC01 */
139136

140137
rett = lt_r_mem_data_read(&g_h, keySlot, (byte*)aes->key, keySz);
141-
if(rett != LT_OK) {
138+
if (rett != LT_OK) {
142139
WOLFSSL_MSG_EX(
143140
"TROPIC01: Get AES Key: Failed to retrieve key, ret=%d",
144141
rett
145142
);
146-
Tropic01_Deinit();
147143
return WC_HW_E;
148144
}
149145

@@ -180,12 +176,11 @@ static int Tropic01_GetKeyECC(byte* ecckey, int keySlot, word32 keySz)
180176
/* Retrieve key from TROPIC01 */
181177

182178
rett = lt_r_mem_data_read(&g_h, keySlot, (byte*)ecckey, keySz);
183-
if(rett != LT_OK) {
179+
if (rett != LT_OK) {
184180
WOLFSSL_MSG_EX(
185181
"TROPIC01: Get ECC Key: Failed to retrieve key, ret=%d",
186182
rett
187183
);
188-
Tropic01_Deinit();
189184
return WC_HW_E;
190185
}
191186

@@ -226,17 +221,18 @@ int Tropic01_CryptoCb(int devId, wc_CryptoInfo* info, void* ctx)
226221
ret = Tropic01_GetRandom(info->seed.seed, info->seed.sz);
227222
break;
228223
case WC_ALGO_TYPE_PK:
229-
#if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
224+
#ifdef HAVE_ED25519
225+
#ifdef HAVE_ED25519_MAKE_KEY
230226
if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
231227
WOLFSSL_MSG("TROPIC01: CryptoCB: ED25519 key generation request");
232228
ret = Tropic01_GenerateKeyED25519(
233229
info->pk.ed25519kg.key->p,
234230
TROPIC01_ED25519_ECC_SLOT_DEFAULT,
235231
info->pk.ed25519kg.size);
236-
237232
}
238-
#ifdef HAVE_ED25519_SIGN
239-
else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
233+
#endif /* HAVE_ED25519_MAKE_KEY */
234+
#ifdef HAVE_ED25519_SIGN
235+
if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
240236

241237
WOLFSSL_MSG("TROPIC01: CryptoCB: ED25519 signing request");
242238
/* retrieve private key from TROPIC01 secure R memory */
@@ -263,9 +259,9 @@ int Tropic01_CryptoCb(int devId, wc_CryptoInfo* info, void* ctx)
263259
/* reset devId */
264260
info->pk.ed25519sign.key->devId = devId;
265261
}
266-
#endif
267-
#ifdef HAVE_ED25519_VERIFY
268-
else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
262+
#endif /* HAVE_ED25519_SIGN */
263+
#ifdef HAVE_ED25519_VERIFY
264+
if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
269265
WOLFSSL_MSG("TROPIC01: CryptoCB: ED25519 verification request");
270266
/* retrieve public key from TROPIC01 secure R memory */
271267
ret = Tropic01_GetKeyECC(
@@ -291,13 +287,13 @@ int Tropic01_CryptoCb(int devId, wc_CryptoInfo* info, void* ctx)
291287
/* reset devId */
292288
info->pk.ed25519verify.key->devId = devId;
293289
}
294-
#endif /* HAVE_ED25519_VERIFY */
290+
#endif /* HAVE_ED25519_VERIFY */
295291
#endif /* HAVE_ED25519 */
296292
break;
297293
case WC_ALGO_TYPE_CIPHER:
298294
WOLFSSL_MSG("TROPIC01: CryptoCB: AES request ");
299295

300-
#if !defined(NO_AES) || !defined(NO_DES3)
296+
#if !defined(NO_AES)
301297
#ifdef HAVE_AESGCM
302298
if (info->cipher.type == WC_CIPHER_AES_GCM) {
303299
if (info->cipher.enc) {
@@ -410,7 +406,7 @@ int Tropic01_CryptoCb(int devId, wc_CryptoInfo* info, void* ctx)
410406
}
411407
}
412408
#endif /* HAVE_AES_CBC */
413-
#endif /* !NO_AES || !NO_DES3 */
409+
#endif /* !NO_AES */
414410
break;
415411

416412
default:
@@ -423,6 +419,7 @@ int Tropic01_CryptoCb(int devId, wc_CryptoInfo* info, void* ctx)
423419
/* Set TROPIC01 pairing keys */
424420
int Tropic01_SetPairingKeys(int kIndex, const byte* kPub, const byte* kPriv)
425421
{
422+
int i;
426423

427424
if (kPub == NULL || kPriv == NULL || kIndex < 0 || kIndex > 3) {
428425
WOLFSSL_MSG_EX("TROPIC01: SetPairingKeys: Invalid arguments");
@@ -433,7 +430,7 @@ int Tropic01_SetPairingKeys(int kIndex, const byte* kPub, const byte* kPriv)
433430
"TROPIC01: SetPairingKeys: Setting pairing key in slot %d",
434431
kIndex);
435432

436-
for (int i = 0; i < TROPIC01_PAIRING_KEY_SIZE; i++) {
433+
for (i = 0; i < TROPIC01_PAIRING_KEY_SIZE; i++) {
437434

438435
sh0priv[i] = kPriv[i];
439436
sh0pub[i] = kPub[i];
@@ -455,12 +452,12 @@ int Tropic01_Init(void)
455452

456453
g_ctx.initialized = 0;
457454
ret = lt_init(&g_h);
458-
if(ret != LT_OK) {
455+
if (ret != LT_OK) {
459456
WOLFSSL_MSG_EX("TROPIC01: lt_init failed with a code %d", ret);
460457
return WC_HW_E;
461458
}
462459
ret = verify_chip_and_start_secure_session(&g_h, sh0priv, sh0pub, pk_index);
463-
if(ret != LT_OK) {
460+
if (ret != LT_OK) {
464461
WOLFSSL_MSG_EX("TROPIC01: secure session failed with a code %d", ret);
465462
lt_deinit(&g_h);
466463
return WC_HW_E;
@@ -477,7 +474,7 @@ int Tropic01_Deinit(void)
477474

478475
if (g_ctx.initialized) {
479476
ret = lt_deinit(&g_h);
480-
if(ret != LT_OK) {
477+
if (ret != LT_OK) {
481478
WOLFSSL_MSG_EX("TROPIC01: lt_deinit failed with a code %d", ret);
482479
return WC_HW_E;
483480
}

wolfcrypt/src/wc_port.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ int wolfCrypt_Cleanup(void)
524524
#ifdef WOLFSSL_SILABS_SE_ACCEL
525525
ret = sl_se_deinit();
526526
#endif
527+
#if defined(WOLFSSL_TROPIC01)
528+
Tropic01_Deinit();
529+
#endif
527530
#if defined(WOLFSSL_RENESAS_TSIP)
528531
tsip_Close();
529532
#endif

0 commit comments

Comments
 (0)