| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "common.h" |
| 21 |
|
| 22 |
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C) |
| 23 |
#include "mbedtls/platform.h" |
| 24 |
#else |
| 25 |
#include <stdio.h> |
| 26 |
#define mbedtls_printf printf |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(MBEDTLS_TIMING_C) |
| 30 |
|
| 31 |
#include "mbedtls/timing.h" |
| 32 |
|
| 33 |
#if !defined(MBEDTLS_TIMING_ALT) |
| 34 |
|
| 35 |
#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
| 36 |
!defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ |
| 37 |
!defined(__HAIKU__) && !defined(__midipix__) |
| 38 |
#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifndef asm |
| 42 |
#define asm __asm |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
| 46 |
|
| 47 |
#include <windows.h> |
| 48 |
#include <process.h> |
| 49 |
|
| 50 |
struct _hr_time |
| 51 |
{ |
| 52 |
LARGE_INTEGER start; |
| 53 |
}; |
| 54 |
|
| 55 |
#else |
| 56 |
|
| 57 |
#include <unistd.h> |
| 58 |
#include <sys/types.h> |
| 59 |
#include <sys/time.h> |
| 60 |
#include <signal.h> |
| 61 |
#include <time.h> |
| 62 |
|
| 63 |
struct _hr_time |
| 64 |
{ |
| 65 |
struct timeval start; |
| 66 |
}; |
| 67 |
|
| 68 |
#endif |
| 69 |
|
| 70 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 71 |
( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__) |
| 72 |
|
| 73 |
#define HAVE_HARDCLOCK |
| 74 |
|
| 75 |
unsigned long mbedtls_timing_hardclock( void ) |
| 76 |
{ |
| 77 |
unsigned long tsc; |
| 78 |
__asm rdtsc |
| 79 |
__asm mov [tsc], eax |
| 80 |
return( tsc ); |
| 81 |
} |
| 82 |
#endif |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 87 |
defined(__GNUC__) && ( defined(__i386__) || ( \ |
| 88 |
( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) ) |
| 89 |
|
| 90 |
#define HAVE_HARDCLOCK |
| 91 |
|
| 92 |
unsigned long mbedtls_timing_hardclock( void ) |
| 93 |
{ |
| 94 |
unsigned long lo, hi; |
| 95 |
asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) ); |
| 96 |
return( lo ); |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
|
| 101 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 102 |
defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) ) |
| 103 |
|
| 104 |
#define HAVE_HARDCLOCK |
| 105 |
|
| 106 |
unsigned long mbedtls_timing_hardclock( void ) |
| 107 |
{ |
| 108 |
unsigned long lo, hi; |
| 109 |
asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) ); |
| 110 |
return( lo | ( hi << 32 ) ); |
| 111 |
} |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 116 |
defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) ) |
| 117 |
|
| 118 |
#define HAVE_HARDCLOCK |
| 119 |
|
| 120 |
unsigned long mbedtls_timing_hardclock( void ) |
| 121 |
{ |
| 122 |
unsigned long tbl, tbu0, tbu1; |
| 123 |
|
| 124 |
do |
| 125 |
{ |
| 126 |
asm volatile( "mftbu %0" : "=r" (tbu0) ); |
| 127 |
asm volatile( "mftb %0" : "=r" (tbl ) ); |
| 128 |
asm volatile( "mftbu %0" : "=r" (tbu1) ); |
| 129 |
} |
| 130 |
while( tbu0 != tbu1 ); |
| 131 |
|
| 132 |
return( tbl ); |
| 133 |
} |
| 134 |
#endif |
| 135 |
|
| 136 |
|
| 137 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 138 |
defined(__GNUC__) && defined(__sparc64__) |
| 139 |
|
| 140 |
#if defined(__OpenBSD__) |
| 141 |
#warning OpenBSD does not allow access to tick register using software version instead |
| 142 |
#else |
| 143 |
#define HAVE_HARDCLOCK |
| 144 |
|
| 145 |
unsigned long mbedtls_timing_hardclock( void ) |
| 146 |
{ |
| 147 |
unsigned long tick; |
| 148 |
asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) ); |
| 149 |
return( tick ); |
| 150 |
} |
| 151 |
#endif |
| 152 |
#endif |
| 153 |
|
| 154 |
|
| 155 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 156 |
defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__) |
| 157 |
|
| 158 |
#define HAVE_HARDCLOCK |
| 159 |
|
| 160 |
unsigned long mbedtls_timing_hardclock( void ) |
| 161 |
{ |
| 162 |
unsigned long tick; |
| 163 |
asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" ); |
| 164 |
asm volatile( "mov %%g1, %0" : "=r" (tick) ); |
| 165 |
return( tick ); |
| 166 |
} |
| 167 |
#endif |
| 168 |
|
| 169 |
|
| 170 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 171 |
defined(__GNUC__) && defined(__alpha__) |
| 172 |
|
| 173 |
#define HAVE_HARDCLOCK |
| 174 |
|
| 175 |
unsigned long mbedtls_timing_hardclock( void ) |
| 176 |
{ |
| 177 |
unsigned long cc; |
| 178 |
asm volatile( "rpcc %0" : "=r" (cc) ); |
| 179 |
return( cc & 0xFFFFFFFF ); |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
|
| 184 |
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ |
| 185 |
defined(__GNUC__) && defined(__ia64__) |
| 186 |
|
| 187 |
#define HAVE_HARDCLOCK |
| 188 |
|
| 189 |
unsigned long mbedtls_timing_hardclock( void ) |
| 190 |
{ |
| 191 |
unsigned long itc; |
| 192 |
asm volatile( "mov %0 = ar.itc" : "=r" (itc) ); |
| 193 |
return( itc ); |
| 194 |
} |
| 195 |
#endif |
| 196 |
|
| 197 |
|
| 198 |
#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \ |
| 199 |
!defined(EFIX64) && !defined(EFI32) |
| 200 |
|
| 201 |
#define HAVE_HARDCLOCK |
| 202 |
|
| 203 |
unsigned long mbedtls_timing_hardclock( void ) |
| 204 |
{ |
| 205 |
LARGE_INTEGER offset; |
| 206 |
|
| 207 |
QueryPerformanceCounter( &offset ); |
| 208 |
|
| 209 |
return( (unsigned long)( offset.QuadPart ) ); |
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
#if !defined(HAVE_HARDCLOCK) |
| 214 |
|
| 215 |
#define HAVE_HARDCLOCK |
| 216 |
|
| 217 |
static int hardclock_init = 0; |
| 218 |
static struct timeval tv_init; |
| 219 |
|
| 220 |
unsigned long mbedtls_timing_hardclock( void ) |
| 221 |
{ |
| 222 |
struct timeval tv_cur; |
| 223 |
|
| 224 |
if( hardclock_init == 0 ) |
| 225 |
{ |
| 226 |
gettimeofday( &tv_init, NULL ); |
| 227 |
hardclock_init = 1; |
| 228 |
} |
| 229 |
|
| 230 |
gettimeofday( &tv_cur, NULL ); |
| 231 |
return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000 |
| 232 |
+ ( tv_cur.tv_usec - tv_init.tv_usec ) ); |
| 233 |
} |
| 234 |
#endif |
| 235 |
|
| 236 |
volatile int mbedtls_timing_alarmed = 0; |
| 237 |
|
| 238 |
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
| 239 |
|
| 240 |
unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ) |
| 241 |
{ |
| 242 |
struct _hr_time *t = (struct _hr_time *) val; |
| 243 |
|
| 244 |
if( reset ) |
| 245 |
{ |
| 246 |
QueryPerformanceCounter( &t->start ); |
| 247 |
return( 0 ); |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
unsigned long delta; |
| 252 |
LARGE_INTEGER now, hfreq; |
| 253 |
QueryPerformanceCounter( &now ); |
| 254 |
QueryPerformanceFrequency( &hfreq ); |
| 255 |
delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul |
| 256 |
/ hfreq.QuadPart ); |
| 257 |
return( delta ); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
static DWORD alarmMs; |
| 263 |
|
| 264 |
static void TimerProc( void *TimerContext ) |
| 265 |
{ |
| 266 |
(void) TimerContext; |
| 267 |
Sleep( alarmMs ); |
| 268 |
mbedtls_timing_alarmed = 1; |
| 269 |
|
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
void mbedtls_set_alarm( int seconds ) |
| 274 |
{ |
| 275 |
if( seconds == 0 ) |
| 276 |
{ |
| 277 |
|
| 278 |
|
| 279 |
mbedtls_timing_alarmed = 1; |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
mbedtls_timing_alarmed = 0; |
| 284 |
alarmMs = seconds * 1000; |
| 285 |
(void) _beginthread( TimerProc, 0, NULL ); |
| 286 |
} |
| 287 |
|
| 288 |
#else |
| 289 |
|
| 290 |
unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ) |
| 291 |
{ |
| 292 |
struct _hr_time *t = (struct _hr_time *) val; |
| 293 |
|
| 294 |
if( reset ) |
| 295 |
{ |
| 296 |
gettimeofday( &t->start, NULL ); |
| 297 |
return( 0 ); |
| 298 |
} |
| 299 |
else |
| 300 |
{ |
| 301 |
unsigned long delta; |
| 302 |
struct timeval now; |
| 303 |
gettimeofday( &now, NULL ); |
| 304 |
delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul |
| 305 |
+ ( now.tv_usec - t->start.tv_usec ) / 1000; |
| 306 |
return( delta ); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
static void sighandler( int signum ) |
| 311 |
{ |
| 312 |
mbedtls_timing_alarmed = 1; |
| 313 |
signal( signum, sighandler ); |
| 314 |
} |
| 315 |
|
| 316 |
void mbedtls_set_alarm( int seconds ) |
| 317 |
{ |
| 318 |
mbedtls_timing_alarmed = 0; |
| 319 |
signal( SIGALRM, sighandler ); |
| 320 |
alarm( seconds ); |
| 321 |
if( seconds == 0 ) |
| 322 |
{ |
| 323 |
|
| 324 |
|
| 325 |
mbedtls_timing_alarmed = 1; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
#endif |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ) |
| 335 |
{ |
| 336 |
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
| 337 |
|
| 338 |
ctx->int_ms = int_ms; |
| 339 |
ctx->fin_ms = fin_ms; |
| 340 |
|
| 341 |
if( fin_ms != 0 ) |
| 342 |
(void) mbedtls_timing_get_timer( &ctx->timer, 1 ); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
int mbedtls_timing_get_delay( void *data ) |
| 349 |
{ |
| 350 |
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
| 351 |
unsigned long elapsed_ms; |
| 352 |
|
| 353 |
if( ctx->fin_ms == 0 ) |
| 354 |
return( -1 ); |
| 355 |
|
| 356 |
elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 ); |
| 357 |
|
| 358 |
if( elapsed_ms >= ctx->fin_ms ) |
| 359 |
return( 2 ); |
| 360 |
|
| 361 |
if( elapsed_ms >= ctx->int_ms ) |
| 362 |
return( 1 ); |
| 363 |
|
| 364 |
return( 0 ); |
| 365 |
} |
| 366 |
|
| 367 |
#endif |
| 368 |
|
| 369 |
#if defined(MBEDTLS_SELF_TEST) |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
static void busy_msleep( unsigned long msec ) |
| 376 |
{ |
| 377 |
struct mbedtls_timing_hr_time hires; |
| 378 |
unsigned long i = 0; |
| 379 |
volatile unsigned long j; |
| 380 |
|
| 381 |
(void) mbedtls_timing_get_timer( &hires, 1 ); |
| 382 |
|
| 383 |
while( mbedtls_timing_get_timer( &hires, 0 ) < msec ) |
| 384 |
i++; |
| 385 |
|
| 386 |
j = i; |
| 387 |
(void) j; |
| 388 |
} |
| 389 |
|
| 390 |
#define FAIL do \ |
| 391 |
{ \ |
| 392 |
if( verbose != 0 ) \ |
| 393 |
{ \ |
| 394 |
mbedtls_printf( "failed at line %d\n", __LINE__ ); \ |
| 395 |
mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \ |
| 396 |
cycles, ratio, millisecs, secs, hardfail, \ |
| 397 |
(unsigned long) a, (unsigned long) b ); \ |
| 398 |
mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \ |
| 399 |
mbedtls_timing_get_timer( &hires, 0 ), \ |
| 400 |
mbedtls_timing_get_timer( &ctx.timer, 0 ), \ |
| 401 |
mbedtls_timing_get_delay( &ctx ) ); \ |
| 402 |
} \ |
| 403 |
return( 1 ); \ |
| 404 |
} while( 0 ) |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
int mbedtls_timing_self_test( int verbose ) |
| 413 |
{ |
| 414 |
unsigned long cycles = 0, ratio = 0; |
| 415 |
unsigned long millisecs = 0, secs = 0; |
| 416 |
int hardfail = 0; |
| 417 |
struct mbedtls_timing_hr_time hires; |
| 418 |
uint32_t a = 0, b = 0; |
| 419 |
mbedtls_timing_delay_context ctx; |
| 420 |
|
| 421 |
if( verbose != 0 ) |
| 422 |
mbedtls_printf( " TIMING tests note: will take some time!\n" ); |
| 423 |
|
| 424 |
if( verbose != 0 ) |
| 425 |
mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " ); |
| 426 |
|
| 427 |
{ |
| 428 |
secs = 1; |
| 429 |
|
| 430 |
(void) mbedtls_timing_get_timer( &hires, 1 ); |
| 431 |
|
| 432 |
mbedtls_set_alarm( (int) secs ); |
| 433 |
while( !mbedtls_timing_alarmed ) |
| 434 |
; |
| 435 |
|
| 436 |
millisecs = mbedtls_timing_get_timer( &hires, 0 ); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 ) |
| 441 |
FAIL; |
| 442 |
} |
| 443 |
|
| 444 |
if( verbose != 0 ) |
| 445 |
mbedtls_printf( "passed\n" ); |
| 446 |
|
| 447 |
if( verbose != 0 ) |
| 448 |
mbedtls_printf( " TIMING test #2 (set/get_delay ): " ); |
| 449 |
|
| 450 |
{ |
| 451 |
a = 800; |
| 452 |
b = 400; |
| 453 |
mbedtls_timing_set_delay( &ctx, a, a + b ); |
| 454 |
|
| 455 |
busy_msleep( a - a / 4 ); |
| 456 |
if( mbedtls_timing_get_delay( &ctx ) != 0 ) |
| 457 |
FAIL; |
| 458 |
|
| 459 |
busy_msleep( a / 4 + b / 4 ); |
| 460 |
if( mbedtls_timing_get_delay( &ctx ) != 1 ) |
| 461 |
FAIL; |
| 462 |
|
| 463 |
busy_msleep( b ); |
| 464 |
if( mbedtls_timing_get_delay( &ctx ) != 2 ) |
| 465 |
FAIL; |
| 466 |
} |
| 467 |
|
| 468 |
mbedtls_timing_set_delay( &ctx, 0, 0 ); |
| 469 |
busy_msleep( 200 ); |
| 470 |
if( mbedtls_timing_get_delay( &ctx ) != -1 ) |
| 471 |
FAIL; |
| 472 |
|
| 473 |
if( verbose != 0 ) |
| 474 |
mbedtls_printf( "passed\n" ); |
| 475 |
|
| 476 |
if( verbose != 0 ) |
| 477 |
mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " ); |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
hard_test: |
| 486 |
if( hardfail > 1 ) |
| 487 |
{ |
| 488 |
if( verbose != 0 ) |
| 489 |
mbedtls_printf( "failed (ignored)\n" ); |
| 490 |
|
| 491 |
goto hard_test_done; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
millisecs = 1; |
| 496 |
cycles = mbedtls_timing_hardclock(); |
| 497 |
busy_msleep( millisecs ); |
| 498 |
cycles = mbedtls_timing_hardclock() - cycles; |
| 499 |
ratio = cycles / millisecs; |
| 500 |
|
| 501 |
|
| 502 |
for( millisecs = 2; millisecs <= 4; millisecs++ ) |
| 503 |
{ |
| 504 |
cycles = mbedtls_timing_hardclock(); |
| 505 |
busy_msleep( millisecs ); |
| 506 |
cycles = mbedtls_timing_hardclock() - cycles; |
| 507 |
|
| 508 |
|
| 509 |
if( cycles / millisecs < ratio - ratio / 5 || |
| 510 |
cycles / millisecs > ratio + ratio / 5 ) |
| 511 |
{ |
| 512 |
hardfail++; |
| 513 |
goto hard_test; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
if( verbose != 0 ) |
| 518 |
mbedtls_printf( "passed\n" ); |
| 519 |
|
| 520 |
hard_test_done: |
| 521 |
|
| 522 |
if( verbose != 0 ) |
| 523 |
mbedtls_printf( "\n" ); |
| 524 |
|
| 525 |
return( 0 ); |
| 526 |
} |
| 527 |
|
| 528 |
#endif |
| 529 |
|
| 530 |
#endif |