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