• 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 #if defined(HITLS_CRYPTO_EAL) && defined(HITLS_CRYPTO_ENTROPY)
18 
19 #include "securec.h"
20 #include "bsl_err_internal.h"
21 #include "bsl_sal.h"
22 #include "crypt_errno.h"
23 #include "eal_entropy.h"
24 #include "eal_common.h"
25 #include "crypt_eal_mac.h"
26 #include "crypt_eal_md.h"
27 
28 #ifdef HITLS_CRYPTO_MAC
29 #define ECF_ALG_KEY_LEN 16
30 
ECFMac(uint32_t algId,uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)31 static int32_t ECFMac(uint32_t algId, uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
32 {
33     CRYPT_EAL_MacCtx *ctx = CRYPT_EAL_MacNewCtx(algId);
34     if (ctx == NULL) {
35         BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL);
36         return CRYPT_ENTROPY_CONDITION_FAILURE;
37     }
38     uint32_t keyLen = ECF_ALG_KEY_LEN;
39     uint8_t *ecfKey = (uint8_t *)BSL_SAL_Malloc(keyLen);
40     if (ecfKey == NULL) {
41         CRYPT_EAL_MacFreeCtx(ctx);
42         BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL);
43         return CRYPT_MEM_ALLOC_FAIL;
44     }
45     /* reference nist-800 90c-3pd section 3.3.1.1
46      * Unlike other cryptographic applications, keys used in these external conditioning functions do not require
47      * secrecy to accomplish their purpose so may be hard-coded, fixed, or all zeros.
48      */
49     (void)memset_s(ecfKey, keyLen, 0, keyLen);
50     int32_t ret = CRYPT_EAL_MacInit(ctx, ecfKey, keyLen);
51     if (ret != CRYPT_SUCCESS) {
52         CRYPT_EAL_MacFreeCtx(ctx);
53         BSL_SAL_FREE(ecfKey);
54         return ret;
55     }
56     ret = CRYPT_EAL_MacUpdate(ctx, in, inLen);
57     if (ret != CRYPT_SUCCESS) {
58         CRYPT_EAL_MacFreeCtx(ctx);
59         BSL_SAL_FREE(ecfKey);
60         return ret;
61     }
62     ret = CRYPT_EAL_MacFinal(ctx, out, outLen);
63     CRYPT_EAL_MacFreeCtx(ctx);
64     BSL_SAL_FREE(ecfKey);
65     return ret;
66 }
67 #endif
68 
EAL_EntropyGetECF(uint32_t algId)69 ExternalConditioningFunction EAL_EntropyGetECF(uint32_t algId)
70 {
71     (void)algId;
72 #ifdef HITLS_CRYPTO_MAC
73     return ECFMac;
74 #else
75     return NULL;
76 #endif
77 }
78 #endif
79