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 #ifndef MBEDTLS_ENTROPY_H
00050 #define MBEDTLS_ENTROPY_H
00051
00052 #if !defined(MBEDTLS_CONFIG_FILE)
00053 #include "config.h"
00054 #else
00055 #include MBEDTLS_CONFIG_FILE
00056 #endif
00057
00058 #include <stddef.h>
00059
00060 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00061 #include "sha512.h"
00062 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
00063 #else
00064 #if defined(MBEDTLS_SHA256_C)
00065 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
00066 #include "sha256.h"
00067 #endif
00068 #endif
00069
00070 #if defined(MBEDTLS_THREADING_C)
00071 #include "threading.h"
00072 #endif
00073
00074 #if defined(MBEDTLS_HAVEGE_C)
00075 #include "havege.h"
00076 #endif
00077
00078 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C
00079 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E
00080 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
00081 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D
00082 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F
00092 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
00093 #define MBEDTLS_ENTROPY_MAX_SOURCES 20
00094 #endif
00095
00096 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
00097 #define MBEDTLS_ENTROPY_MAX_GATHER 128
00098 #endif
00099
00100
00101
00102 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
00103 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64
00104 #else
00105 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32
00106 #endif
00107
00108 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024
00109 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
00110
00111 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1
00112 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0
00114 #ifdef __cplusplus
00115 extern "C" {
00116 #endif
00117
00129 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
00130 size_t *olen);
00131
00135 typedef struct
00136 {
00137 mbedtls_entropy_f_source_ptr f_source;
00138 void * p_source;
00139 size_t size;
00140 size_t threshold;
00141 int strong;
00142 }
00143 mbedtls_entropy_source_state;
00144
00148 typedef struct
00149 {
00150 int accumulator_started;
00151 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
00152 mbedtls_sha512_context accumulator;
00153 #else
00154 mbedtls_sha256_context accumulator;
00155 #endif
00156 int source_count;
00157 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
00158 #if defined(MBEDTLS_HAVEGE_C)
00159 mbedtls_havege_state havege_data;
00160 #endif
00161 #if defined(MBEDTLS_THREADING_C)
00162 mbedtls_threading_mutex_t mutex;
00163 #endif
00164 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00165 int initial_entropy_run;
00166 #endif
00167 }
00168 mbedtls_entropy_context;
00169
00175 void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
00176
00182 void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
00183
00201 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
00202 mbedtls_entropy_f_source_ptr f_source, void *p_source,
00203 size_t threshold, int strong );
00204
00213 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
00214
00226 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
00227
00238 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
00239 const unsigned char *data, size_t len );
00240
00241 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00242
00250 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
00251 #endif
00252
00253 #if defined(MBEDTLS_FS_IO)
00254
00264 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
00265
00278 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
00279 #endif
00280
00281 #if defined(MBEDTLS_SELF_TEST)
00282
00290 int mbedtls_entropy_self_test( int verbose );
00291
00292 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
00293
00306 int mbedtls_entropy_source_self_test( int verbose );
00307 #endif
00308 #endif
00309
00310 #ifdef __cplusplus
00311 }
00312 #endif
00313
00314 #endif