From ac6d8253981eb8d1a89e5a2d529e08c8f75f5aca Mon Sep 17 00:00:00 2001 From: Hisping Lin <hisping.lin@rock-chips.com> Date: Fri, 13 Sep 2024 16:51:28 +0800 Subject: [PATCH] lib: optee_clientApi: add support for esck key Change-Id: Ibee79c0860f4c80b080a2cc50c35624d58cb1d37 Signed-off-by: Hisping Lin <hisping.lin@rock-chips.com> --- include/optee_include/OpteeClientInterface.h | 5 + lib/optee_clientApi/OpteeClientInterface.c | 172 +++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/include/optee_include/OpteeClientInterface.h b/include/optee_include/OpteeClientInterface.h index eb7ce2619d8..a50b46ca827 100644 --- a/include/optee_include/OpteeClientInterface.h +++ b/include/optee_include/OpteeClientInterface.h @@ -24,6 +24,11 @@ enum RK_HDCP_KEYID { RK_HDCP_KEYMAX }; +enum RK_ESCK_KEYID { + RK_ESCK_KEY0 = 0, + RK_ESCK_KEYMAX +}; + /* Crypto mode */ enum RK_CIPIHER_MODE { RK_CIPHER_MODE_ECB = 0, diff --git a/lib/optee_clientApi/OpteeClientInterface.c b/lib/optee_clientApi/OpteeClientInterface.c index cf723bf4df6..230bdeff5d2 100644 --- a/lib/optee_clientApi/OpteeClientInterface.c +++ b/lib/optee_clientApi/OpteeClientInterface.c @@ -35,6 +35,9 @@ #define STORAGE_CMD_SET_OEM_HDCP_KEY_MASK 23 #define STORAGE_CMD_WRITE_OEM_ENCRYPT_DATA 24 #define STORAGE_CMD_OEM_ENCRYPT_DATA_IS_WRITTEN 25 +#define STORAGE_CMD_WRITE_ESCK_KEY 27 +#define STORAGE_CMD_ESCK_KEY_IS_WRITTEN 28 +#define STORAGE_CMD_SET_ESCK_KEY_MASK 29 #define CRYPTO_SERVICE_CMD_OEM_OTP_KEY_PHYS_CIPHER 0x00000002 @@ -1381,6 +1384,175 @@ exit: return TeecResult; } +uint32_t trusty_write_esck_key(enum RK_ESCK_KEYID key_id, + uint8_t *byte_buf, uint32_t byte_len) +{ + TEEC_Result TeecResult; + TEEC_Context TeecContext; + TEEC_Session TeecSession; + uint32_t ErrorOrigin; + + TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, + { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; + TEEC_UUID *TeecUuid = &tempuuid; + TEEC_Operation TeecOperation = {0}; + + TeecResult = OpteeClientApiLibInitialize(); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecResult = TEEC_InitializeContext(NULL, &TeecContext); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecResult = TEEC_OpenSession(&TeecContext, + &TeecSession, + TeecUuid, + TEEC_LOGIN_PUBLIC, + NULL, + NULL, + &ErrorOrigin); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecOperation.params[0].value.a = key_id; + + TEEC_SharedMemory SharedMem = {0}; + + SharedMem.size = byte_len; + SharedMem.flags = 0; + + TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); + if (TeecResult != TEEC_SUCCESS) + goto exit; + + TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; + TeecOperation.params[1].tmpref.size = SharedMem.size; + + memcpy(SharedMem.buffer, byte_buf, SharedMem.size); + TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, + TEEC_MEMREF_TEMP_INPUT, + TEEC_NONE, + TEEC_NONE); + + TeecResult = TEEC_InvokeCommand(&TeecSession, + STORAGE_CMD_WRITE_ESCK_KEY, + &TeecOperation, + &ErrorOrigin); + if (TeecResult != TEEC_SUCCESS) + goto exit; + +exit: + TEEC_ReleaseSharedMemory(&SharedMem); + TEEC_CloseSession(&TeecSession); + TEEC_FinalizeContext(&TeecContext); + + return TeecResult; +} + +uint32_t trusty_esck_key_is_written(enum RK_ESCK_KEYID key_id, uint8_t *value) +{ + TEEC_Result TeecResult; + TEEC_Context TeecContext; + TEEC_Session TeecSession; + uint32_t ErrorOrigin; + + *value = 0xFF; + + TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, + { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; + TEEC_UUID *TeecUuid = &tempuuid; + TEEC_Operation TeecOperation = {0}; + + TeecResult = OpteeClientApiLibInitialize(); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecResult = TEEC_InitializeContext(NULL, &TeecContext); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecResult = TEEC_OpenSession(&TeecContext, + &TeecSession, + TeecUuid, + TEEC_LOGIN_PUBLIC, + NULL, + NULL, + &ErrorOrigin); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecOperation.params[0].value.a = key_id; + + TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, + TEEC_NONE, + TEEC_NONE, + TEEC_NONE); + + TeecResult = TEEC_InvokeCommand(&TeecSession, + STORAGE_CMD_ESCK_KEY_IS_WRITTEN, + &TeecOperation, + &ErrorOrigin); + if (TeecResult == TEEC_SUCCESS) + *value = TeecOperation.params[0].value.b; + + TEEC_CloseSession(&TeecSession); + TEEC_FinalizeContext(&TeecContext); + + return TeecResult; +} + +uint32_t trusty_set_esck_key_mask(enum RK_ESCK_KEYID key_id) +{ + TEEC_Result TeecResult; + TEEC_Context TeecContext; + TEEC_Session TeecSession; + uint32_t ErrorOrigin; + + TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, + { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; + TEEC_UUID *TeecUuid = &tempuuid; + TEEC_Operation TeecOperation = {0}; + + TeecResult = OpteeClientApiLibInitialize(); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecResult = TEEC_InitializeContext(NULL, &TeecContext); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecResult = TEEC_OpenSession(&TeecContext, + &TeecSession, + TeecUuid, + TEEC_LOGIN_PUBLIC, + NULL, + NULL, + &ErrorOrigin); + if (TeecResult != TEEC_SUCCESS) + return TeecResult; + + TeecOperation.params[0].value.a = key_id; + + TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, + TEEC_NONE, + TEEC_NONE, + TEEC_NONE); + + TeecResult = TEEC_InvokeCommand(&TeecSession, + STORAGE_CMD_SET_ESCK_KEY_MASK, + &TeecOperation, + &ErrorOrigin); + if (TeecResult != TEEC_SUCCESS) + goto exit; + +exit: + TEEC_CloseSession(&TeecSession); + TEEC_FinalizeContext(&TeecContext); + + return TeecResult; +} + uint32_t trusty_oem_user_ta_transfer(void) { TEEC_Result TeecResult; -- GitLab