blob: 9bb1f42fb00e423ce1134a13be95217d6804c71b [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_TIMING_H
52#define MBEDTLS_TIMING_H
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkerf2561b32014-02-06 15:11:55 +010055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakkerf2561b32014-02-06 15:11:55 +010059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_TIMING_ALT)
Paul Bakkerf2561b32014-02-06 15:11:55 +010061// Regular implementation
62//
63
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020064#include <stdint.h>
65
Paul Bakker407a0da2013-06-27 14:29:21 +020066#ifdef __cplusplus
67extern "C" {
68#endif
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
71 * \brief timer structure
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073struct mbedtls_timing_hr_time
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
75 unsigned char opaque[32];
76};
77
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020078/**
79 * \brief Context for mbedtls_timing_set/get_delay()
80 */
81typedef struct
82{
83 struct mbedtls_timing_hr_time timer;
84 uint32_t int_ms;
85 uint32_t fin_ms;
86} mbedtls_timing_delay_context;
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088extern volatile int mbedtls_timing_alarmed;
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/**
91 * \brief Return the CPU cycle counter value
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +020092 *
93 * \warning This is only a best effort! Do not rely on this!
94 * In particular, it is known to be unreliable on virtual
95 * machines.
Gilles Peskinea9edc482017-10-10 19:46:45 +020096 *
97 * \note This value starts at an unspecified origin and
98 * may wrap around.
Paul Bakker5121ce52009-01-03 21:22:43 +000099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100unsigned long mbedtls_timing_hardclock( void );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102/**
103 * \brief Return the elapsed time in milliseconds
104 *
105 * \param val points to a timer structure
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200106 * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200107 *
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200108 * \return Elapsed time since the previous reset in ms. When
109 * restarting, this is always 0.
110 *
111 * \note To initialize a timer, call this function with reset=1.
112 *
113 * Determining the elapsed time and resetting the timer is not
114 * atomic on all platforms, so after the sequence
115 * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
116 * get_timer(0) }` the value time1+time2 is only approximately
117 * the delay since the first reset.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
122 * \brief Setup an alarm clock
123 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
Gilles Peskinea9edc482017-10-10 19:46:45 +0200125 * (must be >=0)
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000126 *
127 * \warning Only one alarm at a time is supported. In a threaded
128 * context, this means one for the whole process, not one per
129 * thread.
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_set_alarm( int seconds );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200134 * \brief Set a pair of delays to watch
135 * (See \c mbedtls_timing_get_delay().)
136 *
Gilles Peskinea9edc482017-10-10 19:46:45 +0200137 * \param data Pointer to timing data.
Tillmann Karras588ad502015-09-25 04:27:22 +0200138 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200139 * \param int_ms First (intermediate) delay in milliseconds.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200140 * The effect if int_ms > fin_ms is unspecified.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200141 * \param fin_ms Second (final) delay in milliseconds.
142 * Pass 0 to cancel the current delay.
Gilles Peskinea9edc482017-10-10 19:46:45 +0200143 *
144 * \note To set a single delay, either use \c mbedtls_timing_set_timer
145 * directly or use this function with int_ms == fin_ms.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200146 */
147void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
148
149/**
150 * \brief Get the status of delays
151 * (Memory helper: number of delays passed.)
152 *
153 * \param data Pointer to timing data
Tillmann Karras588ad502015-09-25 04:27:22 +0200154 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200155 *
Gilles Peskinea9edc482017-10-10 19:46:45 +0200156 * \return -1 if cancelled (fin_ms = 0),
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200157 * 0 if none of the delays are passed,
158 * 1 if only the intermediate delay is passed,
159 * 2 if the final delay is passed.
160 */
161int mbedtls_timing_get_delay( void *data );
162
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200163#ifdef __cplusplus
164}
165#endif
166
167#else /* MBEDTLS_TIMING_ALT */
168#include "timing_alt.h"
169#endif /* MBEDTLS_TIMING_ALT */
170
171#ifdef __cplusplus
172extern "C" {
173#endif
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100176/**
177 * \brief Checkup routine
178 *
179 * \return 0 if successful, or 1 if a test failed
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_timing_self_test( int verbose );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100182#endif
183
Paul Bakker5121ce52009-01-03 21:22:43 +0000184#ifdef __cplusplus
185}
186#endif
187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188#endif /* timing.h */