• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _TEEK_NS_CLIENT_H_
33 #define _TEEK_NS_CLIENT_H_
34 
35 #include <linux/list.h>
36 #include <linux/slab.h>
37 #include <securec.h>
38 #include "tc_ns_client.h"
39 #include "tc_ns_log.h"
40 #include "tzdriver_compat.h"
41 
42 #define TC_NS_CLIENT_IOC_MAGIC  't'
43 #define TC_NS_CLIENT_DEV        "tc_ns_client"
44 #define TC_NS_CLIENT_DEV_NAME   "/dev/tc_ns_client"
45 
46 #ifdef CONFIG_SECURE_EXTENSION
47 #define TC_ASYNC_NOTIFY_SUPPORT
48 #endif
49 
50 #define EXCEPTION_MEM_SIZE (8*1024) /* mem for exception handling */
51 #define TSP_REQUEST        0xB2000008
52 #define TSP_RESPONSE       0xB2000009
53 #define TSP_REE_SIQ        0xB200000A
54 #define TSP_CRASH          0xB200000B
55 #define TSP_PREEMPTED      0xB2000005
56 #define TC_CALL_GLOBAL     0x01
57 #define TC_CALL_SYNC       0x02
58 #define TC_CALL_LOGIN            0x04
59 #define TEE_REQ_FROM_USER_MODE   0x0
60 #define TEE_REQ_FROM_KERNEL_MODE 0x1
61 
62 /* Max sizes for login info buffer comming from teecd */
63 #define MAX_PACKAGE_NAME_LEN 255
64 /* The application certificate format is as follows:
65   * modulus_size(4 bytes) + modulus buffer(512 bytes)
66   * + exponent size(4 bytes) + exponent buffer(1 bytes)
67   */
68 #define MAX_PUBKEY_LEN 1024
69 
70 struct TagTcNsSharedMem;
71 struct TagTcNsService;
72 
73 struct TcNsDevList {
74     mutex_t devLock; /* for devFileList */
75     struct list_head devFileList;
76 };
77 
78 #define SERVICES_MAX_COUNT 32 /* service limit can opened on 1 fd */
79 typedef struct TagTcNsDevFile {
80     unsigned int devFileId;
81     mutex_t serviceLock; /* for serviceRef[], services[] */
82     uint8_t serviceRef[SERVICES_MAX_COUNT]; /* a judge if set services[i]=NULL */
83     struct TagTcNsService *services[SERVICES_MAX_COUNT];
84     mutex_t sharedMemLock; /* for sharedMemList */
85     struct list_head sharedMemList;
86     struct list_head head;
87     /* Device is linked to call from kernel */
88     uint8_t kernelApi;
89     /* client login info provided by teecd, can be either package name and public
90      * key or uid(for non services/daemons)
91      * login information can only be set once, dont' allow subsequent calls
92      */
93     bool loginSetup;
94     mutex_t LoginSetupLock; /* for loginSetup */
95     uint32_t pkgNameLen;
96     uint8_t PkgName[MAX_PACKAGE_NAME_LEN];
97     uint32_t pubKeyLen;
98     uint8_t pubKey[MAX_PUBKEY_LEN];
99     int loadAppFlag;
100 } TcNsDevFile;
101 
102 typedef union {
103     struct {
104         unsigned int buffer;
105         unsigned int size;
106     } memref;
107     struct {
108         unsigned int a;
109         unsigned int b;
110     } value;
111 } TcNsParameter;
112 
113 typedef struct TagTcNsLogin {
114     unsigned int method;
115     unsigned int mdata;
116 } tc_ns_login;
117 
118 typedef struct TagTcNsOperation {
119     unsigned int paramTypes;
120     TcNsParameter params[TEE_PARAM_NUM];
121     unsigned int    bufferHaddr[TEE_PARAM_NUM];
122     struct TagTcNsSharedMem *sharemem[TEE_PARAM_NUM];
123     void *mbBuffer[TEE_PARAM_NUM];
124 } TcNsOperation;
125 
126 typedef struct TagTcNsTempBuf {
127     void *tempBuffer;
128     unsigned int size;
129 } TcNsTempBuf;
130 
131 typedef struct  TagTcNsSmcCmd {
132     uint8_t      uuid[sizeof(TeecUuid)];
133     bool         globalCmd; /* mark it's a gtask cmd */
134     unsigned int cmdId;
135     unsigned int devFileId;
136     unsigned int contextId;
137     unsigned int agentId;
138     unsigned int operationPhys;
139     unsigned int operationHphys;
140     unsigned int loginMethod;
141     unsigned int loginDataPhy;
142     unsigned int loginDataHaddr;
143     unsigned int loginDataLen;
144     unsigned int errOrigin;
145     int          retVal;
146     unsigned int eventNr;
147     unsigned int uid;
148 #ifdef CONFIG_TEE_SMP
149     unsigned int caPid;
150 #endif
151 #ifdef SECURITY_AUTH_ENHANCE
152     unsigned int tokenPhys;
153     unsigned int tokenHphys;
154     unsigned int pid;
155     unsigned int paramsPhys;
156     unsigned int paramsHphys;
157     unsigned int eventindex;     // tee audit event index for upload
158 #endif
159     bool started;
160 }__attribute__((__packed__))TcNsSmcCmd;
161 
162 typedef struct TagTcNsSharedMem {
163     void *kernelAddr;
164     void *userAddr;
165     void *userAddrCa; /* for ca alloc share mem */
166     unsigned int len;
167     struct list_head head;
168     atomic_t usage;
169     atomic_t offset;
170 } TcNsSharedMem;
171 
172 typedef struct TagTcNsService {
173     unsigned char uuid[UUID_LEN];
174     mutex_t SessionLock; /* for sessionList */
175     struct list_head sessionList;
176     struct list_head head;
177     mutex_t operationLock; /* for session's open/close */
178     atomic_t usage;
179 } TcNsService;
180 
181 /*
182  * @brief
183  */
184 struct TcWaitData {
185     wait_queue_head_t sendCmdWq;
186     int sendWaitFlag;
187 };
188 
189 #ifdef SECURITY_AUTH_ENHANCE
190 /* Using AES-CBC algorithm to encrypt communication between secure world and
191  * normal world.
192  */
193 #define CIPHER_KEY_BYTESIZE 32   /* AES-256 key size */
194 #define IV_BYTESIZE   16  /* AES-CBC encryption initialization vector size */
195 #define CIPHER_BLOCK_BYTESIZE 16 /* AES-CBC cipher block size */
196 #define SCRAMBLING_NUMBER 3
197 #define CHKSUM_LENGTH  (sizeof(TcNsSmcCmd) - sizeof(uint32_t))
198 
199 #define HASH_PLAINTEXT_SIZE (MAX_SHA_256_SZ + sizeof(struct EncryptionHead))
200 #define HASH_PLAINTEXT_ALIGNED_SIZE \
201     ALIGN_TZ(HASH_PLAINTEXT_SIZE, CIPHER_BLOCK_BYTESIZE)
202 
203 enum SCRAMBLING_ID {
204     SCRAMBLING_TOKEN = 0,
205     SCRAMBLING_OPERATION = 1,
206     SCRAMBLING_MAX = SCRAMBLING_NUMBER
207 };
208 
209 struct SessionCryptoInfo {
210     uint8_t key[CIPHER_KEY_BYTESIZE]; /* AES-256 key */
211     uint8_t iv[IV_BYTESIZE]; /* AES-CBC encryption initialization vector */
212 };
213 
214 struct SessionSecureInfo {
215     uint32_t challengeWord;
216     uint32_t scrambling[SCRAMBLING_NUMBER];
217     struct SessionCryptoInfo cryptoInfo;
218 };
219 
220 #define MAGIC_SIZE 16
221 #define MAGIC_STRING "Trusted-magic"
222 
223 /* One encrypted block, which is aligned with CIPHER_BLOCK_BYTESIZE bytes
224  * Head + Payload + Padding
225  */
226 struct EncryptionHead {
227     int8_t magic[MAGIC_SIZE];
228     uint32_t payloadLen;
229 };
230 
231 struct SessionSecureParams {
232     struct EncryptionHead head;
233     union {
234         struct {
235             uint32_t challengeWord;
236         } ree2tee;
237         struct {
238             uint32_t scrambling[SCRAMBLING_NUMBER];
239             struct SessionCryptoInfo cryptoInfo;
240         } tee2ree;
241     } payload;
242 };
243 #endif
244 
245 #ifdef SECURITY_AUTH_ENHANCE
246 typedef struct TagTcNsToken {
247     /* 42byte, token_32byte + timestamp_8byte + kernal_api_1byte + sync_1byte */
248     uint8_t *tokenBuffer;
249     uint32_t tokenLen;
250 } TcNsToken;
251 #endif
252 
253 #define NUM_OF_SO 1
254 #define KIND_OF_SO 2
255 typedef struct TagTcNsSession {
256     unsigned int sessionId;
257     struct list_head head;
258     struct TcWaitData waitData;
259     mutex_t taSessionLock; /* for open/close/invoke on 1 session */
260     TcNsDevFile *owner;
261 #ifdef SECURITY_AUTH_ENHANCE
262     /* Session secure enhanced information */
263     struct SessionSecureInfo secureInfo;
264     TcNsToken TcNsToken;
265     /* when SECURITY_AUTH_ENHANCE enabled, hash of the same CA and
266      * SO library will be encrypted by different session key,
267      * so put authHashBuf in TcNsSession.
268      * the first MAX_SHA_256_SZ size stores SO hash,
269      * the next HASH_PLAINTEXT_ALIGNED_SIZE stores CA hash and cryptohead,
270      * the last IV_BYTESIZE size stores aes iv
271      */
272     uint8_t authHashBuf[MAX_SHA_256_SZ * NUM_OF_SO + HASH_PLAINTEXT_ALIGNED_SIZE + IV_BYTESIZE];
273 #else
274     uint8_t authHashBuf[MAX_SHA_256_SZ * NUM_OF_SO + MAX_SHA_256_SZ];
275 #endif
276     atomic_t usage;
277 } TcNsSession;
278 
279 void GetServiceStruct(struct TagTcNsService *service);
280 void PutServiceStruct(struct TagTcNsService *service);
281 
GetSessionStruct(struct TagTcNsSession * session)282 static inline void GetSessionStruct(struct TagTcNsSession *session)
283 {
284     if (session != NULL) {
285         atomic_inc(&session->usage);
286     }
287 }
288 
289 void PutSessionStruct(struct TagTcNsSession *session);
290 
291 TcNsSession *TcFindSessionWithOwner(struct list_head *sessionList,
292     unsigned int sessionId, TcNsDevFile *dev);
293 
294 #ifdef SECURITY_AUTH_ENHANCE
295 int GenerateEncryptedSessionSecureParams(
296     const struct SessionSecureInfo *secureInfo,
297     uint8_t *encSecureParams, size_t encParamsSize);
298 #define ENCRYPT 1
299 #define DECRYPT 0
300 
301 int CryptoSessionAescbcKey256(uint8_t *in, uint32_t inLen,
302     uint8_t *out, uint32_t out_len,
303     const uint8_t *key, uint8_t *iv,
304     uint32_t mode);
305 int CryptoAescbcCmsPadding(uint8_t *plaintext, uint32_t plaintextLen,
306     uint32_t payloadLen);
307 #endif
308 
309 int TcNsClientOpen(TcNsDevFile **devFile, uint8_t kernelApi);
310 int TcNsClientClose(TcNsDevFile *dev);
311 int IsAgentAlive(unsigned int agentId);
312 int TcNsOpenSession(TcNsDevFile *devFile, TcNsClientContext *context);
313 int TcNsCloseSession(TcNsDevFile *devFile, TcNsClientContext *context);
314 int TcNsSendCmd(TcNsDevFile *devFile, TcNsClientContext *context);
315 uint32_t TcNsGetUid(void);
316 
317 #endif
318