1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Threading abstraction layer
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
5a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6a8e1175bSopenharmony_ci */
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ci/*
9a8e1175bSopenharmony_ci * Ensure gmtime_r is available even with -std=c99; must be defined before
10a8e1175bSopenharmony_ci * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms.
11a8e1175bSopenharmony_ci */
12a8e1175bSopenharmony_ci#if !defined(_POSIX_C_SOURCE)
13a8e1175bSopenharmony_ci#define _POSIX_C_SOURCE 200112L
14a8e1175bSopenharmony_ci#endif
15a8e1175bSopenharmony_ci
16a8e1175bSopenharmony_ci#include "common.h"
17a8e1175bSopenharmony_ci
18a8e1175bSopenharmony_ci#if defined(MBEDTLS_THREADING_C)
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci#include "mbedtls/threading.h"
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
23a8e1175bSopenharmony_ci
24a8e1175bSopenharmony_ci#if !defined(_WIN32) && (defined(unix) || \
25a8e1175bSopenharmony_ci    defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
26a8e1175bSopenharmony_ci    defined(__MACH__)))
27a8e1175bSopenharmony_ci#include <unistd.h>
28a8e1175bSopenharmony_ci#endif /* !_WIN32 && (unix || __unix || __unix__ ||
29a8e1175bSopenharmony_ci        * (__APPLE__ && __MACH__)) */
30a8e1175bSopenharmony_ci
31a8e1175bSopenharmony_ci#if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) ||     \
32a8e1175bSopenharmony_ci    (defined(_POSIX_THREAD_SAFE_FUNCTIONS) &&                     \
33a8e1175bSopenharmony_ci    _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
34a8e1175bSopenharmony_ci/*
35a8e1175bSopenharmony_ci * This is a convenience shorthand macro to avoid checking the long
36a8e1175bSopenharmony_ci * preprocessor conditions above. Ideally, we could expose this macro in
37a8e1175bSopenharmony_ci * platform_util.h and simply use it in platform_util.c, threading.c and
38a8e1175bSopenharmony_ci * threading.h. However, this macro is not part of the Mbed TLS public API, so
39a8e1175bSopenharmony_ci * we keep it private by only defining it in this file
40a8e1175bSopenharmony_ci */
41a8e1175bSopenharmony_ci
42a8e1175bSopenharmony_ci#if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
43a8e1175bSopenharmony_ci#define THREADING_USE_GMTIME
44a8e1175bSopenharmony_ci#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
47a8e1175bSopenharmony_ci             ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
48a8e1175bSopenharmony_ci                _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_ci#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
51a8e1175bSopenharmony_ci
52a8e1175bSopenharmony_ci#if defined(MBEDTLS_THREADING_PTHREAD)
53a8e1175bSopenharmony_cistatic void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
54a8e1175bSopenharmony_ci{
55a8e1175bSopenharmony_ci    if (mutex == NULL) {
56a8e1175bSopenharmony_ci        return;
57a8e1175bSopenharmony_ci    }
58a8e1175bSopenharmony_ci
59a8e1175bSopenharmony_ci    /* One problem here is that calling lock on a pthread mutex without first
60a8e1175bSopenharmony_ci     * having initialised it is undefined behaviour. Obviously we cannot check
61a8e1175bSopenharmony_ci     * this here in a thread safe manner without a significant performance
62a8e1175bSopenharmony_ci     * hit, so state transitions are checked in tests only via the state
63a8e1175bSopenharmony_ci     * variable. Please make sure any new mutex that gets added is exercised in
64a8e1175bSopenharmony_ci     * tests; see tests/src/threading_helpers.c for more details. */
65a8e1175bSopenharmony_ci    (void) pthread_mutex_init(&mutex->mutex, NULL);
66a8e1175bSopenharmony_ci}
67a8e1175bSopenharmony_ci
68a8e1175bSopenharmony_cistatic void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
69a8e1175bSopenharmony_ci{
70a8e1175bSopenharmony_ci    if (mutex == NULL) {
71a8e1175bSopenharmony_ci        return;
72a8e1175bSopenharmony_ci    }
73a8e1175bSopenharmony_ci
74a8e1175bSopenharmony_ci    (void) pthread_mutex_destroy(&mutex->mutex);
75a8e1175bSopenharmony_ci}
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_cistatic int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
78a8e1175bSopenharmony_ci{
79a8e1175bSopenharmony_ci    if (mutex == NULL) {
80a8e1175bSopenharmony_ci        return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
81a8e1175bSopenharmony_ci    }
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ci    if (pthread_mutex_lock(&mutex->mutex) != 0) {
84a8e1175bSopenharmony_ci        return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
85a8e1175bSopenharmony_ci    }
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci    return 0;
88a8e1175bSopenharmony_ci}
89a8e1175bSopenharmony_ci
90a8e1175bSopenharmony_cistatic int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
91a8e1175bSopenharmony_ci{
92a8e1175bSopenharmony_ci    if (mutex == NULL) {
93a8e1175bSopenharmony_ci        return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
94a8e1175bSopenharmony_ci    }
95a8e1175bSopenharmony_ci
96a8e1175bSopenharmony_ci    if (pthread_mutex_unlock(&mutex->mutex) != 0) {
97a8e1175bSopenharmony_ci        return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
98a8e1175bSopenharmony_ci    }
99a8e1175bSopenharmony_ci
100a8e1175bSopenharmony_ci    return 0;
101a8e1175bSopenharmony_ci}
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_civoid (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
104a8e1175bSopenharmony_civoid (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
105a8e1175bSopenharmony_ciint (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
106a8e1175bSopenharmony_ciint (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
107a8e1175bSopenharmony_ci
108a8e1175bSopenharmony_ci/*
109a8e1175bSopenharmony_ci * With pthreads we can statically initialize mutexes
110a8e1175bSopenharmony_ci */
111a8e1175bSopenharmony_ci#define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci#endif /* MBEDTLS_THREADING_PTHREAD */
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci#if defined(MBEDTLS_THREADING_ALT)
116a8e1175bSopenharmony_cistatic int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
117a8e1175bSopenharmony_ci{
118a8e1175bSopenharmony_ci    ((void) mutex);
119a8e1175bSopenharmony_ci    return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
120a8e1175bSopenharmony_ci}
121a8e1175bSopenharmony_cistatic void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
122a8e1175bSopenharmony_ci{
123a8e1175bSopenharmony_ci    ((void) mutex);
124a8e1175bSopenharmony_ci    return;
125a8e1175bSopenharmony_ci}
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_civoid (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
128a8e1175bSopenharmony_civoid (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
129a8e1175bSopenharmony_ciint (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
130a8e1175bSopenharmony_ciint (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ci/*
133a8e1175bSopenharmony_ci * Set functions pointers and initialize global mutexes
134a8e1175bSopenharmony_ci */
135a8e1175bSopenharmony_civoid mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
136a8e1175bSopenharmony_ci                               void (*mutex_free)(mbedtls_threading_mutex_t *),
137a8e1175bSopenharmony_ci                               int (*mutex_lock)(mbedtls_threading_mutex_t *),
138a8e1175bSopenharmony_ci                               int (*mutex_unlock)(mbedtls_threading_mutex_t *))
139a8e1175bSopenharmony_ci{
140a8e1175bSopenharmony_ci    mbedtls_mutex_init = mutex_init;
141a8e1175bSopenharmony_ci    mbedtls_mutex_free = mutex_free;
142a8e1175bSopenharmony_ci    mbedtls_mutex_lock = mutex_lock;
143a8e1175bSopenharmony_ci    mbedtls_mutex_unlock = mutex_unlock;
144a8e1175bSopenharmony_ci
145a8e1175bSopenharmony_ci#if defined(MBEDTLS_FS_IO)
146a8e1175bSopenharmony_ci    mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
147a8e1175bSopenharmony_ci#endif
148a8e1175bSopenharmony_ci#if defined(THREADING_USE_GMTIME)
149a8e1175bSopenharmony_ci    mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
150a8e1175bSopenharmony_ci#endif
151a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C)
152a8e1175bSopenharmony_ci    mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex);
153a8e1175bSopenharmony_ci    mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex);
154a8e1175bSopenharmony_ci    mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex);
155a8e1175bSopenharmony_ci#endif
156a8e1175bSopenharmony_ci}
157a8e1175bSopenharmony_ci
158a8e1175bSopenharmony_ci/*
159a8e1175bSopenharmony_ci * Free global mutexes
160a8e1175bSopenharmony_ci */
161a8e1175bSopenharmony_civoid mbedtls_threading_free_alt(void)
162a8e1175bSopenharmony_ci{
163a8e1175bSopenharmony_ci#if defined(MBEDTLS_FS_IO)
164a8e1175bSopenharmony_ci    mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
165a8e1175bSopenharmony_ci#endif
166a8e1175bSopenharmony_ci#if defined(THREADING_USE_GMTIME)
167a8e1175bSopenharmony_ci    mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
168a8e1175bSopenharmony_ci#endif
169a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C)
170a8e1175bSopenharmony_ci    mbedtls_mutex_free(&mbedtls_threading_key_slot_mutex);
171a8e1175bSopenharmony_ci    mbedtls_mutex_free(&mbedtls_threading_psa_globaldata_mutex);
172a8e1175bSopenharmony_ci    mbedtls_mutex_free(&mbedtls_threading_psa_rngdata_mutex);
173a8e1175bSopenharmony_ci#endif
174a8e1175bSopenharmony_ci}
175a8e1175bSopenharmony_ci#endif /* MBEDTLS_THREADING_ALT */
176a8e1175bSopenharmony_ci
177a8e1175bSopenharmony_ci/*
178a8e1175bSopenharmony_ci * Define global mutexes
179a8e1175bSopenharmony_ci */
180a8e1175bSopenharmony_ci#ifndef MUTEX_INIT
181a8e1175bSopenharmony_ci#define MUTEX_INIT
182a8e1175bSopenharmony_ci#endif
183a8e1175bSopenharmony_ci#if defined(MBEDTLS_FS_IO)
184a8e1175bSopenharmony_cimbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
185a8e1175bSopenharmony_ci#endif
186a8e1175bSopenharmony_ci#if defined(THREADING_USE_GMTIME)
187a8e1175bSopenharmony_cimbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
188a8e1175bSopenharmony_ci#endif
189a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C)
190a8e1175bSopenharmony_cimbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex MUTEX_INIT;
191a8e1175bSopenharmony_cimbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex MUTEX_INIT;
192a8e1175bSopenharmony_cimbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex MUTEX_INIT;
193a8e1175bSopenharmony_ci#endif
194a8e1175bSopenharmony_ci
195a8e1175bSopenharmony_ci#endif /* MBEDTLS_THREADING_C */
196