Coverage Report

Created: 2024-01-20 12:32

/src/mbedtls/library/timing.c
Line
Count
Source (jump to first uncovered line)
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
39
struct _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>
52
struct _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
78
unsigned 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
98
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
99
239k
{
100
239k
    struct _hr_time *t = (struct _hr_time *) val;
101
102
239k
    if (reset) {
103
162k
        gettimeofday(&t->start, NULL);
104
162k
        return 0;
105
162k
    } else {
106
77.0k
        unsigned long delta;
107
77.0k
        struct timeval now;
108
77.0k
        gettimeofday(&now, NULL);
109
77.0k
        delta = (now.tv_sec  - t->start.tv_sec) * 1000ul
110
77.0k
                + (now.tv_usec - t->start.tv_usec) / 1000;
111
77.0k
        return delta;
112
77.0k
    }
113
239k
}
114
115
#endif /* _WIN32 && !EFIX64 && !EFI32 */
116
117
/*
118
 * Set delays to watch
119
 */
120
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
121
196k
{
122
196k
    mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
123
124
196k
    ctx->int_ms = int_ms;
125
196k
    ctx->fin_ms = fin_ms;
126
127
196k
    if (fin_ms != 0) {
128
162k
        (void) mbedtls_timing_get_timer(&ctx->timer, 1);
129
162k
    }
130
196k
}
131
132
/*
133
 * Get number of delays expired
134
 */
135
int mbedtls_timing_get_delay(void *data)
136
77.0k
{
137
77.0k
    mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
138
77.0k
    unsigned long elapsed_ms;
139
140
77.0k
    if (ctx->fin_ms == 0) {
141
0
        return -1;
142
0
    }
143
144
77.0k
    elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
145
146
77.0k
    if (elapsed_ms >= ctx->fin_ms) {
147
0
        return 2;
148
0
    }
149
150
77.0k
    if (elapsed_ms >= ctx->int_ms) {
151
0
        return 1;
152
0
    }
153
154
77.0k
    return 0;
155
77.0k
}
156
157
/*
158
 * Get the final delay.
159
 */
160
uint32_t mbedtls_timing_get_final_delay(
161
    const mbedtls_timing_delay_context *data)
162
0
{
163
0
    return data->fin_ms;
164
0
}
165
#endif /* !MBEDTLS_TIMING_ALT */
166
#endif /* MBEDTLS_TIMING_C */