timing.c 13 KB

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