![]() |
|
|||
File indexing completed on 2025-08-27 09:37:31
0001 /** 0002 * \file entropy.h 0003 * 0004 * \brief Entropy accumulator implementation 0005 */ 0006 /* 0007 * Copyright The Mbed TLS Contributors 0008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0009 */ 0010 #ifndef MBEDTLS_ENTROPY_H 0011 #define MBEDTLS_ENTROPY_H 0012 #include "mbedtls/private_access.h" 0013 0014 #include "mbedtls/build_info.h" 0015 0016 #include <stddef.h> 0017 0018 #include "md.h" 0019 0020 #if defined(MBEDTLS_MD_CAN_SHA512) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) 0021 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR 0022 #define MBEDTLS_ENTROPY_MD MBEDTLS_MD_SHA512 0023 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ 0024 #else 0025 #if defined(MBEDTLS_MD_CAN_SHA256) 0026 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR 0027 #define MBEDTLS_ENTROPY_MD MBEDTLS_MD_SHA256 0028 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ 0029 #endif 0030 #endif 0031 0032 #if defined(MBEDTLS_THREADING_C) 0033 #include "mbedtls/threading.h" 0034 #endif 0035 0036 0037 /** Critical entropy source failure. */ 0038 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C 0039 /** No more sources can be added. */ 0040 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E 0041 /** No sources have been added to poll. */ 0042 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 0043 /** No strong sources have been added to poll. */ 0044 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D 0045 /** Read/write error in file. */ 0046 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F 0047 0048 /** 0049 * \name SECTION: Module settings 0050 * 0051 * The configuration options you can set for this module are in this section. 0052 * Either change them in mbedtls_config.h or define them on the compiler command line. 0053 * \{ 0054 */ 0055 0056 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES) 0057 #define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ 0058 #endif 0059 0060 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER) 0061 #define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ 0062 #endif 0063 0064 /** \} name SECTION: Module settings */ 0065 0066 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */ 0067 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES 0068 0069 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */ 0070 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */ 0071 0072 #ifdef __cplusplus 0073 extern "C" { 0074 #endif 0075 0076 /** 0077 * \brief Entropy poll callback pointer 0078 * 0079 * \param data Callback-specific data pointer 0080 * \param output Data to fill 0081 * \param len Maximum size to provide 0082 * \param olen The actual amount of bytes put into the buffer (Can be 0) 0083 * 0084 * \return 0 if no critical failures occurred, 0085 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise 0086 */ 0087 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len, 0088 size_t *olen); 0089 0090 /** 0091 * \brief Entropy source state 0092 */ 0093 typedef struct mbedtls_entropy_source_state { 0094 mbedtls_entropy_f_source_ptr MBEDTLS_PRIVATE(f_source); /**< The entropy source callback */ 0095 void *MBEDTLS_PRIVATE(p_source); /**< The callback data pointer */ 0096 size_t MBEDTLS_PRIVATE(size); /**< Amount received in bytes */ 0097 size_t MBEDTLS_PRIVATE(threshold); /**< Minimum bytes required before release */ 0098 int MBEDTLS_PRIVATE(strong); /**< Is the source strong? */ 0099 } 0100 mbedtls_entropy_source_state; 0101 0102 /** 0103 * \brief Entropy context structure 0104 */ 0105 typedef struct mbedtls_entropy_context { 0106 mbedtls_md_context_t MBEDTLS_PRIVATE(accumulator); 0107 int MBEDTLS_PRIVATE(accumulator_started); /* 0 after init. 0108 * 1 after the first update. 0109 * -1 after free. */ 0110 int MBEDTLS_PRIVATE(source_count); /* Number of entries used in source. */ 0111 mbedtls_entropy_source_state MBEDTLS_PRIVATE(source)[MBEDTLS_ENTROPY_MAX_SOURCES]; 0112 #if defined(MBEDTLS_THREADING_C) 0113 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< mutex */ 0114 #endif 0115 #if defined(MBEDTLS_ENTROPY_NV_SEED) 0116 int MBEDTLS_PRIVATE(initial_entropy_run); 0117 #endif 0118 } 0119 mbedtls_entropy_context; 0120 0121 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 0122 /** 0123 * \brief Platform-specific entropy poll callback 0124 */ 0125 int mbedtls_platform_entropy_poll(void *data, 0126 unsigned char *output, size_t len, size_t *olen); 0127 #endif 0128 0129 /** 0130 * \brief Initialize the context 0131 * 0132 * \param ctx Entropy context to initialize 0133 */ 0134 void mbedtls_entropy_init(mbedtls_entropy_context *ctx); 0135 0136 /** 0137 * \brief Free the data in the context 0138 * 0139 * \param ctx Entropy context to free 0140 */ 0141 void mbedtls_entropy_free(mbedtls_entropy_context *ctx); 0142 0143 /** 0144 * \brief Adds an entropy source to poll 0145 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 0146 * 0147 * \param ctx Entropy context 0148 * \param f_source Entropy function 0149 * \param p_source Function data 0150 * \param threshold Minimum required from source before entropy is released 0151 * ( with mbedtls_entropy_func() ) (in bytes) 0152 * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or 0153 * MBEDTLS_ENTROPY_SOURCE_WEAK. 0154 * At least one strong source needs to be added. 0155 * Weaker sources (such as the cycle counter) can be used as 0156 * a complement. 0157 * 0158 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES 0159 */ 0160 int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx, 0161 mbedtls_entropy_f_source_ptr f_source, void *p_source, 0162 size_t threshold, int strong); 0163 0164 /** 0165 * \brief Trigger an extra gather poll for the accumulator 0166 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 0167 * 0168 * \param ctx Entropy context 0169 * 0170 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 0171 */ 0172 int mbedtls_entropy_gather(mbedtls_entropy_context *ctx); 0173 0174 /** 0175 * \brief Retrieve entropy from the accumulator 0176 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE) 0177 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 0178 * 0179 * \param data Entropy context 0180 * \param output Buffer to fill 0181 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE 0182 * 0183 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 0184 */ 0185 int mbedtls_entropy_func(void *data, unsigned char *output, size_t len); 0186 0187 /** 0188 * \brief Add data to the accumulator manually 0189 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 0190 * 0191 * \param ctx Entropy context 0192 * \param data Data to add 0193 * \param len Length of data 0194 * 0195 * \return 0 if successful 0196 */ 0197 int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx, 0198 const unsigned char *data, size_t len); 0199 0200 #if defined(MBEDTLS_ENTROPY_NV_SEED) 0201 /** 0202 * \brief Trigger an update of the seed file in NV by using the 0203 * current entropy pool. 0204 * 0205 * \param ctx Entropy context 0206 * 0207 * \return 0 if successful 0208 */ 0209 int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx); 0210 #endif /* MBEDTLS_ENTROPY_NV_SEED */ 0211 0212 #if defined(MBEDTLS_FS_IO) 0213 /** 0214 * \brief Write a seed file 0215 * 0216 * \param ctx Entropy context 0217 * \param path Name of the file 0218 * 0219 * \return 0 if successful, 0220 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or 0221 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 0222 */ 0223 int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path); 0224 0225 /** 0226 * \brief Read and update a seed file. Seed is added to this 0227 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are 0228 * read from the seed file. The rest is ignored. 0229 * 0230 * \param ctx Entropy context 0231 * \param path Name of the file 0232 * 0233 * \return 0 if successful, 0234 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, 0235 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 0236 */ 0237 int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path); 0238 #endif /* MBEDTLS_FS_IO */ 0239 0240 #if defined(MBEDTLS_SELF_TEST) 0241 /** 0242 * \brief Checkup routine 0243 * 0244 * This module self-test also calls the entropy self-test, 0245 * mbedtls_entropy_source_self_test(); 0246 * 0247 * \return 0 if successful, or 1 if a test failed 0248 */ 0249 int mbedtls_entropy_self_test(int verbose); 0250 0251 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 0252 /** 0253 * \brief Checkup routine 0254 * 0255 * Verifies the integrity of the hardware entropy source 0256 * provided by the function 'mbedtls_hardware_poll()'. 0257 * 0258 * Note this is the only hardware entropy source that is known 0259 * at link time, and other entropy sources configured 0260 * dynamically at runtime by the function 0261 * mbedtls_entropy_add_source() will not be tested. 0262 * 0263 * \return 0 if successful, or 1 if a test failed 0264 */ 0265 int mbedtls_entropy_source_self_test(int verbose); 0266 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */ 0267 #endif /* MBEDTLS_SELF_TEST */ 0268 0269 #ifdef __cplusplus 0270 } 0271 #endif 0272 0273 #endif /* entropy.h */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |