• 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 #include "drv_osal_lib.h"
20 #include "cryp_symc.h"
21 #include "hi_drv_compat.h"
22 #include "securec.h"
23 
24 /* max number of nodes */
25 #define MAX_PKG_NUMBER                  100000
26 
27 /* max length of CCM/GCM AAD */
28 #define MAX_AEAD_A_LEN                  0x100000
29 
30 typedef struct {
31     hi_u32 open   : 1;                  /* open or close */
32     hi_u32 config : 1;                  /* already config or not */
33     symc_func *func;
34     void *cryp_ctx;                     /* Context of cryp instance */
35     CRYPTO_OWNER owner;                 /* user ID */
36     hi_cipher_ctrl  ctrl;               /* control information */
37 } kapi_symc_ctx;
38 
39 typedef struct {
40     hi_u32 soft_id;
41     symc_width width;
42     hi_u32 byca;
43     hi_u32 ca_type;
44     hi_u32 klen;
45 } symc_set_cfg_param;
46 
47 /*! Context of cipher */
48 static kapi_symc_ctx g_kapi_ctx[CRYPTO_HARD_CHANNEL_MAX];
49 
50 /* symc mutex */
51 static CRYPTO_MUTEX g_symc_mutex;
52 
53 #define kapi_symc_lock_err_return()   \
54     do { \
55         ret = crypto_mutex_lock(&g_symc_mutex);  \
56         if (ret != HI_SUCCESS) { \
57             hi_log_error("error, symc lock failed\n"); \
58             hi_log_print_func_err(crypto_mutex_lock, ret); \
59             return ret; \
60         } \
61     } while (0)
62 
63 #define kapi_symc_unlock()          crypto_mutex_unlock(&g_symc_mutex)
64 #define AES_CCM_MIN_TAG_LEN         4
65 #define AES_CCM_MAX_TAG_LEN         16
66 #define AES_GCM_MIN_TAG_LEN         1
67 #define AES_GCM_MAX_TAG_LEN         16
68 
69 /* ****************************** API Code **************************** */
kapi_symc_chk_handle(hi_handle handle)70 static hi_s32 kapi_symc_chk_handle(hi_handle handle)
71 {
72     if ((hi_handle_get_modid(handle) != HI_ID_CIPHER) || (hi_handle_get_private_data(handle) != 0)) {
73         hi_log_error("Invalid handle 0x%x!\n", handle);
74         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_HANDLE);
75         return HI_ERR_CIPHER_INVALID_HANDLE;
76     }
77 
78     if (hi_handle_get_chnid(handle) >= CRYPTO_HARD_CHANNEL_MAX) {
79         hi_log_error("chan %u is too large, max: %d\n", hi_handle_get_chnid(handle), CRYPTO_HARD_CHANNEL_MAX);
80         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_HANDLE);
81         return HI_ERR_CIPHER_INVALID_HANDLE;
82     }
83 
84     if (g_kapi_ctx[hi_handle_get_chnid(handle)].open == HI_FALSE) {
85         hi_log_error("chan %u is not open\n", hi_handle_get_chnid(handle));
86         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_HANDLE);
87         return HI_ERR_CIPHER_INVALID_HANDLE;
88     }
89 
90     return HI_SUCCESS;
91 }
92 
kapi_symc_init(void)93 hi_s32 kapi_symc_init(void)
94 {
95     hi_s32 ret;
96 
97     hi_log_info("kapi_symc_init()\n");
98 
99     hi_log_func_enter();
100 
101     crypto_mutex_init(&g_symc_mutex);
102 
103     (hi_void)memset_s(g_kapi_ctx, sizeof(g_kapi_ctx), 0, sizeof(g_kapi_ctx));
104 
105     ret = cryp_symc_init();
106     if (ret != HI_SUCCESS) {
107         hi_log_print_func_err(cryp_symc_init, ret);
108         return ret;
109     }
110 
111     hi_log_func_exit();
112     return HI_SUCCESS;
113 }
114 
kapi_symc_deinit(void)115 hi_s32 kapi_symc_deinit(void)
116 {
117     hi_log_info("kapi_symc_deinit()\n");
118 
119     hi_log_func_enter();
120 
121     cryp_symc_deinit();
122 
123     crypto_mutex_destroy(&g_symc_mutex);
124 
125     hi_log_func_exit();
126     return HI_SUCCESS;
127 }
128 
kapi_symc_release(void)129 hi_s32 kapi_symc_release(void)
130 {
131     hi_u32 i, chn;
132     hi_s32 ret;
133     kapi_symc_ctx *ctx = HI_NULL;
134     CRYPTO_OWNER owner;
135 
136     hi_log_func_enter();
137 
138     crypto_get_owner(&owner);
139 
140     hi_log_info("symc release owner 0x%x\n", owner);
141 
142     /* destroy the channel which are created by current user */
143     for (i = 0; i < CRYPTO_HARD_CHANNEL_MAX; i++) {
144         ctx = &g_kapi_ctx[i];
145         if (ctx->open == HI_TRUE) {
146             if (memcmp(&owner, &ctx->owner, sizeof(owner)) != 0) {
147                 continue;
148             }
149             chn = hi_handle_init(HI_ID_CIPHER, 0, i);
150             hi_log_info("symc release chn %u\n", chn);
151             ret = kapi_symc_destroy(chn);
152             if (ret != HI_SUCCESS) {
153                 hi_log_print_func_err(kapi_symc_destroy, ret);
154                 return ret;
155             }
156         }
157     }
158 
159     hi_log_func_exit();
160     return HI_SUCCESS;
161 }
162 
kapi_symc_create(hi_u32 * id)163 hi_s32 kapi_symc_create(hi_u32 *id)
164 {
165     hi_s32 ret;
166     hi_u32 chn = 0;
167     kapi_symc_ctx *ctx = HI_NULL;
168 
169     hi_log_func_enter();
170 
171     hi_log_chk_param_return(id == HI_NULL);
172 
173     kapi_symc_lock_err_return();
174 
175     /* allocate a aes soft channel for hard channel allocated */
176     ret = cryp_symc_alloc_chn(&chn);
177     if (ret != HI_SUCCESS) {
178         hi_log_error("error, allocate symc channel failed\n");
179         hi_log_print_func_err(cryp_symc_alloc_chn, ret);
180         kapi_symc_unlock();
181         return ret;
182     }
183     ctx = &g_kapi_ctx[chn];
184 
185     (hi_void)memset_s(ctx, sizeof(kapi_symc_ctx), 0, sizeof(kapi_symc_ctx));
186     crypto_get_owner(&ctx->owner);
187 
188     *id = hi_handle_init(HI_ID_CIPHER, 0, chn);
189     ctx->open = HI_TRUE;
190     ctx->config = HI_FALSE;
191 
192     hi_log_info("kapi_symc_create()- chn %u\n", chn);
193 
194     kapi_symc_unlock();
195 
196     hi_log_func_exit();
197     return HI_SUCCESS;
198 }
199 
kapi_symc_destroy(hi_u32 id)200 hi_s32 kapi_symc_destroy(hi_u32 id)
201 {
202     hi_s32 ret;
203     kapi_symc_ctx *ctx = HI_NULL;
204     hi_u32 soft_id;
205 
206     hi_log_func_enter();
207 
208     ret = kapi_symc_chk_handle((hi_handle)id);
209     if (ret != HI_SUCCESS) {
210         hi_log_print_func_err(kapi_symc_chk_handle, ret);
211         return ret;
212     }
213 
214     soft_id = hi_handle_get_chnid(id);
215     ctx = &g_kapi_ctx[soft_id];
216     crypto_chk_owner_err_return(&ctx->owner);
217 
218     kapi_symc_lock_err_return();
219 
220     cryp_symc_free_chn(soft_id);
221 
222     /* Destroy the attached instance of Symmetric cipher engine */
223     if ((ctx->func != HI_NULL) && (ctx->func->destroy != HI_NULL)) {
224         ret = ctx->func->destroy(ctx->cryp_ctx);
225         if (ret != HI_SUCCESS) {
226             hi_log_print_func_err(ctx->func->destroy, ret);
227             kapi_symc_unlock();
228             return ret;
229         }
230         ctx->cryp_ctx = HI_NULL;
231     }
232 
233     ctx->open = HI_FALSE;
234 
235     hi_log_info("kapi_symc_destroy()- chn 0x%x\n", id);
236 
237     kapi_symc_unlock();
238 
239     hi_log_func_exit();
240     return HI_SUCCESS;
241 }
242 
kapi_symc_chk_des_3des_param(hi_cipher_alg alg,hi_cipher_work_mode mode,hi_u32 width)243 static hi_s32 kapi_symc_chk_des_3des_param(hi_cipher_alg alg, hi_cipher_work_mode mode, hi_u32 width)
244 {
245 #ifndef CHIP_DES_SUPPORT
246     if (alg == HI_CIPHER_ALG_DES) {
247         hi_log_error("Invalid alg, unsupported des.\n");
248         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARA);
249         return HI_ERR_CIPHER_INVALID_PARA;
250     }
251 #endif
252 #ifndef CHIP_3DES_SUPPORT
253     if (alg == HI_CIPHER_ALG_3DES) {
254         hi_log_error("Invalid alg, unsupported 3des.\n");
255         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARA);
256         return HI_ERR_CIPHER_INVALID_PARA;
257     }
258 #endif
259     if (mode > HI_CIPHER_WORK_MODE_OFB) {
260         hi_log_error("Invalid alg %d and mode %d\n", alg, mode);
261         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARA);
262         return HI_ERR_CIPHER_INVALID_PARA;
263     }
264 
265     if ((mode == HI_CIPHER_WORK_MODE_CFB) || (mode == HI_CIPHER_WORK_MODE_OFB)) {
266         if ((width != SYMC_DAT_WIDTH_64) && (width != SYMC_DAT_WIDTH_8) && (width != SYMC_DAT_WIDTH_1)) {
267             hi_log_error("Invalid mode %d and bit width %d\n", mode, width);
268             hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARA);
269             return HI_ERR_CIPHER_INVALID_PARA;
270         }
271     }
272 
273     return HI_SUCCESS;
274 }
275 
kapi_symc_chk_aes_param(hi_cipher_alg alg,hi_cipher_work_mode mode,hi_u32 width)276 static hi_s32 kapi_symc_chk_aes_param(hi_cipher_alg alg, hi_cipher_work_mode mode, hi_u32 width)
277 {
278     if (mode > HI_CIPHER_WORK_MODE_BUTT) {
279         hi_log_error("Invalid alg %d and mode %d\n", alg, mode);
280         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
281         return HI_ERR_CIPHER_INVALID_PARAM;
282     }
283 
284     if ((mode == HI_CIPHER_WORK_MODE_CFB) && (width != SYMC_DAT_WIDTH_1) && (width != SYMC_DAT_WIDTH_8) &&
285         (width != SYMC_DAT_WIDTH_128)) {
286         hi_log_error("Invalid alg %d mode %d and width %u\n", alg, mode, width);
287         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
288         return HI_ERR_CIPHER_INVALID_PARAM;
289     }
290 
291     if ((mode == HI_CIPHER_WORK_MODE_OFB) && (width != SYMC_DAT_WIDTH_128)) {
292         hi_log_error("Invalid alg %d mode %d and width %u\n", alg, mode, width);
293         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
294         return HI_ERR_CIPHER_INVALID_PARAM;
295     }
296 
297     return HI_SUCCESS;
298 }
299 
kapi_symc_chk_sm1_param(hi_cipher_alg alg,hi_cipher_work_mode mode,hi_u32 width,hi_u32 round)300 static hi_s32 kapi_symc_chk_sm1_param(hi_cipher_alg alg, hi_cipher_work_mode mode, hi_u32 width, hi_u32 round)
301 {
302     if (mode > HI_CIPHER_WORK_MODE_OFB) {
303         hi_log_error("Invalid alg %d and mode %d\n", alg, mode);
304         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
305         return HI_ERR_CIPHER_INVALID_PARAM;
306     }
307 
308     if ((mode == HI_CIPHER_WORK_MODE_OFB)
309         && (width != SYMC_DAT_WIDTH_128)) {
310         hi_log_error("Invalid alg %d mode %d and width %u\n", alg, mode, width);
311         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
312         return HI_ERR_CIPHER_INVALID_PARAM;
313     }
314 
315     if ((mode == HI_CIPHER_WORK_MODE_CFB)
316         && (width >= SYMC_DAT_WIDTH_COUNT)) {
317         hi_log_error("Invalid alg %d mode %d and width %u\n", alg, mode, width);
318         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
319         return HI_ERR_CIPHER_INVALID_PARAM;
320     }
321 
322     if ((alg == HI_CIPHER_ALG_SM1) && (round >= HI_CIPHER_SM1_ROUND_BUTT)) {
323         hi_log_error("Invalid alg %d and Sm1Round %u\n", alg, round);
324         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
325         return HI_ERR_CIPHER_INVALID_PARAM;
326     }
327 
328     return HI_SUCCESS;
329 }
330 
kapi_symc_chk_sm4_param(hi_cipher_alg alg,hi_cipher_work_mode mode)331 static hi_s32 kapi_symc_chk_sm4_param(hi_cipher_alg alg, hi_cipher_work_mode mode)
332 {
333     if ((mode != HI_CIPHER_WORK_MODE_ECB) && (mode != HI_CIPHER_WORK_MODE_CBC) && (mode != HI_CIPHER_WORK_MODE_CTR)) {
334         hi_log_error("Invalid alg %d and mode %d\n", alg, mode);
335         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
336         return HI_ERR_CIPHER_INVALID_PARAM;
337     }
338 
339     return HI_SUCCESS;
340 }
341 
kapi_symc_width_check(hi_cipher_alg alg,hi_cipher_work_mode mode,hi_u32 width,hi_u32 round)342 static hi_s32 kapi_symc_width_check(hi_cipher_alg alg, hi_cipher_work_mode mode, hi_u32 width, hi_u32 round)
343 {
344     hi_s32 ret;
345 
346     crypto_unused(round);
347 
348     /* the bit width depend on alg and mode, which limit to hardware
349      * des/3des with cfb/ofb support bit1, bit8, bit 64.
350      * aes with cfb/ofb only support bit128.
351      * sm1 with ofb only support bit128, cfb support bit1, bit8, bit 64.
352      */
353     hi_log_func_enter();
354 
355     hi_log_chk_param_return(alg >= HI_CIPHER_ALG_BUTT);
356     hi_log_chk_param_return((alg != HI_CIPHER_ALG_DMA) && (mode >= HI_CIPHER_WORK_MODE_BUTT));
357     hi_log_chk_param_return((alg != HI_CIPHER_ALG_DMA) && (width >= SYMC_DAT_WIDTH_COUNT));
358 
359     if ((alg == HI_CIPHER_ALG_3DES) || (alg == HI_CIPHER_ALG_DES)) {
360         ret = kapi_symc_chk_des_3des_param(alg, mode, width);
361         if (ret != HI_SUCCESS) {
362             hi_log_print_func_err(kapi_symc_chk_des_3des_param, ret);
363             return ret;
364         }
365     }
366 
367     if (alg == HI_CIPHER_ALG_AES) {
368         ret = kapi_symc_chk_aes_param(alg, mode, width);
369         if (ret != HI_SUCCESS) {
370             hi_log_print_func_err(kapi_symc_chk_aes_param, ret);
371             return ret;
372         }
373     }
374 
375     if (alg == HI_CIPHER_ALG_SM1) {
376         ret = kapi_symc_chk_sm1_param(alg, mode, width, round);
377         if (ret != HI_SUCCESS) {
378             hi_log_print_func_err(kapi_symc_chk_sm1_param, ret);
379             return ret;
380         }
381     }
382 
383     if (alg == HI_CIPHER_ALG_SM4) {
384         ret = kapi_symc_chk_sm4_param(alg, mode);
385         if (ret != HI_SUCCESS) {
386             hi_log_print_func_err(kapi_symc_chk_sm4_param, ret);
387             return ret;
388         }
389     }
390 
391     hi_log_func_exit();
392     return HI_SUCCESS;
393 }
394 
kapi_symc_match_width(hi_cipher_work_mode work_mode,hi_cipher_bit_width bit_width,symc_width * width)395 static hi_s32 kapi_symc_match_width(hi_cipher_work_mode work_mode, hi_cipher_bit_width bit_width, symc_width *width)
396 {
397     hi_log_func_enter();
398 
399     /* set the bit width which depend on alg and mode */
400     if ((work_mode == HI_CIPHER_WORK_MODE_CFB) || (work_mode == HI_CIPHER_WORK_MODE_OFB)) {
401         switch (bit_width) {
402             case HI_CIPHER_BIT_WIDTH_128BIT: {
403                 *width = SYMC_DAT_WIDTH_128;
404                 break;
405             }
406             case HI_CIPHER_BIT_WIDTH_64BIT: {
407                 *width = SYMC_DAT_WIDTH_64;
408                 break;
409             }
410             case HI_CIPHER_BIT_WIDTH_8BIT: {
411                 *width = SYMC_DAT_WIDTH_8;
412                 break;
413             }
414             case HI_CIPHER_BIT_WIDTH_1BIT: {
415                 *width = SYMC_DAT_WIDTH_1;
416                 break;
417             }
418             default: {
419                 hi_log_error("Invalid width: 0x%x, mode 0x%x\n", bit_width, work_mode);
420                 hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
421                 return HI_ERR_CIPHER_INVALID_PARAM;
422             }
423         }
424     } else {
425         if (bit_width == HI_CIPHER_BIT_WIDTH_128BIT) {
426             *width = SYMC_DAT_WIDTH_128;
427         } else if (bit_width == HI_CIPHER_BIT_WIDTH_64BIT) {
428             *width = SYMC_DAT_WIDTH_64;
429         } else {
430             hi_log_error("Invalid width: 0x%x, mode 0x%x\n", bit_width, work_mode);
431             hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
432             return HI_ERR_CIPHER_INVALID_PARAM;
433         }
434     }
435 
436     hi_log_func_exit();
437     return HI_SUCCESS;
438 }
439 
kapi_symc_chk_param(hi_u32 hard_key,const symc_cfg_t * cfg,symc_width * width)440 static hi_s32 kapi_symc_chk_param(hi_u32 hard_key, const symc_cfg_t *cfg, symc_width *width)
441 {
442     hi_s32 ret;
443 
444     hi_log_func_enter();
445 
446     if (cfg->alg == HI_CIPHER_ALG_DMA) {
447         hi_log_info("Alg is DMA.\n");
448         hi_dbg_print_u32(cfg->alg);
449         return HI_SUCCESS;
450     }
451 
452     if ((hard_key != HI_TRUE) && (hard_key != HI_FALSE)) {
453         hi_log_error("Invalid hard_key: 0x%x\n", hard_key);
454         return HI_ERR_CIPHER_INVALID_PARAM;
455     }
456     if (cfg->klen > HI_CIPHER_KEY_DES_2KEY) {
457         hi_log_error("Invalid key len: 0x%x\n", cfg->klen);
458         return HI_ERR_CIPHER_INVALID_PARAM;
459     }
460 
461     /* set the bit width which depend on alg and mode */
462     ret = kapi_symc_match_width(cfg->mode, cfg->width, width);
463     if (ret != HI_SUCCESS) {
464         hi_err_print_u32(cfg->mode);
465         hi_err_print_u32(cfg->width);
466         hi_err_print_u32(*width);
467         hi_log_print_func_err(kapi_symc_match_width, ret);
468         return ret;
469     }
470 
471     ret = kapi_symc_width_check(cfg->alg, cfg->mode, *width, cfg->sm1_round_num);
472     if (ret != HI_SUCCESS) {
473         hi_err_print_u32(cfg->alg);
474         hi_err_print_u32(cfg->mode);
475         hi_err_print_u32(*width);
476         hi_err_print_u32(cfg->sm1_round_num);
477         hi_log_print_func_err(kapi_symc_width_check, ret);
478         return ret;
479     }
480 
481     if (cfg->iv_usage > HI_CIPHER_IV_CHG_ALL_PACK) {
482         hi_log_error("Invalid IV Change Flags: 0x%x\n", cfg->iv_usage);
483         return HI_ERR_CIPHER_INVALID_PARAM;
484     }
485 
486     if ((cfg->iv_usage == HI_CIPHER_IV_CHG_ALL_PACK) &&
487         ((cfg->mode == HI_CIPHER_WORK_MODE_CCM) || (cfg->mode == HI_CIPHER_WORK_MODE_GCM))) {
488         hi_log_error("Invalid IV Change Flags: 0x%x\n", cfg->iv_usage);
489         return HI_ERR_CIPHER_INVALID_PARAM;
490     }
491 
492     hi_log_func_exit();
493     return HI_SUCCESS;
494 }
495 
kapi_symc_check_ccm_gcm_taglen(hi_cipher_alg alg,hi_cipher_work_mode work_mode,hi_u32 tlen)496 static hi_s32 kapi_symc_check_ccm_gcm_taglen(hi_cipher_alg alg, hi_cipher_work_mode work_mode, hi_u32 tlen)
497 {
498     hi_log_chk_param_return(alg != HI_CIPHER_ALG_AES);
499 
500     if (work_mode == HI_CIPHER_WORK_MODE_CCM) {
501         /* the parameter t denotes the octet length of T(tag)
502          * t is an element of  { 4, 6, 8, 10, 12, 14, 16}
503          * here t is pConfig->u32TagLen
504          */
505         if ((tlen & 0x01) || (tlen < AES_CCM_MIN_TAG_LEN) || (tlen > AES_CCM_MAX_TAG_LEN)) {
506             hi_log_error("Invalid ccm tag len, tlen = 0x%x.\n", tlen);
507             hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
508             return HI_ERR_CIPHER_INVALID_PARAM;
509         }
510     } else if (work_mode == HI_CIPHER_WORK_MODE_GCM) {
511         if ((tlen < AES_GCM_MIN_TAG_LEN) || (tlen > AES_GCM_MAX_TAG_LEN)) {
512             hi_log_error("Invalid gcm tag len, tlen = 0x%x.\n", tlen);
513             hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
514             return HI_ERR_CIPHER_INVALID_PARAM;
515         }
516     } else {
517         hi_log_error("Aes with invalid work mode 0x%x for check tag length.\n", work_mode);
518         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
519         return HI_ERR_CIPHER_INVALID_PARAM;
520     }
521 
522     return HI_SUCCESS;
523 }
524 
kapi_symc_chk_cfg(const symc_cfg_t * cfg,symc_set_cfg_param * set_cfg)525 static hi_s32 kapi_symc_chk_cfg(const symc_cfg_t *cfg, symc_set_cfg_param *set_cfg)
526 {
527     hi_s32 ret;
528     kapi_symc_ctx *ctx = HI_NULL;
529 
530     hi_log_chk_param_return(cfg == HI_NULL);
531     hi_log_chk_param_return(cfg->alen > MAX_AEAD_A_LEN);
532     hi_log_chk_param_return(addr_l32(cfg->aad) + cfg->alen < cfg->alen);
533 
534     ret = kapi_symc_chk_handle((hi_handle)cfg->id);
535     if (ret != HI_SUCCESS) {
536         hi_log_print_func_err(kapi_symc_chk_handle, ret);
537         return ret;
538     }
539 
540     set_cfg->soft_id = hi_handle_get_chnid(cfg->id);
541     ctx = &g_kapi_ctx[set_cfg->soft_id];
542     if (ctx == HI_NULL) {
543         hi_log_error("kapi symc ctx is null.\n");
544         return HI_ERR_CIPHER_INVALID_POINT;
545     }
546 
547     crypto_chk_owner_err_return(&ctx->owner);
548 
549     /*
550      * hard_key: bit[0~7]  flag of hard key or not
551      *           bit[8~31] ca type
552      */
553     set_cfg->byca = cfg->hard_key & 0xFF;
554     set_cfg->ca_type = cfg->hard_key >> BITS_IN_BYTE;
555 
556     ret = kapi_symc_chk_param(set_cfg->byca, cfg, &set_cfg->width);
557     if (ret != HI_SUCCESS) {
558         hi_log_error("hard_key 0x%x\n", cfg->hard_key);
559         hi_log_print_func_err(kapi_symc_chk_param, ret);
560         return ret;
561     }
562 
563     set_cfg->klen = (hi_u32)cfg->klen;
564 
565     return HI_SUCCESS;
566 }
567 
kapi_symc_set_alg_mode(const kapi_symc_ctx * ctx,const symc_set_cfg_param * set_cfg)568 static hi_void kapi_symc_set_alg_mode(const kapi_symc_ctx *ctx, const symc_set_cfg_param *set_cfg)
569 {
570     /* set mode and alg */
571     if (ctx->func->setmode) {
572         ctx->func->setmode(ctx->cryp_ctx, ctx->func->alg, ctx->func->mode, set_cfg->width);
573     }
574 }
575 
kapi_symc_set_key(const symc_cfg_t * cfg,const kapi_symc_ctx * ctx,symc_set_cfg_param * set_cfg)576 static hi_s32 kapi_symc_set_key(const symc_cfg_t *cfg,
577     const kapi_symc_ctx *ctx, symc_set_cfg_param *set_cfg)
578 {
579     hi_s32 ret;
580 
581     if (ctx->func->setkey == HI_NULL) {
582         return HI_SUCCESS;
583     }
584 
585     if (set_cfg->byca == HI_TRUE) {
586         ret = ctx->func->setkey(ctx->cryp_ctx, HI_NULL, HI_NULL, &set_cfg->klen);
587         if (ret != HI_SUCCESS) {
588             hi_log_print_func_err(ctx->func->setkey, ret);
589             return ret;
590         }
591 
592         if (cfg->klen == HI_CIPHER_KEY_AES_192BIT) {
593             set_cfg->klen = AES_KEY_256BIT;
594         }
595 
596         ret = klad_load_hard_key(cfg->id, set_cfg->ca_type, cfg->fkey, set_cfg->klen);
597         if (ret != HI_SUCCESS) {
598             hi_log_print_func_err(klad_load_hard_key, ret);
599             return ret;
600         }
601     } else {
602         ret = ctx->func->setkey(ctx->cryp_ctx, cfg->fkey, cfg->skey, &set_cfg->klen);
603         if (ret != HI_SUCCESS) {
604             hi_log_print_func_err(ctx->func->setkey, ret);
605             return ret;
606         }
607     }
608 
609     return HI_SUCCESS;
610 }
611 
kapi_symc_cpy_key_iv(const symc_cfg_t * cfg,kapi_symc_ctx * ctx,const symc_set_cfg_param * set_cfg)612 static hi_s32 kapi_symc_cpy_key_iv(const symc_cfg_t *cfg,
613     kapi_symc_ctx *ctx, const symc_set_cfg_param *set_cfg)
614 {
615     if (cfg->iv != HI_NULL) {
616         if (cfg->ivlen > AES_IV_SIZE) {
617             hi_log_error("Invalid iv len.\n");
618             return HI_ERR_CIPHER_INVALID_PARAM;
619         }
620 
621         if (memcpy_s(ctx->ctrl.iv, AES_IV_SIZE, cfg->iv, cfg->ivlen) != EOK) {
622             hi_log_print_func_err(memcpy_s, HI_ERR_CIPHER_MEMCPY_S_FAILED);
623             return HI_ERR_CIPHER_MEMCPY_S_FAILED;
624         }
625     }
626 
627     if (cfg->fkey != HI_NULL) {
628         if (set_cfg->klen > AES_KEY_256BIT) {
629             hi_log_error("Invalid key len.\n");
630             return HI_ERR_CIPHER_INVALID_PARAM;
631         }
632 
633         if (memcpy_s(ctx->ctrl.key, AES_KEY_256BIT, cfg->fkey, set_cfg->klen) != EOK) {
634             hi_log_print_func_err(memcpy_s, HI_ERR_CIPHER_MEMCPY_S_FAILED);
635             return HI_ERR_CIPHER_MEMCPY_S_FAILED;
636         }
637     }
638 
639     return HI_SUCCESS;
640 }
641 
kapi_symc_cfg_set_param(const symc_cfg_t * cfg,kapi_symc_ctx * ctx,symc_set_cfg_param * set_cfg)642 static hi_s32 kapi_symc_cfg_set_param(const symc_cfg_t *cfg, kapi_symc_ctx *ctx, symc_set_cfg_param *set_cfg)
643 {
644     hi_s32 ret;
645 
646     /* null means can ignore the function */
647     if (ctx->func->create) {
648         /* Create a instance from template of engine */
649         ctx->cryp_ctx = ctx->func->create(set_cfg->soft_id);
650         if (ctx->cryp_ctx == HI_NULL) {
651             hi_log_error("attach context buffer to soft_id %u failed\n", set_cfg->soft_id);
652             hi_log_print_func_err(ctx->func->create, HI_ERR_CIPHER_FAILED_MEM);
653             return HI_ERR_CIPHER_FAILED_MEM;
654         }
655     }
656 
657     /* set mode and alg */
658     kapi_symc_set_alg_mode(ctx, set_cfg);
659 
660     /* Set even key, may be also need set odd key */
661     crypto_chk_err_goto(kapi_symc_set_key(cfg, ctx, set_cfg));
662 
663     /* Set IV */
664     if (ctx->func->setiv) {
665         crypto_chk_err_goto(ctx->func->setiv(ctx->cryp_ctx, cfg->iv, cfg->ivlen, cfg->iv_usage));
666     }
667 
668     /* set sm1 round num */
669     if (ctx->func->setround) {
670         crypto_chk_err_goto(ctx->func->setround(ctx->cryp_ctx, cfg->sm1_round_num));
671     }
672 
673     /* Set AAD */
674     if (ctx->func->setadd) {
675         crypto_chk_err_goto(cipher_check_mmz_phy_addr((hi_phys_addr_t)addr_u64(cfg->aad), cfg->alen));
676         hi_log_info("set add, phy 0x%x, alen %u, tlen %u\n", addr_l32(cfg->aad), cfg->alen, cfg->tlen);
677         crypto_chk_err_goto(kapi_symc_check_ccm_gcm_taglen(cfg->alg, cfg->mode, cfg->tlen));
678         crypto_chk_err_goto(ctx->func->setadd(ctx->cryp_ctx, cfg->aad, cfg->alen, cfg->tlen));
679     }
680 
681     /* save ctrl */
682     (hi_void)memset_s(&ctx->ctrl, sizeof(hi_cipher_ctrl), 0, sizeof(hi_cipher_ctrl));
683     ctx->ctrl.key_by_ca = set_cfg->byca;
684     ctx->ctrl.alg = cfg->alg;
685     ctx->ctrl.bit_width = cfg->width;
686     ctx->ctrl.ca_type = set_cfg->ca_type;
687     ctx->ctrl.key_len = cfg->klen;
688     ctx->ctrl.work_mode = cfg->mode;
689     ctx->ctrl.chg_flags.bits_iv = cfg->iv_usage;
690 
691     crypto_chk_err_goto(kapi_symc_cpy_key_iv(cfg, ctx, set_cfg));
692     ctx->config = HI_TRUE;
693     return HI_SUCCESS;
694 exit__:
695     return ret;
696 }
697 
kapi_symc_cfg(const symc_cfg_t * cfg)698 hi_s32 kapi_symc_cfg(const symc_cfg_t *cfg)
699 {
700     hi_s32 ret;
701     kapi_symc_ctx *ctx = HI_NULL;
702     symc_set_cfg_param set_cfg;
703 
704     hi_log_func_enter();
705 
706     (hi_void)memset_s(&set_cfg, sizeof(set_cfg), 0, sizeof(set_cfg));
707 
708     ret = kapi_symc_chk_cfg(cfg, &set_cfg);
709     if (ret != HI_SUCCESS) {
710         hi_log_print_func_err(kapi_symc_chk_cfg, ret);
711         return ret;
712     }
713     ctx = &g_kapi_ctx[set_cfg.soft_id];
714 
715     kapi_symc_lock_err_return();
716 
717     /* Destroy the last attached instance of Symmetric cipher engine. */
718     if ((ctx->func != HI_NULL) && (ctx->func->destroy != HI_NULL)) {
719         (void)ctx->func->destroy(ctx->cryp_ctx);
720     }
721     ctx->cryp_ctx = HI_NULL;
722 
723     /* Clone the function from template of symc engine. */
724     ctx->func = cryp_get_symc_op(cfg->alg, cfg->mode);
725     if (ctx->func == HI_NULL) {
726         hi_log_error("error, get symc function failed, alg %d, work_mode %d\n", cfg->alg, cfg->mode);
727         hi_log_print_func_err(cryp_get_symc_op, ret);
728         kapi_symc_unlock();
729         return HI_ERR_CIPHER_INVALID_PARAM;
730     }
731 
732     ret = kapi_symc_cfg_set_param(cfg, ctx, &set_cfg);
733     if (ret != HI_SUCCESS) {
734         hi_log_print_func_err(klad_load_hard_key, ret);
735         kapi_symc_unlock();
736         return ret;
737     }
738 
739     kapi_symc_unlock();
740     hi_log_func_exit();
741     return HI_SUCCESS;
742 }
743 
kapi_symc_get_cfg(hi_u32 id,hi_cipher_ctrl * ctrl)744 hi_s32 kapi_symc_get_cfg(hi_u32 id, hi_cipher_ctrl *ctrl)
745 {
746     hi_s32 ret;
747     kapi_symc_ctx *ctx = HI_NULL;
748     hi_u32 soft_id;
749     hi_u32 iv_len = 0;
750 
751     hi_log_func_enter();
752 
753     hi_log_chk_param_return(ctrl == HI_NULL);
754 
755     ret = kapi_symc_chk_handle((hi_handle)id);
756     if (ret != HI_SUCCESS) {
757         hi_log_print_func_err(kapi_symc_chk_handle, ret);
758         return ret;
759     }
760 
761     soft_id = hi_handle_get_chnid(id);
762     ctx = &g_kapi_ctx[soft_id];
763     crypto_chk_owner_err_return(&ctx->owner);
764     hi_log_chk_param_return(ctx->config != HI_TRUE);
765 
766     kapi_symc_lock_err_return();
767 
768     if (ctx->func->getiv != HI_NULL) {
769         ctx->func->getiv(ctx->cryp_ctx, (hi_u8 *)ctrl->iv, &iv_len);
770     }
771 
772     if (memcpy_s(ctrl, sizeof(hi_cipher_ctrl), &ctx->ctrl, sizeof(hi_cipher_ctrl)) != EOK) {
773         kapi_symc_unlock();
774         hi_log_print_func_err(memcpy_s, HI_ERR_CIPHER_MEMCPY_S_FAILED);
775         return HI_ERR_CIPHER_MEMCPY_S_FAILED;
776     }
777     (hi_void)memset_s(ctrl->key, sizeof(ctrl->key), 0, sizeof(ctrl->key));
778 
779     kapi_symc_unlock();
780 
781     hi_log_func_exit();
782 
783     return ret;
784 }
785 
kapi_symc_crypto(symc_encrypt_t * crypt)786 hi_s32 kapi_symc_crypto(symc_encrypt_t *crypt)
787 {
788     hi_s32 ret;
789     symc_node_usage usage;
790     kapi_symc_ctx *ctx = HI_NULL;
791     hi_u32 soft_id;
792     symc_multi_pack pack;
793 
794     hi_log_func_enter();
795     hi_log_chk_param_return(crypt == HI_NULL);
796     ret = kapi_symc_chk_handle((hi_handle)crypt->id);
797     if (ret != HI_SUCCESS) {
798         hi_log_print_func_err(kapi_symc_chk_handle, ret);
799         return ret;
800     }
801 
802     soft_id = hi_handle_get_chnid(crypt->id);
803     ctx = &g_kapi_ctx[soft_id];
804     crypto_chk_owner_err_return(&ctx->owner);
805     hi_log_chk_param_return(addr_u64(crypt->in) + crypt->len < crypt->len);
806     hi_log_chk_param_return(addr_u64(crypt->out) + crypt->len < crypt->len);
807     hi_log_chk_param_return(ctx->func == HI_NULL);
808     hi_log_chk_param_return(ctx->func->crypto == HI_NULL);
809     hi_log_chk_param_return(ctx->config != HI_TRUE);
810     hi_log_chk_param_return((crypt->operation != SYMC_OPERATION_ENCRYPT) &&
811         (crypt->operation != SYMC_OPERATION_DECRYPT));
812 
813     hi_log_info("src/dest phyaddr information.\n");
814     hi_dbg_print_u32(crypt->operation);
815     hi_dbg_print_h32(addr_l32(crypt->in));
816     hi_dbg_print_h32(addr_l32(crypt->out));
817     hi_dbg_print_h32(crypt->len);
818 
819     usage = SYMC_NODE_USAGE_NORMAL;
820     (hi_void)memset_s(&pack, sizeof(pack), 0, sizeof(pack));
821     pack.in = &crypt->in;
822     pack.out = &crypt->out;
823     pack.len = &crypt->len;
824     pack.usage = &usage;
825     pack.num = 1; /* 1 single package encrypt or decrypt. */
826 
827     kapi_symc_lock_err_return();
828 
829     ret = ctx->func->crypto(ctx->cryp_ctx, crypt->operation, &pack, HI_TRUE);
830     if (ret != HI_SUCCESS) {
831         hi_log_print_func_err(ctx->func->crypto, ret);
832         kapi_symc_unlock();
833         return ret;
834     }
835 
836     kapi_symc_unlock();
837     hi_log_func_exit();
838     return HI_SUCCESS;
839 }
840 
kapi_symc_crypto_via(symc_encrypt_t * crypt,hi_u32 is_from_user)841 hi_s32 kapi_symc_crypto_via(symc_encrypt_t *crypt, hi_u32 is_from_user)
842 {
843     hi_s32 ret, ret_exit;
844     crypto_mem mem = {0};
845     symc_encrypt_t local_crypt;
846 
847     hi_log_func_enter();
848 
849     hi_log_chk_param_return(crypt == HI_NULL);
850     hi_log_chk_param_return(addr_via(crypt->in) == HI_NULL);
851     hi_log_chk_param_return(addr_via(crypt->out) == HI_NULL);
852     hi_log_chk_param_return(crypt->len == 0x00);
853 
854     (hi_void)memset_s(&local_crypt, sizeof(local_crypt), 0, sizeof(local_crypt));
855 
856     ret = crypto_mem_create(&mem, SEC_MMZ, "AES_IN", crypt->len);
857     if (ret != HI_SUCCESS) {
858         hi_log_print_func_err(crypto_mem_create, ret);
859         return ret;
860     }
861 
862     if (is_from_user == HI_TRUE) {
863         crypto_chk_err_goto(crypto_copy_from_user(mem.dma_virt, crypt->len, addr_via(crypt->in), crypt->len));
864     } else {
865         if (memcpy_s(mem.dma_virt, crypt->len, addr_via(crypt->in), crypt->len) != EOK) {
866             hi_log_print_func_err(memcpy_s, HI_ERR_CIPHER_MEMCPY_S_FAILED);
867             ret = HI_ERR_CIPHER_MEMCPY_S_FAILED;
868             goto exit__;
869         }
870     }
871 
872     local_crypt.id   = crypt->id;
873     local_crypt.in   = mem.dma_addr;
874     local_crypt.out  = mem.dma_addr;
875     local_crypt.len  = crypt->len;
876     local_crypt.last = crypt->last;
877     local_crypt.operation = crypt->operation & SYMC_OPERATION_DECRYPT;
878     crypto_chk_err_goto(kapi_symc_crypto(&local_crypt));
879 
880     if (is_from_user == HI_TRUE) {
881         crypto_chk_err_goto(crypto_copy_to_user(addr_via(crypt->out), crypt->len, mem.dma_virt, crypt->len));
882     } else {
883         if (memcpy_s(addr_via(crypt->out), crypt->len, mem.dma_virt, crypt->len) != EOK) {
884             hi_log_print_func_err(memcpy_s, HI_ERR_CIPHER_MEMCPY_S_FAILED);
885             ret = HI_ERR_CIPHER_MEMCPY_S_FAILED;
886             goto exit__;
887         }
888     }
889 
890 exit__:
891     ret_exit = crypto_mem_destroy(&mem);
892     if (ret_exit != HI_SUCCESS) {
893         hi_log_print_func_err(crypto_mem_destroy, ret_exit);
894         return ret_exit;
895     }
896 
897     hi_log_func_exit();
898     return ret;
899 }
900 
kapi_symc_chk_multi_pack(hi_cipher_data * tmp,const hi_cipher_data * pack)901 static hi_s32 kapi_symc_chk_multi_pack(hi_cipher_data *tmp, const hi_cipher_data *pack)
902 {
903     hi_s32 ret;
904 
905     /* copy node list from user space to kernel. */
906     ret = crypto_copy_from_user(tmp, sizeof(hi_cipher_data), pack, sizeof(hi_cipher_data));
907     if (ret != HI_SUCCESS) {
908         hi_log_print_func_err(crypto_copy_from_user, ret);
909         return ret;
910     }
911 
912     if (tmp->src_phys_addr + tmp->byte_len < tmp->byte_len) {
913         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
914         return HI_ERR_CIPHER_INVALID_PARAM;
915     }
916 
917     if (tmp->dst_phys_addr + tmp->byte_len < tmp->byte_len) {
918         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
919         return HI_ERR_CIPHER_INVALID_PARAM;
920     }
921 
922     if ((tmp->odd_key != HI_TRUE) && (tmp->odd_key != HI_FALSE)) {
923         hi_log_error("invalid odd key for multicipher crypt!\n");
924         hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM);
925         return HI_ERR_CIPHER_INVALID_PARAM;
926     }
927 
928     ret = cipher_check_mmz_phy_addr(tmp->src_phys_addr, tmp->byte_len);
929     if (ret != HI_SUCCESS) {
930         hi_log_print_func_err(cipher_check_mmz_phy_addr, ret);
931         return ret;
932     }
933 
934     ret = cipher_check_mmz_phy_addr(tmp->dst_phys_addr, tmp->byte_len);
935     if (ret != HI_SUCCESS) {
936         hi_log_print_func_err(cipher_check_mmz_phy_addr, ret);
937         return ret;
938     }
939 
940     return HI_SUCCESS;
941 }
942 
kapi_symc_multi_pack_set_mem(symc_multi_pack * pack,const hi_u8 * buf,hi_u32 size)943 static hi_void kapi_symc_multi_pack_set_mem(symc_multi_pack *pack, const hi_u8 *buf, hi_u32 size)
944 {
945     const hi_u8 *tmp = HI_NULL;
946     hi_u32 pack_num = pack->num;
947 
948     crypto_unused(size);
949 
950     tmp = buf;
951     pack->in = (compat_addr *)tmp;
952 
953     tmp = (hi_u8 *)tmp + sizeof(compat_addr) * pack_num;    /* descrypt: buf + input. */
954     pack->out = (compat_addr *)tmp;
955 
956     tmp = (hi_u8 *)tmp + sizeof(compat_addr) * pack_num;    /* descrypt: buf + input + output. */
957     pack->usage = (symc_node_usage *)tmp;
958 
959     tmp = (hi_u8 *)tmp + sizeof(symc_node_usage) * pack_num;    /* descrypt: buf + input + output + usage. */
960     pack->len = (hi_u32 *)tmp;
961 }
962 
kapi_symc_crypto_multi_start(const kapi_symc_ctx * ctx,const hi_cipher_data * pkg,hi_u32 pkg_num,hi_u32 operation,hi_u32 wait)963 static hi_s32 kapi_symc_crypto_multi_start(const kapi_symc_ctx *ctx,
964     const hi_cipher_data *pkg, hi_u32 pkg_num, hi_u32 operation, hi_u32 wait)
965 {
966     hi_s32 ret;
967     hi_void *buf = HI_NULL;
968     hi_cipher_data pkg_tmp;
969     hi_u32 i, size;
970     symc_multi_pack pack;
971 
972     hi_log_func_enter();
973 
974     hi_log_chk_param_return((ctx == HI_NULL) || (ctx->cryp_ctx == HI_NULL) ||
975         (ctx->func == HI_NULL) || (ctx->func->crypto == HI_NULL));
976     hi_log_chk_param_return((pkg == HI_NULL) || (pkg_num > MAX_PKG_NUMBER) || (pkg_num == 0x00));
977 
978     /* size of input:output:usage:length */
979     size = (sizeof(compat_addr) + sizeof(compat_addr) + sizeof(symc_node_usage) + sizeof(hi_u32)) * pkg_num;
980     buf = crypto_calloc(1, size);
981     if (buf == HI_NULL) {
982         hi_log_print_func_err(crypto_calloc, HI_ERR_CIPHER_FAILED_MEM);
983         return HI_ERR_CIPHER_FAILED_MEM;
984     }
985 
986     (hi_void)memset_s(&pack, sizeof(pack), 0, sizeof(pack));
987 
988     pack.num = pkg_num;
989     kapi_symc_multi_pack_set_mem(&pack, buf, size);
990 
991     /* Compute and check the nodes length. */
992     for (i = 0; i < pkg_num; i++) {
993         ret = kapi_symc_chk_multi_pack(&pkg_tmp, (hi_cipher_data *)((hi_u8 *)pkg + sizeof(hi_cipher_data) * i));
994         if (ret != HI_SUCCESS) {
995             hi_log_print_func_err(kapi_symc_chk_multi_pack, ret);
996             crypto_free(buf);
997             buf = HI_NULL;
998             return ret;
999         }
1000 
1001         addr_u64(pack.in[i]) = pkg_tmp.src_phys_addr;
1002         addr_u64(pack.out[i]) = pkg_tmp.dst_phys_addr;
1003         pack.len[i] = pkg_tmp.byte_len;
1004         pack.usage[i] = SYMC_NODE_USAGE_EVEN_KEY;
1005 
1006         hi_log_debug("pkg %u, in 0x%x, out 0x%x, length 0x%x, usage 0x%x\n", i, addr_l32(pack.in[i]),
1007             addr_l32(pack.out[i]), pack.len[i], pack.usage[i]);
1008     }
1009 
1010     ret = ctx->func->crypto(ctx->cryp_ctx, operation, &pack, wait);
1011     if (ret != HI_SUCCESS) {
1012         hi_log_print_func_err(ctx->func->crypto, ret);
1013         crypto_free(buf);
1014         buf = HI_NULL;
1015         return ret;
1016     }
1017 
1018     crypto_free(buf);
1019     buf = HI_NULL;
1020 
1021     hi_log_func_exit();
1022     return HI_SUCCESS;
1023 }
1024 
kapi_symc_crypto_multi(hi_u32 id,const hi_cipher_data * pkg,hi_u32 pkg_num,hi_u32 operation,hi_u32 last)1025 hi_s32 kapi_symc_crypto_multi(hi_u32 id, const hi_cipher_data *pkg, hi_u32 pkg_num, hi_u32 operation, hi_u32 last)
1026 {
1027     hi_s32 ret;
1028     kapi_symc_ctx *ctx = HI_NULL;
1029     hi_u32 soft_id;
1030 
1031     crypto_unused(last);
1032 
1033     hi_log_func_enter();
1034 
1035     ret = kapi_symc_chk_handle((hi_handle)id);
1036     if (ret != HI_SUCCESS) {
1037         hi_log_print_func_err(kapi_symc_chk_handle, ret);
1038         return ret;
1039     }
1040 
1041     soft_id = hi_handle_get_chnid(id);
1042     ctx = &g_kapi_ctx[soft_id];
1043     crypto_chk_owner_err_return(&ctx->owner);
1044     hi_log_chk_param_return(ctx->config != HI_TRUE);
1045     hi_log_chk_param_return((operation != 0x00) && (operation != 0x01));
1046 
1047     kapi_symc_lock_err_return();
1048 
1049     ret = kapi_symc_crypto_multi_start(ctx, pkg, pkg_num, operation, HI_TRUE);
1050     if (ret != HI_SUCCESS) {
1051         hi_log_print_func_err(kapi_symc_crypto_multi_start, ret);
1052         kapi_symc_unlock();
1053         return ret;
1054     }
1055 
1056     kapi_symc_unlock();
1057 
1058     hi_log_func_exit();
1059     return HI_SUCCESS;
1060 }
1061 
kapi_aead_get_tag(hi_u32 id,hi_u32 tag[AEAD_TAG_SIZE_IN_WORD],hi_u32 * taglen)1062 hi_s32 kapi_aead_get_tag(hi_u32 id, hi_u32 tag[AEAD_TAG_SIZE_IN_WORD], hi_u32 *taglen)
1063 {
1064     hi_s32 ret;
1065     kapi_symc_ctx *ctx = HI_NULL;
1066     hi_u32 soft_id;
1067 
1068     hi_log_func_enter();
1069 
1070     hi_log_chk_param_return(tag == HI_NULL);
1071     hi_log_chk_param_return(taglen == HI_NULL);
1072     hi_log_chk_param_return(*taglen != AES_CCM_MAX_TAG_LEN);
1073 
1074     ret = kapi_symc_chk_handle((hi_handle)id);
1075     if (ret != HI_SUCCESS) {
1076         hi_log_print_func_err(kapi_symc_chk_handle, ret);
1077         return ret;
1078     }
1079 
1080     soft_id = hi_handle_get_chnid(id);
1081     ctx = &g_kapi_ctx[soft_id];
1082     crypto_chk_owner_err_return(&ctx->owner);
1083     hi_log_chk_param_return(ctx->func == HI_NULL);
1084     hi_log_chk_param_return(ctx->func->gettag == HI_NULL);
1085 
1086     kapi_symc_lock_err_return();
1087 
1088     if (ctx->func->gettag) {
1089         ret = ctx->func->gettag(ctx->cryp_ctx, tag, taglen);
1090         if (ret != HI_SUCCESS) {
1091             hi_log_print_func_err(cryp_aead_get_tag, ret);
1092             kapi_symc_unlock();
1093             return ret;
1094         }
1095     }
1096 
1097     kapi_symc_unlock();
1098 
1099     hi_log_func_exit();
1100     return HI_SUCCESS;
1101 }
1102 
kapi_symc_klad_encrypt_key(hi_u32 keysel,hi_u32 target,hi_u8 * clear,hi_u8 * encrypt,hi_u32 key_len)1103 hi_s32 kapi_symc_klad_encrypt_key(hi_u32 keysel,
1104     hi_u32 target, hi_u8 *clear, hi_u8 *encrypt, hi_u32 key_len)
1105 {
1106     hi_s32 ret;
1107 
1108     hi_log_func_enter();
1109 
1110     kapi_symc_lock_err_return();
1111 
1112     ret = klad_encrypt_key(keysel, target, clear, encrypt, key_len);
1113     if (ret != HI_SUCCESS) {
1114         hi_log_print_func_err(klad_encrypt_key, ret);
1115         kapi_symc_unlock();
1116         return ret;
1117     }
1118 
1119     kapi_symc_unlock();
1120 
1121     hi_log_func_exit();
1122 
1123     return HI_SUCCESS;
1124 }
1125 
1126