2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_INTERNAL_TIME_H
11 # define OSSL_INTERNAL_TIME_H
14 # include <openssl/e_os2.h> /* uint64_t */
15 # include "internal/e_os.h" /* for struct timeval */
16 # include "internal/safe_math.h"
19 * Internal type defining a time.
20 * This should be treated as an opaque structure.
22 * The time datum is Unix's 1970 and at nanosecond precision, this gives
23 * a range of 584 years roughly.
26 uint64_t t; /* Ticks since the epoch */
29 /* The precision of times allows this many values per second */
30 # define OSSL_TIME_SECOND ((uint64_t)1000000000)
32 /* One millisecond. */
33 # define OSSL_TIME_MS (OSSL_TIME_SECOND / 1000)
35 /* One microsecond. */
36 # define OSSL_TIME_US (OSSL_TIME_MS / 1000)
39 # define OSSL_TIME_NS (OSSL_TIME_US / 1000)
41 #define ossl_seconds2time(s) ossl_ticks2time((s) * OSSL_TIME_SECOND)
42 #define ossl_time2seconds(t) (ossl_time2ticks(t) / OSSL_TIME_SECOND)
43 #define ossl_ms2time(ms) ossl_ticks2time((ms) * OSSL_TIME_MS)
44 #define ossl_time2ms(t) (ossl_time2ticks(t) / OSSL_TIME_MS)
45 #define ossl_us2time(us) ossl_ticks2time((us) * OSSL_TIME_US)
46 #define ossl_time2us(t) (ossl_time2ticks(t) / OSSL_TIME_US)
49 * Arithmetic operations on times.
50 * These operations are saturating, in that an overflow or underflow returns
51 * the largest or smallest value respectively.
53 OSSL_SAFE_MATH_UNSIGNED(time, uint64_t)
55 /* Convert a tick count into a time */
56 static ossl_unused ossl_inline
57 OSSL_TIME ossl_ticks2time(uint64_t ticks)
65 /* Convert a time to a tick count */
66 static ossl_unused ossl_inline
67 uint64_t ossl_time2ticks(OSSL_TIME t)
72 /* Get current time */
73 OSSL_TIME ossl_time_now(void);
75 /* The beginning and end of the time range */
76 static ossl_unused ossl_inline
77 OSSL_TIME ossl_time_zero(void)
79 return ossl_ticks2time(0);
82 static ossl_unused ossl_inline
83 OSSL_TIME ossl_time_infinite(void)
85 return ossl_ticks2time(~(uint64_t)0);
89 /* Convert time to timeval */
90 static ossl_unused ossl_inline
91 struct timeval ossl_time_to_timeval(OSSL_TIME t)
97 * Round up any nano secs which struct timeval doesn't support. Ensures that
98 * we never return a zero time if the input time is non zero
100 t.t = safe_add_time(t.t, OSSL_TIME_US - 1, &err);
102 t = ossl_time_infinite();
105 tv.tv_sec = (long int)(t.t / OSSL_TIME_SECOND);
107 tv.tv_sec = (time_t)(t.t / OSSL_TIME_SECOND);
109 tv.tv_usec = (t.t % OSSL_TIME_SECOND) / OSSL_TIME_US;
113 /* Convert timeval to time */
114 static ossl_unused ossl_inline
115 OSSL_TIME ossl_time_from_timeval(struct timeval tv)
119 #ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
121 return ossl_time_zero();
123 t.t = tv.tv_sec * OSSL_TIME_SECOND + tv.tv_usec * OSSL_TIME_US;
127 /* Convert OSSL_TIME to time_t */
128 static ossl_unused ossl_inline
129 time_t ossl_time_to_time_t(OSSL_TIME t)
131 return (time_t)(t.t / OSSL_TIME_SECOND);
134 /* Convert time_t to OSSL_TIME */
135 static ossl_unused ossl_inline
136 OSSL_TIME ossl_time_from_time_t(time_t t)
141 ot.t *= OSSL_TIME_SECOND;
145 /* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
146 static ossl_unused ossl_inline
147 int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
156 /* Returns true if an OSSL_TIME is ossl_time_zero(). */
157 static ossl_unused ossl_inline
158 int ossl_time_is_zero(OSSL_TIME t)
160 return ossl_time_compare(t, ossl_time_zero()) == 0;
163 /* Returns true if an OSSL_TIME is ossl_time_infinite(). */
164 static ossl_unused ossl_inline
165 int ossl_time_is_infinite(OSSL_TIME t)
167 return ossl_time_compare(t, ossl_time_infinite()) == 0;
170 static ossl_unused ossl_inline
171 OSSL_TIME ossl_time_add(OSSL_TIME a, OSSL_TIME b)
176 r.t = safe_add_time(a.t, b.t, &err);
177 return err ? ossl_time_infinite() : r;
180 static ossl_unused ossl_inline
181 OSSL_TIME ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
186 r.t = safe_sub_time(a.t, b.t, &err);
187 return err ? ossl_time_zero() : r;
190 /* Returns |a - b|. */
191 static ossl_unused ossl_inline
192 OSSL_TIME ossl_time_abs_difference(OSSL_TIME a, OSSL_TIME b)
194 return a.t > b.t ? ossl_time_subtract(a, b)
195 : ossl_time_subtract(b, a);
198 static ossl_unused ossl_inline
199 OSSL_TIME ossl_time_multiply(OSSL_TIME a, uint64_t b)
204 r.t = safe_mul_time(a.t, b, &err);
205 return err ? ossl_time_infinite() : r;
208 static ossl_unused ossl_inline
209 OSSL_TIME ossl_time_divide(OSSL_TIME a, uint64_t b)
214 r.t = safe_div_time(a.t, b, &err);
215 return err ? ossl_time_zero() : r;
218 static ossl_unused ossl_inline
219 OSSL_TIME ossl_time_muldiv(OSSL_TIME a, uint64_t b, uint64_t c)
224 r.t = safe_muldiv_time(a.t, b, c, &err);
225 return err ? ossl_time_zero() : r;
228 /* Return higher of the two given time values. */
229 static ossl_unused ossl_inline
230 OSSL_TIME ossl_time_max(OSSL_TIME a, OSSL_TIME b)
232 return a.t > b.t ? a : b;
235 /* Return the lower of the two given time values. */
236 static ossl_unused ossl_inline
237 OSSL_TIME ossl_time_min(OSSL_TIME a, OSSL_TIME b)
239 return a.t < b.t ? a : b;