PolarSSL v1.3.1
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 *, unsigned char *, size_t, size_t *);
85 
89 typedef struct
90 {
92  void * p_source;
93  size_t size;
94  size_t threshold;
95 }
97 
101 typedef struct
102 {
103 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
105 #else
106  sha256_context accumulator;
107 #endif
110 #if defined(POLARSSL_HAVEGE_C)
111  havege_state havege_data;
112 #endif
113 #if defined(POLARSSL_THREADING_C)
114  threading_mutex_t mutex;
115 #endif
116 }
118 
124 void entropy_init( entropy_context *ctx );
125 
131 void entropy_free( entropy_context *ctx );
132 
145  f_source_ptr f_source, void *p_source,
146  size_t threshold );
147 
155 int entropy_gather( entropy_context *ctx );
156 
167 int entropy_func( void *data, unsigned char *output, size_t len );
168 
179  const unsigned char *data, size_t len );
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #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:104
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:101
int source_count
Definition: entropy.h:108
void * p_source
The callback data pointer.
Definition: entropy.h:92
Entropy source state.
Definition: entropy.h:89
Threading abstraction layer.
size_t size
Amount received.
Definition: entropy.h:93
f_source_ptr f_source
The entropy source callback.
Definition: entropy.h:91
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:94
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.
int(* f_source_ptr)(void *, unsigned char *, size_t, size_t *)
Entropy poll callback pointer.
Definition: entropy.h:84
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.