| 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_H |
| 44 | |
| 45 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 46 | #include "arch/jitterentropy-base-windows.h" |
| 47 | #else |
| 48 | #include "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_MAJVERSION 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_MINVERSION 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_PATCHLEVEL 0 |
| 74 | |
| 75 | #define JENT_VERSION (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_STIR (1<<0) /* UNUSED */ |
| 82 | #define JENT_DISABLE_UNBIAS (1<<1) /* UNUSED */ |
| 83 | #define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more |
| 84 | entropy, saves MEMORY_SIZE RAM for |
| 85 | entropy collector */ |
| 86 | #define JENT_FORCE_INTERNAL_TIMER (1<<3) /* Force the use of the internal |
| 87 | timer */ |
| 88 | #define JENT_DISABLE_INTERNAL_TIMER (1<<4) /* Disable the potential use of |
| 89 | the internal timer. */ |
| 90 | #define JENT_FORCE_FIPS (1<<5) /* Force FIPS compliant mode |
| 91 | including full SP800-90B |
| 92 | compliance. */ |
| 93 | #define JENT_NTG1 (1<<6) /* AIS 20/31 NTG.1 compliance */ |
| 94 | #define JENT_CACHE_ALL (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_SHIFT 27 |
| 101 | #define JENT_FLAGS_TO_MAX_MEMSIZE(val) ((val) >> JENT_FLAGS_TO_MEMSIZE_SHIFT) |
| 102 | #define JENT_MAX_MEMSIZE_TO_FLAGS(val) ((val) << JENT_FLAGS_TO_MEMSIZE_SHIFT) |
| 103 | #define JENT_MAX_MEMSIZE_1kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 1)) |
| 104 | #define JENT_MAX_MEMSIZE_2kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 2)) |
| 105 | #define JENT_MAX_MEMSIZE_4kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 3)) |
| 106 | #define JENT_MAX_MEMSIZE_8kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 4)) |
| 107 | #define JENT_MAX_MEMSIZE_16kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 5)) |
| 108 | #define JENT_MAX_MEMSIZE_32kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 6)) |
| 109 | #define JENT_MAX_MEMSIZE_64kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 7)) |
| 110 | #define JENT_MAX_MEMSIZE_128kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 8)) |
| 111 | #define JENT_MAX_MEMSIZE_256kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C( 9)) |
| 112 | #define JENT_MAX_MEMSIZE_512kB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(10)) |
| 113 | #define JENT_MAX_MEMSIZE_1MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(11)) |
| 114 | #define JENT_MAX_MEMSIZE_2MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(12)) |
| 115 | #define JENT_MAX_MEMSIZE_4MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(13)) |
| 116 | #define JENT_MAX_MEMSIZE_8MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(14)) |
| 117 | #define JENT_MAX_MEMSIZE_16MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(15)) |
| 118 | #define JENT_MAX_MEMSIZE_32MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(16)) |
| 119 | #define JENT_MAX_MEMSIZE_64MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(17)) |
| 120 | #define JENT_MAX_MEMSIZE_128MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(18)) |
| 121 | #define JENT_MAX_MEMSIZE_256MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(19)) |
| 122 | #define JENT_MAX_MEMSIZE_512MB JENT_MAX_MEMSIZE_TO_FLAGS(UINT32_C(20)) |
| 123 | #define JENT_MAX_MEMSIZE_MAX JENT_MAX_MEMSIZE_512MB |
| 124 | #define JENT_MAX_MEMSIZE_MASK 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_OFFSET 9 |
| 130 | |
| 131 | /* Flags field defining the hash loop */ |
| 132 | #define JENT_FLAGS_TO_HASHLOOP_SHIFT 24 |
| 133 | #define JENT_HASHLOOP_TO_FLAGS(val) ((val) << JENT_FLAGS_TO_HASHLOOP_SHIFT) |
| 134 | #define JENT_MAX_HASHLOOP_MASK JENT_HASHLOOP_TO_FLAGS(0x7) |
| 135 | #define JENT_FLAGS_TO_HASHLOOP(val) (((val) >> JENT_FLAGS_TO_HASHLOOP_SHIFT)\ |
| 136 | & 0x7) |
| 137 | #define JENT_HASHLOOP_1 JENT_HASHLOOP_TO_FLAGS(UINT32_C(0)) |
| 138 | #define JENT_HASHLOOP_2 JENT_HASHLOOP_TO_FLAGS(UINT32_C(1)) |
| 139 | #define JENT_HASHLOOP_4 JENT_HASHLOOP_TO_FLAGS(UINT32_C(2)) |
| 140 | #define JENT_HASHLOOP_8 JENT_HASHLOOP_TO_FLAGS(UINT32_C(3)) |
| 141 | #define JENT_HASHLOOP_16 JENT_HASHLOOP_TO_FLAGS(UINT32_C(4)) |
| 142 | #define JENT_HASHLOOP_32 JENT_HASHLOOP_TO_FLAGS(UINT32_C(5)) |
| 143 | #define JENT_HASHLOOP_64 JENT_HASHLOOP_TO_FLAGS(UINT32_C(6)) |
| 144 | #define JENT_HASHLOOP_128 JENT_HASHLOOP_TO_FLAGS(UINT32_C(7)) |
| 145 | #define JENT_MAX_HASHLOOP 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_STATIC __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_data; |
| 163 | |
| 164 | /* Number of low bits of the time value that we want to consider */ |
| 165 | /* get raw entropy */ |
| 166 | JENT_PRIVATE_STATIC |
| 167 | ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len); |
| 168 | JENT_PRIVATE_STATIC |
| 169 | ssize_t jent_read_entropy_safe(struct rand_data **ec, char *data, size_t len); |
| 170 | /* initialize an instance of the entropy collector */ |
| 171 | JENT_PRIVATE_STATIC |
| 172 | struct rand_data *jent_entropy_collector_alloc(unsigned int osr, |
| 173 | unsigned int flags); |
| 174 | /* clearing of entropy collector */ |
| 175 | JENT_PRIVATE_STATIC |
| 176 | void jent_entropy_collector_free(struct rand_data *entropy_collector); |
| 177 | |
| 178 | /* initialization of entropy collector */ |
| 179 | JENT_PRIVATE_STATIC |
| 180 | int jent_entropy_init(void); |
| 181 | JENT_PRIVATE_STATIC |
| 182 | int jent_entropy_init_ex(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_cb)(struct rand_data *ec, |
| 189 | unsigned int health_failure); |
| 190 | JENT_PRIVATE_STATIC |
| 191 | int jent_set_fips_failure_callback(jent_fips_failure_cb cb); |
| 192 | |
| 193 | /* return version number of core library */ |
| 194 | JENT_PRIVATE_STATIC |
| 195 | unsigned int jent_version(void); |
| 196 | |
| 197 | /* print out human-readable status of the Jitter RNG (JSON) */ |
| 198 | JENT_PRIVATE_STATIC |
| 199 | int jent_status(const struct 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_STATIC |
| 206 | int jent_secure_memory_supported(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_thread { |
| 245 | int (*jent_notime_init)(void **ctx); |
| 246 | void (*jent_notime_fini)(void *ctx); |
| 247 | int (*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_stop)(void *ctx); |
| 254 | }; |
| 255 | |
| 256 | /* Set a different thread handling logic for the notimer support */ |
| 257 | JENT_PRIVATE_STATIC |
| 258 | int jent_entropy_switch_notime_impl(struct 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_STATIC |
| 263 | int jent_notime_init(void **ctx); |
| 264 | |
| 265 | JENT_PRIVATE_STATIC |
| 266 | void jent_notime_fini(void *ctx); |
| 267 | /* -- END timer-less threading support functions to prevent code dupes -- */ |
| 268 | |
| 269 | /* -- BEGIN error codes for init function -- */ |
| 270 | #define ENOTIME 1 /* Timer service not available */ |
| 271 | #define ECOARSETIME 2 /* Timer too coarse for RNG */ |
| 272 | #define ENOMONOTONIC 3 /* Timer is not monotonic increasing */ |
| 273 | #define EMINVARIATION 4 /* Timer variations too small for RNG */ |
| 274 | #define EVARVAR 5 /* Timer does not produce variations of variations |
| 275 | (2nd derivation of time is zero) */ |
| 276 | #define EMINVARVAR 6 /* Timer variations of variations is too small */ |
| 277 | #define EPROGERR 7 /* Programming error */ |
| 278 | #define ESTUCK 8 /* Too many stuck results during init. */ |
| 279 | #define EHEALTH 9 /* Health test failed during initialization */ |
| 280 | #define ERCT 10 /* RCT failed during initialization */ |
| 281 | #define EHASH 11 /* Hash self test failed */ |
| 282 | #define EMEM 12 /* Can't allocate memory for initialization */ |
| 283 | #define EGCD 13 /* GCD self-test failed */ |
| 284 | /* -- END error codes for init function -- */ |
| 285 | |
| 286 | /* -- BEGIN error masks for health tests -- */ |
| 287 | #define JENT_RCT_FAILURE 1 /* Failure in RCT health test. */ |
| 288 | #define JENT_APT_FAILURE 2 /* Failure in APT health test. */ |
| 289 | #define JENT_LAG_FAILURE 4 /* Failure in Lag predictor health test. */ |
| 290 | #define JENT_RCT_MEM_FAILURE 8 /* Failure in RCT with memory health test. */ |
| 291 | #define JENT_PERMANENT_FAILURE_SHIFT 16 |
| 292 | #define JENT_PERMANENT_FAILURE(x) ((x) << JENT_PERMANENT_FAILURE_SHIFT) |
| 293 | #define JENT_RCT_FAILURE_PERMANENT JENT_PERMANENT_FAILURE(JENT_RCT_FAILURE) |
| 294 | #define JENT_APT_FAILURE_PERMANENT JENT_PERMANENT_FAILURE(JENT_APT_FAILURE) |
| 295 | #define JENT_LAG_FAILURE_PERMANENT JENT_PERMANENT_FAILURE(JENT_LAG_FAILURE) |
| 296 | #define JENT_RCT_MEM_FAILURE_PERMANENT JENT_PERMANENT_FAILURE(JENT_RCT_MEM_FAILURE) |
| 297 | /* -- END error masks for health tests -- */ |
| 298 | |
| 299 | #ifdef __cplusplus |
| 300 | } |
| 301 | #endif |
| 302 | |
| 303 | #endif /* _JITTERENTROPY_H */ |