/** * @file ex_sst_kp.c * @author NXP Semiconductors * @version 1.0 * @par License * * Copyright 2016 NXP * SPDX-License-Identifier: Apache-2.0 * * @par Description * Example invocation of ECC key pair secure storage specific functionality of the A71CH */ #include #include #include #include #include "a71ch_ex.h" #include "a71_debug.h" #include "sm_types.h" #include "sm_apdu.h" #include "ax_util.h" #include "tst_sm_util.h" #include "tst_a71ch_util.h" #include "tstHostCrypto.h" #include "HostCryptoAPI.h" /** * Demonstrate storage of key pairs: * - ::exSstKeyPair * */ U8 exSstKp() { U8 result = 1; PRINTF( "\r\n-----------\r\nStart exSstKp()\r\n------------\r\n"); DEV_ClearChannelState(); // No channel encryption result &= exSstKeyPair(INIT_MODE_RESET); // With channel encryption result &= exSstKeyPair(INIT_MODE_RESET_DO_SCP03); // overall result PRINTF( "\r\n-----------\r\nEnd exSstKp(), result = %s\r\n------------\r\n", ((result == 1)? "OK": "FAILED")); return result; } /** * Demonstrate * - setting/getting/erasing/freezing of key pairs * - demonstrate key can be used through sign operations * * @param[in] initMode Visit the documentation of ::a71chInitModule for * more information on this parameter * @param[in] appletVersion The applet version * * @return 1 if passed. */ U8 exSstKeyPair(U8 initMode) { U8 result = 1; U16 err; int i; HLSE_RET_CODE retcode; ECCCurve_t eccCurve = ECCCurve_NIST_P256; EC_KEY *eccKeyTls[A71CH_KEY_PAIR_MAX] = {0}; eccKeyComponents_t eccKcTls[A71CH_KEY_PAIR_MAX] = {0}; EC_KEY *eccKeyAlt = NULL; eccKeyComponents_t eccKcAlt; U8 fetchedPubKey[65]; U16 fetchedPubKeyLen = sizeof(fetchedPubKey); U8 hashSha256[32]; U16 hashSha256Len = sizeof(hashSha256); U8 signature[128]; U16 signatureLen = sizeof(signature); HLSE_MECHANISM_INFO mechInfo; // const U16 expectedPubKeyLen = 65; const U16 expectedPrivKeyLen = 32; SST_Index_t kpIndex; PRINTF("\r\n-----------\r\nStart exSstKeyPair(%s)\r\n------------\r\n", getInitModeAsString(initMode)); // Initialize the A71CH (Debug mode restrictions may apply) result &= a71chInitModule(initMode); // Start by creating, inserting and checking keys for (i=0; i>1; kpIndex++) { PRINTF( "\r\nA71_FreezeEccKeyPair(0x%02x)\r\n", kpIndex); err = A71_FreezeEccKeyPair((SST_Index_t)kpIndex); result &= AX_CHECK_SW(err, SW_OK, "err"); } // Now fetch and compare the values with the reference values for (kpIndex=0; kpIndex>1; kpIndex++) { PRINTF( "\r\nA71_SetEccKeyPair(0x%02x)\r\n", kpIndex); err = A71_SetEccKeyPair((SST_Index_t)kpIndex, eccKcAlt.pub, eccKcAlt.pubLen, eccKcAlt.priv, eccKcAlt.privLen); result &= AX_CHECK_SW(err, SW_COMMAND_NOT_ALLOWED, "Expected to fail, frozen credential cannot be overwritten"); } // Overwrite the second half for (kpIndex=A71CH_KEY_PAIR_MAX>>1; kpIndex>1; kpIndex>1; kpIndex>1; kpIndex