BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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