• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #ifndef __TEE_DRV_CIPHER_KAPI_H__
20 #define __TEE_DRV_CIPHER_KAPI_H__
21 
22 #include "drv_cipher_define.h"
23 #ifdef HI_PLATFORM_TYPE_TEE
24 #include "hi_tee_cipher.h"
25 #include "tee_drv_cipher_compat.h"
26 #else
27 #include "hi_common_cipher.h"
28 #include "hi_cipher_compat.h"
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif    /* __cplusplus */
34 
35 /* success. */
36 #undef  HI_SUCCESS
37 #define HI_SUCCESS                      0
38 
39 /* failure. */
40 #undef  HI_FAILURE
41 #define HI_FAILURE                      (-1)
42 
43 #define crypto_max(a, b) ((a) > (b) ? (a) : (b))
44 
45 #define crypto_min(a, b) ((a) < (b) ? (a) : (b))
46 
47 #ifndef SM2_LEN_IN_WORD
48 #define SM2_LEN_IN_WORD                 8
49 #define SM2_LEN_IN_BYTE                 32
50 #endif
51 
52 /* big coding transform to little coding. */
53 #define crypto_cpu_to_be16(v) (((v) << 8) | ((v) >> 8))
54 
55 #define crypto_cpu_to_be32(v)                      \
56         ((((hi_u32)(v)) >> 24)            | \
57         ((((hi_u32)(v)) >> 8) & 0xff00)   | \
58         ((((hi_u32)(v)) << 8) & 0xff0000) | \
59         (((hi_u32)(v)) << 24))
60 
61 #define crypto_cpu_to_be64(x) \
62         ((hi_u64)( \
63         (((hi_u64)(x) & (hi_u64)0x00000000000000ffULL) << 56) |   \
64         (((hi_u64)(x) & (hi_u64)0x000000000000ff00ULL) << 40) |   \
65         (((hi_u64)(x) & (hi_u64)0x0000000000ff0000ULL) << 24) |   \
66         (((hi_u64)(x) & (hi_u64)0x00000000ff000000ULL) <<  8) |   \
67         (((hi_u64)(x) & (hi_u64)0x000000ff00000000ULL) >>  8) |   \
68         (((hi_u64)(x) & (hi_u64)0x0000ff0000000000ULL) >> 24) |   \
69         (((hi_u64)(x) & (hi_u64)0x00ff000000000000ULL) >> 40) |   \
70         (((hi_u64)(x) & (hi_u64)0xff00000000000000ULL) >> 56)))
71 
72 /* defined the base error code. */
73 #define HI_BASE_ERR_BASE                0x400
74 #define HI_BASE_ERR_BASE_SYMC           (HI_BASE_ERR_BASE + 0x100)
75 #define HI_BASE_ERR_BASE_HASH           (HI_BASE_ERR_BASE + 0x200)
76 #define HI_BASE_ERR_BASE_RSA            (HI_BASE_ERR_BASE + 0x300)
77 #define HI_BASE_ERR_BASE_TRNG           (HI_BASE_ERR_BASE + 0x400)
78 #define HI_BASE_ERR_BASE_SM2            (HI_BASE_ERR_BASE + 0x500)
79 
80 #define CRYPTO_MAGIC_NUM          0xc0704d19
81 
82 /* enumeration module_id. */
83 typedef enum {
84     CRYPTO_MODULE_ID_SYMC,        /* Symmetric Cipher */
85     CRYPTO_MODULE_ID_SYMC_KEY,    /* Symmetric Cipher key */
86     CRYPTO_MODULE_ID_HASH,        /* Message Digest */
87     CRYPTO_MODULE_ID_IFEP_RSA,    /* Asymmetric developed by IFEP */
88     CRYPTO_MODULE_ID_SIC_RSA,     /* Asymmetric developed by SIC */
89     CRYPTO_MODULE_ID_TRNG,        /* Random Data Generation */
90     CRYPTO_MODULE_ID_SM2,         /* Public Key Cryptographic Algorithm Based on Elliptic Curves */
91     CRYPTO_MODULE_ID_SM4,         /* SM4 */
92     CRYPTO_MODULE_ID_SMMU,        /* SMMU */
93     CRYPTO_MODULE_ID_CNT,         /* Count of module id */
94 } module_id;
95 
96 #define crypto_unused(x)    ((x) = (x))
97 
98 #define HASH_BLOCK_SIZE          64
99 
100 #define CRYPTO_IOC_NA            0U
101 #define CRYPTO_IOC_W             1U
102 #define CRYPTO_IOC_R             2U
103 #define CRYPTO_IOC_RW            3U
104 
105 #define HI_ID_CIPHER             0x4D
106 
107 #define CIPHER_NAME              "HI_CIPHER"
108 #define UMAP_DEVNAME_CIPHER      "cipher"
109 #define UMAP_MIN_MINOR_CIPHER    50
110 
111 #define crypto_ioc(dir, type, nr, size) \
112     (((dir) << 30) | ((size) << 16) | ((type) << 8) | ((nr) << 0))
113 
114 #define crypto_ior(nr, size)    crypto_ioc(CRYPTO_IOC_R, HI_ID_CIPHER, (nr), size)
115 #define crypto_iow(nr, size)    crypto_ioc(CRYPTO_IOC_W, HI_ID_CIPHER, (nr), size)
116 #define crypto_iowr(nr, size)   crypto_ioc(CRYPTO_IOC_RW, HI_ID_CIPHER, (nr), size)
117 
118 #define crypto_ioc_dir(cmd)       (((cmd) >> 30) & 0x03)
119 #define crypto_ioc_type(cmd)      (((cmd) >> 8) & 0xFF)
120 #define crypto_ioc_nr(cmd)        (((cmd) >> 0) & 0xFF)
121 #define crypto_ioc_size(cmd)      (((cmd) >> 16) & 0x3FFF)
122 
123 #define CRYPTO_CMD_SYMC_CREATEHANDLE   crypto_ior(0x00, sizeof(symc_create_t))
124 #define CRYPTO_CMD_SYMC_DESTROYHANDLE  crypto_iow (0x01, sizeof(symc_destroy_t))
125 #define CRYPTO_CMD_SYMC_CONFIGHANDLE   crypto_iow (0x02, sizeof(symc_cfg_t))
126 #define CRYPTO_CMD_SYMC_ENCRYPT        crypto_iow (0x03, sizeof(symc_encrypt_t))
127 #define CRYPTO_CMD_SYMC_ENCRYPTMULTI   crypto_iow (0x04, sizeof(symc_encrypt_multi_t))
128 #define CRYPTO_CMD_SYMC_GETTAG         crypto_iowr(0x05, sizeof(aead_tag_t))
129 #define CRYPTO_CMD_HASH_START          crypto_iowr(0x06, sizeof(hash_start_t))
130 #define CRYPTO_CMD_HASH_UPDATE         crypto_iow (0x07, sizeof(hash_update_t))
131 #define CRYPTO_CMD_HASH_FINISH         crypto_iowr(0x08, sizeof(hash_finish_t))
132 #define CRYPTO_CMD_RSA_ENC             crypto_iowr(0x09, sizeof(rsa_info_t))
133 #define CRYPTO_CMD_RSA_DEC             crypto_iowr(0x0a, sizeof(rsa_info_t))
134 #define CRYPTO_CMD_RSA_SIGN            crypto_iowr(0x0b, sizeof(rsa_info_t))
135 #define CRYPTO_CMD_RSA_VERIFY          crypto_iowr(0x0c, sizeof(rsa_info_t))
136 #define CRYPTO_CMD_TRNG                crypto_iowr(0x0d, sizeof(trng_t))
137 #define CRYPTO_CMD_SYMC_GET_CONFIG     crypto_iowr(0x0e, sizeof(symc_get_cfg_t))
138 #define CRYPTO_CMD_KLAD_KEY            crypto_iowr(0x0f, sizeof(klad_key_t))
139 #define CRYPTO_CMD_BN_EXP_MOD          crypto_iowr(0x10, sizeof(klad_key_t))
140 #define CRYPTO_CMD_COUNT               0x11
141 
142 #define crypto_chk_err_goto(_expr) \
143     do { \
144         ret = _expr; \
145         if (ret != HI_SUCCESS) { \
146             hi_log_print_func_err((_expr), ret); \
147             goto exit__; \
148         } \
149     } while (0)
150 
151 /* AES KEY size */
152 #define SYMC_KEY_SIZE       32
153 
154 /* SM1 SK size */
155 #define SYMC_SM1_SK_SIZE    16
156 
157 /* AES IV size */
158 #define AES_IV_SIZE         16
159 
160 /* AES BLOCK size */
161 #define AES_BLOCK_SIZE      16
162 
163 /* DES IV size */
164 #define DES_IV_SIZE         8
165 
166 /* aead tag length */
167 #define AEAD_TAG_SIZE                  16
168 #define AEAD_TAG_SIZE_IN_WORD          4
169 
170 /* bits in a byte */
171 #define BITS_IN_BYTE                   8
172 
173 /* hash result max size */
174 #define HASH_RESULT_MAX_SIZE           64
175 
176 #ifndef CHIP_TYPE_hi3516ev200
177 /* hash result max size in word */
178 #define HASH_RESULT_MAX_SIZE_IN_WORD   16
179 #else
180 /* hash result max size in word */
181 #define HASH_RESULT_MAX_SIZE_IN_WORD   8
182 #endif
183 
184 /*! capacity upport */
185 #define CRYPTO_CAPACITY_SUPPORT        1
186 #define CRYPTO_CAPACITY_NONSUPPORT     0
187 
188 /* max length of SM2 ID */
189 #define SM2_ID_MAX_LEN                 0x1FFF
190 
191 /* Define the time out */
192 #define CRYPTO_TIME_OUT                6000
193 #define MS_TO_US                       1000
194 
195 /* result size */
196 #define SHA1_RESULT_SIZE           20
197 #define SHA224_RESULT_SIZE         28
198 #define SHA256_RESULT_SIZE         32
199 #define SHA384_RESULT_SIZE         48
200 #define SHA512_RESULT_SIZE         64
201 #define SM3_RESULT_SIZE            32
202 /* rsa key length */
203 #define RSA_MIN_KEY_LEN           128
204 #define RSA_MAX_KEY_LEN           512
205 #define RSA_KEY_BITWIDTH_1024     128
206 #define RSA_KEY_BITWIDTH_2048     256
207 #define RSA_KEY_BITWIDTH_3072     384
208 #define RSA_KEY_BITWIDTH_4096     512
209 #define RSA_KEY_EXPONENT_VALUE1   0X3
210 #define RSA_KEY_EXPONENT_VALUE2   0X10001
211 
212 /* the source of hash message */
213 typedef enum {
214     HASH_CHUNCK_SRC_LOCAL,     /* Local buffer, e.g. Kernel  */
215     HASH_CHUNCK_SRC_USER,      /* User buffer, use copy_from_user to read data */
216 } hash_chunk_src;
217 
218 /* union of compat addr */
219 typedef union {
220     hi_void *p;                   /* virtual address */
221     const hi_void *cp;            /* const virtual address */
222     hi_phys_addr_t phy;       /* physical address */
223     unsigned int word[MUL_VAL_2]; /* double word of address */
224 } compat_addr;
225 
226 #define addr_h32(addr)          (addr).word[WORD_IDX_1]  /* High 32 bit of hi_u64 */
227 #define addr_l32(addr)          (addr).word[WORD_IDX_0]  /* Low 32 bit of hi_u64 */
228 #define addr_u64(addr)          (addr).phy      /* 64 bit of hi_u64 */
229 #define addr_via(addr)          (addr).p        /* buffer point */
230 #define addr_via_const(addr)    (addr).cp       /* const buffer point */
231 
232 /* struct of Symmetric cipher create */
233 typedef struct {
234     hi_u32 id;              /* to store the id of soft channel */
235     hi_u32 reserve;         /* reserve to make align at 64bit */
236 } symc_create_t;
237 
238 /* struct of Symmetric cipher destroy */
239 typedef struct {
240     hi_u32 id;              /* id of soft channel */
241     hi_u32 reserve;         /* reserve to make align at 64bit */
242 } symc_destroy_t;
243 
244 /* struct of Symmetric cipher configure information */
245 typedef struct {
246     hi_u32 id;                           /* Id of soft channel */
247     hi_u32 hard_key;                     /* Use hard key or not */
248     hi_cipher_alg alg;                   /* Symmetric cipher algorithm */
249     hi_cipher_work_mode mode;            /* Symmetric cipher algorithm */
250     hi_cipher_bit_width width;           /* Symmetric cipher bit width */
251     hi_cipher_key_len klen;              /* Symmetric cipher key length */
252     hi_cipher_sm1_round sm1_round_num;   /* The round number of sm1 */
253     hi_u8 fkey[SYMC_KEY_SIZE];           /* first  key buffer, default */
254     hi_u8 skey[SYMC_KEY_SIZE];           /* second key buffer */
255     hi_u8 iv[AES_IV_SIZE];               /* IV buffer */
256     hi_u32 ivlen;                        /* IV length */
257     hi_u32 iv_usage;                     /* Usage of IV */
258     hi_u32 reserve;                      /* reserve to make align at 64bit */
259     compat_addr aad;                     /* Associated Data */
260     hi_u32 alen;                        /* Associated Data Length */
261     hi_u32 tlen;                        /* Tag length */
262 } symc_cfg_t;
263 
264 typedef enum {
265     SYMC_OPERATION_ENCRYPT = 0,
266     SYMC_OPERATION_DECRYPT = 1,
267     SYMC_OPERATION_ENCRYPT_VIA = 0x10,
268     SYMC_OPERATION_DECRYPT_VIA = 0x11,
269 } symc_operation_type;
270 
271 /* struct of Symmetric cipher encrypt/decrypt */
272 typedef struct {
273     hi_u32 id;              /* Id of soft channel */
274     hi_u32 len;             /* Length of the encrypted data */
275     hi_u32 operation;       /* operation type */
276     hi_u32 last;            /* last or not */
277     compat_addr in;         /* Physical address of the input data */
278     compat_addr out;        /* Physical address of the output data */
279 } symc_encrypt_t;
280 
281 /* struct of Symmetric cipher multiple encrypt/decrypt */
282 typedef struct {
283     hi_u32 id;              /* Id of soft channel */
284     compat_addr pack;       /* Buffer of package information */
285     hi_u32 pack_num;        /* Number of package information */
286     hi_u32 operation;       /* Decrypt or encrypt */
287 } symc_encrypt_multi_t;
288 
289 /* struct of Symmetric cipher get tag */
290 typedef struct {
291     hi_u32 id;                            /* Id of soft channel */
292     hi_u32 tag[AEAD_TAG_SIZE_IN_WORD];    /* Buffer of tag */
293     hi_u32 taglen;                        /* Length of tag */
294 } aead_tag_t;
295 
296 /* struct of Symmetric cipher get ctrl */
297 typedef struct {
298     hi_u32 id;                            /* Id of soft channel */
299     hi_cipher_ctrl ctrl;                 /* control information */
300 } symc_get_cfg_t;
301 
302 /* struct of Hash start */
303 typedef struct {
304     hi_u32 id;                            /* Id of soft channel */
305     hi_cipher_hash_type type;            /* HASH type */
306     compat_addr key;                      /* HMAC key */
307     hi_u32 keylen;                        /* HMAC key */
308     hi_u32 reserve;                       /* reserve for align at 64bit */
309 } hash_start_t;
310 
311 /* struct of Hash update */
312 typedef struct {
313     hi_u32 id;             /* Id of soft channel */
314     hi_u32 length;         /* Length of the message */
315     compat_addr input;     /* Message data buffer */
316     hash_chunk_src src;    /* source of hash message */
317     hi_u32 reserve;        /* reserve for align at 64bit */
318 } hash_update_t;
319 
320 /* struct of Hash update */
321 typedef struct {
322     hi_u32 id;             /* Id of soft channel */
323     hi_u32 hash[HASH_RESULT_MAX_SIZE_IN_WORD]; /* buffer holding the hash data */
324     hi_u32 hashlen;        /* length of the hash data */
325     hi_u32 reserve;        /* reserve for align at 64bit */
326 } hash_finish_t;
327 
328 /* struct of rsa encrypt/decrypt */
329 typedef struct {
330     hi_cipher_rsa_encrypt_scheme scheme; /* RSA encryption scheme */
331     hi_u16 public;             /* Type of key, true-public or false-private */
332     hi_u16 ca_type;            /* ca Type of key */
333     hi_u32 klen;               /* length of rsa key */
334     hi_u32 e;                  /* The public exponent */
335     compat_addr d;             /* The private exponent */
336     compat_addr n;             /* The modulus */
337     compat_addr p;             /* The p factor of N */
338     compat_addr q;             /* The q factor of N */
339     compat_addr qp;            /* The 1/q mod p CRT param */
340     compat_addr dp;            /* The d mod (p - 1) CRT param */
341     compat_addr dq;            /* The d mod (q - 1) CRT param */
342     compat_addr in;            /* input data to be encryption */
343     compat_addr out;           /* output data of encryption */
344     hi_u32 inlen;              /* length of input data to be encryption */
345     hi_u32 outlen;             /* length of output data */
346 } rsa_info_t;
347 
348 /* RSA PKCS style key */
349 typedef struct {
350     hi_u8 public;              /* Type of key, true-public or false-private */
351     hi_u8 ca_type;             /* The key source */
352     hi_u16 klen;               /* The key length */
353     hi_u32 e;                  /* The public exponent */
354     hi_u8 *d;                  /* The private exponent */
355     hi_u8 *n;                  /* The modulus */
356     hi_u8 *p;                  /* The p factor of n */
357     hi_u8 *q;                  /* The q factor of n */
358     hi_u8 *qp;                 /* The 1/q mod p CRT param */
359     hi_u8 *dp;                 /* The d mod (p - 1) CRT param */
360     hi_u8 *dq;                 /* The d mod (q - 1) CRT param */
361     hi_u32 bufsize;            /* The buffer size alloc for n */
362 } cryp_rsa_key;
363 
364 /* struct of rsa crypt input and output data. */
365 typedef struct {
366     hi_cipher_rsa_encrypt_scheme scheme;
367     hi_u8 *in;
368     hi_u32 in_len;
369     hi_u8 *out;
370     hi_u32 out_len;
371 } cryp_rsa_crypt_data;
372 
373 /* struct of rsa sign or verify data. */
374 typedef struct {
375     hi_cipher_rsa_sign_scheme scheme;
376     hi_u8 *in;
377     hi_u32 in_len;
378     hi_u8 *out;
379     hi_u32 out_len;
380 } cryp_rsa_sign_data;
381 
382 typedef struct {
383     compat_addr n;      /* RSA input N */
384     compat_addr k;      /* RSA input E/D */
385     compat_addr in;     /* RSA input data */
386     compat_addr out;    /* RSA output data */
387     hi_u16 length;      /* RSA N/E/D, input/output data length */
388 } rsa_bn_exp_mod_t;
389 
390 typedef struct {
391     hi_u8 *n;           /* RSA input N */
392     hi_u8 *k;           /* RSA input E/D */
393     hi_u8 *in;          /* RSA input data */
394     hi_u8 *out;         /* RSA output data */
395     hi_u16 length;      /* RSA N/E/D, input/output data length */
396 } cryp_rsa_exp_mod;
397 
398 /* struct of klad key */
399 typedef struct {
400     hi_u32 keysel;
401     hi_u32 target;
402     hi_u8 clear[AES_BLOCK_SIZE];
403     hi_u8 encrypt[AES_BLOCK_SIZE];
404 } klad_key_t;
405 
406 /* struct of trng */
407 typedef struct {
408     hi_u32 randnum;            /* randnum rand number */
409     hi_u32 timeout;            /* time out  */
410 } trng_t;
411 
412 /*
413  * brief   Kapi Init.
414  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
415  */
416 hi_s32 kapi_symc_init(hi_void);
417 
418 /*
419  * brief   Kapi Deinit.
420  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
421  */
422 hi_s32 kapi_symc_deinit(hi_void);
423 
424 /*
425  * brief   Kapi release.
426  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
427  */
428 hi_s32 kapi_symc_release(hi_void);
429 
430 /*
431  * brief   Create symc handle.
432  * param[in]  id The channel number.
433  * param[in]  uuid The user identification.
434  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
435  */
436 hi_s32 kapi_symc_create(hi_u32 *id);
437 
438 /*
439  * brief   Destroy symc handle.
440  * param[in]  id The channel number.
441  * param[in]  uuid The user identification.
442  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
443  */
444 hi_s32 kapi_symc_destroy(hi_u32 id);
445 
446 /*
447  * brief  set work params.
448  * param[in]  cfg config information.
449  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
450  */
451 hi_s32 kapi_symc_cfg(const symc_cfg_t *cfg);
452 
453 /*
454  * brief  get work params.
455  * param[in]  id   The channel number.
456  * param[out] ctrl information.
457  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
458  */
459 hi_s32 kapi_symc_get_cfg(hi_u32 id, hi_cipher_ctrl *ctrl);
460 
461 /*
462  * brief          SYMC  buffer encryption/decryption.
463  *
464  * Note: Due to the nature of aes you should use the same key schedule for
465  * both encryption and decryption.
466  *
467  * param[in] crypt  The symc info data.
468  *
469  * return           0 if successful
470  */
471 hi_s32 kapi_symc_crypto(symc_encrypt_t *crypt);
472 
473 /*
474  * brief            SYMC  via buffer encryption/decryption.
475  *
476  * Note: Due to the nature of aes you should use the same key schedule for
477  * both encryption and decryption.
478  *
479  * param[in] crypt  The symc info data.
480  * param[in] is_from_user input and output virtual address is from user or kapi.
481  *
482  * return           0 if successful
483  */
484 hi_s32 kapi_symc_crypto_via(symc_encrypt_t *crypt, hi_u32 is_from_user);
485 
486 /*
487  * brief            SYMC multiple buffer encryption/decryption.
488  *
489  * Note: Due to the nature of aes you should use the same key schedule for
490  * both encryption and decryption.
491  *
492  * param[in] id     The channel number.
493  * param pack       Buffer of package information
494  * param pack_num   Number of package information
495  * param operation  decrypt or encrypt
496  * param last       last or not
497  *
498  * return           0 if successful
499  */
500 hi_s32 kapi_symc_crypto_multi(hi_u32 id, const hi_cipher_data *pack, hi_u32 pack_num, hi_u32 operation, hi_u32 last);
501 
502 /*
503  * brief          SYMC multiple buffer encryption/decryption.
504  * param[in]  id  The channel number.
505  * param[in]  tag tag data of CCM/GCM
506  *
507  * return         0 if successful
508  */
509 hi_s32 kapi_aead_get_tag(hi_u32 id, hi_u32 tag[AEAD_TAG_SIZE_IN_WORD], hi_u32 *taglen);
510 
511 hi_s32 kapi_symc_klad_encrypt_key(hi_u32 keysel,
512     hi_u32 target, hi_u8 *clear, hi_u8 *encrypt, hi_u32 key_len);
513 
514 /*
515  * brief   Kapi Init.
516  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
517  */
518 hi_s32 kapi_hash_init(hi_void);
519 
520 /*
521  * brief   Kapi Deinit.
522  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
523  */
524 hi_s32 kapi_hash_deinit(hi_void);
525 
526 /*
527  * brief          HASH context setup.
528  *
529  *
530  * param[out] id      The channel number.
531  * param[in]  type    Hash type
532  * param[in]  key     hmac key
533  * param[in]  keylen  hmac key length
534  *
535  * return         0 if successful
536  */
537 hi_s32 kapi_hash_start(hi_u32 *id, hi_cipher_hash_type type, const hi_u8 *key, hi_u32 keylen);
538 
539 /*
540  * brief          HASH process buffer.
541  *
542  * param[in] id       The channel number.
543  * param[in] input    buffer holding the input data
544  * param[in] length   length of the input data
545  * param[in] src      source of hash message
546  *
547  * return         0 if successful
548  */
549 hi_s32 kapi_hash_update(hi_u32 id, const hi_u8 *input, hi_u32 length, hash_chunk_src src);
550 
551 /*
552  * brief          HASH final digest.
553  *
554  * param[in]  id      The channel number.
555  * param[out] hash    buffer holding the hash data_type_t
556  * param[in] hash_buf_len    buffer length of holding the hash data
557  * param[out] hashlen length of the hash data
558  *
559  * return         0 if successful
560  */
561 hi_s32 kapi_hash_finish(hi_u32 id, hi_u8 *hash, hi_u32 hash_buf_len, hi_u32 *hashlen);
562 
563 /*
564  * brief   hash release.
565  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
566  */
567 hi_s32 kapi_hash_release(hi_void);
568 
569 /*
570  * brief   Kapi Init.
571  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
572  */
573 hi_s32 kapi_rsa_init(hi_void);
574 
575 /*
576  * brief   Kapi Deinitialize.
577  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
578  */
579 hi_s32 kapi_rsa_deinit(hi_void);
580 
581 /*
582  * brief RSA encryption a plaintext with a RSA private key.
583  *
584  * param[in] key:       rsa key struct.
585  * param[in/out] rsa:   rsa encrypt/decrypt data.
586  * retval ::HI_SUCCESS  Call this API successful
587  * retval ::HI_FAILURE  Call this API fails.
588  */
589 hi_s32 kapi_rsa_encrypt(cryp_rsa_key *key, cryp_rsa_crypt_data *rsa);
590 
591 /*
592  * brief RSA decryption a ciphertext with a RSA public key.
593  *
594  * param[in] key:       rsa key struct.
595  * param[in/out] rsa:   rsa encrypt/decrypt data.
596  * retval ::HI_SUCCESS  Call this API successful
597  * retval ::HI_FAILURE  Call this API fails.
598  */
599 hi_s32 kapi_rsa_decrypt(cryp_rsa_key *key, cryp_rsa_crypt_data *rsa);
600 
601 /*
602  * brief RSA signature a context with appendix, where a signer's RSA private key is used.
603  *
604  * param[in] key:       rsa key struct.
605  * param[in/out] rsa:   rsa signature data.
606  * retval ::HI_SUCCESS  Call this API successful
607  * retval ::HI_FAILURE  Call this API fails.
608  */
609 hi_s32 kapi_rsa_sign_hash(cryp_rsa_key *key, cryp_rsa_sign_data *rsa);
610 
611 /*
612  * brief RSA verify a ciphertext with a RSA public key.
613  *
614  * param[in]  key: rsa key struct.
615  * param[in/out] rsa:   rsa verify data.
616  * retval ::HI_SUCCESS  Call this API successful
617  * retval ::HI_FAILURE  Call this API fails.
618  */
619 hi_s32 kapi_rsa_verify_hash(cryp_rsa_key *key, cryp_rsa_sign_data *rsa);
620 
621 hi_s32 kapi_rsa_bn_exp_mod(cryp_rsa_exp_mod *exp_mod);
622 
623 /*
624  * brief get rand number.
625  * param[out]  randnum rand number.
626  * param[in]   timeout time out.
627  * retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
628  */
629 hi_s32 kapi_trng_get_random(hi_u32 *randnum, hi_u32 timeout);
630 
631 /* cipher kapi_test_main */
632 hi_void kapi_test_main(hi_void);
633 
634 
635 #ifdef __cplusplus
636 }
637 #endif    /* __cplusplus */
638 
639 #endif    /* End of #ifndef __DRV_CIPHER_KAPI_H__ */
640