Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ __docbook
*.xml
*.pu
_codeql_detected_source_root
format_report.txt
complexity_report.txt
quality_report.html
spell_report.txt
style_report.txt
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/STMicroelectronics/STSELib/compare/v1.1.7...HEAD) - v1.2.0

### Breaking Changes
- **C identifier naming migration** — C/H identifiers now use lowercase snake_case, including exported functions, typedefs, structure tags/members, and PAL callback members. Downstream applications and PAL implementations must update names to match the revised headers; no compatibility aliases are provided. Renamed exported functions change linker symbols and are binary-incompatible. Examples: `stse_Handle_t` to `stse_handle_t`, `stse_return_code_t` to `stse_return_code_t`, `stse_platform_Crc16_Calculate` to `stse_platform_crc16_calculate`, `stse_platform_Crc16_Accumulate` to `stse_platform_crc16_accumulate`, and `stse_platform_Delay_ms` to `stse_platform_delay_ms`.

- **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))
- **Renamed** `stse_handle_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))
- **API renamed** — all STSE host keys management functions must be updated in application code
- `stse_host_key_provisioning` to `stse_host_secure_channel_keys_provisioning`
- `stse_host_key_provisioning_wrapped` to `stse_host_secure_channel_keys_provisioning_wrapped`
Expand Down
238 changes: 137 additions & 101 deletions api/stse_asymmetric_keys_management.c
Original file line number Diff line number Diff line change
@@ -1,148 +1,184 @@
/*!
******************************************************************************
* \file stse_asymmetric_keys_management.c
* \brief STSE Asymmetric keys management API (sources)
* \author STMicroelectronics - SMD application team
*
******************************************************************************
* \attention
*
* <h2><center>&copy; COPYRIGHT 2023 STMicroelectronics</center></h2>
*
* This software is licensed under terms that can be found in the LICENSE file in
* the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
*****************************************************************************/
/**
******************************************************************************
* @file stse_asymmetric_keys_management.c
* @author CS Application Team
* @brief STSE Asymmetric keys management API (sources)
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include <stddef.h>
#include <string.h>

#include "api/stse_asymmetric_keys_management.h"

stse_ReturnCode_t stse_get_ecc_key_slots_count(
stse_Handle_t *pSTSE,
PLAT_UI8 *pPrivate_key_slot_count) {
stse_return_code_t stse_get_ecc_key_slots_count(
stse_handle_t *p_stse,
PLAT_UI8 *p_private_key_slot_count)
{
#ifdef STSE_CONF_STSAFE_A_SUPPORT
stse_ReturnCode_t ret;
stse_return_code_t ret;

if (pSTSE == NULL) {
return (STSE_API_HANDLER_NOT_INITIALISED);
}
/* Check STSE handler initialization */
if (p_stse == NULL)
{
return (STSE_API_HANDLER_NOT_INITIALISED);
}

if (pPrivate_key_slot_count == NULL) {
return (STSE_API_INVALID_PARAMETER);
}
/* Validate output parameter pointer */
if (p_private_key_slot_count == NULL)
{
return (STSE_API_INVALID_PARAMETER);
}

ret = stsafea_query_private_key_slots_count(pSTSE, pPrivate_key_slot_count);
/* Query private key slot count from target device */
ret = stsafea_query_private_key_slots_count(p_stse, p_private_key_slot_count);

return ret;
return ret;
#else
return STSE_API_INCOMPATIBLE_DEVICE_TYPE;
return STSE_API_INCOMPATIBLE_DEVICE_TYPE;
#endif /* STSE_CONF_STSAFE_A_SUPPORT */
}

stse_ReturnCode_t stse_get_ecc_key_slot_info(
stse_Handle_t *pSTSE,
PLAT_UI8 private_key_slot_number,
PLAT_UI16 *pGlobal_usage_limit,
stsafea_private_key_slot_information_t *private_key_slot_info) {
stse_return_code_t stse_get_ecc_key_slot_info(
stse_handle_t *p_stse,
PLAT_UI8 private_key_slot_number,
PLAT_UI16 *p_global_usage_limit,
stsafea_private_key_slot_information_t *private_key_slot_info)
{
#ifdef STSE_CONF_STSAFE_A_SUPPORT
stse_ReturnCode_t ret;

if (pSTSE == NULL) {
return (STSE_API_HANDLER_NOT_INITIALISED);
}

if (private_key_slot_info == NULL) {
return (STSE_API_INVALID_PARAMETER);
}

PLAT_UI8 private_key_slot_count;
PLAT_UI8 slot_info_index;

ret = stsafea_query_private_key_slots_count(pSTSE, &private_key_slot_count);

stsafea_private_key_slot_information_t private_key_table_info[private_key_slot_count];

ret = stsafea_query_private_key_table(pSTSE, private_key_slot_count, pGlobal_usage_limit, private_key_table_info);
stse_return_code_t ret;

/* Check STSE handler initialization */
if (p_stse == NULL)
{
return (STSE_API_HANDLER_NOT_INITIALISED);
}

/* Validate output parameter pointer */
if (private_key_slot_info == NULL)
{
return (STSE_API_INVALID_PARAMETER);
}

PLAT_UI8 private_key_slot_count;
PLAT_UI8 slot_info_index;
stsafea_private_key_slot_information_t private_key_table_info[STSAFEA_MAX_PRIVATE_KEY_SLOTS];

/* Query total number of private key slots available */
ret = stsafea_query_private_key_slots_count(p_stse, &private_key_slot_count);
if (ret != STSE_OK)
{
return ret;
}
if (private_key_slot_count > STSAFEA_MAX_PRIVATE_KEY_SLOTS)
{
return STSE_API_INVALID_PARAMETER;
}

if (ret != STSE_OK) {
return (ret);
}
/* Query private key table from target device */
ret = stsafea_query_private_key_table(p_stse, private_key_slot_count, p_global_usage_limit, private_key_table_info);

for (slot_info_index = 0; slot_info_index < private_key_slot_count; slot_info_index++) {
if (private_key_table_info[slot_info_index].slot_number == private_key_slot_number) {
memcpy(private_key_slot_info, &private_key_table_info[slot_info_index], sizeof(stsafea_private_key_slot_information_t));
break;
}
if (ret != STSE_OK)
{
return (ret);
}

/* Search for the matching private key slot number */
for (slot_info_index = 0; slot_info_index < private_key_slot_count; slot_info_index++)
{
if (private_key_table_info[slot_info_index].slot_number == private_key_slot_number)
{
/* Copy the found slot information to caller buffer */
memcpy(private_key_slot_info,
&private_key_table_info[slot_info_index],
sizeof(stsafea_private_key_slot_information_t));
break;
}
}

if (slot_info_index == private_key_slot_count) {
return (STSE_API_KEY_NOT_FOUND);
}
/* Return key not found if slot index was not found in table */
if (slot_info_index == private_key_slot_count)
{
return (STSE_API_KEY_NOT_FOUND);
}

return ret;
return ret;
#else
return STSE_API_INCOMPATIBLE_DEVICE_TYPE;
return STSE_API_INCOMPATIBLE_DEVICE_TYPE;
#endif /* STSE_CONF_STSAFE_A_SUPPORT */
}

stse_ReturnCode_t stse_sign_for_generic_public_key_slot(
stse_Handle_t *pSTSE,
stse_ecc_key_type_t private_key_type,
PLAT_UI8 *pPrivate_key,
stse_hash_algorithm_t hash_algo,
PLAT_UI16 payload_length,
PLAT_UI8 *pPayload,
PLAT_UI8 *pSignature) {
(void)pSTSE;
stse_return_code_t stse_sign_for_generic_public_key_slot(
stse_handle_t *p_stse,
stse_ecc_key_type_t private_key_type,
PLAT_UI8 *p_private_key,
stse_hash_algorithm_t hash_algo,
PLAT_UI16 payload_length,
PLAT_UI8 *p_payload,
PLAT_UI8 *p_signature)
{
(void)p_stse;
#if defined(STSE_CONF_USE_HOST_KEY_PROVISIONING_WRAPPED_AUTHENTICATED) || \
defined(STSE_CONF_USE_SYMMETRIC_KEY_ESTABLISHMENT_AUTHENTICATED) || \
defined(STSE_CONF_USE_SYMMETRIC_KEY_PROVISIONING_WRAPPED_AUTHENTICATED)

stse_ReturnCode_t ret;
PLAT_UI16 hash_length = stsafea_hash_info_table[hash_algo].length;
PLAT_UI8 hash_data[hash_length];
stse_return_code_t ret;
PLAT_UI16 hash_length = stsafea_hash_info_table[hash_algo].length;
PLAT_UI8 hash_data[STSAFEA_SHA_512_HASH_SIZE];

if (pPrivate_key == NULL || pPayload == NULL || pSignature == NULL ||
private_key_type >= STSE_ECC_KT_INVALID || hash_algo >= STSE_SHA_INVALID) {
return STSE_API_INVALID_PARAMETER;
}
/* Validate input parameters and algorithm bounds */
if (p_private_key == NULL || p_payload == NULL || p_signature == NULL ||
private_key_type >= STSE_ECC_KT_INVALID || hash_algo >= STSE_SHA_INVALID)
{
return STSE_API_INVALID_PARAMETER;
}

#ifdef STSE_CONF_ECC_EDWARD_25519
if (private_key_type != STSE_ECC_KT_ED25519) {
/* Ed25519 signs the payload directly without pre-hashing */
if (private_key_type != STSE_ECC_KT_ED25519)
{
#endif /* STSE_CONF_ECC_EDWARD_25519 */
/* - Hash the payload */
ret = stse_platform_hash_compute(
/* Compute the hash of the payload buffer */
ret = stse_platform_hash_compute(
hash_algo,
pPayload, payload_length,
p_payload, payload_length,
hash_data, &hash_length);

if (ret != STSE_OK) {
return (ret);
}
#ifdef STSE_CONF_ECC_EDWARD_25519
if (ret != STSE_OK)
{
return (ret);
}
#ifdef STSE_CONF_ECC_EDWARD_25519
}
#endif /* STSE_CONF_ECC_EDWARD_25519 */

/* - Sign the hash of concatenation of pub keys */
ret = stse_platform_ecc_sign(
private_key_type,
pPrivate_key,
/* Sign hash or payload using the platform ECC service */
ret = stse_platform_ecc_sign(
private_key_type,
p_private_key,
#ifdef STSE_CONF_ECC_EDWARD_25519
(private_key_type == STSE_ECC_KT_ED25519) ? pPayload : hash_data,
(private_key_type == STSE_ECC_KT_ED25519) ? payload_length : hash_length,
(private_key_type == STSE_ECC_KT_ED25519) ? p_payload : hash_data,
(private_key_type == STSE_ECC_KT_ED25519) ? payload_length : hash_length,
#else
hash_data,
hash_length,
#endif
pSignature);
hash_data,
hash_length,
#endif /* STSE_CONF_ECC_EDWARD_25519 */
p_signature);

return (ret);
return (ret);
#else
return STSE_SERVICE_INCOMPATIBLE_DEVICE_TYPE;
return STSE_SERVICE_INCOMPATIBLE_DEVICE_TYPE;
#endif /* STSE_CONF_USE_HOST_KEY_PROVISIONING_WRAPPED_AUTHENTICATED ||
STSE_CONF_USE_SYMMETRIC_KEY_ESTABLISHMENT_AUTHENTICATED ||
STSE_CONF_USE_SYMMETRIC_KEY_PROVISIONING_WRAPPED_AUTHENTICATED */
Expand Down
Loading