timing.h 4.6 KB

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