1 /**
2  * \file timing.h
3  *
4  * \brief Portable interface to the CPU cycle counter
5  *
6  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  *  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.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_TIMING_H
24 #define MBEDTLS_TIMING_H
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #if !defined(MBEDTLS_TIMING_ALT)
33 // Regular implementation
34 //
35 
36 #include <stdint.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * \brief          timer structure
44  */
45 struct mbedtls_timing_hr_time
46 {
47     unsigned char opaque[32];
48 };
49 
50 /**
51  * \brief          Context for mbedtls_timing_set/get_delay()
52  */
53 typedef 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 
60 extern volatile int mbedtls_timing_alarmed;
61 
62 /**
63  * \brief          Return the CPU cycle counter value
64  *
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.
68  */
69 unsigned long mbedtls_timing_hardclock( void );
70 
71 /**
72  * \brief          Return the elapsed time in milliseconds
73  *
74  * \param val      points to a timer structure
75  * \param reset    if set to 1, the timer is restarted
76  */
77 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
78 
79 /**
80  * \brief          Setup an alarm clock
81  *
82  * \param seconds  delay before the "mbedtls_timing_alarmed" flag is set
83  *
84  * \warning        Only one alarm at a time  is supported. In a threaded
85  *                 context, this means one for the whole process, not one per
86  *                 thread.
87  */
88 void mbedtls_set_alarm( int seconds );
89 
90 /**
91  * \brief          Set a pair of delays to watch
92  *                 (See \c mbedtls_timing_get_delay().)
93  *
94  * \param data     Pointer to timing data
95  *                 Must point to a valid \c mbedtls_timing_delay_context struct.
96  * \param int_ms   First (intermediate) delay in milliseconds.
97  * \param fin_ms   Second (final) delay in milliseconds.
98  *                 Pass 0 to cancel the current delay.
99  */
100 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
101 
102 /**
103  * \brief          Get the status of delays
104  *                 (Memory helper: number of delays passed.)
105  *
106  * \param data     Pointer to timing data
107  *                 Must point to a valid \c mbedtls_timing_delay_context struct.
108  *
109  * \return         -1 if cancelled (fin_ms = 0)
110  *                  0 if none of the delays are passed,
111  *                  1 if only the intermediate delay is passed,
112  *                  2 if the final delay is passed.
113  */
114 int mbedtls_timing_get_delay( void *data );
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #else  /* MBEDTLS_TIMING_ALT */
121 #include "timing_alt.h"
122 #endif /* MBEDTLS_TIMING_ALT */
123 
124 #ifdef __cplusplus
125 extern "C" {
126 #endif
127 
128 #if defined(MBEDTLS_SELF_TEST)
129 /**
130  * \brief          Checkup routine
131  *
132  * \return         0 if successful, or 1 if a test failed
133  */
134 int mbedtls_timing_self_test( int verbose );
135 #endif
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif /* timing.h */
142