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 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #ifndef MBEDTLS_TIMING_H |
| 23 | #define MBEDTLS_TIMING_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 24 | #include "mbedtls/private_access.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 27 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 32 | #include <stdint.h> |
| 33 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
Ron Eldor | 810e650 | 2018-04-01 15:59:58 +0300 | [diff] [blame] | 38 | #if !defined(MBEDTLS_TIMING_ALT) |
| 39 | // Regular implementation |
| 40 | // |
| 41 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | /** |
| 43 | * \brief timer structure |
| 44 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | struct mbedtls_timing_hr_time |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 47 | unsigned char MBEDTLS_PRIVATE(opaque)[32]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 50 | /** |
| 51 | * \brief Context for mbedtls_timing_set/get_delay() |
| 52 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 53 | typedef struct mbedtls_timing_delay_context |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 54 | { |
Mateusz Starzyk | 8fc95a0 | 2021-06-07 11:28:24 +0200 | [diff] [blame] | 55 | struct mbedtls_timing_hr_time MBEDTLS_PRIVATE(timer); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 56 | uint32_t MBEDTLS_PRIVATE(int_ms); |
| 57 | uint32_t MBEDTLS_PRIVATE(fin_ms); |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 58 | } mbedtls_timing_delay_context; |
| 59 | |
Ron Eldor | 810e650 | 2018-04-01 15:59:58 +0300 | [diff] [blame] | 60 | #else /* MBEDTLS_TIMING_ALT */ |
| 61 | #include "timing_alt.h" |
| 62 | #endif /* MBEDTLS_TIMING_ALT */ |
| 63 | |
TRodziewicz | 15a7b73 | 2021-06-16 11:22:53 +0200 | [diff] [blame] | 64 | /* Internal use */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | 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] | 66 | |
| 67 | /** |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 68 | * \brief Set a pair of delays to watch |
| 69 | * (See \c mbedtls_timing_get_delay().) |
| 70 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 71 | * \param data Pointer to timing data. |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 72 | * 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] | 73 | * \param int_ms First (intermediate) delay in milliseconds. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 74 | * The effect if int_ms > fin_ms is unspecified. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 75 | * \param fin_ms Second (final) delay in milliseconds. |
| 76 | * Pass 0 to cancel the current delay. |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 77 | * |
| 78 | * \note To set a single delay, either use \c mbedtls_timing_set_timer |
| 79 | * directly or use this function with int_ms == fin_ms. |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 80 | */ |
| 81 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ); |
| 82 | |
| 83 | /** |
| 84 | * \brief Get the status of delays |
| 85 | * (Memory helper: number of delays passed.) |
| 86 | * |
| 87 | * \param data Pointer to timing data |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 88 | * 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] | 89 | * |
Gilles Peskine | a9edc48 | 2017-10-10 19:46:45 +0200 | [diff] [blame] | 90 | * \return -1 if cancelled (fin_ms = 0), |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 91 | * 0 if none of the delays are passed, |
| 92 | * 1 if only the intermediate delay is passed, |
| 93 | * 2 if the final delay is passed. |
| 94 | */ |
| 95 | int mbedtls_timing_get_delay( void *data ); |
| 96 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | #ifdef __cplusplus |
| 98 | } |
| 99 | #endif |
| 100 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | #endif /* timing.h */ |