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

Example for the mcuxClKey component for RFC3394 key wrap and unwrap into an SGI key slot.

Example for the mcuxClKey component for RFC3394 key wrap and unwrap into an SGI key slot.

/*--------------------------------------------------------------------------*/
/* Copyright 2024-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 <mcuxClSession.h>
#include <mcuxClKey.h>
#include <mcuxClAes.h> // Interface to AES-related definitions and types
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClBuffer.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
// Test vectors are taken from https://datatracker.ietf.org/doc/html/rfc3394
static const uint32_t keyData[MCUXCLAES_AES128_KEY_SIZE / 4U] = {
0x33221100U, 0x77665544U, 0xBBAA9988U, 0xFFEEDDCCU
};
static const uint32_t kwk256Data[MCUXCLAES_AES256_KEY_SIZE / 4U] = {
0x03020100U, 0x07060504U, 0x0B0A0908U, 0x0F0E0D0CU,
0x13121110U, 0x17161514U, 0x1B1A1918U, 0x1F1E1D1CU
};
static const uint32_t expectedwrappedKeyData[MCUXCLAES_ENCODING_RFC3394_AES128_KEY_SIZE / 4U] = {
0xF9C3E864U, 0xA25B0FCEU, 0x7977E963U, 0x2A8A8105U,
0x1E19C893U, 0xE78A6E7DU
};
MCUXCLEXAMPLE_FUNCTION(mcuxClKey_WrapAndLoad_Rfc3394_Sgi_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
/* Allocate and initialize session */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session,
MCUXCLEXAMPLE_MAX_WA(MCUXCLEXAMPLE_MAX_WA(MCUXCLKEY_ENCODE_CPU_WA_SIZE, MCUXCLKEY_LOADCOPRO_CPU_WA_SIZE), MCUXCLRANDOM_NCINIT_WACPU_SIZE), 0U);
/* Initialize the PRNG */
MCUXCLEXAMPLE_INITIALIZE_PRNG(session);
/**************************************************************************/
/* Key Init and load the key-wrapping key */
/**************************************************************************/
uint32_t keyWrappingKeyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClKey_Handle_t keyWrappingKey = (mcuxClKey_Handle_t) &keyWrappingKeyDesc;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ keyWrappingKey,
/* mcuxClKey_Type_t type: */ mcuxClKey_Type_Aes256,
/* uint8_t * pKeyData: */ (const uint8_t*)kwk256Data,
/* uint32_t keyDataLength: */ sizeof(kwk256Data))
);
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_init) != kiKwk_token) || (MCUXCLKEY_STATUS_OK != kiKwk_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ keyWrappingKey,
/* uint32_t loadOptions: */ MCUXCLKEY_LOADOPTION_SLOT_SGI_KEY_6)
);
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_loadCopro) != klKwk_token) || (MCUXCLKEY_STATUS_OK != klKwk_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Key Init+Wrap using the Key_encode API */
/**************************************************************************/
uint32_t keyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
uint32_t wrappedKeyLen = 0u;
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Encoding_t encoding: */ mcuxClAes_Encoding_Rfc3394,
/* mcuxClKey_Handle_t encodedKey: */ key,
/* mcuxClKey_Type_t type: */ mcuxClKey_Type_Aes128,
/* const uint8_t * pPlainKeyData: */ (const uint8_t *)keyData,
/* uint32_t plainKeyDataLength: */ sizeof(keyData),
/* const uint8_t * pAuxData: */ (uint8_t*) keyWrappingKeyDesc,
/* uint32_t auxDataLength: */ sizeof(keyWrappingKeyDesc),
/* uint8_t * pEncodedKeyData: */ wrappedKeyData,
/* uint32_t* const pEncodedKeyDataLength:*/ &wrappedKeyLen)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClKey_encode")
if(!mcuxClCore_assertEqual(wrappedKeyData, (const uint8_t *)expectedwrappedKeyData, sizeof(expectedwrappedKeyData)))
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Crypto Operation */
/**************************************************************************/
/**************************************************************************/
/* Key Load/Unwrap using the mcuxClKey_loadCopro API */
/**************************************************************************/
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ key,
/* uint32_t loadOptions: */ MCUXCLKEY_LOADOPTION_SLOT_SGI_KEY_UNWRAP)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Crypto Operation */
/**************************************************************************/
/**************************************************************************/
/* Flush the loaded keys */
/**************************************************************************/
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ key)
);
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_flush) != kf1_token) || (MCUXCLKEY_STATUS_OK != kf1_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ keyWrappingKey)
);
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClKey_flush) != kf2_token) || (MCUXCLKEY_STATUS_OK != kf2_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Destroy the current session */
/**************************************************************************/
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
Top-level include file for the mcuxClAes component.
Top-level include file for the mcuxClBuffer component.
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the mcuxClKey component.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLAES_ENCODING_RFC3394_AES128_KEY_SIZE
RFC3394 encoding of AES-128 key material, encoded key size in bytes.
Definition mcuxClAes_Constants.h:70
#define MCUXCLAES_AES128_KEY_SIZE
AES-128 key size in bytes.
Definition mcuxClAes_Constants.h:60
#define MCUXCLAES_AES256_KEY_SIZE
AES-256 key size in bytes.
Definition mcuxClAes_Constants.h:64
static const mcuxClKey_Encoding_t mcuxClAes_Encoding_Rfc3394
Key encoding for RFC3394 key wrap/unwrap.
Definition mcuxClAes_KeyEncodingMechanisms.h:67
static const mcuxClKey_Type_t mcuxClKey_Type_Aes256
Key type pointer for AES-256 based keys.
Definition mcuxClAes_KeyTypes.h:97
static const mcuxClKey_Type_t mcuxClKey_Type_Aes128
Key type pointer for AES-128 based keys.
Definition mcuxClAes_KeyTypes.h:71
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:60
#define MCUXCLKEY_LOADOPTION_SLOT_SGI_KEY_UNWRAP
Sgi key slot containing an RFC3394 unwrapped key.
Definition mcuxClKey_Constants.h:201
#define MCUXCLKEY_LOADOPTION_SLOT_SGI_KEY_6
SGI key slot 6.
Definition mcuxClKey_Constants.h:198
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_flush(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key)
Flush key from destination which can be a key slot of coprocessor or memory buffer.
mcuxClKey_Status_t mcuxClKey_loadCopro(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, uint32_t loadOptions)
Load key into destination key slot of a coprocessor.
mcuxClKey_Status_t mcuxClKey_encode(mcuxClSession_Handle_t session, mcuxClKey_Encoding_t encoding, mcuxClKey_Handle_t encodedKey, mcuxClKey_Type_t type, const uint8_t *pPlainKeyData, uint32_t plainKeyDataLength, const uint8_t *pAuxData, uint32_t auxDataLength, uint8_t *pEncodedKeyData, uint32_t *const pEncodedKeyDataLength)
Key descriptor initialization function including applying a encoding mechanism.
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