xref: /freebsd/contrib/libfido2/src/time.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
1f540a430SEd Maste /*
2f540a430SEd Maste  * Copyright (c) 2021 Yubico AB. All rights reserved.
3f540a430SEd Maste  * Use of this source code is governed by a BSD-style
4f540a430SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
6f540a430SEd Maste  */
7f540a430SEd Maste 
8f540a430SEd Maste #include <errno.h>
9f540a430SEd Maste #include "fido.h"
10f540a430SEd Maste 
11f540a430SEd Maste static int
timespec_to_ms(const struct timespec * ts)12f540a430SEd Maste timespec_to_ms(const struct timespec *ts)
13f540a430SEd Maste {
14f540a430SEd Maste 	int64_t x, y;
15f540a430SEd Maste 
16f540a430SEd Maste 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 ||
17f540a430SEd Maste 	    ts->tv_nsec >= 1000000000LL)
18f540a430SEd Maste 		return -1;
19f540a430SEd Maste 
20f540a430SEd Maste 	if ((uint64_t)ts->tv_sec >= INT64_MAX / 1000LL)
21f540a430SEd Maste 		return -1;
22f540a430SEd Maste 
23f540a430SEd Maste 	x = ts->tv_sec * 1000LL;
24f540a430SEd Maste 	y = ts->tv_nsec / 1000000LL;
25f540a430SEd Maste 
26f540a430SEd Maste 	if (INT64_MAX - x < y || x + y > INT_MAX)
27f540a430SEd Maste 		return -1;
28f540a430SEd Maste 
29f540a430SEd Maste 	return (int)(x + y);
30f540a430SEd Maste }
31f540a430SEd Maste 
32f540a430SEd Maste int
fido_time_now(struct timespec * ts_now)33f540a430SEd Maste fido_time_now(struct timespec *ts_now)
34f540a430SEd Maste {
35f540a430SEd Maste 	if (clock_gettime(CLOCK_MONOTONIC, ts_now) != 0) {
36f540a430SEd Maste 		fido_log_error(errno, "%s: clock_gettime", __func__);
37f540a430SEd Maste 		return -1;
38f540a430SEd Maste 	}
39f540a430SEd Maste 
40f540a430SEd Maste 	return 0;
41f540a430SEd Maste }
42f540a430SEd Maste 
43f540a430SEd Maste int
fido_time_delta(const struct timespec * ts_start,int * ms_remain)44f540a430SEd Maste fido_time_delta(const struct timespec *ts_start, int *ms_remain)
45f540a430SEd Maste {
46f540a430SEd Maste 	struct timespec ts_end, ts_delta;
47f540a430SEd Maste 	int ms;
48f540a430SEd Maste 
49f540a430SEd Maste 	if (*ms_remain < 0)
50f540a430SEd Maste 		return 0;
51f540a430SEd Maste 
52f540a430SEd Maste 	if (clock_gettime(CLOCK_MONOTONIC, &ts_end) != 0) {
53f540a430SEd Maste 		fido_log_error(errno, "%s: clock_gettime", __func__);
54f540a430SEd Maste 		return -1;
55f540a430SEd Maste 	}
56f540a430SEd Maste 
57f540a430SEd Maste 	if (timespeccmp(&ts_end, ts_start, <)) {
58f540a430SEd Maste 		fido_log_debug("%s: timespeccmp", __func__);
59f540a430SEd Maste 		return -1;
60f540a430SEd Maste 	}
61f540a430SEd Maste 
62f540a430SEd Maste 	timespecsub(&ts_end, ts_start, &ts_delta);
63f540a430SEd Maste 
64f540a430SEd Maste 	if ((ms = timespec_to_ms(&ts_delta)) < 0) {
65f540a430SEd Maste 		fido_log_debug("%s: timespec_to_ms", __func__);
66f540a430SEd Maste 		return -1;
67f540a430SEd Maste 	}
68f540a430SEd Maste 
69f540a430SEd Maste 	if (ms > *ms_remain)
70f540a430SEd Maste 		ms = *ms_remain;
71f540a430SEd Maste 
72f540a430SEd Maste 	*ms_remain -= ms;
73f540a430SEd Maste 
74f540a430SEd Maste 	return 0;
75f540a430SEd Maste }
76