Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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