00001
00027 #ifndef POLARSSL_ENTROPY_H
00028 #define POLARSSL_ENTROPY_H
00029
00030 #include <string.h>
00031
00032 #include "config.h"
00033
00034 #if defined(POLARSSL_SHA512_C)
00035 #include "sha512.h"
00036 #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR
00037 #else
00038 #if defined(POLARSSL_SHA256_C)
00039 #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR
00040 #include "sha256.h"
00041 #endif
00042 #endif
00043
00044 #if defined(POLARSSL_THREADING_C)
00045 #include "threading.h"
00046 #endif
00047
00048 #if defined(POLARSSL_HAVEGE_C)
00049 #include "havege.h"
00050 #endif
00051
00052 #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C
00053 #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E
00054 #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
00056 #if !defined(POLARSSL_CONFIG_OPTIONS)
00057 #define ENTROPY_MAX_SOURCES 20
00058 #define ENTROPY_MAX_GATHER 128
00059 #endif
00060
00061 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
00062 #define ENTROPY_BLOCK_SIZE 64
00063 #else
00064 #define ENTROPY_BLOCK_SIZE 32
00065 #endif
00066
00067 #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
00068
00069 #ifdef __cplusplus
00070 extern "C" {
00071 #endif
00072
00084 typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
00085
00089 typedef struct
00090 {
00091 f_source_ptr f_source;
00092 void * p_source;
00093 size_t size;
00094 size_t threshold;
00095 }
00096 source_state;
00097
00101 typedef struct
00102 {
00103 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
00104 sha512_context accumulator;
00105 #else
00106 sha256_context accumulator;
00107 #endif
00108 int source_count;
00109 source_state source[ENTROPY_MAX_SOURCES];
00110 #if defined(POLARSSL_HAVEGE_C)
00111 havege_state havege_data;
00112 #endif
00113 #if defined(POLARSSL_THREADING_C)
00114 threading_mutex_t mutex;
00115 #endif
00116 }
00117 entropy_context;
00118
00124 void entropy_init( entropy_context *ctx );
00125
00131 void entropy_free( entropy_context *ctx );
00132
00144 int entropy_add_source( entropy_context *ctx,
00145 f_source_ptr f_source, void *p_source,
00146 size_t threshold );
00147
00155 int entropy_gather( entropy_context *ctx );
00156
00167 int entropy_func( void *data, unsigned char *output, size_t len );
00168
00178 int entropy_update_manual( entropy_context *ctx,
00179 const unsigned char *data, size_t len );
00180
00181 #ifdef __cplusplus
00182 }
00183 #endif
00184
00185 #endif