blob: 579de331018b56aba733822c8a6ac9ff5a1c62dc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file timing.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Gilles Peskine02c31672017-10-10 19:46:45 +02004 * \brief Portable interface to timeouts and to the CPU cycle counter
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_TIMING_H
24#define MBEDTLS_TIMING_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkerf2561b32014-02-06 15:11:55 +010027#include "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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_TIMING_ALT)
Paul Bakkerf2561b32014-02-06 15:11:55 +010033// Regular implementation
34//
35
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020036#include <stdint.h>
37
Paul Bakker407a0da2013-06-27 14:29:21 +020038#ifdef __cplusplus
39extern "C" {
40#endif
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{
47 unsigned char opaque[32];
48};
49
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020050/**
51 * \brief Context for mbedtls_timing_set/get_delay()
52 */
53typedef struct
54{
55 struct mbedtls_timing_hr_time timer;
56 uint32_t int_ms;
57 uint32_t fin_ms;
58} mbedtls_timing_delay_context;
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060extern volatile int mbedtls_timing_alarmed;
Paul Bakker5121ce52009-01-03 21:22:43 +000061
62/**
63 * \brief Return the CPU cycle counter value
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +020064 *
65 * \warning This is only a best effort! Do not rely on this!
66 * In particular, it is known to be unreliable on virtual
67 * machines.
Gilles Peskine02c31672017-10-10 19:46:45 +020068 *
69 * \note This value starts at an unspecified origin and
70 * may wrap around.
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072unsigned long mbedtls_timing_hardclock( void );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74/**
75 * \brief Return the elapsed time in milliseconds
76 *
77 * \param val points to a timer structure
78 * \param reset if set to 1, the timer is restarted
Gilles Peskine02c31672017-10-10 19:46:45 +020079 *
80 * \return Elapsed time in ms (before the reset, if there is a reset)
Paul Bakker5121ce52009-01-03 21:22:43 +000081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84/**
85 * \brief Setup an alarm clock
86 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
Gilles Peskine02c31672017-10-10 19:46:45 +020088 * (must be >=0)
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +000089 *
90 * \warning Only one alarm at a time is supported. In a threaded
91 * context, this means one for the whole process, not one per
92 * thread.
Paul Bakker5121ce52009-01-03 21:22:43 +000093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094void mbedtls_set_alarm( int seconds );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020097 * \brief Set a pair of delays to watch
98 * (See \c mbedtls_timing_get_delay().)
99 *
Gilles Peskine02c31672017-10-10 19:46:45 +0200100 * \param data Pointer to timing data.
Tillmann Karras588ad502015-09-25 04:27:22 +0200101 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200102 * \param int_ms First (intermediate) delay in milliseconds.
Gilles Peskine02c31672017-10-10 19:46:45 +0200103 * The effect if int_ms > fin_ms is unspecified.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200104 * \param fin_ms Second (final) delay in milliseconds.
105 * Pass 0 to cancel the current delay.
Gilles Peskine02c31672017-10-10 19:46:45 +0200106 *
107 * \note To set a single delay, either use \c mbedtls_timing_set_timer
108 * directly or use this function with int_ms == fin_ms.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200109 */
110void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
111
112/**
113 * \brief Get the status of delays
114 * (Memory helper: number of delays passed.)
115 *
116 * \param data Pointer to timing data
Tillmann Karras588ad502015-09-25 04:27:22 +0200117 * Must point to a valid \c mbedtls_timing_delay_context struct.
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200118 *
Gilles Peskine02c31672017-10-10 19:46:45 +0200119 * \return -1 if cancelled (fin_ms = 0),
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200120 * 0 if none of the delays are passed,
121 * 1 if only the intermediate delay is passed,
122 * 2 if the final delay is passed.
123 */
124int mbedtls_timing_get_delay( void *data );
125
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200126#ifdef __cplusplus
127}
128#endif
129
130#else /* MBEDTLS_TIMING_ALT */
131#include "timing_alt.h"
132#endif /* MBEDTLS_TIMING_ALT */
133
134#ifdef __cplusplus
135extern "C" {
136#endif
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100139/**
140 * \brief Checkup routine
141 *
142 * \return 0 if successful, or 1 if a test failed
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_timing_self_test( int verbose );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100145#endif
146
Paul Bakker5121ce52009-01-03 21:22:43 +0000147#ifdef __cplusplus
148}
149#endif
150
Paul Bakker5121ce52009-01-03 21:22:43 +0000151#endif /* timing.h */