1 /**
2  * \file timing.h
3  *
4  * \brief Portable interface to timeouts and to the CPU cycle counter
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  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.
21  */
22 #ifndef MBEDTLS_TIMING_H
23 #define MBEDTLS_TIMING_H
24 
25 #if !defined(MBEDTLS_CONFIG_FILE)
26 #include "mbedtls/config.h"
27 #else
28 #include MBEDTLS_CONFIG_FILE
29 #endif
30 
31 #include <stdint.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #if !defined(MBEDTLS_TIMING_ALT)
38 // Regular implementation
39 //
40 
41 /**
42  * \brief          timer structure
43  */
44 struct mbedtls_timing_hr_time
45 {
46     unsigned char opaque[32];
47 };
48 
49 /**
50  * \brief          Context for mbedtls_timing_set/get_delay()
51  */
52 typedef struct mbedtls_timing_delay_context
53 {
54     struct mbedtls_timing_hr_time   timer;
55     uint32_t                        int_ms;
56     uint32_t                        fin_ms;
57 } mbedtls_timing_delay_context;
58 
59 #else  /* MBEDTLS_TIMING_ALT */
60 #include "timing_alt.h"
61 #endif /* MBEDTLS_TIMING_ALT */
62 
63 extern volatile int mbedtls_timing_alarmed;
64 
65 /**
66  * \brief          Return the CPU cycle counter value
67  *
68  * \warning        This is only a best effort! Do not rely on this!
69  *                 In particular, it is known to be unreliable on virtual
70  *                 machines.
71  *
72  * \note           This value starts at an unspecified origin and
73  *                 may wrap around.
74  */
75 unsigned long mbedtls_timing_hardclock( void );
76 
77 /**
78  * \brief          Return the elapsed time in milliseconds
79  *
80  * \param val      points to a timer structure
81  * \param reset    If 0, query the elapsed time. Otherwise (re)start the timer.
82  *
83  * \return         Elapsed time since the previous reset in ms. When
84  *                 restarting, this is always 0.
85  *
86  * \note           To initialize a timer, call this function with reset=1.
87  *
88  *                 Determining the elapsed time and resetting the timer is not
89  *                 atomic on all platforms, so after the sequence
90  *                 `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
91  *                 get_timer(0) }` the value time1+time2 is only approximately
92  *                 the delay since the first reset.
93  */
94 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
95 
96 /**
97  * \brief          Setup an alarm clock
98  *
99  * \param seconds  delay before the "mbedtls_timing_alarmed" flag is set
100  *                 (must be >=0)
101  *
102  * \warning        Only one alarm at a time  is supported. In a threaded
103  *                 context, this means one for the whole process, not one per
104  *                 thread.
105  */
106 void mbedtls_set_alarm( int seconds );
107 
108 /**
109  * \brief          Set a pair of delays to watch
110  *                 (See \c mbedtls_timing_get_delay().)
111  *
112  * \param data     Pointer to timing data.
113  *                 Must point to a valid \c mbedtls_timing_delay_context struct.
114  * \param int_ms   First (intermediate) delay in milliseconds.
115  *                 The effect if int_ms > fin_ms is unspecified.
116  * \param fin_ms   Second (final) delay in milliseconds.
117  *                 Pass 0 to cancel the current delay.
118  *
119  * \note           To set a single delay, either use \c mbedtls_timing_set_timer
120  *                 directly or use this function with int_ms == fin_ms.
121  */
122 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
123 
124 /**
125  * \brief          Get the status of delays
126  *                 (Memory helper: number of delays passed.)
127  *
128  * \param data     Pointer to timing data
129  *                 Must point to a valid \c mbedtls_timing_delay_context struct.
130  *
131  * \return         -1 if cancelled (fin_ms = 0),
132  *                  0 if none of the delays are passed,
133  *                  1 if only the intermediate delay is passed,
134  *                  2 if the final delay is passed.
135  */
136 int mbedtls_timing_get_delay( void *data );
137 
138 #if defined(MBEDTLS_SELF_TEST)
139 /**
140  * \brief          Checkup routine
141  *
142  * \return         0 if successful, or 1 if a test failed
143  */
144 int mbedtls_timing_self_test( int verbose );
145 #endif
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif /* timing.h */
152