Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mbedtls: Add an alt implementation of timing #14756

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions connectivity/mbedtls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions connectivity/mbedtls/include/mbedtls/check_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) && \
Expand Down
42 changes: 42 additions & 0 deletions connectivity/mbedtls/platform/inc/timing_alt.h
Original file line number Diff line number Diff line change
@@ -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 <time.h>

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
67 changes: 67 additions & 0 deletions connectivity/mbedtls/platform/src/timing.cpp
Original file line number Diff line number Diff line change
@@ -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 <chrono>

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 */