timing.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "common.h"
  20. #if defined(MBEDTLS_TIMING_C)
  21. #include "mbedtls/timing.h"
  22. #if !defined(MBEDTLS_TIMING_ALT)
  23. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  24. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  25. !defined(__HAIKU__) && !defined(__midipix__)
  26. #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
  27. #endif
  28. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  29. #include <windows.h>
  30. #include <process.h>
  31. struct _hr_time
  32. {
  33. LARGE_INTEGER start;
  34. };
  35. #else
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <signal.h>
  40. #include <time.h>
  41. struct _hr_time
  42. {
  43. struct timeval start;
  44. };
  45. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  46. /**
  47. * \brief Return the elapsed time in milliseconds
  48. *
  49. * \warning May change without notice
  50. *
  51. * \param val points to a timer structure
  52. * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
  53. *
  54. * \return Elapsed time since the previous reset in ms. When
  55. * restarting, this is always 0.
  56. *
  57. * \note To initialize a timer, call this function with reset=1.
  58. *
  59. * Determining the elapsed time and resetting the timer is not
  60. * atomic on all platforms, so after the sequence
  61. * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
  62. * get_timer(0) }` the value time1+time2 is only approximately
  63. * the delay since the first reset.
  64. */
  65. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  66. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
  67. {
  68. struct _hr_time *t = (struct _hr_time *) val;
  69. if( reset )
  70. {
  71. QueryPerformanceCounter( &t->start );
  72. return( 0 );
  73. }
  74. else
  75. {
  76. unsigned long delta;
  77. LARGE_INTEGER now, hfreq;
  78. QueryPerformanceCounter( &now );
  79. QueryPerformanceFrequency( &hfreq );
  80. delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
  81. / hfreq.QuadPart );
  82. return( delta );
  83. }
  84. }
  85. #else /* _WIN32 && !EFIX64 && !EFI32 */
  86. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
  87. {
  88. struct _hr_time *t = (struct _hr_time *) val;
  89. if( reset )
  90. {
  91. gettimeofday( &t->start, NULL );
  92. return( 0 );
  93. }
  94. else
  95. {
  96. unsigned long delta;
  97. struct timeval now;
  98. gettimeofday( &now, NULL );
  99. delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
  100. + ( now.tv_usec - t->start.tv_usec ) / 1000;
  101. return( delta );
  102. }
  103. }
  104. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  105. /*
  106. * Set delays to watch
  107. */
  108. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
  109. {
  110. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  111. ctx->int_ms = int_ms;
  112. ctx->fin_ms = fin_ms;
  113. if( fin_ms != 0 )
  114. (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
  115. }
  116. /*
  117. * Get number of delays expired
  118. */
  119. int mbedtls_timing_get_delay( void *data )
  120. {
  121. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  122. unsigned long elapsed_ms;
  123. if( ctx->fin_ms == 0 )
  124. return( -1 );
  125. elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
  126. if( elapsed_ms >= ctx->fin_ms )
  127. return( 2 );
  128. if( elapsed_ms >= ctx->int_ms )
  129. return( 1 );
  130. return( 0 );
  131. }
  132. #endif /* !MBEDTLS_TIMING_ALT */
  133. #endif /* MBEDTLS_TIMING_C */