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