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