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