MCUX CLNS
MCUX Crypto Library Normal Secure
 
Loading...
Searching...
No Matches
mcuxClPsaDriver_keygen_export_public_key_secpk1_example.c

Example for SECP_K1 256bits curve key pairs generating and public exporting.

Example for SECP_K1 256bits curve key pairs generating and public exporting

/*--------------------------------------------------------------------------*/
/* Copyright 2022-2023 NXP */
/* */
/* NXP Confidential. This software is owned or controlled by NXP and may */
/* only be used strictly in accordance with the applicable license terms. */
/* By expressly accepting such terms or by downloading, installing, */
/* activating and/or otherwise using the software, you are agreeing that */
/* you have read, and that you agree to comply with and are bound by, such */
/* license terms. If you do not agree to be bound by the applicable license */
/* terms, then you may not retain, install, activate or otherwise use the */
/* software. */
/*--------------------------------------------------------------------------*/
#include "common.h"
#include <mcuxClEls.h> // Interface to the entire mcuxClEls component
#include <mcuxClSession.h> // Interface to the entire mcuxClSession component
#include <mcuxClKey.h> // Interface to the entire mcuxClKey component
#include <mcuxCsslFlowProtection.h> // Code flow protection
#include <mcuxClToolchain.h> // memory segment definitions
#include <stdbool.h> // bool type for the example's return code
#include <mcuxClCore_Examples.h>
#include <mcuxClEcc.h>
#include <mcuxClPkc.h>
#define LIFETIME_INTERNAL PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_LIFETIME_VOLATILE, PSA_KEY_LOCATION_EXTERNAL_STORAGE)
#define LIFETIME_EXTERNAL PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_LIFETIME_VOLATILE, PSA_KEY_LOCATION_LOCAL_STORAGE)
MCUXCLEXAMPLE_FUNCTION(mcuxClPsaDriver_keygen_export_public_key_secpk1_example)
{
/* Enable ELS */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_Enable_Async()); // Enable the ELS.
// mcuxClEls_Enable_Async is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_WaitForOperation(MCUXCLELS_ERROR_FLAGS_CLEAR)); // Wait for the mcuxClEls_Enable_Async operation to complete.
// mcuxClEls_WaitForOperation is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**********************************************************************************************/
/************************************* Example **********************************************/
/************************** Generate SECP_K1 256bits curve key pairs**************************/
/**********************************************************************************************/
psa_key_attributes_t keygenAttr = {
.core = { // Core attributes
.type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_K1), // Keypair family with curve SECP_K1
.bits = MCUXCLKEY_SIZE_256 * 8u, // Key bits of SECP_K1_P256
.lifetime = LIFETIME_EXTERNAL, // Volatile (RAM), S50 Temporary Storage for private key
.id = 0U, // ID zero
.policy = {
.usage = PSA_ALG_NONE,
.alg = PSA_ALG_ECDSA_ANY,
.alg2 = PSA_ALG_NONE},
.flags = 0U}, // No flags
.domain_parameters = NULL,
.domain_parameters_size = 0U};
/* Call generate_key operation */
ALIGNED uint8_t key_buffer[MCUXCLKEY_SIZE_256] = {0U};
size_t key_buffer_size = MCUXCLKEY_SIZE_256;
size_t key_buffer_length = 0U;
psa_status_t status = psa_driver_wrapper_generate_key(
&keygenAttr,
key_buffer, key_buffer_size, &key_buffer_length);
/* Check the return value */
if(status != PSA_SUCCESS)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check the output length */
if(key_buffer_length != MCUXCLKEY_SIZE_256)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**********************************************************************************************/
/************************************* Example *********************************************/
/************************ Export SECP_K1 curve public key *********************************/
/**********************************************************************************************/
psa_key_attributes_t exportAttr = {
.core = { // Core attributes
.type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_K1), // Keypair family with curve SECP_K1
.bits = MCUXCLKEY_SIZE_256 * 8u, // Key bits of SECP_K1
.lifetime = LIFETIME_EXTERNAL, // Volatile (RAM), S50 Temporary Storage for private key
.id = 0U, // ID zero
.policy = {
.usage = PSA_KEY_USAGE_EXPORT,
.alg = PSA_ALG_ECDSA_ANY,
.alg2 = PSA_ALG_NONE},
.flags = 0U}, // No flags
.domain_parameters = NULL,
.domain_parameters_size = 0U
};
/* Call export_public_key operation */
MCUX_CSSL_ANALYSIS_START_PATTERN_EXTERNAL_MACRO()
ALIGNED uint8_t data[PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_K1), MCUXCLKEY_SIZE_256 * 8u)] = {0U}; //2u * sizeof(prime_p)
size_t data_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_K1), MCUXCLKEY_SIZE_256 * 8u); //byteLenP =32U
MCUX_CSSL_ANALYSIS_STOP_PATTERN_EXTERNAL_MACRO()
size_t data_length = 0U;
status = psa_driver_wrapper_export_public_key(
&exportAttr,
key_buffer, MCUXCLKEY_SIZE_256,
data, data_size, &data_length);
/* Check the return value */
if(status != PSA_SUCCESS)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check the output length */
if(data_length != data_size)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_WaitForOperation(MCUXCLELS_ERROR_FLAGS_CLEAR)); // Wait for the mcuxClEls_KeyDelete_Async operation to complete.
// mcuxClEls_LimitedWaitForOperation is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Disable ELS */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_Disable()); // Disable the ELS.
// mcuxClEls_Disable is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Return */
return MCUXCLEXAMPLE_STATUS_OK;
}
static mcuxClEls_EccByte_t ecc_public_key_client[MCUXCLELS_ECC_PUBLICKEY_SIZE] ALIGNED
Destination buffer to receive the public key of the mcuxClEls_EccKeyGen_Async operation.
Definition mcuxClEls_Tls_Master_Key_Session_Keys_example.c:33
Top level header of mcuxClEcc component.
Top-level include file for the ELS driver.
Top-level include file for the mcuxClKey component.
Top level header of mcuxClPkc component (PKC hardware driver)
Additional macros for the ARM PSA driver.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLELS_ERROR_FLAGS_CLEAR
Set this option at mcuxClEls_ErrorHandling_t to clear all ELS error flags.
Definition mcuxClEls_Common.h:110
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_Enable_Async(void)
Enables the ELS.
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_Disable(void)
Disable the ELS.
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_WaitForOperation(mcuxClEls_ErrorHandling_t errorHandling)
Wait for an ELS operation and optionally clear the error status.
#define MCUXCLELS_STATUS_OK
No error occurred.
Definition mcuxClEls_Types.h:171
#define MCUXCLELS_STATUS_OK_WAIT
An _Async function successfully started an ELS command. Call mcuxClEls_WaitForOperation to complete i...
Definition mcuxClEls_Types.h:172
#define MCUXCLKEY_SIZE_256
256 bit key, size in bytes
Definition mcuxClKey_Constants.h:106
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:581
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:735
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:616