Skip to content

Commit fd7e33b

Browse files
authored
Merge pull request #14772 from LDong-Arm/mbedtls_timing
Improve implementation of Mbed TLS timing
2 parents 0c9be22 + 49163f0 commit fd7e33b

File tree

8 files changed

+119
-17
lines changed

8 files changed

+119
-17
lines changed

TESTS/configs/mbedtls.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"macros": [
3+
"MBEDTLS_SELF_TEST",
4+
"MBEDTLS_TIMING_C",
5+
"MBEDTLS_TIMING_ALT"
6+
]
7+
}

connectivity/mbedtls/include/mbedtls/config-no-entropy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define MBEDTLS_PK_RSA_ALT_SUPPORT
4949
#define MBEDTLS_PKCS1_V15
5050
#define MBEDTLS_PKCS1_V21
51-
#define MBEDTLS_SELF_TEST
51+
//#define MBEDTLS_SELF_TEST
5252
#define MBEDTLS_VERSION_FEATURES
5353
#define MBEDTLS_X509_CHECK_KEY_USAGE
5454
#define MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE

connectivity/mbedtls/include/mbedtls/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@
13961396
*
13971397
* Enable the checkup functions (*_self_test).
13981398
*/
1399-
#define MBEDTLS_SELF_TEST
1399+
//#define MBEDTLS_SELF_TEST
14001400

14011401
/**
14021402
* \def MBEDTLS_SHA256_SMALLER

connectivity/mbedtls/platform/inc/timing_alt.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
#include "mbedtls/timing.h"
2525
#if defined(MBEDTLS_TIMING_ALT)
2626

27-
#include <time.h>
28-
2927
struct mbedtls_timing_hr_time
3028
{
31-
struct timeval start;
29+
unsigned long start;
3230
};
3331

3432
typedef struct mbedtls_timing_delay_context

connectivity/mbedtls/platform/src/timing_mbed.cpp

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* timing.cpp
33
*
4+
* Copyright The Mbed TLS Contributors
45
* Copyright (C) 2021, Arm Limited, All Rights Reserved
56
* SPDX-License-Identifier: Apache-2.0
67
*
@@ -23,8 +24,14 @@
2324
#else
2425
#include MBEDTLS_CONFIG_FILE
2526
#endif
27+
28+
#if defined(MBEDTLS_TIMING_ALT)
29+
2630
#include "mbedtls/timing.h"
2731
#include "drivers/Timeout.h"
32+
#include "drivers/LowPowerTimeout.h"
33+
#include "drivers/Timer.h"
34+
#include "drivers/LowPowerTimer.h"
2835
#include <chrono>
2936

3037
extern "C" {
@@ -38,30 +45,101 @@ static void handle_alarm(void)
3845

3946
extern "C" void mbedtls_set_alarm(int seconds)
4047
{
48+
#if DEVICE_LPTICKER
49+
static mbed::LowPowerTimeout t;
50+
#elif DEVICE_USTICKER
4151
static mbed::Timeout t;
52+
#else
53+
#error "MBEDTLS_TIMING_C requires either LPTICKER or USTICKER"
54+
#endif
55+
4256
mbedtls_timing_alarmed = 0;
4357

4458
t.attach(handle_alarm, std::chrono::seconds(seconds));
4559
}
4660

61+
// The static Mbed timer here is initialized once only.
62+
// Mbed TLS can have multiple timers (mbedtls_timing_hr_time) derived
63+
// from the Mbed timer.
64+
#if DEVICE_LPTICKER
65+
static mbed::LowPowerTimer timer;
66+
#elif DEVICE_USTICKER
67+
static mbed::Timer timer;
68+
#else
69+
#error "MBEDTLS_TIMING_C requires either LPTICKER or USTICKER"
70+
#endif
71+
static int timer_init = 0;
72+
4773
#if !defined(HAVE_HARDCLOCK)
4874
#define HAVE_HARDCLOCK
49-
#include "platform/mbed_rtc_time.h"
50-
static int hardclock_init = 0;
51-
static struct timeval tv_init;
5275

5376
extern "C" unsigned long mbedtls_timing_hardclock(void)
5477
{
55-
struct timeval tv_cur;
56-
57-
if (hardclock_init == 0)
58-
{
59-
gettimeofday(&tv_init, NULL);
60-
hardclock_init = 1;
78+
if (timer_init == 0) {
79+
timer.reset();
80+
timer.start();
81+
timer_init = 1;
6182
}
6283

63-
gettimeofday(&tv_cur, NULL);
64-
return((tv_cur.tv_sec - tv_init.tv_sec) * 1000000
65-
+ (tv_cur.tv_usec - tv_init.tv_usec));
84+
return timer.elapsed_time().count();
6685
}
6786
#endif /* !HAVE_HARDCLOCK */
87+
88+
extern "C" unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
89+
{
90+
if (timer_init == 0) {
91+
timer.reset();
92+
timer.start();
93+
timer_init = 1;
94+
}
95+
96+
if (reset) {
97+
val->start = std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count();
98+
return 0;
99+
} else {
100+
return std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count() - val->start;
101+
}
102+
}
103+
104+
/**
105+
* Note: The following implementations come from the default timing.c
106+
* from Mbed TLS. They are disabled in timing.c when MBEDTLS_TIMING_ALT
107+
* is defined, but the implementation is nonetheless applicable to
108+
* Mbed OS, so we copy them over.
109+
*/
110+
111+
extern "C" void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
112+
{
113+
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
114+
115+
ctx->int_ms = int_ms;
116+
ctx->fin_ms = fin_ms;
117+
118+
if (fin_ms != 0) {
119+
(void) mbedtls_timing_get_timer(&ctx->timer, 1);
120+
}
121+
}
122+
123+
extern "C" int mbedtls_timing_get_delay(void *data)
124+
{
125+
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
126+
unsigned long elapsed_ms;
127+
128+
if (ctx->fin_ms == 0) {
129+
return -1;
130+
}
131+
132+
elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
133+
134+
if (elapsed_ms >= ctx->fin_ms) {
135+
return 2;
136+
}
137+
138+
if (elapsed_ms >= ctx->int_ms) {
139+
return 1;
140+
}
141+
142+
return 0;
143+
}
144+
145+
#endif // MBEDTLS_TIMING_ALT

