mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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