Skip to content

Commit 85e0e30

Browse files
Grolleau-BenjaminGrom-
authored andcommitted
[core, services] move session keys to platform secure storage
Signed-off-by: Benjamin Grolleau <benjamin.grolleau@outlook.com>
1 parent da3ba98 commit 85e0e30

6 files changed

Lines changed: 67 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
- **Renamed** `stse_Handler_t` to `stse_Handle_t` — all references to the handle type must be updated in application code ([31fb0fa](https://github.com/STMicroelectronics/STSELib/commit/31fb0fa9865828d82479e8c40fa3499c68b33fe9))
1818
- **API change** — all platform-level SecureElement initialization functions now require an additional `void *pArg` parameter ([51aa3f8](https://github.com/STMicroelectronics/STSELib/commit/51aa3f8114d33adfe38da6fd261e4b9a71a1fa9b))
19+
- **Platform AES API change** — all platform AES cryptographic functions now reference keys by secure storage index (`PLAT_UI32 key_idx`) instead of raw key pointer and length (`PLAT_UI8 *pKey, PLAT_UI16 key_length`). Affected functions: `stse_platform_aes_cmac_init`, `stse_platform_aes_cmac_compute`, `stse_platform_aes_cmac_verify`, `stse_platform_aes_cbc_enc`, `stse_platform_aes_cbc_dec`, `stse_platform_aes_ecb_enc`
20+
- **Renamed** `stsafea_open_host_session` to `stsafea_open_host_session_from_idx` — signature updated to accept key indices (`PLAT_UI32 host_MAC_key_idx`, `PLAT_UI32 host_cypher_key_idx`) instead of raw key pointers
1921

2022
### Added
2123

24+
- `stse_platform_store_aes_key()` — new platform function to store an AES key into platform secure storage and retrieve its index
25+
- `stse_platform_delete_aes_key()` — new platform function to delete an AES key from platform secure storage by index
26+
2227
### Changed
2328

2429
### Deprecated
2530

2631
### Removed
32+
- `stsafea_open_host_session` function. Now replaced by `stsafea_open_host_session_from_idx` which accepts key indices instead of raw key pointers. Note that you must first store the AES keys in platform secure storage using `stse_platform_store_aes_key()` to obtain the required indices before opening a host session.
2733

2834
### Fixed
2935

3036
### Security
37+
- AES keys are now securely stored in platform secure storage and referenced by index, eliminating the need to handle raw key material in application memory and enhancing overall security.
3138

3239
---
3340

core/stse_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ struct stse_session_t {
160160
union {
161161
struct {
162162
stse_Handle_t *pSTSE;
163-
PLAT_UI8 *pHost_MAC_key;
164-
PLAT_UI8 *pHost_cypher_key;
163+
PLAT_UI32 host_MAC_key_idx;
164+
PLAT_UI32 host_cypher_key_idx;
165165
stse_aes_key_type_t key_type;
166166
PLAT_UI32 MAC_counter;
167167
} host;

core/stse_generic_typedef.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ typedef enum stse_aes_key_type_t {
111111
STSE_AES_256_KT
112112
} stse_aes_key_type_t;
113113

114+
/*!
115+
* \enum stse_aes_key_usage_t
116+
* \brief STSE AES key usage
117+
*/
118+
typedef enum stse_aes_key_usage_t {
119+
STSE_AES_KEY_USAGE_MAC = 0, /**< Key used for MAC operations (CMAC) */
120+
STSE_AES_KEY_USAGE_CIPHER /**< Key used for cipher operations (CBC, ECB) */
121+
} stse_aes_key_usage_t;
122+
114123
/*!
115124
* \enum stse_hash_algorithm_t
116125
* \brief STSE Hash algorithm type

core/stse_platform.h

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,29 @@ stse_ReturnCode_t stse_platform_nist_kw_encrypt(PLAT_UI8 *pPayload, PLAT_UI32 pa
215215
#if defined(STSE_CONF_USE_HOST_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_SYMMETRIC_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_HOST_SESSION)
216216

217217
/*!
218-
* \brief Initialize AES CMAC computation
219-
* \param[in] pKey Pointer to the key
218+
* \brief Store aes key in platform secure storage
219+
* \param[in] pKey Pointer to the aes key
220220
* \param[in] key_length Length of the key
221+
* \param[in] usage Key usage type, used by the platform to set the appropriate key attributes in secure storage
222+
* \param[out] pKey_idx Pointer to the index assigned to the stored key in secure storage
223+
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
224+
*/
225+
stse_ReturnCode_t stse_platform_store_aes_key(PLAT_UI8 *pKey, PLAT_UI16 key_length, stse_aes_key_usage_t usage, PLAT_UI32 *pKey_idx);
226+
227+
/*!
228+
* \brief Delete aes key from platform secure storage
229+
* \param[in] key_idx Index of the key to delete from secure storage
230+
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
231+
*/
232+
stse_ReturnCode_t stse_platform_delete_aes_key(PLAT_UI32 key_idx);
233+
234+
/*!
235+
* \brief Initialize AES CMAC computation
236+
* \param[in] key_idx Index of the key to use for AES CMAC computation in secure storage
221237
* \param[in] exp_tag_size Expected tag size
222238
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
223239
*/
224-
stse_ReturnCode_t stse_platform_aes_cmac_init(const PLAT_UI8 *pKey,
225-
PLAT_UI16 key_length,
240+
stse_ReturnCode_t stse_platform_aes_cmac_init(const PLAT_UI32 key_idx,
226241
PLAT_UI16 exp_tag_size);
227242

228243
/*!
@@ -252,77 +267,69 @@ stse_ReturnCode_t stse_platform_aes_cmac_verify_finish(PLAT_UI8 *pTag);
252267
* \brief Perform an AES CMAC encryption
253268
* \param[in] pPayload Pointer to Payload
254269
* \param[in] payload_length Length of the payload in bytes
255-
* \param[in] pKey Pointer to key
256-
* \param[in] key_length Length of the key in bytes
270+
* \param[in] key_idx Index of the key to use for AES CMAC computation in secure storage
257271
* \param[in] exp_tag_size Expected tag size in bytes
258272
* \param[out] pTag Pointer to Tag
259273
* \param[out] pTag_length Pointer to Tag length value output
260274
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
261275
*/
262276
stse_ReturnCode_t stse_platform_aes_cmac_compute(const PLAT_UI8 *pPayload, PLAT_UI16 payload_length,
263-
const PLAT_UI8 *pKey, PLAT_UI16 key_length,
277+
const PLAT_UI32 key_idx,
264278
PLAT_UI16 exp_tag_size,
265279
PLAT_UI8 *pTag, PLAT_UI16 *pTag_length);
266280

267281
/*!
268282
* \brief Perform an AES CMAC decryption
269283
* \param[in] pPayload Pointer to Payload
270284
* \param[in] payload_length Length of the payload in bytes
271-
* \param[in] pKey Pointer to key
272-
* \param[in] key_length Length of the key in bytes
285+
* \param[in] key_idx Index of the key to use for AES CMAC computation in secure storage
273286
* \param[in] pTag Pointer to Tag
274287
* \param[in] tag_length Pointer to Tag length value output
275288
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
276289
*/
277290
stse_ReturnCode_t stse_platform_aes_cmac_verify(const PLAT_UI8 *pPayload, PLAT_UI16 payload_length,
278-
const PLAT_UI8 *pKey, PLAT_UI16 key_length,
291+
const PLAT_UI32 key_idx,
279292
const PLAT_UI8 *pTag, PLAT_UI16 tag_length);
280293

281294
/*!
282295
* \brief Perform an AES CBC encryption
283296
* \param[in] pPlaintext Pointer to the plaintext data
284297
* \param[in] plaintext_length Length of the plaintext data
285298
* \param[in] pInitial_value Pointer to encryption IV
286-
* \param[in] pKey Pointer to the key
287-
* \param[in] key_length Length of the key
299+
* \param[in] key_idx Index of the key to use for AES encryption in secure storage
288300
* \param[out] pEncryptedtext Pointer to the encrypted payload
289301
* \param[out] pEncryptedtext_length Length of encrypted payload
290302
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
291303
*/
292304
stse_ReturnCode_t stse_platform_aes_cbc_enc(const PLAT_UI8 *pPlaintext, PLAT_UI16 plaintext_length,
293-
PLAT_UI8 *pInitial_value, const PLAT_UI8 *pKey,
294-
PLAT_UI16 key_length, PLAT_UI8 *pEncryptedtext,
305+
PLAT_UI8 *pInitial_value, const PLAT_UI32 key_idx, PLAT_UI8 *pEncryptedtext,
295306
PLAT_UI16 *pEncryptedtext_length);
296307

297308
/*!
298309
* \brief Perform an AES CBC decryption
299310
* \param[in] pEncryptedtext Pointer to the encrypted payload
300311
* \param[in] encryptedtext_length Length of encrypted payload
301312
* \param[in] pInitial_value Pointer to decryption IV
302-
* \param[in] pKey Pointer to the key
303-
* \param[in] key_length Length of the key
313+
* \param[in] key_idx Index of the key to use for AES decryption in secure storage
304314
* \param[out] pPlaintext Pointer to PlainText payload
305315
* \param[out] pPlaintext_length Length of the PlainText payload
306316
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
307317
*/
308318
stse_ReturnCode_t stse_platform_aes_cbc_dec(const PLAT_UI8 *pEncryptedtext, PLAT_UI16 encryptedtext_length,
309-
PLAT_UI8 *pInitial_value, const PLAT_UI8 *pKey,
310-
PLAT_UI16 key_length, PLAT_UI8 *pPlaintext,
319+
PLAT_UI8 *pInitial_value, const PLAT_UI32 key_idx, PLAT_UI8 *pPlaintext,
311320
PLAT_UI16 *pPlaintext_length);
312321

313322
/*!
314323
* \brief Perform an AES ECB encryption
315324
* \param[in] pPlaintext Pointer to the plaintext data
316325
* \param[in] plaintext_length Length of the plaintext data
317-
* \param[in] pKey Pointer to the key
318-
* \param[in] key_length Length of the key
326+
* \param[in] key_idx Index of the key to use for AES encryption in secure storage
319327
* \param[out] pEncryptedtext Pointer to the encrypted payload
320328
* \param[out] pEncryptedtext_length Length of encrypted payload
321329
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
322330
*/
323331
stse_ReturnCode_t stse_platform_aes_ecb_enc(const PLAT_UI8 *pPlaintext, PLAT_UI16 plaintext_length,
324-
const PLAT_UI8 *pKey, PLAT_UI16 key_length,
325-
PLAT_UI8 *pEncryptedtext, PLAT_UI16 *pEncryptedtext_length);
332+
const PLAT_UI32 key_idx, PLAT_UI8 *pEncryptedtext, PLAT_UI16 *pEncryptedtext_length);
326333

327334
#endif /* defined(STSE_CONF_USE_HOST_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_SYMMETRIC_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_HOST_SESSION) */
328335

services/stsafea/stsafea_sessions.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
#ifdef STSE_CONF_USE_HOST_SESSION
4343

44-
stse_ReturnCode_t stsafea_open_host_session(stse_Handle_t *pSTSE, stse_session_t *pSession, PLAT_UI8 *pHost_MAC_key, PLAT_UI8 *pHost_cypher_key) {
44+
stse_ReturnCode_t stsafea_open_host_session_from_idx(stse_Handle_t *pSTSE, stse_session_t *pSession, PLAT_UI32 host_MAC_key_idx, PLAT_UI32 host_cypher_key_idx) {
4545
stse_ReturnCode_t ret;
4646

4747
if (pSTSE == NULL) {
@@ -81,8 +81,8 @@ stse_ReturnCode_t stsafea_open_host_session(stse_Handle_t *pSTSE, stse_session_t
8181
}
8282

8383
pSession->type = STSE_HOST_SESSION;
84-
pSession->context.host.pHost_MAC_key = pHost_MAC_key;
85-
pSession->context.host.pHost_cypher_key = pHost_cypher_key;
84+
pSession->context.host.host_MAC_key_idx = host_MAC_key_idx;
85+
pSession->context.host.host_cypher_key_idx = host_cypher_key_idx;
8686
pSession->context.host.pSTSE = pSTSE;
8787
pSTSE->pActive_host_session = pSession;
8888

@@ -169,8 +169,7 @@ stse_ReturnCode_t stsafea_session_frame_encrypt(stse_session_t *pSession,
169169
/* - Perform first AES ECB round on IV */
170170
ret = stse_platform_aes_ecb_enc(initial_value,
171171
STSAFEA_HOST_AES_BLOCK_SIZE,
172-
pSession->context.host.pHost_cypher_key,
173-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
172+
pSession->context.host.host_cypher_key_idx,
174173
initial_value,
175174
&encrypted_iv_len);
176175
if (ret != STSE_OK) {
@@ -201,8 +200,7 @@ stse_ReturnCode_t stsafea_session_frame_encrypt(stse_session_t *pSession,
201200
pEnc_payload_element->pData,
202201
pEnc_payload_element->length,
203202
initial_value,
204-
pSession->context.host.pHost_cypher_key,
205-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
203+
pSession->context.host.host_cypher_key_idx,
206204
pEnc_payload_element->pData,
207205
&encrypted_payload_len);
208206
if (ret != 0) {
@@ -261,8 +259,7 @@ static stse_ReturnCode_t stsafea_session_frame_decrypt(stse_session_t *pSession,
261259
/* - Transform IV using AES ECB */
262260
ret = stse_platform_aes_ecb_enc(initial_value,
263261
STSAFEA_HOST_AES_BLOCK_SIZE,
264-
pSession->context.host.pHost_cypher_key,
265-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
262+
pSession->context.host.host_cypher_key_idx,
266263
initial_value,
267264
&out_len);
268265

@@ -276,8 +273,7 @@ static stse_ReturnCode_t stsafea_session_frame_decrypt(stse_session_t *pSession,
276273
ret = stse_platform_aes_cbc_dec(decrypt_buffer,
277274
encrypted_payload_len,
278275
initial_value,
279-
pSession->context.host.pHost_cypher_key,
280-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
276+
pSession->context.host.host_cypher_key_idx,
281277
decrypt_buffer,
282278
&decrypted_payload_len);
283279

@@ -333,8 +329,7 @@ static stse_ReturnCode_t stsafea_session_frame_c_mac_compute(stse_session_t *pSe
333329

334330
/*- Initialize AES C-MAC computation */
335331

336-
ret = stse_platform_aes_cmac_init(pSession->context.host.pHost_MAC_key,
337-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
332+
ret = stse_platform_aes_cmac_init(pSession->context.host.host_MAC_key_idx,
338333
STSAFEA_MAC_SIZE);
339334
if (ret != STSE_OK) {
340335
return ret;
@@ -425,8 +420,7 @@ static stse_ReturnCode_t stsafea_session_frame_r_mac_verify(stse_session_t *pSes
425420

426421
/*- Initialize AES CMAC computation */
427422
stse_platform_aes_cmac_init(
428-
pSession->context.host.pHost_MAC_key,
429-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
423+
pSession->context.host.host_MAC_key_idx,
430424
STSAFEA_MAC_SIZE);
431425

432426
/*- Perform First AES-CMAC round */

services/stsafea/stsafea_sessions.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@
2626
#include "core/stse_return_codes.h"
2727

2828
/*!
29-
* \brief This Core function Create a session context and associate it to STSAFE handler
30-
* \param[in] *pSession \ref stse_session_t Pointer to session
31-
* \param[in] *pHost_MAC_key Pointer to MAC key buffer to be used under the session
32-
* \param[in] *pHost_cypher_key Pointer to cypher key buffer to be used under the session
29+
* \brief This Core function creates a session context and associates it to STSAFE handler using keys stored in platform secure storage
30+
* \param[in] *pSTSE Pointer to STSE handler
31+
* \param[in] *pSession \ref stse_session_t Pointer to session
32+
* \param[in] host_MAC_key_idx Index of the MAC key in platform secure storage to be used under the session
33+
* \param[in] host_cypher_key_idx Index of the cypher key in platform secure storage to be used under the session
3334
* \return \ref STSE_OK on success ; \ref stse_ReturnCode_t error code otherwise
34-
* \details \include{doc} stsafe_erase_context.dox
35+
* \details \include{doc} stsafe_erase_context.dox
36+
* \details \include{doc} stse_platform_store_aes_key.dox
3537
*/
36-
stse_ReturnCode_t stsafea_open_host_session(stse_Handle_t *pSTSE,
37-
stse_session_t *pSession,
38-
PLAT_UI8 *pHost_MAC_key,
39-
PLAT_UI8 *pHost_cypher_key);
38+
stse_ReturnCode_t stsafea_open_host_session_from_idx(stse_Handle_t *pSTSE,
39+
stse_session_t *pSession,
40+
PLAT_UI32 host_MAC_key_idx,
41+
PLAT_UI32 host_cypher_key_idx);
4042

4143
/*!
4244
* \brief This Core function Close an existing host session context

0 commit comments

Comments
 (0)