timing.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. LARGE_INTEGER start;
  33. };
  34. #else
  35. #include <unistd.h>
  36. #include <sys/types.h>
  37. #include <signal.h>
  38. /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
  39. * platform matches the ifdefs above, it will be used. */
  40. #include <time.h>
  41. #include <sys/time.h>
  42. struct _hr_time {
  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. QueryPerformanceCounter(&t->start);
  71. return 0;
  72. } else {
  73. unsigned long delta;
  74. LARGE_INTEGER now, hfreq;
  75. QueryPerformanceCounter(&now);
  76. QueryPerformanceFrequency(&hfreq);
  77. delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
  78. / hfreq.QuadPart);
  79. return delta;
  80. }
  81. }
  82. #else /* _WIN32 && !EFIX64 && !EFI32 */
  83. unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
  84. {
  85. struct _hr_time *t = (struct _hr_time *) val;
  86. if (reset) {
  87. gettimeofday(&t->start, NULL);
  88. return 0;
  89. } else {
  90. unsigned long delta;
  91. struct timeval now;
  92. gettimeofday(&now, NULL);
  93. delta = (now.tv_sec - t->start.tv_sec) * 1000ul
  94. + (now.tv_usec - t->start.tv_usec) / 1000;
  95. return delta;
  96. }
  97. }
  98. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  99. /*
  100. * Set delays to watch
  101. */
  102. void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
  103. {
  104. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  105. ctx->int_ms = int_ms;
  106. ctx->fin_ms = fin_ms;
  107. if (fin_ms != 0) {
  108. (void) mbedtls_timing_get_timer(&ctx->timer, 1);
  109. }
  110. }
  111. /*
  112. * Get number of delays expired
  113. */
  114. int mbedtls_timing_get_delay(void *data)
  115. {
  116. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  117. unsigned long elapsed_ms;
  118. if (ctx->fin_ms == 0) {
  119. return -1;
  120. }
  121. elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
  122. if (elapsed_ms >= ctx->fin_ms) {
  123. return 2;
  124. }
  125. if (elapsed_ms >= ctx->int_ms) {
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * Get the final delay.
  132. */
  133. uint32_t mbedtls_timing_get_final_delay(
  134. const mbedtls_timing_delay_context *data)
  135. {
  136. return data->fin_ms;
  137. }
  138. #endif /* !MBEDTLS_TIMING_ALT */
  139. #endif /* MBEDTLS_TIMING_C */