| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  Portable interface to the CPU cycle counter | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-09-05 12:53:20 +02:00
										 |  |  |  *  Copyright The Mbed TLS Contributors | 
					
						
							| 
									
										
										
										
											2024-01-30 14:09:13 +01:00
										 |  |  |  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | #include "common.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_TIMING_C)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mbedtls/timing.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined(MBEDTLS_TIMING_ALT)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
 | 
					
						
							| 
									
										
										
										
											2018-07-28 11:16:41 +02:00
										 |  |  |     !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     !defined(__HAIKU__) && !defined(__midipix__) | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | #include <process.h>
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | struct _hr_time { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     LARGE_INTEGER start; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-18 14:48:00 +02:00
										 |  |  | /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
 | 
					
						
							|  |  |  |  * platform matches the ifdefs above, it will be used. */ | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-18 14:48:00 +02:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | struct _hr_time { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     struct timeval start; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | #endif /* _WIN32 && !EFIX64 && !EFI32 */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * \brief          Return the elapsed time in milliseconds | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \warning        May change without notice | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param val      points to a timer structure | 
					
						
							|  |  |  |  * \param reset    If 0, query the elapsed time. Otherwise (re)start the timer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \return         Elapsed time since the previous reset in ms. When | 
					
						
							|  |  |  |  *                 restarting, this is always 0. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note           To initialize a timer, call this function with reset=1. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *                 Determining the elapsed time and resetting the timer is not | 
					
						
							|  |  |  |  *                 atomic on all platforms, so after the sequence | 
					
						
							|  |  |  |  *                 `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 = | 
					
						
							|  |  |  |  *                 get_timer(0) }` the value time1+time2 is only approximately | 
					
						
							|  |  |  |  *                 the delay since the first reset. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     struct _hr_time *t = (struct _hr_time *) val; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (reset) { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |         QueryPerformanceCounter(&t->start); | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         unsigned long delta; | 
					
						
							|  |  |  |         LARGE_INTEGER now, hfreq; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         QueryPerformanceCounter(&now); | 
					
						
							|  |  |  |         QueryPerformanceFrequency(&hfreq); | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |         delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |                                  / hfreq.QuadPart); | 
					
						
							|  |  |  |         return delta; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else /* _WIN32 && !EFIX64 && !EFI32 */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     struct _hr_time *t = (struct _hr_time *) val; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (reset) { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |         gettimeofday(&t->start, NULL); | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         unsigned long delta; | 
					
						
							|  |  |  |         struct timeval now; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         gettimeofday(&now, NULL); | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |         delta = (now.tv_sec  - t->start.tv_sec) * 1000ul | 
					
						
							|  |  |  |                 + (now.tv_usec - t->start.tv_usec) / 1000; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         return delta; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* _WIN32 && !EFIX64 && !EFI32 */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Set delays to watch | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->int_ms = int_ms; | 
					
						
							|  |  |  |     ctx->fin_ms = fin_ms; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (fin_ms != 0) { | 
					
						
							|  |  |  |         (void) mbedtls_timing_get_timer(&ctx->timer, 1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Get number of delays expired | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_timing_get_delay(void *data) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; | 
					
						
							|  |  |  |     unsigned long elapsed_ms; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (ctx->fin_ms == 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (elapsed_ms >= ctx->fin_ms) { | 
					
						
							|  |  |  |         return 2; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (elapsed_ms >= ctx->int_ms) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |  * Get the final delay. | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | uint32_t mbedtls_timing_get_final_delay( | 
					
						
							|  |  |  |     const mbedtls_timing_delay_context *data) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     return data->fin_ms; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* !MBEDTLS_TIMING_ALT */
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif /* MBEDTLS_TIMING_C */
 |