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