mbed TLS v2.3.0
entropy.h
Go to the documentation of this file.
1 
25 #ifndef MBEDTLS_ENTROPY_H
26 #define MBEDTLS_ENTROPY_H
27 
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
33 
34 #include <stddef.h>
35 
36 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
37 #include "sha512.h"
38 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
39 #else
40 #if defined(MBEDTLS_SHA256_C)
41 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
42 #include "sha256.h"
43 #endif
44 #endif
45 
46 #if defined(MBEDTLS_THREADING_C)
47 #include "threading.h"
48 #endif
49 
50 #if defined(MBEDTLS_HAVEGE_C)
51 #include "havege.h"
52 #endif
53 
54 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C
55 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E
56 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
57 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D
58 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F
68 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
69 #define MBEDTLS_ENTROPY_MAX_SOURCES 20
70 #endif
71 
72 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
73 #define MBEDTLS_ENTROPY_MAX_GATHER 128
74 #endif
75 
76 /* \} name SECTION: Module settings */
77 
78 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
79 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64
80 #else
81 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32
82 #endif
83 
84 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024
85 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
86 
87 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1
88 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93 
105 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
106  size_t *olen);
107 
111 typedef struct
112 {
114  void * p_source;
115  size_t size;
116  size_t threshold;
117  int strong;
118 }
120 
124 typedef struct
125 {
126 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
128 #else
129  mbedtls_sha256_context accumulator;
130 #endif
133 #if defined(MBEDTLS_HAVEGE_C)
134  mbedtls_havege_state havege_data;
135 #endif
136 #if defined(MBEDTLS_THREADING_C)
137  mbedtls_threading_mutex_t mutex;
138 #endif
139 #if defined(MBEDTLS_ENTROPY_NV_SEED)
140  int initial_entropy_run;
141 #endif
142 }
144 
151 
158 
177  mbedtls_entropy_f_source_ptr f_source, void *p_source,
178  size_t threshold, int strong );
179 
189 
201 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
202 
214  const unsigned char *data, size_t len );
215 
216 #if defined(MBEDTLS_ENTROPY_NV_SEED)
217 
225 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
226 #endif /* MBEDTLS_ENTROPY_NV_SEED */
227 
228 #if defined(MBEDTLS_FS_IO)
229 
239 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
240 
253 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
254 #endif /* MBEDTLS_FS_IO */
255 
256 #if defined(MBEDTLS_SELF_TEST)
257 
262 int mbedtls_entropy_self_test( int verbose );
263 #endif /* MBEDTLS_SELF_TEST */
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif /* entropy.h */
void * p_source
The callback data pointer.
Definition: entropy.h:114
int(* mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback pointer.
Definition: entropy.h:105
size_t threshold
Minimum bytes required before release.
Definition: entropy.h:116
HAVEGE state structure.
Definition: havege.h:39
mbedtls_entropy_f_source_ptr f_source
The entropy source callback.
Definition: entropy.h:113
#define MBEDTLS_ENTROPY_MAX_SOURCES
Maximum number of sources supported.
Definition: entropy.h:69
Configuration options (set of defines)
void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Initialize the context.
size_t size
Amount received in bytes.
Definition: entropy.h:115
Entropy context structure.
Definition: entropy.h:124
int mbedtls_entropy_self_test(int verbose)
Checkup routine.
mbedtls_sha512_context accumulator
Definition: entropy.h:127
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE) (Thread-safe if MB...
Threading abstraction layer.
SHA-512 context structure.
Definition: sha512.h:48
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
SHA-256 context structure.
Definition: sha256.h:48
void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Free the data in the context.
int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx, const unsigned char *data, size_t len)
Add data to the accumulator manually (Thread-safe if MBEDTLS_THREADING_C is enabled) ...
int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
Write a seed file.
SHA-384 and SHA-512 cryptographic hash function.
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Trigger an extra gather poll for the accumulator (Thread-safe if MBEDTLS_THREADING_C is enabled) ...
int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
Read and update a seed file.
int strong
Is the source strong?
Definition: entropy.h:117
SHA-224 and SHA-256 cryptographic hash function.
Entropy source state.
Definition: entropy.h:111
int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx, mbedtls_entropy_f_source_ptr f_source, void *p_source, size_t threshold, int strong)
Adds an entropy source to poll (Thread-safe if MBEDTLS_THREADING_C is enabled)