| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file platform_time.h | 
|  | 3 | * | 
|  | 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 | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 8 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 9 | * | 
|  | 10 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 11 | *  not use this file except in compliance with the License. | 
|  | 12 | *  You may obtain a copy of the License at | 
|  | 13 | * | 
|  | 14 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 15 | * | 
|  | 16 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 17 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 18 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 19 | *  See the License for the specific language governing permissions and | 
|  | 20 | *  limitations under the License. | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 21 | */ | 
|  | 22 | #ifndef MBEDTLS_PLATFORM_TIME_H | 
|  | 23 | #define MBEDTLS_PLATFORM_TIME_H | 
|  | 24 |  | 
| Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 25 | #include "mbedtls/build_info.h" | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 26 |  | 
|  | 27 | #ifdef __cplusplus | 
|  | 28 | extern "C" { | 
|  | 29 | #endif | 
|  | 30 |  | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 31 | /* | 
|  | 32 | * The time_t datatype | 
|  | 33 | */ | 
|  | 34 | #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) | 
|  | 35 | typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t; | 
|  | 36 | #else | 
|  | 37 | /* For time_t */ | 
|  | 38 | #include <time.h> | 
|  | 39 | typedef time_t mbedtls_time_t; | 
|  | 40 | #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */ | 
|  | 41 |  | 
| Jerry Yu | eba0ab5 | 2022-12-15 17:41:41 +0800 | [diff] [blame] | 42 | #if defined(MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO) | 
|  | 43 | typedef MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO mbedtls_ms_time_t; | 
|  | 44 | #else | 
| Jerry Yu | eb30684 | 2023-01-31 12:49:45 +0800 | [diff] [blame] | 45 | #include <stdint.h> | 
| Jerry Yu | 041c8c1 | 2023-02-03 13:15:09 +0800 | [diff] [blame] | 46 | #include <inttypes.h> | 
| Jerry Yu | eb30684 | 2023-01-31 12:49:45 +0800 | [diff] [blame] | 47 | typedef int64_t mbedtls_ms_time_t; | 
| Jerry Yu | eba0ab5 | 2022-12-15 17:41:41 +0800 | [diff] [blame] | 48 | #endif /* MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO */ | 
|  | 49 |  | 
| Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 50 | /** | 
| Jerry Yu | b1d3015 | 2023-01-31 12:48:07 +0800 | [diff] [blame] | 51 | * \brief   Get time in milliseconds. | 
| Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 52 | * | 
| Jerry Yu | 67bf677 | 2023-02-03 11:16:13 +0800 | [diff] [blame] | 53 | * \return Monotonically-increasing current time in milliseconds. | 
| Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 54 | * | 
| Jerry Yu | 67bf677 | 2023-02-03 11:16:13 +0800 | [diff] [blame] | 55 | * \note Define MBEDTLS_PLATFORM_MS_TIME_ALT to be able to provide an | 
|  | 56 | *       alternative implementation | 
| Jerry Yu | b1d3015 | 2023-01-31 12:48:07 +0800 | [diff] [blame] | 57 | * | 
| Jerry Yu | 67bf677 | 2023-02-03 11:16:13 +0800 | [diff] [blame] | 58 | * \warning This function returns a monotonically-increasing time value from a | 
|  | 59 | *          start time that will differ from platform to platform, and possibly | 
|  | 60 | *          from run to run of the process. | 
| Jerry Yu | b1d3015 | 2023-01-31 12:48:07 +0800 | [diff] [blame] | 61 | * | 
| Jerry Yu | 3825749 | 2022-12-15 17:54:47 +0800 | [diff] [blame] | 62 | */ | 
|  | 63 | mbedtls_ms_time_t mbedtls_ms_time(void); | 
|  | 64 |  | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 65 | /* | 
|  | 66 | * The function pointers for time | 
|  | 67 | */ | 
|  | 68 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | extern mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *time); | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 70 |  | 
|  | 71 | /** | 
|  | 72 | * \brief   Set your own time function pointer | 
|  | 73 | * | 
|  | 74 | * \param   time_func   the time function implementation | 
|  | 75 | * | 
|  | 76 | * \return              0 | 
|  | 77 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | 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] | 79 | #else | 
|  | 80 | #if defined(MBEDTLS_PLATFORM_TIME_MACRO) | 
|  | 81 | #define mbedtls_time    MBEDTLS_PLATFORM_TIME_MACRO | 
|  | 82 | #else | 
|  | 83 | #define mbedtls_time   time | 
|  | 84 | #endif /* MBEDTLS_PLATFORM_TIME_MACRO */ | 
|  | 85 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ | 
|  | 86 |  | 
|  | 87 | #ifdef __cplusplus | 
|  | 88 | } | 
|  | 89 | #endif | 
|  | 90 |  | 
| Simon Butcher | 8631143 | 2016-07-12 13:11:00 +0100 | [diff] [blame] | 91 | #endif /* platform_time.h */ |