Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
entropy.h
00001 /** 00002 * \file entropy.h 00003 * 00004 * \brief Entropy accumulator implementation 00005 */ 00006 /* 00007 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 00008 * SPDX-License-Identifier: Apache-2.0 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00011 * not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00018 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 * 00022 * This file is part of mbed TLS (https://tls.mbed.org) 00023 */ 00024 #ifndef MBEDTLS_ENTROPY_H 00025 #define MBEDTLS_ENTROPY_H 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "mbedtls/config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include <stddef.h> 00034 00035 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) 00036 #include "mbedtls/sha512.h" 00037 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR 00038 #else 00039 #if defined(MBEDTLS_SHA256_C) 00040 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR 00041 #include "mbedtls/sha256.h" 00042 #endif 00043 #endif 00044 00045 #if defined(MBEDTLS_THREADING_C) 00046 #include "mbedtls/threading.h" 00047 #endif 00048 00049 #if defined(MBEDTLS_HAVEGE_C) 00050 #include "mbedtls/havege.h" 00051 #endif 00052 00053 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ 00054 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */ 00055 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */ 00056 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D /**< No strong sources have been added to poll. */ 00057 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F /**< Read/write error in file. */ 00058 00059 /** 00060 * \name SECTION: Module settings 00061 * 00062 * The configuration options you can set for this module are in this section. 00063 * Either change them in config.h or define them on the compiler command line. 00064 * \{ 00065 */ 00066 00067 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES) 00068 #define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ 00069 #endif 00070 00071 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER) 00072 #define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ 00073 #endif 00074 00075 /* \} name SECTION: Module settings */ 00076 00077 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) 00078 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ 00079 #else 00080 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ 00081 #endif 00082 00083 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */ 00084 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES 00085 00086 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */ 00087 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */ 00088 00089 #ifdef __cplusplus 00090 extern "C" { 00091 #endif 00092 00093 /** 00094 * \brief Entropy poll callback pointer 00095 * 00096 * \param data Callback-specific data pointer 00097 * \param output Data to fill 00098 * \param len Maximum size to provide 00099 * \param olen The actual amount of bytes put into the buffer (Can be 0) 00100 * 00101 * \return 0 if no critical failures occurred, 00102 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise 00103 */ 00104 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len, 00105 size_t *olen); 00106 00107 /** 00108 * \brief Entropy source state 00109 */ 00110 typedef struct mbedtls_entropy_source_state 00111 { 00112 mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */ 00113 void * p_source; /**< The callback data pointer */ 00114 size_t size; /**< Amount received in bytes */ 00115 size_t threshold; /**< Minimum bytes required before release */ 00116 int strong; /**< Is the source strong? */ 00117 } 00118 mbedtls_entropy_source_state; 00119 00120 /** 00121 * \brief Entropy context structure 00122 */ 00123 typedef struct mbedtls_entropy_context 00124 { 00125 int accumulator_started; 00126 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) 00127 mbedtls_sha512_context accumulator; 00128 #else 00129 mbedtls_sha256_context accumulator; 00130 #endif 00131 int source_count; 00132 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES]; 00133 #if defined(MBEDTLS_HAVEGE_C) 00134 mbedtls_havege_state havege_data; 00135 #endif 00136 #if defined(MBEDTLS_THREADING_C) 00137 mbedtls_threading_mutex_t mutex ; /*!< mutex */ 00138 #endif 00139 #if defined(MBEDTLS_ENTROPY_NV_SEED) 00140 int initial_entropy_run; 00141 #endif 00142 } 00143 mbedtls_entropy_context; 00144 00145 /** 00146 * \brief Initialize the context 00147 * 00148 * \param ctx Entropy context to initialize 00149 */ 00150 void mbedtls_entropy_init( mbedtls_entropy_context *ctx ); 00151 00152 /** 00153 * \brief Free the data in the context 00154 * 00155 * \param ctx Entropy context to free 00156 */ 00157 void mbedtls_entropy_free( mbedtls_entropy_context *ctx ); 00158 00159 /** 00160 * \brief Adds an entropy source to poll 00161 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 00162 * 00163 * \param ctx Entropy context 00164 * \param f_source Entropy function 00165 * \param p_source Function data 00166 * \param threshold Minimum required from source before entropy is released 00167 * ( with mbedtls_entropy_func() ) (in bytes) 00168 * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or 00169 * MBEDTLS_ENTROPY_SOURCE_WEAK. 00170 * At least one strong source needs to be added. 00171 * Weaker sources (such as the cycle counter) can be used as 00172 * a complement. 00173 * 00174 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES 00175 */ 00176 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx, 00177 mbedtls_entropy_f_source_ptr f_source, void *p_source, 00178 size_t threshold, int strong ); 00179 00180 /** 00181 * \brief Trigger an extra gather poll for the accumulator 00182 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 00183 * 00184 * \param ctx Entropy context 00185 * 00186 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 00187 */ 00188 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx ); 00189 00190 /** 00191 * \brief Retrieve entropy from the accumulator 00192 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE) 00193 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 00194 * 00195 * \param data Entropy context 00196 * \param output Buffer to fill 00197 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE 00198 * 00199 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 00200 */ 00201 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len ); 00202 00203 /** 00204 * \brief Add data to the accumulator manually 00205 * (Thread-safe if MBEDTLS_THREADING_C is enabled) 00206 * 00207 * \param ctx Entropy context 00208 * \param data Data to add 00209 * \param len Length of data 00210 * 00211 * \return 0 if successful 00212 */ 00213 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx, 00214 const unsigned char *data, size_t len ); 00215 00216 #if defined(MBEDTLS_ENTROPY_NV_SEED) 00217 /** 00218 * \brief Trigger an update of the seed file in NV by using the 00219 * current entropy pool. 00220 * 00221 * \param ctx Entropy context 00222 * 00223 * \return 0 if successful 00224 */ 00225 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx ); 00226 #endif /* MBEDTLS_ENTROPY_NV_SEED */ 00227 00228 #if defined(MBEDTLS_FS_IO) 00229 /** 00230 * \brief Write a seed file 00231 * 00232 * \param ctx Entropy context 00233 * \param path Name of the file 00234 * 00235 * \return 0 if successful, 00236 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or 00237 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 00238 */ 00239 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path ); 00240 00241 /** 00242 * \brief Read and update a seed file. Seed is added to this 00243 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are 00244 * read from the seed file. The rest is ignored. 00245 * 00246 * \param ctx Entropy context 00247 * \param path Name of the file 00248 * 00249 * \return 0 if successful, 00250 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, 00251 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED 00252 */ 00253 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path ); 00254 #endif /* MBEDTLS_FS_IO */ 00255 00256 #if defined(MBEDTLS_SELF_TEST) 00257 /** 00258 * \brief Checkup routine 00259 * 00260 * This module self-test also calls the entropy self-test, 00261 * mbedtls_entropy_source_self_test(); 00262 * 00263 * \return 0 if successful, or 1 if a test failed 00264 */ 00265 int mbedtls_entropy_self_test( int verbose ); 00266 00267 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 00268 /** 00269 * \brief Checkup routine 00270 * 00271 * Verifies the integrity of the hardware entropy source 00272 * provided by the function 'mbedtls_hardware_poll()'. 00273 * 00274 * Note this is the only hardware entropy source that is known 00275 * at link time, and other entropy sources configured 00276 * dynamically at runtime by the function 00277 * mbedtls_entropy_add_source() will not be tested. 00278 * 00279 * \return 0 if successful, or 1 if a test failed 00280 */ 00281 int mbedtls_entropy_source_self_test( int verbose ); 00282 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */ 00283 #endif /* MBEDTLS_SELF_TEST */ 00284 00285 #ifdef __cplusplus 00286 } 00287 #endif 00288 00289 #endif /* entropy.h */
Generated on Tue Jul 12 2022 13:54:18 by
