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