xref: /freebsd/crypto/openssl/include/internal/time.h (revision 10a428653ee7216475f1ddce3fb4cbf1200319f8)
1 /*
2  * Copyright 2022-2026 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 #ifndef OSSL_INTERNAL_TIME_H
11 #define OSSL_INTERNAL_TIME_H
12 #pragma once
13 
14 #include <openssl/e_os2.h> /* uint64_t */
15 #include "internal/e_os.h"
16 #include "internal/e_winsock.h" /* for struct timeval */
17 #include "internal/safe_math.h"
18 
19 /*
20  * Internal type defining a time.
21  * This should be treated as an opaque structure.
22  *
23  * The time datum is Unix's 1970 and at nanosecond precision, this gives
24  * a range of 584 years roughly.
25  */
26 typedef struct {
27     uint64_t t; /* Ticks since the epoch */
28 } OSSL_TIME;
29 
30 /* The precision of times allows this many values per second */
31 #define OSSL_TIME_SECOND ((uint64_t)1000000000)
32 
33 /* One millisecond. */
34 #define OSSL_TIME_MS (OSSL_TIME_SECOND / 1000)
35 
36 /* One microsecond. */
37 #define OSSL_TIME_US (OSSL_TIME_MS / 1000)
38 
39 /* One nanosecond. */
40 #define OSSL_TIME_NS (OSSL_TIME_US / 1000)
41 
42 #define ossl_seconds2time(s) ossl_ticks2time((s) * OSSL_TIME_SECOND)
43 #define ossl_time2seconds(t) (ossl_time2ticks(t) / OSSL_TIME_SECOND)
44 #define ossl_ms2time(ms) ossl_ticks2time((ms) * OSSL_TIME_MS)
45 #define ossl_time2ms(t) (ossl_time2ticks(t) / OSSL_TIME_MS)
46 #define ossl_us2time(us) ossl_ticks2time((us) * OSSL_TIME_US)
47 #define ossl_time2us(t) (ossl_time2ticks(t) / OSSL_TIME_US)
48 
49 /*
50  * Arithmetic operations on times.
51  * These operations are saturating, in that an overflow or underflow returns
52  * the largest or smallest value respectively.
53  */
OSSL_SAFE_MATH_UNSIGNED(time,uint64_t)54 OSSL_SAFE_MATH_UNSIGNED(time, uint64_t)
55 
56 /* Convert a tick count into a time */
57 static ossl_unused ossl_inline OSSL_TIME
58 ossl_ticks2time(uint64_t ticks)
59 {
60     OSSL_TIME r;
61 
62     r.t = ticks;
63     return r;
64 }
65 
66 /* Convert a time to a tick count */
67 static ossl_unused ossl_inline uint64_t
ossl_time2ticks(OSSL_TIME t)68 ossl_time2ticks(OSSL_TIME t)
69 {
70     return t.t;
71 }
72 
73 /* Get current time */
74 OSSL_TIME ossl_time_now(void);
75 
76 /* The beginning and end of the time range */
77 static ossl_unused ossl_inline OSSL_TIME
ossl_time_zero(void)78 ossl_time_zero(void)
79 {
80     return ossl_ticks2time(0);
81 }
82 
83 static ossl_unused ossl_inline OSSL_TIME
ossl_time_infinite(void)84 ossl_time_infinite(void)
85 {
86     return ossl_ticks2time(~(uint64_t)0);
87 }
88 
89 /* Convert time to timeval */
ossl_time_to_timeval(OSSL_TIME t)90 static ossl_unused ossl_inline struct timeval ossl_time_to_timeval(OSSL_TIME t)
91 {
92     struct timeval tv;
93     int err = 0;
94 
95     /*
96      * Round up any nano secs which struct timeval doesn't support. Ensures that
97      * we never return a zero time if the input time is non zero
98      */
99     t.t = safe_add_time(t.t, OSSL_TIME_US - 1, &err);
100     if (err)
101         t = ossl_time_infinite();
102 
103 #ifdef _WIN32
104     tv.tv_sec = (long int)(t.t / OSSL_TIME_SECOND);
105 #else
106     tv.tv_sec = (time_t)(t.t / OSSL_TIME_SECOND);
107 #endif
108     tv.tv_usec = (t.t % OSSL_TIME_SECOND) / OSSL_TIME_US;
109     return tv;
110 }
111 
112 /* Convert timeval to time */
113 static ossl_unused ossl_inline OSSL_TIME
ossl_time_from_timeval(struct timeval tv)114 ossl_time_from_timeval(struct timeval tv)
115 {
116     OSSL_TIME t;
117 
118 #ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
119     if (tv.tv_sec < 0)
120         return ossl_time_zero();
121 #endif
122     t.t = tv.tv_sec * OSSL_TIME_SECOND + tv.tv_usec * OSSL_TIME_US;
123     return t;
124 }
125 
126 /* Convert OSSL_TIME to time_t */
127 static ossl_unused ossl_inline
128     time_t
ossl_time_to_time_t(OSSL_TIME t)129     ossl_time_to_time_t(OSSL_TIME t)
130 {
131     return (time_t)(t.t / OSSL_TIME_SECOND);
132 }
133 
134 /* Convert time_t to OSSL_TIME */
135 static ossl_unused ossl_inline OSSL_TIME
ossl_time_from_time_t(time_t t)136 ossl_time_from_time_t(time_t t)
137 {
138     OSSL_TIME ot;
139 
140     ot.t = t;
141     ot.t *= OSSL_TIME_SECOND;
142     return ot;
143 }
144 
145 /* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
ossl_time_compare(OSSL_TIME a,OSSL_TIME b)146 static ossl_unused ossl_inline int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
147 {
148     if (a.t > b.t)
149         return 1;
150     if (a.t < b.t)
151         return -1;
152     return 0;
153 }
154 
155 /* Returns true if an OSSL_TIME is ossl_time_zero(). */
ossl_time_is_zero(OSSL_TIME t)156 static ossl_unused ossl_inline int ossl_time_is_zero(OSSL_TIME t)
157 {
158     return ossl_time_compare(t, ossl_time_zero()) == 0;
159 }
160 
161 /* Returns true if an OSSL_TIME is ossl_time_infinite(). */
ossl_time_is_infinite(OSSL_TIME t)162 static ossl_unused ossl_inline int ossl_time_is_infinite(OSSL_TIME t)
163 {
164     return ossl_time_compare(t, ossl_time_infinite()) == 0;
165 }
166 
167 static ossl_unused ossl_inline OSSL_TIME
ossl_time_add(OSSL_TIME a,OSSL_TIME b)168 ossl_time_add(OSSL_TIME a, OSSL_TIME b)
169 {
170     OSSL_TIME r;
171     int err = 0;
172 
173     r.t = safe_add_time(a.t, b.t, &err);
174     return err ? ossl_time_infinite() : r;
175 }
176 
177 static ossl_unused ossl_inline OSSL_TIME
ossl_time_subtract(OSSL_TIME a,OSSL_TIME b)178 ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
179 {
180     OSSL_TIME r;
181     int err = 0;
182 
183     r.t = safe_sub_time(a.t, b.t, &err);
184     return err ? ossl_time_zero() : r;
185 }
186 
187 /* Returns |a - b|. */
188 static ossl_unused ossl_inline OSSL_TIME
ossl_time_abs_difference(OSSL_TIME a,OSSL_TIME b)189 ossl_time_abs_difference(OSSL_TIME a, OSSL_TIME b)
190 {
191     return a.t > b.t ? ossl_time_subtract(a, b)
192                      : ossl_time_subtract(b, a);
193 }
194 
195 static ossl_unused ossl_inline OSSL_TIME
ossl_time_multiply(OSSL_TIME a,uint64_t b)196 ossl_time_multiply(OSSL_TIME a, uint64_t b)
197 {
198     OSSL_TIME r;
199     int err = 0;
200 
201     r.t = safe_mul_time(a.t, b, &err);
202     return err ? ossl_time_infinite() : r;
203 }
204 
205 static ossl_unused ossl_inline OSSL_TIME
ossl_time_divide(OSSL_TIME a,uint64_t b)206 ossl_time_divide(OSSL_TIME a, uint64_t b)
207 {
208     OSSL_TIME r;
209     int err = 0;
210 
211     r.t = safe_div_time(a.t, b, &err);
212     return err ? ossl_time_zero() : r;
213 }
214 
215 static ossl_unused ossl_inline OSSL_TIME
ossl_time_muldiv(OSSL_TIME a,uint64_t b,uint64_t c)216 ossl_time_muldiv(OSSL_TIME a, uint64_t b, uint64_t c)
217 {
218     OSSL_TIME r;
219     int err = 0;
220 
221     r.t = safe_muldiv_time(a.t, b, c, &err);
222     return err ? ossl_time_zero() : r;
223 }
224 
225 /* Return higher of the two given time values. */
226 static ossl_unused ossl_inline OSSL_TIME
ossl_time_max(OSSL_TIME a,OSSL_TIME b)227 ossl_time_max(OSSL_TIME a, OSSL_TIME b)
228 {
229     return a.t > b.t ? a : b;
230 }
231 
232 /* Return the lower of the two given time values. */
233 static ossl_unused ossl_inline OSSL_TIME
ossl_time_min(OSSL_TIME a,OSSL_TIME b)234 ossl_time_min(OSSL_TIME a, OSSL_TIME b)
235 {
236     return a.t < b.t ? a : b;
237 }
238 
239 #endif
240