blob: 97f1963abaad236e74b5d5e55134b68e6e376015 [file] [log] [blame]
Simon Butcher86311432016-07-12 13:11:00 +01001/**
2 * \file platform_time.h
3 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +02004 * \brief Mbed TLS Platform time abstraction
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Simon Butcher86311432016-07-12 13:11:00 +01009 */
10#ifndef MBEDTLS_PLATFORM_TIME_H
11#define MBEDTLS_PLATFORM_TIME_H
12
Bence Szépkútic662b362021-05-27 11:25:03 +020013#include "mbedtls/build_info.h"
Simon Butcher86311432016-07-12 13:11:00 +010014
15#ifdef __cplusplus
16extern "C" {
17#endif
18
Simon Butcher86311432016-07-12 13:11:00 +010019/*
20 * The time_t datatype
21 */
22#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
23typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
24#else
25/* For time_t */
26#include <time.h>
27typedef time_t mbedtls_time_t;
28#endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
29
Jerry Yueba0ab52022-12-15 17:41:41 +080030#if defined(MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO)
31typedef MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO mbedtls_ms_time_t;
32#else
Jerry Yueb306842023-01-31 12:49:45 +080033#include <stdint.h>
Jerry Yu041c8c12023-02-03 13:15:09 +080034#include <inttypes.h>
Jerry Yueb306842023-01-31 12:49:45 +080035typedef int64_t mbedtls_ms_time_t;
Jerry Yueba0ab52022-12-15 17:41:41 +080036#endif /* MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO */
37
Jerry Yu38257492022-12-15 17:54:47 +080038/**
Jerry Yub1d30152023-01-31 12:48:07 +080039 * \brief Get time in milliseconds.
Jerry Yu38257492022-12-15 17:54:47 +080040 *
Jerry Yu67bf6772023-02-03 11:16:13 +080041 * \return Monotonically-increasing current time in milliseconds.
Jerry Yu38257492022-12-15 17:54:47 +080042 *
Jerry Yu67bf6772023-02-03 11:16:13 +080043 * \note Define MBEDTLS_PLATFORM_MS_TIME_ALT to be able to provide an
44 * alternative implementation
Jerry Yub1d30152023-01-31 12:48:07 +080045 *
Jerry Yu67bf6772023-02-03 11:16:13 +080046 * \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 Yub1d30152023-01-31 12:48:07 +080049 *
Jerry Yu38257492022-12-15 17:54:47 +080050 */
51mbedtls_ms_time_t mbedtls_ms_time(void);
52
Simon Butcher86311432016-07-12 13:11:00 +010053/*
54 * The function pointers for time
55 */
56#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010057extern mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *time);
Simon Butcher86311432016-07-12 13:11:00 +010058
59/**
60 * \brief Set your own time function pointer
61 *
62 * \param time_func the time function implementation
63 *
64 * \return 0
65 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *time));
Simon Butcher86311432016-07-12 13:11:00 +010067#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 Butcher86311432016-07-12 13:11:00 +010079#endif /* platform_time.h */