Skip to content
0.2.0-dev Download ZIP
76 lines ·1.5 KB Raw
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 "ssl_misc.h"
9
10#if defined(MBEDTLS_TIMING_C)
11
12#include "mbedtls/timing.h"
13
14#if !defined(MBEDTLS_TIMING_ALT)
15
16unsigned long long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
17{
18 if (reset) {
19 val->ms = mbedtls_ms_time();
20 return 0;
21 } else {
22 mbedtls_ms_time_t now = mbedtls_ms_time();
23 return now - val->ms;
24 }
25}
26
27/*
28 * Set delays to watch
29 */
30void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
31{
32 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
33
34 ctx->int_ms = int_ms;
35 ctx->fin_ms = fin_ms;
36
37 if (fin_ms != 0) {
38 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
39 }
40}
41
42/*
43 * Get number of delays expired
44 */
45int mbedtls_timing_get_delay(void *data)
46{
47 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
48 unsigned long long elapsed_ms;
49
50 if (ctx->fin_ms == 0) {
51 return -1;
52 }
53
54 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
55
56 if (elapsed_ms >= ctx->fin_ms) {
57 return 2;
58 }
59
60 if (elapsed_ms >= ctx->int_ms) {
61 return 1;
62 }
63
64 return 0;
65}
66
67/*
68 * Get the final delay.
69 */
70uint32_t mbedtls_timing_get_final_delay(
71 const mbedtls_timing_delay_context *data)
72{
73 return data->fin_ms;
74}
75#endif /* !MBEDTLS_TIMING_ALT */
76#endif /* MBEDTLS_TIMING_C */