timing.c 14 KB

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