timing.c 14 KB

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