2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Portable interface to the CPU cycle counter
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* 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
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* 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.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-05-12 19:30:45 +02:00
|
|
|
#if defined(MBEDTLS_TIMING_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/timing.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-05-12 19:30:45 +02:00
|
|
|
#if !defined(MBEDTLS_TIMING_ALT)
|
|
|
|
|
2016-02-22 10:33:34 +01:00
|
|
|
#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
|
2018-04-12 02:27:32 +02:00
|
|
|
!defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
|
2020-05-20 11:32:39 +02:00
|
|
|
!defined(__HAIKU__) && !defined(__midipix__)
|
2021-05-28 09:42:25 +02:00
|
|
|
#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
|
2016-02-22 10:33:34 +01:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#include <windows.h>
|
2018-06-23 17:55:14 +02:00
|
|
|
#include <process.h>
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
struct _hr_time
|
|
|
|
{
|
|
|
|
LARGE_INTEGER start;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
2022-04-07 14:08:21 +02:00
|
|
|
/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
|
|
|
|
* platform matches the ifdefs above, it will be used. */
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <time.h>
|
2022-03-02 17:20:23 +01:00
|
|
|
#include <sys/time.h>
|
2009-01-03 22:22:43 +01:00
|
|
|
struct _hr_time
|
|
|
|
{
|
|
|
|
struct timeval start;
|
|
|
|
};
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-06-18 13:22:57 +02:00
|
|
|
/**
|
|
|
|
* \brief Return the elapsed time in milliseconds
|
|
|
|
*
|
|
|
|
* \warning May change without notice
|
|
|
|
*
|
|
|
|
* \param val points to a timer structure
|
|
|
|
* \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
|
|
|
|
*
|
|
|
|
* \return Elapsed time since the previous reset in ms. When
|
|
|
|
* restarting, this is always 0.
|
|
|
|
*
|
|
|
|
* \note To initialize a timer, call this function with reset=1.
|
|
|
|
*
|
|
|
|
* Determining the elapsed time and resetting the timer is not
|
|
|
|
* atomic on all platforms, so after the sequence
|
|
|
|
* `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
|
|
|
|
* get_timer(0) }` the value time1+time2 is only approximately
|
|
|
|
* the delay since the first reset.
|
|
|
|
*/
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
struct _hr_time *t = (struct _hr_time *) val;
|
|
|
|
|
|
|
|
if( reset )
|
2017-10-16 19:33:06 +02:00
|
|
|
{
|
2009-01-03 22:22:43 +01:00
|
|
|
QueryPerformanceCounter( &t->start );
|
2017-10-16 19:33:06 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned long delta;
|
|
|
|
LARGE_INTEGER now, hfreq;
|
|
|
|
QueryPerformanceCounter( &now );
|
|
|
|
QueryPerformanceFrequency( &hfreq );
|
|
|
|
delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
|
|
|
|
/ hfreq.QuadPart );
|
|
|
|
return( delta );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 20:07:08 +01:00
|
|
|
#else /* _WIN32 && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
struct _hr_time *t = (struct _hr_time *) val;
|
|
|
|
|
|
|
|
if( reset )
|
|
|
|
{
|
2017-10-16 19:33:06 +02:00
|
|
|
gettimeofday( &t->start, NULL );
|
2014-07-14 22:32:21 +02:00
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2017-10-16 19:33:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned long delta;
|
|
|
|
struct timeval now;
|
|
|
|
gettimeofday( &now, NULL );
|
|
|
|
delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
|
|
|
|
+ ( now.tv_usec - t->start.tv_usec ) / 1000;
|
|
|
|
return( delta );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 20:07:08 +01:00
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-05-12 20:17:06 +02:00
|
|
|
/*
|
|
|
|
* Set delays to watch
|
|
|
|
*/
|
|
|
|
void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
|
|
|
|
{
|
|
|
|
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
|
|
|
|
|
|
|
|
ctx->int_ms = int_ms;
|
|
|
|
ctx->fin_ms = fin_ms;
|
|
|
|
|
|
|
|
if( fin_ms != 0 )
|
|
|
|
(void) mbedtls_timing_get_timer( &ctx->timer, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get number of delays expired
|
|
|
|
*/
|
|
|
|
int mbedtls_timing_get_delay( void *data )
|
|
|
|
{
|
|
|
|
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
|
|
|
|
unsigned long elapsed_ms;
|
|
|
|
|
|
|
|
if( ctx->fin_ms == 0 )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
|
|
|
|
|
|
|
|
if( elapsed_ms >= ctx->fin_ms )
|
|
|
|
return( 2 );
|
|
|
|
|
|
|
|
if( elapsed_ms >= ctx->int_ms )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2022-03-09 16:34:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the final delay.
|
|
|
|
*/
|
|
|
|
uint32_t mbedtls_timing_get_final_delay(
|
|
|
|
const mbedtls_timing_delay_context *data )
|
|
|
|
{
|
|
|
|
return( data->fin_ms );
|
|
|
|
}
|
2015-05-12 19:30:45 +02:00
|
|
|
#endif /* !MBEDTLS_TIMING_ALT */
|
|
|
|
#endif /* MBEDTLS_TIMING_C */
|