Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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