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 #ifndef EAL_ENTROPY_H 17 #define EAL_ENTROPY_H 18 19 #include "hitls_build.h" 20 #if defined(HITLS_CRYPTO_EAL) && defined(HITLS_CRYPTO_ENTROPY) 21 22 #include "crypt_eal_entropy.h" 23 #include "bsl_sal.h" 24 #include "crypt_entropy.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif // __cplusplus 29 30 #ifdef HITLS_CRYPTO_ENTROPY_SYS 31 struct CryptEalEntropySource { 32 ENTROPY_EntropySource *es; 33 BSL_SAL_ThreadLockHandle lock; // thread lock 34 }; 35 #endif 36 37 typedef struct { 38 /* whether non-physical entropy sources are allowed. */ 39 bool isNpesUsed; 40 /* whether a full-entropy bit string is required. */ 41 bool isNeedFe; 42 /* the minimum length of entropy data. */ 43 uint32_t minLen; 44 /* the maximum length of entropy data. */ 45 uint32_t maxLen; 46 /* the amount of entropy required. */ 47 uint32_t requestEntropy; 48 /* the current amount of entropy. */ 49 uint32_t curEntropy; 50 /* external conditioning function algorithm */ 51 int32_t ecfuncId; 52 int32_t ecfOutLen; 53 /* external conditioning function */ 54 ExternalConditioningFunction ecfunc; 55 /* the length of the existing entropy data */ 56 uint32_t curLen; 57 /* the length of entropy buffer. */ 58 uint32_t bufLen; 59 /* the buffer of entropy buffer. */ 60 uint8_t *buf; 61 } EAL_EntropyCtx; 62 63 struct EAL_SeedPool { 64 CRYPT_EAL_Es *es; 65 void *pool; 66 BSL_SAL_ThreadLockHandle lock; // thread lock 67 }; 68 69 /* 70 * @brief Creating an Entropy Source Application Handle. 71 * 72 * @param seedPool[IN] seed pool handle 73 * @param isNpesUsed[IN] whether non-physical entropy sources are allowed 74 * @param minLen[IN] the minimum length of entropy data 75 * @param maxLen[IN] the maximum length of entropy data 76 * @param entropy[IN] the amount of entropy required 77 * @return entropy context 78 */ 79 EAL_EntropyCtx *EAL_EntropyNewCtx(CRYPT_EAL_SeedPoolCtx *seedPool, uint8_t isNpesUsed, uint32_t minLen, 80 uint32_t maxLen, uint32_t entropy); 81 82 /* 83 * @brief Release an Entropy Source Application Handle. 84 * 85 * @param ctx[IN] seed pool handle 86 * @return void 87 */ 88 void EAL_EntropyFreeCtx(EAL_EntropyCtx *ctx); 89 90 /* 91 * @brief collect entropy data. 92 * 93 * @param seedPool[IN] seed pool handle 94 * @param ctx[IN] entropy source application Handle 95 * @return success: CRYPT_SUCCESS 96 * failed: other error codes 97 */ 98 int32_t EAL_EntropyCollection(CRYPT_EAL_SeedPoolCtx *seedPool, EAL_EntropyCtx *ctx); 99 100 /* 101 * @brief pop entropy data. 102 * 103 * @param seedPool[IN] seed pool handle 104 * @param ctx[IN] entropy source application Handle 105 * @param len[OUT] entropy buf length 106 * @return success: buffer 107 * failed: NULL 108 */ 109 uint8_t *EAL_EntropyDetachBuf(EAL_EntropyCtx *ctx, uint32_t *len); 110 111 /** 112 * @brief Set the random number method that uses the default system entropy source. 113 * 114 * @param meth meth method 115 * @return Success: CRYPT_SUCCESS 116 */ 117 int32_t EAL_SetDefaultEntropyMeth(CRYPT_RandSeedMethod *meth); 118 119 /** 120 * @brief Obtain the conditioning function of the corresponding algorithm. 121 * 122 * @param algId algId 123 * @return ExternalConditioningFunction 124 */ 125 ExternalConditioningFunction EAL_EntropyGetECF(uint32_t algId); 126 127 #ifdef __cplusplus 128 } 129 #endif // __cplusplus 130 131 #endif 132 133 #endif 134