1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: cipher driver trng. \n
16 *
17 * History: \n
18 * 2023-03-22, Create file. \n
19 */
20 #include "drv_trng.h"
21 #include "drv_inner.h"
22
23 #include "hal_cipher_trng.h"
24 #include "crypto_drv_common.h"
25 #include "crypto_common_macro.h"
26
27 #define TRNG_COMPAT_ERRNO(err_code) DRV_COMPAT_ERRNO(ERROR_MODULE_TRNG, err_code)
28 #define TRNG_ONCE_WIDTH_IN_BYTE 4
29
30 static td_bool g_drv_trng_init_flag = TD_FALSE;
31
drv_cipher_trng_init(td_void)32 td_s32 drv_cipher_trng_init(td_void)
33 {
34 td_s32 ret;
35 if (g_drv_trng_init_flag == TD_TRUE) {
36 return TD_SUCCESS;
37 }
38 ret = hal_cipher_trng_init();
39 g_drv_trng_init_flag = TD_TRUE;
40
41 return ret;
42 }
43
drv_cipher_trng_get_random(td_u32 * randnum)44 td_s32 drv_cipher_trng_get_random(td_u32 *randnum)
45 {
46 crypto_chk_return(g_drv_trng_init_flag == TD_FALSE, TRNG_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first!\n");
47 crypto_chk_return(randnum == TD_NULL, TRNG_COMPAT_ERRNO(ERROR_PARAM_IS_NULL), "randnum is NULL\n");
48 return hal_cipher_trng_get_random(randnum);
49 }
50
drv_cipher_trng_get_multi_random(td_u32 size,td_u8 * randnum)51 td_s32 drv_cipher_trng_get_multi_random(td_u32 size, td_u8 *randnum)
52 {
53 td_s32 ret = TD_FAILURE;
54 td_u32 cnt;
55 td_u32 i;
56 td_u32 randnum_once = 0;
57 td_u32 tail = 0;
58 crypto_chk_return(g_drv_trng_init_flag == TD_FALSE, TRNG_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first!\n");
59 crypto_chk_return(randnum == TD_NULL, TRNG_COMPAT_ERRNO(ERROR_PARAM_IS_NULL), "randnum is NULL\n");
60 cnt = size / TRNG_ONCE_WIDTH_IN_BYTE;
61 for (i = 0; i < cnt; i++) {
62 ret = hal_cipher_trng_get_random(&randnum_once);
63 crypto_chk_return(ret != TD_SUCCESS, ret, "ERROR! hal_cipher_trng_get_random failed!\n");
64 ret = memcpy_s(randnum + i * TRNG_ONCE_WIDTH_IN_BYTE, size - i * TRNG_ONCE_WIDTH_IN_BYTE,
65 &randnum_once, TRNG_ONCE_WIDTH_IN_BYTE);
66 crypto_chk_return(ret != TD_SUCCESS, ret, "memcpy_s failed!\n");
67 }
68 /* less then 4 byte */
69 tail = size - cnt * TRNG_ONCE_WIDTH_IN_BYTE;
70 if (tail != 0) {
71 ret = hal_cipher_trng_get_random(&randnum_once);
72 crypto_chk_return(ret != TD_SUCCESS, ret, "ERROR! hal_cipher_trng_get_random failed!\n");
73 ret = memcpy_s(randnum + cnt * TRNG_ONCE_WIDTH_IN_BYTE, tail, &randnum_once, tail);
74 crypto_chk_return(ret != TD_SUCCESS, ret, "memcpy_s failed!\n");
75 }
76 return ret;
77 }
78
drv_cipher_trng_deinit(td_void)79 td_s32 drv_cipher_trng_deinit(td_void)
80 {
81 if (g_drv_trng_init_flag == TD_FALSE) {
82 return TD_SUCCESS;
83 }
84 g_drv_trng_init_flag = TD_FALSE;
85 return hal_cipher_trng_deinit();
86 }