00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef MBEDTLS_ENTROPY_H
00052 #define MBEDTLS_ENTROPY_H
00053
00054 #if !defined(MBEDTLS_CONFIG_FILE)
00055 #include "config.h"
00056 #else
00057 #include MBEDTLS_CONFIG_FILE
00058 #endif
00059
00060 #include <stddef.h>
00061
00062 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00063 #include "sha512.h"
00064 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
00065 #else
00066 #if defined(MBEDTLS_SHA256_C)
00067 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
00068 #include "sha256.h"
00069 #endif
00070 #endif
00071
00072 #if defined(MBEDTLS_THREADING_C)
00073 #include "threading.h"
00074 #endif
00075
00076 #if defined(MBEDTLS_HAVEGE_C)
00077 #include "havege.h"
00078 #endif
00079
00080 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C
00081 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E
00082 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
00083 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D
00084 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F
00094 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
00095 #define MBEDTLS_ENTROPY_MAX_SOURCES 20
00096 #endif
00097
00098 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
00099 #define MBEDTLS_ENTROPY_MAX_GATHER 128
00100 #endif
00101
00102
00103
00104 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
00105 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64
00106 #else
00107 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32
00108 #endif
00109
00110 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024
00111 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
00112
00113 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1
00114 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0
00116 #ifdef __cplusplus
00117 extern "C" {
00118 #endif
00119
00131 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
00132 size_t *olen);
00133
00137 typedef struct
00138 {
00139 mbedtls_entropy_f_source_ptr f_source;
00140 void * p_source;
00141 size_t size;
00142 size_t threshold;
00143 int strong;
00144 }
00145 mbedtls_entropy_source_state;
00146
00150 typedef struct
00151 {
00152 int accumulator_started;
00153 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
00154 mbedtls_sha512_context accumulator;
00155 #else
00156 mbedtls_sha256_context accumulator;
00157 #endif
00158 int source_count;
00159 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
00160 #if defined(MBEDTLS_HAVEGE_C)
00161 mbedtls_havege_state havege_data;
00162 #endif
00163 #if defined(MBEDTLS_THREADING_C)
00164 mbedtls_threading_mutex_t mutex;
00165 #endif
00166 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00167 int initial_entropy_run;
00168 #endif
00169 }
00170 mbedtls_entropy_context;
00171
00177 void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
00178
00184 void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
00185
00203 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
00204 mbedtls_entropy_f_source_ptr f_source, void *p_source,
00205 size_t threshold, int strong );
00206
00215 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
00216
00228 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
00229
00240 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
00241 const unsigned char *data, size_t len );
00242
00243 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00244
00252 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
00253 #endif
00254
00255 #if defined(MBEDTLS_FS_IO)
00256
00266 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
00267
00280 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
00281 #endif
00282
00283 #if defined(MBEDTLS_SELF_TEST)
00284
00292 int mbedtls_entropy_self_test( int verbose );
00293
00294 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
00295
00308 int mbedtls_entropy_source_self_test( int verbose );
00309 #endif
00310 #endif
00311
00312 #ifdef __cplusplus
00313 }
00314 #endif
00315
00316 #endif