Debian Codegraph @

jitterentropy.h

raw symbols SCIP
1 /*
2 * Non-physical true random number generator based on timing jitter.
3 *
4 * Copyright Stephan Mueller <[email protected]>, 2014 - 2026
5 *
6 * License
7 * =======
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, and the entire permission notice in its entirety,
14 * including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
20 * written permission.
21 *
22 * ALTERNATIVELY, this product may be distributed under the terms of
23 * the GNU General Public License, in which case the provisions of the GPL are
24 * required INSTEAD OF the above restrictions. (This clause is
25 * necessary due to a potential bad interaction between the GPL and
26 * the restrictions contained in a BSD-style copyright.)
27 *
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
31 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
39 * DAMAGE.
40 */
41
42 #ifndef _JITTERENTROPY_H
43 #define _JITTERENTROPY_Hcxx . . $ `jitterentropy.h:43:9`!
44
45 #if defined(_MSC_VER) || defined(__MINGW32__)
46 #include "arch/jitterentropy-base-windows.h"
47 #else
48 #include "jitterentropy-base-user.h"cxx . . $ `<file>/jitterentropy-base-user.h`/
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 /*
56 * API / ABI incompatible changes, functional changes that require consumer to
57 * be updated (as long as this number is zero, the API is not considered stable
58 * and can change without a bump of the major version).
59 */
60 #define JENT_MAJVERSIONcxx . . $ `jitterentropy.h:60:9`! 3
61
62 /*
63 * API compatible, ABI may change, functional enhancements only, consumer can be
64 * left unchanged if enhancements are not considered.
65 */
66 #define JENT_MINVERSIONcxx . . $ `jitterentropy.h:66:9`! 7
67
68 /*
69 * API / ABI compatible, no functional changes, no enhancements, bug fixes only.
70 * Also, the entropy collection is not changed in any way that would necessitate
71 * a re-assessment.
72 */
73 #define JENT_PATCHLEVELcxx . . $ `jitterentropy.h:73:9`! 0
74
75 #define JENT_VERSIONcxx . . $ `jitterentropy.h:75:9`! (JENT_MAJVERSION * 1000000 + \
76 JENT_MINVERSION * 10000 + \
77 JENT_PATCHLEVEL * 100)
78
79 /* -- BEGIN Main interface functions -- */
80 /* Flags that can be used to initialize the RNG */
81 #define JENT_DISABLE_STIRcxx . . $ `jitterentropy.h:81:9`! (1<<0) /* UNUSED */
82 #define JENT_DISABLE_UNBIAScxx . . $ `jitterentropy.h:82:9`! (1<<1) /* UNUSED */
83 #define JENT_DISABLE_MEMORY_ACCESScxx . . $ `jitterentropy.h:83:9`! (1<<2) /* Disable memory access for more
84 entropy, saves MEMORY_SIZE RAM for
85 entropy collector */
86 #define JENT_FORCE_INTERNAL_TIMERcxx . . $ `jitterentropy.h:86:9`! (1<<3) /* Force the use of the internal
87 timer */
88 #define JENT_DISABLE_INTERNAL_TIMERcxx . . $ `jitterentropy.h:88:9`! (1<<4) /* Disable the potential use of
89 the internal timer. */
90 #define JENT_FORCE_FIPScxx . . $ `jitterentropy.h:90:9`! (1<<5) /* Force FIPS compliant mode
91 including full SP800-90B
92 compliance. */
93 #define JENT_NTG1cxx . . $ `jitterentropy.h:93:9`! (1<<6) /* AIS 20/31 NTG.1 compliance */
94 #define JENT_CACHE_ALLcxx . . $ `jitterentropy.h:94:9`! (1<<7) /* Shall size of all caches be used to
95 automatically determine the memory size for the
96 memory access? By default it is only the L1
97 cache size. */
98
99 /* Flags field limiting the amount of memory to be used for memory access */
100 #define JENT_FLAGS_TO_MEMSIZE_SHIFTcxx . . $ `jitterentropy.h:100:9`! 27
101 #define JENT_FLAGS_TO_MAX_MEMSIZEcxx . . $ `jitterentropy.h:101:9`!(val) ((val) >> JENT_FLAGS_TO_MEMSIZE_SHIFT)
102 #define JENT_MAX_MEMSIZE_TO_FLAGScxx . . $ `jitterentropy.h:102:9`!(val) ((val) << JENT_FLAGS_TO_MEMSIZE_SHIFT)
103 #define JENT_MAX_MEMSIZE_1kBcxx . . $ `jitterentropy.h:103:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 1))
104 #define JENT_MAX_MEMSIZE_2kBcxx . . $ `jitterentropy.h:104:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 2))
105 #define JENT_MAX_MEMSIZE_4kBcxx . . $ `jitterentropy.h:105:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 3))
106 #define JENT_MAX_MEMSIZE_8kBcxx . . $ `jitterentropy.h:106:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 4))
107 #define JENT_MAX_MEMSIZE_16kBcxx . . $ `jitterentropy.h:107:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 5))
108 #define JENT_MAX_MEMSIZE_32kBcxx . . $ `jitterentropy.h:108:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 6))
109 #define JENT_MAX_MEMSIZE_64kBcxx . . $ `jitterentropy.h:109:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 7))
110 #define JENT_MAX_MEMSIZE_128kBcxx . . $ `jitterentropy.h:110:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 8))
111 #define JENT_MAX_MEMSIZE_256kBcxx . . $ `jitterentropy.h:111:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 9))
112 #define JENT_MAX_MEMSIZE_512kBcxx . . $ `jitterentropy.h:112:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(10))
113 #define JENT_MAX_MEMSIZE_1MBcxx . . $ `jitterentropy.h:113:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(11))
114 #define JENT_MAX_MEMSIZE_2MBcxx . . $ `jitterentropy.h:114:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(12))
115 #define JENT_MAX_MEMSIZE_4MBcxx . . $ `jitterentropy.h:115:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(13))
116 #define JENT_MAX_MEMSIZE_8MBcxx . . $ `jitterentropy.h:116:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(14))
117 #define JENT_MAX_MEMSIZE_16MBcxx . . $ `jitterentropy.h:117:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(15))
118 #define JENT_MAX_MEMSIZE_32MBcxx . . $ `jitterentropy.h:118:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(16))
119 #define JENT_MAX_MEMSIZE_64MBcxx . . $ `jitterentropy.h:119:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(17))
120 #define JENT_MAX_MEMSIZE_128MBcxx . . $ `jitterentropy.h:120:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(18))
121 #define JENT_MAX_MEMSIZE_256MBcxx . . $ `jitterentropy.h:121:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(19))
122 #define JENT_MAX_MEMSIZE_512MBcxx . . $ `jitterentropy.h:122:9`! JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(20))
123 #define JENT_MAX_MEMSIZE_MAXcxx . . $ `jitterentropy.h:123:9`! JENT_MAX_MEMSIZE_512MB
124 #define JENT_MAX_MEMSIZE_MASKcxx . . $ `jitterentropy.h:124:9`! JENT_MAX_MEMSIZE_TO_FLAGS(0xffffffff)
125 /*
126 * We start at 1kB -> offset is log2(1024) - 1 as the flag value above is added
127 * to this offset.
128 */
129 #define JENT_MAX_MEMSIZE_OFFSETcxx . . $ `jitterentropy.h:129:9`! 9
130
131 /* Flags field defining the hash loop */
132 #define JENT_FLAGS_TO_HASHLOOP_SHIFTcxx . . $ `jitterentropy.h:132:9`! 24
133 #define JENT_HASHLOOP_TO_FLAGScxx . . $ `jitterentropy.h:133:9`!(val) ((val) << JENT_FLAGS_TO_HASHLOOP_SHIFT)
134 #define JENT_MAX_HASHLOOP_MASKcxx . . $ `jitterentropy.h:134:9`! JENT_HASHLOOP_TO_FLAGS(0x7)
135 #define JENT_FLAGS_TO_HASHLOOPcxx . . $ `jitterentropy.h:135:9`!(val) (((val) >> JENT_FLAGS_TO_HASHLOOP_SHIFT)\
136 & 0x7)
137 #define JENT_HASHLOOP_1cxx . . $ `jitterentropy.h:137:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(0))
138 #define JENT_HASHLOOP_2cxx . . $ `jitterentropy.h:138:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(1))
139 #define JENT_HASHLOOP_4cxx . . $ `jitterentropy.h:139:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(2))
140 #define JENT_HASHLOOP_8cxx . . $ `jitterentropy.h:140:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(3))
141 #define JENT_HASHLOOP_16cxx . . $ `jitterentropy.h:141:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(4))
142 #define JENT_HASHLOOP_32cxx . . $ `jitterentropy.h:142:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(5))
143 #define JENT_HASHLOOP_64cxx . . $ `jitterentropy.h:143:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(6))
144 #define JENT_HASHLOOP_128cxx . . $ `jitterentropy.h:144:9`! JENT_HASHLOOP_TO_FLAGS(UINT32_C(7))
145 #define JENT_MAX_HASHLOOPcxx . . $ `jitterentropy.h:145:9`! JENT_HASHLOOP_128
146
147 #ifdef JENT_PRIVATE_COMPILE
148 # define JENT_PRIVATE_STATIC static
149 #else /* JENT_PRIVATE_COMPILE */
150 #if defined(_MSC_VER)
151 #define JENT_PRIVATE_STATIC __declspec(dllexport)
152 #else
153 #define JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`! __attribute__((visibility("default")))
154 #endif
155 #endif
156
157 #if defined(__MINGW32__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
158 #define JENT_PTHREAD
159 #endif
160
161 /* Forward declaration of opaque value */
162 struct rand_datacxx . . $ rand_data#;
163
164 /* Number of low bits of the time value that we want to consider */
165 /* get raw entropy */
166 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
167 ssize_tcxx . . $ ssize_t# jent_read_entropycxx . . $ jent_read_entropy(d73b7c7d0a42315f).(struct rand_datacxx . . $ rand_data# *ec, char *data, size_t len);
168 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
169 ssize_tcxx . . $ ssize_t# jent_read_entropy_safecxx . . $ jent_read_entropy_safe(472376e7fa383f61).(struct rand_datacxx . . $ rand_data# **ec, char *data, size_t len);
170 /* initialize an instance of the entropy collector */
171 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
172 struct rand_datacxx . . $ rand_data# *jent_entropy_collector_alloccxx . . $ jent_entropy_collector_alloc(5e5cd898694a5438).(unsigned int osr,
173 unsigned int flags);
174 /* clearing of entropy collector */
175 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
176 void jent_entropy_collector_freecxx . . $ jent_entropy_collector_free(dd399101f0e3ec34).(struct rand_datacxx . . $ rand_data# *entropy_collector);
177
178 /* initialization of entropy collector */
179 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
180 int jent_entropy_initcxx . . $ jent_entropy_init(b126dc7c1de90089).(void);
181 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
182 int jent_entropy_init_excxx . . $ jent_entropy_init_ex(21cb2064190e2a90).(unsigned int osr, unsigned int flags);
183
184 /*
185 * Set a callback to run on health failure in FIPS mode.
186 * This function will take an action determined by the caller.
187 */
188 typedef void (*jent_fips_failure_cbcxx . . $ jent_fips_failure_cb#)(struct rand_datacxx . . $ rand_data# *ec,
189 unsigned int health_failure);
190 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
191 int jent_set_fips_failure_callbackcxx . . $ jent_set_fips_failure_callback(986e4a616045e682).(jent_fips_failure_cbcxx . . $ jent_fips_failure_cb# cb);
192
193 /* return version number of core library */
194 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
195 unsigned int jent_versioncxx . . $ jent_version(db52760e79bf9054).(void);
196
197 /* print out human-readable status of the Jitter RNG (JSON) */
198 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
199 int jent_statuscxx . . $ jent_status(b830aaa7046db909).(const struct rand_datacxx . . $ rand_data# *ec, char *buf, size_t buflen);
200
201 /* return secure memory support, must be done
202 * in jitterentropy itself, as users may not define
203 * a crypto library and so the define in jitterentropy-base-user.h
204 * is not set for them. */
205 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
206 int jent_secure_memory_supportedcxx . . $ jent_secure_memory_supported(b126dc7c1de90089).(void);
207
208 /**
209 * Function pointer data structure to register an external thread handler
210 * used for the timer-less mode of the Jitter RNG.
211 *
212 * The external caller provides these function pointers to handle the
213 * management of the timer thread that is spawned by the Jitter RNG.
214 *
215 * @var jent_notime_init This function is intended to initialize the threading
216 * support. All data that is required by the threading code must be
217 * held in the data structure ctx. The Jitter RNG maintains the
218 * data structure and uses it for every invocation of the following calls.
219 *
220 * @var jent_notime_fini This function shall terminate the threading support.
221 * The function must dispose of all memory and resources used for the
222 * threading operation. It must also dispose of the ctx memory.
223 *
224 * @var jent_notime_start This function is called when the Jitter RNG wants
225 * to start a thread. Besides providing a pointer to the ctx
226 * allocated during initialization time, the Jitter RNG provides a
227 * pointer to the function the thread shall execute and the argument
228 * the function shall be invoked with. These two parameters have the
229 * same purpose as the trailing two parameters of pthread_create(3).
230 *
231 * @var jent_notime_stop This function is invoked by the Jitter RNG when the
232 * thread should be stopped. Note, the Jitter RNG intends to start/stop
233 * the thread frequently.
234 *
235 * An example implementation is found in the Jitter RNG itself with its
236 * default thread handler of jent_notime_thread_builtin.
237 *
238 * If the caller wants to register its own thread handler, it must be done
239 * with the API call jent_entropy_switch_notime_impl as the first
240 * call to interact with the Jitter RNG, even before jent_entropy_init.
241 * After jent_entropy_init is called, changing of the threading implementation
242 * is not allowed.
243 */
244 struct jent_notime_threadcxx . . $ jent_notime_thread# {
245 int (*jent_notime_initcxx . . $ jent_notime_thread#jent_notime_init.)(void **ctx);
246 void (*jent_notime_finicxx . . $ jent_notime_thread#jent_notime_fini.)(void *ctx);
247 int (*jent_notime_startcxx . . $ jent_notime_thread#jent_notime_start.)(void *ctx,
248 #ifdef JENT_PTHREAD
249 void *(*start_routine) (void *), void *arg);
250 #else
251 int (*start_routine)(void *), void *arg);
252 #endif
253 void (*jent_notime_stopcxx . . $ jent_notime_thread#jent_notime_stop.)(void *ctx);
254 };
255
256 /* Set a different thread handling logic for the notimer support */
257 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
258 int jent_entropy_switch_notime_implcxx . . $ jent_entropy_switch_notime_impl(f2e5a00d6ded4bd0).(struct jent_notime_threadcxx . . $ jent_notime_thread# *new_thread);
259 /* -- END of Main interface functions -- */
260
261 /* -- BEGIN timer-less threading support functions to prevent code dupes -- */
262 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
263 int jent_notime_initcxx . . $ jent_notime_init(22ee7711e4bb6c63).(void **ctx);
264
265 JENT_PRIVATE_STATICcxx . . $ `jitterentropy.h:153:9`!
266 void jent_notime_finicxx . . $ jent_notime_fini(5d22bdacc48458e8).(void *ctx);
267 /* -- END timer-less threading support functions to prevent code dupes -- */
268
269 /* -- BEGIN error codes for init function -- */
270 #define ENOTIMEcxx . . $ `jitterentropy.h:270:9`! 1 /* Timer service not available */
271 #define ECOARSETIMEcxx . . $ `jitterentropy.h:271:9`! 2 /* Timer too coarse for RNG */
272 #define ENOMONOTONICcxx . . $ `jitterentropy.h:272:9`! 3 /* Timer is not monotonic increasing */
273 #define EMINVARIATIONcxx . . $ `jitterentropy.h:273:9`! 4 /* Timer variations too small for RNG */
274 #define EVARVARcxx . . $ `jitterentropy.h:274:9`! 5 /* Timer does not produce variations of variations
275 (2nd derivation of time is zero) */
276 #define EMINVARVARcxx . . $ `jitterentropy.h:276:9`! 6 /* Timer variations of variations is too small */
277 #define EPROGERRcxx . . $ `jitterentropy.h:277:9`! 7 /* Programming error */
278 #define ESTUCKcxx . . $ `jitterentropy.h:278:9`! 8 /* Too many stuck results during init. */
279 #define EHEALTHcxx . . $ `jitterentropy.h:279:9`! 9 /* Health test failed during initialization */
280 #define ERCTcxx . . $ `jitterentropy.h:280:9`! 10 /* RCT failed during initialization */
281 #define EHASHcxx . . $ `jitterentropy.h:281:9`! 11 /* Hash self test failed */
282 #define EMEMcxx . . $ `jitterentropy.h:282:9`! 12 /* Can't allocate memory for initialization */
283 #define EGCDcxx . . $ `jitterentropy.h:283:9`! 13 /* GCD self-test failed */
284 /* -- END error codes for init function -- */
285
286 /* -- BEGIN error masks for health tests -- */
287 #define JENT_RCT_FAILUREcxx . . $ `jitterentropy.h:287:9`! 1 /* Failure in RCT health test. */
288 #define JENT_APT_FAILUREcxx . . $ `jitterentropy.h:288:9`! 2 /* Failure in APT health test. */
289 #define JENT_LAG_FAILUREcxx . . $ `jitterentropy.h:289:9`! 4 /* Failure in Lag predictor health test. */
290 #define JENT_RCT_MEM_FAILUREcxx . . $ `jitterentropy.h:290:9`! 8 /* Failure in RCT with memory health test. */
291 #define JENT_PERMANENT_FAILURE_SHIFTcxx . . $ `jitterentropy.h:291:9`! 16
292 #define JENT_PERMANENT_FAILUREcxx . . $ `jitterentropy.h:292:9`!(x) ((x) << JENT_PERMANENT_FAILURE_SHIFTcxx . . $ `jitterentropy.h:291:9`!)
293 #define JENT_RCT_FAILURE_PERMANENTcxx . . $ `jitterentropy.h:293:9`! JENT_PERMANENT_FAILUREcxx . . $ `jitterentropy.h:292:9`!(JENT_RCT_FAILUREcxx . . $ `jitterentropy.h:287:9`!)
294 #define JENT_APT_FAILURE_PERMANENTcxx . . $ `jitterentropy.h:294:9`! JENT_PERMANENT_FAILUREcxx . . $ `jitterentropy.h:292:9`!(JENT_APT_FAILUREcxx . . $ `jitterentropy.h:288:9`!)
295 #define JENT_LAG_FAILURE_PERMANENTcxx . . $ `jitterentropy.h:295:9`! JENT_PERMANENT_FAILUREcxx . . $ `jitterentropy.h:292:9`!(JENT_LAG_FAILUREcxx . . $ `jitterentropy.h:289:9`!)
296 #define JENT_RCT_MEM_FAILURE_PERMANENTcxx . . $ `jitterentropy.h:296:9`! JENT_PERMANENT_FAILUREcxx . . $ `jitterentropy.h:292:9`!(JENT_RCT_MEM_FAILUREcxx . . $ `jitterentropy.h:290:9`!)
297 /* -- END error masks for health tests -- */
298
299 #ifdef __cplusplus
300 }
301 #endif
302
303 #endif /* _JITTERENTROPY_H */