blob: 7964102e65744e02764a7a8702a10e724b23a249 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010024#else
25#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010027#endif
28
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020029#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020033#if !defined(MBEDTLS_TIMING_ALT)
34
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010035#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040036 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000037 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010038#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
39#endif
40
Paul Bakkerfa6a6202013-10-28 18:48:30 +010041#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000042
43#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030044#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000045
46struct _hr_time
47{
48 LARGE_INTEGER start;
49};
50
51#else
52
53#include <unistd.h>
54#include <sys/types.h>
55#include <sys/time.h>
56#include <signal.h>
57#include <time.h>
58
59struct _hr_time
60{
61 struct timeval start;
62};
63
Paul Bakker9af723c2014-05-01 13:03:14 +020064#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
Paul Bakker5121ce52009-01-03 21:22:43 +000072 struct _hr_time *t = (struct _hr_time *) val;
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +020075 {
Paul Bakker5121ce52009-01-03 21:22:43 +000076 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +020077 return( 0 );
78 }
79 else
80 {
81 unsigned long delta;
82 LARGE_INTEGER now, hfreq;
83 QueryPerformanceCounter( &now );
84 QueryPerformanceFrequency( &hfreq );
85 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
86 / hfreq.QuadPart );
87 return( delta );
88 }
Paul Bakker5121ce52009-01-03 21:22:43 +000089}
90
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010091#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
Paul Bakker5121ce52009-01-03 21:22:43 +000095 struct _hr_time *t = (struct _hr_time *) val;
96
Paul Bakker5121ce52009-01-03 21:22:43 +000097 if( reset )
98 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +020099 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200100 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200102 else
103 {
104 unsigned long delta;
105 struct timeval now;
106 gettimeofday( &now, NULL );
107 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
108 + ( now.tv_usec - t->start.tv_usec ) / 1000;
109 return( delta );
110 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
112
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100113#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200115/*
116 * Set delays to watch
117 */
118void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
119{
120 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
121
122 ctx->int_ms = int_ms;
123 ctx->fin_ms = fin_ms;
124
125 if( fin_ms != 0 )
126 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
127}
128
129/*
130 * Get number of delays expired
131 */
132int mbedtls_timing_get_delay( void *data )
133{
134 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
135 unsigned long elapsed_ms;
136
137 if( ctx->fin_ms == 0 )
138 return( -1 );
139
140 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
141
142 if( elapsed_ms >= ctx->fin_ms )
143 return( 2 );
144
145 if( elapsed_ms >= ctx->int_ms )
146 return( 1 );
147
148 return( 0 );
149}
150
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200151#endif /* !MBEDTLS_TIMING_ALT */
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100154
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200155#define FAIL do \
156 { \
157 if( verbose != 0 ) \
158 { \
159 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
160 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
161 cycles, ratio, millisecs, secs, hardfail, \
162 (unsigned long) a, (unsigned long) b ); \
Gilles Peskined41d59e2021-05-25 09:19:45 +0200163 print_timers( &hires, &ctx ); \
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200164 } \
165 return( 1 ); \
166 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100169
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200170#endif /* MBEDTLS_TIMING_C */