blob: 246120ed34f20902017dfddd31df86c57f30c77e [file] [log] [blame]
Simon Butcher86311432016-07-12 13:11:00 +01001/**
2 * \file platform_time.h
3 *
4 * \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
Simon Butcher86311432016-07-12 13:11:00 +01008 * 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 Butcher86311432016-07-12 13:11:00 +010021 */
22#ifndef MBEDTLS_PLATFORM_TIME_H
23#define MBEDTLS_PLATFORM_TIME_H
24
Bence Szépkútic662b362021-05-27 11:25:03 +020025#include "mbedtls/build_info.h"
Simon Butcher86311432016-07-12 13:11:00 +010026
27#ifdef __cplusplus
28extern "C" {
29#endif
30
Simon Butcher86311432016-07-12 13:11:00 +010031/*
32 * The time_t datatype
33 */
34#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
35typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
36#else
37/* For time_t */
38#include <time.h>
39typedef time_t mbedtls_time_t;
40#endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
41
Jerry Yueba0ab52022-12-15 17:41:41 +080042#if defined(MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO)
43typedef MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO mbedtls_ms_time_t;
44#else
Jerry Yueb306842023-01-31 12:49:45 +080045#include <stdint.h>
46typedef int64_t mbedtls_ms_time_t;
Jerry Yueba0ab52022-12-15 17:41:41 +080047#endif /* MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO */
48
Jerry Yu38257492022-12-15 17:54:47 +080049/**
Jerry Yub1d30152023-01-31 12:48:07 +080050 * \brief Get time in milliseconds.
Jerry Yu38257492022-12-15 17:54:47 +080051 *
Jerry Yub1d30152023-01-31 12:48:07 +080052 * \return Current time in milliseconds which is monotonically increasing.
Jerry Yu38257492022-12-15 17:54:47 +080053 *
54 * \note If MBEDTLS_PLATFORM_MS_TIME_ALT defined, users can provide their own
55 * implementation.
Jerry Yub1d30152023-01-31 12:48:07 +080056 *
57 * \warning This function is used for time difference only. The start time is
58 * not defined. A well defined time function is not required in
59 * TLS negotiation.
60 *
Jerry Yu38257492022-12-15 17:54:47 +080061 */
62mbedtls_ms_time_t mbedtls_ms_time(void);
63
Simon Butcher86311432016-07-12 13:11:00 +010064/*
65 * The function pointers for time
66 */
67#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010068extern mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *time);
Simon Butcher86311432016-07-12 13:11:00 +010069
70/**
71 * \brief Set your own time function pointer
72 *
73 * \param time_func the time function implementation
74 *
75 * \return 0
76 */
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *time));
Simon Butcher86311432016-07-12 13:11:00 +010078#else
79#if defined(MBEDTLS_PLATFORM_TIME_MACRO)
80#define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO
81#else
82#define mbedtls_time time
83#endif /* MBEDTLS_PLATFORM_TIME_MACRO */
84#endif /* MBEDTLS_PLATFORM_TIME_ALT */
85
86#ifdef __cplusplus
87}
88#endif
89
Simon Butcher86311432016-07-12 13:11:00 +010090#endif /* platform_time.h */