| 1 | /* |
| 2 | * Copyright (C) 2021 - 2026, Stephan Mueller <[email protected]> |
| 3 | * |
| 4 | * License: see LICENSE file in root directory |
| 5 | * |
| 6 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 7 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 8 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 9 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 10 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 11 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 12 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 13 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 14 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 15 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 16 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | */ |
| 19 | |
| 20 | #ifndef JITTERENTROPY_HEALTH_H |
| 21 | #define JITTERENTROPY_HEALTH_H |
| 22 | |
| 23 | #include "jitterentropy-internal.h" |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" |
| 27 | { |
| 28 | #endif |
| 29 | |
| 30 | void jent_health_cb_block_switch(void); |
| 31 | int jent_set_fips_failure_callback_internal(jent_fips_failure_cb cb); |
| 32 | |
| 33 | static inline uint64_t jent_delta(uint64_t prev, uint64_t next) |
| 34 | { |
| 35 | /* |
| 36 | * Return the delta between two values. If the values are monotonic |
| 37 | * increasing counters which can wrap, this caluculation implicitly |
| 38 | * returns the absolute value of the delta all the time. |
| 39 | */ |
| 40 | return (next - prev); |
| 41 | } |
| 42 | |
| 43 | #if 0 |
| 44 | static inline uint64_t jent_delta_abs(uint64_t prev, uint64_t next) |
| 45 | { |
| 46 | /* |
| 47 | * Return the absolute value of the delta when the values are not a |
| 48 | * monotonic counter that may wrap. |
| 49 | */ |
| 50 | return (next > prev) ? (next - prev) : (prev - next); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * The cutoff value is based on the following consideration: |
| 57 | * alpha = 2^-30 or 2^-60 as recommended in SP800-90B. |
| 58 | * In addition, we require an entropy value H of 1/osr as this is the minimum |
| 59 | * entropy required to provide full entropy. Note, we collect |
| 60 | * (DATA_SIZE_BITS + ENTROPY_SAFETY_FACTOR)*osr deltas for inserting them into |
| 61 | * the entropy pool which should then have (close to) DATA_SIZE_BITS bits of |
| 62 | * entropy in the conditioned output. |
| 63 | * |
| 64 | * Note, ec->rct_count (which equals to value B in the pseudo code of SP800-90B |
| 65 | * section 4.4.1) starts with zero (see jent_health_init(), jent_rct_insert()). |
| 66 | * Hence we need to subtract one from the cutoff value as calculated following |
| 67 | * SP800-90B. Thus C = ceil(-log_2(alpha)/H) = 30*osr or 60*osr. |
| 68 | */ |
| 69 | /* RCT: Intermittent cutoff threshold for alpha = 2**-30 */ |
| 70 | #define JENT_HEALTH_RCT_INTERMITTENT_CUTOFF(x) ((x) * 30) |
| 71 | /* RCT: permanent cutoff threshold for alpha = 2**-60 */ |
| 72 | #define JENT_HEALTH_RCT_PERMANENT_CUTOFF(x) ((x) * 60) |
| 73 | |
| 74 | void jent_lag_duplicate(struct rand_data *new_ec, struct rand_data *old_ec); |
| 75 | void jent_apt_duplicate(struct rand_data *new_ec, struct rand_data *old_ec); |
| 76 | void jent_rct_duplicate(struct rand_data *new_ec); |
| 77 | void jent_rct_mem_duplicate(struct rand_data *new_ec, struct rand_data *old_ec); |
| 78 | unsigned int jent_stuck(struct rand_data *ec, uint64_t current_delta); |
| 79 | unsigned int jent_health_failure(struct rand_data *ec); |
| 80 | |
| 81 | enum jent_health_init_type { |
| 82 | jent_health_init_type_common, |
| 83 | jent_health_init_type_ntg1, |
| 84 | }; |
| 85 | void jent_health_init(struct rand_data *ec, |
| 86 | enum jent_health_init_type inittype); |
| 87 | |
| 88 | #ifdef __cplusplus |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #endif /* JITTERENTROPY_HEALTH_H */ |