Skip to content

Commit

Permalink
mbedtls: Add an alt implementation of timing
Browse files Browse the repository at this point in the history
Implement the MBEDTLS_TIMING_ALT interface for Mbed OS. This
implementation is sufficient to run the Mbed TLS benchmarking
application.
  • Loading branch information
Patater committed Jun 9, 2021
1 parent 386f197 commit b8781e5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
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 */

0 comments on commit b8781e5

Please sign in to comment.