blob: 82e8c8b3d5d52253e8873cbb81528836e708303d [file] [log] [blame]
Andrzej Kurekc890b222023-01-17 04:03:19 -05001/*
2 * Copy of the internal MbedTLS timing implementation, to be used in tests.
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef EXTERNAL_TIMING_FOR_TEST_H
22#define EXTERNAL_TIMING_FOR_TEST_H
23
24#if !defined(MBEDTLS_CONFIG_FILE)
25#include "mbedtls/config.h"
26#else
27#include MBEDTLS_CONFIG_FILE
28#endif
29
30#include <stdint.h>
31
32/**
33 * \brief timer structure
34 */
35struct mbedtls_timing_hr_time {
36 unsigned char opaque[32];
37};
38
39/**
40 * \brief Context for mbedtls_timing_set/get_delay()
41 */
42typedef struct mbedtls_timing_delay_context {
43 struct mbedtls_timing_hr_time timer;
44 uint32_t int_ms;
45 uint32_t fin_ms;
46} mbedtls_timing_delay_context;
47
48#else /* MBEDTLS_TIMING_ALT */
49#include "timing_alt.h"
50#endif /* MBEDTLS_TIMING_ALT */
51
52extern volatile int mbedtls_timing_alarmed;
53
54/**
55 * \brief Return the CPU cycle counter value
56 *
57 * \warning This is only a best effort! Do not rely on this!
58 * In particular, it is known to be unreliable on virtual
59 * machines.
60 *
61 * \note This value starts at an unspecified origin and
62 * may wrap around.
63 */
64unsigned long mbedtls_timing_hardclock(void);
65
66/**
67 * \brief Return the elapsed time in milliseconds
68 *
69 * \param val points to a timer structure
70 * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
71 *
72 * \return Elapsed time since the previous reset in ms. When
73 * restarting, this is always 0.
74 *
75 * \note To initialize a timer, call this function with reset=1.
76 *
77 * Determining the elapsed time and resetting the timer is not
78 * atomic on all platforms, so after the sequence
79 * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
80 * get_timer(0) }` the value time1+time2 is only approximately
81 * the delay since the first reset.
82 */
83unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset);
84
85/**
86 * \brief Setup an alarm clock
87 *
88 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
89 * (must be >=0)
90 *
91 * \warning Only one alarm at a time is supported. In a threaded
92 * context, this means one for the whole process, not one per
93 * thread.
94 */
95void mbedtls_set_alarm(int seconds);
96
97/**
98 * \brief Set a pair of delays to watch
99 * (See \c mbedtls_timing_get_delay().)
100 *
101 * \param data Pointer to timing data.
102 * Must point to a valid \c mbedtls_timing_delay_context struct.
103 * \param int_ms First (intermediate) delay in milliseconds.
104 * The effect if int_ms > fin_ms is unspecified.
105 * \param fin_ms Second (final) delay in milliseconds.
106 * Pass 0 to cancel the current delay.
107 *
108 * \note To set a single delay, either use \c mbedtls_timing_set_timer
109 * directly or use this function with int_ms == fin_ms.
110 */
111void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
112
113/**
114 * \brief Get the status of delays
115 * (Memory helper: number of delays passed.)
116 *
117 * \param data Pointer to timing data
118 * Must point to a valid \c mbedtls_timing_delay_context struct.
119 *
120 * \return -1 if cancelled (fin_ms = 0),
121 * 0 if none of the delays are passed,
122 * 1 if only the intermediate delay is passed,
123 * 2 if the final delay is passed.
124 */
125int mbedtls_timing_get_delay(void *data);
126
127#ifdef __cplusplus
128}
129
Andrzej Kurek721cff12023-01-17 05:44:06 -0500130#endif /* EXTERNAL_TIMING_FOR_TEST_H */