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