timing.c 12.6 KB
Newer Older
1 2 3
/*
 *  Portable interface to the CPU cycle counter
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker's avatar
Paul Bakker committed
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker's avatar
Paul Bakker committed
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
23
#if !defined(MBEDTLS_CONFIG_FILE)
24
#include "mbedtls/config.h"
25
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
26
#include MBEDTLS_CONFIG_FILE
27
#endif
28

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
29
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
30
#include "mbedtls/platform.h"
31 32
#else
#include <stdio.h>
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
33
#define mbedtls_printf     printf
34 35
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
36
#if defined(MBEDTLS_TIMING_C) && !defined(MBEDTLS_TIMING_ALT)
37

38
#include "mbedtls/timing.h"
39

40
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#include <windows.h>
#include <winbase.h>

struct _hr_time
{
    LARGE_INTEGER start;
};

#else

#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <time.h>

struct _hr_time
{
    struct timeval start;
};

63
#endif /* _WIN32 && !EFIX64 && !EFI32 */
64

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
65
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
66
    ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
67

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
68
#define MBEDTLS_HAVE_HARDCLOCK
69

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
70
unsigned long mbedtls_timing_hardclock( void )
71 72 73 74 75 76
{
    unsigned long tsc;
    __asm   rdtsc
    __asm   mov  [tsc], eax
    return( tsc );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
77
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
78
          ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
79

80
/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
81
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
82 83
    defined(__GNUC__) && ( defined(__i386__) || (                       \
    ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
84

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
85
#define MBEDTLS_HAVE_HARDCLOCK
86

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
87
unsigned long mbedtls_timing_hardclock( void )
88
{
89
    unsigned long lo, hi;
90
    asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
91
    return( lo );
92
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
93
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
94
          __GNUC__ && __i386__ */
95

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
96
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
97
    defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
98

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
99
#define MBEDTLS_HAVE_HARDCLOCK
100

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
101
unsigned long mbedtls_timing_hardclock( void )
102 103
{
    unsigned long lo, hi;
104
    asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
105
    return( lo | ( hi << 32 ) );
106
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
107
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
108
          __GNUC__ && ( __amd64__ || __x86_64__ ) */
109

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
110
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
111
    defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
112

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
113
#define MBEDTLS_HAVE_HARDCLOCK
114

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
115
unsigned long mbedtls_timing_hardclock( void )
116 117 118 119 120
{
    unsigned long tbl, tbu0, tbu1;

    do
    {
121 122 123
        asm volatile( "mftbu %0" : "=r" (tbu0) );
        asm volatile( "mftb  %0" : "=r" (tbl ) );
        asm volatile( "mftbu %0" : "=r" (tbu1) );
124 125 126 127 128
    }
    while( tbu0 != tbu1 );

    return( tbl );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
129
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
130
          __GNUC__ && ( __powerpc__ || __ppc__ ) */
131

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
132
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
133
    defined(__GNUC__) && defined(__sparc64__)
134

135 136
#if defined(__OpenBSD__)
#warning OpenBSD does not allow access to tick register using software version instead
137
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
138
#define MBEDTLS_HAVE_HARDCLOCK
139

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
140
unsigned long mbedtls_timing_hardclock( void )
141 142
{
    unsigned long tick;
143
    asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
144 145
    return( tick );
}
146
#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
147
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
148
          __GNUC__ && __sparc64__ */
149

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
150
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
151 152
    defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
153
#define MBEDTLS_HAVE_HARDCLOCK
154

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
155
unsigned long mbedtls_timing_hardclock( void )
156 157
{
    unsigned long tick;
158 159
    asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
    asm volatile( "mov   %%g1, %0" : "=r" (tick) );
160 161
    return( tick );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
162
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
163
          __GNUC__ && __sparc__ && !__sparc64__ */
164

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
165
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&      \
166 167
    defined(__GNUC__) && defined(__alpha__)

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
168
#define MBEDTLS_HAVE_HARDCLOCK
169

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
170
unsigned long mbedtls_timing_hardclock( void )
171 172
{
    unsigned long cc;
173
    asm volatile( "rpcc %0" : "=r" (cc) );
174 175
    return( cc & 0xFFFFFFFF );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
176
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
177
          __GNUC__ && __alpha__ */
178

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
179
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&      \
180 181
    defined(__GNUC__) && defined(__ia64__)

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
182
#define MBEDTLS_HAVE_HARDCLOCK
183

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
184
unsigned long mbedtls_timing_hardclock( void )
185 186
{
    unsigned long itc;
187
    asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
188 189
    return( itc );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
190
#endif /* !MBEDTLS_HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
191
          __GNUC__ && __ia64__ */
192

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
193
#if !defined(MBEDTLS_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
194
    !defined(EFIX64) && !defined(EFI32)
195

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
196
#define MBEDTLS_HAVE_HARDCLOCK
197

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
198
unsigned long mbedtls_timing_hardclock( void )
199 200
{
    LARGE_INTEGER offset;
201

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
202
    QueryPerformanceCounter( &offset );
203

204
    return( (unsigned long)( offset.QuadPart ) );
205
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
206
#endif /* !MBEDTLS_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
207

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
208
#if !defined(MBEDTLS_HAVE_HARDCLOCK)
209

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
210
#define MBEDTLS_HAVE_HARDCLOCK
211 212 213 214

static int hardclock_init = 0;
static struct timeval tv_init;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
215
unsigned long mbedtls_timing_hardclock( void )
216 217 218 219 220 221 222 223 224 225 226 227 228
{
    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 ) );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
229
#endif /* !MBEDTLS_HAVE_HARDCLOCK */
230

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
231
volatile int mbedtls_timing_alarmed = 0;
232

233
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
234

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
235
unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
    unsigned long delta;
    LARGE_INTEGER offset, hfreq;
    struct _hr_time *t = (struct _hr_time *) val;

    QueryPerformanceCounter(  &offset );
    QueryPerformanceFrequency( &hfreq );

    delta = (unsigned long)( ( 1000 *
        ( offset.QuadPart - t->start.QuadPart ) ) /
           hfreq.QuadPart );

    if( reset )
        QueryPerformanceCounter( &t->start );

    return( delta );
}

254 255 256
/* It's OK to use a global because alarm() is supposed to be global anyway */
static DWORD alarmMs;

257
static DWORD WINAPI TimerProc( LPVOID TimerContext )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
258
{
259 260
    ((void) TimerContext);
    Sleep( alarmMs );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
261
    mbedtls_timing_alarmed = 1;
262 263 264
    return( TRUE );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
265
void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
266
{
267 268
    DWORD ThreadId;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
269
    mbedtls_timing_alarmed = 0;
270 271
    alarmMs = seconds * 1000;
    CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
272 273
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
274
void mbedtls_timing_m_sleep( int milliseconds )
275 276 277 278
{
    Sleep( milliseconds );
}

279
#else /* _WIN32 && !EFIX64 && !EFI32 */
280

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
281
unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
282 283 284 285 286 287 288 289 290 291 292
{
    unsigned long delta;
    struct timeval offset;
    struct _hr_time *t = (struct _hr_time *) val;

    gettimeofday( &offset, NULL );

    if( reset )
    {
        t->start.tv_sec  = offset.tv_sec;
        t->start.tv_usec = offset.tv_usec;
293
        return( 0 );
294 295
    }

296 297 298
    delta = ( offset.tv_sec  - t->start.tv_sec  ) * 1000
          + ( offset.tv_usec - t->start.tv_usec ) / 1000;

299 300 301
    return( delta );
}

Paul Bakker's avatar
Paul Bakker committed
302
#if defined(INTEGRITY)
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
303
void mbedtls_timing_m_sleep( int milliseconds )
Paul Bakker's avatar
Paul Bakker committed
304 305 306 307
{
    usleep( milliseconds * 1000 );
}

308
#else /* INTEGRITY */
Paul Bakker's avatar
Paul Bakker committed
309

310
static void sighandler( int signum )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
311
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
312
    mbedtls_timing_alarmed = 1;
313 314 315
    signal( signum, sighandler );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
316
void mbedtls_set_alarm( int seconds )
317
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
318
    mbedtls_timing_alarmed = 0;
319 320 321 322
    signal( SIGALRM, sighandler );
    alarm( seconds );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
323
void mbedtls_timing_m_sleep( int milliseconds )
324 325 326 327
{
    struct timeval tv;

    tv.tv_sec  = milliseconds / 1000;
328
    tv.tv_usec = ( milliseconds % 1000 ) * 1000;
329 330 331

    select( 0, NULL, NULL, NULL, &tv );
}
Paul Bakker's avatar
Paul Bakker committed
332
#endif /* INTEGRITY */
333

334
#endif /* _WIN32 && !EFIX64 && !EFI32 */
335

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
336
#if defined(MBEDTLS_SELF_TEST)
337

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
338 339
/* To test mbedtls_net_usleep against our functions */
#if defined(MBEDTLS_NET_C) && defined(MBEDTLS_HAVE_TIME)
340
#include "mbedtls/net.h"
341 342
#endif

343 344
/*
 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
345
 * Used for testing mbedtls_timing_hardclock.
346 347 348
 */
static void busy_msleep( unsigned long msec )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
349
    struct mbedtls_timing_hr_time hires;
350 351 352
    unsigned long i = 0; /* for busy-waiting */
    volatile unsigned long j; /* to prevent optimisation */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
353
    (void) mbedtls_timing_get_timer( &hires, 1 );
354

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
355
    while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
356 357 358 359 360 361
        i++;

    j = i;
    (void) j;
}

362 363
/*
 * Checkup routine
364 365 366
 *
 * Warning: this is work in progress, some tests may not be reliable enough
 * yet! False positives may happen.
367
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
368
int mbedtls_timing_self_test( int verbose )
369 370 371 372
{
    unsigned long cycles, ratio;
    unsigned long millisecs, secs;
    int hardfail;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
373
    struct mbedtls_timing_hr_time hires;
374

375
    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
376
        mbedtls_printf( "  TIMING tests note: will take some time!\n" );
377 378

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
379
        mbedtls_printf( "  TIMING test #1 (m_sleep   / get_timer): " );
380 381 382

    for( secs = 1; secs <= 3; secs++ )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
383
        (void) mbedtls_timing_get_timer( &hires, 1 );
384

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
385
        mbedtls_timing_m_sleep( (int)( 500 * secs ) );
386

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
387
        millisecs = mbedtls_timing_get_timer( &hires, 0 );
388

389
        if( millisecs < 450 * secs || millisecs > 550 * secs )
390 391
        {
            if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
392
                mbedtls_printf( "failed\n" );
393 394 395 396 397 398

            return( 1 );
        }
    }

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
399
        mbedtls_printf( "passed\n" );
400 401

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
402
        mbedtls_printf( "  TIMING test #2 (set_alarm / get_timer): " );
403 404 405

    for( secs = 1; secs <= 3; secs++ )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
406
        (void) mbedtls_timing_get_timer( &hires, 1 );
407

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
408 409
        mbedtls_set_alarm( (int) secs );
        while( !mbedtls_timing_alarmed )
410 411
            ;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
412
        millisecs = mbedtls_timing_get_timer( &hires, 0 );
413 414 415 416

        if( millisecs < 900 * secs || millisecs > 1100 * secs )
        {
            if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
417
                mbedtls_printf( "failed\n" );
418 419 420 421 422 423

            return( 1 );
        }
    }

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
424
        mbedtls_printf( "passed\n" );
425 426

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
427
        mbedtls_printf( "  TIMING test #3 (hardclock / get_timer): " );
428 429 430 431 432 433 434 435 436 437 438 439

    /*
     * Allow one failure for possible counter wrapping.
     * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
     * since the whole test is about 10ms, it shouldn't happen twice in a row.
     */
    hardfail = 0;

hard_test:
    if( hardfail > 1 )
    {
        if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
440
            mbedtls_printf( "failed\n" );
441 442 443 444 445

        return( 1 );
    }

    /* Get a reference ratio cycles/ms */
446
    millisecs = 1;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
447
    cycles = mbedtls_timing_hardclock();
448
    busy_msleep( millisecs );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
449
    cycles = mbedtls_timing_hardclock() - cycles;
450
    ratio = cycles / millisecs;
451

452
    /* Check that the ratio is mostly constant */
453 454
    for( millisecs = 2; millisecs <= 4; millisecs++ )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
455
        cycles = mbedtls_timing_hardclock();
456
        busy_msleep( millisecs );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
457
        cycles = mbedtls_timing_hardclock() - cycles;
458 459 460 461 462 463 464 465 466 467 468

        /* Allow variation up to 20% */
        if( cycles / millisecs < ratio - ratio / 5 ||
            cycles / millisecs > ratio + ratio / 5 )
        {
            hardfail++;
            goto hard_test;
        }
    }

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
469
        mbedtls_printf( "passed\n" );
470

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
471
#if defined(MBEDTLS_NET_C) && defined(MBEDTLS_HAVE_TIME)
472
    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
473
        mbedtls_printf( "  TIMING test #4 (net_usleep/ get_timer): " );
474 475 476

    for( secs = 1; secs <= 3; secs++ )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
477
        (void) mbedtls_timing_get_timer( &hires, 1 );
478

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
479
        mbedtls_net_usleep( 500000 * secs );
480

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
481
        millisecs = mbedtls_timing_get_timer( &hires, 0 );
482 483 484 485

        if( millisecs < 450 * secs || millisecs > 550 * secs )
        {
            if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
486
                mbedtls_printf( "failed\n" );
487 488 489 490 491 492

            return( 1 );
        }
    }

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
493 494
        mbedtls_printf( "passed\n" );
#endif /* MBEDTLS_NET_C */
495

496
    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
497
        mbedtls_printf( "\n" );
498

499 500 501
    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
502
#endif /* MBEDTLS_SELF_TEST */
503

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
504
#endif /* MBEDTLS_TIMING_C && !MBEDTLS_TIMING_ALT */