blob: 62ae1022d91d355edb71e5694e8e347d6059f07f [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 Rodgman16799db2023-11-02 19:47:20 +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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Paul Bakkerf2561b32014-02-06 15:11:55 +010015
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020016#include <stdint.h>
17
Paul Bakker407a0da2013-06-27 14:29:21 +020018#ifdef __cplusplus
19extern "C" {
20#endif
21
Ron Eldor810e6502018-04-01 15:59:58 +030022#if !defined(MBEDTLS_TIMING_ALT)
23// Regular implementation
24//
25
Paul Bakker5121ce52009-01-03 21:22:43 +000026/**
27 * \brief timer structure
28 */
Gilles Peskine449bd832023-01-11 14:50:10 +010029struct mbedtls_timing_hr_time {
Dave Rodgman33b22102023-03-31 11:40:24 +010030 uint64_t MBEDTLS_PRIVATE(opaque)[4];
Paul Bakker5121ce52009-01-03 21:22:43 +000031};
32
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020033/**
34 * \brief Context for mbedtls_timing_set/get_delay()
35 */
Gilles Peskine449bd832023-01-11 14:50:10 +010036typedef struct mbedtls_timing_delay_context {
Mateusz Starzyk8fc95a02021-06-07 11:28:24 +020037 struct mbedtls_timing_hr_time MBEDTLS_PRIVATE(timer);
Mateusz Starzyk846f0212021-05-19 19:44:07 +020038 uint32_t MBEDTLS_PRIVATE(int_ms);
39 uint32_t MBEDTLS_PRIVATE(fin_ms);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020040} mbedtls_timing_delay_context;
41
Ron Eldor810e6502018-04-01 15:59:58 +030042#else /* MBEDTLS_TIMING_ALT */
43#include "timing_alt.h"
44#endif /* MBEDTLS_TIMING_ALT */
45
TRodziewicz15a7b732021-06-16 11:22:53 +020046/* Internal use */
Gilles Peskine449bd832023-01-11 14:50:10 +010047unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset);
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020050 * \brief Set a pair of delays to watch
51 * (See \c mbedtls_timing_get_delay().)
52 *
Gilles Peskinea9edc482017-10-10 19:46:45 +020053 * \param data Pointer to timing data.
Tillmann Karras588ad502015-09-25 04:27:22 +020054 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020055 * \param int_ms First (intermediate) delay in milliseconds.
Gilles Peskinea9edc482017-10-10 19:46:45 +020056 * The effect if int_ms > fin_ms is unspecified.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020057 * \param fin_ms Second (final) delay in milliseconds.
58 * Pass 0 to cancel the current delay.
Gilles Peskinea9edc482017-10-10 19:46:45 +020059 *
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é-Gonnardca3bdc52015-05-12 20:17:06 +020062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020064
65/**
66 * \brief Get the status of delays
67 * (Memory helper: number of delays passed.)
68 *
69 * \param data Pointer to timing data
Tillmann Karras588ad502015-09-25 04:27:22 +020070 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020071 *
Gilles Peskinea9edc482017-10-10 19:46:45 +020072 * \return -1 if cancelled (fin_ms = 0),
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020073 * 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 Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_timing_get_delay(void *data);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020078
Paul Elliottb9af2db2022-03-09 15:34:37 +000079/**
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 */
87uint32_t mbedtls_timing_get_final_delay(
Gilles Peskine449bd832023-01-11 14:50:10 +010088 const mbedtls_timing_delay_context *data);
Paul Elliottb9af2db2022-03-09 15:34:37 +000089
Paul Bakker5121ce52009-01-03 21:22:43 +000090#ifdef __cplusplus
91}
92#endif
93
Paul Bakker5121ce52009-01-03 21:22:43 +000094#endif /* timing.h */