Skip to content

Commit b8781e5

Browse files
committed
mbedtls: Add an alt implementation of timing
Implement the MBEDTLS_TIMING_ALT interface for Mbed OS. This implementation is sufficient to run the Mbed TLS benchmarking application.
1 parent 386f197 commit b8781e5

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

connectivity/mbedtls/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target_sources(mbed-mbedtls
1616
platform/src/mbed_trng.cpp
1717
platform/src/platform_alt.cpp
1818
platform/src/shared_rng.cpp
19+
platform/src/timing.cpp
1920

2021
source/aes.c
2122
source/aesni.c

connectivity/mbedtls/include/mbedtls/check_config.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@
5555
#endif
5656
#endif /* _WIN32 */
5757

58-
#if defined(TARGET_LIKE_MBED) && \
59-
( defined(MBEDTLS_NET_C) || defined(MBEDTLS_TIMING_C) )
60-
#error "The NET and TIMING modules are not available for mbed OS - please use the network and timing functions provided by mbed OS"
58+
#if defined(TARGET_LIKE_MBED) && defined(MBEDTLS_NET_C)
59+
#error "The NET module is not available for mbed OS - please use the network functions provided by mbed OS"
6160
#endif
6261

6362
#if defined(MBEDTLS_DEPRECATED_WARNING) && \
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* timing_alt.h
3+
*
4+
* Copyright (C) 2021, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#ifndef TIMING_ALT_H
22+
#define TIMING_ALT_H
23+
24+
#include "mbedtls/timing.h"
25+
#if defined(MBEDTLS_TIMING_ALT)
26+
27+
#include <time.h>
28+
29+
struct mbedtls_timing_hr_time
30+
{
31+
struct timeval start;
32+
};
33+
34+
typedef struct mbedtls_timing_delay_context
35+
{
36+
struct mbedtls_timing_hr_time timer;
37+
uint32_t int_ms;
38+
uint32_t fin_ms;
39+
} mbedtls_timing_delay_context;
40+
41+
#endif
42+
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* timing.cpp
3+
*
4+
* Copyright (C) 2021, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#if !defined(MBEDTLS_CONFIG_FILE)
22+
#include "mbedtls/config.h"
23+
#else
24+
#include MBEDTLS_CONFIG_FILE
25+
#endif
26+
#include "mbedtls/timing.h"
27+
#include "drivers/Timeout.h"
28+
#include <chrono>
29+
30+
extern "C" {
31+
volatile int mbedtls_timing_alarmed = 0;
32+
};
33+
34+
static void handle_alarm(void)
35+
{
36+
mbedtls_timing_alarmed = 1;
37+
}
38+
39+
extern "C" void mbedtls_set_alarm(int seconds)
40+
{
41+
static mbed::Timeout t;
42+
mbedtls_timing_alarmed = 0;
43+
44+
t.attach(handle_alarm, std::chrono::seconds(seconds));
45+
}
46+
47+
#if !defined(HAVE_HARDCLOCK)
48+
#define HAVE_HARDCLOCK
49+
#include "platform/mbed_rtc_time.h"
50+
static int hardclock_init = 0;
51+
static struct timeval tv_init;
52+
53+
extern "C" unsigned long mbedtls_timing_hardclock(void)
54+
{
55+
struct timeval tv_cur;
56+
57+
if (hardclock_init == 0)
58+
{
59+
gettimeofday(&tv_init, NULL);
60+
hardclock_init = 1;
61+
}
62+
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));
66+
}
67+
#endif /* !HAVE_HARDCLOCK */

0 commit comments

Comments
 (0)