00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef MBEDTLS_ENTROPY_H
00025 #define MBEDTLS_ENTROPY_H
00026
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "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 "sha512.h"
00037 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
00038 #else
00039 #if defined(MBEDTLS_SHA256_C)
00040 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
00041 #include "sha256.h"
00042 #endif
00043 #endif
00044
00045 #if defined(MBEDTLS_THREADING_C)
00046 #include "threading.h"
00047 #endif
00048
00049 #if defined(MBEDTLS_HAVEGE_C)
00050 #include "havege.h"
00051 #endif
00052
00053 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C
00054 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E
00055 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
00056 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D
00057 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F
00067 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
00068 #define MBEDTLS_ENTROPY_MAX_SOURCES 20
00069 #endif
00070
00071 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
00072 #define MBEDTLS_ENTROPY_MAX_GATHER 128
00073 #endif
00074
00075
00076
00077 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
00078 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64
00079 #else
00080 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32
00081 #endif
00082
00083 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024
00084 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
00085
00086 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1
00087 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0
00089 #ifdef __cplusplus
00090 extern "C" {
00091 #endif
00092
00104 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
00105 size_t *olen);
00106
00110 typedef struct
00111 {
00112 mbedtls_entropy_f_source_ptr f_source;
00113 void * p_source;
00114 size_t size;
00115 size_t threshold;
00116 int strong;
00117 }
00118 mbedtls_entropy_source_state;
00119
00123 typedef struct
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;
00138 #endif
00139 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00140 int initial_entropy_run;
00141 #endif
00142 }
00143 mbedtls_entropy_context;
00144
00150 void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
00151
00157 void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
00158
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
00188 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
00189
00201 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
00202
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
00225 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
00226 #endif
00227
00228 #if defined(MBEDTLS_FS_IO)
00229
00239 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
00240
00253 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
00254 #endif
00255
00256 #if defined(MBEDTLS_SELF_TEST)
00257
00265 int mbedtls_entropy_self_test( int verbose );
00266
00267 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
00268
00281 int mbedtls_entropy_source_self_test( int verbose );
00282 #endif
00283 #endif
00284
00285 #ifdef __cplusplus
00286 }
00287 #endif
00288
00289 #endif