mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-03-20 19:21:09 +01:00
Simplify MBEDTLS_TIMING_C to use mbedtls_ms_time()
Don't ship two slightly different wheels. This reduces our platform adherence by using only `clock_gettime()` in the library and not `gettimeofday()` as well. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
@@ -23,11 +23,13 @@ extern "C" {
|
|||||||
// Regular implementation
|
// Regular implementation
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <mbedtls/platform_time.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief timer structure
|
* \brief timer structure
|
||||||
*/
|
*/
|
||||||
struct mbedtls_timing_hr_time {
|
struct mbedtls_timing_hr_time {
|
||||||
uint64_t MBEDTLS_PRIVATE(opaque)[4];
|
mbedtls_ms_time_t MBEDTLS_PRIVATE(ms);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,95 +13,17 @@
|
|||||||
|
|
||||||
#if !defined(MBEDTLS_TIMING_ALT)
|
#if !defined(MBEDTLS_TIMING_ALT)
|
||||||
|
|
||||||
#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
|
|
||||||
!defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
|
|
||||||
!defined(__HAIKU__) && !defined(__midipix__)
|
|
||||||
#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
#include <process.h>
|
|
||||||
|
|
||||||
struct _hr_time {
|
|
||||||
LARGE_INTEGER start;
|
|
||||||
};
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <signal.h>
|
|
||||||
/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
|
|
||||||
* platform matches the ifdefs above, it will be used. */
|
|
||||||
#include <time.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
struct _hr_time {
|
|
||||||
struct timeval start;
|
|
||||||
};
|
|
||||||
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Return the elapsed time in milliseconds
|
|
||||||
*
|
|
||||||
* \warning May change without notice
|
|
||||||
*
|
|
||||||
* \param val points to a timer structure
|
|
||||||
* \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
|
|
||||||
*
|
|
||||||
* \return Elapsed time since the previous reset in ms. When
|
|
||||||
* restarting, this is always 0.
|
|
||||||
*
|
|
||||||
* \note To initialize a timer, call this function with reset=1.
|
|
||||||
*
|
|
||||||
* Determining the elapsed time and resetting the timer is not
|
|
||||||
* atomic on all platforms, so after the sequence
|
|
||||||
* `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
|
|
||||||
* get_timer(0) }` the value time1+time2 is only approximately
|
|
||||||
* the delay since the first reset.
|
|
||||||
*/
|
|
||||||
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
|
||||||
|
|
||||||
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
|
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
|
||||||
{
|
{
|
||||||
struct _hr_time *t = (struct _hr_time *) val;
|
|
||||||
|
|
||||||
if (reset) {
|
if (reset) {
|
||||||
QueryPerformanceCounter(&t->start);
|
val->ms = mbedtls_ms_time();
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
unsigned long delta;
|
mbedtls_ms_time_t now = mbedtls_ms_time();
|
||||||
LARGE_INTEGER now, hfreq;
|
return now - val->ms;
|
||||||
QueryPerformanceCounter(&now);
|
|
||||||
QueryPerformanceFrequency(&hfreq);
|
|
||||||
delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
|
|
||||||
/ hfreq.QuadPart);
|
|
||||||
return delta;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* _WIN32 && !EFIX64 && !EFI32 */
|
|
||||||
|
|
||||||
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
|
|
||||||
{
|
|
||||||
struct _hr_time *t = (struct _hr_time *) val;
|
|
||||||
|
|
||||||
if (reset) {
|
|
||||||
gettimeofday(&t->start, NULL);
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
unsigned long delta;
|
|
||||||
struct timeval now;
|
|
||||||
gettimeofday(&now, NULL);
|
|
||||||
delta = (now.tv_sec - t->start.tv_sec) * 1000ul
|
|
||||||
+ (now.tv_usec - t->start.tv_usec) / 1000;
|
|
||||||
return delta;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set delays to watch
|
* Set delays to watch
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user