xref: /openssl/include/internal/time.h (revision 0022bc81)
1 /*
2  * Copyright 2022-2023 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
58 OSSL_TIME 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
ossl_time2ticks(OSSL_TIME t)68 uint64_t 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_zero(void)78 OSSL_TIME ossl_time_zero(void)
79 {
80     return ossl_ticks2time(0);
81 }
82 
83 static ossl_unused ossl_inline
ossl_time_infinite(void)84 OSSL_TIME ossl_time_infinite(void)
85 {
86     return ossl_ticks2time(~(uint64_t)0);
87 }
88 
89 
90 /* Convert time to timeval */
91 static ossl_unused ossl_inline
ossl_time_to_timeval(OSSL_TIME t)92 struct timeval ossl_time_to_timeval(OSSL_TIME t)
93 {
94     struct timeval tv;
95     int err = 0;
96 
97     /*
98      * Round up any nano secs which struct timeval doesn't support. Ensures that
99      * we never return a zero time if the input time is non zero
100      */
101     t.t = safe_add_time(t.t, OSSL_TIME_US - 1, &err);
102     if (err)
103         t = ossl_time_infinite();
104 
105 #ifdef _WIN32
106     tv.tv_sec = (long int)(t.t / OSSL_TIME_SECOND);
107 #else
108     tv.tv_sec = (time_t)(t.t / OSSL_TIME_SECOND);
109 #endif
110     tv.tv_usec = (t.t % OSSL_TIME_SECOND) / OSSL_TIME_US;
111     return tv;
112 }
113 
114 /* Convert timeval to time */
115 static ossl_unused ossl_inline
ossl_time_from_timeval(struct timeval tv)116 OSSL_TIME ossl_time_from_timeval(struct timeval tv)
117 {
118     OSSL_TIME t;
119 
120 #ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
121     if (tv.tv_sec < 0)
122         return ossl_time_zero();
123 #endif
124     t.t = tv.tv_sec * OSSL_TIME_SECOND + tv.tv_usec * OSSL_TIME_US;
125     return t;
126 }
127 
128 /* Convert OSSL_TIME to time_t */
129 static ossl_unused ossl_inline
ossl_time_to_time_t(OSSL_TIME t)130 time_t ossl_time_to_time_t(OSSL_TIME t)
131 {
132     return (time_t)(t.t / OSSL_TIME_SECOND);
133 }
134 
135 /* Convert time_t to OSSL_TIME */
136 static ossl_unused ossl_inline
ossl_time_from_time_t(time_t t)137 OSSL_TIME ossl_time_from_time_t(time_t t)
138 {
139     OSSL_TIME ot;
140 
141     ot.t = t;
142     ot.t *= OSSL_TIME_SECOND;
143     return ot;
144 }
145 
146 /* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
147 static ossl_unused ossl_inline
ossl_time_compare(OSSL_TIME a,OSSL_TIME b)148 int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
149 {
150     if (a.t > b.t)
151         return 1;
152     if (a.t < b.t)
153         return -1;
154     return 0;
155 }
156 
157 /* Returns true if an OSSL_TIME is ossl_time_zero(). */
158 static ossl_unused ossl_inline
ossl_time_is_zero(OSSL_TIME t)159 int ossl_time_is_zero(OSSL_TIME t)
160 {
161     return ossl_time_compare(t, ossl_time_zero()) == 0;
162 }
163 
164 /* Returns true if an OSSL_TIME is ossl_time_infinite(). */
165 static ossl_unused ossl_inline
ossl_time_is_infinite(OSSL_TIME t)166 int ossl_time_is_infinite(OSSL_TIME t)
167 {
168     return ossl_time_compare(t, ossl_time_infinite()) == 0;
169 }
170 
171 static ossl_unused ossl_inline
ossl_time_add(OSSL_TIME a,OSSL_TIME b)172 OSSL_TIME ossl_time_add(OSSL_TIME a, OSSL_TIME b)
173 {
174     OSSL_TIME r;
175     int err = 0;
176 
177     r.t = safe_add_time(a.t, b.t, &err);
178     return err ? ossl_time_infinite() : r;
179 }
180 
181 static ossl_unused ossl_inline
ossl_time_subtract(OSSL_TIME a,OSSL_TIME b)182 OSSL_TIME ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
183 {
184     OSSL_TIME r;
185     int err = 0;
186 
187     r.t = safe_sub_time(a.t, b.t, &err);
188     return err ? ossl_time_zero() : r;
189 }
190 
191 /* Returns |a - b|. */
192 static ossl_unused ossl_inline
ossl_time_abs_difference(OSSL_TIME a,OSSL_TIME b)193 OSSL_TIME ossl_time_abs_difference(OSSL_TIME a, OSSL_TIME b)
194 {
195     return a.t > b.t ? ossl_time_subtract(a, b)
196                      : ossl_time_subtract(b, a);
197 }
198 
199 static ossl_unused ossl_inline
ossl_time_multiply(OSSL_TIME a,uint64_t b)200 OSSL_TIME ossl_time_multiply(OSSL_TIME a, uint64_t b)
201 {
202     OSSL_TIME r;
203     int err = 0;
204 
205     r.t = safe_mul_time(a.t, b, &err);
206     return err ? ossl_time_infinite() : r;
207 }
208 
209 static ossl_unused ossl_inline
ossl_time_divide(OSSL_TIME a,uint64_t b)210 OSSL_TIME ossl_time_divide(OSSL_TIME a, uint64_t b)
211 {
212     OSSL_TIME r;
213     int err = 0;
214 
215     r.t = safe_div_time(a.t, b, &err);
216     return err ? ossl_time_zero() : r;
217 }
218 
219 static ossl_unused ossl_inline
ossl_time_muldiv(OSSL_TIME a,uint64_t b,uint64_t c)220 OSSL_TIME ossl_time_muldiv(OSSL_TIME a, uint64_t b, uint64_t c)
221 {
222     OSSL_TIME r;
223     int err = 0;
224 
225     r.t = safe_muldiv_time(a.t, b, c, &err);
226     return err ? ossl_time_zero() : r;
227 }
228 
229 /* Return higher of the two given time values. */
230 static ossl_unused ossl_inline
ossl_time_max(OSSL_TIME a,OSSL_TIME b)231 OSSL_TIME ossl_time_max(OSSL_TIME a, OSSL_TIME b)
232 {
233     return a.t > b.t ? a : b;
234 }
235 
236 /* Return the lower of the two given time values. */
237 static ossl_unused ossl_inline
ossl_time_min(OSSL_TIME a,OSSL_TIME b)238 OSSL_TIME ossl_time_min(OSSL_TIME a, OSSL_TIME b)
239 {
240     return a.t < b.t ? a : b;
241 }
242 
243 #endif
244