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