| 1 | /* Jitter RNG: Health Tests |
| 2 | * |
| 3 | * Copyright (C) 2021 - 2026, Joshua E. Hill <[email protected]> |
| 4 | * Copyright (C) 2021 - 2026, Stephan Mueller <[email protected]> |
| 5 | * |
| 6 | * License: see LICENSE file in root directory |
| 7 | * |
| 8 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 9 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 10 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 11 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 12 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 13 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 14 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 15 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 16 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 17 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 18 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 19 | * DAMAGE. |
| 20 | */ |
| 21 | |
| 22 | #include "jitterentropy-health.h" |
| 23 | #include "jitterentropy-noise.h" |
| 24 | |
| 25 | static jent_fips_failure_cb fips_cb = NULL; |
| 26 | static int jent_health_cb_switch_blocked = 0; |
| 27 | |
| 28 | void jent_health_cb_block_switch(void) |
| 29 | { |
| 30 | jent_health_cb_switch_blocked = 1; |
| 31 | } |
| 32 | |
| 33 | int jent_set_fips_failure_callback_internal(jent_fips_failure_cb cb) |
| 34 | { |
| 35 | if (jent_health_cb_switch_blocked) |
| 36 | return -EAGAIN; |
| 37 | fips_cb = cb; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /*************************************************************************** |
| 42 | * Lag Predictor Test |
| 43 | * |
| 44 | * This test is a vendor-defined conditional test that is designed to detect |
| 45 | * a known failure mode where the result becomes mostly deterministic |
| 46 | * Note that (lag_observations & JENT_LAG_MASK) is the index where the next |
| 47 | * value provided will be stored. |
| 48 | ***************************************************************************/ |
| 49 | |
| 50 | #ifdef JENT_HEALTH_LAG_PREDICTOR |
| 51 | |
| 52 | /* |
| 53 | * These cutoffs are configured using an entropy estimate of 1/osr under an |
| 54 | * alpha=2^(-22) for a window size of 131072. The other health tests use |
| 55 | * alpha=2^-30, but operate on much smaller window sizes. This larger selection |
| 56 | * of alpha makes the behavior per-lag-window similar to the APT test. |
| 57 | * |
| 58 | * The global cutoffs are calculated using the |
| 59 | * InverseBinomialCDF(n=(JENT_LAG_WINDOW_SIZE-JENT_LAG_HISTORY_SIZE), p=2^(-1/osr); 1-alpha) |
| 60 | * The local cutoffs are somewhat more complicated. For background, see Feller's |
| 61 | * _Introduction to Probability Theory and It's Applications_ Vol. 1, |
| 62 | * Chapter 13, section 7 (in particular see equation 7.11, where x is a root |
| 63 | * of the denominator of equation 7.6). |
| 64 | * |
| 65 | * We'll proceed using the notation of SP 800-90B Section 6.3.8 (which is |
| 66 | * developed in Kelsey-McKay-Turan paper "Predictive Models for Min-entropy |
| 67 | * Estimation".) |
| 68 | * |
| 69 | * Here, we set p=2^(-1/osr), seeking a run of successful guesses (r) with |
| 70 | * probability of less than (1-alpha). That is, it is very very likely |
| 71 | * (probability 1-alpha) that there is _no_ run of length r in a block of size |
| 72 | * JENT_LAG_WINDOW_SIZE-JENT_LAG_HISTORY_SIZE. |
| 73 | * |
| 74 | * We have to iteratively look for an appropriate value for the cutoff r. |
| 75 | */ |
| 76 | static const unsigned int jent_lag_global_cutoff_lookup[20] = |
| 77 | { 66443, 93504, 104761, 110875, 114707, 117330, 119237, 120686, 121823, |
| 78 | 122739, 123493, 124124, 124660, 125120, 125520, 125871, 126181, 126457, |
| 79 | 126704, 126926 }; |
| 80 | static const unsigned int jent_lag_local_cutoff_lookup[20] = |
| 81 | { 38, 75, 111, 146, 181, 215, 250, 284, 318, 351, |
| 82 | 385, 419, 452, 485, 518, 551, 584, 617, 650, 683 }; |
| 83 | |
| 84 | void jent_lag_init(struct rand_data *ec, unsigned int osr) |
| 85 | { |
| 86 | /* |
| 87 | * Establish the lag global and local cutoffs based on the presumed |
| 88 | * entropy rate of 1/osr. |
| 89 | */ |
| 90 | /* TODO: add permanent health failure */ |
| 91 | if (osr > ARRAY_SIZE(jent_lag_global_cutoff_lookup)) { |
| 92 | ec->lag_global_cutoff = |
| 93 | jent_lag_global_cutoff_lookup[ |
| 94 | ARRAY_SIZE(jent_lag_global_cutoff_lookup) - 1]; |
| 95 | } else { |
| 96 | ec->lag_global_cutoff = jent_lag_global_cutoff_lookup[osr - 1]; |
| 97 | } |
| 98 | |
| 99 | if (osr > ARRAY_SIZE(jent_lag_local_cutoff_lookup)) { |
| 100 | ec->lag_local_cutoff = |
| 101 | jent_lag_local_cutoff_lookup[ |
| 102 | ARRAY_SIZE(jent_lag_local_cutoff_lookup) - 1]; |
| 103 | } else { |
| 104 | ec->lag_local_cutoff = jent_lag_local_cutoff_lookup[osr - 1]; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Reset the lag counters |
| 110 | * |
| 111 | * @param[in] ec Reference to entropy collector |
| 112 | */ |
| 113 | static void jent_lag_reset(struct rand_data *ec) |
| 114 | { |
| 115 | unsigned int i; |
| 116 | |
| 117 | /* Reset Lag counters */ |
| 118 | ec->lag_prediction_success_count = 0; |
| 119 | ec->lag_prediction_success_run = 0; |
| 120 | ec->lag_best_predictor = 0; /* The first guess is basically arbitrary. */ |
| 121 | ec->lag_observations = 0; |
| 122 | |
| 123 | for (i = 0; i < JENT_LAG_HISTORY_SIZE; i++) { |
| 124 | ec->lag_scoreboard[i] = 0; |
| 125 | ec->lag_delta_history[i] = 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * A macro for accessing the history. Index 0 is the last observed symbol |
| 131 | * index 1 is the symbol observed two inputs ago, etc. |
| 132 | */ |
| 133 | #define JENT_LAG_HISTORY(EC,LOC) \ |
| 134 | ((EC)->lag_delta_history[((EC)->lag_observations - (LOC) - 1) & \ |
| 135 | JENT_LAG_MASK]) |
| 136 | |
| 137 | /** |
| 138 | * Insert a new entropy event into the lag predictor test |
| 139 | * |
| 140 | * @param[in] ec Reference to entropy collector |
| 141 | * @param[in] current_delta Current time delta |
| 142 | */ |
| 143 | static void jent_lag_insert(struct rand_data *ec, uint64_t current_delta) |
| 144 | { |
| 145 | uint64_t prediction; |
| 146 | unsigned int i; |
| 147 | |
| 148 | /* Initialize the delta_history */ |
| 149 | if (ec->lag_observations < JENT_LAG_HISTORY_SIZE) { |
| 150 | ec->lag_delta_history[ec->lag_observations] = current_delta; |
| 151 | ec->lag_observations++; |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * The history is initialized. First make a guess and examine the |
| 157 | * results. |
| 158 | */ |
| 159 | prediction = JENT_LAG_HISTORY(ec, ec->lag_best_predictor); |
| 160 | |
| 161 | if (prediction == current_delta) { |
| 162 | /* The prediction was correct. */ |
| 163 | ec->lag_prediction_success_count++; |
| 164 | ec->lag_prediction_success_run++; |
| 165 | |
| 166 | /* TODO: add permanent health failure */ |
| 167 | if ((ec->lag_prediction_success_run >= ec->lag_local_cutoff) || |
| 168 | (ec->lag_prediction_success_count >= ec->lag_global_cutoff)) |
| 169 | ec->health_failure |= JENT_LAG_FAILURE; |
| 170 | } else { |
| 171 | /* The prediction wasn't correct. End any run of successes.*/ |
| 172 | ec->lag_prediction_success_run = 0; |
| 173 | } |
| 174 | |
| 175 | /* Now update the predictors using the current data. */ |
| 176 | for (i = 0; i < JENT_LAG_HISTORY_SIZE; i++) { |
| 177 | if (JENT_LAG_HISTORY(ec, i) == current_delta) { |
| 178 | /* |
| 179 | * The ith predictor (which guesses i + 1 symbols in |
| 180 | * the past) successfully guessed. |
| 181 | */ |
| 182 | ec->lag_scoreboard[i] ++; |
| 183 | |
| 184 | /* |
| 185 | * Keep track of the best predictor (tie goes to the |
| 186 | * shortest lag) |
| 187 | */ |
| 188 | if (ec->lag_scoreboard[i] > |
| 189 | ec->lag_scoreboard[ec->lag_best_predictor]) |
| 190 | ec->lag_best_predictor = i; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Finally, update the lag_delta_history array with the newly input |
| 196 | * value. |
| 197 | */ |
| 198 | ec->lag_delta_history[(ec->lag_observations) & JENT_LAG_MASK] = |
| 199 | current_delta; |
| 200 | ec->lag_observations++; |
| 201 | |
| 202 | /* |
| 203 | * lag_best_predictor now is the index of the predictor with the largest |
| 204 | * number of correct guesses. |
| 205 | * This establishes our next guess. |
| 206 | */ |
| 207 | |
| 208 | /* Do we now need a new window? */ |
| 209 | if (ec->lag_observations >= JENT_LAG_WINDOW_SIZE) |
| 210 | jent_lag_reset(ec); |
| 211 | } |
| 212 | |
| 213 | static inline uint64_t jent_delta2(struct rand_data *ec, uint64_t current_delta) |
| 214 | { |
| 215 | /* Note that delta2_n = delta_n - delta_{n-1} */ |
| 216 | return jent_delta(JENT_LAG_HISTORY(ec, 0), current_delta); |
| 217 | } |
| 218 | |
| 219 | static inline uint64_t jent_delta3(struct rand_data *ec, uint64_t delta2) |
| 220 | { |
| 221 | /* |
| 222 | * Note that delta3_n = delta2_n - delta2_{n-1} |
| 223 | * = delta2_n - (delta_{n-1} - delta_{n-2}) |
| 224 | */ |
| 225 | return jent_delta(jent_delta(JENT_LAG_HISTORY(ec, 1), |
| 226 | JENT_LAG_HISTORY(ec, 0)), delta2); |
| 227 | } |
| 228 | |
| 229 | void jent_lag_duplicate(struct rand_data *new_ec, struct rand_data *old_ec) |
| 230 | { |
| 231 | unsigned int i; |
| 232 | |
| 233 | new_ec->lag_prediction_success_run = old_ec->lag_prediction_success_run; |
| 234 | new_ec->lag_prediction_success_count = |
| 235 | old_ec->lag_prediction_success_count; |
| 236 | new_ec->lag_best_predictor = old_ec->lag_best_predictor; |
| 237 | new_ec->lag_observations = old_ec->lag_observations; |
| 238 | |
| 239 | for (i = 0; i < JENT_LAG_HISTORY_SIZE; i++) { |
| 240 | new_ec->lag_scoreboard[i] = old_ec->lag_scoreboard[i]; |
| 241 | new_ec->lag_delta_history[i] = old_ec->lag_delta_history[i]; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | #else /* JENT_HEALTH_LAG_PREDICTOR */ |
| 246 | |
| 247 | static inline void jent_lag_insert(struct rand_data *ec, uint64_t current_delta) |
| 248 | { |
| 249 | (void)ec; |
| 250 | (void)current_delta; |
| 251 | } |
| 252 | |
| 253 | static inline uint64_t jent_delta2(struct rand_data *ec, uint64_t current_delta) |
| 254 | { |
| 255 | uint64_t delta2 = jent_delta(ec->last_delta, current_delta); |
| 256 | |
| 257 | ec->last_delta = current_delta; |
| 258 | return delta2; |
| 259 | } |
| 260 | |
| 261 | static inline uint64_t jent_delta3(struct rand_data *ec, uint64_t delta2) |
| 262 | { |
| 263 | uint64_t delta3 = jent_delta(ec->last_delta2, delta2); |
| 264 | |
| 265 | ec->last_delta2 = delta2; |
| 266 | return delta3; |
| 267 | } |
| 268 | |
| 269 | static inline void jent_lag_init(struct rand_data *ec, unsigned int osr) |
| 270 | { |
| 271 | (void)ec; |
| 272 | (void)osr; |
| 273 | } |
| 274 | |
| 275 | void jent_lag_duplicate(struct rand_data *new_ec, struct rand_data *old_ec) |
| 276 | { |
| 277 | new_ec->last_delta = old_ec->last_delta; |
| 278 | new_ec->last_delta2 = old_ec->last_delta2; |
| 279 | } |
| 280 | |
| 281 | #endif /* JENT_HEALTH_LAG_PREDICTOR */ |
| 282 | |
| 283 | /*************************************************************************** |
| 284 | * Adaptive Proportion Test |
| 285 | * |
| 286 | * This test complies with SP800-90B section 4.4.2. |
| 287 | ***************************************************************************/ |
| 288 | |
| 289 | /* |
| 290 | * See the SP 800-90B comment #10b for the corrected cutoff for the SP 800-90B |
| 291 | * APT. |
| 292 | * http://www.untruth.org/~josh/sp80090b/UL%20SP800-90B-final%20comments%20v1.9%2020191212.pdf |
| 293 | * In in the syntax of R, this is C = 2 + qbinom(1 - 2^(-30), 511, 2^(-1/osr)). |
| 294 | * (The original formula wasn't correct because the first symbol must |
| 295 | * necessarily have been observed, so there is no chance of observing 0 of these |
| 296 | * symbols.) |
| 297 | * |
| 298 | * For the alpha < 2^-53, R cannot be used as it uses a float data type without |
| 299 | * arbitrary precision. A SageMath script is used to calculate those cutoff |
| 300 | * values. |
| 301 | * |
| 302 | * For any value above 14, this yields the maximal allowable value of 512 |
| 303 | * (by FIPS 140-2 IG 7.19 Resolution # 16, we cannot choose a cutoff value that |
| 304 | * renders the test unable to fail). |
| 305 | */ |
| 306 | static const unsigned int jent_apt_cutoff_lookup[15]= |
| 307 | { 325, 422, 459, 477, 488, 494, 499, 502, |
| 308 | 505, 507, 508, 509, 510, 511, 512 }; |
| 309 | static const unsigned int jent_apt_cutoff_permanent_lookup[15]= |
| 310 | { 355, 447, 479, 494, 502, 507, 510, 512, |
| 311 | 512, 512, 512, 512, 512, 512, 512 }; |
| 312 | |
| 313 | static void jent_apt_init(struct rand_data *ec) |
| 314 | { |
| 315 | /* |
| 316 | * Establish the apt_cutoff based on the presumed entropy rate of |
| 317 | * 1/osr. |
| 318 | */ |
| 319 | if (ec->osr >= ARRAY_SIZE(jent_apt_cutoff_lookup)) { |
| 320 | ec->apt_cutoff = jent_apt_cutoff_lookup[ |
| 321 | ARRAY_SIZE(jent_apt_cutoff_lookup) - 1]; |
| 322 | ec->apt_cutoff_permanent = jent_apt_cutoff_permanent_lookup[ |
| 323 | ARRAY_SIZE(jent_apt_cutoff_permanent_lookup) - 1]; |
| 324 | } else { |
| 325 | ec->apt_cutoff = jent_apt_cutoff_lookup[ec->osr - 1]; |
| 326 | ec->apt_cutoff_permanent = |
| 327 | jent_apt_cutoff_permanent_lookup[ec->osr - 1]; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * For NTG.1: 8-fold security margin |
| 333 | * alpha for intermdiate error: 2^-30 |
| 334 | * alpha for permanent error: 2^-60 |
| 335 | * |
| 336 | * Example formula for R for intermediate cutoffs: |
| 337 | * C = 2 + qbinom(1 - 2^(-30), 511, 2^(-8/osr)) |
| 338 | */ |
| 339 | static const unsigned int jent_apt_cutoff_lookup_ntg1[15]= |
| 340 | { 17, 71, 136, 191, 236, 272, 301, 325, |
| 341 | 345, 361, 375, 388, 398, 407, 415 }; |
| 342 | static const unsigned int jent_apt_cutoff_permanent_lookup_ntg1[15]= |
| 343 | { 26, 92, 162, 221, 267, 303, 332, 355, |
| 344 | 375, 390, 404, 415, 425, 433, 440 }; |
| 345 | |
| 346 | static void jent_apt_init_ntg1(struct rand_data *ec) |
| 347 | { |
| 348 | if (ec->osr >= ARRAY_SIZE(jent_apt_cutoff_lookup_ntg1)) { |
| 349 | ec->apt_cutoff = jent_apt_cutoff_lookup_ntg1[ |
| 350 | ARRAY_SIZE(jent_apt_cutoff_lookup_ntg1) - 1]; |
| 351 | ec->apt_cutoff_permanent = |
| 352 | jent_apt_cutoff_permanent_lookup_ntg1[ |
| 353 | ARRAY_SIZE(jent_apt_cutoff_permanent_lookup_ntg1) - 1]; |
| 354 | } else { |
| 355 | ec->apt_cutoff = jent_apt_cutoff_lookup_ntg1[ec->osr - 1]; |
| 356 | ec->apt_cutoff_permanent = |
| 357 | jent_apt_cutoff_permanent_lookup_ntg1[ec->osr - 1]; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void jent_apt_reinit(struct rand_data *ec, |
| 362 | uint64_t current_delta, |
| 363 | unsigned int apt_count, |
| 364 | unsigned int apt_observations) |
| 365 | { |
| 366 | ec->apt_base = current_delta; /* APT Step 1 */ |
| 367 | ec->apt_base_set = 1; /* APT Step 2 */ |
| 368 | |
| 369 | /* |
| 370 | * Reset APT counter |
| 371 | * Note that we've taken in the first symbol in the window. |
| 372 | * |
| 373 | * Thus, if apt_count is zero, set it to the intermittent error. |
| 374 | */ |
| 375 | if (apt_count) |
| 376 | ec->apt_count = apt_count; |
| 377 | else |
| 378 | ec->apt_count = ec->apt_cutoff; |
| 379 | ec->apt_observations = apt_observations; |
| 380 | } |
| 381 | |
| 382 | void jent_apt_duplicate(struct rand_data *new_ec, struct rand_data *old_ec) |
| 383 | { |
| 384 | if (old_ec->apt_observations) { |
| 385 | /* APT re-initialization to intermittent error */ |
| 386 | jent_apt_reinit(new_ec, old_ec->apt_base, 0, |
| 387 | old_ec->apt_observations); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Reset the APT counter |
| 393 | * |
| 394 | * @param[in] ec Reference to entropy collector |
| 395 | */ |
| 396 | static void jent_apt_reset(struct rand_data *ec) |
| 397 | { |
| 398 | /* When reset, accept the _next_ value input as the new base. */ |
| 399 | ec->apt_base_set = 0; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Insert a new entropy event into APT |
| 404 | * |
| 405 | * @param[in] ec Reference to entropy collector |
| 406 | * @param[in] current_delta Current time delta |
| 407 | */ |
| 408 | static void jent_apt_insert(struct rand_data *ec, uint64_t current_delta) |
| 409 | { |
| 410 | current_delta &= JENT_APT_MASK; |
| 411 | |
| 412 | /* Initialize the base reference */ |
| 413 | if (!ec->apt_base_set) { |
| 414 | jent_apt_reinit(ec, current_delta, 1, 1); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | if (current_delta == ec->apt_base) { |
| 419 | ec->apt_count++; /* B = B + 1 */ |
| 420 | |
| 421 | /* Note, ec->apt_count starts with one. */ |
| 422 | if (ec->apt_count >= ec->apt_cutoff_permanent) |
| 423 | ec->health_failure |= JENT_APT_FAILURE_PERMANENT; |
| 424 | else if (ec->apt_count == ec->apt_cutoff) |
| 425 | ec->health_failure |= JENT_APT_FAILURE; |
| 426 | } |
| 427 | |
| 428 | ec->apt_observations++; |
| 429 | |
| 430 | /* Completed one window, the next symbol input will be new apt_base. */ |
| 431 | if (ec->apt_observations >= JENT_APT_WINDOW_SIZE) |
| 432 | jent_apt_reset(ec); /* APT Step 4 */ |
| 433 | } |
| 434 | |
| 435 | /*************************************************************************** |
| 436 | * Stuck Test and its use as Repetition Count with Memory Test |
| 437 | * |
| 438 | * The Jitter RNG applies the stuck test to the repetition count test with |
| 439 | * memory as defined in a paper "Überlegungen zum Jitter-RNG" by Jonas Fiege, |
| 440 | * Johannes Mittmann, Werner Schindler, 6. Februar 2026. This document |
| 441 | * outlines a health test which applies the standard normal distribution. Using |
| 442 | * the distribution, the cutoffs are calculated by applying the heuristic |
| 443 | * entropy value (potentially adjusted by a safety factor). |
| 444 | * |
| 445 | * The test is defined to cover the window required non-rejected time deltas |
| 446 | * to be generated for one output block of 256 bits of data. This window |
| 447 | * is 321 * OSR as implemented by jent_random_data_one. |
| 448 | * |
| 449 | * The intermittent cutoff is defined by calculating |
| 450 | * round(pnorm(2^(-(safety_factor)/OSR))*321*OSR) |
| 451 | * where safety_factor is 1 for common case, 8 for the NTG.1. |
| 452 | * |
| 453 | * The permanent cutoff is defined by calculating |
| 454 | * round(pnorm(2^(-(safety_factor / 2)/OSR))*321*OSR) |
| 455 | ***************************************************************************/ |
| 456 | |
| 457 | /* |
| 458 | * Recovery loop count defining the number of successful generation of |
| 459 | * random blocks after a RCT with mem health alarm to recover from that |
| 460 | * alarm. |
| 461 | */ |
| 462 | #define JENT_RCT_MEM_RECOVERY_LOOP_CNT 10 |
| 463 | |
| 464 | /* |
| 465 | * RCT with memory using tau = 3 - these values effectively disables the |
| 466 | * health test. |
| 467 | */ |
| 468 | static const unsigned short jent_rct_mem_cutoff_lookup[] = |
| 469 | { 107, 214, 321, 428, 535, 642, 749, 856, 963, 1070, |
| 470 | 1177, 1284, 1391, 1498, 1605, 1712, 1819, 1926, 2033, 2140 }; |
| 471 | /* RCT with memory using tau = 3 */ |
| 472 | static const unsigned short jent_rct_mem_cutoff_permanent_lookup[] = |
| 473 | { 108, 215, 322, 429, 536, 643, 750, 857, 964, 1071, |
| 474 | 1178, 1285, 1392, 1499, 1606, 1713, 1820, 1927, 2034, 2141 }; |
| 475 | |
| 476 | static void jent_rct_mem_init(struct rand_data *ec) |
| 477 | { |
| 478 | if (ec->osr >= ARRAY_SIZE(jent_rct_mem_cutoff_lookup)) { |
| 479 | ec->rct_mem_cutoff = jent_rct_mem_cutoff_lookup[ |
| 480 | ARRAY_SIZE(jent_rct_mem_cutoff_lookup) - 1]; |
| 481 | ec->rct_mem_cutoff_permanent = |
| 482 | jent_rct_mem_cutoff_permanent_lookup[ |
| 483 | ARRAY_SIZE(jent_rct_mem_cutoff_permanent_lookup) - 1]; |
| 484 | } else { |
| 485 | ec->rct_mem_cutoff = jent_rct_mem_cutoff_lookup[ec->osr - 1]; |
| 486 | ec->rct_mem_cutoff_permanent = |
| 487 | jent_rct_mem_cutoff_permanent_lookup[ec->osr - 1]; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * For NTG.1: 8-fold security margin using tau 4 with a significance level |
| 493 | * pnorm(-4) yielding 3.17e-05 (roughly 2^-15) for first-order errors. Due to |
| 494 | * the recovery loop we can afford such higher value. |
| 495 | */ |
| 496 | static const unsigned short jent_rct_mem_cutoff_lookup_ntg1[] = |
| 497 | { 4, 46, 134, 255, 399, 560, 733, 856, 963, 1070, |
| 498 | 1177, 1284, 1391, 1498, 1605, 1712, 1819, 1926, 2033, 2140 }; |
| 499 | /* |
| 500 | * For NTG.1: 8-fold security margin using tau 5 with a significance level of |
| 501 | * pnorm(-5) yielding about 2^-20. |
| 502 | */ |
| 503 | static const unsigned short jent_rct_mem_cutoff_permanent_lookup_ntg1[] = |
| 504 | { 5, 50, 142, 265, 410, 572, 746, 857, 964, 1071, |
| 505 | 1178, 1285, 1392, 1499, 1606, 1713, 1820, 1927, 2034, 2141 }; |
| 506 | static void jent_rct_mem_init_ntg1(struct rand_data *ec) |
| 507 | { |
| 508 | if (ec->osr >= ARRAY_SIZE(jent_rct_mem_cutoff_lookup_ntg1)) { |
| 509 | ec->rct_mem_cutoff = jent_rct_mem_cutoff_lookup_ntg1[ |
| 510 | ARRAY_SIZE(jent_rct_mem_cutoff_lookup_ntg1) - 1]; |
| 511 | ec->rct_mem_cutoff_permanent = |
| 512 | jent_rct_mem_cutoff_permanent_lookup_ntg1[ |
| 513 | ARRAY_SIZE(jent_rct_mem_cutoff_permanent_lookup_ntg1) - 1]; |
| 514 | } else { |
| 515 | ec->rct_mem_cutoff = |
| 516 | jent_rct_mem_cutoff_lookup_ntg1[ec->osr - 1]; |
| 517 | ec->rct_mem_cutoff_permanent = |
| 518 | jent_rct_mem_cutoff_permanent_lookup_ntg1[ec->osr - 1]; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static void jent_rct_mem_insert(struct rand_data *ec, unsigned int stuck) |
| 523 | { |
| 524 | /* Start of a new window */ |
| 525 | if (ec->rct_mem_ctr == 0) |
| 526 | ec->rct_mem_count = 0; |
| 527 | |
| 528 | /* |
| 529 | * If we are outside of a window, do not bother any more: the health |
| 530 | * test will not be applied any more. |
| 531 | * |
| 532 | * We could simply return at this point if we leave the window, but then |
| 533 | * we have a multi-modal behavior of the noise source, because if the |
| 534 | * window is completed, a significant part of the code is not executed. |
| 535 | * Therefore we apply a different strategy: always apply the health |
| 536 | * test, but only set a health error if we are within the window. |
| 537 | */ |
| 538 | #define JENT_RCT_MEM_IN_WINDOW (ec->rct_mem_ctr < ec->rct_mem_nosr) |
| 539 | |
| 540 | /* |
| 541 | * According to the specification of this health test, we only consider |
| 542 | * every third iteration count. |
| 543 | * |
| 544 | * Again, we could simply return here, but that would again imply a |
| 545 | * multi-modal behavior. Therefore, always perform the health test |
| 546 | * and turn it into a noop. |
| 547 | */ |
| 548 | #define JENT_RCT_MEM_SKIP_STUCK (ec->rct_mem_ctr % 3) |
| 549 | |
| 550 | /* |
| 551 | * We have a stuck value, count it |
| 552 | */ |
| 553 | if (stuck && JENT_RCT_MEM_IN_WINDOW && !JENT_RCT_MEM_SKIP_STUCK) |
| 554 | ec->rct_mem_count++; |
| 555 | |
| 556 | /* |
| 557 | * Apply the cut off value. |
| 558 | */ |
| 559 | if (ec->rct_mem_count >= ec->rct_mem_cutoff_permanent) { |
| 560 | if (JENT_RCT_MEM_IN_WINDOW) |
| 561 | ec->health_failure |= JENT_RCT_MEM_FAILURE_PERMANENT; |
| 562 | } else if (ec->rct_mem_count == ec->rct_mem_cutoff) { |
| 563 | /* |
| 564 | * This is a "recovery loop" to recover from expected false |
| 565 | * positives of the health test. Note, the health test cutoffs |
| 566 | * have a significantly higher false positive rate than the |
| 567 | * APT and RCT. Therefore, this loop generates additional |
| 568 | * random values to see whether no health test is detected |
| 569 | * during this loop. If no health test error is seen, then |
| 570 | * we incurred an expected spurious false positive that we |
| 571 | * can ignore. The random data generated by that recovery |
| 572 | * loop is simply added to the internal state and thus is not |
| 573 | * wasted. |
| 574 | */ |
| 575 | if (!ec->in_recovery) { |
| 576 | unsigned int i; |
| 577 | |
| 578 | /* |
| 579 | * Clear the RCT with mem counter to generate fresh |
| 580 | * data. |
| 581 | */ |
| 582 | ec->rct_mem_count = 0; |
| 583 | ec->in_recovery = 1; |
| 584 | for (i = 0; i < JENT_RCT_MEM_RECOVERY_LOOP_CNT; i++) |
| 585 | jent_random_data(ec); |
| 586 | ec->in_recovery = 0; |
| 587 | |
| 588 | /* |
| 589 | * We now leave the set health falures incurred from |
| 590 | * the jent_random_data loop. If that loop did not |
| 591 | * detect a failure, we have no failure at this point |
| 592 | * either. Also, leave the rct_mem_count value as is. |
| 593 | */ |
| 594 | } else { |
| 595 | if (JENT_RCT_MEM_IN_WINDOW) |
| 596 | ec->health_failure |= JENT_RCT_MEM_FAILURE; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Count up the seen measurements if we are in the window - this |
| 602 | * prevents also a wrap-around. |
| 603 | */ |
| 604 | if (JENT_RCT_MEM_IN_WINDOW) |
| 605 | ec->rct_mem_ctr++; |
| 606 | } |
| 607 | |
| 608 | void jent_rct_mem_duplicate(struct rand_data *new_ec, struct rand_data *old_ec) |
| 609 | { |
| 610 | new_ec->rct_mem_count = old_ec->rct_mem_cutoff; |
| 611 | } |
| 612 | |
| 613 | /*************************************************************************** |
| 614 | * Stuck Test and its use as Repetition Count Test |
| 615 | * |
| 616 | * The Jitter RNG uses an enhanced version of the Repetition Count Test |
| 617 | * (RCT) specified in SP800-90B section 4.4.1. Instead of counting identical |
| 618 | * back-to-back values, the input to the RCT is the counting of the stuck |
| 619 | * values during the generation of one Jitter RNG output block. |
| 620 | * |
| 621 | * The RCT is applied with an alpha of 2^{-30} compliant to SP800-90B section |
| 622 | * 4.2 for the intermittent failure and 2^{-60} for permanent failures. |
| 623 | * |
| 624 | * During the counting operation, the Jitter RNG always calculates the RCT |
| 625 | * cut-off value of C. If that value exceeds the allowed cut-off value, |
| 626 | * the Jitter RNG output block will be calculated completely but discarded at |
| 627 | * the end. The caller of the Jitter RNG is informed with an error code. |
| 628 | ***************************************************************************/ |
| 629 | static void jent_rct_init(struct rand_data *ec, unsigned short safety) |
| 630 | { |
| 631 | unsigned short osr = (unsigned short)ec->osr; |
| 632 | |
| 633 | ec->rct_cutoff = JENT_HEALTH_RCT_INTERMITTENT_CUTOFF(osr); |
| 634 | ec->rct_cutoff_permanent = JENT_HEALTH_RCT_PERMANENT_CUTOFF(osr); |
| 635 | |
| 636 | if (safety) { |
| 637 | ec->rct_cutoff = (unsigned short) |
| 638 | ((ec->rct_cutoff + safety - 1) / safety); |
| 639 | ec->rct_cutoff_permanent = (unsigned short) |
| 640 | ((ec->rct_cutoff_permanent + safety - 1) / safety); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | void jent_rct_duplicate(struct rand_data *new_ec) |
| 645 | { |
| 646 | /* |
| 647 | * RCT re-initialization to intermittent error: As during a reset, |
| 648 | * the OSR is updated and the OSR is at the same time the cutoff for |
| 649 | * the RCT, we re-initialize the new RCT to the intermittent error |
| 650 | * value based on the new OSR. |
| 651 | */ |
| 652 | new_ec->rct_count = |
| 653 | (unsigned int)(JENT_HEALTH_RCT_INTERMITTENT_CUTOFF(new_ec->osr)); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Repetition Count Test as defined in SP800-90B section 4.4.1 |
| 658 | * |
| 659 | * @param[in] ec Reference to entropy collector |
| 660 | * @param[in] stuck Indicator whether the value is stuck |
| 661 | */ |
| 662 | static void jent_rct_insert(struct rand_data *ec, unsigned int stuck) |
| 663 | { |
| 664 | if (stuck) { |
| 665 | ec->rct_count++; |
| 666 | |
| 667 | if (ec->rct_count >= ec->rct_cutoff_permanent) { |
| 668 | ec->health_failure |= JENT_RCT_FAILURE_PERMANENT; |
| 669 | } else if (ec->rct_count == ec->rct_cutoff) { |
| 670 | ec->health_failure |= JENT_RCT_FAILURE; |
| 671 | } |
| 672 | } else { |
| 673 | /* Must start at zero to reach the correct cutoff value */ |
| 674 | ec->rct_count = 0; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Stuck test by checking the: |
| 680 | * 1st derivative of the jitter measurement (time delta) |
| 681 | * 2nd derivative of the jitter measurement (delta of time deltas) |
| 682 | * 3rd derivative of the jitter measurement (delta of delta of time deltas) |
| 683 | * |
| 684 | * All values must always be non-zero. |
| 685 | * |
| 686 | * @param[in] ec Reference to entropy collector |
| 687 | * @param[in] current_delta Jitter time delta |
| 688 | * |
| 689 | * @return |
| 690 | * 0 jitter measurement not stuck (good bit) |
| 691 | * 1 jitter measurement stuck (reject bit) |
| 692 | */ |
| 693 | unsigned int jent_stuck(struct rand_data *ec, uint64_t current_delta) |
| 694 | { |
| 695 | uint64_t delta2 = jent_delta2(ec, current_delta); |
| 696 | uint64_t delta3 = jent_delta3(ec, delta2); |
| 697 | unsigned int stuck = !current_delta || !delta2 || !delta3; |
| 698 | |
| 699 | /* |
| 700 | * Insert the result of the comparison of two back-to-back time |
| 701 | * deltas. |
| 702 | */ |
| 703 | jent_apt_insert(ec, current_delta); |
| 704 | jent_lag_insert(ec, current_delta); |
| 705 | |
| 706 | /* RCT with stuck result */ |
| 707 | jent_rct_insert(ec, stuck); |
| 708 | jent_rct_mem_insert(ec, stuck); |
| 709 | |
| 710 | return stuck; |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Report any health test failures |
| 715 | * |
| 716 | * @param[in] ec Reference to entropy collector |
| 717 | * |
| 718 | * @return a bitmask indicating which tests failed |
| 719 | * 0 No health test failure |
| 720 | * 1 RCT failure |
| 721 | * 2 APT failure |
| 722 | * 4 Lag predictor test failure |
| 723 | * 8 RCT with memory failure |
| 724 | * 1<<JENT_PERMANENT_FAILURE_SHIFT RCT permanent failure |
| 725 | * 2<<JENT_PERMANENT_FAILURE_SHIFT APT permanent failure |
| 726 | * 4<<JENT_PERMANENT_FAILURE_SHIFT Lag predictor test permanent failure |
| 727 | * 8<<JENT_PERMANENT_FAILURE_SHIFT RCT with memory permanent failure |
| 728 | */ |
| 729 | unsigned int jent_health_failure(struct rand_data *ec) |
| 730 | { |
| 731 | /* Test is only enabled in FIPS mode */ |
| 732 | if (!ec->fips_enabled) |
| 733 | return 0; |
| 734 | |
| 735 | if (fips_cb && ec->health_failure) { |
| 736 | fips_cb(ec, ec->health_failure); |
| 737 | } |
| 738 | |
| 739 | return ec->health_failure; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Initialize the health tests |
| 744 | * |
| 745 | * @param[in] ec Reference to entropy collector |
| 746 | * @param[in] inittype Startup type |
| 747 | */ |
| 748 | void jent_health_init(struct rand_data *ec, enum jent_health_init_type inittype) |
| 749 | { |
| 750 | /* Must start at zero to reach the correct cutoff value */ |
| 751 | ec->rct_count = 0; |
| 752 | jent_lag_init(ec, ec->osr); |
| 753 | switch (inittype) { |
| 754 | case jent_health_init_type_ntg1: |
| 755 | jent_apt_init_ntg1(ec); |
| 756 | jent_rct_init(ec, 8); |
| 757 | jent_rct_mem_init_ntg1(ec); |
| 758 | break; |
| 759 | case jent_health_init_type_common: |
| 760 | default: |
| 761 | jent_apt_init(ec); |
| 762 | jent_rct_init(ec, 0); |
| 763 | jent_rct_mem_init(ec); |
| 764 | break; |
| 765 | } |
| 766 | } |