Skip to content

Commit 293d022

Browse files
authored
Merge pull request #408 from tropicsquare/ETR01SDK-459-Enhance-deinitialization-on-errors
ETR01SDK-459: Enhance deinitialization on errors
2 parents d1186a1 + def4f1c commit 293d022

File tree

9 files changed

+127
-84
lines changed

9 files changed

+127
-84
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Logging: `lt_port_log` function for platform-specific logging mechanism; is used by the logging macros declared in `libtropic_logging.h`.
2323
- CAL: `lt_sha256_deinit` function to deinitialize the SHA-256 context. It is called after the SHA-256 operation is finalized, so it must do an exhaustive cleanup.
2424
- ESP-IDF HAL for Espressif SoCs.
25+
- Missing secure memory zeroing to `lt_in__session_start()` and `lt_hkdf()` (internal function).
26+
- Missing check of `lt_handle_t.l3.session_status` in `lt_in__ecc_key_generate()`.
2527

2628
### Fixed
2729
- `lt_print_bytes` function now returns `LT_PARAM_ERR` when incorrect parameters are passed instead of `LT_FAIL`.
2830
- `lt_print_fw_header` function now returns `LT_PARAM_ERR` when incorrect bank ID is used instead of `LT_FAIL`.
2931
- Linux SPI HAL: If SPI mode 0 is not supported, cleanup and return with an error.
32+
- If `lt_init()` fails, it performs the needed cleanup itself -> the user should call `lt_deinit()` only after `lt_init()` succeeds.
3033

3134
### Removed
3235
- Logging: Redundant/unused macros `LT_LOG`, `LT_LOG_RESULT`, `LT_LOG_VALUE`.
3336
- Arduino HAL: Removed `rng_seed` from `lt_dev_arduino_t`, as it should be user's responsibility to initialize the PRNG.
3437
- Arduino HAL: Removed `SPI.begin()` and `SPI.end()` calls (fixes [this](https://github.com/tropicsquare/libtropic-arduino/issues/15) issue). It is now expected that users initialize SPI in their code themselves.
38+
- Redundant checks of `lt_handle_t.l3.session_status` in `lt_l3_encrypt_request()` and `lt_l3_decrypt_response()`.
3539

3640
## [3.0.0]
3741

src/libtropic.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ lt_ret_t lt_init(lt_handle_t *h)
4242
return LT_PARAM_ERR;
4343
}
4444

45-
lt_ret_t ret;
45+
lt_ret_t ret, ret_unused;
4646

4747
// When compiling libtropic with l3 buffer embedded into handle,
4848
// define buffer's length here (later used to prevent overflow during communication).
@@ -59,21 +59,31 @@ lt_ret_t lt_init(lt_handle_t *h)
5959

6060
ret = lt_crypto_ctx_init(h->l3.crypto_ctx);
6161
if (ret != LT_OK) {
62-
return ret;
62+
goto l1_cleanup;
6363
}
6464

6565
// Prevent usage of insufficient buffer.
6666
if (h->l3.buff_len < LT_SIZE_OF_L3_BUFF) {
67-
return LT_L3_BUFFER_TOO_SMALL;
67+
ret = LT_L3_BUFFER_TOO_SMALL;
68+
goto crypto_ctx_cleanup;
6869
}
6970

7071
// Initialize the TROPIC01 attributes based on its Application FW.
7172
ret = lt_init_tr01_attrs(h);
7273
if (ret != LT_OK) {
73-
return ret;
74+
goto crypto_ctx_cleanup;
7475
}
7576

7677
return LT_OK;
78+
79+
crypto_ctx_cleanup:
80+
ret_unused = lt_crypto_ctx_deinit(&h->l3.crypto_ctx);
81+
82+
l1_cleanup:
83+
ret_unused = lt_l1_deinit(&h->l2);
84+
LT_UNUSED(ret_unused);
85+
86+
return ret;
7787
}
7888

7989
lt_ret_t lt_deinit(lt_handle_t *h)
@@ -415,21 +425,21 @@ lt_ret_t lt_session_start(lt_handle_t *h, const uint8_t *stpub, const lt_pkey_in
415425

416426
lt_ret_t ret = lt_out__session_start(h, pkey_index, &host_eph_keys);
417427
if (ret != LT_OK) {
418-
goto lt_session_start_cleanup;
428+
goto cleanup;
419429
}
420430

421431
ret = lt_l2_send(&h->l2);
422432
if (ret != LT_OK) {
423-
goto lt_session_start_cleanup;
433+
goto cleanup;
424434
}
425435
ret = lt_l2_receive(&h->l2);
426436
if (ret != LT_OK) {
427-
goto lt_session_start_cleanup;
437+
goto cleanup;
428438
}
429439

430440
ret = lt_in__session_start(h, stpub, pkey_index, shipriv, shipub, &host_eph_keys);
431441

432-
lt_session_start_cleanup:
442+
cleanup:
433443
lt_secure_memzero(&host_eph_keys, sizeof(lt_host_eph_keys_t));
434444
return ret;
435445
}

0 commit comments

Comments
 (0)