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

Example for the mcuxClEcc component EdDsa signature generation using the test vectors from "TEST SHA(abc)" from Section 7.1 of IRTF rfc 8032.

Example for the mcuxClEcc component EdDsa signature generation using the test vectors from "TEST SHA(abc)" from Section 7.1 of IRTF rfc 8032

/*--------------------------------------------------------------------------*/
/* 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 <mcuxClToolchain.h>
#include <mcuxClBuffer.h>
#include <mcuxClEcc.h>
#include <mcuxClKey.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.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_EDDSA_GENERATEKEYPAIR_ED25519_WACPU_SIZE, \
MCUXCLECC_EDDSA_GENERATESIGNATURE_ED25519_WACPU_SIZE)))
#define MAX_PKCWA_SIZE MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATEKEYPAIR_ED25519_WAPKC_SIZE, \
MCUXCLECC_EDDSA_GENERATESIGNATURE_ED25519_WAPKC_SIZE)
/* Private key input taken from "TEST SHA(abc)" from Section 7.1 of IRTF rfc 8032 */
static const ALIGNED uint8_t pPrivKeyInput[MCUXCLECC_EDDSA_ED25519_SIZE_PRIVATEKEY] =
{
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
};
/* Reference signature taken from "TEST SHA(abc)" from Section 7.1 of IRTF rfc 8032 */
static const ALIGNED uint8_t pRefSignature[MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE] =
{
0xDCu, 0x2Au, 0x44u, 0x59u, 0xE7u, 0x36u, 0x96u, 0x33u,
0xA5u, 0x2Bu, 0x1Bu, 0xF2u, 0x77u, 0x83u, 0x9Au, 0x00u,
0x20u, 0x10u, 0x09u, 0xA3u, 0xEFu, 0xBFu, 0x3Eu, 0xCBu,
0x69u, 0xBEu, 0xA2u, 0x18u, 0x6Cu, 0x26u, 0xB5u, 0x89u,
0x09u, 0x35u, 0x1Fu, 0xC9u, 0xACu, 0x90u, 0xB3u, 0xECu,
0xFDu, 0xFBu, 0xC7u, 0xC6u, 0x64u, 0x31u, 0xE0u, 0x30u,
0x3Du, 0xCAu, 0x17u, 0x9Cu, 0x13u, 0x8Au, 0xC1u, 0x7Au,
0xD9u, 0xBEu, 0xF1u, 0x17u, 0x73u, 0x31u, 0xA7u, 0x04u
};
/* Input message taken from "TEST SHA(abc)" from Section 7.1 of IRTF rfc 8032 */
static const ALIGNED uint8_t pMessage[] =
{
0xDDu, 0xAFu, 0x35u, 0xA1u, 0x93u, 0x61u, 0x7Au, 0xBAu,
0xCCu, 0x41u, 0x73u, 0x49u, 0xAEu, 0x20u, 0x41u, 0x31u,
0x12u, 0xE6u, 0xFAu, 0x4Eu, 0x89u, 0xA9u, 0x7Eu, 0xA2u,
0x0Au, 0x9Eu, 0xEEu, 0xE6u, 0x4Bu, 0x55u, 0xD3u, 0x9Au,
0x21u, 0x92u, 0x99u, 0x2Au, 0x27u, 0x4Fu, 0xC1u, 0xA8u,
0x36u, 0xBAu, 0x3Cu, 0x23u, 0xA3u, 0xFEu, 0xEBu, 0xBDu,
0x45u, 0x4Du, 0x44u, 0x23u, 0x64u, 0x3Cu, 0xE8u, 0x0Eu,
0x2Au, 0x9Au, 0xC9u, 0x4Fu, 0xA5u, 0x4Cu, 0xA4u, 0x9Fu
};
MCUXCLEXAMPLE_FUNCTION(mcuxClEcc_EdDSA_GenerateSignature_Ed25519_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 */
mcuxClSession_Descriptor_t session;
/* Allocate and initialize PKC workarea */
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 */
uint32_t privKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t privKey = (mcuxClKey_Handle_t) privKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(privkeyinit_result, privkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("The pointer privKey is of the right type (mcuxClKey_Handle_t)")
/* mcuxClKey_Handle_t key */ privKey,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* mcuxClKey_Type_t type */ mcuxClKey_Type_EdDSA_Ed25519_Priv,
/* uint8_t * pKeyData */ pPrivKeyData,
/* uint32_t keyDataLength */ sizeof(pPrivKeyData)));
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 */
uint32_t pubKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t pubKey = (mcuxClKey_Handle_t) pubKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(pubkeyinit_result, pubkeyinit_token, mcuxClKey_init(
/* mcuxClSession_Handle_t session */ &session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("The pointer pubKey is of the right type (mcuxClKey_Handle_t)")
/* mcuxClKey_Handle_t key */ pubKey,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
/* mcuxClKey_Type_t type */ mcuxClKey_Type_EdDSA_Ed25519_Pub,
/* uint8_t * pKeyData */ pPubKeyData,
/* uint32_t keyDataLength */ (uint32_t)sizeof(pPubKeyData)));
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,
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *mode */ (mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *) &privKeyInputDescriptor,
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* const uint8_t *pPrivKey */ pPrivKeyInput));
{
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,
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* const mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *mode */ (mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t *) &privKeyInputDescriptor,
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* mcuxClKey_Handle_t privKey */ privKey,
/* mcuxClKey_Handle_t pubKey */ pubKey));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Ed25519 signature generation */
/**************************************************************************/
/* Call mcuxClEcc_EdDSA_GenerateSignature to generate the signature. */
ALIGNED uint8_t signatureBuffer[MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE] = {0};
uint32_t signatureSize = 0u;
MCUXCLBUFFER_INIT_RO(buffIn, NULL, pMessage, sizeof(pMessage));
MCUXCLBUFFER_INIT(buffSignature, NULL, signatureBuffer, MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE);
/* mcuxClSession_Handle_t pSession */ &session,
/* mcuxClKey_Handle_t key */ privKey,
/* const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t *mode */ &mcuxClEcc_EdDsa_Ed25519ProtocolDescriptor,
/* 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;
}
/**************************************************************************/
/* Ed25519 signature verification */
/**************************************************************************/
MCUXCLBUFFER_INIT_RO(buffSignatureIn, NULL, signatureBuffer, MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE);
/* Call mcuxClEcc_EdDSA_VerifySignature to verify the signature. */
/* mcuxClSession_Handle_t pSession */ &session,
/* mcuxClKey_Handle_t key */ pubKey,
/* const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t * */ &mcuxClEcc_EdDsa_Ed25519ProtocolDescriptor,
/* mcuxCl_InputBuffer_t pIn */ buffIn,
/* uint32_t inSize */ sizeof(pMessage),
/* mcuxCl_InputBuffer_t pSignature */ buffSignatureIn,
/* uint32_t signatureSize */ signatureSize
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Compare the generated signature to the reference. */
if(!mcuxClCore_assertEqual(signatureBuffer, pRefSignature, MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE))
{
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:53
Top-level include file for the mcuxClBuffer component.
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.
#define MCUXCLBUFFER_INIT(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t).
Definition mcuxClBuffer.h:88
#define MCUXCLBUFFER_INIT_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:105
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_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:995
static const mcuxClKey_Type_t mcuxClKey_Type_EdDSA_Ed25519_Pub
Key type pointer for ECC EdDSA Ed25519 public keys.
Definition mcuxClEcc_KeyTypes.h:1009
struct mcuxClEcc_EdDSA_GenerateKeyPairDescriptor mcuxClEcc_EdDSA_GenerateKeyPairDescriptor_t
EdDSA GenerateKeyPair variant descriptor type.
Definition mcuxClEcc_Types.h:88
#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
#define MCUXCLECC_EDDSA_ED25519_SIZE_PRIVATEKEYDATA
Byte length of an Ed25519 private key handle data buffer.
Definition mcuxClEcc_Constants.h:499
#define MCUXCLECC_EDDSA_ED25519_SIZE_PRIVATEKEY
Byte length of an Ed25519 private key.
Definition mcuxClEcc_Constants.h:498
#define MCUXCLECC_EDDSA_ED25519_SIZE_PUBLICKEY
Byte length of an Ed25519 public key.
Definition mcuxClEcc_Constants.h:500
#define MCUXCLECC_EDDSA_ED25519_SIZE_SIGNATURE
Byte length of an Ed25519 signature.
Definition mcuxClEcc_Constants.h:501
const mcuxClEcc_EdDSA_SignatureProtocolDescriptor_t mcuxClEcc_EdDsa_Ed25519ProtocolDescriptor
Ed25519 signature protocol descriptor.
#define MCUXCLECC_EDDSA_GENERATEKEYPAIR_DESCRIPTOR_SIZE_IN_WORDS
EdDSA key pair generation descriptor size in words.
Definition mcuxClEcc_MemoryConsumption.h:230