rand: remove unimplemented librandom stub code
[openssl.git] / crypto / threads_none.c
1 /*
2  * Copyright 2016-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/cryptlib.h"
12 #include "internal/rcu.h"
13 #include "rcu_internal.h"
14
15 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
16
17 # if defined(OPENSSL_SYS_UNIX)
18 #  include <sys/types.h>
19 #  include <unistd.h>
20 # endif
21
22 struct rcu_lock_st {
23     struct rcu_cb_item *cb_items;
24 };
25
26 CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
27 {
28     struct rcu_lock_st *lock;
29
30     lock = OPENSSL_zalloc(sizeof(*lock));
31     return lock;
32 }
33
34 void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
35 {
36     OPENSSL_free(lock);
37 }
38
39 void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
40 {
41     return;
42 }
43
44 void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
45 {
46     return;
47 }
48
49 void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
50 {
51     return;
52 }
53
54 void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
55 {
56     return;
57 }
58
59 void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
60 {
61     struct rcu_cb_item *items = lock->cb_items;
62     struct rcu_cb_item *tmp;
63
64     lock->cb_items = NULL;
65
66     while (items != NULL) {
67         tmp = items->next;
68         items->fn(items->data);
69         OPENSSL_free(items);
70         items = tmp;
71     }
72 }
73
74 int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
75 {
76     struct rcu_cb_item *new = OPENSSL_zalloc(sizeof(*new));
77
78     if (new == NULL)
79         return 0;
80
81     new->fn = cb;
82     new->data = data;
83     new->next = lock->cb_items;
84     lock->cb_items = new;
85     return 1;
86 }
87
88 void *ossl_rcu_uptr_deref(void **p)
89 {
90     return (void *)*p;
91 }
92
93 void ossl_rcu_assign_uptr(void **p, void **v)
94 {
95     *(void **)p = *(void **)v;
96 }
97
98 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
99 {
100     CRYPTO_RWLOCK *lock;
101
102     if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
103         /* Don't set error, to avoid recursion blowup. */
104         return NULL;
105
106     *(unsigned int *)lock = 1;
107
108     return lock;
109 }
110
111 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
112 {
113     if (!ossl_assert(*(unsigned int *)lock == 1))
114         return 0;
115     return 1;
116 }
117
118 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
119 {
120     if (!ossl_assert(*(unsigned int *)lock == 1))
121         return 0;
122     return 1;
123 }
124
125 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
126 {
127     if (!ossl_assert(*(unsigned int *)lock == 1))
128         return 0;
129     return 1;
130 }
131
132 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
133     if (lock == NULL)
134         return;
135
136     *(unsigned int *)lock = 0;
137     OPENSSL_free(lock);
138
139     return;
140 }
141
142 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
143 {
144     if (*once != 0)
145         return 1;
146
147     init();
148     *once = 1;
149
150     return 1;
151 }
152
153 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
154
155 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
156
157 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
158 {
159     static unsigned int thread_local_key = 0;
160
161     if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
162         return 0;
163
164     *key = thread_local_key++;
165
166     thread_local_storage[*key] = NULL;
167
168     return 1;
169 }
170
171 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
172 {
173     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
174         return NULL;
175
176     return thread_local_storage[*key];
177 }
178
179 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
180 {
181     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
182         return 0;
183
184     thread_local_storage[*key] = val;
185
186     return 1;
187 }
188
189 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
190 {
191     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
192     return 1;
193 }
194
195 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
196 {
197     return 0;
198 }
199
200 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
201 {
202     return (a == b);
203 }
204
205 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
206 {
207     *val += amount;
208     *ret  = *val;
209
210     return 1;
211 }
212
213 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
214                      CRYPTO_RWLOCK *lock)
215 {
216     *val |= op;
217     *ret  = *val;
218
219     return 1;
220 }
221
222 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
223 {
224     *ret  = *val;
225
226     return 1;
227 }
228
229 int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
230 {
231     *dst = val;
232
233     return 1;
234 }
235
236 int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
237 {
238     *ret = *val;
239
240     return 1;
241 }
242
243 int openssl_init_fork_handlers(void)
244 {
245     return 0;
246 }
247
248 int openssl_get_fork_id(void)
249 {
250 # if defined(OPENSSL_SYS_UNIX)
251     return getpid();
252 # else
253     return 0;
254 # endif
255 }
256 #endif