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 ES_NOISE_SOURCE_H 17 #define ES_NOISE_SOURCE_H 18 19 #include "hitls_build.h" 20 #if defined(HITLS_CRYPTO_ENTROPY) && defined(HITLS_CRYPTO_ENTROPY_SYS) 21 22 #include <stdint.h> 23 #include "bsl_list.h" 24 #include "es_health_test.h" 25 #include "crypt_types.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 typedef struct { 32 /* Whether to enable the health test */ 33 bool enableTest; 34 /* Whether the noise source automatically performs the health test */ 35 bool autoTest; 36 /* Whether the noise source is available */ 37 bool isEnable; 38 /* Whether the noise source is initialized */ 39 bool isInit; 40 /* Noise source name, which must be unique. */ 41 char *name; 42 /* Initialization parameters of the noise source */ 43 void *para; 44 /* Noise Source Handle */ 45 void *usrdata; 46 /* Noise Source Initialization Interface. */ 47 void *(*init)(void *para); 48 /* Interface for Obtaining Noise Sources. */ 49 int32_t (*read)(void *usrdata, uint32_t timeout, uint8_t *buf, uint32_t bufLen); 50 /* Noise Source Deinitialization Interface. */ 51 void (*deinit)(void *usrdata); 52 /* minimum entropy, bit entropy contained in a byte. */ 53 uint32_t minEntropy; 54 ES_HealthTest state; 55 } ES_NoiseSource; 56 57 /* Noise Source List create. */ 58 BslList *ES_NsListCreat(void); 59 60 /* Noise Source List Initialization. */ 61 int32_t ES_NsListInit(BslList *nsList, bool enableTest); 62 63 /* Noise Source List deinitialization. */ 64 void ES_NsListDeinit(BslList *nsList); 65 66 /* Noise Source List release. */ 67 void ES_NsListFree(BslList *nsList); 68 69 /** 70 * @brief add ns 71 * 72 * @param nsList [IN] noise source list 73 * @param name [IN] Noise source name, which must be unique. 74 * @param autoTest [IN] Whether the noise source automatically performs the health test. 75 * @param minEntropy [IN] minimum entropy, bit entropy contained in a byte. 76 * @param method [IN] noise source callback Interface. 77 * @param para [IN] noise source health test parameter. 78 * 79 * @return CRYPT_SUCCESS succeeded. 80 * For other error codes, see crypt_error.h. 81 */ 82 int32_t ES_NsAdd(BslList *nsList, const char *name, bool autoTest, uint32_t minEntropy, 83 const CRYPT_EAL_NsMethod *method, const CRYPT_EAL_NsTestPara *para); 84 85 /** 86 * @brief remove ns 87 * 88 * @param nsList [IN] noise source list 89 * @param name [IN] Noise source name, which must be unique. 90 * 91 * @return CRYPT_SUCCESS succeeded. 92 * For other error codes, see crypt_error.h. 93 */ 94 int32_t ES_NsRemove(BslList *nsList, const char *name); 95 96 /** 97 * @brief Read the raw noise data. 98 * 99 * @param ns [IN] noise source handle 100 * @param buf [IN] the raw noise data buffer. 101 * @param bufLen [IN] the length of the raw noise data. 102 * 103 * @return CRYPT_SUCCESS succeeded. 104 * For other error codes, see crypt_error.h. 105 */ 106 int32_t ES_NsRead(ES_NoiseSource *ns, uint8_t *buf, uint32_t bufLen); 107 108 /** 109 * @brief Obtains the minimum value of the minimum entropy. 110 * 111 * @param nsList [IN] noise source list 112 * 113 * @return CRYPT_SUCCESS succeeded. 114 * For other error codes, see crypt_error.h. 115 */ 116 uint32_t ES_NsListGetMinEntropy(BslList *nsList); 117 118 /* Obtains the handle of the cpu-jiiter. */ 119 ES_NoiseSource *ES_CpuJitterGetCtx(void); 120 121 /* Obtains the handle of the timestamp. */ 122 ES_NoiseSource *ES_TimeStampGetCtx(void); 123 #ifdef __cplusplus 124 } 125 #endif 126 127 #endif 128 129 #endif