MCUX CLNS
MCUX Crypto Library Normal Secure
mcuxClPsaDriver_keygen_export_public_key_brainpoolpr1_example.c

Example for brainpool_p_r1 384bits 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)
bool mcuxClPsaDriver_keygen_export_public_key_brainpoolpr1_example(void)
{
/* 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 BRAINPOOL_P_R1 384bits curve key pairs**********************/
/**********************************************************************************************/
psa_key_attributes_t keygenAttr = {
.core = { // Core attributes
.type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_BRAINPOOL_P_R1), // Keypair family with curve BRAINPOOL_P_R1
.bits = MCUXCLKEY_SIZE_384 * 8u, // Key bits of BRAINPOOL_P_R1_384
.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 */
uint8_t key_buffer[MCUXCLKEY_SIZE_384] = {0U};
size_t key_buffer_size = MCUXCLKEY_SIZE_384;
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_384)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**********************************************************************************************/
/************************************* Example *********************************************/
/************************ Export BRAINPOOL_P_R1 curve public key **************************/
/**********************************************************************************************/
psa_key_attributes_t exportAttr = {
.core = { // Core attributes
.type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_BRAINPOOL_P_R1), // Keypair family with curve BRAINPOOL_P_R1
.bits = MCUXCLKEY_SIZE_384 * 8u, // Key bits of BRAINPOOL_P_R1
.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 */
uint8_t data[PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_BRAINPOOL_P_R1), MCUXCLKEY_SIZE_384 * 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_BRAINPOOL_P_R1), MCUXCLKEY_SIZE_384 * 8u); //byteLenP =32U
size_t data_length = 0U;
status = psa_driver_wrapper_export_public_key(
&exportAttr,
key_buffer, MCUXCLKEY_SIZE_384,
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;
}