Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /*
HannesTschofenig 0:796d0f61a05b 2 * Portable interface to the CPU cycle counter
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * Copyright (C) 2006-2014, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 8 *
HannesTschofenig 0:796d0f61a05b 9 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 12 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 13 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 14 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 15 *
HannesTschofenig 0:796d0f61a05b 16 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 19 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 20 *
HannesTschofenig 0:796d0f61a05b 21 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 22 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 24 */
HannesTschofenig 0:796d0f61a05b 25
HannesTschofenig 0:796d0f61a05b 26 #if !defined(POLARSSL_CONFIG_FILE)
HannesTschofenig 0:796d0f61a05b 27 #include "polarssl/config.h"
HannesTschofenig 0:796d0f61a05b 28 #else
HannesTschofenig 0:796d0f61a05b 29 #include POLARSSL_CONFIG_FILE
HannesTschofenig 0:796d0f61a05b 30 #endif
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_PLATFORM_C)
HannesTschofenig 0:796d0f61a05b 33 #include "polarssl/platform.h"
HannesTschofenig 0:796d0f61a05b 34 #else
HannesTschofenig 0:796d0f61a05b 35 #include <stdio.h>
HannesTschofenig 0:796d0f61a05b 36 #define polarssl_printf printf
HannesTschofenig 0:796d0f61a05b 37 #endif
HannesTschofenig 0:796d0f61a05b 38
HannesTschofenig 0:796d0f61a05b 39 #if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
HannesTschofenig 0:796d0f61a05b 40
HannesTschofenig 0:796d0f61a05b 41 #include "polarssl/timing.h"
HannesTschofenig 0:796d0f61a05b 42
HannesTschofenig 0:796d0f61a05b 43 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
HannesTschofenig 0:796d0f61a05b 44
HannesTschofenig 0:796d0f61a05b 45 #include <windows.h>
HannesTschofenig 0:796d0f61a05b 46 #include <winbase.h>
HannesTschofenig 0:796d0f61a05b 47
HannesTschofenig 0:796d0f61a05b 48 struct _hr_time
HannesTschofenig 0:796d0f61a05b 49 {
HannesTschofenig 0:796d0f61a05b 50 LARGE_INTEGER start;
HannesTschofenig 0:796d0f61a05b 51 };
HannesTschofenig 0:796d0f61a05b 52
HannesTschofenig 0:796d0f61a05b 53 #else
HannesTschofenig 0:796d0f61a05b 54
HannesTschofenig 0:796d0f61a05b 55 #include <unistd.h>
HannesTschofenig 0:796d0f61a05b 56 #include <sys/types.h>
HannesTschofenig 0:796d0f61a05b 57 #include <sys/time.h>
HannesTschofenig 0:796d0f61a05b 58 #include <signal.h>
HannesTschofenig 0:796d0f61a05b 59 #include <time.h>
HannesTschofenig 0:796d0f61a05b 60
HannesTschofenig 0:796d0f61a05b 61 struct _hr_time
HannesTschofenig 0:796d0f61a05b 62 {
HannesTschofenig 0:796d0f61a05b 63 struct timeval start;
HannesTschofenig 0:796d0f61a05b 64 };
HannesTschofenig 0:796d0f61a05b 65
HannesTschofenig 0:796d0f61a05b 66 #endif /* _WIN32 && !EFIX64 && !EFI32 */
HannesTschofenig 0:796d0f61a05b 67
HannesTschofenig 0:796d0f61a05b 68 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 69 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
HannesTschofenig 0:796d0f61a05b 70
HannesTschofenig 0:796d0f61a05b 71 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 72
HannesTschofenig 0:796d0f61a05b 73 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 74 {
HannesTschofenig 0:796d0f61a05b 75 unsigned long tsc;
HannesTschofenig 0:796d0f61a05b 76 __asm rdtsc
HannesTschofenig 0:796d0f61a05b 77 __asm mov [tsc], eax
HannesTschofenig 0:796d0f61a05b 78 return( tsc );
HannesTschofenig 0:796d0f61a05b 79 }
HannesTschofenig 0:796d0f61a05b 80 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 81 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
HannesTschofenig 0:796d0f61a05b 82
HannesTschofenig 0:796d0f61a05b 83 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 84 defined(__GNUC__) && defined(__i386__)
HannesTschofenig 0:796d0f61a05b 85
HannesTschofenig 0:796d0f61a05b 86 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 87
HannesTschofenig 0:796d0f61a05b 88 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 89 {
HannesTschofenig 0:796d0f61a05b 90 unsigned long lo, hi;
HannesTschofenig 0:796d0f61a05b 91 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
HannesTschofenig 0:796d0f61a05b 92 return( lo );
HannesTschofenig 0:796d0f61a05b 93 }
HannesTschofenig 0:796d0f61a05b 94 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 95 __GNUC__ && __i386__ */
HannesTschofenig 0:796d0f61a05b 96
HannesTschofenig 0:796d0f61a05b 97 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 98 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
HannesTschofenig 0:796d0f61a05b 99
HannesTschofenig 0:796d0f61a05b 100 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 101
HannesTschofenig 0:796d0f61a05b 102 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 103 {
HannesTschofenig 0:796d0f61a05b 104 unsigned long lo, hi;
HannesTschofenig 0:796d0f61a05b 105 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
HannesTschofenig 0:796d0f61a05b 106 return( lo | (hi << 32) );
HannesTschofenig 0:796d0f61a05b 107 }
HannesTschofenig 0:796d0f61a05b 108 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 109 __GNUC__ && ( __amd64__ || __x86_64__ ) */
HannesTschofenig 0:796d0f61a05b 110
HannesTschofenig 0:796d0f61a05b 111 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 112 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
HannesTschofenig 0:796d0f61a05b 113
HannesTschofenig 0:796d0f61a05b 114 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 115
HannesTschofenig 0:796d0f61a05b 116 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 117 {
HannesTschofenig 0:796d0f61a05b 118 unsigned long tbl, tbu0, tbu1;
HannesTschofenig 0:796d0f61a05b 119
HannesTschofenig 0:796d0f61a05b 120 do
HannesTschofenig 0:796d0f61a05b 121 {
HannesTschofenig 0:796d0f61a05b 122 asm volatile( "mftbu %0" : "=r" (tbu0) );
HannesTschofenig 0:796d0f61a05b 123 asm volatile( "mftb %0" : "=r" (tbl ) );
HannesTschofenig 0:796d0f61a05b 124 asm volatile( "mftbu %0" : "=r" (tbu1) );
HannesTschofenig 0:796d0f61a05b 125 }
HannesTschofenig 0:796d0f61a05b 126 while( tbu0 != tbu1 );
HannesTschofenig 0:796d0f61a05b 127
HannesTschofenig 0:796d0f61a05b 128 return( tbl );
HannesTschofenig 0:796d0f61a05b 129 }
HannesTschofenig 0:796d0f61a05b 130 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 131 __GNUC__ && ( __powerpc__ || __ppc__ ) */
HannesTschofenig 0:796d0f61a05b 132
HannesTschofenig 0:796d0f61a05b 133 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 134 defined(__GNUC__) && defined(__sparc64__)
HannesTschofenig 0:796d0f61a05b 135
HannesTschofenig 0:796d0f61a05b 136 #if defined(__OpenBSD__)
HannesTschofenig 0:796d0f61a05b 137 #warning OpenBSD does not allow access to tick register using software version instead
HannesTschofenig 0:796d0f61a05b 138 #else
HannesTschofenig 0:796d0f61a05b 139 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 140
HannesTschofenig 0:796d0f61a05b 141 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 142 {
HannesTschofenig 0:796d0f61a05b 143 unsigned long tick;
HannesTschofenig 0:796d0f61a05b 144 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
HannesTschofenig 0:796d0f61a05b 145 return( tick );
HannesTschofenig 0:796d0f61a05b 146 }
HannesTschofenig 0:796d0f61a05b 147 #endif /* __OpenBSD__ */
HannesTschofenig 0:796d0f61a05b 148 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 149 __GNUC__ && __sparc64__ */
HannesTschofenig 0:796d0f61a05b 150
HannesTschofenig 0:796d0f61a05b 151 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 152 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
HannesTschofenig 0:796d0f61a05b 153
HannesTschofenig 0:796d0f61a05b 154 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 155
HannesTschofenig 0:796d0f61a05b 156 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 157 {
HannesTschofenig 0:796d0f61a05b 158 unsigned long tick;
HannesTschofenig 0:796d0f61a05b 159 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
HannesTschofenig 0:796d0f61a05b 160 asm volatile( "mov %%g1, %0" : "=r" (tick) );
HannesTschofenig 0:796d0f61a05b 161 return( tick );
HannesTschofenig 0:796d0f61a05b 162 }
HannesTschofenig 0:796d0f61a05b 163 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 164 __GNUC__ && __sparc__ && !__sparc64__ */
HannesTschofenig 0:796d0f61a05b 165
HannesTschofenig 0:796d0f61a05b 166 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 167 defined(__GNUC__) && defined(__alpha__)
HannesTschofenig 0:796d0f61a05b 168
HannesTschofenig 0:796d0f61a05b 169 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 170
HannesTschofenig 0:796d0f61a05b 171 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 172 {
HannesTschofenig 0:796d0f61a05b 173 unsigned long cc;
HannesTschofenig 0:796d0f61a05b 174 asm volatile( "rpcc %0" : "=r" (cc) );
HannesTschofenig 0:796d0f61a05b 175 return( cc & 0xFFFFFFFF );
HannesTschofenig 0:796d0f61a05b 176 }
HannesTschofenig 0:796d0f61a05b 177 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 178 __GNUC__ && __alpha__ */
HannesTschofenig 0:796d0f61a05b 179
HannesTschofenig 0:796d0f61a05b 180 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
HannesTschofenig 0:796d0f61a05b 181 defined(__GNUC__) && defined(__ia64__)
HannesTschofenig 0:796d0f61a05b 182
HannesTschofenig 0:796d0f61a05b 183 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 184
HannesTschofenig 0:796d0f61a05b 185 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 186 {
HannesTschofenig 0:796d0f61a05b 187 unsigned long itc;
HannesTschofenig 0:796d0f61a05b 188 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
HannesTschofenig 0:796d0f61a05b 189 return( itc );
HannesTschofenig 0:796d0f61a05b 190 }
HannesTschofenig 0:796d0f61a05b 191 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
HannesTschofenig 0:796d0f61a05b 192 __GNUC__ && __ia64__ */
HannesTschofenig 0:796d0f61a05b 193
HannesTschofenig 0:796d0f61a05b 194 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
HannesTschofenig 0:796d0f61a05b 195 !defined(EFIX64) && !defined(EFI32)
HannesTschofenig 0:796d0f61a05b 196
HannesTschofenig 0:796d0f61a05b 197 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 198
HannesTschofenig 0:796d0f61a05b 199 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 200 {
HannesTschofenig 0:796d0f61a05b 201 LARGE_INTEGER offset;
HannesTschofenig 0:796d0f61a05b 202
HannesTschofenig 0:796d0f61a05b 203 QueryPerformanceCounter( &offset );
HannesTschofenig 0:796d0f61a05b 204
HannesTschofenig 0:796d0f61a05b 205 return (unsigned long)( offset.QuadPart );
HannesTschofenig 0:796d0f61a05b 206 }
HannesTschofenig 0:796d0f61a05b 207 #endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
HannesTschofenig 0:796d0f61a05b 208
HannesTschofenig 0:796d0f61a05b 209 #if !defined(POLARSSL_HAVE_HARDCLOCK)
HannesTschofenig 0:796d0f61a05b 210
HannesTschofenig 0:796d0f61a05b 211 #define POLARSSL_HAVE_HARDCLOCK
HannesTschofenig 0:796d0f61a05b 212
HannesTschofenig 0:796d0f61a05b 213 static int hardclock_init = 0;
HannesTschofenig 0:796d0f61a05b 214 static struct timeval tv_init;
HannesTschofenig 0:796d0f61a05b 215
HannesTschofenig 0:796d0f61a05b 216 unsigned long hardclock( void )
HannesTschofenig 0:796d0f61a05b 217 {
HannesTschofenig 0:796d0f61a05b 218 struct timeval tv_cur;
HannesTschofenig 0:796d0f61a05b 219
HannesTschofenig 0:796d0f61a05b 220 if( hardclock_init == 0 )
HannesTschofenig 0:796d0f61a05b 221 {
HannesTschofenig 0:796d0f61a05b 222 gettimeofday( &tv_init, NULL );
HannesTschofenig 0:796d0f61a05b 223 hardclock_init = 1;
HannesTschofenig 0:796d0f61a05b 224 }
HannesTschofenig 0:796d0f61a05b 225
HannesTschofenig 0:796d0f61a05b 226 gettimeofday( &tv_cur, NULL );
HannesTschofenig 0:796d0f61a05b 227 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
HannesTschofenig 0:796d0f61a05b 228 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
HannesTschofenig 0:796d0f61a05b 229 }
HannesTschofenig 0:796d0f61a05b 230 #endif /* !POLARSSL_HAVE_HARDCLOCK */
HannesTschofenig 0:796d0f61a05b 231
HannesTschofenig 0:796d0f61a05b 232 volatile int alarmed = 0;
HannesTschofenig 0:796d0f61a05b 233
HannesTschofenig 0:796d0f61a05b 234 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
HannesTschofenig 0:796d0f61a05b 235
HannesTschofenig 0:796d0f61a05b 236 unsigned long get_timer( struct hr_time *val, int reset )
HannesTschofenig 0:796d0f61a05b 237 {
HannesTschofenig 0:796d0f61a05b 238 unsigned long delta;
HannesTschofenig 0:796d0f61a05b 239 LARGE_INTEGER offset, hfreq;
HannesTschofenig 0:796d0f61a05b 240 struct _hr_time *t = (struct _hr_time *) val;
HannesTschofenig 0:796d0f61a05b 241
HannesTschofenig 0:796d0f61a05b 242 QueryPerformanceCounter( &offset );
HannesTschofenig 0:796d0f61a05b 243 QueryPerformanceFrequency( &hfreq );
HannesTschofenig 0:796d0f61a05b 244
HannesTschofenig 0:796d0f61a05b 245 delta = (unsigned long)( ( 1000 *
HannesTschofenig 0:796d0f61a05b 246 ( offset.QuadPart - t->start.QuadPart ) ) /
HannesTschofenig 0:796d0f61a05b 247 hfreq.QuadPart );
HannesTschofenig 0:796d0f61a05b 248
HannesTschofenig 0:796d0f61a05b 249 if( reset )
HannesTschofenig 0:796d0f61a05b 250 QueryPerformanceCounter( &t->start );
HannesTschofenig 0:796d0f61a05b 251
HannesTschofenig 0:796d0f61a05b 252 return( delta );
HannesTschofenig 0:796d0f61a05b 253 }
HannesTschofenig 0:796d0f61a05b 254
HannesTschofenig 0:796d0f61a05b 255 DWORD WINAPI TimerProc( LPVOID uElapse )
HannesTschofenig 0:796d0f61a05b 256 {
HannesTschofenig 0:796d0f61a05b 257 Sleep( (DWORD) uElapse );
HannesTschofenig 0:796d0f61a05b 258 alarmed = 1;
HannesTschofenig 0:796d0f61a05b 259 return( TRUE );
HannesTschofenig 0:796d0f61a05b 260 }
HannesTschofenig 0:796d0f61a05b 261
HannesTschofenig 0:796d0f61a05b 262 void set_alarm( int seconds )
HannesTschofenig 0:796d0f61a05b 263 {
HannesTschofenig 0:796d0f61a05b 264 DWORD ThreadId;
HannesTschofenig 0:796d0f61a05b 265
HannesTschofenig 0:796d0f61a05b 266 alarmed = 0;
HannesTschofenig 0:796d0f61a05b 267 CloseHandle( CreateThread( NULL, 0, TimerProc,
HannesTschofenig 0:796d0f61a05b 268 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
HannesTschofenig 0:796d0f61a05b 269 }
HannesTschofenig 0:796d0f61a05b 270
HannesTschofenig 0:796d0f61a05b 271 void m_sleep( int milliseconds )
HannesTschofenig 0:796d0f61a05b 272 {
HannesTschofenig 0:796d0f61a05b 273 Sleep( milliseconds );
HannesTschofenig 0:796d0f61a05b 274 }
HannesTschofenig 0:796d0f61a05b 275
HannesTschofenig 0:796d0f61a05b 276 #else /* _WIN32 && !EFIX64 && !EFI32 */
HannesTschofenig 0:796d0f61a05b 277
HannesTschofenig 0:796d0f61a05b 278 unsigned long get_timer( struct hr_time *val, int reset )
HannesTschofenig 0:796d0f61a05b 279 {
HannesTschofenig 0:796d0f61a05b 280 unsigned long delta;
HannesTschofenig 0:796d0f61a05b 281 struct timeval offset;
HannesTschofenig 0:796d0f61a05b 282 struct _hr_time *t = (struct _hr_time *) val;
HannesTschofenig 0:796d0f61a05b 283
HannesTschofenig 0:796d0f61a05b 284 gettimeofday( &offset, NULL );
HannesTschofenig 0:796d0f61a05b 285
HannesTschofenig 0:796d0f61a05b 286 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
HannesTschofenig 0:796d0f61a05b 287 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
HannesTschofenig 0:796d0f61a05b 288
HannesTschofenig 0:796d0f61a05b 289 if( reset )
HannesTschofenig 0:796d0f61a05b 290 {
HannesTschofenig 0:796d0f61a05b 291 t->start.tv_sec = offset.tv_sec;
HannesTschofenig 0:796d0f61a05b 292 t->start.tv_usec = offset.tv_usec;
HannesTschofenig 0:796d0f61a05b 293 }
HannesTschofenig 0:796d0f61a05b 294
HannesTschofenig 0:796d0f61a05b 295 return( delta );
HannesTschofenig 0:796d0f61a05b 296 }
HannesTschofenig 0:796d0f61a05b 297
HannesTschofenig 0:796d0f61a05b 298 #if defined(INTEGRITY)
HannesTschofenig 0:796d0f61a05b 299 void m_sleep( int milliseconds )
HannesTschofenig 0:796d0f61a05b 300 {
HannesTschofenig 0:796d0f61a05b 301 usleep( milliseconds * 1000 );
HannesTschofenig 0:796d0f61a05b 302 }
HannesTschofenig 0:796d0f61a05b 303
HannesTschofenig 0:796d0f61a05b 304 #else /* INTEGRITY */
HannesTschofenig 0:796d0f61a05b 305
HannesTschofenig 0:796d0f61a05b 306 static void sighandler( int signum )
HannesTschofenig 0:796d0f61a05b 307 {
HannesTschofenig 0:796d0f61a05b 308 alarmed = 1;
HannesTschofenig 0:796d0f61a05b 309 signal( signum, sighandler );
HannesTschofenig 0:796d0f61a05b 310 }
HannesTschofenig 0:796d0f61a05b 311
HannesTschofenig 0:796d0f61a05b 312 void set_alarm( int seconds )
HannesTschofenig 0:796d0f61a05b 313 {
HannesTschofenig 0:796d0f61a05b 314 alarmed = 0;
HannesTschofenig 0:796d0f61a05b 315 signal( SIGALRM, sighandler );
HannesTschofenig 0:796d0f61a05b 316 alarm( seconds );
HannesTschofenig 0:796d0f61a05b 317 }
HannesTschofenig 0:796d0f61a05b 318
HannesTschofenig 0:796d0f61a05b 319 void m_sleep( int milliseconds )
HannesTschofenig 0:796d0f61a05b 320 {
HannesTschofenig 0:796d0f61a05b 321 struct timeval tv;
HannesTschofenig 0:796d0f61a05b 322
HannesTschofenig 0:796d0f61a05b 323 tv.tv_sec = milliseconds / 1000;
HannesTschofenig 0:796d0f61a05b 324 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
HannesTschofenig 0:796d0f61a05b 325
HannesTschofenig 0:796d0f61a05b 326 select( 0, NULL, NULL, NULL, &tv );
HannesTschofenig 0:796d0f61a05b 327 }
HannesTschofenig 0:796d0f61a05b 328 #endif /* INTEGRITY */
HannesTschofenig 0:796d0f61a05b 329
HannesTschofenig 0:796d0f61a05b 330 #endif /* _WIN32 && !EFIX64 && !EFI32 */
HannesTschofenig 0:796d0f61a05b 331
HannesTschofenig 0:796d0f61a05b 332 #if defined(POLARSSL_SELF_TEST)
HannesTschofenig 0:796d0f61a05b 333
HannesTschofenig 0:796d0f61a05b 334 /* To test net_usleep against our functions */
HannesTschofenig 0:796d0f61a05b 335 #if defined(POLARSSL_NET_C)
HannesTschofenig 0:796d0f61a05b 336 #include "polarssl/net.h"
HannesTschofenig 0:796d0f61a05b 337 #endif
HannesTschofenig 0:796d0f61a05b 338
HannesTschofenig 0:796d0f61a05b 339 /*
HannesTschofenig 0:796d0f61a05b 340 * Checkup routine
HannesTschofenig 0:796d0f61a05b 341 *
HannesTschofenig 0:796d0f61a05b 342 * Warning: this is work in progress, some tests may not be reliable enough
HannesTschofenig 0:796d0f61a05b 343 * yet! False positives may happen.
HannesTschofenig 0:796d0f61a05b 344 */
HannesTschofenig 0:796d0f61a05b 345 int timing_self_test( int verbose )
HannesTschofenig 0:796d0f61a05b 346 {
HannesTschofenig 0:796d0f61a05b 347 unsigned long cycles, ratio;
HannesTschofenig 0:796d0f61a05b 348 unsigned long millisecs, secs;
HannesTschofenig 0:796d0f61a05b 349 int hardfail;
HannesTschofenig 0:796d0f61a05b 350 struct hr_time hires;
HannesTschofenig 0:796d0f61a05b 351
HannesTschofenig 0:796d0f61a05b 352 if( verbose != 0)
HannesTschofenig 0:796d0f61a05b 353 polarssl_printf( " TIMING tests warning: will take some time!\n" );
HannesTschofenig 0:796d0f61a05b 354
HannesTschofenig 0:796d0f61a05b 355 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 356 polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " );
HannesTschofenig 0:796d0f61a05b 357
HannesTschofenig 0:796d0f61a05b 358 for( secs = 1; secs <= 3; secs++ )
HannesTschofenig 0:796d0f61a05b 359 {
HannesTschofenig 0:796d0f61a05b 360 (void) get_timer( &hires, 1 );
HannesTschofenig 0:796d0f61a05b 361
HannesTschofenig 0:796d0f61a05b 362 m_sleep( 500 * secs );
HannesTschofenig 0:796d0f61a05b 363
HannesTschofenig 0:796d0f61a05b 364 millisecs = get_timer( &hires, 0 );
HannesTschofenig 0:796d0f61a05b 365
HannesTschofenig 0:796d0f61a05b 366 if( millisecs < 450 * secs || millisecs > 550 * secs )
HannesTschofenig 0:796d0f61a05b 367 {
HannesTschofenig 0:796d0f61a05b 368 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 369 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 370
HannesTschofenig 0:796d0f61a05b 371 return( 1 );
HannesTschofenig 0:796d0f61a05b 372 }
HannesTschofenig 0:796d0f61a05b 373 }
HannesTschofenig 0:796d0f61a05b 374
HannesTschofenig 0:796d0f61a05b 375 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 376 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 377
HannesTschofenig 0:796d0f61a05b 378 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 379 polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " );
HannesTschofenig 0:796d0f61a05b 380
HannesTschofenig 0:796d0f61a05b 381 for( secs = 1; secs <= 3; secs++ )
HannesTschofenig 0:796d0f61a05b 382 {
HannesTschofenig 0:796d0f61a05b 383 (void) get_timer( &hires, 1 );
HannesTschofenig 0:796d0f61a05b 384
HannesTschofenig 0:796d0f61a05b 385 set_alarm( secs );
HannesTschofenig 0:796d0f61a05b 386 while( !alarmed )
HannesTschofenig 0:796d0f61a05b 387 ;
HannesTschofenig 0:796d0f61a05b 388
HannesTschofenig 0:796d0f61a05b 389 millisecs = get_timer( &hires, 0 );
HannesTschofenig 0:796d0f61a05b 390
HannesTschofenig 0:796d0f61a05b 391 if( millisecs < 900 * secs || millisecs > 1100 * secs )
HannesTschofenig 0:796d0f61a05b 392 {
HannesTschofenig 0:796d0f61a05b 393 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 394 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 395
HannesTschofenig 0:796d0f61a05b 396 return( 1 );
HannesTschofenig 0:796d0f61a05b 397 }
HannesTschofenig 0:796d0f61a05b 398 }
HannesTschofenig 0:796d0f61a05b 399
HannesTschofenig 0:796d0f61a05b 400 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 401 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 402
HannesTschofenig 0:796d0f61a05b 403 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 404 polarssl_printf( " TIMING test #3 (hardclock / m_sleep ): " );
HannesTschofenig 0:796d0f61a05b 405
HannesTschofenig 0:796d0f61a05b 406 /*
HannesTschofenig 0:796d0f61a05b 407 * Allow one failure for possible counter wrapping.
HannesTschofenig 0:796d0f61a05b 408 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
HannesTschofenig 0:796d0f61a05b 409 * since the whole test is about 10ms, it shouldn't happen twice in a row.
HannesTschofenig 0:796d0f61a05b 410 */
HannesTschofenig 0:796d0f61a05b 411 hardfail = 0;
HannesTschofenig 0:796d0f61a05b 412
HannesTschofenig 0:796d0f61a05b 413 hard_test:
HannesTschofenig 0:796d0f61a05b 414 if( hardfail > 1 )
HannesTschofenig 0:796d0f61a05b 415 {
HannesTschofenig 0:796d0f61a05b 416 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 417 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 418
HannesTschofenig 0:796d0f61a05b 419 return( 1 );
HannesTschofenig 0:796d0f61a05b 420 }
HannesTschofenig 0:796d0f61a05b 421
HannesTschofenig 0:796d0f61a05b 422 /* Get a reference ratio cycles/ms */
HannesTschofenig 0:796d0f61a05b 423 cycles = hardclock();
HannesTschofenig 0:796d0f61a05b 424 m_sleep( 1 );
HannesTschofenig 0:796d0f61a05b 425 cycles = hardclock() - cycles;
HannesTschofenig 0:796d0f61a05b 426 ratio = cycles / 1;
HannesTschofenig 0:796d0f61a05b 427
HannesTschofenig 0:796d0f61a05b 428 for( millisecs = 2; millisecs <= 4; millisecs++ )
HannesTschofenig 0:796d0f61a05b 429 {
HannesTschofenig 0:796d0f61a05b 430 cycles = hardclock();
HannesTschofenig 0:796d0f61a05b 431 m_sleep( millisecs );
HannesTschofenig 0:796d0f61a05b 432 cycles = hardclock() - cycles;
HannesTschofenig 0:796d0f61a05b 433
HannesTschofenig 0:796d0f61a05b 434 /* Allow variation up to 20% */
HannesTschofenig 0:796d0f61a05b 435 if( cycles / millisecs < ratio - ratio / 5 ||
HannesTschofenig 0:796d0f61a05b 436 cycles / millisecs > ratio + ratio / 5 )
HannesTschofenig 0:796d0f61a05b 437 {
HannesTschofenig 0:796d0f61a05b 438 hardfail++;
HannesTschofenig 0:796d0f61a05b 439 goto hard_test;
HannesTschofenig 0:796d0f61a05b 440 }
HannesTschofenig 0:796d0f61a05b 441 }
HannesTschofenig 0:796d0f61a05b 442
HannesTschofenig 0:796d0f61a05b 443 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 444 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 445
HannesTschofenig 0:796d0f61a05b 446 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 447 polarssl_printf( "\n" );
HannesTschofenig 0:796d0f61a05b 448
HannesTschofenig 0:796d0f61a05b 449 #if defined(POLARSSL_NET_C)
HannesTschofenig 0:796d0f61a05b 450 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 451 polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " );
HannesTschofenig 0:796d0f61a05b 452
HannesTschofenig 0:796d0f61a05b 453 for( secs = 1; secs <= 3; secs++ )
HannesTschofenig 0:796d0f61a05b 454 {
HannesTschofenig 0:796d0f61a05b 455 (void) get_timer( &hires, 1 );
HannesTschofenig 0:796d0f61a05b 456
HannesTschofenig 0:796d0f61a05b 457 net_usleep( 500000 * secs );
HannesTschofenig 0:796d0f61a05b 458
HannesTschofenig 0:796d0f61a05b 459 millisecs = get_timer( &hires, 0 );
HannesTschofenig 0:796d0f61a05b 460
HannesTschofenig 0:796d0f61a05b 461 if( millisecs < 450 * secs || millisecs > 550 * secs )
HannesTschofenig 0:796d0f61a05b 462 {
HannesTschofenig 0:796d0f61a05b 463 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 464 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 465
HannesTschofenig 0:796d0f61a05b 466 return( 1 );
HannesTschofenig 0:796d0f61a05b 467 }
HannesTschofenig 0:796d0f61a05b 468 }
HannesTschofenig 0:796d0f61a05b 469
HannesTschofenig 0:796d0f61a05b 470 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 471 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 472 #endif /* POLARSSL_NET_C */
HannesTschofenig 0:796d0f61a05b 473
HannesTschofenig 0:796d0f61a05b 474 return( 0 );
HannesTschofenig 0:796d0f61a05b 475 }
HannesTschofenig 0:796d0f61a05b 476
HannesTschofenig 0:796d0f61a05b 477 #endif /* POLARSSL_SELF_TEST */
HannesTschofenig 0:796d0f61a05b 478
HannesTschofenig 0:796d0f61a05b 479 #endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */
HannesTschofenig 0:796d0f61a05b 480
HannesTschofenig 0:796d0f61a05b 481