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

Example for the mcuxClEcc component curve448 related functions.

Example for the mcuxClEcc component curve448 related functions

/*--------------------------------------------------------------------------*/
/* Copyright 2021-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 <mcuxClToolchain.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
#include <mcuxClExample_ELS_Helper.h>
#define MAX_CPUWA_SIZE MCUXCLCORE_MAX(MCUXCLRANDOMMODES_NCINIT_WACPU_SIZE,\
MCUXCLCORE_MAX(MCUXCLRANDOMMODES_INIT_WACPU_SIZE,\
MCUXCLCORE_MAX(MCUXCLECC_MONTDH_GENERATEKEYPAIR_CURVE448_WACPU_SIZE, \
MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE448_WACPU_SIZE)))
#define MAX_PKCWA_SIZE MCUXCLCORE_MAX(MCUXCLECC_MONTDH_GENERATEKEYPAIR_CURVE448_WAPKC_SIZE, \
MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE448_WAPKC_SIZE)
MCUXCLEXAMPLE_FUNCTION(mcuxClEcc_MontDH_Curve448_example)
{
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Setup one session to be used by all functions called */
mcuxClSession_Descriptor_t session;
//Allocate and initialize session with pkcWA on the beginning of PKC RAM
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(&session, MAX_CPUWA_SIZE, MAX_PKCWA_SIZE);
#if defined(MCUXCL_FEATURE_RANDOMMODES_SECSTRENGTH_256) && defined(MCUXCL_FEATURE_RANDOMMODES_CTRDRBG) && defined(MCUXCL_FEATURE_RANDOMMODES_PR_DISABLED) && defined(MCUXCL_FEATURE_RANDOMMODES_NORMALMODE)
/* NOTE: Key generation for Curve448 from a DRBG with 128 bit security strength is not recommended. */
/* Initialize the RNG and Initialize the PRNG */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_RNG(&session, MCUXCLRANDOMMODES_CTR_DRBG_AES256_CONTEXT_SIZE, mcuxClRandomModes_Mode_CtrDrbg_AES256_DRG3);
#else
/* Initialize the RNG and Initialize the PRNG */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_RNG(&session, 0u, mcuxClRandomModes_Mode_ELS_Drbg);
#endif /* MCUXCL_FEATURE_RANDOMMODES_SECSTRENGTH_256 && MCUXCL_FEATURE_RANDOMMODES_CTRDRBG && MCUXCL_FEATURE_RANDOMMODES_PR_DISABLED && MCUXCL_FEATURE_RANDOMMODES_NORMALMODE */
/* 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_CURVE448_SIZE_PRIVATEKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(aliceprivkeyinit_result, aliceprivkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("The pointer alicePrivKeyHandler is of the right type (mcuxClKey_Handle_t)")
/* mcuxClKey_Handle_t key */ alicePrivKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve448_PrivateKey,
/* const uint8_t * pKeyData */ alicePrivKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE448_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_CURVE448_SIZE_PUBLICKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(alicepubkeyinit_result, alicepubkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("The pointer alicePubKeyHandler is of the right type (mcuxClKey_Handle_t)")
/* mcuxClKey_Handle_t key */ alicePubKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve448_PublicKey,
/* const uint8_t * pKeyData */ alicePubKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE448_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_CURVE448_SIZE_PRIVATEKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bobprivkeyinit_result, bobprivkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("The pointer bobPrivKeyHandler is of the right type (mcuxClKey_Handle_t)")
/* mcuxClKey_Handle_t key */ bobPrivKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve448_PrivateKey,
/* const uint8_t * pKeyData */ bobPrivKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE448_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_CURVE448_SIZE_PUBLICKEY]={0};
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bobpubkeyinit_result, bobpubkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("bobPubKeyHandler has compatible type and cast was valid")
/* mcuxClKey_Handle_t key */ bobPubKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Ecc_MontDH_Curve448_PublicKey,
/* const uint8_t * pKeyData */ bobPubKeyBuffer,
/* uint32_t keyDataLength */ MCUXCLECC_MONTDH_CURVE448_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,
alicePrivKeyHandler,
alicePubKeyHandler) );
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEcc_MontDH_GenerateKeyPair) != alice_keygeneration_token) || (MCUXCLECC_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,
bobPrivKeyHandler,
bobPubKeyHandler) );
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEcc_MontDH_GenerateKeyPair) != bob_keygeneration_token) || (MCUXCLECC_STATUS_OK != bob_keygeneration_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Prepare input for Alice shared secret calculation */
ALIGNED uint8_t aliceSharedSecret[MCUXCLECC_MONTDH_CURVE448_SIZE_SHAREDSECRET];
uint32_t aliceSharedSecretSize;
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(alice_keyagreement_result, alice_keyagreement_token,
alicePrivKeyHandler,
bobPubKeyHandler,
aliceSharedSecret,
&aliceSharedSecretSize)
);
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEcc_MontDH_KeyAgreement) != alice_keyagreement_token) || (MCUXCLECC_STATUS_OK != alice_keyagreement_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Prepare input for Bob shared secret calculation */
ALIGNED uint8_t bobSharedSecret[MCUXCLECC_MONTDH_CURVE448_SIZE_SHAREDSECRET];
uint32_t bobSharedSecretSize;
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(bob_keyagreement_result, bob_keyagreement_token,
bobPrivKeyHandler,
alicePubKeyHandler,
bobSharedSecret,
&bobSharedSecretSize)
);
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEcc_MontDH_KeyAgreement) != bob_keyagreement_token) || (MCUXCLECC_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_CURVE448_SIZE_SHAREDSECRET; i++)
{
if(bobSharedSecret[i] != aliceSharedSecret[i])
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
}
if(!mcuxClExample_Session_Clean(&session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
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:53
Definition of function identifiers for the flow protection mechanism.
Definition of macros.
Top level header of mcuxClEcc component.
Top-level include file for the mcuxClKey component.
Type definitions of mcuxClPkc component.
Top level header of mcuxClRandomModes component.
Provides the API for the CSSL flow protection mechanism.
mcuxClEcc_Status_t mcuxClEcc_MontDH_KeyAgreement(mcuxClSession_Handle_t pSession, mcuxClKey_Handle_t key, mcuxClKey_Handle_t otherKey, uint8_t *pOut, uint32_t *const pOutLength)
Implements the MontDH key agreement according to rfc7748.
mcuxClEcc_Status_t mcuxClEcc_MontDH_GenerateKeyPair(mcuxClSession_Handle_t pSession, mcuxClKey_Handle_t privKey, mcuxClKey_Handle_t pubKey)
implements ECC key pair generation step for a MontDH key agreement according to rfc7748.
static const mcuxClKey_Type_t mcuxClKey_Type_Ecc_MontDH_Curve448_PrivateKey
Private key type pointer for ECC MontDH Curve448.
Definition mcuxClEcc_KeyTypes.h:1068
static const mcuxClKey_Type_t mcuxClKey_Type_Ecc_MontDH_Curve448_PublicKey
Public key type pointer for ECC MontDH Curve448.
Definition mcuxClEcc_KeyTypes.h:1076
#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
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:59
mcuxClKey_Status_t mcuxClKey_init(mcuxClSession_Handle_t pSession, mcuxClKey_Handle_t key, mcuxClKey_Type_t type, const uint8_t *pKeyData, uint32_t keyDataLength)
Initializes a key handle.
mcuxClKey_Descriptor_t *const mcuxClKey_Handle_t
Key handle type.
Definition mcuxClKey_Types.h:108
#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
#define MCUXCLECC_STATUS_OK
Operation was successful.
Definition mcuxClEcc_Constants.h:66