blob: 2d4a19c01af420c2fb50263be8b56ed869a88184 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakkerf2561b32014-02-06 15:11:55 +010027
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020028#include <stdint.h>
29
Paul Bakker407a0da2013-06-27 14:29:21 +020030#ifdef __cplusplus
31extern "C" {
32#endif
33
Ron Eldor810e6502018-04-01 15:59:58 +030034#if !defined(MBEDTLS_TIMING_ALT)
35// Regular implementation
36//
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038/**
39 * \brief timer structure
40 */
Gilles Peskine449bd832023-01-11 14:50:10 +010041struct mbedtls_timing_hr_time {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020042 unsigned char MBEDTLS_PRIVATE(opaque)[32];
Paul Bakker5121ce52009-01-03 21:22:43 +000043};
44
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020045/**
46 * \brief Context for mbedtls_timing_set/get_delay()
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048typedef struct mbedtls_timing_delay_context {
Mateusz Starzyk8fc95a02021-06-07 11:28:24 +020049 struct mbedtls_timing_hr_time MBEDTLS_PRIVATE(timer);
Mateusz Starzyk846f0212021-05-19 19:44:07 +020050 uint32_t MBEDTLS_PRIVATE(int_ms);
51 uint32_t MBEDTLS_PRIVATE(fin_ms);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020052} mbedtls_timing_delay_context;
53
Ron Eldor810e6502018-04-01 15:59:58 +030054#else /* MBEDTLS_TIMING_ALT */
55#include "timing_alt.h"
56#endif /* MBEDTLS_TIMING_ALT */
57
TRodziewicz15a7b732021-06-16 11:22:53 +020058/* Internal use */
Gilles Peskine449bd832023-01-11 14:50:10 +010059unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset);
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020062 * \brief Set a pair of delays to watch
63 * (See \c mbedtls_timing_get_delay().)
64 *
Gilles Peskinea9edc482017-10-10 19:46:45 +020065 * \param data Pointer to timing data.
Tillmann Karras588ad502015-09-25 04:27:22 +020066 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020067 * \param int_ms First (intermediate) delay in milliseconds.
Gilles Peskinea9edc482017-10-10 19:46:45 +020068 * The effect if int_ms > fin_ms is unspecified.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020069 * \param fin_ms Second (final) delay in milliseconds.
70 * Pass 0 to cancel the current delay.
Gilles Peskinea9edc482017-10-10 19:46:45 +020071 *
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é-Gonnardca3bdc52015-05-12 20:17:06 +020074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020076
77/**
78 * \brief Get the status of delays
79 * (Memory helper: number of delays passed.)
80 *
81 * \param data Pointer to timing data
Tillmann Karras588ad502015-09-25 04:27:22 +020082 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020083 *
Gilles Peskinea9edc482017-10-10 19:46:45 +020084 * \return -1 if cancelled (fin_ms = 0),
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020085 * 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 Peskine449bd832023-01-11 14:50:10 +010089int mbedtls_timing_get_delay(void *data);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020090
Paul Elliottb9af2db2022-03-09 15:34:37 +000091/**
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 */
99uint32_t mbedtls_timing_get_final_delay(
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 const mbedtls_timing_delay_context *data);
Paul Elliottb9af2db2022-03-09 15:34:37 +0000101
Paul Bakker5121ce52009-01-03 21:22:43 +0000102#ifdef __cplusplus
103}
104#endif
105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106#endif /* timing.h */