Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file timing.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 4 | * \brief Portable interface to timeouts and to the CPU cycle counter |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 21 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_TIMING_H |
| 25 | #define MBEDTLS_TIMING_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_TIMING_ALT) |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 34 | // Regular implementation |
| 35 | // |
| 36 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | /** |
| 44 | * \brief timer structure |
| 45 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | struct mbedtls_timing_hr_time |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | { |
| 48 | unsigned char opaque[32]; |
| 49 | }; |
| 50 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 51 | /** |
| 52 | * \brief Context for mbedtls_timing_set/get_delay() |
| 53 | */ |
| 54 | typedef struct |
| 55 | { |
| 56 | struct mbedtls_timing_hr_time timer; |
| 57 | uint32_t int_ms; |
| 58 | uint32_t fin_ms; |
| 59 | } mbedtls_timing_delay_context; |
| 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | extern volatile int mbedtls_timing_alarmed; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * \brief Return the CPU cycle counter value |
Manuel Pégourié-Gonnard | 5791109 | 2015-07-01 19:19:49 +0200 | [diff] [blame] | 65 | * |
| 66 | * \warning This is only a best effort! Do not rely on this! |
| 67 | * In particular, it is known to be unreliable on virtual |
| 68 | * machines. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 69 | * |
| 70 | * \note This value starts at an unspecified origin and |
| 71 | * may wrap around. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | unsigned long mbedtls_timing_hardclock( void ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * \brief Return the elapsed time in milliseconds |
| 77 | * |
| 78 | * \param val points to a timer structure |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 79 | * \param reset If 0, query the elapsed time. Otherwise (re)start the timer. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 80 | * |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 81 | * \return Elapsed time since the previous reset in ms. When |
| 82 | * restarting, this is always 0. |
| 83 | * |
| 84 | * \note To initialize a timer, call this function with reset=1. |
| 85 | * |
| 86 | * Determining the elapsed time and resetting the timer is not |
| 87 | * atomic on all platforms, so after the sequence |
| 88 | * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 = |
| 89 | * get_timer(0) }` the value time1+time2 is only approximately |
| 90 | * the delay since the first reset. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * \brief Setup an alarm clock |
| 96 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | * \param seconds delay before the "mbedtls_timing_alarmed" flag is set |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 98 | * (must be >=0) |
Manuel Pégourié-Gonnard | dda5213 | 2015-02-11 11:36:31 +0000 | [diff] [blame] | 99 | * |
| 100 | * \warning Only one alarm at a time is supported. In a threaded |
| 101 | * context, this means one for the whole process, not one per |
| 102 | * thread. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | void mbedtls_set_alarm( int seconds ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 107 | * \brief Set a pair of delays to watch |
| 108 | * (See \c mbedtls_timing_get_delay().) |
| 109 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 110 | * \param data Pointer to timing data. |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 111 | * Must point to a valid \c mbedtls_timing_delay_context struct. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 112 | * \param int_ms First (intermediate) delay in milliseconds. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 113 | * The effect if int_ms > fin_ms is unspecified. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 114 | * \param fin_ms Second (final) delay in milliseconds. |
| 115 | * Pass 0 to cancel the current delay. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 116 | * |
| 117 | * \note To set a single delay, either use \c mbedtls_timing_set_timer |
| 118 | * directly or use this function with int_ms == fin_ms. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 119 | */ |
| 120 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ); |
| 121 | |
| 122 | /** |
| 123 | * \brief Get the status of delays |
| 124 | * (Memory helper: number of delays passed.) |
| 125 | * |
| 126 | * \param data Pointer to timing data |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 127 | * Must point to a valid \c mbedtls_timing_delay_context struct. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 128 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 129 | * \return -1 if cancelled (fin_ms = 0), |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 130 | * 0 if none of the delays are passed, |
| 131 | * 1 if only the intermediate delay is passed, |
| 132 | * 2 if the final delay is passed. |
| 133 | */ |
| 134 | int mbedtls_timing_get_delay( void *data ); |
| 135 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 136 | #ifdef __cplusplus |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #else /* MBEDTLS_TIMING_ALT */ |
| 141 | #include "timing_alt.h" |
| 142 | #endif /* MBEDTLS_TIMING_ALT */ |
| 143 | |
| 144 | #ifdef __cplusplus |
| 145 | extern "C" { |
| 146 | #endif |
| 147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 149 | /** |
| 150 | * \brief Checkup routine |
| 151 | * |
| 152 | * \return 0 if successful, or 1 if a test failed |
| 153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | int mbedtls_timing_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 155 | #endif |
| 156 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | #ifdef __cplusplus |
| 158 | } |
| 159 | #endif |
| 160 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | #endif /* timing.h */ |