1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 
20 /******************************************************************************
21  * @file     drv/tee.h
22  * @brief    Header File for TEE Driver
23  * @version  V1.0
24  * @date     12 Sep 2020
25  * @model    tee
26  ******************************************************************************/
27 #ifndef _DRV_TEE_H_
28 #define _DRV_TEE_H_
29 
30 #include <stdint.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 /****** TEE AES mode *****/
36 typedef enum {
37     TEE_AES_MODE_ECB = 0,    ///< TEE AES ECB mode
38     TEE_AES_MODE_CBC = 1,    ///< TEE AES CBC mode
39     TEE_AES_MODE_MAX,        ///< invaild mode
40 }
41 tee_aes_mode_e;
42 
43 /**
44   \brief       TEE AES encrypt
45   \note        Length should be a multiple of the block size (16 bytes)
46                After calling this function, the content of iv is updated.
47   \param[in]   in      Pointer to plaintext buffer
48   \param[in]   in_len  Plaintext buffer length
49   \param[in]   key     Pointer to secret key
50   \param[in]   key_len Secret key size,must be 16 bytes for AES128,24 bytes for AES192 or 32byes for AES256
51   \param[out]  out     Pointer to ciphertext buffer
52   \param[in]   mode    \ref tee_aes_mode_e
53   \return      Return  0 if successful,otherwise error code
54 */
55 int32_t csi_tee_aes_encrypt(const uint8_t *in, uint32_t in_len,
56                             const uint8_t *key, uint32_t key_len,
57                             uint8_t iv[16],
58                             uint8_t *out,
59                             tee_aes_mode_e mode);
60 
61 /**
62   \brief       TEE AES decrypt
63   \note        Length should be a multiple of the block size (16 bytes)
64                After calling this function, the content of iv is updated.
65   \param[in]   in      Pointer to ciphertext buffer
66   \param[in]   in_len  Ciphertext buffer length
67   \param[in]   key     Pointer to secret key
68   \param[in]   key_len Secret key size,must be 16 bytes for AES128,24 bytes for AES192 or 32byes for AES256
69   \param[out]  out     Pointer to plaintext buffer
70   \param[in]   mode    \ref tee_aes_mode_e
71   \return      Return  0 if successful,otherwise error code
72 */
73 int32_t csi_tee_aes_decrypt(const uint8_t *in, uint32_t in_len,
74                             const uint8_t *key, uint32_t key_len,
75                             uint8_t iv[16],
76                             uint8_t *out,
77                             uint32_t mode);
78 
79 /**
80   \brief       TEE AES ECB encrypt
81   \note        Length should be a multiple of the block size (16 bytes)
82                After calling this function, the content of iv is updated.
83   \param[in]   in      Pointer to plaintext buffer
84   \param[in]   in_len  Plaintext buffer length
85   \param[in]   key     Pointer to secret key
86   \param[in]   key_len Secret key size,must be 16 bytes for AES128,24 bytes for AES192 or 32byes for AES256
87   \param[out]  out     Pointer to ciphertext buffer
88   \return      Return  0 if successful,otherwise error code
89 */
90 #define csi_tee_aes_encrypt_ecb(in, in_len, key, key_len, out) \
91     csi_tee_aes_encrypt(in, in_len, key, key_len, NULL, out, TEE_AES_MODE_ECB)
92 
93 /**
94   \brief       TEE AES ECB decrypt
95   \note        Length should be a multiple of the block size (16 bytes)
96                After calling this function, the content of iv is updated.
97   \param[in]   in      Pointer to ciphertext buffer
98   \param[in]   in_len  Ciphertext buffer length
99   \param[in]   key     Pointer to secret key
100   \param[in]   key_len Secret key size,must be 16 bytes for AES128,24 bytes for AES192 or 32byes for AES256
101   \param[out]  out     Pointer to plaintext buffer
102   \return      Return  0 if successful,otherwise error code
103 */
104 #define csi_tee_aes_decrypt_ecb(in, in_len, key, key_len, out) \
105     csi_tee_aes_decrypt(in, in_len, key, key_len, NULL, out, TEE_AES_MODE_ECB)
106 
107 /**
108   \brief       TEE AES CBC encrypt
109   \note        Length should be a multiple of the block size (16 bytes)
110                After calling this function, the content of iv is updated.
111   \param[in]   in      Pointer to ciphertext buffer
112   \param[in]   in_len  Ciphertext buffer length
113   \param[in]   key     Pointer to secret key
114   \param[in]   key_len Secret key size,must be 16 bytes for AES128,24 bytes for AES192 or 32byes for AES256
115   \param[out]  out     Pointer to plaintext buffer
116   \return      Return  0 if successful,otherwise error code
117 */
118 #define csi_tee_aes_encrypt_cbc(in, in_len, key, key_len, iv, out) \
119     csi_tee_aes_encrypt(in, in_len, key, key_len, iv, out, TEE_AES_MODE_CBC)
120 
121 /**
122   \brief       TEE AES CBC decrypt
123   \note        Length should be a multiple of the block size (16 bytes)
124                After calling this function, the content of iv is updated.
125   \param[in]   in      Pointer to ciphertext buffer
126   \param[in]   in_len  Ciphertext buffer length
127   \param[in]   key     Pointer to secret key
128   \param[in]   key_len Secret key size,must be 16 bytes for AES128,24 bytes for AES192 or 32byes for AES256
129   \param[out]  out     Pointer to plaintext buffer
130   \return      Return  0 if successful,otherwise error code
131 */
132 #define csi_tee_aes_decrypt_cbc(in, in_len, key, key_len, iv, out) \
133     csi_tee_aes_decrypt(in, in_len, key, key_len, iv, out, TEE_AES_MODE_CBC)
134 
135 /**
136   \brief       TEE BASE64 encode/decode
137   \param[in]   in        Pointer to input data buffer
138   \param[in]   in_len    Input data buffer length
139   \param[out]  out       Pointer to output data buffer
140   \param[out]  out_len   Output data buffer length
141   \param[in]   is_encode 1 encode 0 decode
142   \param[in]   wsafe     Base64 websafe feature,set 1, replace "+/" with "-_"
143   \return      Return 0 if successful,otherwise error code
144 */
145 int32_t csi_tee_base64(const uint8_t *in, uint32_t in_len,
146                        uint8_t *out, uint32_t *out_len,
147                        uint32_t is_encode,
148                        uint32_t wsafe);
149 
150 /**
151   \brief       TEE BASE64 encode
152   \param[in]   in        Pointer to input data buffer
153   \param[in]   in_len    Input data buffer length
154   \param[out]  out       Pointer to output data buffer
155   \param[out]  out_len   Output data buffer length
156   \return      Return 0 if successful,otherwise error code
157 */
158 #define csi_tee_base64_encode(in,in_len,out,out_len) \
159     csi_tee_base64(in,in_len,out,out_len,1,0)
160 
161 /**
162   \brief       TEE BASE64 decode
163   \param[in]   in        Pointer to input data buffer
164   \param[in]   in_len    Input data buffer length
165   \param[out]  out       Pointer to output data buffer
166   \param[out]  out_len   Output data buffer length
167   \return      Return 0 if successful,otherwise error code
168 */
169 #define csi_tee_base64_decode(in,in_len,out,out_len) \
170     csi_tee_base64(in,in_len,out,out_len,0,0)
171 
172 /**
173   \brief       TEE BASE64 web safe encode
174   \param[in]   in        Pointer to input data buffer
175   \param[in]   in_len    Input data buffer length
176   \param[out]  out       Pointer to output data buffer
177   \param[out]  out_len   Output data buffer length
178   \return      Return 0 if successful,otherwise error code
179 */
180 #define csi_tee_base64_websafe_encode(in,in_len,out,out_len) \
181     csi_tee_base64(in,in_len,out,out_len,1,1)
182 
183 /**
184   \brief       TEE BASE64 web safe decode
185   \param[in]   in        Pointer to input data buffer
186   \param[in]   in_len    Input data buffer length
187   \param[out]  out       Pointer to output data buffer
188   \param[out]  out_len   Output data buffer length
189   \return      Return 0 if successful,otherwise error code
190 */
191 #define csi_tee_base64_websafe_decode(in,in_len,out,out_len) \
192     csi_tee_base64(in,in_len,out,out_len,0,1)
193 
194 /**
195   \brief       TEE obtain CID from Key Provisioning
196   \param[out]  out       Pointer to cid buffer
197   \param[out]  out_len   CID buffer length,if cid obtain successfully,
198                          out_len is updated to actual cid sizes
199   \return      Return 0 if successful,otherwise error code
200 */
201 int32_t csi_tee_get_cid(uint8_t *out, uint32_t *out_len);
202 
203 /****** lpm mode *****/
204 typedef enum {
205     TEE_LPM_MODE_WAIT = 0,   ///< lpm wait
206     TEE_LPM_MODE_DOZE = 1,   ///< lpm doze
207     TEE_LPM_MODE_STOP = 2,   ///< lpm stop
208     TEE_LPM_MODE_STANDBY = 3, ///< lpm standby
209     TEE_LPM_MODE_CLOCK = 4,  ///< lpm clock gate
210     TEE_LPM_MODE_MAX,
211 } tee_lpm_mode_e;
212 
213 /**
214   \brief       TEE set low power mode
215   \param[in]   gate  Not use for now
216   \param[in]   irqid Not use for now
217   \param[in]   mode  \ref tee_lpm_mode_e
218   \return      Return 0 if successful,otherwise error code
219 */
220 int32_t csi_tee_enter_lpm(uint32_t gate, uint32_t irqid, tee_lpm_mode_e mode);
221 
222 /**
223   \brief       TEE obtain manifest info from manifest table
224   \note        call csi_tee_get_sys_img_info, csi_tee_get_sys_os_version or csi_tee_get_sys_partition is better
225   \param[out]  out     Pointer to info buffer
226   \param[out]  out_len Info buffer length,if info obtain successfully,
227                        out_len is updated to actual sizes
228   \param[in]   name    info name
229   \return      Return 0 if successful,otherwise error code
230 */
231 int32_t csi_tee_get_manifest_info(uint8_t *out, uint32_t *out_len, char *name);
232 
233 /**
234   \brief       TEE obtain image buffer from manifest table
235   \param[out]  out      Pointer to image buffer
236   \param[out]  out_len  Image buffer length,if info obtain successfully,
237                         out_len is updated to actual image buffer sizes
238   \param[in]   img_name Image name
239   \return      Return 0 if successful,otherwise error code
240 */
241 #define csi_tee_get_sys_img_info(out,out_len,img_name) \
242     csi_tee_get_manifest_info(out,out_len,img_name)
243 
244 /**
245   \brief       TEE obtain os version from manifest table
246   \param[out]  out     Pointer to os version buffer
247   \param[out]  out_len OS version buffer length,if info obtain successfully,
248                        out_len is updated to actual os version buffer sizes
249   \return      Return 0 if successful,otherwise error code
250 */
251 #define csi_tee_get_sys_os_version(out,out_len) \
252     csi_tee_get_manifest_info(out,out_len,"os_v")
253 
254 /**
255   \brief       TEE obtain partition buffer from manifest table
256   \param[out]  out     Pointer to partition buffer
257   \param[out]  out_len Partition buffer length,if info obtain successfully,
258                        out_len is updated to actual partition buffer sizes
259   \return      Return 0 if successful,otherwise error code
260 */
261 #define csi_tee_get_sys_partition(out,out_len) \
262     csi_tee_get_manifest_info(out,out_len,"sys_p")
263 
264 /**
265   \brief       TEE set random seed
266   \param[in]   Seed random sedd
267   \return      Return 0 if successful,otherwise error code
268 */
269 int32_t csi_tee_rand_seed(uint32_t seed);
270 
271 /**
272   \brief       TEE ramdom date generation
273   \param[out]  out     Pointer to random data buffer
274   \param[in]   out_len Data buffer length
275   \return      Return 0 if successful,otherwise error code
276 */
277 int32_t csi_tee_rand_generate(uint8_t *out, uint32_t out_len);
278 
279 /****** TEE RSA sign type *****/
280 typedef enum {
281     TEE_RSA_MD5    = 0,     ///< MD5
282     TEE_RSA_SHA1   = 1,     ///< SHA1
283     TEE_RSA_SHA256 = 3,     ///< SHA256
284     TEE_RSA_SIGN_TYPE_MAX,  ///< invailed type
285 } tee_rsa_sign_type_e;
286 
287 /**
288   \brief       TEE RSA sign with private key
289   \param[in]   in       Pointer to digest buffer
290   \param[in]   in_len   Digest buffer length
291   \param[in]   key      Pointer to private key,key contains n, e, d
292   \param[in]   key_len  Private key size,must be 128*3 = 384 bytes for RSA1024, 256*3 = 768 bytes for RSA2048
293   \param[out]  sign     Pointer to sign buffer
294   \param[out]  sign_len Sign buffer length
295   \param[in]   type     \ref tee_rsa_sign_type_e
296   \return      Return  0 if successful,otherwise error code
297 */
298 int32_t csi_tee_rsa_sign(const uint8_t *in, uint32_t in_len,
299                          const uint8_t *key, uint32_t key_len,
300                          uint8_t *sign, uint32_t *sign_len,
301                          tee_rsa_sign_type_e type);
302 
303 /**
304   \brief       TEE RSA verify with public key
305   \param[in]   in       Pointer to digest buffer
306   \param[in]   in_len   Digest buffer length
307   \param[in]   key      Pointer to public key,key contains n, e
308   \param[in]   key_len  Public key size,must be 128*2 = 256 bytes for RSA1024, 256*2 = 512 bytes for RSA2048
309   \param[in]   sign     Pointer to sign buffer
310   \param[in]   sign_len Sign buffer length
311   \param[in]   type     \ref tee_rsa_sign_type_e
312   \return      Return  0 if verify successful,otherwise error code
313 */
314 int32_t csi_tee_rsa_verify(const uint8_t *in, uint32_t in_len,
315                            const uint8_t *key, uint32_t key_len,
316                            uint8_t *sign, uint32_t sign_len,
317                            tee_rsa_sign_type_e type);
318 
319 /****** TEE RSA padding mode *****/
320 typedef enum {
321     TEE_RSA_PKCS1_PADDING = 0x01,     ///< RSA PKCS padding mode
322     TEE_RSA_NO_PADDING    = 0x02,     ///< RSA no padding mode
323 } tee_rsa_padding_mode_e;
324 
325 /**
326   \brief       TEE RSA encrypt with public key
327   \param[in]   in       Pointer to plaintext buffer
328   \param[in]   in_len   Plaintext buffer length
329   \param[in]   key      Pointer to public key,key contains n, e
330   \param[in]   key_len  Public key size, must be 128*2 = 256 bytes for RSA1024, 256*2 = 512 bytes for RSA2048
331   \param[in]   out      Pointer to ciphertext buffer
332   \param[in]   out_len  Ciphertext buffer length
333   \param[in]   padding  \ref tee_rsa_padding_mode_e
334   \return      Return  0 if successful,otherwise error code
335 */
336 int32_t csi_tee_rsa_encrypt(const uint8_t *in, uint32_t in_len,
337                             const uint8_t *key, uint32_t key_len,
338                             uint8_t *out, uint32_t *out_len,
339                             tee_rsa_padding_mode_e padding);
340 /**
341   \brief       TEE RSA decrypt with private key
342   \param[in]   in       Pointer to ciphertext buffer
343   \param[in]   in_len   Ciphertext buffer length
344   \param[in]   key      Pointer to private key,key contains n, e, d
345   \param[in]   key_len  Private key size,must be 128*3 = 384 bytes for RSA1024, 256*3 = 768 bytes for RSA2048
346   \param[in]   out      Pointer to plaintext buffer
347   \param[in]   out_len  Plaintext buffer length
348   \param[in]   padding  \ref tee_rsa_padding_mode_e
349   \return      Return  0 if successful,otherwise error code
350 */
351 int32_t csi_tee_rsa_decrypt(const uint8_t *in, uint32_t in_len,
352                             const uint8_t *key, uint32_t key_len,
353                             uint8_t *out, uint32_t *out_len,
354                             tee_rsa_padding_mode_e padding);
355 
356 /**
357   \brief       TEE RSA sign with internal private key
358   \note        Only use if key provisioning exist
359   \param[in]   in       Pointer to digest buffer
360   \param[in]   in_len   Digest buffer length
361   \param[out]  sign     Pointer to sign buffer
362   \param[out]  sign_len Sign buffer length
363   \param[in]   type     \ref tee_rsa_sign_type_e
364   \return      Return  0 if successful,otherwise error code
365 */
366 #define csi_tee_cid_rsa_sign(in,in_len,sign,sign_len,type) \
367     csi_tee_rsa_sign(in,in_len,NULL,0,sign,sign_len,type)
368 
369 /**
370   \brief       TEE RSA verify with internal public key
371   \note        Only use if key provisioning exist
372   \param[in]   in       Pointer to digest buffer
373   \param[in]   in_len   Digest buffer length
374   \param[in]   sign     Pointer to sign buffer
375   \param[in]   sign_len Sign buffer length
376   \param[in]   type     \ref tee_rsa_sign_type_e
377   \return      Return  0 if verify successful,otherwise error code
378 */
379 #define csi_tee_cid_rsa_verify(in,in_len,sign,sign_len,type) \
380     csi_tee_rsa_verify(in,in_len,NULL,0,sign,sign_len,type)
381 
382 /**
383   \brief       TEE RSA encrypt with internal public key
384   \note        Only use if key provisioning exist
385   \param[in]   in       Pointer to plaintext buffer
386   \param[in]   in_len   Plaintext buffer length
387   \param[in]   out      Pointer to ciphertext buffer
388   \param[in]   out_len  Ciphertext buffer length
389   \param[in]   padding  \ref tee_rsa_padding_mode_e
390   \return      Return  0 if successful,otherwise error code
391 */
392 #define csi_tee_cid_rsa_encrypt(in,in_len,out,out_len,padding) \
393     csi_tee_rsa_encrypt(in,in_len,NULL,0,out,out_len,padding)
394 
395 /**
396   \brief       TEE RSA decrypt with internal private key
397   \note        Only use if key provisioning exist
398   \param[in]   in       Pointer to ciphertext buffer
399   \param[in]   in_len   Ciphertext buffer length
400   \param[in]   key      Pointer to private key,key contains n, e, d
401   \param[in]   key_len  Private key size,must be 128*3 = 384 bytes for RSA1024, 256*3 = 768 bytes for RSA2048
402   \param[in]   out      Pointer to plaintext buffer
403   \param[in]   out_len  Plaintext buffer length
404   \param[in]   padding  \ref tee_rsa_padding_mode_e
405   \return      Return  0 if successful,otherwise error code
406 */
407 #define csi_tee_cid_rsa_decrypt(in,in_len,out,out_len,padding) \
408     csi_tee_rsa_decrypt(in,in_len,NULL,0,out,out_len,padding)
409 
410 /**
411   \brief       verify boot image with boot public key
412   \note        Only use if key provisioning exist
413   \param[in]   in       Pointer to digest buffer
414   \param[in]   in_len   Digest buffer length
415   \param[in]   sign     Pointer to sign buffer
416   \param[in]   sign_len Sign buffer length
417   \param[in]   type     \ref tee_rsa_sign_type_e
418   \return      Return  0 if verify successful,otherwise error code
419 */
420 int32_t csi_tee_img_rsa_verify(const uint8_t *in, uint32_t in_len,
421                                uint8_t *sign, uint32_t sign_len,
422                                tee_rsa_sign_type_e type);
423 
424 /****** TEE HASH operation mode *****/
425 typedef enum {
426     TEE_HASH_OP_NONE = 0,     ///< No operation
427     TEE_HASH_OP_START = 1,    ///< HASH init
428     TEE_HASH_OP_UPDATA = 2,   ///< HASH update
429     TEE_HASH_OP_FINISH = 3,   ///< HASH finish
430     TEE_HASH_OP_MAX,          ///< invailed operation
431 } tee_hash_op_e;
432 
433 /****** TEE HMAC type *****/
434 typedef enum {
435     TEE_HMAC_SHA1 = 1,    ///< HMAC with SHA1
436 } tee_hmac_type_e;
437 
438 /**
439   \brief       TEE HAMC
440   \note        Call csi_tee_hmac_digest is better
441                out buffer size must be large enough according to type, eg. 20 bytes for TEE_HMAC_SHA1
442   \param[in]   in       Pointer to input data buffer
443   \param[in]   in_len   Input data buffer length
444   \param[in]   key      Pointer to key buffer
445   \param[in]   key_len  Key buffer size
446   \param[out]  out      Pointer to output date buffer
447   \param[in]   type     \ref tee_hmac_type_e
448   \param[in]   hash_op  \ref tee_hash_op_e
449   \param[in]   ctx      Pointer to context of hmac
450   \return      Return  0 if successful,otherwise error code
451 */
452 int32_t csi_tee_hmac(const uint8_t *in, uint32_t  in_len,
453                      const uint8_t *key, uint32_t key_len,
454                      uint8_t *out,
455                      tee_hmac_type_e type,
456                      tee_hash_op_e hash_op,
457                      uint32_t *ctx);
458 
459 /**
460   \brief       TEE HAMC digest
461   \note        out buffer size must be large enough according to type, eg. 20 bytes for TEE_HMAC_SHA1
462   \param[in]   in       Pointer to input data buffer
463   \param[in]   in_len   Input data buffer length
464   \param[in]   key      Pointer to key buffer
465   \param[in]   key_len  Key buffer size
466   \param[out]  out      Pointer to output date buffer
467   \param[in]   type     \ref tee_hmac_type_e
468   \return      Return  0 if successful,otherwise error code
469 */
470 #define csi_tee_hmac_digest(in,in_len,key,key_len,out,type) \
471     csi_tee_hmac(in,in_len,key,key_len,out,type,TEE_HASH_OP_NONE,NULL)
472 
473 /****** TEE SHA type *****/
474 typedef enum {
475     TEE_SHA1 = 0,   ///< SHA1
476     TEE_SHA256 = 1, ///< SHA256
477     TEE_SHA224 = 2, ///< SHA224
478     TEE_SHA384 = 3, ///< SHA384
479     TEE_SHA512 = 4, ///< SHA512
480     TEE_SHA_MAX,    ///< invaild sha type
481 } tee_sha_type_t;
482 
483 /**
484   \brief       TEE SHA
485   \note        Call csi_tee_sha_digest, csi_tee_sha_start, csi_tee_sha_update or csi_tee_sha_finish is better
486                out buffer size must be large enough according to type, eg. 20 bytes for TEE_SHA1, 32 bytes for TEE_SHA256
487   \param[in]   in       Pointer to input data buffer
488   \param[in]   in_len   Input data buffer length
489   \param[out]  out      Pointer to output date buffer
490   \param[in]   type     \ref tee_sha_type_t
491   \param[in]   hash_op  \ref tee_hash_op_e
492   \param[in]   ctx      Pointer to context of sha
493   \return      Return  0 if successful,otherwise error code
494 */
495 int32_t csi_tee_sha(const uint8_t *in, uint32_t in_len,
496                     uint8_t *out,
497                     tee_sha_type_t type,
498                     tee_hash_op_e hash_op,
499                     void *ctx);
500 
501 /**
502   \brief       TEE SHA digest
503   \note        out buffer size must be large enough according to type, eg. 20 bytes for TEE_SHA1, 32 bytes for TEE_SHA256
504   \param[in]   in       Pointer to input data buffer
505   \param[in]   in_len   Input data buffer length
506   \param[out]  out      Pointer to output date buffer
507   \param[in]   type     \ref tee_sha_type_t
508   \return      Return  0 if successful,otherwise error code
509 */
510 #define csi_tee_sha_digest(in,in_len,out,type) \
511     csi_tee_sha(in,in_len,out,type,TEE_HASH_OP_NONE,NULL);
512 
513 /**
514   \brief       TEE SHA start, initial sha
515   \param[in]   type     \ref tee_sha_type_t
516   \param[in]   ctx      Pointer to context of sha
517   \return      Return  0 if successful,otherwise error code
518 */
519 #define csi_tee_sha_start(type,ctx) \
520     csi_tee_sha(NULL,0,NULL,type,TEE_HASH_OP_START,ctx);
521 
522 /**
523   \brief       TEE SHA update, update data
524   \param[in]   in       Pointer to input data buffer
525   \param[in]   in_len   Input data buffer length
526   \param[in]   ctx      Pointer to context of sha
527   \return      Return  0 if successful,otherwise error code
528 */
529 #define csi_tee_sha_update(in,in_len,ctx) \
530     csi_tee_sha(in,in_len,NULL,0,TEE_HASH_OP_UPDATA,ctx);
531 
532 /**
533   \brief       TEE SHA digest, get sha digest
534   \note        out buffer size must be large enough according to type, eg. 20 bytes for TEE_SHA1, 32 bytes for TEE_SHA256
535   \param[out]  out      Pointer to output date buffer
536   \param[in]   ctx      Pointer to context of sha
537   \return      Return  0 if successful,otherwise error code
538 */
539 #define csi_tee_sha_finish(out,ctx) \
540     csi_tee_sha(NULL,0,out,0,TEE_HASH_OP_FINISH,ctx);
541 
542 /**
543   \brief       TEE get device name and product key
544   \param[in]   name_encrypted             Pointer to device name ciphertext
545   \param[in]   name_encrypted_len         device name ciphertext length
546   \param[in]   product_key_encrypted      Pointer to device product key ciphertext
547   \param[in]   product_key_encrypted_len  Device product key ciphertext length
548   \param[out]  name                       Pointer to device name
549   \param[out]  name_len                   Device name length
550   \param[out]  product_key                Pointer to device product key
551   \param[out]  product_key_len            Device product key length
552   \return      Return  0 if successful,otherwise error code
553 */
554 int32_t csi_tee_dev_info_get(const uint8_t *name_encrypted, uint32_t name_encrypted_len,
555                              const uint8_t *product_key_encrypted, uint32_t product_key_encrypted_len,
556                              const uint8_t *name, uint32_t *name_len,
557                              const uint8_t *product_key, uint32_t *product_key_len);
558 
559 /**
560   \brief       TEE device info sign
561   \param[in]   in                 Pointer to input date buffer
562   \param[in]   in_len             Input data buffer length
563   \param[in]   device_secret      Pointer to device secret ciphertext
564   \param[in]   device_secret_len  Device secret ciphertext length
565   \param[out]  sign               Pointer to signed buffer
566   \param[out]  sign_len           Signed buffer length
567   \return      Return  0 if successful,otherwise error code
568 */
569 int32_t csi_tee_dev_info_sign(const uint8_t *in, uint32_t in_len,
570                               const uint8_t *device_secret, uint32_t device_secret_len,
571                               const uint8_t *sign, uint32_t *sign_len);
572 
573 /**
574   \brief       TEE device info encrypt/decrypt
575   \param[in]   in                 Pointer to input date buffer
576   \param[in]   in_len             Input data buffer length
577   \param[in]   out                Pointer to output date buffer
578   \param[in]   out_len            Onput data buffer length
579   \param[in]   is_enc             1 incrypt 0 decrypt
580   \return      Return  0 if successful,otherwise error code
581 */
582 int32_t csi_tee_dev_info_crypt(const uint8_t *in, uint32_t in_len,
583                                uint8_t *out, uint32_t *out_len,
584                                uint8_t is_enc);
585 
586 /**
587   \brief       TEE device info encrypt
588   \param[in]   in                 Pointer to input date buffer
589   \param[in]   in_len             Input data buffer length
590   \param[in]   out                Pointer to output date buffer
591   \param[in]   out_len            Onput data buffer length
592   \return      Return  0 if successful,otherwise error code
593 */
594 #define csi_tee_dev_info_encrypt(in, in_len, out, out_len) \
595     csi_tee_dev_info_crypt(in, in_len, out, out_len, 1)
596 
597 /**
598   \brief       TEE device info decrypt
599   \param[in]   in                 Pointer to input date buffer
600   \param[in]   in_len             Input data buffer length
601   \param[in]   out                Pointer to output date buffer
602   \param[in]   out_len            Onput data buffer length
603   \return      Return  0 if successful,otherwise error code
604 */
605 #define csi_tee_dev_info_decrypt(in, in_len, out, out_len) \
606     csi_tee_dev_info_crypt(in, in_len, out, out_len, 0)
607 
608 /**
609   \brief       Set system frequence
610   \param[in]   clk_src      Indicate clock source type
611   \param[in]   clk_val      System freqence to be set
612   \return      Return  0 if successful,otherwise error code
613 */
614 int32_t csi_tee_set_sys_freq(uint32_t clk_src, uint32_t clk_val);
615 
616 /**
617   \brief       Get system frequence
618   \param[in]   clk_val      Value address to store system freqence
619   \return      Return  0 if successful,otherwise error code
620 */
621 int32_t csi_tee_get_sys_freq(uint32_t *clk_val);
622 
623 /**
624   \brief        Read system register
625   \param[in]    addr        Indicate register address
626   \param[out]   val         Value to read from the address
627   \return       Return  0 if successful,otherwise error code
628 */
629 int32_t csi_tee_read_reg(uint32_t addr, uint32_t *val);
630 
631 /**
632   \brief        Wrte system register
633   \param[in]    addr        Indicate register address
634   \param[in]    val         Value to be written into the address
635   \return      Return  0 if successful,otherwise error code
636 */
637 int32_t csi_tee_write_reg(uint32_t addr, uint32_t val);
638 
639 #ifdef __cplusplus
640 }
641 #endif
642 
643 #endif /* _DRV_TEE_H_ */
644