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