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

Example for montgomery curves 384bits key pairs generating and public exporting.

Example for montgomery curves 384bits key pairs generating and public exporting

/*--------------------------------------------------------------------------*/
/* Copyright 2022-2024 NXP */
/* */
/* SPDX-License-Identifier: BSD-3-Clause */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions are */
/* met: */
/* */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* */
/* 2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the */
/* documentation and/or other materials provided with the distribution. */
/* */
/* 3. Neither the name of the copyright holder nor the names of its */
/* contributors may be used to endorse or promote products derived from */
/* this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS */
/* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
/* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
/* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/*--------------------------------------------------------------------------*/
#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>
#include <mcuxClExample_ELS_Helper.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_mont_curve25519_example)
{
/* Enable ELS */
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**********************************************************************************************/
/****************************** Example *****************************************************/
/****************** Generate Curve25519 montgomery key pairs**********************************/
/**********************************************************************************************/
/* Set up PSA key attributes. */
psa_key_attributes_t keygenAttr = {
.core = { // Core attributes
.type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY), // Keypair family with curve montgomery
.bits = 256U, // Key bits of a Curve25519
.lifetime = LIFETIME_EXTERNAL, // Volatile (RAM), S50 Temporary Storage for private key
.id = 0U, // ID zero
.policy = {
.usage = PSA_ALG_NONE, // Key may be used for sign message or hash
.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[MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY] = {0U};
size_t key_buffer_size = MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY;
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 != MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**********************************************************************************************/
/*************************** Example ********************************************************/
/***************Export Curve25519 montgomery public key****************************************/
/**********************************************************************************************/
/* Set up PSA key attributes. */
psa_key_attributes_t exportAttr = {
.core = { // Core attributes
.type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY), // Keypair family with curve montgomery
.bits = 256U, // Key bits of a Curve25519
.lifetime = LIFETIME_EXTERNAL, // Volatile (RAM), S50 Temporary Storage for private key
.id = 0U, // ID zero
.policy = {
.usage = PSA_KEY_USAGE_EXPORT, // Key may be used for sign message or hash
.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_MONTGOMERY), 256u)] = {0U};
size_t data_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_MONTGOMERY), 256u);
MCUX_CSSL_ANALYSIS_STOP_PATTERN_EXTERNAL_MACRO()
size_t data_length = 0U;
status = psa_driver_wrapper_export_public_key(
&exportAttr,
key_buffer, MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY,
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 the ELS */
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Return */
return MCUXCLEXAMPLE_STATUS_OK;
}
static const uint8_t data[]
Example data buffer.
Definition mcuxClCrc_Crc16_example.c:55
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:53
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:130
#define MCUXCLELS_RESET_DO_NOT_CANCEL
Set this option at mcuxClEls_ResetOption_t to abort the requested command if another ELS operation is...
Definition mcuxClEls_Common.h:139
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:191
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:601
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:755
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:636