2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
|
* Portable interface to the CPU cycle counter
|
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 19:47:20 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-11-12 18:14:23 +01:00
|
|
|
#include "ssl_misc.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-05-12 19:30:45 +02:00
|
|
|
#if defined(MBEDTLS_TIMING_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/timing.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-05-12 19:30:45 +02:00
|
|
|
#if !defined(MBEDTLS_TIMING_ALT)
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reset) {
|
2026-02-18 22:50:07 +01:00
|
|
|
val->ms = mbedtls_ms_time();
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
|
|
|
|
} else {
|
2026-02-18 22:50:07 +01:00
|
|
|
mbedtls_ms_time_t now = mbedtls_ms_time();
|
|
|
|
|
return now - val->ms;
|
2017-10-16 19:33:06 +02:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-12 20:17:06 +02:00
|
|
|
/*
|
|
|
|
|
* Set delays to watch
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
|
2015-05-12 20:17:06 +02:00
|
|
|
{
|
|
|
|
|
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
|
|
|
|
|
|
|
|
|
|
ctx->int_ms = int_ms;
|
|
|
|
|
ctx->fin_ms = fin_ms;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fin_ms != 0) {
|
|
|
|
|
(void) mbedtls_timing_get_timer(&ctx->timer, 1);
|
|
|
|
|
}
|
2015-05-12 20:17:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get number of delays expired
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_timing_get_delay(void *data)
|
2015-05-12 20:17:06 +02:00
|
|
|
{
|
|
|
|
|
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
|
|
|
|
|
unsigned long elapsed_ms;
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->fin_ms == 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-05-12 20:17:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
|
2015-05-12 20:17:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (elapsed_ms >= ctx->fin_ms) {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
2015-05-12 20:17:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (elapsed_ms >= ctx->int_ms) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2015-05-12 20:17:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-05-12 20:17:06 +02:00
|
|
|
}
|
2022-03-09 15:34:37 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get the final delay.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t mbedtls_timing_get_final_delay(
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_timing_delay_context *data)
|
2022-03-09 15:34:37 +00:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return data->fin_ms;
|
2022-03-09 15:34:37 +00:00
|
|
|
}
|
2015-05-12 19:30:45 +02:00
|
|
|
#endif /* !MBEDTLS_TIMING_ALT */
|
|
|
|
|
#endif /* MBEDTLS_TIMING_C */
|