Debian Codegraph @

random/jitterentropy-timer.c

raw
1 /* Jitter RNG: Internal timer implementation
2 *
3 * Copyright (C) 2021, 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 #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
25
26 /***************************************************************************
27 * Thread handler
28 ***************************************************************************/
29
30 JENT_PRIVATE_STATIC
31 int jent_notime_init(void **ctx)
32 {
33 struct jent_notime_ctx *thread_ctx;
34 long ncpu = jent_ncpu();
35
36 if (ncpu < 0)
37 return (int)ncpu;
38
39 /* We need at least two CPUs to enable the timer thread */
40 if (ncpu < 2)
41 return -EOPNOTSUPP;
42
43 thread_ctx = calloc(1, sizeof(struct jent_notime_ctx));
44 if (!thread_ctx)
45 return -errno;
46
47 *ctx = thread_ctx;
48
49 return 0;
50 }
51
52 JENT_PRIVATE_STATIC
53 void jent_notime_fini(void *ctx)
54 {
55 struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx;
56
57 if (thread_ctx)
58 free(thread_ctx);
59 }
60
61 static int jent_notime_start(void *ctx,
62 void *(*start_routine) (void *), void *arg)
63 {
64 struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx;
65 int ret;
66
67 if (!thread_ctx)
68 return -EINVAL;
69
70 ret = -pthread_attr_init(&thread_ctx->notime_pthread_attr);
71 if (ret)
72 return ret;
73
74 return -pthread_create(&thread_ctx->notime_thread_id,
75 &thread_ctx->notime_pthread_attr,
76 start_routine, arg);
77 }
78
79 static void jent_notime_stop(void *ctx)
80 {
81 struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx;
82
83 pthread_join(thread_ctx->notime_thread_id, NULL);
84 pthread_attr_destroy(&thread_ctx->notime_pthread_attr);
85 }
86
87 static struct jent_notime_thread jent_notime_thread_builtin = {
88 .jent_notime_init = jent_notime_init,
89 .jent_notime_fini = jent_notime_fini,
90 .jent_notime_start = jent_notime_start,
91 .jent_notime_stop = jent_notime_stop
92 };
93
94 /***************************************************************************
95 * Timer-less timer replacement
96 *
97 * If there is no high-resolution hardware timer available, we create one
98 * ourselves. This logic is only used when the initialization identifies
99 * that no suitable time source is available.
100 ***************************************************************************/
101
102 static int jent_force_internal_timer = 0;
103 static int jent_notime_switch_blocked = 0;
104
105 void jent_notime_block_switch(void)
106 {
107 jent_notime_switch_blocked = 1;
108 }
109
110 static struct jent_notime_thread *notime_thread = &jent_notime_thread_builtin;
111
112 /**
113 * Timer-replacement loop
114 *
115 * @brief The measurement loop triggers the read of the value from the
116 * counter function. It conceptually acts as the low resolution
117 * samples timer from a ring oscillator.
118 */
119 static void *jent_notime_sample_timer(void *arg)
120 {
121 struct rand_data *ec = (struct rand_data *)arg;
122
123 ec->notime_timer = 0;
124
125 while (1) {
126 if (ec->notime_interrupt)
127 return NULL;
128
129 ec->notime_timer++;
130 }
131
132 return NULL;
133 }
134
135 /*
136 * Enable the clock: spawn a new thread that holds a counter.
137 *
138 * Note, although creating a thread is expensive, we do that every time a
139 * caller wants entropy from us and terminate the thread afterwards. This
140 * is to ensure an attacker cannot easily identify the ticking thread.
141 */
142 int jent_notime_settick(struct rand_data *ec)
143 {
144 if (!ec->enable_notime || !notime_thread)
145 return 0;
146
147 ec->notime_interrupt = 0;
148 ec->notime_prev_timer = 0;
149 ec->notime_timer = 0;
150
151 return notime_thread->jent_notime_start(ec->notime_thread_ctx,
152 jent_notime_sample_timer, ec);
153 }
154
155 void jent_notime_unsettick(struct rand_data *ec)
156 {
157 if (!ec->enable_notime || !notime_thread)
158 return;
159
160 ec->notime_interrupt = 1;
161 notime_thread->jent_notime_stop(ec->notime_thread_ctx);
162 }
163
164 void jent_get_nstime_internal(struct rand_data *ec, uint64_t *out)
165 {
166 if (ec->enable_notime) {
167 /*
168 * Allow the counting thread to be initialized and guarantee
169 * that it ticked since last time we looked.
170 *
171 * Note, we do not use an atomic operation here for reading
172 * jent_notime_timer since if this integer is garbled, it even
173 * adds to entropy. But on most architectures, read/write
174 * of an uint64_t should be atomic anyway.
175 */
176 while (ec->notime_timer == ec->notime_prev_timer)
177 jent_yield();
178
179 ec->notime_prev_timer = ec->notime_timer;
180 *out = ec->notime_prev_timer;
181 } else {
182 jent_get_nstime(out);
183 }
184 }
185
186 static inline int jent_notime_enable_thread(struct rand_data *ec)
187 {
188 if (notime_thread)
189 return notime_thread->jent_notime_init(&ec->notime_thread_ctx);
190 return 0;
191 }
192
193 void jent_notime_disable(struct rand_data *ec)
194 {
195 if (notime_thread)
196 notime_thread->jent_notime_fini(ec->notime_thread_ctx);
197 }
198
199 int jent_notime_enable(struct rand_data *ec, unsigned int flags)
200 {
201 /* Use internal timer */
202 if (jent_force_internal_timer || (flags & JENT_FORCE_INTERNAL_TIMER)) {
203 /* Self test not run yet */
204 if (!jent_force_internal_timer &&
205 jent_time_entropy_init(flags | JENT_FORCE_INTERNAL_TIMER,
206 ec->osr))
207 return EHEALTH;
208
209 ec->enable_notime = 1;
210 return jent_notime_enable_thread(ec);
211 }
212
213 return 0;
214 }
215
216 int jent_notime_switch(struct jent_notime_thread *new_thread)
217 {
218 if (jent_notime_switch_blocked)
219 return -EAGAIN;
220 notime_thread = new_thread;
221 return 0;
222 }
223
224 void jent_notime_force(void)
225 {
226 jent_force_internal_timer = 1;
227 }
228
229 int jent_notime_forced(void)
230 {
231 return jent_force_internal_timer;
232 }
233
234 #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */