Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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