Skip to content

Commit 942bfc6

Browse files
refactor(core): secure session keys using platform storage
Signed-off-by: Benjamin Grolleau <benjamin.grolleau@outlook.com>
1 parent 2f6a547 commit 942bfc6

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

core/stse_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ struct stse_session_t {
161161
union {
162162
struct {
163163
stse_Handler_t *pSTSE;
164-
PLAT_UI8 *pHost_MAC_key;
165-
PLAT_UI8 *pHost_cypher_key;
164+
PLAT_UI32 Host_MAC_key_idx;
165+
PLAT_UI32 Host_cypher_key_idx;
166166
stse_aes_key_type_t key_type;
167167
PLAT_UI32 MAC_counter;
168168
} host;

core/stse_platform.h

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,34 @@ stse_ReturnCode_t stse_platform_nist_kw_encrypt(PLAT_UI8 *pPayload, PLAT_UI32 pa
222222

223223
#if defined(STSE_CONF_USE_HOST_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_SYMMETRIC_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_HOST_SESSION)
224224

225+
/*!
226+
* \brief Store session key in platform secure storage
227+
* \param[in] pCypherKey Pointer to the cypher key
228+
* \param[out] pCypherKeyIdx Pointer to receive the index of the stored cypher key
229+
* \param[in] pMACKey Pointer to the MAC key
230+
* \param[out] pMACKeyIdx Pointer to receive the index of the stored MAC key
231+
* \param[in] key_length Length of the keys in bytes
232+
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
233+
*/
234+
stse_ReturnCode_t stse_platform_store_session_key(PLAT_UI8 *pCypherKey, PLAT_UI32 *pCypherKeyIdx,
235+
PLAT_UI8 *pMACKey, PLAT_UI32 *pMACKeyIdx,
236+
PLAT_UI16 key_length);
237+
238+
/*!
239+
* \brief Delete session key from platform secure storage
240+
* \param[in] CypherKeyIdx Index of the cypher key to delete
241+
* \param[in] MACKeyIdx Index of the MAC key to delete
242+
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
243+
*/
244+
stse_ReturnCode_t stse_platform_delete_key(PLAT_UI32 CypherKeyIdx, PLAT_UI32 MACKeyIdx);
245+
225246
/*!
226247
* \brief Initialize AES CMAC computation
227-
* \param[in] pKey Pointer to the key
228-
* \param[in] key_length Length of the key
248+
* \param[in] key_idx Index of the key in secure storage
229249
* \param[in] exp_tag_size Expected tag size
230250
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
231251
*/
232-
stse_ReturnCode_t stse_platform_aes_cmac_init(const PLAT_UI8 *pKey,
233-
PLAT_UI16 key_length,
252+
stse_ReturnCode_t stse_platform_aes_cmac_init(const PLAT_UI32 key_idx,
234253
PLAT_UI16 exp_tag_size);
235254

236255
/*!
@@ -260,76 +279,71 @@ stse_ReturnCode_t stse_platform_aes_cmac_verify_finish(PLAT_UI8 *pTag);
260279
* \brief Perform an AES CMAC encryption
261280
* \param[in] pPayload Pointer to Payload
262281
* \param[in] payload_length Length of the payload in bytes
263-
* \param[in] pKey Pointer to key
264-
* \param[in] key_length Length of the key in bytes
282+
* \param[in] key_idx Index of the key in secure storage
265283
* \param[in] exp_tag_size Expected tag size in bytes
266284
* \param[out] pTag Pointer to Tag
267285
* \param[out] pTag_length Pointer to Tag length value output
268286
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
269287
*/
270288
stse_ReturnCode_t stse_platform_aes_cmac_compute(const PLAT_UI8 *pPayload, PLAT_UI16 payload_length,
271-
const PLAT_UI8 *pKey, PLAT_UI16 key_length,
289+
const PLAT_UI32 key_idx,
272290
PLAT_UI16 exp_tag_size,
273291
PLAT_UI8 *pTag, PLAT_UI16 *pTag_length);
274292

275293
/*!
276294
* \brief Perform an AES CMAC decryption
277295
* \param[in] pPayload Pointer to Payload
278296
* \param[in] payload_length Length of the payload in bytes
279-
* \param[in] pKey Pointer to key
280-
* \param[in] key_length Length of the key in bytes
297+
* \param[in] key_idx Index of the key in secure storage
281298
* \param[in] pTag Pointer to Tag
282299
* \param[in] tag_length Pointer to Tag length value output
283300
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
284301
*/
285302
stse_ReturnCode_t stse_platform_aes_cmac_verify(const PLAT_UI8 *pPayload, PLAT_UI16 payload_length,
286-
const PLAT_UI8 *pKey, PLAT_UI16 key_length,
303+
const PLAT_UI32 key_idx,
287304
const PLAT_UI8 *pTag, PLAT_UI16 tag_length);
288305

289306
/*!
290307
* \brief Perform an AES CBC encryption
291308
* \param[in] pPlaintext Pointer to the plaintext data
292309
* \param[in] plaintext_length Length of the plaintext data
293310
* \param[in] pInitial_value Pointer to encryption IV
294-
* \param[in] pKey Pointer to the key
295-
* \param[in] key_length Length of the key
311+
* \param[in] key_idx Index of the key in secure storage
296312
* \param[out] pEncryptedtext Pointer to the encrypted payload
297313
* \param[out] pEncryptedtext_length Length of encrypted payload
298314
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
299315
*/
300316
stse_ReturnCode_t stse_platform_aes_cbc_enc(const PLAT_UI8 *pPlaintext, PLAT_UI16 plaintext_length,
301-
PLAT_UI8 *pInitial_value, const PLAT_UI8 *pKey,
302-
PLAT_UI16 key_length, PLAT_UI8 *pEncryptedtext,
317+
PLAT_UI8 *pInitial_value, const PLAT_UI32 key_idx,
318+
PLAT_UI8 *pEncryptedtext,
303319
PLAT_UI16 *pEncryptedtext_length);
304320

305321
/*!
306322
* \brief Perform an AES CBC decryption
307323
* \param[in] pEncryptedtext Pointer to the encrypted payload
308324
* \param[in] encryptedtext_length Length of encrypted payload
309325
* \param[in] pInitial_value Pointer to decryption IV
310-
* \param[in] pKey Pointer to the key
311-
* \param[in] key_length Length of the key
326+
* \param[in] key_idx Index of the key in secure storage
312327
* \param[out] pPlaintext Pointer to PlainText payload
313328
* \param[out] pPlaintext_length Length of the PlainText payload
314329
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
315330
*/
316331
stse_ReturnCode_t stse_platform_aes_cbc_dec(const PLAT_UI8 *pEncryptedtext, PLAT_UI16 encryptedtext_length,
317-
PLAT_UI8 *pInitial_value, const PLAT_UI8 *pKey,
318-
PLAT_UI16 key_length, PLAT_UI8 *pPlaintext,
332+
PLAT_UI8 *pInitial_value, const PLAT_UI32 key_idx,
333+
PLAT_UI8 *pPlaintext,
319334
PLAT_UI16 *pPlaintext_length);
320335

321336
/*!
322337
* \brief Perform an AES ECB encryption
323338
* \param[in] pPlaintext Pointer to the plaintext data
324339
* \param[in] plaintext_length Length of the plaintext data
325-
* \param[in] pKey Pointer to the key
326-
* \param[in] key_length Length of the key
340+
* \param[in] key_idx Index of the key in secure storage
327341
* \param[out] pEncryptedtext Pointer to the encrypted payload
328342
* \param[out] pEncryptedtext_length Length of encrypted payload
329343
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise
330344
*/
331345
stse_ReturnCode_t stse_platform_aes_ecb_enc(const PLAT_UI8 *pPlaintext, PLAT_UI16 plaintext_length,
332-
const PLAT_UI8 *pKey, PLAT_UI16 key_length,
346+
const PLAT_UI32 key_idx,
333347
PLAT_UI8 *pEncryptedtext, PLAT_UI16 *pEncryptedtext_length);
334348

335349
#endif /* defined(STSE_CONF_USE_HOST_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_SYMMETRIC_KEY_ESTABLISHMENT) || defined(STSE_CONF_USE_HOST_SESSION) */

services/stsafea/stsafea_sessions.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,21 @@ stse_ReturnCode_t stsafea_open_host_session(stse_Handler_t *pSTSE, stse_session_
7575
pSession->context.host.MAC_counter = ARRAY_3B_SWAP_TO_UI32(host_key_slot.cmac_sequence_counter);
7676
}
7777

78+
PLAT_UI32 HostMacKeyIdx;
79+
PLAT_UI32 HostCypherKeyIdx;
80+
PLAT_UI16 key_length = (pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE;
81+
82+
ret = stse_platform_store_session_key(pHost_cypher_key, &HostCypherKeyIdx, pHost_MAC_key, &HostMacKeyIdx, key_length);
83+
if (ret != STSE_OK) {
84+
return ret;
85+
}
86+
87+
memset(pHost_MAC_key, 0x00, key_length);
88+
memset(pHost_cypher_key, 0x00, key_length);
89+
7890
pSession->type = STSE_HOST_SESSION;
79-
pSession->context.host.pHost_MAC_key = pHost_MAC_key;
80-
pSession->context.host.pHost_cypher_key = pHost_cypher_key;
91+
pSession->context.host.Host_MAC_key_idx = HostMacKeyIdx;
92+
pSession->context.host.Host_cypher_key_idx = HostCypherKeyIdx;
8193
pSession->context.host.pSTSE = pSTSE;
8294
pSTSE->pActive_host_session = pSession;
8395

@@ -107,6 +119,12 @@ void stsafea_session_clear_context(stse_session_t *pSession) {
107119
return;
108120
}
109121

122+
if (pSession->context.host.Host_MAC_key_idx && pSession->context.host.Host_cypher_key_idx) {
123+
stse_platform_delete_key(pSession->context.host.Host_cypher_key_idx, pSession->context.host.Host_MAC_key_idx);
124+
pSession->context.host.Host_MAC_key_idx = 0x00;
125+
pSession->context.host.Host_cypher_key_idx = 0x00;
126+
}
127+
110128
/* - Clear session context */
111129
memset(pSession, 0x00, sizeof(stse_session_t));
112130
}
@@ -164,8 +182,7 @@ stse_ReturnCode_t stsafea_session_frame_encrypt(stse_session_t *pSession,
164182
/* - Perform first AES ECB round on IV */
165183
ret = stse_platform_aes_ecb_enc(initial_value,
166184
STSAFEA_HOST_AES_BLOCK_SIZE,
167-
pSession->context.host.pHost_cypher_key,
168-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
185+
pSession->context.host.Host_cypher_key_idx,
169186
initial_value,
170187
&encrypted_iv_len);
171188
if (ret != STSE_OK) {
@@ -196,8 +213,7 @@ stse_ReturnCode_t stsafea_session_frame_encrypt(stse_session_t *pSession,
196213
pEnc_payload_element->pData,
197214
pEnc_payload_element->length,
198215
initial_value,
199-
pSession->context.host.pHost_cypher_key,
200-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
216+
pSession->context.host.Host_cypher_key_idx,
201217
pEnc_payload_element->pData,
202218
&encrypted_payload_len);
203219
if (ret != 0) {
@@ -256,8 +272,7 @@ static stse_ReturnCode_t stsafea_session_frame_decrypt(stse_session_t *pSession,
256272
/* - Transform IV using AES ECB */
257273
ret = stse_platform_aes_ecb_enc(initial_value,
258274
STSAFEA_HOST_AES_BLOCK_SIZE,
259-
pSession->context.host.pHost_cypher_key,
260-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
275+
pSession->context.host.Host_cypher_key_idx,
261276
initial_value,
262277
&out_len);
263278

@@ -271,8 +286,7 @@ static stse_ReturnCode_t stsafea_session_frame_decrypt(stse_session_t *pSession,
271286
ret = stse_platform_aes_cbc_dec(decrypt_buffer,
272287
encrypted_payload_len,
273288
initial_value,
274-
pSession->context.host.pHost_cypher_key,
275-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
289+
pSession->context.host.Host_cypher_key_idx,
276290
decrypt_buffer,
277291
&decrypted_payload_len);
278292

@@ -328,8 +342,7 @@ static stse_ReturnCode_t stsafea_session_frame_c_mac_compute(stse_session_t *pSe
328342

329343
/*- Initialize AES C-MAC computation */
330344

331-
ret = stse_platform_aes_cmac_init(pSession->context.host.pHost_MAC_key,
332-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
345+
ret = stse_platform_aes_cmac_init(pSession->context.host.Host_MAC_key_idx,
333346
STSAFEA_MAC_SIZE);
334347
if (ret != STSE_OK) {
335348
return ret;
@@ -420,8 +433,7 @@ static stse_ReturnCode_t stsafea_session_frame_r_mac_verify(stse_session_t *pSes
420433

421434
/*- Initialize AES CMAC computation */
422435
stse_platform_aes_cmac_init(
423-
pSession->context.host.pHost_MAC_key,
424-
(pSession->context.host.key_type == STSE_AES_128_KT) ? STSE_AES_128_KEY_SIZE : STSE_AES_256_KEY_SIZE,
436+
pSession->context.host.Host_MAC_key_idx,
425437
STSAFEA_MAC_SIZE);
426438

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

0 commit comments

Comments
 (0)