mirror of https://github.com/ARMmbed/mbed-os.git
mbedtls: Use LowPowerTimer/Timer for timing
Previously we used `gettimeofday()` for Mbed TLS timing, but its implementation provided by Mbed OS is only precise to seconds. The microsecond component of the output `struct timeval` is always set to zero. But Mbed TLS requires millisecond precision. To provide required timing precision, switch to use `LowPowerTicker` or (microsecond) `Ticker`. `LowPowerTicker` is preferred as it saves power and Mbed TLS does not require microsecond precision.pull/14772/head
parent
2a73d44343
commit
f96f98e60e
|
@ -24,11 +24,9 @@
|
||||||
#include "mbedtls/timing.h"
|
#include "mbedtls/timing.h"
|
||||||
#if defined(MBEDTLS_TIMING_ALT)
|
#if defined(MBEDTLS_TIMING_ALT)
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
struct mbedtls_timing_hr_time
|
struct mbedtls_timing_hr_time
|
||||||
{
|
{
|
||||||
struct timeval start;
|
unsigned long start;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct mbedtls_timing_delay_context
|
typedef struct mbedtls_timing_delay_context
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
#endif
|
#endif
|
||||||
#include "mbedtls/timing.h"
|
#include "mbedtls/timing.h"
|
||||||
#include "drivers/Timeout.h"
|
#include "drivers/Timeout.h"
|
||||||
|
#include "drivers/Timer.h"
|
||||||
|
#include "drivers/LowPowerTimer.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -46,22 +48,24 @@ extern "C" void mbedtls_set_alarm(int seconds)
|
||||||
|
|
||||||
#if !defined(HAVE_HARDCLOCK)
|
#if !defined(HAVE_HARDCLOCK)
|
||||||
#define HAVE_HARDCLOCK
|
#define HAVE_HARDCLOCK
|
||||||
#include "platform/mbed_rtc_time.h"
|
static int timer_init = 0;
|
||||||
static int hardclock_init = 0;
|
|
||||||
static struct timeval tv_init;
|
|
||||||
|
|
||||||
extern "C" unsigned long mbedtls_timing_hardclock(void)
|
extern "C" unsigned long mbedtls_timing_hardclock(void)
|
||||||
{
|
{
|
||||||
struct timeval tv_cur;
|
#if DEVICE_LPTICKER
|
||||||
|
static mbed::LowPowerTimer timer;
|
||||||
|
#elif DEVICE_USTICKER
|
||||||
|
static mbed::Timer timer;
|
||||||
|
#else
|
||||||
|
#error "MBEDTLS_TIMING_C requires either LPTICKER or USTICKER"
|
||||||
|
#endif
|
||||||
|
|
||||||
if (hardclock_init == 0)
|
if (timer_init == 0) {
|
||||||
{
|
timer.reset();
|
||||||
gettimeofday(&tv_init, NULL);
|
timer.start();
|
||||||
hardclock_init = 1;
|
timer_init = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
gettimeofday(&tv_cur, NULL);
|
return timer.elapsed_time().count();
|
||||||
return((tv_cur.tv_sec - tv_init.tv_sec) * 1000000
|
|
||||||
+ (tv_cur.tv_usec - tv_init.tv_usec));
|
|
||||||
}
|
}
|
||||||
#endif /* !HAVE_HARDCLOCK */
|
#endif /* !HAVE_HARDCLOCK */
|
||||||
|
|
Loading…
Reference in New Issue