Coverage Report

Created: 2025-03-01 06:26

/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 OR GPL-2.0-or-later
6
 */
7
8
#include "common.h"
9
10
#if defined(MBEDTLS_TIMING_C)
11
12
#include "mbedtls/timing.h"
13
14
#if !defined(MBEDTLS_TIMING_ALT)
15
16
#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
17
    !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
18
    !defined(__HAIKU__) && !defined(__midipix__)
19
#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
20
#endif
21
22
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
23
24
#include <windows.h>
25
#include <process.h>
26
27
struct _hr_time {
28
    LARGE_INTEGER start;
29
};
30
31
#else
32
33
#include <unistd.h>
34
#include <sys/types.h>
35
#include <signal.h>
36
/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
37
 * platform matches the ifdefs above, it will be used. */
38
#include <time.h>
39
#include <sys/time.h>
40
struct _hr_time {
41
    struct timeval start;
42
};
43
#endif /* _WIN32 && !EFIX64 && !EFI32 */
44
45
/**
46
 * \brief          Return the elapsed time in milliseconds
47
 *
48
 * \warning        May change without notice
49
 *
50
 * \param val      points to a timer structure
51
 * \param reset    If 0, query the elapsed time. Otherwise (re)start the timer.
52
 *
53
 * \return         Elapsed time since the previous reset in ms. When
54
 *                 restarting, this is always 0.
55
 *
56
 * \note           To initialize a timer, call this function with reset=1.
57
 *
58
 *                 Determining the elapsed time and resetting the timer is not
59
 *                 atomic on all platforms, so after the sequence
60
 *                 `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
61
 *                 get_timer(0) }` the value time1+time2 is only approximately
62
 *                 the delay since the first reset.
63
 */
64
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
65
66
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
67
{
68
    struct _hr_time *t = (struct _hr_time *) val;
69
70
    if (reset) {
71
        QueryPerformanceCounter(&t->start);
72
        return 0;
73
    } else {
74
        unsigned long delta;
75
        LARGE_INTEGER now, hfreq;
76
        QueryPerformanceCounter(&now);
77
        QueryPerformanceFrequency(&hfreq);
78
        delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
79
                                 / hfreq.QuadPart);
80
        return delta;
81
    }
82
}
83
84
#else /* _WIN32 && !EFIX64 && !EFI32 */
85
86
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
87
0
{
88
0
    struct _hr_time *t = (struct _hr_time *) val;
89
90
0
    if (reset) {
91
0
        gettimeofday(&t->start, NULL);
92
0
        return 0;
93
0
    } else {
94
0
        unsigned long delta;
95
0
        struct timeval now;
96
0
        gettimeofday(&now, NULL);
97
0
        delta = (now.tv_sec  - t->start.tv_sec) * 1000ul
98
0
                + (now.tv_usec - t->start.tv_usec) / 1000;
99
0
        return delta;
100
0
    }
101
0
}
102
103
#endif /* _WIN32 && !EFIX64 && !EFI32 */
104
105
/*
106
 * Set delays to watch
107
 */
108
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
109
1.08k
{
110
1.08k
    mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
111
112
1.08k
    ctx->int_ms = int_ms;
113
1.08k
    ctx->fin_ms = fin_ms;
114
115
1.08k
    if (fin_ms != 0) {
116
0
        (void) mbedtls_timing_get_timer(&ctx->timer, 1);
117
0
    }
118
1.08k
}
119
120
/*
121
 * Get number of delays expired
122
 */
123
int mbedtls_timing_get_delay(void *data)
124
1.08k
{
125
1.08k
    mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
126
1.08k
    unsigned long elapsed_ms;
127
128
1.08k
    if (ctx->fin_ms == 0) {
129
1.08k
        return -1;
130
1.08k
    }
131
132
0
    elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
133
134
0
    if (elapsed_ms >= ctx->fin_ms) {
135
0
        return 2;
136
0
    }
137
138
0
    if (elapsed_ms >= ctx->int_ms) {
139
0
        return 1;
140
0
    }
141
142
0
    return 0;
143
0
}
144
145
/*
146
 * Get the final delay.
147
 */
148
uint32_t mbedtls_timing_get_final_delay(
149
    const mbedtls_timing_delay_context *data)
150
0
{
151
0
    return data->fin_ms;
152
0
}
153
#endif /* !MBEDTLS_TIMING_ALT */
154
#endif /* MBEDTLS_TIMING_C */