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