xref: /third_party/mbedtls/library/timing.c (revision a8e1175b)
1/*
2 *  Portable interface to the CPU cycle counter
3 *
4 *  Copyright The Mbed TLS Contributors
5 *  SPDX-License-Identifier: Apache-2.0
6 *
7 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8 *  not use this file except in compliance with the License.
9 *  You may obtain a copy of the License at
10 *
11 *  http://www.apache.org/licenses/LICENSE-2.0
12 *
13 *  Unless required by applicable law or agreed to in writing, software
14 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 *  See the License for the specific language governing permissions and
17 *  limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_TIMING_C)
23
24#include "mbedtls/timing.h"
25
26#if !defined(MBEDTLS_TIMING_ALT)
27
28#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
29    !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
30    !defined(__HAIKU__) && !defined(__midipix__)
31#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
32#endif
33
34#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
35
36#include <windows.h>
37#include <process.h>
38
39struct _hr_time {
40    LARGE_INTEGER start;
41};
42
43#else
44
45#include <unistd.h>
46#include <sys/types.h>
47#include <signal.h>
48/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
49 * platform matches the ifdefs above, it will be used. */
50#include <time.h>
51#include <sys/time.h>
52struct _hr_time {
53    struct timeval start;
54};
55#endif /* _WIN32 && !EFIX64 && !EFI32 */
56
57/**
58 * \brief          Return the elapsed time in milliseconds
59 *
60 * \warning        May change without notice
61 *
62 * \param val      points to a timer structure
63 * \param reset    If 0, query the elapsed time. Otherwise (re)start the timer.
64 *
65 * \return         Elapsed time since the previous reset in ms. When
66 *                 restarting, this is always 0.
67 *
68 * \note           To initialize a timer, call this function with reset=1.
69 *
70 *                 Determining the elapsed time and resetting the timer is not
71 *                 atomic on all platforms, so after the sequence
72 *                 `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
73 *                 get_timer(0) }` the value time1+time2 is only approximately
74 *                 the delay since the first reset.
75 */
76#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
77
78unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
79{
80    struct _hr_time *t = (struct _hr_time *) val;
81
82    if (reset) {
83        QueryPerformanceCounter(&t->start);
84        return 0;
85    } else {
86        unsigned long delta;
87        LARGE_INTEGER now, hfreq;
88        QueryPerformanceCounter(&now);
89        QueryPerformanceFrequency(&hfreq);
90        delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
91                                 / hfreq.QuadPart);
92        return delta;
93    }
94}
95
96#else /* _WIN32 && !EFIX64 && !EFI32 */
97
98unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
99{
100    struct _hr_time *t = (struct _hr_time *) val;
101
102    if (reset) {
103        gettimeofday(&t->start, NULL);
104        return 0;
105    } else {
106        unsigned long delta;
107        struct timeval now;
108        gettimeofday(&now, NULL);
109        delta = (now.tv_sec  - t->start.tv_sec) * 1000ul
110                + (now.tv_usec - t->start.tv_usec) / 1000;
111        return delta;
112    }
113}
114
115#endif /* _WIN32 && !EFIX64 && !EFI32 */
116
117/*
118 * Set delays to watch
119 */
120void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
121{
122    mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
123
124    ctx->int_ms = int_ms;
125    ctx->fin_ms = fin_ms;
126
127    if (fin_ms != 0) {
128        (void) mbedtls_timing_get_timer(&ctx->timer, 1);
129    }
130}
131
132/*
133 * Get number of delays expired
134 */
135int mbedtls_timing_get_delay(void *data)
136{
137    mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
138    unsigned long elapsed_ms;
139
140    if (ctx->fin_ms == 0) {
141        return -1;
142    }
143
144    elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
145
146    if (elapsed_ms >= ctx->fin_ms) {
147        return 2;
148    }
149
150    if (elapsed_ms >= ctx->int_ms) {
151        return 1;
152    }
153
154    return 0;
155}
156
157/*
158 * Get the final delay.
159 */
160uint32_t mbedtls_timing_get_final_delay(
161    const mbedtls_timing_delay_context *data)
162{
163    return data->fin_ms;
164}
165#endif /* !MBEDTLS_TIMING_ALT */
166#endif /* MBEDTLS_TIMING_C */
167