blob: d87d8b6cab47d9d3abf2fec40cf18cc3205dd826 [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é-Gonnard8903fe02015-05-12 19:30:45 +020022#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020026#if !defined(MBEDTLS_TIMING_ALT)
27
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010028#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040029 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000030 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010031#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
32#endif
33
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030037#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39struct _hr_time
40{
41 LARGE_INTEGER start;
42};
43
44#else
45
46#include <unistd.h>
47#include <sys/types.h>
48#include <sys/time.h>
49#include <signal.h>
50#include <time.h>
51
52struct _hr_time
53{
54 struct timeval start;
55};
56
Paul Bakker9af723c2014-05-01 13:03:14 +020057#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +000064{
Paul Bakker5121ce52009-01-03 21:22:43 +000065 struct _hr_time *t = (struct _hr_time *) val;
66
Paul Bakker5121ce52009-01-03 21:22:43 +000067 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +020068 {
Paul Bakker5121ce52009-01-03 21:22:43 +000069 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +020070 return( 0 );
71 }
72 else
73 {
74 unsigned long delta;
75 LARGE_INTEGER now, hfreq;
76 QueryPerformanceCounter( &now );
77 QueryPerformanceFrequency( &hfreq );
78 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
79 / hfreq.QuadPart );
80 return( delta );
81 }
Paul Bakker5121ce52009-01-03 21:22:43 +000082}
83
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010084#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Paul Bakker5121ce52009-01-03 21:22:43 +000088 struct _hr_time *t = (struct _hr_time *) val;
89
Paul Bakker5121ce52009-01-03 21:22:43 +000090 if( reset )
91 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +020092 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +020093 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000094 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +020095 else
96 {
97 unsigned long delta;
98 struct timeval now;
99 gettimeofday( &now, NULL );
100 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
101 + ( now.tv_usec - t->start.tv_usec ) / 1000;
102 return( delta );
103 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000104}
105
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100106#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200108/*
109 * Set delays to watch
110 */
111void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
112{
113 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
114
115 ctx->int_ms = int_ms;
116 ctx->fin_ms = fin_ms;
117
118 if( fin_ms != 0 )
119 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
120}
121
122/*
123 * Get number of delays expired
124 */
125int mbedtls_timing_get_delay( void *data )
126{
127 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
128 unsigned long elapsed_ms;
129
130 if( ctx->fin_ms == 0 )
131 return( -1 );
132
133 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
134
135 if( elapsed_ms >= ctx->fin_ms )
136 return( 2 );
137
138 if( elapsed_ms >= ctx->int_ms )
139 return( 1 );
140
141 return( 0 );
142}
143
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200144#endif /* !MBEDTLS_TIMING_ALT */
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200145#endif /* MBEDTLS_TIMING_C */