Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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