17777dab0Sopenharmony_ci/*
27777dab0Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
37777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47777dab0Sopenharmony_ci * you may not use this file except in compliance with the License.
57777dab0Sopenharmony_ci * You may obtain a copy of the License at
67777dab0Sopenharmony_ci *
77777dab0Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
87777dab0Sopenharmony_ci *
97777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127777dab0Sopenharmony_ci * See the License for the specific language governing permissions and
137777dab0Sopenharmony_ci * limitations under the License.
147777dab0Sopenharmony_ci */
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci#ifndef TEE_CLIENT_API_H
177777dab0Sopenharmony_ci#define TEE_CLIENT_API_H
187777dab0Sopenharmony_ci/**
197777dab0Sopenharmony_ci * @addtogroup TeeClient
207777dab0Sopenharmony_ci * @{
217777dab0Sopenharmony_ci *
227777dab0Sopenharmony_ci * @brief Provides APIs for the client applications (CAs) in the Rich Execution Environment (normal mode) to
237777dab0Sopenharmony_ci * access the trusted applications (TAs) in a Trusted Execution Environment (TEE).
247777dab0Sopenharmony_ci *
257777dab0Sopenharmony_ci * @since 12
267777dab0Sopenharmony_ci * @version 1.0
277777dab0Sopenharmony_ci */
287777dab0Sopenharmony_ci
297777dab0Sopenharmony_ci/**
307777dab0Sopenharmony_ci * @file tee_client_api.h
317777dab0Sopenharmony_ci *
327777dab0Sopenharmony_ci * @brief Defines APIs for CAs to access TAs.
337777dab0Sopenharmony_ci *
347777dab0Sopenharmony_ci * <p> Example:
357777dab0Sopenharmony_ci * <p>1. Initialize a TEE: Call <b>TEEC_InitializeContext</b> to initialize the TEE.
367777dab0Sopenharmony_ci * <p>2. Open a session: Call <b>TEEC_OpenSession</b> with the Universal Unique Identifier (UUID) of the TA.
377777dab0Sopenharmony_ci * <p>3. Send a command: Call <b>TEEC_InvokeCommand</b> to send a command to the TA.
387777dab0Sopenharmony_ci * <p>4. Close the session: Call <b>TEEC_CloseSession</b> to close the session.
397777dab0Sopenharmony_ci * <p>5. Close the TEE: Call <b>TEEC_FinalizeContext</b> to close the TEE.
407777dab0Sopenharmony_ci *
417777dab0Sopenharmony_ci * @library libteec.so
427777dab0Sopenharmony_ci * @kit TEEKit
437777dab0Sopenharmony_ci * @syscap SystemCapability.Tee.TeeClient
447777dab0Sopenharmony_ci * @since 12
457777dab0Sopenharmony_ci * @version 1.0
467777dab0Sopenharmony_ci */
477777dab0Sopenharmony_ci
487777dab0Sopenharmony_ci#include <string.h>
497777dab0Sopenharmony_ci#include "tee_client_type.h"
507777dab0Sopenharmony_ci
517777dab0Sopenharmony_ci#ifdef __cplusplus
527777dab0Sopenharmony_ciextern "C" {
537777dab0Sopenharmony_ci#endif
547777dab0Sopenharmony_ci
557777dab0Sopenharmony_ci/**
567777dab0Sopenharmony_ci * @brief Defines the values of the parameters transmitted between the REE and TEE.
577777dab0Sopenharmony_ci *
587777dab0Sopenharmony_ci * @since 12
597777dab0Sopenharmony_ci * @version 1.0
607777dab0Sopenharmony_ci */
617777dab0Sopenharmony_ci#define TEEC_PARAM_TYPES(param0Type, param1Type, param2Type, param3Type) \
627777dab0Sopenharmony_ci    ((param3Type) << 12 | (param2Type) << 8 | (param1Type) << 4 | (param0Type))
637777dab0Sopenharmony_ci
647777dab0Sopenharmony_ci/**
657777dab0Sopenharmony_ci * @brief Defines the value of the parameter specified by <b>paramTypes</b> and <b>index</b>.
667777dab0Sopenharmony_ci *
677777dab0Sopenharmony_ci * @since 12
687777dab0Sopenharmony_ci * @version 1.0
697777dab0Sopenharmony_ci */
707777dab0Sopenharmony_ci#define TEEC_PARAM_TYPE_GET(paramTypes, index) \
717777dab0Sopenharmony_ci    (((paramTypes) >> (4*(index))) & 0x0F)
727777dab0Sopenharmony_ci
737777dab0Sopenharmony_ci/**
747777dab0Sopenharmony_ci * @brief Initializes a TEE.
757777dab0Sopenharmony_ci *
767777dab0Sopenharmony_ci * The TEE must be initialized before a session is open or commands are sent.
777777dab0Sopenharmony_ci * After the initialization, a connection is set up between the CA and the TEE.
787777dab0Sopenharmony_ci *
797777dab0Sopenharmony_ci * @param name [IN] Indicates the pointer to the TEE path.
807777dab0Sopenharmony_ci * @param context [IN/OUT] Indicates the context pointer, which is the handle of the TEE.
817777dab0Sopenharmony_ci *
827777dab0Sopenharmony_ci * @return Returns {@code TEEC_SUCCESS} if the TEE is successfully initialized.
837777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_BAD_PARAMETERS} if <b>name</b> is incorrect or <b>context</b> is null.
847777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_GENERIC} if the available system resources are insufficient.
857777dab0Sopenharmony_ci *
867777dab0Sopenharmony_ci * @since 12
877777dab0Sopenharmony_ci * @version 1.0
887777dab0Sopenharmony_ci */
897777dab0Sopenharmony_ciTEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context);
907777dab0Sopenharmony_ci
917777dab0Sopenharmony_ci/**
927777dab0Sopenharmony_ci * @brief Closes the TEE.
937777dab0Sopenharmony_ci *
947777dab0Sopenharmony_ci * After the TEE is closed, the CA is disconnected from the TEE.
957777dab0Sopenharmony_ci *
967777dab0Sopenharmony_ci * @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
977777dab0Sopenharmony_ci *
987777dab0Sopenharmony_ci * @since 12
997777dab0Sopenharmony_ci * @version 1.0
1007777dab0Sopenharmony_ci */
1017777dab0Sopenharmony_civoid TEEC_FinalizeContext(TEEC_Context *context);
1027777dab0Sopenharmony_ci
1037777dab0Sopenharmony_ci/**
1047777dab0Sopenharmony_ci * @brief Opens a session.
1057777dab0Sopenharmony_ci *
1067777dab0Sopenharmony_ci * This function is used to set up a connection between the CA and the TA of the specified UUID in the specified TEE
1077777dab0Sopenharmony_ci * context. The data to be transferred is contained in <b>operation</b>.
1087777dab0Sopenharmony_ci * If a session is opened successfully, <b>session</b> is returned providing a description of the connection.
1097777dab0Sopenharmony_ci * If the session fails to open, <b>returnOrigin</b> is returned indicating the cause of the failure.
1107777dab0Sopenharmony_ci *
1117777dab0Sopenharmony_ci * @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
1127777dab0Sopenharmony_ci * @param session [OUT] Indicates the pointer to the session. The value cannot be null.
1137777dab0Sopenharmony_ci * @param destination [IN] Indicates the pointer to the UUID of the target TA. Each TA has a unique UUID.
1147777dab0Sopenharmony_ci * @param connectionMethod [IN] Indicates the connection method. For details, see {@link TEEC_LoginMethod}.
1157777dab0Sopenharmony_ci * @param connectionData [IN] Indicates the pointer to the connection data, which varies with the connection mode.
1167777dab0Sopenharmony_ci * If the connection mode is {@code TEEC_LOGIN_PUBLIC}, {@code TEEC_LOGIN_USER},
1177777dab0Sopenharmony_ci * {@code TEEC_LOGIN_USER_APPLICATION}, or {@code TEEC_LOGIN_GROUP_APPLICATION}, the connection data must be null.
1187777dab0Sopenharmony_ci * If the connection mode is {@code TEEC_LOGIN_GROUP} or {@code TEEC_LOGIN_GROUP_APPLICATION},
1197777dab0Sopenharmony_ci * the connection data must point to data of the uint32_t type, which indicates the target group user to be connected
1207777dab0Sopenharmony_ci * by the CA.
1217777dab0Sopenharmony_ci * @param operation [IN/OUT] Indicates the pointer to the data to be transmitted between the CA and TA.
1227777dab0Sopenharmony_ci * @param returnOrigin [IN/OUT] Indicates the pointer to the error source.
1237777dab0Sopenharmony_ci * For details, see {@code TEEC_ReturnCodeOrigin}.
1247777dab0Sopenharmony_ci *
1257777dab0Sopenharmony_ci * @return Returns {@code TEEC_SUCCESS} if the session is open successfully.
1267777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_BAD_PARAMETERS} if <b>context</b>, <b>session</b>, or <b>destination</b> is null.
1277777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_ACCESS_DENIED} if the access request is denied.
1287777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_OUT_OF_MEMORY} if the available system resources are insufficient.
1297777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_TRUSTED_APP_LOAD_ERROR} if the TA failed to be loaded.
1307777dab0Sopenharmony_ci *         For details about other return values, see {@code TEEC_ReturnCode}.
1317777dab0Sopenharmony_ci *
1327777dab0Sopenharmony_ci * @since 12
1337777dab0Sopenharmony_ci * @version 1.0
1347777dab0Sopenharmony_ci */
1357777dab0Sopenharmony_ciTEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, const TEEC_UUID *destination,
1367777dab0Sopenharmony_ci    uint32_t connectionMethod, const void *connectionData, TEEC_Operation *operation, uint32_t *returnOrigin);
1377777dab0Sopenharmony_ci
1387777dab0Sopenharmony_ci/**
1397777dab0Sopenharmony_ci * @brief Closes a session.
1407777dab0Sopenharmony_ci *
1417777dab0Sopenharmony_ci * After the session is closed, the CA is disconnected from the TA.
1427777dab0Sopenharmony_ci *
1437777dab0Sopenharmony_ci * @param session [IN/OUT] Indicates the pointer to the session to close.
1447777dab0Sopenharmony_ci *
1457777dab0Sopenharmony_ci * @since 12
1467777dab0Sopenharmony_ci * @version 1.0
1477777dab0Sopenharmony_ci */
1487777dab0Sopenharmony_civoid TEEC_CloseSession(TEEC_Session *session);
1497777dab0Sopenharmony_ci
1507777dab0Sopenharmony_ci/**
1517777dab0Sopenharmony_ci * @brief Sends a command to a TA.
1527777dab0Sopenharmony_ci *
1537777dab0Sopenharmony_ci * The CA sends the command ID to the TA through the specified session.
1547777dab0Sopenharmony_ci *
1557777dab0Sopenharmony_ci * @param session [IN/OUT] Indicates the pointer to the session opened.
1567777dab0Sopenharmony_ci * @param commandID [IN] Indicates the command ID supported by the TA. It is defined by the TA.
1577777dab0Sopenharmony_ci * @param operation [IN/OUT] Indicates the pointer to the data to be sent from the CA to the TA.
1587777dab0Sopenharmony_ci * @param returnOrigin [IN/OUT] Indicates the pointer to the error source.
1597777dab0Sopenharmony_ci * For details, see {@code TEEC_ReturnCodeOrigin}.
1607777dab0Sopenharmony_ci *
1617777dab0Sopenharmony_ci * @return Returns {@code TEEC_SUCCESS} if the command is sent successfully.
1627777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_BAD_PARAMETERS} if <b>session</b> is null or
1637777dab0Sopenharmony_ci * <b>operation</b> is in incorrect format.
1647777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_ACCESS_DENIED} if the access request is denied.
1657777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_OUT_OF_MEMORY} if the available system resources are insufficient.
1667777dab0Sopenharmony_ci *         For details about other return values, see {@code TEEC_ReturnCode}.
1677777dab0Sopenharmony_ci *
1687777dab0Sopenharmony_ci * @since 12
1697777dab0Sopenharmony_ci * @version 1.0
1707777dab0Sopenharmony_ci */
1717777dab0Sopenharmony_ciTEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID,
1727777dab0Sopenharmony_ci    TEEC_Operation *operation, uint32_t *returnOrigin);
1737777dab0Sopenharmony_ci
1747777dab0Sopenharmony_ci/**
1757777dab0Sopenharmony_ci * @brief Registers shared memory in the specified TEE context.
1767777dab0Sopenharmony_ci *
1777777dab0Sopenharmony_ci * The registered shared memory can implement zero-copy.
1787777dab0Sopenharmony_ci * The zero-copy function, however, also requires support by the operating system.
1797777dab0Sopenharmony_ci * At present, zero-copy cannot be implemented in this manner.
1807777dab0Sopenharmony_ci *
1817777dab0Sopenharmony_ci * @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
1827777dab0Sopenharmony_ci * @param sharedMem [IN/OUT] Indicates the pointer to the shared memory.
1837777dab0Sopenharmony_ci * The pointed shared memory cannot be null and the size cannot be 0.
1847777dab0Sopenharmony_ci *
1857777dab0Sopenharmony_ci * @return Returns {@code TEEC_SUCCESS} if the operation is successful.
1867777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_BAD_PARAMETERS} if <b>context</b> or <b>sharedMem</b> is null or
1877777dab0Sopenharmony_ci * the pointed memory is empty.
1887777dab0Sopenharmony_ci *
1897777dab0Sopenharmony_ci * @since 12
1907777dab0Sopenharmony_ci * @version 1.0
1917777dab0Sopenharmony_ci */
1927777dab0Sopenharmony_ciTEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, TEEC_SharedMemory *sharedMem);
1937777dab0Sopenharmony_ci
1947777dab0Sopenharmony_ci/**
1957777dab0Sopenharmony_ci * @brief Requests shared memory in the specified TEE context.
1967777dab0Sopenharmony_ci *
1977777dab0Sopenharmony_ci * The shared memory can be used to implement zero-copy during data transmission between the REE and TEE.
1987777dab0Sopenharmony_ci * The zero-copy function, however, also requires support by the operating system.
1997777dab0Sopenharmony_ci * At present, zero-copy cannot be implemented in this manner.
2007777dab0Sopenharmony_ci *
2017777dab0Sopenharmony_ci * @attention If the <b>size</b> field of the input parameter <b>sharedMem</b> is set to <b>0</b>, <b>TEEC_SUCCESS</b>
2027777dab0Sopenharmony_ci * will be returned but the shared memory cannot be used because this memory has neither an address nor size.
2037777dab0Sopenharmony_ci * @param context [IN/OUT] Indicates the pointer to the TEE that is successfully initialized.
2047777dab0Sopenharmony_ci * @param sharedMem [IN/OUT] Indicates the pointer to the shared memory. The size of the shared memory cannot be 0.
2057777dab0Sopenharmony_ci *
2067777dab0Sopenharmony_ci * @return Returns {@code TEEC_SUCCESS} if the operation is successful.
2077777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_BAD_PARAMETERS} if <b>context</b> or <b>sharedMem</b> is null.
2087777dab0Sopenharmony_ci *         Returns {@code TEEC_ERROR_OUT_OF_MEMORY} if the available system resources are insufficient.
2097777dab0Sopenharmony_ci *
2107777dab0Sopenharmony_ci * @since 12
2117777dab0Sopenharmony_ci * @version 1.0
2127777dab0Sopenharmony_ci */
2137777dab0Sopenharmony_ciTEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, TEEC_SharedMemory *sharedMem);
2147777dab0Sopenharmony_ci
2157777dab0Sopenharmony_ci/**
2167777dab0Sopenharmony_ci * @brief Releases the shared memory registered or acquired.
2177777dab0Sopenharmony_ci *
2187777dab0Sopenharmony_ci * @attention If the shared memory is acquired by using {@code TEEC_AllocateSharedMemory},
2197777dab0Sopenharmony_ci * the memory released will be reclaimed. If the shared memory is acquired by using {@code TEEC_RegisterSharedMemory},
2207777dab0Sopenharmony_ci * the local memory released will not be reclaimed.
2217777dab0Sopenharmony_ci * @param sharedMem [IN/OUT] Indicates the pointer to the shared memory to release.
2227777dab0Sopenharmony_ci *
2237777dab0Sopenharmony_ci * @since 12
2247777dab0Sopenharmony_ci * @version 1.0
2257777dab0Sopenharmony_ci */
2267777dab0Sopenharmony_civoid TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem);
2277777dab0Sopenharmony_ci
2287777dab0Sopenharmony_ci/**
2297777dab0Sopenharmony_ci * @brief Cancels an operation.
2307777dab0Sopenharmony_ci *
2317777dab0Sopenharmony_ci * @attention This operation is only used to send a cancel message. Whether to perform the cancel operation is
2327777dab0Sopenharmony_ci * determined by the TEE or TA.
2337777dab0Sopenharmony_ci * At present, the cancel operation does not take effect.
2347777dab0Sopenharmony_ci * @param operation [IN/OUT] Indicates the pointer to the data to be sent from the CA to the TA.
2357777dab0Sopenharmony_ci *
2367777dab0Sopenharmony_ci * @since 12
2377777dab0Sopenharmony_ci * @version 1.0
2387777dab0Sopenharmony_ci */
2397777dab0Sopenharmony_civoid TEEC_RequestCancellation(TEEC_Operation *operation);
2407777dab0Sopenharmony_ci
2417777dab0Sopenharmony_ci#ifdef __cplusplus
2427777dab0Sopenharmony_ci}
2437777dab0Sopenharmony_ci#endif
2447777dab0Sopenharmony_ci/** @} */
2457777dab0Sopenharmony_ci#endif
246