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

Example for the mcuxClEcc component Curve25519 related functions.

Example for the mcuxClEcc component Curve25519 related functions

/*--------------------------------------------------------------------------*/
/* Copyright 2021-2025 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 <mcuxClToolchain.h>
#include <mcuxClRandom.h>
#include <mcuxClEcc.h>
#include <mcuxClKey.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#define MAX_CPUWA_SIZE MCUXCLCORE_MAX(MCUXCLRANDOM_NCINIT_WACPU_SIZE, \
MCUXCLCORE_MAX(MCUXCLRANDOMMODES_INIT_WACPU_SIZE, \
MCUXCLCORE_MAX(MCUXCLECC_MONTDH_GENERATEKEYPAIR_CURVE25519_WACPU_SIZE, \
MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE25519_WACPU_SIZE)))
#define MAX_PKCWA_SIZE MCUXCLCORE_MAX(MCUXCLECC_MONTDH_GENERATEKEYPAIR_CURVE25519_WAPKC_SIZE, \
MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE25519_WAPKC_SIZE)
MCUXCLEXAMPLE_FUNCTION(mcuxClEcc_MontDH_Curve25519_example)
{
/* Setup one session to be used by all functions called */
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MAX_CPUWA_SIZE, MAX_PKCWA_SIZE);
/* Initialize the RNG context and initialize the PRNG */
#if defined(MCUXCL_FEATURE_RANDOMMODES_SECSTRENGTH_256)
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_RNG(session, MCUXCLRANDOMMODES_CTR_DRBG_AES256_CONTEXT_SIZE, mcuxClRandomModes_Mode_CtrDrbg_AES256_DRG3);
#elif defined(MCUXCL_FEATURE_RANDOMMODES_SECSTRENGTH_128)
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_RNG(session, MCUXCLRANDOMMODES_CTR_DRBG_AES128_CONTEXT_SIZE, mcuxClRandomModes_Mode_CtrDrbg_AES128_DRG3);
#else
#error "Example not supported for target"
#endif
/* Prepare input for Alice key generation */
uint32_t alicePrivKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t alicePrivKeyHandler = (mcuxClKey_Handle_t) &alicePrivKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
ALIGNED uint8_t alicePrivKeyBuffer[MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(aliceprivkeyinit_result, aliceprivkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ session,
/* mcuxClKey_Handle_t key */ alicePrivKeyHandler,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve25519_PrivateKey,
/* const uint8_t * pKeyData */ alicePrivKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != aliceprivkeyinit_token) || (MCUXCLKEY_STATUS_OK != aliceprivkeyinit_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
uint32_t alicePubKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t alicePubKeyHandler = (mcuxClKey_Handle_t) &alicePubKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
ALIGNED uint8_t alicePubKeyBuffer[MCUXCLECC_MONTDH_CURVE25519_SIZE_PUBLICKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(alicepubkeyinit_result, alicepubkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ session,
/* mcuxClKey_Handle_t key */ alicePubKeyHandler,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve25519_PublicKey,
/* const uint8_t * pKeyData */ alicePubKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE25519_SIZE_PUBLICKEY));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != alicepubkeyinit_token) || (MCUXCLKEY_STATUS_OK != alicepubkeyinit_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Prepare input for Bob key generation */
uint32_t bobPrivKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t bobPrivKeyHandler = (mcuxClKey_Handle_t) &bobPrivKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
ALIGNED uint8_t bobPrivKeyBuffer[MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bobprivkeyinit_result, bobprivkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ session,
/* mcuxClKey_Handle_t key */ bobPrivKeyHandler,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve25519_PrivateKey,
/* const uint8_t * pKeyData */ bobPrivKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE25519_SIZE_PRIVATEKEY));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != bobprivkeyinit_token) || (MCUXCLKEY_STATUS_OK != bobprivkeyinit_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
uint32_t bobPubKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t bobPubKeyHandler = (mcuxClKey_Handle_t) &bobPubKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
ALIGNED uint8_t bobPubKeyBuffer[MCUXCLECC_MONTDH_CURVE25519_SIZE_PUBLICKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bobpubkeyinit_result, bobpubkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ session,
/* mcuxClKey_Handle_t key */ bobPubKeyHandler,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve25519_PublicKey,
/* const uint8_t * pKeyData */ bobPubKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE25519_SIZE_PUBLICKEY));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != bobpubkeyinit_token) || (MCUXCLKEY_STATUS_OK != bobpubkeyinit_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Call Dh KeyGeneration for Alice keys generation and check FP and return code */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(alice_keygeneration_result, alice_keygeneration_token,
mcuxClKey_Generation_MontDH,
alicePrivKeyHandler,
alicePubKeyHandler));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_generate_keypair) != alice_keygeneration_token) || (MCUXCLKEY_STATUS_OK != alice_keygeneration_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Call Dh KeyGeneration for Bob keys generation and check FP and return code */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bob_keygeneration_result, bob_keygeneration_token,
mcuxClKey_Generation_MontDH,
bobPrivKeyHandler,
bobPubKeyHandler));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_generate_keypair) != bob_keygeneration_token) || (MCUXCLKEY_STATUS_OK != bob_keygeneration_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Prepare input for Alice shared secret calculation */
ALIGNED uint8_t aliceSharedSecret[MCUXCLECC_MONTDH_CURVE25519_SIZE_SHAREDSECRET];
uint32_t aliceSharedSecretSize = 0u;
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(alice_keyagreement_result, alice_keyagreement_token,
&mcuxClKey_AgreementDescriptor_MontDH,
alicePrivKeyHandler,
bobPubKeyHandler,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_NULL_POINTER_CONSTANT("NULL is used in code")
NULL,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_NULL_POINTER_CONSTANT()
0u,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_USE_UNINITIALIZED_VALUE("Buffer used as output")
aliceSharedSecret,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_USE_UNINITIALIZED_VALUE()
&aliceSharedSecretSize));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_agreement) != alice_keyagreement_token) || (MCUXCLKEY_STATUS_OK != alice_keyagreement_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Prepare input for Bob shared secret calculation */
ALIGNED uint8_t bobSharedSecret[MCUXCLECC_MONTDH_CURVE25519_SIZE_SHAREDSECRET];
uint32_t bobSharedSecretSize;
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bob_keyagreement_result, bob_keyagreement_token,
&mcuxClKey_AgreementDescriptor_MontDH,
bobPrivKeyHandler,
alicePubKeyHandler,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_NULL_POINTER_CONSTANT("NULL is used in code")
NULL,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_NULL_POINTER_CONSTANT()
0u,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_USE_UNINITIALIZED_VALUE("Buffer used as output")
bobSharedSecret,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_USE_UNINITIALIZED_VALUE()
&bobSharedSecretSize));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_agreement) != bob_keyagreement_token) || (MCUXCLKEY_STATUS_OK != bob_keyagreement_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Alice's shared secret shall be equal to Bob's shared secret */
for(size_t i = 0u; i < MCUXCLECC_MONTDH_CURVE25519_SIZE_SHAREDSECRET; i++)
{
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClKey_agreement")
if(bobSharedSecret[i] != aliceSharedSecret[i])
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
}
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
Definition of function identifiers for the flow protection mechanism.
Top level header of mcuxClEcc component.
Top-level include file for the mcuxClKey component.
Top level header of mcuxClRandom component.
Top level header of mcuxClRandomModes component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:60
mcuxClKey_Status_t mcuxClKey_init(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, mcuxClKey_Type_t type, const uint8_t *pKeyData, uint32_t keyDataLength)
Initializes a key handle.
mcuxClKey_Status_t mcuxClKey_generate_keypair(mcuxClSession_Handle_t session, mcuxClKey_Generation_t generation, mcuxClKey_Handle_t privKey, mcuxClKey_Handle_t pubKey)
Key-pair generation function.
mcuxClKey_Status_t mcuxClKey_agreement(mcuxClSession_Handle_t session, mcuxClKey_Agreement_t agreement, mcuxClKey_Handle_t key, mcuxClKey_Handle_t otherKey, mcuxClKey_Agreement_AdditionalInput_t additionalInputs[], uint32_t numberOfInputs, uint8_t *pOut, uint32_t *const pOutLength)
Key agreement function.
mcuxClKey_Descriptor_t *const mcuxClKey_Handle_t
Key handle type.
Definition mcuxClKey_Types.h:111
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:118
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:643
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:797
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:678