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