diff --git a/connectivity/mbedtls/CMakeLists.txt b/connectivity/mbedtls/CMakeLists.txt index aa7eac9a205..f0196b2e238 100644 --- a/connectivity/mbedtls/CMakeLists.txt +++ b/connectivity/mbedtls/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources(mbed-mbedtls platform/src/mbed_trng.cpp platform/src/platform_alt.cpp platform/src/shared_rng.cpp + platform/src/timing.cpp source/aes.c source/aesni.c diff --git a/connectivity/mbedtls/include/mbedtls/check_config.h b/connectivity/mbedtls/include/mbedtls/check_config.h index 500b3cf8045..7e59accc703 100644 --- a/connectivity/mbedtls/include/mbedtls/check_config.h +++ b/connectivity/mbedtls/include/mbedtls/check_config.h @@ -55,9 +55,8 @@ #endif #endif /* _WIN32 */ -#if defined(TARGET_LIKE_MBED) && \ - ( defined(MBEDTLS_NET_C) || defined(MBEDTLS_TIMING_C) ) -#error "The NET and TIMING modules are not available for mbed OS - please use the network and timing functions provided by mbed OS" +#if defined(TARGET_LIKE_MBED) && defined(MBEDTLS_NET_C) +#error "The NET module is not available for mbed OS - please use the network functions provided by mbed OS" #endif #if defined(MBEDTLS_DEPRECATED_WARNING) && \ diff --git a/connectivity/mbedtls/platform/inc/timing_alt.h b/connectivity/mbedtls/platform/inc/timing_alt.h new file mode 100644 index 00000000000..de61d528146 --- /dev/null +++ b/connectivity/mbedtls/platform/inc/timing_alt.h @@ -0,0 +1,42 @@ +/* + * timing_alt.h + * + * Copyright (C) 2021, Arm Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef TIMING_ALT_H +#define TIMING_ALT_H + +#include "mbedtls/timing.h" +#if defined(MBEDTLS_TIMING_ALT) + +#include + +struct mbedtls_timing_hr_time +{ + struct timeval start; +}; + +typedef struct mbedtls_timing_delay_context +{ + struct mbedtls_timing_hr_time timer; + uint32_t int_ms; + uint32_t fin_ms; +} mbedtls_timing_delay_context; + +#endif +#endif diff --git a/connectivity/mbedtls/platform/src/timing.cpp b/connectivity/mbedtls/platform/src/timing.cpp new file mode 100644 index 00000000000..e26d2d5d77f --- /dev/null +++ b/connectivity/mbedtls/platform/src/timing.cpp @@ -0,0 +1,67 @@ +/* + * timing.cpp + * + * Copyright (C) 2021, Arm Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif +#include "mbedtls/timing.h" +#include "drivers/Timeout.h" +#include + +extern "C" { + volatile int mbedtls_timing_alarmed = 0; +}; + +static void handle_alarm(void) +{ + mbedtls_timing_alarmed = 1; +} + +extern "C" void mbedtls_set_alarm(int seconds) +{ + static mbed::Timeout t; + mbedtls_timing_alarmed = 0; + + t.attach(handle_alarm, std::chrono::seconds(seconds)); +} + +#if !defined(HAVE_HARDCLOCK) +#define HAVE_HARDCLOCK +#include "platform/mbed_rtc_time.h" +static int hardclock_init = 0; +static struct timeval tv_init; + +extern "C" unsigned long mbedtls_timing_hardclock(void) +{ + struct timeval tv_cur; + + if (hardclock_init == 0) + { + gettimeofday(&tv_init, NULL); + hardclock_init = 1; + } + + gettimeofday(&tv_cur, NULL); + return((tv_cur.tv_sec - tv_init.tv_sec) * 1000000 + + (tv_cur.tv_usec - tv_init.tv_usec)); +} +#endif /* !HAVE_HARDCLOCK */