MCUXpresso SDK Documentation

middleware/wireless/framework/services/SecLib_RNG/SecLib_psa.c

middleware/wireless/framework/services/SecLib_RNG/SecLib_psa.c#

   1/*
   2 * Copyright 2025-2026 NXP
   3 * SPDX-License-Identifier: BSD-3-Clause
   4 */
   5/*! *********************************************************************************
   6 * \file
   7 *
   8 *
   9 * This is the source file for the security module used by the connectivity stacks. The Security
  10 *    Module SecLib provides an abstraction from the Hardware to the upper layer.
  11 *    In this file, a wrapper to PSA API component is implemented.
  12 ***********************************************************************************/
  13
  14/*! *********************************************************************************
  15*************************************************************************************
  16* Include
  17*************************************************************************************
  18********************************************************************************** */
  19
  20#include <stdint.h>
  21#include "EmbeddedTypes.h"
  22#include "fwk_config.h"
  23#include "SecLib.h"
  24#include "psa/crypto.h"
  25#include "psa/crypto_types.h"
  26#include "psa/crypto_values.h"
  27#include "psa/crypto_extra.h"
  28#include "fsl_component_mem_manager.h"
  29#include "FunctionLib.h"
  30#include "fwk_platform_crypto.h"
  31#include "SecLib_ecp256.h"
  32#include "CryptoLibSW.h"
  33
  34#if defined(PSA_CRYPTO_DRIVER_ELE_S2XX)
  35#include "ele_s2xx.h"
  36#endif /* PSA_CRYPTO_DRIVER_ELE_S2XX */
  37
  38/*! *********************************************************************************
  39*************************************************************************************
  40* Private macros
  41*************************************************************************************
  42********************************************************************************** */
  43#define KEY_ID_BLE0 0x426c6530
  44
  45#define RAISE_ERROR(st, expected)                                \
  46    if ((st) != (expected))                                      \
  47    {                                                            \
  48        if (PRINTF("\tassertion failed at %s:%d - "              \
  49                   "actual:-%d expected:-%d\r\n",                \
  50                   __FILE__, __LINE__, -(st), -(expected)) > -1) \
  51        {                                                        \
  52            break;                                               \
  53        }                                                        \
  54    }
  55
  56/*! *********************************************************************************
  57*************************************************************************************
  58* Private type definitions
  59*************************************************************************************
  60********************************************************************************** */
  61#define ECP256_COORDINATE_BITLEN 256u
  62#define ECP256_COORDINATE_LEN    (ECP256_COORDINATE_BITLEN >> 3)
  63#define ECP256_COORDINATE_WLEN   ((ECP256_COORDINATE_LEN) / 4U)
  64
  65/************************************************************************************
  66*************************************************************************************
  67* Private memory declarations
  68*************************************************************************************
  69************************************************************************************/
  70typedef struct psa_ecp256_context_t
  71{
  72    big_int256_t PrivateKey[ECP256_COORDINATE_WLEN];        /*!< The private key : RNG output */
  73    big_int256_t OwnPublicKey[2U * ECP256_COORDINATE_WLEN]; /*! Own Public computed from PrivateKey */
  74    uint32_t     keyId;
  75    psa_key_id_t OwnKey;                                    /*! Own Key object reference */
  76} psa_ecp256_context_t;
  77
  78static psa_ecp256_context_t  psa_g_ECP_KeyPair;
  79static psa_ecp256_context_t *psa_pECPKeyPair = ((void *)0);
  80
  81/*! *********************************************************************************
  82*************************************************************************************
  83* Private functions
  84*************************************************************************************
  85********************************************************************************** */
  86
  87static bool ECP256_LePointValid(const ecp256Point_t *P)
  88{
  89#if defined gSecLibUseDspExtension_d && (gSecLibUseDspExtension_d != 0)
  90    ecp256Point_t tmp;
  91    ECP256_PointCopy_and_change_endianness(tmp.raw, P->raw);
  92    return ECP256_PointValid(&tmp);
  93#else
  94    extern bool_t EcP256_IsPointOnCurve(const uint32_t *X, const uint32_t *Y);
  95    return EcP256_IsPointOnCurve((const uint32_t *)&P->components_32bit.x[0],
  96                                 (const uint32_t *)&P->components_32bit.y[0]);
  97#endif
  98}
  99
 100psa_key_location_t get_most_secure_key_location()
 101{
 102    /* default set to transparent location */
 103    psa_key_location_t loc = PSA_KEY_LOCATION_LOCAL_STORAGE;
 104
 105#if defined(PSA_CRYPTO_DRIVER_ELE_S2XX)
 106    loc = PSA_KEY_LOCATION_S200_KEY_STORAGE_NON_EL2GO;
 107#endif /* PSA_CRYPTO_DRIVER_ELE_S2XX */
 108
 109    return loc;
 110}
 111
 112/*! *********************************************************************************
 113*************************************************************************************
 114* Public functions
 115*************************************************************************************
 116********************************************************************************** */
 117
 118/*! *********************************************************************************
 119 * \brief  This function performs initialization of the cryptographic HW acceleration.
 120 *
 121 ********************************************************************************** */
 122void SecLib_Init(void)
 123{
 124    psa_status_t status;
 125    do
 126    {
 127        /* initialize psa crypto*/
 128        status = psa_crypto_init();
 129        RAISE_ERROR(status, PSA_SUCCESS);
 130    } while (false);
 131}
 132
 133void SecLib_ReInit(void)
 134{
 135    /* Initialize cryptographic hardware.*/
 136    (void)PLATFORM_ReinitCrypto();
 137}
 138
 139/*! *********************************************************************************
 140 * \brief  This function will allow reinitizialize the cryptographic HW acceleration
 141 * next time we need it, typically after lowpower mode.
 142 *
 143 ********************************************************************************** */
 144void SecLib_DeInit(void)
 145{
 146    /* Deinitialize cryptographic hardware.*/
 147    (void)PLATFORM_TerminateCrypto();
 148}
 149
 150/*! *********************************************************************************
 151 * \brief  This function performs all SHA256 steps on multiple bytes: initialize,
 152 *         update, finish, and update context data.
 153 *         The final hash value is stored at the provided output location.
 154 *
 155 * \param [in]       pData       Pointer to the input data
 156 * \param [in]       numBytes    Number of bytes to hash
 157 * \param [in,out]   pOutput     Pointer to the output location
 158 *
 159 ********************************************************************************** */
 160secResultType_t SecLib_SHA256_Hash(const uint8_t *pData, uint32_t numBytes, uint8_t *pOutput)
 161{
 162    secResultType_t       res        = gSecError_c;
 163    const psa_algorithm_t alg        = PSA_ALG_SHA_256;
 164    size_t                hashLength = 0U; /* Initialize hash length to 0 */
 165    psa_status_t          status;
 166
 167    do
 168    {
 169        if ((pOutput == NULL) || (pData == NULL))
 170        {
 171            res = gSecBadArgument_c;
 172            break;
 173        }
 174        /* SHA 256 computation */
 175        status = psa_hash_compute(alg, pData, numBytes, pOutput, SHA256_HASH_SIZE, &hashLength);
 176        RAISE_ERROR(status, PSA_SUCCESS);
 177        res = gSecSuccess_c;
 178    } while (false);
 179    return res;
 180}
 181
 182/*! *********************************************************************************
 183 * \brief  This function performs all HMAC SHA256 steps on multiple bytes: initialize,
 184 *         update, finish, and update context data.
 185 *         The final HMAC value is stored at the provided output location.
 186 *
 187 * \param [in]       pKey        Pointer to the HMAC key
 188 * \param [in]       keyLen      Length of the HMAC key in bytes
 189 * \param [in]       pData       Pointer to the input data
 190 * \param [in]       numBytes    Number of bytes to perform HMAC on
 191 * \param [out]   pOutput     Pointer to the output location
 192 *
 193 ********************************************************************************** */
 194secResultType_t SecLib_HMAC_SHA256(
 195    const uint8_t *pKey, uint32_t keyLen, const uint8_t *pData, uint32_t numBytes, uint8_t *pOutput)
 196{
 197    secResultType_t       res        = gSecError_c;
 198    const psa_algorithm_t alg        = PSA_ALG_HMAC(PSA_ALG_SHA_256);
 199    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 200    psa_key_id_t          key        = 0U;
 201    size_t                output_len = 0U;
 202    psa_status_t          status;
 203
 204    do
 205    {
 206        if ((pKey == NULL) || (pData == NULL) || (pOutput == NULL) || (keyLen == 0U))
 207        {
 208            res = gSecBadArgument_c;
 209            break;
 210        }
 211
 212        /* key initialisation before import */
 213        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
 214        psa_set_key_algorithm(&attributes, alg);
 215        psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
 216        psa_set_key_bits(&attributes, keyLen * 8U);
 217        psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
 218                                              PSA_KEY_LIFETIME_VOLATILE, get_most_secure_key_location()));
 219
 220        status = psa_import_key(&attributes, pKey, keyLen, &key);
 221        RAISE_ERROR(status, PSA_SUCCESS);
 222
 223        /* compute HMAC-SHA256 over pData */
 224        status = psa_mac_compute(key, alg, pData, numBytes, pOutput, SHA256_HASH_SIZE, &output_len);
 225        (void)psa_destroy_key(key); /* destroy key after use */
 226        RAISE_ERROR(status, PSA_SUCCESS);
 227
 228        res = gSecSuccess_c;
 229    } while (false);
 230    return res;
 231}
 232
 233/*! *********************************************************************************
 234 * \brief  This function calculates XOR of individual byte pairs in two uint8_t arrays.
 235 *         pDst[i] := pDst[i] ^ pSrc[i] for i=0 to n-1
 236 *
 237 * \param[in]  pDst First byte array operand for XOR and destination byte array
 238 *
 239 * \param[in]  pSrc Second byte array operand for XOR
 240 *
 241 * \param[in]  n  Length of the byte array which will be XORed
 242 *
 243 ********************************************************************************** */
 244void SecLib_XorN(uint8_t *pDst, const uint8_t *pSrc, uint8_t n)
 245{
 246    while (n != 0U)
 247    {
 248        *pDst = *pDst ^ *pSrc;
 249        pDst  = pDst + 1;
 250        pSrc  = pSrc + 1;
 251        n--;
 252    }
 253}
 254
 255/*! *********************************************************************************
 256 * \brief  This function performs AES-128 encryption on a 16-byte block.
 257 *
 258 * \param[in]  pInput Pointer to the location of the 16-byte plain text block.
 259 *
 260 * \param[in]  pKey Pointer to the location of the 128-bit key.
 261 *
 262 * \param[out]  pOutput Pointer to the location to store the 16-byte ciphered output.
 263 *
 264 * \pre All Input/Output pointers must refer to a memory address aligned to 4 bytes!
 265 *
 266 ********************************************************************************** */
 267secResultType_t SecLib_AES_128_Encrypt(const uint8_t *pInput, const uint8_t *pKey, uint8_t *pOutput)
 268{
 269    secResultType_t       res      = gSecError_c;
 270    size_t                key_bits = AES_128_KEY_BYTE_LEN;
 271    const psa_algorithm_t alg      = PSA_ALG_ECB_NO_PADDING;
 272    psa_status_t          status;
 273    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 274    psa_key_id_t          key        = 0U;
 275    size_t                output_len = 0U;
 276
 277    /* key initialisation before import */
 278    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 279    psa_set_key_algorithm(&attributes, alg);
 280    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
 281    psa_set_key_bits(&attributes, AES_128_KEY_BITS);
 282    psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_LIFETIME_VOLATILE,
 283                                                                                     get_most_secure_key_location()));
 284
 285    do
 286    {
 287        if ((pInput == NULL) || (pKey == NULL) || (pOutput == NULL))
 288        {
 289            res = gSecBadArgument_c;
 290            break;
 291        }
 292        status = psa_import_key(&attributes, pKey, key_bits, &key); /* import the key in psa and get its id */
 293        RAISE_ERROR(status, PSA_SUCCESS);
 294
 295        /* execute encryption with ECB NO PADDING */
 296        status = psa_cipher_encrypt(key, alg, pInput, AES_BLOCK_SIZE, pOutput, AES_BLOCK_SIZE, &output_len);
 297        RAISE_ERROR(status, PSA_SUCCESS);
 298
 299        status = psa_destroy_key(key);
 300        RAISE_ERROR(status, PSA_SUCCESS);
 301        res = gSecSuccess_c;
 302    } while (false);
 303    return res;
 304}
 305
 306/*! *********************************************************************************
 307 * \brief  This function performs AES-128-ECB encryption on a message block.
 308 *         This function only accepts input lengths which are multiple
 309 *         of 16 bytes (AES 128 block size).
 310 *
 311 * \param[in]  pInput Pointer to the location of the input message.
 312 *
 313 * \param[in]  inputLen Input message length in bytes.
 314 *
 315 * \param[in]  pKey Pointer to the location of the 128-bit key.
 316 *
 317 * \param[out]  pOutput Pointer to the location to store the ciphered output.
 318 *
 319 * \pre All Input/Output pointers must refer to a memory address aligned to 4 bytes!
 320 *
 321 ********************************************************************************** */
 322secResultType_t SecLib_AES_128_ECB_Encrypt(const uint8_t *pInput,
 323                                           uint32_t       inputLen,
 324                                           const uint8_t *pKey,
 325                                           uint8_t       *pOutput)
 326{
 327    secResultType_t       res      = gSecError_c;
 328    size_t                key_bits = AES_128_KEY_BYTE_LEN;
 329    const psa_algorithm_t alg      = PSA_ALG_ECB_NO_PADDING;
 330    psa_status_t          status;
 331    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 332    psa_key_id_t          key        = 0U;
 333    size_t                output_len = 0U;
 334
 335    do
 336    {
 337        if ((pInput == NULL) || (pKey == NULL) || (pOutput == NULL) || (inputLen == 0) ||
 338            ((inputLen % AES_BLOCK_SIZE) != 0U))
 339        {
 340            res = gSecBadArgument_c;
 341            break;
 342        }
 343        /* key initialisation before import */
 344        psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 345        psa_set_key_algorithm(&attributes, alg);
 346        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
 347        psa_set_key_bits(&attributes, AES_128_KEY_BITS);
 348        psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
 349                                              PSA_KEY_LIFETIME_VOLATILE, get_most_secure_key_location()));
 350
 351        /* import pKey and get the address of the imported key */
 352        status = psa_import_key(&attributes, pKey, key_bits, &key);
 353        RAISE_ERROR(status, PSA_SUCCESS);
 354
 355        /* execute the encryption with our key*/
 356        status = psa_cipher_encrypt(key, alg, pInput, inputLen, pOutput, inputLen, &output_len);
 357        RAISE_ERROR(status, PSA_SUCCESS);
 358
 359        status = psa_destroy_key(key); /* destroy key after use */
 360        RAISE_ERROR(status, PSA_SUCCESS);
 361        res = gSecSuccess_c;
 362    } while (false);
 363    return res;
 364}
 365
 366/*! *********************************************************************************
 367 * \brief  This function performs AES-128 decryption on a 16-byte block.
 368 *
 369 * \param[in]  pInput Pointer to the location of the 16-byte plain text block.
 370 *
 371 * \param[in]  pKey Pointer to the location of the 128-bit key.
 372 *
 373 * \param[out]  pOutput Pointer to the location to store the 16-byte ciphered output.
 374 *
 375 * \pre All Input/Output pointers must refer to a memory address aligned to 4 bytes!
 376 *
 377 ********************************************************************************** */
 378secResultType_t SecLib_AES_128_Decrypt(const uint8_t *pInput, const uint8_t *pKey, uint8_t *pOutput)
 379{
 380    secResultType_t       res      = gSecError_c;
 381    size_t                key_bits = AES_128_KEY_BYTE_LEN;
 382    const psa_algorithm_t alg      = PSA_ALG_ECB_NO_PADDING;
 383    psa_status_t          status;
 384    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 385    psa_key_id_t          key        = 0U;
 386    size_t                output_len = 0U;
 387
 388    /* key initialisation before import */
 389    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 390    psa_set_key_algorithm(&attributes, alg);
 391    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
 392    psa_set_key_bits(&attributes, AES_128_KEY_BITS);
 393    psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_LIFETIME_VOLATILE,
 394                                                                                     get_most_secure_key_location()));
 395
 396    do
 397    {
 398        if ((pInput == NULL) || (pKey == NULL) || (pOutput == NULL))
 399        {
 400            res = gSecBadArgument_c;
 401            break;
 402        }
 403        status = psa_import_key(&attributes, pKey, key_bits, &key);
 404        RAISE_ERROR(status, PSA_SUCCESS);
 405
 406        /* decrypt using ECB NO PADDING */
 407        status = psa_cipher_decrypt(key, alg, pInput, AES_BLOCK_SIZE, pOutput, AES_128_BLOCK_SIZE, &output_len);
 408        RAISE_ERROR(status, PSA_SUCCESS);
 409
 410        /* destroy key after use */
 411        status = psa_destroy_key(key);
 412        RAISE_ERROR(status, PSA_SUCCESS);
 413        res = gSecSuccess_c;
 414    } while (false);
 415    return res;
 416}
 417
 418/*! *********************************************************************************
 419 * \brief  This function performs AES-128 decryption on a 16-byte block.
 420 *
 421 * \param[in]  pInput Pointer to the location of the 16-byte plain text block.
 422 *
 423 * \param[in]  inputLen Input message length in bytes.
 424 *
 425 * \param[in]  pKey Pointer to the location of the 128-bit key.
 426 *
 427 * \param[out]  pOutput Pointer to the location to store the 16-byte ciphered output.
 428 *
 429 * \pre All Input/Output pointers must refer to a memory address aligned to 4 bytes!
 430 *
 431 ********************************************************************************** */
 432secResultType_t SecLib_AES_128_ECB_Decrypt(const uint8_t *pInput,
 433                                           uint32_t       inputLen,
 434                                           const uint8_t *pKey,
 435                                           uint8_t       *pOutput)
 436{
 437    secResultType_t res = gSecError_c;
 438
 439    size_t                key_bits = AES_128_KEY_BYTE_LEN;
 440    const psa_algorithm_t alg      = PSA_ALG_ECB_NO_PADDING;
 441    psa_status_t          status;
 442    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 443    psa_key_id_t          key        = 0U;
 444    size_t                output_len = 0U;
 445
 446    do
 447    {
 448        if ((pInput == NULL) || (pKey == NULL) || (pOutput == NULL) || (inputLen == 0) ||
 449            ((inputLen % AES_BLOCK_SIZE) != 0U))
 450        {
 451            res = gSecBadArgument_c;
 452            break;
 453        }
 454        psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 455        psa_set_key_algorithm(&attributes, alg);
 456        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
 457        psa_set_key_bits(&attributes, AES_128_KEY_BITS);
 458        psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
 459                                              PSA_KEY_LIFETIME_VOLATILE, get_most_secure_key_location()));
 460
 461        status = psa_import_key(&attributes, pKey, key_bits, &key);
 462        RAISE_ERROR(status, PSA_SUCCESS);
 463
 464        status = psa_cipher_decrypt(key, alg, pInput, inputLen, pOutput, inputLen, &output_len);
 465        RAISE_ERROR(status, PSA_SUCCESS);
 466
 467        status = psa_destroy_key(key);
 468        RAISE_ERROR(status, PSA_SUCCESS);
 469        res = gSecSuccess_c;
 470    } while (false);
 471
 472    return res;
 473}
 474
 475/*! *********************************************************************************
 476 * \brief  This function performs AES-128-CMAC on a message block accepting input data
 477 *         which is in LSB first format and computing the authentication code
 478 *         starting from the end of the data.
 479 *
 480 * \param[in]  pInput Pointer to the location of the input message.
 481 *
 482 * \param[in]  inputLen Length of the input message in bytes.
 483 *             The input data must be provided LSB first.
 484 *
 485 * \param[in]  pKey Pointer to the location of the 128-bit key.
 486 *              The key must be provided MSB first.
 487 *
 488 * \param[out]  pOutput Pointer to the location to store the 16-byte authentication code.
 489 *              The code will be generated MSB first.
 490 *
 491 ********************************************************************************** */
 492secResultType_t SecLib_AES_128_CMAC_LsbFirstInput(const uint8_t *pInput,
 493                                                  uint32_t       inputLen,
 494                                                  const uint8_t *pKey,
 495                                                  uint8_t       *pOutput)
 496{
 497    secResultType_t       res      = gSecError_c;
 498    size_t                key_bits = AES_128_KEY_BYTE_LEN;
 499    const psa_algorithm_t alg      = PSA_ALG_CMAC; /* Set algorithm to cmac */
 500    psa_status_t          status;
 501    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 502    psa_key_id_t          key        = 0U;
 503    size_t                output_len = 0U;
 504
 505    do
 506    {
 507        if ((pInput == NULL) || (pKey == NULL) || (pOutput == NULL))
 508        {
 509            res = gSecBadArgument_c;
 510            break;
 511        }
 512
 513        /* key initialisation before import */
 514        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
 515        psa_set_key_algorithm(&attributes, alg);
 516        psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 517
 518        status = psa_import_key(&attributes, pKey, key_bits, &key);
 519        RAISE_ERROR(status, PSA_SUCCESS);
 520
 521        psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; /* init cmac operation */
 522
 523        status = psa_mac_sign_setup(&operation, key, alg);      /* setup function */
 524        RAISE_ERROR(status, PSA_SUCCESS);
 525
 526        /* Walk the input buffer from the end to the start and reverse the blocks
 527         * before calling the CMAC update function. */
 528        uint8_t reversedBlock[AES_128_BLOCK_SIZE] = {0U};
 529        pInput += inputLen;
 530        do
 531        {
 532            uint32_t currentCmacInputBlkLen = 0U;
 533            if (inputLen < AES_128_BLOCK_SIZE)
 534            {
 535                /* If this is the first and single block it is legal for it to have an input length of 0
 536                 * in which case nothing will be copied in the reversed CMAC input buffer. */
 537                currentCmacInputBlkLen = inputLen;
 538            }
 539            else
 540            {
 541                currentCmacInputBlkLen = AES_128_BLOCK_SIZE;
 542            }
 543            pInput -= currentCmacInputBlkLen;
 544            inputLen -= currentCmacInputBlkLen;
 545            /* Copy the input block to the reversed CMAC input buffer */
 546            FLib_MemCpyReverseOrder(reversedBlock, pInput, currentCmacInputBlkLen);
 547
 548            /* compute mac operation on a block */
 549            status = psa_mac_update(&operation, reversedBlock, currentCmacInputBlkLen);
 550            RAISE_ERROR(status, PSA_SUCCESS);
 551
 552        } while (inputLen != 0U);
 553
 554        size_t macLen =
 555            16U; /* value of PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, key_bits, alg) but macro has multiple misras; */
 556        status = psa_mac_sign_finish(&operation, pOutput, macLen, &output_len);
 557        RAISE_ERROR(status, PSA_SUCCESS);
 558
 559        status = psa_destroy_key(key); /* destroy key after use */
 560        RAISE_ERROR(status, PSA_SUCCESS);
 561        res = gSecSuccess_c;
 562    } while (false);
 563    return res;
 564}
 565
 566/*! *********************************************************************************
 567 * \brief  This function performs AES-128-CMAC on a message block.
 568 *
 569 * \param[in]  pInput Pointer to the location of the input message.
 570 *
 571 * \param[in]  inputLen Length of the input message in bytes. The input data must be provided MSB first.
 572 *
 573 * \param[in]  pKey Pointer to the location of the 128-bit key. The key must be provided MSB first.
 574 *
 575 * \param[out]  pOutput Pointer to the location to store the 16-byte authentication code. The code will be generated MSB
 576 *first.
 577 *
 578 * \remarks This is public open source code! Terms of use must be checked before use!
 579 *
 580 ********************************************************************************** */
 581secResultType_t SecLib_AES_128_CMAC(const uint8_t *pInput,
 582                                    const uint32_t inputLen,
 583                                    const uint8_t *pKey,
 584                                    uint8_t       *pOutput)
 585{
 586    secResultType_t res = gSecError_c;
 587    ;
 588    size_t                key_bits   = AES_128_KEY_BYTE_LEN;
 589    const psa_algorithm_t alg        = PSA_ALG_CMAC; /* set algorithm to cmac */
 590    psa_key_attributes_t  attributes = PSA_KEY_ATTRIBUTES_INIT;
 591    psa_key_id_t          key        = 0U;
 592    size_t                output_len = 0U;
 593
 594    do
 595    {
 596        psa_status_t status;
 597
 598        if ((pInput == NULL) || (pKey == NULL) || (pOutput == NULL))
 599        {
 600            res = gSecBadArgument_c;
 601            break;
 602        }
 603        /* key initialisation before import */
 604        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
 605        psa_set_key_algorithm(&attributes, alg);
 606        psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 607        psa_set_key_bits(&attributes, AES_128_KEY_BITS);
 608        psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
 609                                              PSA_KEY_LIFETIME_VOLATILE, get_most_secure_key_location()));
 610
 611        status = psa_import_key(&attributes, pKey, key_bits, &key);
 612
 613        RAISE_ERROR(status, PSA_SUCCESS);
 614
 615        /* compute mac operation on pInput */
 616        status = psa_mac_compute(key, alg, pInput, inputLen, pOutput, AES_BLOCK_SIZE, &output_len);
 617        RAISE_ERROR(status, PSA_SUCCESS);
 618
 619        status = psa_destroy_key(key); /* destroy key after use */
 620        RAISE_ERROR(status, PSA_SUCCESS);
 621
 622        res = gSecSuccess_c;
 623
 624    } while (false);
 625    return res;
 626}
 627
 628/*! *********************************************************************************
 629 * \brief  This function performs AES-128-CCM on a message block.
 630 *
 631 * \param[in]  pInput       Pointer to the location of the input message (plaintext or ciphertext).
 632 *
 633 * \param[in]  inputLen     Length of the input plaintext in bytes when encrypting.
 634 *                          Length of the input ciphertext without the MAC length when decrypting.
 635 *
 636 * \param[in]  pAuthData    Pointer to the additional authentication data.
 637 *
 638 * \param[in]  authDataLen  Length of additional authentication data.
 639 *
 640 * \param[in]  pNonce       Pointer to the Nonce.
 641 *
 642 * \param[in]  nonceSize    The size of the nonce (7-13).
 643 *
 644 * \param[in]  pKey         Pointer to the location of the 128-bit key.
 645 *
 646 * \param[out]  pOutput     Pointer to the location to store the plaintext data when decrypting.
 647 *                          Pointer to the location to store the ciphertext data when encrypting.
 648 *
 649 * \param[out]  pCbcMac     Pointer to the location to store the Message Authentication Code (MAC) when encrypting.
 650 *                          Pointer to the location where the received MAC can be found when decrypting.
 651 *
 652 * \param[in]  macSize     The size of the MAC.
 653 *
 654 * \param[in]  flags       Select encrypt/decrypt operations (gSecLib_CCM_Encrypt_c, gSecLib_CCM_Decrypt_c)
 655 *
 656 * \return 0 if encryption/decryption was successful; otherwise, error code for failed encryption/decryption
 657 *
 658 * \remarks At decryption, MIC fail is also signalled by returning a non-zero value.
 659 *
 660 ********************************************************************************** */
 661secResultType_t SecLib_AES_128_CCM(const uint8_t *pInput,
 662                                   uint16_t       inputLen,
 663                                   const uint8_t *pAuthData,
 664                                   uint16_t       authDataLen,
 665                                   const uint8_t *pNonce,
 666                                   uint8_t        nonceSize,
 667                                   const uint8_t *pKey,
 668                                   uint8_t       *pOutput,
 669                                   uint8_t       *pCbcMac,
 670                                   uint8_t        macSize,
 671                                   uint32_t       flags)
 672{
 673    secResultType_t ret = gSecError_c;
 674
 675    size_t               key_bits   = AES_128_KEY_BYTE_LEN;
 676    psa_algorithm_t      alg        = PSA_ALG_CCM;
 677    psa_status_t         status     = PSA_ERROR_GENERIC_ERROR;
 678    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
 679    psa_key_id_t         key        = 0U;
 680    size_t               output_len = 0U;
 681
 682    do
 683    {
 684        uint8_t *buff;
 685        if ((pInput == NULL) || (pAuthData == NULL) || (pNonce == NULL) || (pOutput == NULL) || (pKey == NULL) ||
 686            (pCbcMac == NULL))
 687        {
 688            ret = gSecBadArgument_c;
 689            break;
 690        }
 691        buff = MEM_BufferAlloc((uint32_t)(inputLen + (uint32_t)macSize));
 692        if (buff == NULL)
 693        {
 694            ret = gSecAllocError_c;
 695            break;
 696        }
 697
 698        /* set key usage depending on flags */
 699        if ((flags & gSecLib_CCM_Decrypt_c) != 0U)
 700        {
 701            psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
 702        }
 703        else
 704        {
 705            psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
 706        }
 707
 708        /* set the mac size of the algorithm to macSize, without this default is 32 */
 709        alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, (uint32_t)macSize);
 710
 711        psa_set_key_algorithm(&attributes, alg);
 712        psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
 713        psa_set_key_bits(&attributes, AES_128_KEY_BITS);
 714        psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
 715                                              PSA_KEY_LIFETIME_VOLATILE, get_most_secure_key_location()));
 716
 717        status = psa_import_key(&attributes, pKey, key_bits, &key);
 718        RAISE_ERROR(status, PSA_SUCCESS);
 719
 720        if ((flags & gSecLib_CCM_Decrypt_c) != 0U)
 721        {
 722            /* combine pInput and pCbcMac in a buffer to comply with PSA prototype psa_aead_decrypt */
 723            FLib_MemCpy(buff, pInput, inputLen);
 724            FLib_MemCpy(buff + inputLen, pCbcMac, macSize);
 725            status = psa_aead_decrypt(key, alg, pNonce, nonceSize, pAuthData, authDataLen, buff,
 726                                      (uint32_t)(inputLen + (uint32_t)macSize), pOutput,
 727                                      PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(inputLen), &output_len);
 728        }
 729        else
 730        {
 731            status = psa_aead_encrypt(key, alg, pNonce, nonceSize, pAuthData, authDataLen, pInput, inputLen, buff,
 732                                      (uint32_t)(inputLen + (uint32_t)macSize), &output_len);
 733            /* split output buffer in pOutput and pCbcMac */
 734            FLib_MemCpy(pOutput, buff, inputLen);
 735            FLib_MemCpy(pCbcMac, buff + inputLen, macSize);
 736        }
 737        RAISE_ERROR(status, PSA_SUCCESS);
 738
 739        status = psa_destroy_key(key); /* destroy key after use */
 740        RAISE_ERROR(status, PSA_SUCCESS);
 741
 742        (void)MEM_BufferFree(buff);
 743        ret = gSecSuccess_c;
 744    } while (false);
 745    return ret;
 746}
 747
 748/************************************************************************************
 749 * \brief Checks whether a public key is valid (point is on the curve).
 750 *
 751 * \return TRUE if valid, FALSE if not
 752 *
 753 ************************************************************************************/
 754bool_t ECP256_IsKeyValid(const ecp256Point_t *pKey)
 755{
 756    bool_t ret = false;
 757
 758    if (ECP256_LePointValid(pKey))
 759    {
 760        ret = true;
 761    }
 762
 763    return ret;
 764}
 765
 766/************************************************************************************
 767 * \brief Computes the Diffie-Hellman Key for an ECDH P256 key pair.
 768 *
 769 * \return gSecSuccess_c or error
 770 *
 771 ************************************************************************************/
 772secResultType_t ECDH_P256_ComputeDhKeySeg(computeDhKeyParam_t *pDhKeyData)
 773{
 774    secResultType_t res = gSecBadArgument_c;
 775    if (pDhKeyData != NULL)
 776    {
 777        res = ECDH_P256_ComputeDhKey(&pDhKeyData->privateKey, &pDhKeyData->peerPublicKey, &pDhKeyData->outPoint,
 778                                     pDhKeyData->keepInternalBlob);
 779    }
 780    return res;
 781}
 782
 783/************************************************************************************
 784 * \brief Computes the Diffie-Hellman Key for an ECDH P256 key pair.
 785 *
 786 * \return gSecSuccess_c or error
 787 *
 788 ************************************************************************************/
 789secResultType_t ECDH_P256_ComputeDhKey(const ecdhPrivateKey_t *pInPrivateKey,
 790                                       const ecdhPublicKey_t  *pInPeerPublicKey,
 791                                       ecdhDhKey_t            *pOutDhKey,
 792                                       const bool_t            keepBlobDhKey)
 793{
 794    secResultType_t ret        = gSecError_c;
 795    size_t          output_len = 0U;
 796    uint8_t         bufPub[sizeof(ecdhPublicKey_t) + 1]; /* +1 for point format byte*/
 797    uint8_t         bufSecret[sizeof(ecdhDhKey_t)];
 798    do
 799    {
 800        /* Check if output DH key pointer is valid */
 801        if ((pInPrivateKey == NULL) || (pInPeerPublicKey == NULL) || (pOutDhKey == NULL))
 802        {
 803            ret = gSecBadArgument_c;
 804            break;
 805        }
 806        if (psa_pECPKeyPair == NULL)
 807        {
 808            ret = gSecError_c;
 809            break;
 810        }
 811        /* Validate that the peer public key is a valid point on the curve */
 812        if (!ECP256_LePointValid(pInPeerPublicKey))
 813        {
 814            ret = gSecInvalidPublicKey_c;
 815            break;
 816        }
 817
 818        /* Convert little-endian to big-endian format for PSA API */
 819        ECP256_PointWrite(bufPub, pInPeerPublicKey, true);
 820
 821        /* Perform ECDH key agreement using PSA crypto API */
 822        psa_status_t status =
 823            psa_raw_key_agreement(PSA_ALG_ECDH, psa_pECPKeyPair->OwnKey, bufPub, sizeof(ecdhPublicKey_t) + 1, bufSecret,
 824                                  sizeof(ecdhDhKey_t), &output_len);
 825        RAISE_ERROR(status, PSA_SUCCESS);
 826
 827        ret = gSecSuccess_c;
 828
 829        /* Convert big-endian to little-endian format for output */
 830        ECP256_PointCopy_and_change_endianness(pOutDhKey->raw, bufSecret);
 831
 832    } while (false);
 833    return ret;
 834}
 835
 836/************************************************************************************
 837 * \brief Generates a new ECDH P256 Private/Public key pair
 838 *
 839 * \return gSecSuccess_c or error
 840 *
 841 ************************************************************************************/
 842secResultType_t ECDH_P256_GenerateKeysSeg(computeDhKeyParam_t *pDhKeyData)
 843{
 844    secResultType_t res = gSecBadArgument_c;
 845    if (pDhKeyData != NULL)
 846    {
 847        res = ECDH_P256_GenerateKeys(&pDhKeyData->outPoint, &pDhKeyData->privateKey);
 848    }
 849    return res;
 850}
 851
 852/************************************************************************************
 853 * \brief Generates a new ECDH P256 Private/Public key pair
 854 *
 855 * \return gSecSuccess_c or error
 856 *
 857 ************************************************************************************/
 858secResultType_t ECDH_P256_GenerateKeys(ecdhPublicKey_t *pOutPublicKey, ecdhPrivateKey_t *pOutPrivateKey)
 859{
 860    secResultType_t      ret        = gSecError_c;
 861    psa_status_t         st         = PSA_SUCCESS;
 862    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
 863    psa_key_id_t         key        = 0U;
 864    size_t               output_len = 0U;
 865    uint8_t              bufPub[sizeof(ecdhPublicKey_t) + 1]; /* +1 for point format byte*/
 866
 867    do
 868    {
 869        if ((pOutPublicKey == NULL) || (pOutPrivateKey == NULL))
 870        {
 871            ret = gSecBadArgument_c;
 872            break;
 873        }
 874        /* Check if there's an existing key pair and destroy it if present */
 875        if (psa_pECPKeyPair != NULL)
 876        {
 877            /* Once the key oject gets destroyed context is not ready anymore */
 878            st = psa_destroy_key(psa_pECPKeyPair->OwnKey);
 879            RAISE_ERROR(st, PSA_SUCCESS)
 880            FLib_MemSet(psa_pECPKeyPair, 0U, sizeof(psa_ecp256_context_t));
 881            psa_pECPKeyPair = NULL;
 882        }
 883
 884        /* psa_g_ECP_KeyPair.keyId = KEY_ID_BLE0; */
 885
 886        /* Set the global key pair context */
 887        psa_pECPKeyPair = &psa_g_ECP_KeyPair;
 888
 889        /* Configure key attributes for ECC P-256 key pair generation */
 890        psa_set_key_bits(&attributes, 256);
 891        psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
 892        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
 893        psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
 894                                              PSA_KEY_LIFETIME_VOLATILE, get_most_secure_key_location()));
 895        psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
 896
 897        /* Generate the ECC P-256 key pair */
 898        st = psa_generate_key(&attributes, &key);
 899        RAISE_ERROR(st, PSA_SUCCESS);
 900
 901        /* Export the public key from the generated key pair */
 902        st = psa_export_public_key(key, bufPub, sizeof(ecdhPublicKey_t) + 1, &output_len);
 903        RAISE_ERROR(st, PSA_SUCCESS);
 904
 905        /* Store the key ID in the global context */
 906        psa_pECPKeyPair->OwnKey = key;
 907
 908        /* Convert public key from big-endian to little-endian format and store in output */
 909        ECP256_PointLoad(pOutPublicKey, bufPub, true);
 910
 911        ret = gSecSuccess_c;
 912    } while (false);
 913    return ret;
 914}
 915
 916/************************************************************************************
 917 * \brief Generates a public key from a scalar given as input
 918 *
 919 * This function performs the multiplication of the scalar by the EC P 256 G point.
 920 * The resulting point is the public key corresponding to the private key constituted by the scalar.
 921 * This calculation is also involved in the compute L stage in the SPAKE2+ where the W1 argument
 922 * plays the role of the private key argument after modular reduction.
 923 * PSA does not support scalar multiplication via ele200 in hardware so this function uses software
 924 * implementation.
 925 *
 926 * \return gSecEcp256Success_c or error
 927 *
 928 ************************************************************************************/
 929secEcp256Status_t ECP256_GeneratePublicKey(uint8_t       *pOutPublicKey,
 930                                           const uint8_t *pInPrivateKey,
 931                                           void          *pMultiplicationBuffer)
 932{
 933    secEcp256Status_t ret = gSecEcp256BadParameters_c;
 934    if ((pOutPublicKey != NULL) && (pInPrivateKey != NULL))
 935    {
 936#if !(defined gSecLibUseDspExtension_d && (gSecLibUseDspExtension_d != 0))
 937        if (pMultiplicationBuffer != NULL)
 938        {
 939            big_int256_t  privKey;
 940            ecp256Point_t out;
 941            FLib_MemCpyReverseOrder((uint8_t *)&privKey, pInPrivateKey, sizeof(big_int256_t));
 942            ret = ECP256_GeneratePublicKeySeg(&out.raw[0], (uint8_t *)&privKey, pMultiplicationBuffer);
 943            ECP256_PointCopy_and_change_endianness((uint8_t *)pOutPublicKey, &out.raw[0]);
 944        }
 945#else
 946        NOT_USED(pMultiplicationBuffer);
 947        ret = ECP256_GeneratePublicKeyUltraFast(pOutPublicKey, pInPrivateKey);
 948#endif
 949    }
 950    return ret;
 951}
 952
 953/************************************************************************************
 954 * \brief Function used to create the mac key and LTK using Bluetooth F5 algorithm.
 955 *        Less secure version not using secure bus.
 956 *
 957 * \param  [out] pMacKey 128 bit MacKey output location (pointer)
 958 * \param  [out] pLtk    128 bit LTK output location (pointer)
 959 * \param  [in] pW       256 bit W (pointer) (DHKey)
 960 * \param  [in] pN1      128 bit N1 (pointer) (Na)
 961 * \param  [in] pN2      128 bit N2 (pointer) (Nb)
 962 * \param  [in] a1at     8 bit A1 address type, 0 = Public, 1 = Random
 963 * \param  [in] pA1      48 bit A1 (pointer) (A)
 964 * \param  [in] a2at     8 bit A2 address type, 0 = Public, 1 = Random
 965 * \param  [in] pA2      48 bit A2 (pointer) (B)
 966 *
 967 * \retval gSecSuccess_c operation succeeded
 968 * \retval gSecError_c operation failed
 969 *
 970 ************************************************************************************/
 971secResultType_t SecLib_GenerateBluetoothF5Keys(uint8_t       *pMacKey,
 972                                               uint8_t       *pLtk,
 973                                               const uint8_t *pW,
 974                                               const uint8_t *pN1,
 975                                               const uint8_t *pN2,
 976                                               const uint8_t  a1at,
 977                                               const uint8_t *pA1,
 978                                               const uint8_t  a2at,
 979                                               const uint8_t *pA2)
 980{
 981    secResultType_t result     = gSecError_c;
 982    const uint8_t   f5KeyId[4] = {0x62, 0x74, 0x6c, 0x65}; /*!< Big Endian, "btle" */
 983    uint8_t         f5CmacBuffer[1 + 4 + 16 + 16 + 7 + 7 + 2];
 984    /* Counter[1] || keyId[4] || N1[16] || N2[16] || A1[7] || A2[7] || Length[2] = 53 */
 985
 986    uint8_t       f5T[16]    = {0};
 987    const uint8_t f5Salt[16] = {0x6C, 0x88, 0x83, 0x91, 0xAA, 0xF5, 0xA5, 0x38,
 988                                0x60, 0x37, 0x0B, 0xDB, 0x5A, 0x60, 0x83, 0xBE}; /*!< Big endian */
 989
 990    do
 991    {
 992        uint8_t tempOut[16] = {0u};
 993
 994        /*! Check for NULL output pointers and return with proper status if this is the case. */
 995        if ((NULL == pMacKey) || (NULL == pLtk) || (NULL == pN1) || (NULL == pN2) || (NULL == pA1) || (NULL == pA2))
 996        {
 997            result = gSecBadArgument_c;
 998            break;
 999        }
1000
1001        /*! Compute the f5 function key T using the predefined salt as key for AES-128-CAMC */
1002        result = SecLib_AES_128_CMAC_LsbFirstInput((const uint8_t *)pW, 32, (const uint8_t *)f5Salt, f5T);
1003        if (result != gSecSuccess_c)
1004        {
1005            break;
1006        }
1007
1008        /*! Build the most significant part of the f5 input data to compute the MacKey */
1009        f5CmacBuffer[0] = 0; /* Counter = 0 */
1010        FLib_MemCpy(&f5CmacBuffer[1], (const uint8_t *)f5KeyId, 4);
1011        FLib_MemCpyReverseOrder(&f5CmacBuffer[5], (const uint8_t *)pN1, 16);
1012        FLib_MemCpyReverseOrder(&f5CmacBuffer[21], (const uint8_t *)pN2, 16);
1013        f5CmacBuffer[37] = 0x01U & a1at;
1014        FLib_MemCpyReverseOrder(&f5CmacBuffer[38], (const uint8_t *)pA1, 6);
1015        f5CmacBuffer[44] = 0x01U & a2at;
1016        FLib_MemCpyReverseOrder(&f5CmacBuffer[45], (const uint8_t *)pA2, 6);
1017        f5CmacBuffer[51] = 0x01; /* Length msB big endian = 0x01, Length = 256 */
1018        f5CmacBuffer[52] = 0x00; /* Length lsB big endian = 0x00, Length = 256 */
1019
1020        /*! Compute the MacKey into the temporary buffer. */
1021        result = SecLib_AES_128_CMAC(f5CmacBuffer, sizeof(f5CmacBuffer), f5T, tempOut);
1022        if (result != gSecSuccess_c)
1023        {
1024            break;
1025        }
1026        /*! Copy the MacKey to the output location
1027         *  in reverse order. The CMAC result is generated MSB first. */
1028        FLib_MemCpyReverseOrder(pMacKey, (const uint8_t *)tempOut, 16);
1029
1030        /*! Build the least significant part of the f5 input data to compute the MacKey.
1031         *  It is identical to the most significant part with the exception of the counter. */
1032        f5CmacBuffer[0] = 1; /* Counter = 1 */
1033
1034        /*! Compute the LTK into the temporary buffer. */
1035        result = SecLib_AES_128_CMAC(f5CmacBuffer, sizeof(f5CmacBuffer), f5T, tempOut);
1036        if (result != gSecSuccess_c)
1037        {
1038            break;
1039        }
1040        /*! Copy the LTK to the output location
1041         *  in reverse order. The CMAC result is generated MSB first. */
1042        FLib_MemCpyReverseOrder(pLtk, (const uint8_t *)tempOut, 16);
1043
1044        result = gSecSuccess_c;
1045
1046    } while (false);
1047
1048    return result;
1049}
1050
1051/*! *********************************************************************************
1052 * \brief  This function implements the SMP ah cryptographic toolbox function which
1053           calculates the hash part of a Resolvable Private Address.
1054 *         The key is kept in plaintext.
1055 *
1056 * \param[out]  pHash  Pointer where the 24 bit hash of a Resolvable Private Address value
1057 *                     will be written.
1058 *
1059 * \param[in]  pKey  Pointer to the 128 bit key.
1060 *
1061 * \param[in]  pR   Pointer to the 24 bit random value (Prand) of a Resolvable private Address.
1062 *                  The most significant bits of this field must be 0b01 for Resolvable Private
1063 *                  Addresses.
1064 *
1065 * \retval  gSecSuccess_c  All operations were successful.
1066 * \retval  gSecError_c The call failed.
1067 *
1068 ********************************************************************************** */
1069secResultType_t SecLib_VerifyBluetoothAh(uint8_t *pHash, const uint8_t *pKey, const uint8_t *pR)
1070{
1071    secResultType_t result           = gSecError_c;
1072    uint8_t         tempAddrPart[16] = {0u};
1073    uint8_t         tempOutHash[16]  = {0u};
1074    uint8_t         tempKey[16]      = {0u};
1075    do
1076    {
1077        /*! Check for NULL output pointers and return with proper status if this is the case. */
1078        if ((NULL == pHash) || (NULL == pKey) || (NULL == pR))
1079        {
1080            result = gSecBadArgument_c;
1081            break;
1082        }
1083        /* Initialize the r' value in the temporary location. 3 bytes of ramdom value.
1084         *  Initialize it reversed for AES.
1085         */
1086        for (int i = 0; i < 3; i++)
1087        {
1088            tempAddrPart[15 - i] = pR[i];
1089        }
1090        /* Regular operation with plaintext key */
1091        /*! Reverse the Key and place it in a temporary location. */
1092        FLib_MemCpyReverseOrder(tempKey, (const uint8_t *)pKey, 16);
1093
1094        /*! Compute the hash. */
1095        AES_128_Encrypt(tempAddrPart, tempKey, tempOutHash);
1096
1097        /*! Copy the relevant bytes to the output. */
1098        pHash[0] = tempOutHash[15];
1099        pHash[1] = tempOutHash[14];
1100        pHash[2] = tempOutHash[13];
1101
1102        result = gSecSuccess_c;
1103
1104    } while (false);
1105    return result;
1106}
1107
1108void ECDH_P256_FreeDhKeyDataSecure(computeDhKeyParam_t *pDhKeyData)
1109{
1110    NOT_USED(pDhKeyData);
1111}
1112
1113secResultType_t SecLib_DeriveBluetoothSKDSecure(const uint8_t *pInSKD,
1114                                                const uint8_t *pLtkBlob,
1115                                                bool_t         bOpenKey,
1116                                                uint8_t       *pOutSKD)
1117{
1118    NOT_USED(pInSKD);
1119    NOT_USED(pLtkBlob);
1120    NOT_USED(bOpenKey);
1121    NOT_USED(pOutSKD);
1122
1123    return gSecError_c;
1124}
1125
1126secResultType_t SecLib_ObfuscateKeySecure(const uint8_t *pKey, uint8_t *pBlob, const uint8_t blobType)
1127{
1128    NOT_USED(pKey);
1129    NOT_USED(pBlob);
1130    NOT_USED(blobType);
1131    return gSecError_c;
1132}
1133
1134secResultType_t SecLib_DeobfuscateKeySecure(const uint8_t *pBlob, uint8_t *pKey)
1135{
1136    NOT_USED(pBlob);
1137    NOT_USED(pKey);
1138    return gSecError_c;
1139}
1140
1141secResultType_t ECDH_P256_ComputeA2BKeySecure(const ecdhPublicKey_t *pInPeerPublicKey, ecdhDhKey_t *pOutE2EKey)
1142{
1143    NOT_USED(pInPeerPublicKey);
1144    NOT_USED(pOutE2EKey);
1145    return gSecError_c;
1146}
1147
1148secResultType_t ECDH_P256_FreeE2EKeyDataSecure(ecdhDhKey_t *pE2EKeyData)
1149{
1150    NOT_USED(pE2EKeyData);
1151    return gSecError_c;
1152}
1153
1154secResultType_t SecLib_ExportA2BBlobSecure(const void *pKey, const secInputKeyType_t keyType, uint8_t *pOutKey)
1155{
1156    NOT_USED(pKey);
1157    NOT_USED(keyType);
1158    NOT_USED(pOutKey);
1159    return gSecError_c;
1160}
1161
1162secResultType_t SecLib_ImportA2BBlobSecure(const uint8_t *pKey, const secInputKeyType_t keyType, uint8_t *pOutKey)
1163{
1164    NOT_USED(pKey);
1165    NOT_USED(keyType);
1166    NOT_USED(pOutKey);
1167    return gSecError_c;
1168}
1169
1170secResultType_t SecLib_GenerateBluetoothF5KeysSecure(uint8_t       *pMacKey,
1171                                                     uint8_t       *pLtk,
1172                                                     const uint8_t *pW,
1173                                                     const uint8_t *pN1,
1174                                                     const uint8_t *pN2,
1175                                                     const uint8_t  a1at,
1176                                                     const uint8_t *pA1,
1177                                                     const uint8_t  a2at,
1178                                                     const uint8_t *pA2)
1179{
1180    NOT_USED(pMacKey);
1181    NOT_USED(pLtk);
1182    NOT_USED(pW);
1183    NOT_USED(pN1);
1184    NOT_USED(pN2);
1185    NOT_USED(a1at);
1186    NOT_USED(pA1);
1187    NOT_USED(a2at);
1188    NOT_USED(pA2);
1189    return gSecError_c;
1190}
1191
1192secResultType_t SecLib_VerifyBluetoothAhSecure(uint8_t *pHash, const uint8_t *pKey, const uint8_t *pR)
1193{
1194    NOT_USED(pHash);
1195    NOT_USED(pKey);
1196    NOT_USED(pR);
1197    return gSecError_c;
1198}
1199
1200secResultType_t SecLib_GenerateBluetoothEIRKBlobSecure(const void  *pIRK,
1201                                                       const bool_t blobInput,
1202                                                       const bool_t generateDKeyIRK,
1203                                                       uint8_t     *pOutEIRKblob)
1204{
1205    NOT_USED(pIRK);
1206    NOT_USED(blobInput);
1207    NOT_USED(generateDKeyIRK);
1208    NOT_USED(pOutEIRKblob);
1209    return gSecError_c;
1210}
1211
1212secResultType_t SecLib_GenerateSymmetricKey(const uint32_t keySize, const bool_t blobOutput, void *pOut)
1213{
1214    NOT_USED(keySize);
1215    NOT_USED(blobOutput);
1216    NOT_USED(pOut);
1217    return gSecError_c;
1218}