connectivity/mbedtls/tests/TESTS/mbedtls/selftest/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ using namespace utest::v1;
3131
#include MBEDTLS_CONFIG_FILE
3232
#endif
3333

34+
#if !defined(MBEDTLS_SELF_TEST)
35+
#error [NOT_SUPPORTED] MBEDTLS_SELF_TEST undefined
36+
#endif
37+
3438
#include "mbedtls/sha256.h"
3539
#include "mbedtls/sha512.h"
3640
#include "mbedtls/entropy.h"
3741
#include "mbedtls/entropy_poll.h"
42+
#include "mbedtls/timing.h"
3843

3944
#include <string.h>
4045

@@ -65,6 +70,10 @@ MBEDTLS_SELF_TEST_TEST_CASE(mbedtls_sha512_self_test)
6570
MBEDTLS_SELF_TEST_TEST_CASE(mbedtls_entropy_self_test)
6671
#endif
6772

73+
#if defined(MBEDTLS_TIMING_C)
74+
MBEDTLS_SELF_TEST_TEST_CASE(mbedtls_timing_self_test)
75+
#endif
76+
6877
#else
6978
#warning "MBEDTLS_SELF_TEST not enabled"
7079
#endif /* MBEDTLS_SELF_TEST */
@@ -84,6 +93,10 @@ Case cases[] = {
8493
Case("mbedtls_entropy_self_test", mbedtls_entropy_self_test_test_case),
8594
#endif
8695

96+
#if defined(MBEDTLS_TIMING_C)
97+
Case("mbedtls_timing_self_test", mbedtls_timing_self_test_test_case),
98+
#endif
99+
87100
#endif /* MBEDTLS_SELF_TEST */
88101
};
89102

connectivity/mbedtls/tools/importer/adjust-config.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ conf unset MBEDTLS_SSL_TRUNCATED_HMAC
117117

118118
conf unset MBEDTLS_PLATFORM_TIME_TYPE_MACRO
119119

120+
# potentially save flash space by not enabling self-tests by default
121+
conf unset MBEDTLS_SELF_TEST
122+
120123
# The default size of MBEDTLS_MPI_MAX_SIZE is 1024 bytes.
121124
# In some cases, this value is set to stack buffers.
122125
# Reduce the maximal MBEDTLS_MPI_MAX_SIZE to 512 bytes,

connectivity/mbedtls/tools/importer/adjust-no-entropy-config.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ add_code() {
3737

3838
conf set MBEDTLS_CMAC_C
3939
conf unset MBEDTLS_CIPHER_MODE_XTS
40+
41+
# potentially save flash space by not enabling self-tests by default
42+
conf unset MBEDTLS_SELF_TEST

0 commit comments

Comments
 (0)