PolarSSL v1.3.4
entropy.h
Go to the documentation of this file.
1 
27 #ifndef POLARSSL_ENTROPY_H
28 #define POLARSSL_ENTROPY_H
29 
30 #include <string.h>
31 
32 #include "config.h"
33 
34 #if defined(POLARSSL_SHA512_C)
35 #include "sha512.h"
36 #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR
37 #else
38 #if defined(POLARSSL_SHA256_C)
39 #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR
40 #include "sha256.h"
41 #endif
42 #endif
43 
44 #if defined(POLARSSL_THREADING_C)
45 #include "threading.h"
46 #endif
47 
48 #if defined(POLARSSL_HAVEGE_C)
49 #include "havege.h"
50 #endif
51 
52 #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C
53 #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E
54 #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
56 #if !defined(POLARSSL_CONFIG_OPTIONS)
57 #define ENTROPY_MAX_SOURCES 20
58 #define ENTROPY_MAX_GATHER 128
59 #endif /* !POLARSSL_CONFIG_OPTIONS */
60 
61 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
62 #define ENTROPY_BLOCK_SIZE 64
63 #else
64 #define ENTROPY_BLOCK_SIZE 32
65 #endif
66 
67 #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
84 typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
85  size_t *olen);
86 
90 typedef struct
91 {
93  void * p_source;
94  size_t size;
95  size_t threshold;
96 }
98 
102 typedef struct
103 {
104 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
106 #else
107  sha256_context accumulator;
108 #endif
111 #if defined(POLARSSL_HAVEGE_C)
112  havege_state havege_data;
113 #endif
114 #if defined(POLARSSL_THREADING_C)
115  threading_mutex_t mutex;
116 #endif
117 }
119 
125 void entropy_init( entropy_context *ctx );
126 
132 void entropy_free( entropy_context *ctx );
133 
146  f_source_ptr f_source, void *p_source,
147  size_t threshold );
148 
156 int entropy_gather( entropy_context *ctx );
157 
168 int entropy_func( void *data, unsigned char *output, size_t len );
169 
180  const unsigned char *data, size_t len );
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* entropy.h */
int entropy_add_source(entropy_context *ctx, f_source_ptr f_source, void *p_source, size_t threshold)
Adds an entropy source to poll.
int entropy_update_manual(entropy_context *ctx, const unsigned char *data, size_t len)
Add data to the accumulator manually.
sha512_context accumulator
Definition: entropy.h:105
Configuration options (set of defines)
int entropy_gather(entropy_context *ctx)
Trigger an extra gather poll for the accumulator.
Entropy context structure.
Definition: entropy.h:102
int source_count
Definition: entropy.h:109
void * p_source
The callback data pointer.
Definition: entropy.h:93
int(* f_source_ptr)(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback pointer.
Definition: entropy.h:84
Entropy source state.
Definition: entropy.h:90
Threading abstraction layer.
size_t size
Amount received.
Definition: entropy.h:94
f_source_ptr f_source
The entropy source callback.
Definition: entropy.h:92
SHA-512 context structure.
Definition: sha512.h:55
HAVEGE state structure.
Definition: havege.h:41
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
#define ENTROPY_MAX_SOURCES
Maximum number of sources supported.
Definition: entropy.h:57
size_t threshold
Minimum level required before release.
Definition: entropy.h:95
SHA-384 and SHA-512 cryptographic hash function.
SHA-256 context structure.
Definition: sha256.h:54
void entropy_init(entropy_context *ctx)
Initialize the context.
SHA-224 and SHA-256 cryptographic hash function.
int entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_THREADING_C i...
void entropy_free(entropy_context *ctx)
Free the data in the context.