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