• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HITLS_CRYPTO_PROVIDER
18 
19 #include "crypt_eal_implprovider.h"
20 #include "crypt_drbg.h"
21 #include "bsl_sal.h"
22 #include "crypt_errno.h"
23 #include "bsl_log_internal.h"
24 #include "bsl_err_internal.h"
25 #include "crypt_ealinit.h"
26 #include "bsl_params.h"
27 #include "crypt_default_provider.h"
28 
29 #ifdef HITLS_CRYPTO_ENTROPY
GetDefaultSeed(BSL_Param * param)30 static int32_t GetDefaultSeed(BSL_Param *param)
31 {
32     void *defaultSeedCtx = NULL;
33     CRYPT_RandSeedMethod *defaultSeedMethod = NULL;
34     int32_t ret = CRYPT_EAL_ProviderGetSeed(&defaultSeedMethod, &defaultSeedCtx);
35     if (ret != CRYPT_SUCCESS) {
36         BSL_ERR_PUSH_ERROR(ret);
37         return ret;
38     }
39     (void)BSL_PARAM_InitValue(&param[0], CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, defaultSeedCtx, 0);
40     (void)BSL_PARAM_InitValue(&param[1], CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR,
41         defaultSeedMethod->getEntropy, 0);
42     (void)BSL_PARAM_InitValue(&param[2], CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR,
43         defaultSeedMethod->cleanEntropy, 0);
44     (void)BSL_PARAM_InitValue(&param[3], CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR,
45         defaultSeedMethod->getNonce, 0);
46     (void)BSL_PARAM_InitValue(&param[4], CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR,
47         defaultSeedMethod->cleanNonce, 0);
48     return CRYPT_SUCCESS;
49 }
50 #endif
51 
CRYPT_EAL_DefRandNewCtx(void * provCtx,int32_t algId,BSL_Param * param)52 void *CRYPT_EAL_DefRandNewCtx(void *provCtx, int32_t algId, BSL_Param *param)
53 {
54     (void) provCtx;
55     void *randCtx = NULL;
56 #ifdef HITLS_CRYPTO_ASM_CHECK
57     if (CRYPT_ASMCAP_Drbg(algId) != CRYPT_SUCCESS) {
58         BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT);
59         return NULL;
60     }
61 #endif
62     BSL_Param *getEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETENTROPY);
63     BSL_Param *cleanEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_CLEANENTROPY);
64     BSL_Param *getNonce = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETNONCE);
65     BSL_Param *cleanNonce = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_CLEANNONCE);
66     BSL_Param *ctx = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEEDCTX);
67     /**
68      * If you use a registered entropy source, the getEntropy callback cannot be NULL,
69      * and if getEntropy is NULL, cleanEntropy, getNonce, cleanNonce, etc. must be NULL
70      */
71     if (getEnt == NULL && ((cleanEnt != NULL && cleanEnt->value != NULL) ||
72         (getNonce != NULL && getNonce->value != NULL) || (cleanNonce != NULL && cleanNonce->value != NULL) ||
73         (ctx != NULL && ctx->value != NULL))) {
74         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
75         return NULL;
76     }
77     if (param == NULL || getEnt == NULL) {
78 #ifdef HITLS_CRYPTO_ENTROPY
79         BSL_Param defaultParam[6] = {BSL_PARAM_END};
80         if (GetDefaultSeed(defaultParam) != CRYPT_SUCCESS) {
81             BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
82             return NULL;
83         }
84         return DRBG_New(algId, defaultParam);
85 #else
86         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
87         return NULL;
88 #endif
89     }
90     randCtx = DRBG_New(algId, param);
91     if (randCtx == NULL) {
92         BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT);
93         return NULL;
94     }
95     return randCtx;
96 }
97 
98 const CRYPT_EAL_Func g_defEalRand[] = {
99 #if defined(HITLS_CRYPTO_DRBG)
100     {CRYPT_EAL_IMPLRAND_DRBGNEWCTX, (CRYPT_EAL_ImplRandDrbgNewCtx)CRYPT_EAL_DefRandNewCtx},
101     {CRYPT_EAL_IMPLRAND_DRBGINST, (CRYPT_EAL_ImplRandDrbgInst)DRBG_Instantiate},
102     {CRYPT_EAL_IMPLRAND_DRBGUNINST, (CRYPT_EAL_ImplRandDrbgUnInst)DRBG_Uninstantiate},
103     {CRYPT_EAL_IMPLRAND_DRBGGEN, (CRYPT_EAL_ImplRandDrbgGen)DRBG_GenerateBytes},
104     {CRYPT_EAL_IMPLRAND_DRBGRESEED, (CRYPT_EAL_ImplRandDrbgReSeed)DRBG_Reseed},
105     {CRYPT_EAL_IMPLRAND_DRBGCTRL, (CRYPT_EAL_ImplRandDrbgCtrl)DRBG_Ctrl},
106     {CRYPT_EAL_IMPLRAND_DRBGFREECTX, (CRYPT_EAL_ImplRandDrbgFreeCtx)DRBG_Free},
107 #endif
108     CRYPT_EAL_FUNC_END,
109 };
110 
111 #endif /* HITLS_CRYPTO_PROVIDER */