timing.c 3.9 KB

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