timing.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * \file timing.h
  3. *
  4. * \brief Portable interface to timeouts and to the CPU cycle counter
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef MBEDTLS_TIMING_H
  23. #define MBEDTLS_TIMING_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #include <stdint.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #if !defined(MBEDTLS_TIMING_ALT)
  34. // Regular implementation
  35. //
  36. /**
  37. * \brief timer structure
  38. */
  39. struct mbedtls_timing_hr_time
  40. {
  41. unsigned char opaque[32];
  42. };
  43. /**
  44. * \brief Context for mbedtls_timing_set/get_delay()
  45. */
  46. typedef struct mbedtls_timing_delay_context
  47. {
  48. struct mbedtls_timing_hr_time timer;
  49. uint32_t int_ms;
  50. uint32_t fin_ms;
  51. } mbedtls_timing_delay_context;
  52. #else /* MBEDTLS_TIMING_ALT */
  53. #include "timing_alt.h"
  54. #endif /* MBEDTLS_TIMING_ALT */
  55. extern volatile int mbedtls_timing_alarmed;
  56. /**
  57. * \brief Return the CPU cycle counter value
  58. *
  59. * \warning This is only a best effort! Do not rely on this!
  60. * In particular, it is known to be unreliable on virtual
  61. * machines.
  62. *
  63. * \note This value starts at an unspecified origin and
  64. * may wrap around.
  65. */
  66. unsigned long mbedtls_timing_hardclock( void );
  67. /**
  68. * \brief Return the elapsed time in milliseconds
  69. *
  70. * \param val points to a timer structure
  71. * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
  72. *
  73. * \return Elapsed time since the previous reset in ms. When
  74. * restarting, this is always 0.
  75. *
  76. * \note To initialize a timer, call this function with reset=1.
  77. *
  78. * Determining the elapsed time and resetting the timer is not
  79. * atomic on all platforms, so after the sequence
  80. * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
  81. * get_timer(0) }` the value time1+time2 is only approximately
  82. * the delay since the first reset.
  83. */
  84. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
  85. /**
  86. * \brief Setup an alarm clock
  87. *
  88. * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
  89. * (must be >=0)
  90. *
  91. * \warning Only one alarm at a time is supported. In a threaded
  92. * context, this means one for the whole process, not one per
  93. * thread.
  94. */
  95. void mbedtls_set_alarm( int seconds );
  96. /**
  97. * \brief Set a pair of delays to watch
  98. * (See \c mbedtls_timing_get_delay().)
  99. *
  100. * \param data Pointer to timing data.
  101. * Must point to a valid \c mbedtls_timing_delay_context struct.
  102. * \param int_ms First (intermediate) delay in milliseconds.
  103. * The effect if int_ms > fin_ms is unspecified.
  104. * \param fin_ms Second (final) delay in milliseconds.
  105. * Pass 0 to cancel the current delay.
  106. *
  107. * \note To set a single delay, either use \c mbedtls_timing_set_timer
  108. * directly or use this function with int_ms == fin_ms.
  109. */
  110. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
  111. /**
  112. * \brief Get the status of delays
  113. * (Memory helper: number of delays passed.)
  114. *
  115. * \param data Pointer to timing data
  116. * Must point to a valid \c mbedtls_timing_delay_context struct.
  117. *
  118. * \return -1 if cancelled (fin_ms = 0),
  119. * 0 if none of the delays are passed,
  120. * 1 if only the intermediate delay is passed,
  121. * 2 if the final delay is passed.
  122. */
  123. int mbedtls_timing_get_delay( void *data );
  124. #if defined(MBEDTLS_SELF_TEST)
  125. /**
  126. * \brief Checkup routine
  127. *
  128. * \return 0 if successful, or 1 if a test failed
  129. */
  130. int mbedtls_timing_self_test( int verbose );
  131. #endif
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif /* timing.h */