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 |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | * |
| 10 | * This file is provided under the Apache License 2.0, or the |
| 11 | * GNU General Public License v2.0 or later. |
| 12 | * |
| 13 | * ********** |
| 14 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 27 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 28 | * ********** |
| 29 | * |
| 30 | * ********** |
| 31 | * GNU General Public License v2.0 or later: |
| 32 | * |
| 33 | * This program is free software; you can redistribute it and/or modify |
| 34 | * it under the terms of the GNU General Public License as published by |
| 35 | * the Free Software Foundation; either version 2 of the License, or |
| 36 | * (at your option) any later version. |
| 37 | * |
| 38 | * This program is distributed in the hope that it will be useful, |
| 39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 | * GNU General Public License for more details. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License along |
| 44 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 45 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 46 | * |
| 47 | * ********** |
| 48 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 49 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #ifndef MBEDTLS_TIMING_H |
| 52 | #define MBEDTLS_TIMING_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 55 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 56 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 58 | #endif |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 60 | #include <stdint.h> |
| 61 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 62 | #ifdef __cplusplus |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
Ron Eldor | 810e650 | 2018-04-01 15:59:58 +0300 | [diff] [blame] | 66 | #if !defined(MBEDTLS_TIMING_ALT) |
| 67 | // Regular implementation |
| 68 | // |
| 69 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | /** |
| 71 | * \brief timer structure |
| 72 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | struct mbedtls_timing_hr_time |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | { |
| 75 | unsigned char opaque[32]; |
| 76 | }; |
| 77 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 78 | /** |
| 79 | * \brief Context for mbedtls_timing_set/get_delay() |
| 80 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 81 | typedef struct mbedtls_timing_delay_context |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 82 | { |
| 83 | struct mbedtls_timing_hr_time timer; |
| 84 | uint32_t int_ms; |
| 85 | uint32_t fin_ms; |
| 86 | } mbedtls_timing_delay_context; |
| 87 | |
Ron Eldor | 810e650 | 2018-04-01 15:59:58 +0300 | [diff] [blame] | 88 | #else /* MBEDTLS_TIMING_ALT */ |
| 89 | #include "timing_alt.h" |
| 90 | #endif /* MBEDTLS_TIMING_ALT */ |
| 91 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | extern volatile int mbedtls_timing_alarmed; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * \brief Return the CPU cycle counter value |
Manuel Pégourié-Gonnard | 5791109 | 2015-07-01 19:19:49 +0200 | [diff] [blame] | 96 | * |
| 97 | * \warning This is only a best effort! Do not rely on this! |
| 98 | * In particular, it is known to be unreliable on virtual |
| 99 | * machines. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 100 | * |
| 101 | * \note This value starts at an unspecified origin and |
| 102 | * may wrap around. |
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 | unsigned long mbedtls_timing_hardclock( void ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief Return the elapsed time in milliseconds |
| 108 | * |
| 109 | * \param val points to a timer structure |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 110 | * \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] | 111 | * |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 112 | * \return Elapsed time since the previous reset in ms. When |
| 113 | * restarting, this is always 0. |
| 114 | * |
| 115 | * \note To initialize a timer, call this function with reset=1. |
| 116 | * |
| 117 | * Determining the elapsed time and resetting the timer is not |
| 118 | * atomic on all platforms, so after the sequence |
| 119 | * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 = |
| 120 | * get_timer(0) }` the value time1+time2 is only approximately |
| 121 | * the delay since the first reset. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | 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] | 124 | |
| 125 | /** |
| 126 | * \brief Setup an alarm clock |
| 127 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | * \param seconds delay before the "mbedtls_timing_alarmed" flag is set |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 129 | * (must be >=0) |
Manuel Pégourié-Gonnard | dda5213 | 2015-02-11 11:36:31 +0000 | [diff] [blame] | 130 | * |
| 131 | * \warning Only one alarm at a time is supported. In a threaded |
| 132 | * context, this means one for the whole process, not one per |
| 133 | * thread. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | void mbedtls_set_alarm( int seconds ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | |
| 137 | /** |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 138 | * \brief Set a pair of delays to watch |
| 139 | * (See \c mbedtls_timing_get_delay().) |
| 140 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 141 | * \param data Pointer to timing data. |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 142 | * 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] | 143 | * \param int_ms First (intermediate) delay in milliseconds. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 144 | * The effect if int_ms > fin_ms is unspecified. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 145 | * \param fin_ms Second (final) delay in milliseconds. |
| 146 | * Pass 0 to cancel the current delay. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 147 | * |
| 148 | * \note To set a single delay, either use \c mbedtls_timing_set_timer |
| 149 | * directly or use this function with int_ms == fin_ms. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 150 | */ |
| 151 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ); |
| 152 | |
| 153 | /** |
| 154 | * \brief Get the status of delays |
| 155 | * (Memory helper: number of delays passed.) |
| 156 | * |
| 157 | * \param data Pointer to timing data |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 158 | * 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] | 159 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 160 | * \return -1 if cancelled (fin_ms = 0), |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 161 | * 0 if none of the delays are passed, |
| 162 | * 1 if only the intermediate delay is passed, |
| 163 | * 2 if the final delay is passed. |
| 164 | */ |
| 165 | int mbedtls_timing_get_delay( void *data ); |
| 166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 168 | /** |
| 169 | * \brief Checkup routine |
| 170 | * |
| 171 | * \return 0 if successful, or 1 if a test failed |
| 172 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | int mbedtls_timing_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 174 | #endif |
| 175 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | #ifdef __cplusplus |
| 177 | } |
| 178 | #endif |
| 179 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 | #endif /* timing.h */ |