Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /*
Simon Cooksey 0:fb7af294d5d9 2 * Portable interface to the CPU cycle counter
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 5 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 8 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 *
Simon Cooksey 0:fb7af294d5d9 19 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 20 */
Simon Cooksey 0:fb7af294d5d9 21
Simon Cooksey 0:fb7af294d5d9 22 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 23 #include "mbedtls/config.h"
Simon Cooksey 0:fb7af294d5d9 24 #else
Simon Cooksey 0:fb7af294d5d9 25 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 26 #endif
Simon Cooksey 0:fb7af294d5d9 27
Simon Cooksey 0:fb7af294d5d9 28 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Simon Cooksey 0:fb7af294d5d9 29 #include "mbedtls/platform.h"
Simon Cooksey 0:fb7af294d5d9 30 #else
Simon Cooksey 0:fb7af294d5d9 31 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 32 #define mbedtls_printf printf
Simon Cooksey 0:fb7af294d5d9 33 #endif
Simon Cooksey 0:fb7af294d5d9 34
Simon Cooksey 0:fb7af294d5d9 35 #if defined(MBEDTLS_TIMING_C)
Simon Cooksey 0:fb7af294d5d9 36
Simon Cooksey 0:fb7af294d5d9 37 #include "mbedtls/timing.h"
Simon Cooksey 0:fb7af294d5d9 38
Simon Cooksey 0:fb7af294d5d9 39 #if !defined(MBEDTLS_TIMING_ALT)
Simon Cooksey 0:fb7af294d5d9 40
Simon Cooksey 0:fb7af294d5d9 41 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Simon Cooksey 0:fb7af294d5d9 42 !defined(__APPLE__) && !defined(_WIN32)
Simon Cooksey 0:fb7af294d5d9 43 #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
Simon Cooksey 0:fb7af294d5d9 44 #endif
Simon Cooksey 0:fb7af294d5d9 45
Simon Cooksey 0:fb7af294d5d9 46 #ifndef asm
Simon Cooksey 0:fb7af294d5d9 47 #define asm __asm
Simon Cooksey 0:fb7af294d5d9 48 #endif
Simon Cooksey 0:fb7af294d5d9 49
Simon Cooksey 0:fb7af294d5d9 50 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Simon Cooksey 0:fb7af294d5d9 51
Simon Cooksey 0:fb7af294d5d9 52 #include <windows.h>
Simon Cooksey 0:fb7af294d5d9 53 #include <winbase.h>
Simon Cooksey 0:fb7af294d5d9 54
Simon Cooksey 0:fb7af294d5d9 55 struct _hr_time
Simon Cooksey 0:fb7af294d5d9 56 {
Simon Cooksey 0:fb7af294d5d9 57 LARGE_INTEGER start;
Simon Cooksey 0:fb7af294d5d9 58 };
Simon Cooksey 0:fb7af294d5d9 59
Simon Cooksey 0:fb7af294d5d9 60 #else
Simon Cooksey 0:fb7af294d5d9 61
Simon Cooksey 0:fb7af294d5d9 62 #include <unistd.h>
Simon Cooksey 0:fb7af294d5d9 63 #include <sys/types.h>
Simon Cooksey 0:fb7af294d5d9 64 #include <sys/time.h>
Simon Cooksey 0:fb7af294d5d9 65 #include <signal.h>
Simon Cooksey 0:fb7af294d5d9 66 #include <time.h>
Simon Cooksey 0:fb7af294d5d9 67
Simon Cooksey 0:fb7af294d5d9 68 struct _hr_time
Simon Cooksey 0:fb7af294d5d9 69 {
Simon Cooksey 0:fb7af294d5d9 70 struct timeval start;
Simon Cooksey 0:fb7af294d5d9 71 };
Simon Cooksey 0:fb7af294d5d9 72
Simon Cooksey 0:fb7af294d5d9 73 #endif /* _WIN32 && !EFIX64 && !EFI32 */
Simon Cooksey 0:fb7af294d5d9 74
Simon Cooksey 0:fb7af294d5d9 75 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 76 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Simon Cooksey 0:fb7af294d5d9 77
Simon Cooksey 0:fb7af294d5d9 78 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 79
Simon Cooksey 0:fb7af294d5d9 80 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 81 {
Simon Cooksey 0:fb7af294d5d9 82 unsigned long tsc;
Simon Cooksey 0:fb7af294d5d9 83 __asm rdtsc
Simon Cooksey 0:fb7af294d5d9 84 __asm mov [tsc], eax
Simon Cooksey 0:fb7af294d5d9 85 return( tsc );
Simon Cooksey 0:fb7af294d5d9 86 }
Simon Cooksey 0:fb7af294d5d9 87 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 88 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Simon Cooksey 0:fb7af294d5d9 89
Simon Cooksey 0:fb7af294d5d9 90 /* some versions of mingw-64 have 32-bit longs even on x84_64 */
Simon Cooksey 0:fb7af294d5d9 91 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 92 defined(__GNUC__) && ( defined(__i386__) || ( \
Simon Cooksey 0:fb7af294d5d9 93 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Simon Cooksey 0:fb7af294d5d9 94
Simon Cooksey 0:fb7af294d5d9 95 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 96
Simon Cooksey 0:fb7af294d5d9 97 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 98 {
Simon Cooksey 0:fb7af294d5d9 99 unsigned long lo, hi;
Simon Cooksey 0:fb7af294d5d9 100 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Simon Cooksey 0:fb7af294d5d9 101 return( lo );
Simon Cooksey 0:fb7af294d5d9 102 }
Simon Cooksey 0:fb7af294d5d9 103 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 104 __GNUC__ && __i386__ */
Simon Cooksey 0:fb7af294d5d9 105
Simon Cooksey 0:fb7af294d5d9 106 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 107 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Simon Cooksey 0:fb7af294d5d9 108
Simon Cooksey 0:fb7af294d5d9 109 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 110
Simon Cooksey 0:fb7af294d5d9 111 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 112 {
Simon Cooksey 0:fb7af294d5d9 113 unsigned long lo, hi;
Simon Cooksey 0:fb7af294d5d9 114 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Simon Cooksey 0:fb7af294d5d9 115 return( lo | ( hi << 32 ) );
Simon Cooksey 0:fb7af294d5d9 116 }
Simon Cooksey 0:fb7af294d5d9 117 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 118 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Simon Cooksey 0:fb7af294d5d9 119
Simon Cooksey 0:fb7af294d5d9 120 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 121 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Simon Cooksey 0:fb7af294d5d9 122
Simon Cooksey 0:fb7af294d5d9 123 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 124
Simon Cooksey 0:fb7af294d5d9 125 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 126 {
Simon Cooksey 0:fb7af294d5d9 127 unsigned long tbl, tbu0, tbu1;
Simon Cooksey 0:fb7af294d5d9 128
Simon Cooksey 0:fb7af294d5d9 129 do
Simon Cooksey 0:fb7af294d5d9 130 {
Simon Cooksey 0:fb7af294d5d9 131 asm volatile( "mftbu %0" : "=r" (tbu0) );
Simon Cooksey 0:fb7af294d5d9 132 asm volatile( "mftb %0" : "=r" (tbl ) );
Simon Cooksey 0:fb7af294d5d9 133 asm volatile( "mftbu %0" : "=r" (tbu1) );
Simon Cooksey 0:fb7af294d5d9 134 }
Simon Cooksey 0:fb7af294d5d9 135 while( tbu0 != tbu1 );
Simon Cooksey 0:fb7af294d5d9 136
Simon Cooksey 0:fb7af294d5d9 137 return( tbl );
Simon Cooksey 0:fb7af294d5d9 138 }
Simon Cooksey 0:fb7af294d5d9 139 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 140 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Simon Cooksey 0:fb7af294d5d9 141
Simon Cooksey 0:fb7af294d5d9 142 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 143 defined(__GNUC__) && defined(__sparc64__)
Simon Cooksey 0:fb7af294d5d9 144
Simon Cooksey 0:fb7af294d5d9 145 #if defined(__OpenBSD__)
Simon Cooksey 0:fb7af294d5d9 146 #warning OpenBSD does not allow access to tick register using software version instead
Simon Cooksey 0:fb7af294d5d9 147 #else
Simon Cooksey 0:fb7af294d5d9 148 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 149
Simon Cooksey 0:fb7af294d5d9 150 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 151 {
Simon Cooksey 0:fb7af294d5d9 152 unsigned long tick;
Simon Cooksey 0:fb7af294d5d9 153 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Simon Cooksey 0:fb7af294d5d9 154 return( tick );
Simon Cooksey 0:fb7af294d5d9 155 }
Simon Cooksey 0:fb7af294d5d9 156 #endif /* __OpenBSD__ */
Simon Cooksey 0:fb7af294d5d9 157 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 158 __GNUC__ && __sparc64__ */
Simon Cooksey 0:fb7af294d5d9 159
Simon Cooksey 0:fb7af294d5d9 160 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 161 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
Simon Cooksey 0:fb7af294d5d9 162
Simon Cooksey 0:fb7af294d5d9 163 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 164
Simon Cooksey 0:fb7af294d5d9 165 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 166 {
Simon Cooksey 0:fb7af294d5d9 167 unsigned long tick;
Simon Cooksey 0:fb7af294d5d9 168 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
Simon Cooksey 0:fb7af294d5d9 169 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Simon Cooksey 0:fb7af294d5d9 170 return( tick );
Simon Cooksey 0:fb7af294d5d9 171 }
Simon Cooksey 0:fb7af294d5d9 172 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 173 __GNUC__ && __sparc__ && !__sparc64__ */
Simon Cooksey 0:fb7af294d5d9 174
Simon Cooksey 0:fb7af294d5d9 175 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 176 defined(__GNUC__) && defined(__alpha__)
Simon Cooksey 0:fb7af294d5d9 177
Simon Cooksey 0:fb7af294d5d9 178 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 179
Simon Cooksey 0:fb7af294d5d9 180 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 181 {
Simon Cooksey 0:fb7af294d5d9 182 unsigned long cc;
Simon Cooksey 0:fb7af294d5d9 183 asm volatile( "rpcc %0" : "=r" (cc) );
Simon Cooksey 0:fb7af294d5d9 184 return( cc & 0xFFFFFFFF );
Simon Cooksey 0:fb7af294d5d9 185 }
Simon Cooksey 0:fb7af294d5d9 186 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 187 __GNUC__ && __alpha__ */
Simon Cooksey 0:fb7af294d5d9 188
Simon Cooksey 0:fb7af294d5d9 189 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Simon Cooksey 0:fb7af294d5d9 190 defined(__GNUC__) && defined(__ia64__)
Simon Cooksey 0:fb7af294d5d9 191
Simon Cooksey 0:fb7af294d5d9 192 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 193
Simon Cooksey 0:fb7af294d5d9 194 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 195 {
Simon Cooksey 0:fb7af294d5d9 196 unsigned long itc;
Simon Cooksey 0:fb7af294d5d9 197 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Simon Cooksey 0:fb7af294d5d9 198 return( itc );
Simon Cooksey 0:fb7af294d5d9 199 }
Simon Cooksey 0:fb7af294d5d9 200 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Simon Cooksey 0:fb7af294d5d9 201 __GNUC__ && __ia64__ */
Simon Cooksey 0:fb7af294d5d9 202
Simon Cooksey 0:fb7af294d5d9 203 #if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Simon Cooksey 0:fb7af294d5d9 204 !defined(EFIX64) && !defined(EFI32)
Simon Cooksey 0:fb7af294d5d9 205
Simon Cooksey 0:fb7af294d5d9 206 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 207
Simon Cooksey 0:fb7af294d5d9 208 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 209 {
Simon Cooksey 0:fb7af294d5d9 210 LARGE_INTEGER offset;
Simon Cooksey 0:fb7af294d5d9 211
Simon Cooksey 0:fb7af294d5d9 212 QueryPerformanceCounter( &offset );
Simon Cooksey 0:fb7af294d5d9 213
Simon Cooksey 0:fb7af294d5d9 214 return( (unsigned long)( offset.QuadPart ) );
Simon Cooksey 0:fb7af294d5d9 215 }
Simon Cooksey 0:fb7af294d5d9 216 #endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Simon Cooksey 0:fb7af294d5d9 217
Simon Cooksey 0:fb7af294d5d9 218 #if !defined(HAVE_HARDCLOCK)
Simon Cooksey 0:fb7af294d5d9 219
Simon Cooksey 0:fb7af294d5d9 220 #define HAVE_HARDCLOCK
Simon Cooksey 0:fb7af294d5d9 221
Simon Cooksey 0:fb7af294d5d9 222 static int hardclock_init = 0;
Simon Cooksey 0:fb7af294d5d9 223 static struct timeval tv_init;
Simon Cooksey 0:fb7af294d5d9 224
Simon Cooksey 0:fb7af294d5d9 225 unsigned long mbedtls_timing_hardclock( void )
Simon Cooksey 0:fb7af294d5d9 226 {
Simon Cooksey 0:fb7af294d5d9 227 struct timeval tv_cur;
Simon Cooksey 0:fb7af294d5d9 228
Simon Cooksey 0:fb7af294d5d9 229 if( hardclock_init == 0 )
Simon Cooksey 0:fb7af294d5d9 230 {
Simon Cooksey 0:fb7af294d5d9 231 gettimeofday( &tv_init, NULL );
Simon Cooksey 0:fb7af294d5d9 232 hardclock_init = 1;
Simon Cooksey 0:fb7af294d5d9 233 }
Simon Cooksey 0:fb7af294d5d9 234
Simon Cooksey 0:fb7af294d5d9 235 gettimeofday( &tv_cur, NULL );
Simon Cooksey 0:fb7af294d5d9 236 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
Simon Cooksey 0:fb7af294d5d9 237 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
Simon Cooksey 0:fb7af294d5d9 238 }
Simon Cooksey 0:fb7af294d5d9 239 #endif /* !HAVE_HARDCLOCK */
Simon Cooksey 0:fb7af294d5d9 240
Simon Cooksey 0:fb7af294d5d9 241 volatile int mbedtls_timing_alarmed = 0;
Simon Cooksey 0:fb7af294d5d9 242
Simon Cooksey 0:fb7af294d5d9 243 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Simon Cooksey 0:fb7af294d5d9 244
Simon Cooksey 0:fb7af294d5d9 245 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Simon Cooksey 0:fb7af294d5d9 246 {
Simon Cooksey 0:fb7af294d5d9 247 unsigned long delta;
Simon Cooksey 0:fb7af294d5d9 248 LARGE_INTEGER offset, hfreq;
Simon Cooksey 0:fb7af294d5d9 249 struct _hr_time *t = (struct _hr_time *) val;
Simon Cooksey 0:fb7af294d5d9 250
Simon Cooksey 0:fb7af294d5d9 251 QueryPerformanceCounter( &offset );
Simon Cooksey 0:fb7af294d5d9 252 QueryPerformanceFrequency( &hfreq );
Simon Cooksey 0:fb7af294d5d9 253
Simon Cooksey 0:fb7af294d5d9 254 delta = (unsigned long)( ( 1000 *
Simon Cooksey 0:fb7af294d5d9 255 ( offset.QuadPart - t->start.QuadPart ) ) /
Simon Cooksey 0:fb7af294d5d9 256 hfreq.QuadPart );
Simon Cooksey 0:fb7af294d5d9 257
Simon Cooksey 0:fb7af294d5d9 258 if( reset )
Simon Cooksey 0:fb7af294d5d9 259 QueryPerformanceCounter( &t->start );
Simon Cooksey 0:fb7af294d5d9 260
Simon Cooksey 0:fb7af294d5d9 261 return( delta );
Simon Cooksey 0:fb7af294d5d9 262 }
Simon Cooksey 0:fb7af294d5d9 263
Simon Cooksey 0:fb7af294d5d9 264 /* It's OK to use a global because alarm() is supposed to be global anyway */
Simon Cooksey 0:fb7af294d5d9 265 static DWORD alarmMs;
Simon Cooksey 0:fb7af294d5d9 266
Simon Cooksey 0:fb7af294d5d9 267 static DWORD WINAPI TimerProc( LPVOID TimerContext )
Simon Cooksey 0:fb7af294d5d9 268 {
Simon Cooksey 0:fb7af294d5d9 269 ((void) TimerContext);
Simon Cooksey 0:fb7af294d5d9 270 Sleep( alarmMs );
Simon Cooksey 0:fb7af294d5d9 271 mbedtls_timing_alarmed = 1;
Simon Cooksey 0:fb7af294d5d9 272 return( TRUE );
Simon Cooksey 0:fb7af294d5d9 273 }
Simon Cooksey 0:fb7af294d5d9 274
Simon Cooksey 0:fb7af294d5d9 275 void mbedtls_set_alarm( int seconds )
Simon Cooksey 0:fb7af294d5d9 276 {
Simon Cooksey 0:fb7af294d5d9 277 DWORD ThreadId;
Simon Cooksey 0:fb7af294d5d9 278
Simon Cooksey 0:fb7af294d5d9 279 mbedtls_timing_alarmed = 0;
Simon Cooksey 0:fb7af294d5d9 280 alarmMs = seconds * 1000;
Simon Cooksey 0:fb7af294d5d9 281 CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
Simon Cooksey 0:fb7af294d5d9 282 }
Simon Cooksey 0:fb7af294d5d9 283
Simon Cooksey 0:fb7af294d5d9 284 #else /* _WIN32 && !EFIX64 && !EFI32 */
Simon Cooksey 0:fb7af294d5d9 285
Simon Cooksey 0:fb7af294d5d9 286 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Simon Cooksey 0:fb7af294d5d9 287 {
Simon Cooksey 0:fb7af294d5d9 288 unsigned long delta;
Simon Cooksey 0:fb7af294d5d9 289 struct timeval offset;
Simon Cooksey 0:fb7af294d5d9 290 struct _hr_time *t = (struct _hr_time *) val;
Simon Cooksey 0:fb7af294d5d9 291
Simon Cooksey 0:fb7af294d5d9 292 gettimeofday( &offset, NULL );
Simon Cooksey 0:fb7af294d5d9 293
Simon Cooksey 0:fb7af294d5d9 294 if( reset )
Simon Cooksey 0:fb7af294d5d9 295 {
Simon Cooksey 0:fb7af294d5d9 296 t->start.tv_sec = offset.tv_sec;
Simon Cooksey 0:fb7af294d5d9 297 t->start.tv_usec = offset.tv_usec;
Simon Cooksey 0:fb7af294d5d9 298 return( 0 );
Simon Cooksey 0:fb7af294d5d9 299 }
Simon Cooksey 0:fb7af294d5d9 300
Simon Cooksey 0:fb7af294d5d9 301 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
Simon Cooksey 0:fb7af294d5d9 302 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
Simon Cooksey 0:fb7af294d5d9 303
Simon Cooksey 0:fb7af294d5d9 304 return( delta );
Simon Cooksey 0:fb7af294d5d9 305 }
Simon Cooksey 0:fb7af294d5d9 306
Simon Cooksey 0:fb7af294d5d9 307 static void sighandler( int signum )
Simon Cooksey 0:fb7af294d5d9 308 {
Simon Cooksey 0:fb7af294d5d9 309 mbedtls_timing_alarmed = 1;
Simon Cooksey 0:fb7af294d5d9 310 signal( signum, sighandler );
Simon Cooksey 0:fb7af294d5d9 311 }
Simon Cooksey 0:fb7af294d5d9 312
Simon Cooksey 0:fb7af294d5d9 313 void mbedtls_set_alarm( int seconds )
Simon Cooksey 0:fb7af294d5d9 314 {
Simon Cooksey 0:fb7af294d5d9 315 mbedtls_timing_alarmed = 0;
Simon Cooksey 0:fb7af294d5d9 316 signal( SIGALRM, sighandler );
Simon Cooksey 0:fb7af294d5d9 317 alarm( seconds );
Simon Cooksey 0:fb7af294d5d9 318 }
Simon Cooksey 0:fb7af294d5d9 319
Simon Cooksey 0:fb7af294d5d9 320 #endif /* _WIN32 && !EFIX64 && !EFI32 */
Simon Cooksey 0:fb7af294d5d9 321
Simon Cooksey 0:fb7af294d5d9 322 /*
Simon Cooksey 0:fb7af294d5d9 323 * Set delays to watch
Simon Cooksey 0:fb7af294d5d9 324 */
Simon Cooksey 0:fb7af294d5d9 325 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
Simon Cooksey 0:fb7af294d5d9 326 {
Simon Cooksey 0:fb7af294d5d9 327 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
Simon Cooksey 0:fb7af294d5d9 328
Simon Cooksey 0:fb7af294d5d9 329 ctx->int_ms = int_ms;
Simon Cooksey 0:fb7af294d5d9 330 ctx->fin_ms = fin_ms;
Simon Cooksey 0:fb7af294d5d9 331
Simon Cooksey 0:fb7af294d5d9 332 if( fin_ms != 0 )
Simon Cooksey 0:fb7af294d5d9 333 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
Simon Cooksey 0:fb7af294d5d9 334 }
Simon Cooksey 0:fb7af294d5d9 335
Simon Cooksey 0:fb7af294d5d9 336 /*
Simon Cooksey 0:fb7af294d5d9 337 * Get number of delays expired
Simon Cooksey 0:fb7af294d5d9 338 */
Simon Cooksey 0:fb7af294d5d9 339 int mbedtls_timing_get_delay( void *data )
Simon Cooksey 0:fb7af294d5d9 340 {
Simon Cooksey 0:fb7af294d5d9 341 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
Simon Cooksey 0:fb7af294d5d9 342 unsigned long elapsed_ms;
Simon Cooksey 0:fb7af294d5d9 343
Simon Cooksey 0:fb7af294d5d9 344 if( ctx->fin_ms == 0 )
Simon Cooksey 0:fb7af294d5d9 345 return( -1 );
Simon Cooksey 0:fb7af294d5d9 346
Simon Cooksey 0:fb7af294d5d9 347 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
Simon Cooksey 0:fb7af294d5d9 348
Simon Cooksey 0:fb7af294d5d9 349 if( elapsed_ms >= ctx->fin_ms )
Simon Cooksey 0:fb7af294d5d9 350 return( 2 );
Simon Cooksey 0:fb7af294d5d9 351
Simon Cooksey 0:fb7af294d5d9 352 if( elapsed_ms >= ctx->int_ms )
Simon Cooksey 0:fb7af294d5d9 353 return( 1 );
Simon Cooksey 0:fb7af294d5d9 354
Simon Cooksey 0:fb7af294d5d9 355 return( 0 );
Simon Cooksey 0:fb7af294d5d9 356 }
Simon Cooksey 0:fb7af294d5d9 357
Simon Cooksey 0:fb7af294d5d9 358 #endif /* !MBEDTLS_TIMING_ALT */
Simon Cooksey 0:fb7af294d5d9 359
Simon Cooksey 0:fb7af294d5d9 360 #if defined(MBEDTLS_SELF_TEST)
Simon Cooksey 0:fb7af294d5d9 361
Simon Cooksey 0:fb7af294d5d9 362 /*
Simon Cooksey 0:fb7af294d5d9 363 * Busy-waits for the given number of milliseconds.
Simon Cooksey 0:fb7af294d5d9 364 * Used for testing mbedtls_timing_hardclock.
Simon Cooksey 0:fb7af294d5d9 365 */
Simon Cooksey 0:fb7af294d5d9 366 static void busy_msleep( unsigned long msec )
Simon Cooksey 0:fb7af294d5d9 367 {
Simon Cooksey 0:fb7af294d5d9 368 struct mbedtls_timing_hr_time hires;
Simon Cooksey 0:fb7af294d5d9 369 unsigned long i = 0; /* for busy-waiting */
Simon Cooksey 0:fb7af294d5d9 370 volatile unsigned long j; /* to prevent optimisation */
Simon Cooksey 0:fb7af294d5d9 371
Simon Cooksey 0:fb7af294d5d9 372 (void) mbedtls_timing_get_timer( &hires, 1 );
Simon Cooksey 0:fb7af294d5d9 373
Simon Cooksey 0:fb7af294d5d9 374 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Simon Cooksey 0:fb7af294d5d9 375 i++;
Simon Cooksey 0:fb7af294d5d9 376
Simon Cooksey 0:fb7af294d5d9 377 j = i;
Simon Cooksey 0:fb7af294d5d9 378 (void) j;
Simon Cooksey 0:fb7af294d5d9 379 }
Simon Cooksey 0:fb7af294d5d9 380
Simon Cooksey 0:fb7af294d5d9 381 #define FAIL do \
Simon Cooksey 0:fb7af294d5d9 382 { \
Simon Cooksey 0:fb7af294d5d9 383 if( verbose != 0 ) \
Simon Cooksey 0:fb7af294d5d9 384 mbedtls_printf( "failed\n" ); \
Simon Cooksey 0:fb7af294d5d9 385 \
Simon Cooksey 0:fb7af294d5d9 386 return( 1 ); \
Simon Cooksey 0:fb7af294d5d9 387 } while( 0 )
Simon Cooksey 0:fb7af294d5d9 388
Simon Cooksey 0:fb7af294d5d9 389 /*
Simon Cooksey 0:fb7af294d5d9 390 * Checkup routine
Simon Cooksey 0:fb7af294d5d9 391 *
Simon Cooksey 0:fb7af294d5d9 392 * Warning: this is work in progress, some tests may not be reliable enough
Simon Cooksey 0:fb7af294d5d9 393 * yet! False positives may happen.
Simon Cooksey 0:fb7af294d5d9 394 */
Simon Cooksey 0:fb7af294d5d9 395 int mbedtls_timing_self_test( int verbose )
Simon Cooksey 0:fb7af294d5d9 396 {
Simon Cooksey 0:fb7af294d5d9 397 unsigned long cycles, ratio;
Simon Cooksey 0:fb7af294d5d9 398 unsigned long millisecs, secs;
Simon Cooksey 0:fb7af294d5d9 399 int hardfail;
Simon Cooksey 0:fb7af294d5d9 400 struct mbedtls_timing_hr_time hires;
Simon Cooksey 0:fb7af294d5d9 401 uint32_t a, b;
Simon Cooksey 0:fb7af294d5d9 402 mbedtls_timing_delay_context ctx;
Simon Cooksey 0:fb7af294d5d9 403
Simon Cooksey 0:fb7af294d5d9 404 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 405 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Simon Cooksey 0:fb7af294d5d9 406
Simon Cooksey 0:fb7af294d5d9 407
Simon Cooksey 0:fb7af294d5d9 408 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 409 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Simon Cooksey 0:fb7af294d5d9 410
Simon Cooksey 0:fb7af294d5d9 411 for( secs = 1; secs <= 3; secs++ )
Simon Cooksey 0:fb7af294d5d9 412 {
Simon Cooksey 0:fb7af294d5d9 413 (void) mbedtls_timing_get_timer( &hires, 1 );
Simon Cooksey 0:fb7af294d5d9 414
Simon Cooksey 0:fb7af294d5d9 415 mbedtls_set_alarm( (int) secs );
Simon Cooksey 0:fb7af294d5d9 416 while( !mbedtls_timing_alarmed )
Simon Cooksey 0:fb7af294d5d9 417 ;
Simon Cooksey 0:fb7af294d5d9 418
Simon Cooksey 0:fb7af294d5d9 419 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Simon Cooksey 0:fb7af294d5d9 420
Simon Cooksey 0:fb7af294d5d9 421 /* For some reason on Windows it looks like alarm has an extra delay
Simon Cooksey 0:fb7af294d5d9 422 * (maybe related to creating a new thread). Allow some room here. */
Simon Cooksey 0:fb7af294d5d9 423 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Simon Cooksey 0:fb7af294d5d9 424 {
Simon Cooksey 0:fb7af294d5d9 425 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 426 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 427
Simon Cooksey 0:fb7af294d5d9 428 return( 1 );
Simon Cooksey 0:fb7af294d5d9 429 }
Simon Cooksey 0:fb7af294d5d9 430 }
Simon Cooksey 0:fb7af294d5d9 431
Simon Cooksey 0:fb7af294d5d9 432 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 433 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 434
Simon Cooksey 0:fb7af294d5d9 435 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 436 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Simon Cooksey 0:fb7af294d5d9 437
Simon Cooksey 0:fb7af294d5d9 438 for( a = 200; a <= 400; a += 200 )
Simon Cooksey 0:fb7af294d5d9 439 {
Simon Cooksey 0:fb7af294d5d9 440 for( b = 200; b <= 400; b += 200 )
Simon Cooksey 0:fb7af294d5d9 441 {
Simon Cooksey 0:fb7af294d5d9 442 mbedtls_timing_set_delay( &ctx, a, a + b );
Simon Cooksey 0:fb7af294d5d9 443
Simon Cooksey 0:fb7af294d5d9 444 busy_msleep( a - a / 8 );
Simon Cooksey 0:fb7af294d5d9 445 if( mbedtls_timing_get_delay( &ctx ) != 0 )
Simon Cooksey 0:fb7af294d5d9 446 FAIL;
Simon Cooksey 0:fb7af294d5d9 447
Simon Cooksey 0:fb7af294d5d9 448 busy_msleep( a / 4 );
Simon Cooksey 0:fb7af294d5d9 449 if( mbedtls_timing_get_delay( &ctx ) != 1 )
Simon Cooksey 0:fb7af294d5d9 450 FAIL;
Simon Cooksey 0:fb7af294d5d9 451
Simon Cooksey 0:fb7af294d5d9 452 busy_msleep( b - a / 8 - b / 8 );
Simon Cooksey 0:fb7af294d5d9 453 if( mbedtls_timing_get_delay( &ctx ) != 1 )
Simon Cooksey 0:fb7af294d5d9 454 FAIL;
Simon Cooksey 0:fb7af294d5d9 455
Simon Cooksey 0:fb7af294d5d9 456 busy_msleep( b / 4 );
Simon Cooksey 0:fb7af294d5d9 457 if( mbedtls_timing_get_delay( &ctx ) != 2 )
Simon Cooksey 0:fb7af294d5d9 458 FAIL;
Simon Cooksey 0:fb7af294d5d9 459 }
Simon Cooksey 0:fb7af294d5d9 460 }
Simon Cooksey 0:fb7af294d5d9 461
Simon Cooksey 0:fb7af294d5d9 462 mbedtls_timing_set_delay( &ctx, 0, 0 );
Simon Cooksey 0:fb7af294d5d9 463 busy_msleep( 200 );
Simon Cooksey 0:fb7af294d5d9 464 if( mbedtls_timing_get_delay( &ctx ) != -1 )
Simon Cooksey 0:fb7af294d5d9 465 FAIL;
Simon Cooksey 0:fb7af294d5d9 466
Simon Cooksey 0:fb7af294d5d9 467 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 468 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 469
Simon Cooksey 0:fb7af294d5d9 470 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 471 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
Simon Cooksey 0:fb7af294d5d9 472
Simon Cooksey 0:fb7af294d5d9 473 /*
Simon Cooksey 0:fb7af294d5d9 474 * Allow one failure for possible counter wrapping.
Simon Cooksey 0:fb7af294d5d9 475 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
Simon Cooksey 0:fb7af294d5d9 476 * since the whole test is about 10ms, it shouldn't happen twice in a row.
Simon Cooksey 0:fb7af294d5d9 477 */
Simon Cooksey 0:fb7af294d5d9 478 hardfail = 0;
Simon Cooksey 0:fb7af294d5d9 479
Simon Cooksey 0:fb7af294d5d9 480 hard_test:
Simon Cooksey 0:fb7af294d5d9 481 if( hardfail > 1 )
Simon Cooksey 0:fb7af294d5d9 482 {
Simon Cooksey 0:fb7af294d5d9 483 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 484 mbedtls_printf( "failed (ignored)\n" );
Simon Cooksey 0:fb7af294d5d9 485
Simon Cooksey 0:fb7af294d5d9 486 goto hard_test_done;
Simon Cooksey 0:fb7af294d5d9 487 }
Simon Cooksey 0:fb7af294d5d9 488
Simon Cooksey 0:fb7af294d5d9 489 /* Get a reference ratio cycles/ms */
Simon Cooksey 0:fb7af294d5d9 490 millisecs = 1;
Simon Cooksey 0:fb7af294d5d9 491 cycles = mbedtls_timing_hardclock();
Simon Cooksey 0:fb7af294d5d9 492 busy_msleep( millisecs );
Simon Cooksey 0:fb7af294d5d9 493 cycles = mbedtls_timing_hardclock() - cycles;
Simon Cooksey 0:fb7af294d5d9 494 ratio = cycles / millisecs;
Simon Cooksey 0:fb7af294d5d9 495
Simon Cooksey 0:fb7af294d5d9 496 /* Check that the ratio is mostly constant */
Simon Cooksey 0:fb7af294d5d9 497 for( millisecs = 2; millisecs <= 4; millisecs++ )
Simon Cooksey 0:fb7af294d5d9 498 {
Simon Cooksey 0:fb7af294d5d9 499 cycles = mbedtls_timing_hardclock();
Simon Cooksey 0:fb7af294d5d9 500 busy_msleep( millisecs );
Simon Cooksey 0:fb7af294d5d9 501 cycles = mbedtls_timing_hardclock() - cycles;
Simon Cooksey 0:fb7af294d5d9 502
Simon Cooksey 0:fb7af294d5d9 503 /* Allow variation up to 20% */
Simon Cooksey 0:fb7af294d5d9 504 if( cycles / millisecs < ratio - ratio / 5 ||
Simon Cooksey 0:fb7af294d5d9 505 cycles / millisecs > ratio + ratio / 5 )
Simon Cooksey 0:fb7af294d5d9 506 {
Simon Cooksey 0:fb7af294d5d9 507 hardfail++;
Simon Cooksey 0:fb7af294d5d9 508 goto hard_test;
Simon Cooksey 0:fb7af294d5d9 509 }
Simon Cooksey 0:fb7af294d5d9 510 }
Simon Cooksey 0:fb7af294d5d9 511
Simon Cooksey 0:fb7af294d5d9 512 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 513 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 514
Simon Cooksey 0:fb7af294d5d9 515 hard_test_done:
Simon Cooksey 0:fb7af294d5d9 516
Simon Cooksey 0:fb7af294d5d9 517 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 518 mbedtls_printf( "\n" );
Simon Cooksey 0:fb7af294d5d9 519
Simon Cooksey 0:fb7af294d5d9 520 return( 0 );
Simon Cooksey 0:fb7af294d5d9 521 }
Simon Cooksey 0:fb7af294d5d9 522
Simon Cooksey 0:fb7af294d5d9 523 #endif /* MBEDTLS_SELF_TEST */
Simon Cooksey 0:fb7af294d5d9 524
Simon Cooksey 0:fb7af294d5d9 525 #endif /* MBEDTLS_TIMING_C */