| 1 | /* Jitter RNG: Internal timer implementation |
| 2 | * |
| 3 | * Copyright (C) 2021 - 2026, Stephan Mueller <[email protected]> |
| 4 | * |
| 5 | * License: see LICENSE file in root directory |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 8 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 9 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 10 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 13 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 14 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 15 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 16 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 17 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 18 | * DAMAGE. |
| 19 | */ |
| 20 | |
| 21 | #include "jitterentropy-base.h" |
| 22 | #include "jitterentropy-timer.h" |
| 23 | |
| 24 | /* Timer-less entropy source */ |
| 25 | #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER |
| 26 | |
| 27 | #ifdef JENT_PTHREAD |
| 28 | #include <pthread.h> |
| 29 | struct jent_notime_ctx { |
| 30 | pthread_attr_t notime_pthread_attr; /* pthreads library */ |
| 31 | pthread_t notime_thread_id; /* pthreads thread ID */ |
| 32 | }; |
| 33 | #else |
| 34 | #include <threads.h> |
| 35 | struct jent_notime_ctx { |
| 36 | thrd_t notime_thread_id; /* thread ID */ |
| 37 | }; |
| 38 | #endif |
| 39 | |
| 40 | /*************************************************************************** |
| 41 | * Thread handler |
| 42 | ***************************************************************************/ |
| 43 | |
| 44 | JENT_PRIVATE_STATIC |
| 45 | int jent_notime_init(void **ctx) |
| 46 | { |
| 47 | struct jent_notime_ctx *thread_ctx; |
| 48 | long ncpu = jent_ncpu(); |
| 49 | |
| 50 | if (ncpu < 0) |
| 51 | return (int)ncpu; |
| 52 | |
| 53 | /* We need at least two CPUs to enable the timer thread */ |
| 54 | if (ncpu < 2) |
| 55 | return -ENOENT; |
| 56 | |
| 57 | thread_ctx = jent_zalloc(sizeof(struct jent_notime_ctx)); |
| 58 | if (!thread_ctx) |
| 59 | return -ENOMEM; |
| 60 | |
| 61 | *ctx = thread_ctx; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | JENT_PRIVATE_STATIC |
| 67 | void jent_notime_fini(void *ctx) |
| 68 | { |
| 69 | struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx; |
| 70 | |
| 71 | if (thread_ctx) |
| 72 | jent_zfree(thread_ctx, sizeof(struct jent_notime_ctx)); |
| 73 | } |
| 74 | |
| 75 | #else /* JENT_CONF_ENABLE_INTERNAL_TIMER */ |
| 76 | |
| 77 | int jent_notime_init(void **ctx) { (void)ctx; return 0; } |
| 78 | void jent_notime_fini(void *ctx) { (void)ctx; } |
| 79 | |
| 80 | #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */ |
| 81 | |
| 82 | #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER |
| 83 | |
| 84 | static int jent_notime_start(void *ctx, |
| 85 | #ifdef JENT_PTHREAD |
| 86 | void *(*start_routine) (void *), |
| 87 | #else |
| 88 | int (*start_routine) (void *), |
| 89 | #endif |
| 90 | void *arg) |
| 91 | { |
| 92 | struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx; |
| 93 | #ifdef JENT_PTHREAD |
| 94 | int ret; |
| 95 | #endif |
| 96 | |
| 97 | if (!thread_ctx) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | #ifdef JENT_PTHREAD |
| 101 | ret = -pthread_attr_init(&thread_ctx->notime_pthread_attr); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | return -pthread_create(&thread_ctx->notime_thread_id, |
| 105 | &thread_ctx->notime_pthread_attr, |
| 106 | start_routine, arg); |
| 107 | #else |
| 108 | switch (thrd_create(&thread_ctx->notime_thread_id, start_routine, arg)) { |
| 109 | case thrd_success: |
| 110 | return 0; |
| 111 | case thrd_nomem: |
| 112 | return -ENOMEM; |
| 113 | case thrd_timedout: |
| 114 | return -ETIMEDOUT; |
| 115 | case thrd_busy: |
| 116 | return -EBUSY; |
| 117 | case thrd_error: |
| 118 | default: |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | static void jent_notime_stop(void *ctx) |
| 125 | { |
| 126 | struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx; |
| 127 | |
| 128 | /* defensive check */ |
| 129 | if (ctx == NULL) |
| 130 | return; |
| 131 | |
| 132 | #ifdef JENT_PTHREAD |
| 133 | pthread_join(thread_ctx->notime_thread_id, NULL); |
| 134 | pthread_attr_destroy(&thread_ctx->notime_pthread_attr); |
| 135 | #else |
| 136 | thrd_join(thread_ctx->notime_thread_id, NULL); |
| 137 | #endif |
| 138 | } |
| 139 | |
| 140 | static struct jent_notime_thread jent_notime_thread_builtin = { |
| 141 | .jent_notime_init = jent_notime_init, |
| 142 | .jent_notime_fini = jent_notime_fini, |
| 143 | .jent_notime_start = jent_notime_start, |
| 144 | .jent_notime_stop = jent_notime_stop |
| 145 | }; |
| 146 | |
| 147 | /*************************************************************************** |
| 148 | * Timer-less timer replacement |
| 149 | * |
| 150 | * If there is no high-resolution hardware timer available, we create one |
| 151 | * ourselves. This logic is only used when the initialization identifies |
| 152 | * that no suitable time source is available. |
| 153 | ***************************************************************************/ |
| 154 | |
| 155 | static int jent_force_internal_timer = 0; |
| 156 | static int jent_notime_switch_blocked = 0; |
| 157 | |
| 158 | void jent_notime_block_switch(void) |
| 159 | { |
| 160 | jent_notime_switch_blocked = 1; |
| 161 | } |
| 162 | |
| 163 | static struct jent_notime_thread *notime_thread = &jent_notime_thread_builtin; |
| 164 | |
| 165 | /** |
| 166 | * Timer-replacement loop |
| 167 | * |
| 168 | * @brief The measurement loop triggers the read of the value from the |
| 169 | * counter function. It conceptually acts as the low resolution |
| 170 | * samples timer from a ring oscillator. |
| 171 | */ |
| 172 | #ifdef JENT_PTHREAD |
| 173 | static void *jent_notime_sample_timer(void *arg) |
| 174 | #else |
| 175 | static int jent_notime_sample_timer(void *arg) |
| 176 | #endif |
| 177 | { |
| 178 | struct rand_data *ec = (struct rand_data *)arg; |
| 179 | |
| 180 | ec->notime_timer = 0; |
| 181 | |
| 182 | while (1) { |
| 183 | if (ec->notime_interrupt) |
| 184 | goto out; |
| 185 | |
| 186 | ec->notime_timer++; |
| 187 | } |
| 188 | |
| 189 | out: |
| 190 | #ifdef JENT_PTHREAD |
| 191 | return NULL; |
| 192 | #else |
| 193 | return 0; |
| 194 | #endif |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Enable the clock: spawn a new thread that holds a counter. |
| 199 | * |
| 200 | * Note, although creating a thread is expensive, we do that every time a |
| 201 | * caller wants entropy from us and terminate the thread afterwards. This |
| 202 | * is to ensure an attacker cannot easily identify the ticking thread. |
| 203 | */ |
| 204 | int jent_notime_settick(struct rand_data *ec) |
| 205 | { |
| 206 | if (!ec->enable_notime || !notime_thread) |
| 207 | return 0; |
| 208 | |
| 209 | ec->notime_interrupt = 0; |
| 210 | ec->notime_prev_timer = 0; |
| 211 | ec->notime_timer = 0; |
| 212 | |
| 213 | return notime_thread->jent_notime_start(ec->notime_thread_ctx, |
| 214 | jent_notime_sample_timer, ec); |
| 215 | } |
| 216 | |
| 217 | void jent_notime_unsettick(struct rand_data *ec) |
| 218 | { |
| 219 | if (!ec->enable_notime || !notime_thread) |
| 220 | return; |
| 221 | |
| 222 | ec->notime_interrupt = 1; |
| 223 | notime_thread->jent_notime_stop(ec->notime_thread_ctx); |
| 224 | } |
| 225 | |
| 226 | void jent_get_nstime_internal(struct rand_data *ec, uint64_t *out) |
| 227 | { |
| 228 | if (ec->enable_notime) { |
| 229 | /* |
| 230 | * Allow the counting thread to be initialized and guarantee |
| 231 | * that it ticked since last time we looked. |
| 232 | * |
| 233 | * Note, we do not use an atomic operation here for reading |
| 234 | * jent_notime_timer since if this integer is garbled, it even |
| 235 | * adds to entropy. But on most architectures, read/write |
| 236 | * of an uint64_t should be atomic anyway. |
| 237 | */ |
| 238 | while (ec->notime_timer == ec->notime_prev_timer) |
| 239 | jent_yield(); |
| 240 | |
| 241 | ec->notime_prev_timer = ec->notime_timer; |
| 242 | *out = ec->notime_prev_timer; |
| 243 | } else { |
| 244 | jent_get_nstime(out); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static inline int jent_notime_enable_thread(struct rand_data *ec) |
| 249 | { |
| 250 | if (notime_thread) |
| 251 | return notime_thread->jent_notime_init(&ec->notime_thread_ctx); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | void jent_notime_disable(struct rand_data *ec) |
| 256 | { |
| 257 | if (notime_thread) |
| 258 | notime_thread->jent_notime_fini(ec->notime_thread_ctx); |
| 259 | } |
| 260 | |
| 261 | int jent_notime_enable(struct rand_data *ec, unsigned int flags) |
| 262 | { |
| 263 | /* Use internal timer */ |
| 264 | if (jent_force_internal_timer || (flags & JENT_FORCE_INTERNAL_TIMER)) { |
| 265 | /* Self test not run yet */ |
| 266 | if (!jent_force_internal_timer && |
| 267 | jent_time_entropy_init(ec->osr, |
| 268 | flags | JENT_FORCE_INTERNAL_TIMER)) |
| 269 | return EHEALTH; |
| 270 | |
| 271 | ec->enable_notime = 1; |
| 272 | return jent_notime_enable_thread(ec); |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int jent_notime_switch(struct jent_notime_thread *new_thread) |
| 279 | { |
| 280 | if (jent_notime_switch_blocked) |
| 281 | return -EAGAIN; |
| 282 | notime_thread = new_thread; |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | void jent_notime_force(void) |
| 287 | { |
| 288 | jent_force_internal_timer = 1; |
| 289 | } |
| 290 | |
| 291 | int jent_notime_forced(void) |
| 292 | { |
| 293 | return jent_force_internal_timer; |
| 294 | } |
| 295 | |
| 296 | #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */ |