1 /*
2 * Portable interface to the CPU cycle counter
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #include <string.h>
9
10 #include "common.h"
11
12 #include "mbedtls/platform.h"
13
14 #if defined(MBEDTLS_TIMING_C)
15
16 #include "mbedtls/timing.h"
17
18 #if !defined(MBEDTLS_TIMING_ALT)
19
20 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
21 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
22 !defined(__HAIKU__) && !defined(__midipix__)
23 #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
24 #endif
25
26 /* *INDENT-OFF* */
27 #ifndef asm
28 #define asm __asm
29 #endif
30 /* *INDENT-ON* */
31
32 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
33
34 #include <windows.h>
35 #include <process.h>
36
37 struct _hr_time {
38 LARGE_INTEGER start;
39 };
40
41 #else
42
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <signal.h>
46 /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
47 * platform matches the ifdefs above, it will be used. */
48 #include <time.h>
49 #include <sys/time.h>
50 struct _hr_time {
51 struct timeval start;
52 };
53 #endif /* _WIN32 && !EFIX64 && !EFI32 */
54
55 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
56 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
57
58 #define HAVE_HARDCLOCK
59
mbedtls_timing_hardclock(void)60 unsigned long mbedtls_timing_hardclock(void)
61 {
62 unsigned long tsc;
63 __asm rdtsc
64 __asm mov[tsc], eax
65 return tsc;
66 }
67 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
68 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
69
70 /* some versions of mingw-64 have 32-bit longs even on x84_64 */
71 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
72 defined(__GNUC__) && (defined(__i386__) || ( \
73 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
74
75 #define HAVE_HARDCLOCK
76
mbedtls_timing_hardclock(void)77 unsigned long mbedtls_timing_hardclock(void)
78 {
79 unsigned long lo, hi;
80 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
81 return lo;
82 }
83 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
84 __GNUC__ && __i386__ */
85
86 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
87 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
88
89 #define HAVE_HARDCLOCK
90
mbedtls_timing_hardclock(void)91 unsigned long mbedtls_timing_hardclock(void)
92 {
93 unsigned long lo, hi;
94 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
95 return lo | (hi << 32);
96 }
97 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
98 __GNUC__ && ( __amd64__ || __x86_64__ ) */
99
100 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
101 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
102
103 #define HAVE_HARDCLOCK
104
mbedtls_timing_hardclock(void)105 unsigned long mbedtls_timing_hardclock(void)
106 {
107 unsigned long tbl, tbu0, tbu1;
108
109 do {
110 asm volatile ("mftbu %0" : "=r" (tbu0));
111 asm volatile ("mftb %0" : "=r" (tbl));
112 asm volatile ("mftbu %0" : "=r" (tbu1));
113 } while (tbu0 != tbu1);
114
115 return tbl;
116 }
117 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
118 __GNUC__ && ( __powerpc__ || __ppc__ ) */
119
120 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
121 defined(__GNUC__) && defined(__sparc64__)
122
123 #if defined(__OpenBSD__)
124 #warning OpenBSD does not allow access to tick register using software version instead
125 #else
126 #define HAVE_HARDCLOCK
127
mbedtls_timing_hardclock(void)128 unsigned long mbedtls_timing_hardclock(void)
129 {
130 unsigned long tick;
131 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
132 return tick;
133 }
134 #endif /* __OpenBSD__ */
135 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
136 __GNUC__ && __sparc64__ */
137
138 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
139 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
140
141 #define HAVE_HARDCLOCK
142
mbedtls_timing_hardclock(void)143 unsigned long mbedtls_timing_hardclock(void)
144 {
145 unsigned long tick;
146 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
147 asm volatile ("mov %%g1, %0" : "=r" (tick));
148 return tick;
149 }
150 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
151 __GNUC__ && __sparc__ && !__sparc64__ */
152
153 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
154 defined(__GNUC__) && defined(__alpha__)
155
156 #define HAVE_HARDCLOCK
157
mbedtls_timing_hardclock(void)158 unsigned long mbedtls_timing_hardclock(void)
159 {
160 unsigned long cc;
161 asm volatile ("rpcc %0" : "=r" (cc));
162 return cc & 0xFFFFFFFF;
163 }
164 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
165 __GNUC__ && __alpha__ */
166
167 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
168 defined(__GNUC__) && defined(__ia64__)
169
170 #define HAVE_HARDCLOCK
171
mbedtls_timing_hardclock(void)172 unsigned long mbedtls_timing_hardclock(void)
173 {
174 unsigned long itc;
175 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
176 return itc;
177 }
178 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
179 __GNUC__ && __ia64__ */
180
181 #if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
182 !defined(EFIX64) && !defined(EFI32)
183
184 #define HAVE_HARDCLOCK
185
mbedtls_timing_hardclock(void)186 unsigned long mbedtls_timing_hardclock(void)
187 {
188 LARGE_INTEGER offset;
189
190 QueryPerformanceCounter(&offset);
191
192 return (unsigned long) (offset.QuadPart);
193 }
194 #endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
195
196 #if !defined(HAVE_HARDCLOCK)
197
198 #define HAVE_HARDCLOCK
199
200 static int hardclock_init = 0;
201 static struct timeval tv_init;
202
mbedtls_timing_hardclock(void)203 unsigned long mbedtls_timing_hardclock(void)
204 {
205 struct timeval tv_cur;
206
207 if (hardclock_init == 0) {
208 gettimeofday(&tv_init, NULL);
209 hardclock_init = 1;
210 }
211
212 gettimeofday(&tv_cur, NULL);
213 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
214 + (tv_cur.tv_usec - tv_init.tv_usec);
215 }
216 #endif /* !HAVE_HARDCLOCK */
217
218 volatile int mbedtls_timing_alarmed = 0;
219
220 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
221
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)222 unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
223 {
224 struct _hr_time t;
225
226 if (reset) {
227 QueryPerformanceCounter(&t.start);
228 memcpy(val, &t, sizeof(struct _hr_time));
229 return 0;
230 } else {
231 unsigned long delta;
232 LARGE_INTEGER now, hfreq;
233 /* We can't safely cast val because it may not be aligned, so use memcpy */
234 memcpy(&t, val, sizeof(struct _hr_time));
235 QueryPerformanceCounter(&now);
236 QueryPerformanceFrequency(&hfreq);
237 delta = (unsigned long) ((now.QuadPart - t.start.QuadPart) * 1000ul
238 / hfreq.QuadPart);
239 return delta;
240 }
241 }
242
243 /* It's OK to use a global because alarm() is supposed to be global anyway */
244 static DWORD alarmMs;
245
TimerProc(void * TimerContext)246 static void TimerProc(void *TimerContext)
247 {
248 (void) TimerContext;
249 Sleep(alarmMs);
250 mbedtls_timing_alarmed = 1;
251 /* _endthread will be called implicitly on return
252 * That ensures execution of thread function's epilogue */
253 }
254
mbedtls_set_alarm(int seconds)255 void mbedtls_set_alarm(int seconds)
256 {
257 if (seconds == 0) {
258 /* No need to create a thread for this simple case.
259 * Also, this shorcut is more reliable at least on MinGW32 */
260 mbedtls_timing_alarmed = 1;
261 return;
262 }
263
264 mbedtls_timing_alarmed = 0;
265 alarmMs = seconds * 1000;
266 (void) _beginthread(TimerProc, 0, NULL);
267 }
268
269 #else /* _WIN32 && !EFIX64 && !EFI32 */
270
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)271 unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
272 {
273 struct _hr_time t;
274
275 if (reset) {
276 gettimeofday(&t.start, NULL);
277 memcpy(val, &t, sizeof(struct _hr_time));
278 return 0;
279 } else {
280 unsigned long delta;
281 struct timeval now;
282 /* We can't safely cast val because it may not be aligned, so use memcpy */
283 memcpy(&t, val, sizeof(struct _hr_time));
284 gettimeofday(&now, NULL);
285 delta = (now.tv_sec - t.start.tv_sec) * 1000ul
286 + (now.tv_usec - t.start.tv_usec) / 1000;
287 return delta;
288 }
289 }
290
sighandler(int signum)291 static void sighandler(int signum)
292 {
293 mbedtls_timing_alarmed = 1;
294 signal(signum, sighandler);
295 }
296
mbedtls_set_alarm(int seconds)297 void mbedtls_set_alarm(int seconds)
298 {
299 mbedtls_timing_alarmed = 0;
300 signal(SIGALRM, sighandler);
301 alarm(seconds);
302 if (seconds == 0) {
303 /* alarm(0) cancelled any previous pending alarm, but the
304 handler won't fire, so raise the flag straight away. */
305 mbedtls_timing_alarmed = 1;
306 }
307 }
308
309 #endif /* _WIN32 && !EFIX64 && !EFI32 */
310
311 /*
312 * Set delays to watch
313 */
mbedtls_timing_set_delay(void * data,uint32_t int_ms,uint32_t fin_ms)314 void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
315 {
316 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
317
318 ctx->int_ms = int_ms;
319 ctx->fin_ms = fin_ms;
320
321 if (fin_ms != 0) {
322 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
323 }
324 }
325
326 /*
327 * Get number of delays expired
328 */
mbedtls_timing_get_delay(void * data)329 int mbedtls_timing_get_delay(void *data)
330 {
331 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
332 unsigned long elapsed_ms;
333
334 if (ctx->fin_ms == 0) {
335 return -1;
336 }
337
338 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
339
340 if (elapsed_ms >= ctx->fin_ms) {
341 return 2;
342 }
343
344 if (elapsed_ms >= ctx->int_ms) {
345 return 1;
346 }
347
348 return 0;
349 }
350
351 #endif /* !MBEDTLS_TIMING_ALT */
352
353 #if defined(MBEDTLS_SELF_TEST)
354 /*
355 * Busy-waits for the given number of milliseconds.
356 * Used for testing mbedtls_timing_hardclock.
357 */
busy_msleep(unsigned long msec)358 static void busy_msleep(unsigned long msec)
359 {
360 struct mbedtls_timing_hr_time hires;
361 unsigned long i = 0; /* for busy-waiting */
362 volatile unsigned long j; /* to prevent optimisation */
363
364 (void) mbedtls_timing_get_timer(&hires, 1);
365
366 while (mbedtls_timing_get_timer(&hires, 0) < msec) {
367 i++;
368 }
369
370 j = i;
371 (void) j;
372 }
373
374 #define FAIL do \
375 { \
376 if (verbose != 0) \
377 { \
378 mbedtls_printf("failed at line %d\n", __LINE__); \
379 mbedtls_printf(" cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
380 cycles, ratio, millisecs, secs, hardfail, \
381 (unsigned long) a, (unsigned long) b); \
382 mbedtls_printf(" elapsed(hires)=%lu status(ctx)=%d\n", \
383 mbedtls_timing_get_timer(&hires, 0), \
384 mbedtls_timing_get_delay(&ctx)); \
385 } \
386 return 1; \
387 } while (0)
388
389 /*
390 * Checkup routine
391 *
392 * Warning: this is work in progress, some tests may not be reliable enough
393 * yet! False positives may happen.
394 */
mbedtls_timing_self_test(int verbose)395 int mbedtls_timing_self_test(int verbose)
396 {
397 unsigned long cycles = 0, ratio = 0;
398 unsigned long millisecs = 0, secs = 0;
399 int hardfail = 0;
400 struct mbedtls_timing_hr_time hires;
401 uint32_t a = 0, b = 0;
402 mbedtls_timing_delay_context ctx;
403
404 if (verbose != 0) {
405 mbedtls_printf(" TIMING tests note: will take some time!\n");
406 }
407
408 if (verbose != 0) {
409 mbedtls_printf(" TIMING test #1 (set_alarm / get_timer): ");
410 }
411
412 {
413 secs = 1;
414
415 (void) mbedtls_timing_get_timer(&hires, 1);
416
417 mbedtls_set_alarm((int) secs);
418 while (!mbedtls_timing_alarmed) {
419 ;
420 }
421
422 millisecs = mbedtls_timing_get_timer(&hires, 0);
423
424 /* For some reason on Windows it looks like alarm has an extra delay
425 * (maybe related to creating a new thread). Allow some room here. */
426 if (millisecs < 800 * secs || millisecs > 1200 * secs + 300) {
427 FAIL;
428 }
429 }
430
431 if (verbose != 0) {
432 mbedtls_printf("passed\n");
433 }
434
435 if (verbose != 0) {
436 mbedtls_printf(" TIMING test #2 (set/get_delay ): ");
437 }
438
439 {
440 a = 800;
441 b = 400;
442 mbedtls_timing_set_delay(&ctx, a, a + b); /* T = 0 */
443
444 busy_msleep(a - a / 4); /* T = a - a/4 */
445 if (mbedtls_timing_get_delay(&ctx) != 0) {
446 FAIL;
447 }
448
449 busy_msleep(a / 4 + b / 4); /* T = a + b/4 */
450 if (mbedtls_timing_get_delay(&ctx) != 1) {
451 FAIL;
452 }
453
454 busy_msleep(b); /* T = a + b + b/4 */
455 if (mbedtls_timing_get_delay(&ctx) != 2) {
456 FAIL;
457 }
458 }
459
460 mbedtls_timing_set_delay(&ctx, 0, 0);
461 busy_msleep(200);
462 if (mbedtls_timing_get_delay(&ctx) != -1) {
463 FAIL;
464 }
465
466 if (verbose != 0) {
467 mbedtls_printf("passed\n");
468 }
469
470 if (verbose != 0) {
471 mbedtls_printf(" TIMING test #3 (hardclock / get_timer): ");
472 }
473
474 /*
475 * Allow one failure for possible counter wrapping.
476 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
477 * since the whole test is about 10ms, it shouldn't happen twice in a row.
478 */
479
480 hard_test:
481 if (hardfail > 1) {
482 if (verbose != 0) {
483 mbedtls_printf("failed (ignored)\n");
484 }
485
486 goto hard_test_done;
487 }
488
489 /* Get a reference ratio cycles/ms */
490 millisecs = 1;
491 cycles = mbedtls_timing_hardclock();
492 busy_msleep(millisecs);
493 cycles = mbedtls_timing_hardclock() - cycles;
494 ratio = cycles / millisecs;
495
496 /* Check that the ratio is mostly constant */
497 for (millisecs = 2; millisecs <= 4; millisecs++) {
498 cycles = mbedtls_timing_hardclock();
499 busy_msleep(millisecs);
500 cycles = mbedtls_timing_hardclock() - cycles;
501
502 /* Allow variation up to 20% */
503 if (cycles / millisecs < ratio - ratio / 5 ||
504 cycles / millisecs > ratio + ratio / 5) {
505 hardfail++;
506 goto hard_test;
507 }
508 }
509
510 if (verbose != 0) {
511 mbedtls_printf("passed\n");
512 }
513
514 hard_test_done:
515
516 if (verbose != 0) {
517 mbedtls_printf("\n");
518 }
519
520 return 0;
521 }
522
523 #endif /* MBEDTLS_SELF_TEST */
524 #endif /* MBEDTLS_TIMING_C */
525