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-2024 NXP */
/* */
/* NXP Proprietary. 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 <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
#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_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)
{
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Setup one session to be used by all functions called */
//Allocate and initialize session with pkcWA on the beginning of PKC RAM
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(&session, MAX_CPUWA_SIZE, MAX_PKCWA_SIZE);
/* Initialize the RNG and Initialize the PRNG */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_RNG(&session, 0u, mcuxClRandomModes_Mode_ELS_Drbg);
/* 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,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("alicePrivKeyHandler has compatible type and cast was valid")
/* mcuxClKey_Handle_t key */ alicePrivKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* 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,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("alicePubKeyHandler has compatible type and cast was valid")
/* mcuxClKey_Handle_t key */ alicePubKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* 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,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("bobPrivKeyHandler has compatible type and cast was valid")
/* mcuxClKey_Handle_t key */ bobPrivKeyHandler,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* 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,
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()
/* 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,
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_CURVE25519_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_CURVE25519_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_CURVE25519_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:33
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 mcuxClRandom 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_Curve25519_PublicKey
Public key type pointer for ECC MontDH Curve25519.
Definition mcuxClEcc_KeyTypes.h:1023
static const mcuxClKey_Type_t mcuxClKey_Type_Ecc_MontDH_Curve25519_PrivateKey
Private key type pointer for ECC MontDH Curve25519.
Definition mcuxClEcc_KeyTypes.h:1014
#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:119
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:39
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:88
#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
#define MCUXCLECC_STATUS_OK
Operation was successful.
Definition mcuxClEcc_Constants.h:46
Structure for mcuxClSession Descriptor.
Definition mcuxClSession_Types.h:121