/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup TeeClient
* @{
*
* @brief Provides APIs for the client applications (CAs) in the Rich Execution Environment (normal mode) to
* access the trusted applications (TAs) in a Trusted Execution Environment (TEE).
*
* @since 20
*/
/**
* @file tee_client_api.h
*
* @brief Defines APIs for CAs to access TAs.
*
*
Example:
*
1. Initialize a TEE: Call TEEC_InitializeContext to initialize the TEE.
*
2. Open a session: Call TEEC_OpenSession with the Universal Unique Identifier (UUID) of the TA.
*
3. Send a command: Call TEEC_InvokeCommand to send a command to the TA.
*
4. Close the session: Call TEEC_CloseSession to close the session.
*
5. Close the TEE: Call TEEC_FinalizeContext to close the TEE.
*
* @library libteec.so
* @kit TEEKit
* @syscap SystemCapability.Tee.TeeClient
* @since 20
*/
#ifndef TEE_CLIENT_API_H
#define TEE_CLIENT_API_H
#include
#include "tee_client_type.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Defines the values of the parameters transmitted between the REE and TEE.
*
* @since 20
*/
#define TEEC_PARAM_TYPES(param0Type, param1Type, param2Type, param3Type) \
((param3Type) << 12 | (param2Type) << 8 | (param1Type) << 4 | (param0Type))
/**
* @brief Defines the value of the parameter specified by paramTypes and index.
*
* @since 20
*/
#define TEEC_PARAM_TYPE_GET(paramTypes, index) \
(((paramTypes) >> (4*(index))) & 0x0F)
/**
* @brief Initializes a TEE.
*
* The TEE must be initialized before a session is open or commands are sent.
* After the initialization, a connection is set up between the CA and the TEE.
*
* @param name [IN] Indicates the pointer to the TEE path.
* @param context [IN/OUT] Indicates the context pointer, which is the handle of the TEE.
*
* @return Returns {@code TEEC_SUCCESS} if the TEE is successfully initialized.
* Returns {@code TEEC_ERROR_BAD_PARAMETERS} if name is incorrect or context is null.
* Returns {@code TEEC_ERROR_GENERIC} if the available system resources are insufficient.
*
* @since 20
*/
TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context);
/**
* @brief Closes the TEE.
*
* After the TEE is closed, the CA is disconnected from the TEE.
*
* @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
*
* @since 20
*/
void TEEC_FinalizeContext(TEEC_Context *context);
/**
* @brief Opens a session.
*
* This function is used to set up a connection between the CA and the TA of the specified UUID in the specified TEE
* context. The data to be transferred is contained in operation.
* If a session is opened successfully, session is returned providing a description of the connection.
* If the session fails to open, returnOrigin is returned indicating the cause of the failure.
*
* @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
* @param session [OUT] Indicates the pointer to the session. The value cannot be null.
* @param destination [IN] Indicates the pointer to the UUID of the target TA. Each TA has a unique UUID.
* @param connectionMethod [IN] Indicates the connection method. For details, see {@link TEEC_LoginMethod}.
* @param connectionData [IN] Indicates the pointer to the connection data, which varies with the connection mode.
* If the connection mode is {@code TEEC_LOGIN_PUBLIC}, {@code TEEC_LOGIN_USER},
* {@code TEEC_LOGIN_USER_APPLICATION}, or {@code TEEC_LOGIN_GROUP_APPLICATION}, the connection data must be null.
* If the connection mode is {@code TEEC_LOGIN_GROUP} or {@code TEEC_LOGIN_GROUP_APPLICATION},
* the connection data must point to data of the uint32_t type, which indicates the target group user to be connected
* by the CA.
* @param operation [IN/OUT] Indicates the pointer to the data to be transmitted between the CA and TA.
* @param returnOrigin [IN/OUT] Indicates the pointer to the error source.
* For details, see {@code TEEC_ReturnCodeOrigin}.
*
* @return Returns {@code TEEC_SUCCESS} if the session is open successfully.
* Returns {@code TEEC_ERROR_BAD_PARAMETERS} if context, session, or destination is null.
* Returns {@code TEEC_ERROR_ACCESS_DENIED} if the access request is denied.
* Returns {@code TEEC_ERROR_OUT_OF_MEMORY} if the available system resources are insufficient.
* Returns {@code TEEC_ERROR_TRUSTED_APP_LOAD_ERROR} if the TA failed to be loaded.
* For details about other return values, see {@code TEEC_ReturnCode}.
*
* @since 20
*/
TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, const TEEC_UUID *destination,
uint32_t connectionMethod, const void *connectionData, TEEC_Operation *operation, uint32_t *returnOrigin);
/**
* @brief Closes a session.
*
* After the session is closed, the CA is disconnected from the TA.
*
* @param session [IN/OUT] Indicates the pointer to the session to close.
*
* @since 20
*/
void TEEC_CloseSession(TEEC_Session *session);
/**
* @brief Sends a command to a TA.
*
* The CA sends the command ID to the TA through the specified session.
*
* @param session [IN/OUT] Indicates the pointer to the session opened.
* @param commandID [IN] Indicates the command ID supported by the TA. It is defined by the TA.
* @param operation [IN/OUT] Indicates the pointer to the data to be sent from the CA to the TA.
* @param returnOrigin [IN/OUT] Indicates the pointer to the error source.
* For details, see {@code TEEC_ReturnCodeOrigin}.
*
* @return Returns {@code TEEC_SUCCESS} if the command is sent successfully.
* Returns {@code TEEC_ERROR_BAD_PARAMETERS} if session is null or
* operation is in incorrect format.
* Returns {@code TEEC_ERROR_ACCESS_DENIED} if the access request is denied.
* Returns {@code TEEC_ERROR_OUT_OF_MEMORY} if the available system resources are insufficient.
* For details about other return values, see {@code TEEC_ReturnCode}.
*
* @since 20
*/
TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID,
TEEC_Operation *operation, uint32_t *returnOrigin);
/**
* @brief Registers shared memory in the specified TEE context.
*
* The registered shared memory can implement zero-copy.
* The zero-copy function, however, also requires support by the operating system.
* At present, zero-copy cannot be implemented in this manner.
*
* @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
* @param sharedMem [IN/OUT] Indicates the pointer to the shared memory.
* The pointed shared memory cannot be null and the size cannot be 0.
*
* @return Returns {@code TEEC_SUCCESS} if the operation is successful.
* Returns {@code TEEC_ERROR_BAD_PARAMETERS} if context or sharedMem is null or
* the pointed memory is empty.
*
* @since 20
*/
TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, TEEC_SharedMemory *sharedMem);
/**
* @brief Requests shared memory in the specified TEE context.
*
* The shared memory can be used to implement zero-copy during data transmission between the REE and TEE.
* The zero-copy function, however, also requires support by the operating system.
* At present, zero-copy cannot be implemented in this manner.
*
* @attention If the size field of the input parameter sharedMem is set to 0, TEEC_SUCCESS
* will be returned but the shared memory cannot be used because this memory has neither an address nor size.
* @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
* @param sharedMem [IN/OUT] Indicates the pointer to the shared memory. The size of the shared memory cannot be 0.
*
* @return Returns {@code TEEC_SUCCESS} if the operation is successful.
* Returns {@code TEEC_ERROR_BAD_PARAMETERS} if context or sharedMem is null.
* Returns {@code TEEC_ERROR_OUT_OF_MEMORY} if the available system resources are insufficient.
*
* @since 20
*/
TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, TEEC_SharedMemory *sharedMem);
/**
* @brief Releases the shared memory registered or acquired.
*
* @attention If the shared memory is acquired by using {@code TEEC_AllocateSharedMemory},
* the memory released will be reclaimed. If the shared memory is acquired by using {@code TEEC_RegisterSharedMemory},
* the local memory released will not be reclaimed.
* @param sharedMem [IN/OUT] Indicates the pointer to the shared memory to release.
*
* @since 20
*/
void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem);
/**
* @brief Cancels an operation.
*
* @attention This operation is only used to send a cancel message. Whether to perform the cancel operation is
* determined by the TEE or TA.
* At present, the cancel operation does not take effect.
* @param operation [IN/OUT] Indicates the pointer to the data to be sent from the CA to the TA.
*
* @since 20
*/
void TEEC_RequestCancellation(TEEC_Operation *operation);
#ifdef __cplusplus
}
#endif
#endif
/** @} */