Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file platform_time.h |
| 3 | * |
Gilles Peskine | e820c0a | 2023-08-03 17:45:20 +0200 | [diff] [blame] | 4 | * \brief Mbed TLS Platform time abstraction |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 9 | */ |
| 10 | #ifndef MBEDTLS_PLATFORM_TIME_H |
| 11 | #define MBEDTLS_PLATFORM_TIME_H |
| 12 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 13 | #include "mbedtls/build_info.h" |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 14 | |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 19 | /* |
| 20 | * The time_t datatype |
| 21 | */ |
| 22 | #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) |
| 23 | typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t; |
| 24 | #else |
| 25 | /* For time_t */ |
| 26 | #include <time.h> |
| 27 | typedef time_t mbedtls_time_t; |
| 28 | #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */ |
| 29 | |
Jerry Yu | eba0ab5 | 2022-12-15 17:41:41 +0800 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO) |
| 31 | typedef MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO mbedtls_ms_time_t; |
| 32 | #else |
Jerry Yu | eb30684 | 2023-01-31 12:49:45 +0800 | [diff] [blame] | 33 | #include <stdint.h> |
Jerry Yu | 041c8c1 | 2023-02-03 13:15:09 +0800 | [diff] [blame] | 34 | #include <inttypes.h> |
Jerry Yu | eb30684 | 2023-01-31 12:49:45 +0800 | [diff] [blame] | 35 | typedef int64_t mbedtls_ms_time_t; |
Jerry Yu | eba0ab5 | 2022-12-15 17:41:41 +0800 | [diff] [blame] | 36 | #endif /* MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO */ |
| 37 | |
Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 38 | /** |
Jerry Yu | b1d3015 | 2023-01-31 12:48:07 +0800 | [diff] [blame] | 39 | * \brief Get time in milliseconds. |
Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 40 | * |
Jerry Yu | 67bf677 | 2023-02-03 11:16:13 +0800 | [diff] [blame] | 41 | * \return Monotonically-increasing current time in milliseconds. |
Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 42 | * |
Jerry Yu | 67bf677 | 2023-02-03 11:16:13 +0800 | [diff] [blame] | 43 | * \note Define MBEDTLS_PLATFORM_MS_TIME_ALT to be able to provide an |
| 44 | * alternative implementation |
Jerry Yu | b1d3015 | 2023-01-31 12:48:07 +0800 | [diff] [blame] | 45 | * |
Jerry Yu | 67bf677 | 2023-02-03 11:16:13 +0800 | [diff] [blame] | 46 | * \warning This function returns a monotonically-increasing time value from a |
| 47 | * start time that will differ from platform to platform, and possibly |
| 48 | * from run to run of the process. |
Jerry Yu | b1d3015 | 2023-01-31 12:48:07 +0800 | [diff] [blame] | 49 | * |
Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 50 | */ |
| 51 | mbedtls_ms_time_t mbedtls_ms_time(void); |
| 52 | |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 53 | /* |
| 54 | * The function pointers for time |
| 55 | */ |
| 56 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | extern mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *time); |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * \brief Set your own time function pointer |
| 61 | * |
| 62 | * \param time_func the time function implementation |
| 63 | * |
| 64 | * \return 0 |
| 65 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *time)); |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 67 | #else |
| 68 | #if defined(MBEDTLS_PLATFORM_TIME_MACRO) |
| 69 | #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO |
| 70 | #else |
| 71 | #define mbedtls_time time |
| 72 | #endif /* MBEDTLS_PLATFORM_TIME_MACRO */ |
| 73 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ |
| 74 | |
| 75 | #ifdef __cplusplus |
| 76 | } |
| 77 | #endif |
| 78 | |
Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 79 | #endif /* platform_time.h */ |