blob: bbc8fff7630cc8358fc5086f899adb24200e7526 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_TIMING_H
11#define MBEDTLS_TIMING_H
Paul Bakker5121ce52009-01-03 21:22:43 +000012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Paul Bakkerf2561b32014-02-06 15:11:55 +010018
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020019#include <stdint.h>
20
Paul Bakker407a0da2013-06-27 14:29:21 +020021#ifdef __cplusplus
22extern "C" {
23#endif
24
Ron Eldor810e6502018-04-01 15:59:58 +030025#if !defined(MBEDTLS_TIMING_ALT)
26// Regular implementation
27//
28
Paul Bakker5121ce52009-01-03 21:22:43 +000029/**
30 * \brief timer structure
31 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032struct mbedtls_timing_hr_time {
Paul Bakker5121ce52009-01-03 21:22:43 +000033 unsigned char opaque[32];
34};
35
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020036/**
37 * \brief Context for mbedtls_timing_set/get_delay()
38 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039typedef struct mbedtls_timing_delay_context {
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020040 struct mbedtls_timing_hr_time timer;
41 uint32_t int_ms;
42 uint32_t fin_ms;
43} mbedtls_timing_delay_context;
44
Ron Eldor810e6502018-04-01 15:59:58 +030045#else /* MBEDTLS_TIMING_ALT */
46#include "timing_alt.h"
47#endif /* MBEDTLS_TIMING_ALT */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049extern volatile int mbedtls_timing_alarmed;
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51/**
52 * \brief Return the CPU cycle counter value
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +020053 *
54 * \warning This is only a best effort! Do not rely on this!
55 * In particular, it is known to be unreliable on virtual
56 * machines.
Gilles Peskinea9edc482017-10-10 19:46:45 +020057 *
58 * \note This value starts at an unspecified origin and
59 * may wrap around.
Paul Bakker5121ce52009-01-03 21:22:43 +000060 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061unsigned long mbedtls_timing_hardclock(void);
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63/**
64 * \brief Return the elapsed time in milliseconds
65 *
66 * \param val points to a timer structure
Gilles Peskined92f0aa2017-10-16 19:33:06 +020067 * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
Gilles Peskinea9edc482017-10-10 19:46:45 +020068 *
Gilles Peskined92f0aa2017-10-16 19:33:06 +020069 * \return Elapsed time since the previous reset in ms. When
70 * restarting, this is always 0.
71 *
72 * \note To initialize a timer, call this function with reset=1.
73 *
74 * Determining the elapsed time and resetting the timer is not
75 * atomic on all platforms, so after the sequence
76 * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
77 * get_timer(0) }` the value time1+time2 is only approximately
78 * the delay since the first reset.
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset);
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82/**
83 * \brief Setup an alarm clock
84 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
Gilles Peskinea9edc482017-10-10 19:46:45 +020086 * (must be >=0)
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +000087 *
88 * \warning Only one alarm at a time is supported. In a threaded
89 * context, this means one for the whole process, not one per
90 * thread.
Paul Bakker5121ce52009-01-03 21:22:43 +000091 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092void mbedtls_set_alarm(int seconds);
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020095 * \brief Set a pair of delays to watch
96 * (See \c mbedtls_timing_get_delay().)
97 *
Gilles Peskinea9edc482017-10-10 19:46:45 +020098 * \param data Pointer to timing data.
Tillmann Karras588ad502015-09-25 04:27:22 +020099 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200100 * \param int_ms First (intermediate) delay in milliseconds.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200101 * The effect if int_ms > fin_ms is unspecified.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200102 * \param fin_ms Second (final) delay in milliseconds.
103 * Pass 0 to cancel the current delay.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200104 *
105 * \note To set a single delay, either use \c mbedtls_timing_set_timer
106 * directly or use this function with int_ms == fin_ms.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200109
110/**
111 * \brief Get the status of delays
112 * (Memory helper: number of delays passed.)
113 *
114 * \param data Pointer to timing data
Tillmann Karras588ad502015-09-25 04:27:22 +0200115 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200116 *
Gilles Peskinea9edc482017-10-10 19:46:45 +0200117 * \return -1 if cancelled (fin_ms = 0),
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200118 * 0 if none of the delays are passed,
119 * 1 if only the intermediate delay is passed,
120 * 2 if the final delay is passed.
121 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122int mbedtls_timing_get_delay(void *data);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100125/**
126 * \brief Checkup routine
127 *
128 * \return 0 if successful, or 1 if a test failed
129 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130int mbedtls_timing_self_test(int verbose);
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100131#endif
132
Paul Bakker5121ce52009-01-03 21:22:43 +0000133#ifdef __cplusplus
134}
135#endif
136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137#endif /* timing.h */