1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #if defined(HITLS_CRYPTO_EAL) && defined(HITLS_CRYPTO_DRBG)
18
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include <securec.h>
22 #include "crypt_eal_rand.h"
23 #include "crypt_errno.h"
24 #include "bsl_errno.h"
25 #include "bsl_err_internal.h"
26 #include "bsl_sal.h"
27 #include "eal_common.h"
28 #include "crypt_types.h"
29 #include "crypt_local_types.h"
30 #include "crypt_algid.h"
31 #include "crypt_drbg.h"
32 #include "crypt_drbg_local.h"
33 #ifdef HITLS_CRYPTO_MD
34 #include "eal_md_local.h"
35 #endif
36 #ifdef HITLS_CRYPTO_MAC
37 #include "eal_mac_local.h"
38 #endif
39 #ifdef HITLS_CRYPTO_CIPHER
40 #include "eal_cipher_local.h"
41 #endif
42
43 static EAL_RandUnitaryMethod g_randMethod = {
44 .newCtx = (RandNewCtx)DRBG_New,
45 .inst = (RandDrbgInst)DRBG_Instantiate,
46 .unInst = (RandDrbgUnInst)DRBG_Uninstantiate,
47 .gen = (RandDrbgGen)DRBG_GenerateBytes,
48 .reSeed = (RandDrbgReSeed)DRBG_Reseed,
49 .ctrl = (RandDrbgCtrl)DRBG_Ctrl,
50 .freeCtx = (RandDrbgFreeCtx)DRBG_Free,
51 };
52
EAL_RandGetMethod(void)53 EAL_RandUnitaryMethod* EAL_RandGetMethod(void)
54 {
55 return &g_randMethod;
56 }
57
GetRequiredMethod(const DrbgIdMap * map,EAL_RandMethLookup * lu)58 static int32_t GetRequiredMethod(const DrbgIdMap *map, EAL_RandMethLookup *lu)
59 {
60 switch (map->type) {
61 #ifdef HITLS_CRYPTO_DRBG_HASH
62 case RAND_TYPE_MD: {
63 const EAL_MdMethod *md = EAL_MdFindMethod(map->depId);
64 if (md == NULL) {
65 BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
66 return CRYPT_EAL_ERR_ALGID;
67 }
68 lu->methodId = map->depId;
69 lu->method = md;
70 break;
71 }
72 #endif
73 #ifdef HITLS_CRYPTO_DRBG_HMAC
74 case RAND_TYPE_MAC: {
75 EAL_MacMethLookup hmac;
76 int32_t ret = EAL_MacFindMethod(map->depId, &hmac);
77 if (ret != CRYPT_SUCCESS) {
78 BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
79 return CRYPT_EAL_ERR_ALGID;
80 }
81 lu->methodId = map->depId;
82 lu->method = hmac.macMethod;
83 break;
84 }
85 #endif
86 #ifdef HITLS_CRYPTO_DRBG_CTR
87 case RAND_TYPE_SM4_DF:
88 case RAND_TYPE_AES:
89 case RAND_TYPE_AES_DF: {
90 const EAL_SymMethod *ciphMeth = EAL_GetSymMethod(map->depId);
91 if (ciphMeth == NULL) {
92 BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
93 return CRYPT_EAL_ERR_ALGID;
94 }
95 lu->methodId = map->depId;
96 lu->method = ciphMeth;
97 break;
98 }
99 #endif
100 default:
101 BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
102 return CRYPT_EAL_ERR_ALGID;
103 }
104 return CRYPT_SUCCESS;
105 }
106
EAL_RandFindMethod(CRYPT_RAND_AlgId id,EAL_RandMethLookup * lu)107 int32_t EAL_RandFindMethod(CRYPT_RAND_AlgId id, EAL_RandMethLookup *lu)
108 {
109 if (lu == NULL) {
110 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
111 return CRYPT_NULL_INPUT;
112 }
113
114 const DrbgIdMap *map = DRBG_GetIdMap(id);
115 if (map == NULL) {
116 BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
117 return CRYPT_EAL_ERR_ALGID;
118 }
119
120 int32_t ret = GetRequiredMethod(map, lu);
121 if (ret != CRYPT_SUCCESS) {
122 return ret;
123 }
124
125 lu->type = map->type;
126 return CRYPT_SUCCESS;
127 }
128 #endif
129