/** * @file ex_hlse_gpstorage.c * @author NXP Semiconductors * @version 1.0 * @par License * * Copyright 2016 NXP * SPDX-License-Identifier: Apache-2.0 * * @par Description * Example invocations of general purpose storage related functionality of the A71CH */ #include #include #include #include // #include "a70cm_configuration.h" #include "a71ch_ex_hlse.h" #include "ax_util.h" #include "a71_debug.h" #include "sm_types.h" #include "sm_apdu.h" #include "tst_sm_util.h" #include "tst_a71ch_util.h" #include "tst_hlse_a71ch_util.h" #include "HLSEAPI.h" #define EX_RND_DATA 0x00 //!< Bit position 0 decides on random or incrementing data payload #define EX_INC_DATA 0x01 //!< Bit position 0 decides on random or incrementing data payload #define EX_ALL_PACKET_SIZES 0x02 //!< Bit position 1 decides on all or a selection of packetsizes /****************************************************************************** * test *****************************************************************************/ static U8 exGpStoragePacketSize(U8 initMode, U8 tstMode); static U8 exGpStorageFreeze(U8 initMode, U8 tstMode); static U8 exMonotonicCounter(U8 initMode); /** * Demonstrate general purpose storage functionality * - ::exGpStoragePacketSize * - ::exGpStorageFreeze * * Demonstrate monotonic counter usage * - ::exMonotonicCounter */ U8 exHlseGPStorage(U8 tstMode) { U8 result = 1; PRINTF( "\r\n-----------\r\nStart exGPStorage(%s)\r\n------------\r\n", ((tstMode & EXTENDED_TEST) == EXTENDED_TEST) ? "Extended Test" : "Fast Test"); // No channel encryption DEV_ClearChannelState(); result &= exMonotonicCounter(INIT_MODE_RESET); result &= exGpStoragePacketSize(INIT_MODE_RESET, EX_RND_DATA); result &= exGpStoragePacketSize(INIT_MODE_NO_RESET, EX_INC_DATA); if ((tstMode & EXTENDED_TEST) == EXTENDED_TEST) { result &= exGpStoragePacketSize(INIT_MODE_NO_RESET, EX_RND_DATA|EX_ALL_PACKET_SIZES); } result &= exGpStorageFreeze(INIT_MODE_RESET, EX_INC_DATA); // Use channel encryption result &= exMonotonicCounter(INIT_MODE_RESET_DO_SCP03); result &= exGpStoragePacketSize(INIT_MODE_NO_RESET, EX_RND_DATA); PRINTF( "\r\n-----------\r\nEnd exGPStorage(%s), result = %s\r\n------------\r\n", ((tstMode & EXTENDED_TEST) == EXTENDED_TEST) ? "Extended Test" : "Fast Test", ((result == 1)? "OK": "FAILED")); return result; } /** * Demonstrate monotonic counter usage. * * @param[in] initMode Visit the documentation of ::a71chInitModule for more information on this parameter */ static U8 exMonotonicCounter(U8 initMode) { U8 result = 1; U8 index = 0; U16 err; U32 tgtValue[] = {0x00000004, 0x00000014, 0x00000024, 0x00000034}; U32 readValue = 0; HLSE_OBJECT_HANDLE counterHandles[A71CH_COUNTER_MAX] = {0}; U16 counterHandlesNum = A71CH_COUNTER_MAX; PRINTF("\r\n-----------\r\nStart exMonotonicCounter(%s)\r\n------------\r\n", getInitModeAsString(initMode)); // Initialize the A71CH (Debug mode restrictions may apply) result &= hlse_a71chInitModule(initMode); assert(result); // To hold counters handles err = HLSE_EnumerateObjects(HLSE_COUNTER, counterHandles, &counterHandlesNum); result &= AX_CHECK_SW(err, HLSE_SW_OK, "err"); assert(result); // Check all counters default to value 0 for (index=0; index>1); #if 1 err = A71_FreezeGpData(0, A71CH_GP_STORAGE_SIZE>>1); #else // TODO HLSE // HLSE_FreezeObject //hlse_FreezeGpData(0, A71CH_GP_STORAGE_SIZE_A >> 1); assert(0); #endif result &= AX_CHECK_SW(err, SW_OK, "A71_FreezeGpData fails"); // Attempt to write in the frozen area packetSize = 16; offset = 0; PRINTF( "\r\nA71_SetGpData(%d, %d, ...)\r\n", offset, packetSize); #if 1 err = A71_SetGpData(offset, gpStorageNew, packetSize); #else // TODO HLSE assert(0); #endif result &= AX_CHECK_SW(err, SW_COMMAND_NOT_ALLOWED, "A71_SetGpData was expected to fail (because area has just been locked)"); // Write to open area offset = A71CH_GP_STORAGE_SIZE>>1; PRINTF( "\r\nA71_SetGpData(%d, %d, ...)\r\n", offset, packetSize); #if 1 err = A71_SetGpData(offset, gpStorageNew, packetSize); #else // TODO HLSE assert(0); #endif result &= AX_CHECK_SW(err, SW_OK, "A71_SetGpData fails"); // Create expected data pattern memcpy(gpStorageExpected, gpStorageRef, A71CH_GP_STORAGE_SIZE); memcpy(gpStorageExpected+(A71CH_GP_STORAGE_SIZE>>1), gpStorageNew, 16); // Retrieve data and compare with expected data pattern packetSize = A71CH_GP_STORAGE_SIZE; PRINTF( "\r\nA71_GetGpData(0, %d, ...)\r\n", packetSize); #if 1 err = A71_GetGpData(0, gpStorage, packetSize); #else // TODO HLSE assert(0); #endif result &= AX_CHECK_SW(err, SW_OK, "A71_GetGpData fails"); result &= AX_COMPARE_BYTE_ARRAY("gpStorageExpected", gpStorageExpected, A71CH_GP_STORAGE_SIZE, "gpStorage", gpStorage, A71CH_GP_STORAGE_SIZE, AX_COLON_32); PRINTF( "\r\n-----------\r\nEnd exGpStorageFreeze(%s), result = %s\r\n------------\r\n", getInitModeAsString(initMode), ((result == 1)? "OK": "FAILED")); return result; }