bin2bn(): When len==0, just return a zero BIGNUM
[openssl.git] / crypto / threads_none.c
1 /*
2  * Copyright 2016-2021 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
13 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
14
15 # if defined(OPENSSL_SYS_UNIX)
16 #  include <sys/types.h>
17 #  include <unistd.h>
18 # endif
19
20 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
21 {
22     CRYPTO_RWLOCK *lock;
23
24     if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
25         /* Don't set error, to avoid recursion blowup. */
26         return NULL;
27
28     *(unsigned int *)lock = 1;
29
30     return lock;
31 }
32
33 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
34 {
35     if (!ossl_assert(*(unsigned int *)lock == 1))
36         return 0;
37     return 1;
38 }
39
40 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
41 {
42     if (!ossl_assert(*(unsigned int *)lock == 1))
43         return 0;
44     return 1;
45 }
46
47 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
48 {
49     if (!ossl_assert(*(unsigned int *)lock == 1))
50         return 0;
51     return 1;
52 }
53
54 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
55     if (lock == NULL)
56         return;
57
58     *(unsigned int *)lock = 0;
59     OPENSSL_free(lock);
60
61     return;
62 }
63
64 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
65 {
66     if (*once != 0)
67         return 1;
68
69     init();
70     *once = 1;
71
72     return 1;
73 }
74
75 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
76
77 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
78
79 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
80 {
81     static unsigned int thread_local_key = 0;
82
83     if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
84         return 0;
85
86     *key = thread_local_key++;
87
88     thread_local_storage[*key] = NULL;
89
90     return 1;
91 }
92
93 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
94 {
95     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
96         return NULL;
97
98     return thread_local_storage[*key];
99 }
100
101 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
102 {
103     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
104         return 0;
105
106     thread_local_storage[*key] = val;
107
108     return 1;
109 }
110
111 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
112 {
113     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
114     return 1;
115 }
116
117 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
118 {
119     return 0;
120 }
121
122 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
123 {
124     return (a == b);
125 }
126
127 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
128 {
129     *val += amount;
130     *ret  = *val;
131
132     return 1;
133 }
134
135 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
136                      CRYPTO_RWLOCK *lock)
137 {
138     *val |= op;
139     *ret  = *val;
140
141     return 1;
142 }
143
144 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
145 {
146     *ret  = *val;
147
148     return 1;
149 }
150
151 int openssl_init_fork_handlers(void)
152 {
153     return 0;
154 }
155
156 int openssl_get_fork_id(void)
157 {
158 # if defined(OPENSSL_SYS_UNIX)
159     return getpid();
160 # else
161     return 0;
162 # endif
163 }
164 #endif