blob: 7669bb9b3ebdaaab94bd7e3a45bd37ddd575bb3d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file timing.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Gilles Peskinea9edc482017-10-10 19:46:45 +02004 * \brief Portable interface to timeouts and to the CPU cycle counter
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * 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 Bakker5121ce52009-01-03 21:22:43 +000021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_TIMING_H
23#define MBEDTLS_TIMING_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkerf2561b32014-02-06 15:11:55 +010031
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020032#include <stdint.h>
33
Paul Bakker407a0da2013-06-27 14:29:21 +020034#ifdef __cplusplus
35extern "C" {
36#endif
37
Ron Eldor810e6502018-04-01 15:59:58 +030038#if !defined(MBEDTLS_TIMING_ALT)
39// Regular implementation
40//
41
Paul Bakker5121ce52009-01-03 21:22:43 +000042/**
43 * \brief timer structure
44 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045struct mbedtls_timing_hr_time
Paul Bakker5121ce52009-01-03 21:22:43 +000046{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020047 unsigned char MBEDTLS_PRIVATE(opaque)[32];
Paul Bakker5121ce52009-01-03 21:22:43 +000048};
49
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020050/**
51 * \brief Context for mbedtls_timing_set/get_delay()
52 */
Dawid Drozd428cc522018-07-24 10:02:47 +020053typedef struct mbedtls_timing_delay_context
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020054{
Mateusz Starzyk8fc95a02021-06-07 11:28:24 +020055 struct mbedtls_timing_hr_time MBEDTLS_PRIVATE(timer);
Mateusz Starzyk846f0212021-05-19 19:44:07 +020056 uint32_t MBEDTLS_PRIVATE(int_ms);
57 uint32_t MBEDTLS_PRIVATE(fin_ms);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020058} mbedtls_timing_delay_context;
59
Ron Eldor810e6502018-04-01 15:59:58 +030060#else /* MBEDTLS_TIMING_ALT */
61#include "timing_alt.h"
62#endif /* MBEDTLS_TIMING_ALT */
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064extern volatile int mbedtls_timing_alarmed;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66/**
67 * \brief Return the CPU cycle counter value
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +020068 *
69 * \warning This is only a best effort! Do not rely on this!
70 * In particular, it is known to be unreliable on virtual
71 * machines.
Gilles Peskinea9edc482017-10-10 19:46:45 +020072 *
73 * \note This value starts at an unspecified origin and
74 * may wrap around.
Paul Bakker5121ce52009-01-03 21:22:43 +000075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076unsigned long mbedtls_timing_hardclock( void );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/**
79 * \brief Return the elapsed time in milliseconds
80 *
81 * \param val points to a timer structure
Gilles Peskined92f0aa2017-10-16 19:33:06 +020082 * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
Gilles Peskinea9edc482017-10-10 19:46:45 +020083 *
Gilles Peskined92f0aa2017-10-16 19:33:06 +020084 * \return Elapsed time since the previous reset in ms. When
85 * restarting, this is always 0.
86 *
87 * \note To initialize a timer, call this function with reset=1.
88 *
89 * Determining the elapsed time and resetting the timer is not
90 * atomic on all platforms, so after the sequence
91 * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
92 * get_timer(0) }` the value time1+time2 is only approximately
93 * the delay since the first reset.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97/**
98 * \brief Setup an alarm clock
99 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
Gilles Peskinea9edc482017-10-10 19:46:45 +0200101 * (must be >=0)
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000102 *
103 * \warning Only one alarm at a time is supported. In a threaded
104 * context, this means one for the whole process, not one per
105 * thread.
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_set_alarm( int seconds );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200110 * \brief Set a pair of delays to watch
111 * (See \c mbedtls_timing_get_delay().)
112 *
Gilles Peskinea9edc482017-10-10 19:46:45 +0200113 * \param data Pointer to timing data.
Tillmann Karras588ad502015-09-25 04:27:22 +0200114 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200115 * \param int_ms First (intermediate) delay in milliseconds.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200116 * The effect if int_ms > fin_ms is unspecified.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200117 * \param fin_ms Second (final) delay in milliseconds.
118 * Pass 0 to cancel the current delay.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200119 *
120 * \note To set a single delay, either use \c mbedtls_timing_set_timer
121 * directly or use this function with int_ms == fin_ms.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200122 */
123void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
124
125/**
126 * \brief Get the status of delays
127 * (Memory helper: number of delays passed.)
128 *
129 * \param data Pointer to timing data
Tillmann Karras588ad502015-09-25 04:27:22 +0200130 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200131 *
Gilles Peskinea9edc482017-10-10 19:46:45 +0200132 * \return -1 if cancelled (fin_ms = 0),
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200133 * 0 if none of the delays are passed,
134 * 1 if only the intermediate delay is passed,
135 * 2 if the final delay is passed.
136 */
137int mbedtls_timing_get_delay( void *data );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100140/**
141 * \brief Checkup routine
142 *
143 * \return 0 if successful, or 1 if a test failed
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_timing_self_test( int verbose );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100146#endif
147
Paul Bakker5121ce52009-01-03 21:22:43 +0000148#ifdef __cplusplus
149}
150#endif
151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152#endif /* timing.h */