108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/** 208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * Copyright(c) 2011 Trusted Logic. All rights reserved. 308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * 408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * Redistribution and use in source and binary forms, with or without 508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * modification, are permitted provided that the following conditions 608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * are met: 708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * 808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * * Redistributions of source code must retain the above copyright 908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * notice, this list of conditions and the following disclaimer. 1008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * * Redistributions in binary form must reproduce the above copyright 1108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * notice, this list of conditions and the following disclaimer in 1208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * the documentation and/or other materials provided with the 1308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * distribution. 1408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * * Neither the name Trusted Logic nor the names of its 1508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * contributors may be used to endorse or promote products derived 1608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * from this software without specific prior written permission. 1708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * 1808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic */ 3008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 3108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#include <assert.h> 3208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#include <stdlib.h> 3308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#include <string.h> 3408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 3508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#define MTC_EXPORTS 3608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#include "mtc.h" 3708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 3808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/* Included for the TEE management */ 3908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#include "pkcs11_internal.h" 4008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 4108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 4208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/*------------------------------------------------------------------------------ 4308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic Defines 4408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic------------------------------------------------------------------------------*/ 4508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 4608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/** 4708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * The magic word. 4808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic */ 4908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic#define MTC_SESSION_MAGIC ( (uint32_t)0x4D544300 ) /* "MTC\0" */ 5008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 5108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/** 5208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic * The MTC session context 5308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic */ 5408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logictypedef struct 5508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 5608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic /* Magic word, must be set to {MTC_SESSION_MAGIC}. */ 5708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic uint32_t nMagicWord; 5808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 5908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic /* MTC Identifier */ 6008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic uint32_t nCounterIdentifier; 6108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 6208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic /* TEEC session and cryptoki session */ 6308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_Session sSession; 6408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic uint32_t hCryptoSession; 6508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 6608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} MTC_SESSION_CONTEXT; 6708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 6808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 6908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic bool g_bMTCInitialized = false; 7008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 7108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 7208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/*------------------------------------------------------------------------------ 7308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic Static functions 7408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic------------------------------------------------------------------------------*/ 7508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 7608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicstatic S_RESULT static_getMonotonicCounter(S_HANDLE hCounter, 7708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic S_MONOTONIC_COUNTER_VALUE* psValue, 7808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic bool bIncrement) 7908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 8008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_Result nError; 8108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_Operation sOperation; 8208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic MTC_SESSION_CONTEXT* pSession = NULL; 8308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic uint32_t nCommandID; 8408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 8508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (!g_bMTCInitialized) 8608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 8708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return S_ERROR_BAD_STATE; 8808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 8908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 9008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic pSession = (MTC_SESSION_CONTEXT *)hCounter; 9108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if ((pSession == NULL) || (pSession->nMagicWord != MTC_SESSION_MAGIC)) 9208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 9308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return S_ERROR_BAD_PARAMETERS; 9408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 9508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 9608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (bIncrement) 9708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 9808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nCommandID = SERVICE_SYSTEM_PKCS11_INCREMENT_MTC_COMMAND_ID; 9908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 10008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic else 10108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 10208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nCommandID = SERVICE_SYSTEM_PKCS11_GET_MTC_COMMAND_ID; 10308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 10408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 10508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, TEEC_NONE); 10608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic sOperation.params[0].value.a = pSession->nCounterIdentifier; 10708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic sOperation.params[0].value.b = 0; 10808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nError = TEEC_InvokeCommand(&pSession->sSession, 10908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic (pSession->hCryptoSession << 16 ) | 11008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic (nCommandID & 0x00007FFF), 11108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic &sOperation, 11208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL); 11308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 11408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic psValue->nLow = sOperation.params[0].value.a; 11508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic psValue->nHigh = sOperation.params[0].value.b; 11608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 11708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return nError; 11808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 11908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 12008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic/*------------------------------------------------------------------------------ 12108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic API 12208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic------------------------------------------------------------------------------*/ 12308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 12408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicMTC_EXPORT S_RESULT SMonotonicCounterInit(void) 12508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 12608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_Result nTeeError; 12708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 12808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic stubMutexLock(); 12908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (g_bMTCInitialized) 13008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 13108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nTeeError = TEEC_SUCCESS; 13208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 13308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic else 13408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 13508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nTeeError = stubInitializeContext(); 13608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (nTeeError == TEEC_SUCCESS) 13708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 13808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic g_bMTCInitialized = true; 13908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 14008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 14108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic stubMutexUnlock(); 14208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 14308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return nTeeError; 14408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 14508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 14608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicMTC_EXPORT void SMonotonicCounterTerminate(void) 14708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 14808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic stubMutexLock(); 14908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (g_bMTCInitialized) 15008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 15108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic stubFinalizeContext(); 15208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic g_bMTCInitialized = false; 15308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 15408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic stubMutexUnlock(); 15508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 15608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 15708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicMTC_EXPORT S_RESULT SMonotonicCounterOpen( 15808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic uint32_t nCounterIdentifier, 15908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic OUT S_HANDLE* phCounter) 16008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 16108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_Result nError; 16208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_Operation sOperation; 16308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic MTC_SESSION_CONTEXT* pSession = NULL; 16408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic S_MONOTONIC_COUNTER_VALUE nCounterValue; 16508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 16608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (phCounter == NULL) 16708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 16808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return S_ERROR_BAD_PARAMETERS; 16908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 17008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 17108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *phCounter = S_HANDLE_NULL; 17208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 17308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (!g_bMTCInitialized) 17408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 17508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return S_ERROR_BAD_STATE; 17608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 17708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 17808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (nCounterIdentifier != S_MONOTONIC_COUNTER_GLOBAL) 17908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 18008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return S_ERROR_ITEM_NOT_FOUND; 18108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 18208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 18308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic pSession = (MTC_SESSION_CONTEXT*)malloc(sizeof(MTC_SESSION_CONTEXT)); 18408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (pSession == NULL) 18508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 18608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return S_ERROR_OUT_OF_MEMORY; 18708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 18808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic memset(pSession, 0, sizeof(MTC_SESSION_CONTEXT)); 18908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic pSession->nMagicWord = MTC_SESSION_MAGIC; 19008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 19108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic /* Open a TEE session with the system service */ 19208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nError = TEEC_OpenSession(&g_sContext, 19308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic &pSession->sSession, 19408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic &SERVICE_UUID, 19508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_LOGIN_PUBLIC, 19608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL, 19708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL, /* No operation parameters */ 19808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL); 19908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (nError != TEEC_SUCCESS) 20008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 20108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic goto error; 20208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 20308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 20408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic /* Open a cryptoki session */ 20508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, TEEC_NONE); 20608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic sOperation.params[0].value.a = CKV_TOKEN_SYSTEM_SHARED; 20708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic sOperation.params[0].value.b = CKF_RW_SESSION | CKF_SERIAL_SESSION; 20808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nError = TEEC_InvokeCommand(&pSession->sSession, 20908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic SERVICE_SYSTEM_PKCS11_C_OPEN_SESSION_COMMAND_ID & 0x00007FFF, 21008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic &sOperation, 21108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL); 21208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (nError != TEEC_SUCCESS) 21308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 21408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_CloseSession(&pSession->sSession); 21508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic goto error; 21608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 21708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 21808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic pSession->hCryptoSession = sOperation.params[0].value.a; 21908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic pSession->nCounterIdentifier = nCounterIdentifier; 22008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 22108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic nError = SMonotonicCounterGet((S_HANDLE)pSession, &nCounterValue); 22208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (nError != TEEC_SUCCESS) 22308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 22408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic SMonotonicCounterClose((S_HANDLE)pSession); 22508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return nError; 22608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 22708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 22808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic *phCounter = (S_HANDLE)pSession; 22908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 23008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return TEEC_SUCCESS; 23108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 23208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logicerror: 23308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic free(pSession); 23408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return nError; 23508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 23608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 23708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicMTC_EXPORT void SMonotonicCounterClose(S_HANDLE hCounter) 23808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 23908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic MTC_SESSION_CONTEXT* pSession; 24008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 24108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if (!g_bMTCInitialized) 24208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 24308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return; 24408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 24508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 24608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic pSession = (MTC_SESSION_CONTEXT *)hCounter; 24708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic if ((pSession == NULL) || (pSession->nMagicWord != MTC_SESSION_MAGIC)) 24808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic { 24908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return; 25008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic } 25108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 25208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic (void)TEEC_InvokeCommand(&pSession->sSession, 25308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic (pSession->hCryptoSession << 16 ) | 25408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic (SERVICE_SYSTEM_PKCS11_C_CLOSE_SESSION_COMMAND_ID & 0x00007FFF), 25508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL, /* No operation parameters */ 25608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic NULL); 25708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 25808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic TEEC_CloseSession(&pSession->sSession); 25908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic free(pSession); 26008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 26108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 26208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicMTC_EXPORT S_RESULT SMonotonicCounterGet( 26308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic S_HANDLE hCounter, 26408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic S_MONOTONIC_COUNTER_VALUE* psCurrentValue) 26508f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 26608f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return static_getMonotonicCounter(hCounter, psCurrentValue, false); 26708f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 26808f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic 26908f4da4b7ac801a691376dd94aa2af35b31e7998Trusted LogicMTC_EXPORT S_RESULT SMonotonicCounterIncrement( 27008f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic S_HANDLE hCounter, 27108f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic S_MONOTONIC_COUNTER_VALUE* psNewValue) 27208f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic{ 27308f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic return static_getMonotonicCounter(hCounter, psNewValue, true); 27408f4da4b7ac801a691376dd94aa2af35b31e7998Trusted Logic} 275