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

Example for the mcuxClEcc component EdDsa related functions.

Example for the mcuxClEcc component EdDsa related functions

/*--------------------------------------------------------------------------*/
/* Copyright 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. */
/*--------------------------------------------------------------------------*/
/* Security Classification: Company Confidential */
/*--------------------------------------------------------------------------*/
#include <mcuxClToolchain.h>
#include <mcuxClBuffer.h>
#include <mcuxClEcc.h>
#include <mcuxClKey.h>
#include <mcuxClSession.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
#include <mcuxClExample_ELS_Helper.h>
#define MAX_CPUWA_SIZE MCUXCLCORE_MAX(MCUXCLRANDOMMODES_NCINIT_WACPU_SIZE, \
MCUXCLCORE_MAX(MCUXCLRANDOMMODES_INIT_WACPU_SIZE, \
MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATEKEYPAIR_ED25519_WACPU_SIZE, \
MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATESIGNATURE_ED25519_WACPU_SIZE, \
MCUXCLECC_EDDSA_VERIFYSIGNATURE_ED25519_WACPU_SIZE))))
#define MAX_PKCWA_SIZE MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATEKEYPAIR_ED25519_WAPKC_SIZE, \
MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATESIGNATURE_ED25519_WAPKC_SIZE, \
MCUXCLECC_EDDSA_VERIFYSIGNATURE_ED25519_WAPKC_SIZE))
/* Input taken from "TEST abc" from Section 7.3 of IRTF rfc 8032 */
static const ALIGNED uint8_t pMessage[] __attribute__((aligned(4))) =
{
0x61u, 0x62u, 0x63u
};
/* Signature taken from "TEST abc" from Section 7.3 of IRTF rfc 8032 */
static const ALIGNED uint8_t pRefSignature[MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE] __attribute__((aligned(4))) =
{
0x98u, 0xa7u, 0x02u, 0x22u, 0xf0u, 0xb8u, 0x12u, 0x1au,
0xa9u, 0xd3u, 0x0fu, 0x81u, 0x3du, 0x68u, 0x3fu, 0x80u,
0x9eu, 0x46u, 0x2bu, 0x46u, 0x9cu, 0x7fu, 0xf8u, 0x76u,
0x39u, 0x49u, 0x9bu, 0xb9u, 0x4eu, 0x6du, 0xaeu, 0x41u,
0x31u, 0xf8u, 0x50u, 0x42u, 0x46u, 0x3cu, 0x2au, 0x35u,
0x5au, 0x20u, 0x03u, 0xd0u, 0x62u, 0xadu, 0xf5u, 0xaau,
0xa1u, 0x0bu, 0x8cu, 0x61u, 0xe6u, 0x36u, 0x06u, 0x2au,
0xaau, 0xd1u, 0x1cu, 0x2au, 0x26u, 0x08u, 0x34u, 0x06u
};
/* Private key taken from "TEST abc" from Section 7.3 of IRTF rfc 8032 */
static const ALIGNED uint8_t pPrivateKey[MCUXCLECC_EDDSA_ED25519_SIZE_PRIVATEKEY] __attribute__((aligned(4))) =
{
0x83u, 0x3fu, 0xe6u, 0x24u, 0x09u, 0x23u, 0x7bu, 0x9du,
0x62u, 0xecu, 0x77u, 0x58u, 0x75u, 0x20u, 0x91u, 0x1eu,
0x9au, 0x75u, 0x9cu, 0xecu, 0x1du, 0x19u, 0x75u, 0x5bu,
0x7du, 0xa9u, 0x01u, 0xb9u, 0x6du, 0xcau, 0x3du, 0x42u
};
MCUXCLEXAMPLE_FUNCTION(mcuxClEcc_EdDSA_Ed25519ph_example)
{
/******************************************/
/* Set up the environment */
/******************************************/
/* Initialize ELS, Enable the ELS */
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 context and Initialize the PRNG */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_RNG(&session, 0u, mcuxClRandomModes_Mode_ELS_Drbg);
/******************************************/
/* Initialize the private and public keys */
/******************************************/
/* Allocate space for and initialize private key handle for an Ed25519 private key */
ALIGNED uint8_t privKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE];
mcuxClKey_Handle_t privKey = (mcuxClKey_Handle_t)&privKeyDesc;
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(privkeyinit_result, privkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
/* mcuxClKey_Handle_t key */ privKey,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_EdDSA_Ed25519_Priv,
/* uint8_t * pKeyData */ pPrivateKeyData,
/* uint32_t keyDataLength */ sizeof(pPrivateKeyData)));
if ((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != privkeyinit_token) || (MCUXCLKEY_STATUS_OK != privkeyinit_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Allocate space for and initialize public key handle for an Ed25519 public key */
ALIGNED uint8_t pubKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE];
mcuxClKey_Handle_t pubKey = (mcuxClKey_Handle_t)&pubKeyDesc;
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(pubkeyinit_result, pubkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
/* mcuxClKey_Handle_t key */ pubKey,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_EdDSA_Ed25519_Pub,
/* uint8_t * pKeyData */ pPublicKeyData,
/* uint32_t keyDataLength */ sizeof(pPublicKeyData)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != pubkeyinit_token) || (MCUXCLKEY_STATUS_OK != pubkeyinit_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Allocate space for and initialize EdDSA key pair generation descriptor for private key input */
/* mcuxClSession_Handle_t pSession */ &session,
/* mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *mode */ (mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *)&privKeyInputDescriptor,
/* const uint8_t *pPrivKey */ pPrivateKey));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Key pair generation for EdDSA on Ed25519 */
/**************************************************************************/
/* Call mcuxClEcc_EdDSA_GenerateKeyPair to derive the public key from the private one. */
/* mcuxClSession_Handle_t pSession */ &session,
/* const mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *mode */ (mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *) &privKeyInputDescriptor,
/* mcuxClKey_Handle_t privKey */ privKey,
/* mcuxClKey_Handle_t pubKey */ pubKey));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/******************************************/
/* Generate the protocol descriptor */
/******************************************/
/* Allocate space for the hash prefix and a protocol descriptor for Ed25519ph. */
/* Generate Ed25519ph protocol descriptor */
/* mcuxClSession_Handle_t pSession */ &session,
/* const mcuxClEcc_EdDSA_DomainParams_t *pDomainParams */ &mcuxClEcc_EdDSA_DomainParams_Ed25519,
/* mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t *pProtocolDescriptor */ pProtocolDesc,
/* uint32_t phflag */ MCUXCLECC_EDDSA_PHFLAG_ONE,
/* mcuxCl_InputBuffer_t pContext */ NULL,
/* uint32_t contextLen */ 0u));
|| (MCUXCLECC_STATUS_OK != genProtocolDescr_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Ed25519ph signature generation */
/**************************************************************************/
uint32_t signatureSize = 0u;
MCUXCLBUFFER_INIT_RO(buffIn, NULL, pMessage, sizeof(pMessage));
/* Call mcuxClEcc_EdDSA_GenerateSignature to generate the signature. */
/* mcuxClSession_Handle_t pSession */ &session,
/* mcuxClKey_Handle_t key */ privKey,
/* const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t *mode */ pProtocolDesc,
/* mcuxCl_InputBuffer_t pIn */ buffIn,
/* uint32_t inSize */ sizeof(pMessage),
/* mcuxCl_Buffer_t pSignature */ buffSignature,
/* uint32_t *const pSignatureSize */ &signatureSize));
|| (MCUXCLECC_STATUS_OK != sign_result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Ed25519ph signature verification */
/**************************************************************************/
/* Call mcuxClEcc_EdDSA_VerifySignature to verify the signature. */
/* mcuxClSession_Handle_t pSession */ &session,
/* mcuxClKey_Handle_t key */ pubKey,
/* const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t * */ pProtocolDesc,
/* mcuxCl_InputBuffer_t pIn */ buffIn,
/* uint32_t inSize */ sizeof(pMessage),
/* mcuxCl_InputBuffer_t pSignature */ buffSignature,
/* uint32_t signatureSize */ signatureSize
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Verify the signature with the reference signature. */
if(!mcuxClCore_assertEqual((const uint8_t*)signature, pRefSignature, sizeof(pRefSignature)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/******************************************/
/* Clean up */
/******************************************/
/* Destroy Session and cleanup Session */
if(!mcuxClExample_Session_Clean(&session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Disable the ELS */
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
static const ALIGNED uint8_t signature[RSA_KEY_BYTE_LENGTH]
Example value for RSA signature s.
Definition mcuxClRsa_verify_pssverify_sha2_256_example.c:87
Provides the API for the CL buffer types.
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.
Top-level include file for the mcuxClSession component.
#define MCUXCLBUFFER_INIT(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t).
Definition mcuxClBuffer.h:71
#define MCUXCLBUFFER_INIT_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:88
mcuxClEcc_Status_t mcuxClEcc_EdDSA_GenerateKeyPair(mcuxClSession_Handle_t pSession, const mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *mode, mcuxClKey_Handle_t privKey, mcuxClKey_Handle_t pubKey)
This function implements the EdDSA key pair generation for Ed25519 and Ed448 as specified in rfc8032 ...
mcuxClEcc_Status_t mcuxClEcc_EdDSA_GenerateProtocolDescriptor(mcuxClSession_Handle_t pSession, const mcuxClEcc_EdDSA_DomainParams_t *pDomainParams, mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t *pProtocolDescriptor, uint32_t phflag, mcuxCl_InputBuffer_t pContext, uint32_t contextLen)
This function implements the protocol descriptor generation for Ed25519ctx, Ed25519ph,...
mcuxClEcc_Status_t mcuxClEcc_EdDSA_InitPrivKeyInputMode(mcuxClSession_Handle_t pSession, mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *mode, const uint8_t *pPrivKey)
This function initializes an EdDSA mode descriptor for EdDSA key pair generation with private key inp...
mcuxClEcc_Status_t mcuxClEcc_EdDSA_VerifySignature(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t *mode, mcuxCl_InputBuffer_t pIn, uint32_t inSize, mcuxCl_InputBuffer_t pSignature, uint32_t signatureSize)
This function implements the EdDSA signature verification for Ed25519 and Ed448 as specified in rfc80...
mcuxClEcc_Status_t mcuxClEcc_EdDSA_GenerateSignature(mcuxClSession_Handle_t pSession, mcuxClKey_Handle_t key, const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t *mode, mcuxCl_InputBuffer_t pIn, uint32_t inSize, mcuxCl_Buffer_t pSignature, uint32_t *const pSignatureSize)
This function implements the EdDSA signature generation for Ed25519 and Ed448 as specified in rfc8032...
static const mcuxClKey_Type_t mcuxClKey_Type_EdDSA_Ed25519_Priv
Key type pointer for ECC EdDSA Ed25519 private keys.
Definition mcuxClEcc_KeyTypes.h:975
static const mcuxClKey_Type_t mcuxClKey_Type_EdDSA_Ed25519_Pub
Key type pointer for ECC EdDSA Ed25519 public keys.
Definition mcuxClEcc_KeyTypes.h:989
struct mcuxClEcc_EdDSA_GenerateKeyPairDescriptor mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t
EdDSA GenerateKeyPair variant descriptor type.
Definition mcuxClEcc_Types.h:68
struct mcuxClEcc_EdDSA_SignatureProtocolDescriptor mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t
EdDSA SignatureProtocol variant descriptor type.
Definition mcuxClEcc_Types.h:78
#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:47
#define MCUXCLECC_EDDSA_ED25519_SIZE_PRIVATEKEYDATA
Byte length of an Ed25519 private key handle data buffer.
Definition mcuxClEcc_Constants.h:480
#define MCUXCLECC_EDDSA_ED25519_SIZE_PRIVATEKEY
Byte length of an Ed25519 private key.
Definition mcuxClEcc_Constants.h:479
#define MCUXCLECC_EDDSA_ED25519_SIZE_PUBLICKEY
Byte length of an Ed25519 public key.
Definition mcuxClEcc_Constants.h:481
#define MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE
Byte length of an Ed25519 signature.
Definition mcuxClEcc_Constants.h:482
#define MCUXCLECC_EDDSA_GENERATEKEYPAIR_DESCRIPTOR_SIZE
EdDSA key pair generation descriptor size.
Definition mcuxClEcc_MemoryConsumption.h:208
#define MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE_PROTOCOL_DESCRIPTOR(contextLen)
Byte length of an Ed25519 signature protocol descriptor.
Definition mcuxClEcc_MemoryConsumption.h:220
Structure for mcuxClSession Descriptor.
Definition mcuxClSession_Types.h:121