mbed TLS v1.3.16
entropy.h
Go to the documentation of this file.
1 
24 #ifndef POLARSSL_ENTROPY_H
25 #define POLARSSL_ENTROPY_H
26 
27 #if !defined(POLARSSL_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include POLARSSL_CONFIG_FILE
31 #endif
32 
33 #include <stddef.h>
34 
35 #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
36 #include "sha512.h"
37 #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR
38 #else
39 #if defined(POLARSSL_SHA256_C)
40 #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR
41 #include "sha256.h"
42 #endif
43 #endif
44 
45 #if defined(POLARSSL_THREADING_C)
46 #include "threading.h"
47 #endif
48 
49 #if defined(POLARSSL_HAVEGE_C)
50 #include "havege.h"
51 #endif
52 
53 #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C
54 #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E
55 #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
56 #define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058
66 #if !defined(ENTROPY_MAX_SOURCES)
67 #define ENTROPY_MAX_SOURCES 20
68 #endif
69 
70 #if !defined(ENTROPY_MAX_GATHER)
71 #define ENTROPY_MAX_GATHER 128
72 #endif
73 
74 /* \} name SECTION: Module settings */
75 
76 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
77 #define ENTROPY_BLOCK_SIZE 64
78 #else
79 #define ENTROPY_BLOCK_SIZE 32
80 #endif
81 
82 #define ENTROPY_MAX_SEED_SIZE 1024
83 #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
100 typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
101  size_t *olen);
102 
106 typedef struct
107 {
109  void * p_source;
110  size_t size;
111  size_t threshold;
112 }
114 
118 typedef struct
119 {
120 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
122 #else
123  sha256_context accumulator;
124 #endif
127 #if defined(POLARSSL_HAVEGE_C)
128  havege_state havege_data;
129 #endif
130 #if defined(POLARSSL_THREADING_C)
131  threading_mutex_t mutex;
132 #endif
133 }
135 
141 void entropy_init( entropy_context *ctx );
142 
148 void entropy_free( entropy_context *ctx );
149 
163  f_source_ptr f_source, void *p_source,
164  size_t threshold );
165 
174 int entropy_gather( entropy_context *ctx );
175 
187 int entropy_func( void *data, unsigned char *output, size_t len );
188 
200  const unsigned char *data, size_t len );
201 
202 #if defined(POLARSSL_FS_IO)
203 
213 int entropy_write_seed_file( entropy_context *ctx, const char *path );
214 
227 int entropy_update_seed_file( entropy_context *ctx, const char *path );
228 #endif /* POLARSSL_FS_IO */
229 
230 #if defined(POLARSSL_SELF_TEST)
231 
236 int entropy_self_test( int verbose );
237 #endif /* POLARSSL_SELF_TEST */
238 
239 #ifdef __cplusplus
240 }
241 #endif
242 
243 #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 (Thread-safe if POLARSSL_THREADING_C is enabled)
int entropy_update_manual(entropy_context *ctx, const unsigned char *data, size_t len)
Add data to the accumulator manually (Thread-safe if POLARSSL_THREADING_C is enabled) ...
sha512_context accumulator
Definition: entropy.h:121
Configuration options (set of defines)
int entropy_gather(entropy_context *ctx)
Trigger an extra gather poll for the accumulator (Thread-safe if POLARSSL_THREADING_C is enabled) ...
Entropy context structure.
Definition: entropy.h:118
int source_count
Definition: entropy.h:125
void * p_source
The callback data pointer.
Definition: entropy.h:109
Entropy source state.
Definition: entropy.h:106
int(* f_source_ptr)(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback pointer.
Definition: entropy.h:100
int entropy_write_seed_file(entropy_context *ctx, const char *path)
Write a seed file.
Threading abstraction layer.
size_t size
Amount received.
Definition: entropy.h:110
f_source_ptr f_source
The entropy source callback.
Definition: entropy.h:108
SHA-512 context structure.
Definition: sha512.h:54
HAVEGE state structure.
Definition: havege.h:38
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
#define ENTROPY_MAX_SOURCES
Maximum number of sources supported.
Definition: entropy.h:67
int entropy_self_test(int verbose)
Checkup routine.
size_t threshold
Minimum level required before release.
Definition: entropy.h:111
SHA-384 and SHA-512 cryptographic hash function.
int entropy_update_seed_file(entropy_context *ctx, const char *path)
Read and update a seed file.
SHA-256 context structure.
Definition: sha256.h:55
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 (Maximum length: ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_T...
void entropy_free(entropy_context *ctx)
Free the data in the context.