timing.c 15 KB

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