/src/openssl33/crypto/sleep.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2022-2024 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 | | #include <openssl/crypto.h> |
11 | | #include "internal/e_os.h" |
12 | | #include "internal/time.h" |
13 | | |
14 | | /* system-specific variants defining OSSL_sleep() */ |
15 | | #if (defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)) \ |
16 | | && !defined(OPENSSL_USE_SLEEP_BUSYLOOP) |
17 | | # include <unistd.h> |
18 | | |
19 | | static void ossl_sleep_millis(uint64_t millis) |
20 | 0 | { |
21 | | # ifdef OPENSSL_SYS_VXWORKS |
22 | | struct timespec ts; |
23 | | |
24 | | ts.tv_sec = (long int) (millis / 1000); |
25 | | ts.tv_nsec = (long int) (millis % 1000) * 1000000ul; |
26 | | nanosleep(&ts, NULL); |
27 | | # elif defined(__TANDEM) && !defined(_REENTRANT) |
28 | | # include <cextdecs.h(PROCESS_DELAY_)> |
29 | | |
30 | | /* HPNS does not support usleep for non threaded apps */ |
31 | | PROCESS_DELAY_(millis * 1000); |
32 | | # else |
33 | 0 | unsigned int s = (unsigned int)(millis / 1000); |
34 | 0 | unsigned int us = (unsigned int)((millis % 1000) * 1000); |
35 | |
|
36 | 0 | if (s > 0) |
37 | 0 | sleep(s); |
38 | 0 | usleep(us); |
39 | 0 | # endif |
40 | 0 | } |
41 | | #elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI) |
42 | | # include <windows.h> |
43 | | |
44 | | static void ossl_sleep_millis(uint64_t millis) |
45 | | { |
46 | | /* |
47 | | * Windows' Sleep() takes a DWORD argument, which is smaller than |
48 | | * a uint64_t, so we need to limit it to 49 days, which should be enough. |
49 | | */ |
50 | | DWORD limited_millis = (DWORD)-1; |
51 | | |
52 | | if (millis < limited_millis) |
53 | | limited_millis = (DWORD)millis; |
54 | | Sleep(limited_millis); |
55 | | } |
56 | | |
57 | | #else |
58 | | /* Fallback to a busy wait */ |
59 | | # define USE_SLEEP_SECS |
60 | | |
61 | | static void ossl_sleep_secs(uint64_t secs) |
62 | | { |
63 | | /* |
64 | | * sleep() takes an unsigned int argument, which is smaller than |
65 | | * a uint64_t, so it needs to be limited to 136 years which |
66 | | * should be enough even for Sleeping Beauty. |
67 | | */ |
68 | | unsigned int limited_secs = UINT_MAX; |
69 | | |
70 | | if (secs < limited_secs) |
71 | | limited_secs = (unsigned int)secs; |
72 | | sleep(limited_secs); |
73 | | } |
74 | | |
75 | | static void ossl_sleep_millis(uint64_t millis) |
76 | | { |
77 | | const OSSL_TIME finish |
78 | | = ossl_time_add(ossl_time_now(), ossl_ms2time(millis)); |
79 | | |
80 | | while (ossl_time_compare(ossl_time_now(), finish) < 0) |
81 | | /* busy wait */ ; |
82 | | } |
83 | | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */ |
84 | | |
85 | | void OSSL_sleep(uint64_t millis) |
86 | 0 | { |
87 | 0 | OSSL_TIME now = ossl_time_now(); |
88 | 0 | OSSL_TIME finish = ossl_time_add(now, ossl_ms2time(millis)); |
89 | 0 | uint64_t left = millis; |
90 | |
|
91 | | #if defined(USE_SLEEP_SECS) |
92 | | do { |
93 | | ossl_sleep_secs(left / 1000); |
94 | | now = ossl_time_now(); |
95 | | left = ossl_time2ms(ossl_time_subtract(finish, now)); |
96 | | } while (ossl_time_compare(now, finish) < 0 && left > 1000); |
97 | | |
98 | | if (ossl_time_compare(now, finish) >= 0) |
99 | | return; |
100 | | #endif |
101 | |
|
102 | 0 | do { |
103 | 0 | ossl_sleep_millis(left); |
104 | 0 | now = ossl_time_now(); |
105 | 0 | left = ossl_time2ms(ossl_time_subtract(finish, now)); |
106 | 0 | } while (ossl_time_compare(now, finish) < 0); |
107 | 0 | } |