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