47#if !defined(MBEDTLS_CONFIG_FILE)
50#include MBEDTLS_CONFIG_FILE
53#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
57#define mbedtls_printf printf
60#if defined(MBEDTLS_TIMING_C)
64#if !defined(MBEDTLS_TIMING_ALT)
66#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
67 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
69#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
76#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
101#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
102 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
104#define HAVE_HARDCLOCK
117#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
118 defined(__GNUC__) && ( defined(__i386__) || ( \
119 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
121#define HAVE_HARDCLOCK
125 unsigned long lo, hi;
126 asm volatile(
"rdtsc" :
"=a" (lo),
"=d" (hi) );
132#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
133 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
135#define HAVE_HARDCLOCK
139 unsigned long lo, hi;
140 asm volatile(
"rdtsc" :
"=a" (lo),
"=d" (hi) );
141 return( lo | ( hi << 32 ) );
146#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
147 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
149#define HAVE_HARDCLOCK
153 unsigned long tbl, tbu0, tbu1;
157 asm volatile(
"mftbu %0" :
"=r" (tbu0) );
158 asm volatile(
"mftb %0" :
"=r" (tbl ) );
159 asm volatile(
"mftbu %0" :
"=r" (tbu1) );
161 while( tbu0 != tbu1 );
168#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
169 defined(__GNUC__) && defined(__sparc64__)
171#if defined(__OpenBSD__)
172#warning OpenBSD does not allow access to tick register using software version instead
174#define HAVE_HARDCLOCK
179 asm volatile(
"rdpr %%tick, %0;" :
"=&r" (tick) );
186#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
187 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
189#define HAVE_HARDCLOCK
194 asm volatile(
".byte 0x83, 0x41, 0x00, 0x00" );
195 asm volatile(
"mov %%g1, %0" :
"=r" (tick) );
201#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
202 defined(__GNUC__) && defined(__alpha__)
204#define HAVE_HARDCLOCK
209 asm volatile(
"rpcc %0" :
"=r" (
cc) );
210 return(
cc & 0xFFFFFFFF );
215#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
216 defined(__GNUC__) && defined(__ia64__)
218#define HAVE_HARDCLOCK
223 asm volatile(
"mov %0 = ar.itc" :
"=r" (itc) );
229#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
230 !defined(EFIX64) && !defined(EFI32)
232#define HAVE_HARDCLOCK
240 return( (
unsigned long)(
offset.QuadPart ) );
244#if !defined(HAVE_HARDCLOCK)
246#define HAVE_HARDCLOCK
248static int hardclock_init = 0;
255 if( hardclock_init == 0 )
262 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
263 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
269#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
273 struct _hr_time *
t = (
struct _hr_time *)
val;
286 delta = (
unsigned long)( (
now.QuadPart -
t->start.QuadPart ) * 1000ul
315 alarmMs = seconds * 1000;
323 struct _hr_time *
t = (
struct _hr_time *)
val;
335 delta = (
now.tv_sec -
t->start.tv_sec ) * 1000ul
336 + (
now.tv_usec -
t->start.tv_usec ) / 1000;
369 ctx->int_ms = int_ms;
370 ctx->fin_ms = fin_ms;
382 unsigned long elapsed_ms;
384 if(
ctx->fin_ms == 0 )
389 if( elapsed_ms >=
ctx->fin_ms )
392 if( elapsed_ms >=
ctx->int_ms )
400#if defined(MBEDTLS_SELF_TEST)
406static void busy_msleep(
unsigned long msec )
410 volatile unsigned long j;
425 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
426 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
427 cycles, ratio, millisecs, secs, hardfail, \
428 (unsigned long) a, (unsigned long) b ); \
429 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
430 mbedtls_timing_get_timer( &hires, 0 ), \
431 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
432 mbedtls_timing_get_delay( &ctx ) ); \
443int mbedtls_timing_self_test(
int verbose )
445 unsigned long cycles = 0, ratio = 0;
446 unsigned long millisecs = 0, secs = 0;
471 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
486 busy_msleep(
a -
a / 4 );
490 busy_msleep(
a / 4 +
b / 4 );
528 busy_msleep( millisecs );
530 ratio = cycles / millisecs;
533 for( millisecs = 2; millisecs <= 4; millisecs++ )
536 busy_msleep( millisecs );
540 if( cycles / millisecs < ratio - ratio / 5 ||
541 cycles / millisecs > ratio + ratio / 5 )
static void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
#define gettimeofday(tv, tz)
BOOL WINAPI QueryPerformanceFrequency(OUT PLARGE_INTEGER lpFrequency)
BOOL WINAPI QueryPerformanceCounter(OUT PLARGE_INTEGER lpPerformanceCount)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
static void __cdecl sighandler(int signum)
_CRTIMP uintptr_t __cdecl _beginthread(_In_ void(__cdecl *_StartAddress)(void *), _In_ unsigned _StackSize, _In_opt_ void *_ArgList)
Configuration options (set of defines)
Context for mbedtls_timing_set/get_delay()
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
ecx edi movl ebx edx edi decl ecx esi eax jecxz decl eax andl eax esi movl eax
Portable interface to timeouts and to the CPU cycle counter.
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Return the elapsed time in milliseconds.
void mbedtls_set_alarm(int seconds)
Setup an alarm clock.
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
Set a pair of delays to watch (See mbedtls_timing_get_delay().)
unsigned long mbedtls_timing_hardclock(void)
Return the CPU cycle counter value.
volatile int mbedtls_timing_alarmed
int mbedtls_timing_get_delay(void *data)
Get the status of delays (Memory helper: number of delays passed.)
_In_ PLARGE_INTEGER _In_opt_ PTIMER_APC_ROUTINE _In_opt_ PVOID TimerContext