SCA hardening for mod. field inversion in EC_GROUP
[openssl.git] / crypto / threads_win.c
1 /*
2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #if defined(_WIN32)
11 # include <windows.h>
12 #endif
13
14 #include <openssl/crypto.h>
15
16 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
17
18 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
19 {
20     CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION));
21     if (lock == NULL)
22         return NULL;
23
24     /* 0x400 is the spin count value suggested in the documentation */
25     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
26         OPENSSL_free(lock);
27         return NULL;
28     }
29
30     return lock;
31 }
32
33 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
34 {
35     EnterCriticalSection(lock);
36     return 1;
37 }
38
39 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
40 {
41     EnterCriticalSection(lock);
42     return 1;
43 }
44
45 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
46 {
47     LeaveCriticalSection(lock);
48     return 1;
49 }
50
51 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
52 {
53     if (lock == NULL)
54         return;
55
56     DeleteCriticalSection(lock);
57     OPENSSL_free(lock);
58
59     return;
60 }
61
62 #  define ONCE_UNINITED     0
63 #  define ONCE_ININIT       1
64 #  define ONCE_DONE         2
65
66 /*
67  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
68  * we still have to support.
69  */
70 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
71 {
72     LONG volatile *lock = (LONG *)once;
73     LONG result;
74
75     if (*lock == ONCE_DONE)
76         return 1;
77
78     do {
79         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
80         if (result == ONCE_UNINITED) {
81             init();
82             *lock = ONCE_DONE;
83             return 1;
84         }
85     } while (result == ONCE_ININIT);
86
87     return (*lock == ONCE_DONE);
88 }
89
90 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
91 {
92     *key = TlsAlloc();
93     if (*key == TLS_OUT_OF_INDEXES)
94         return 0;
95
96     return 1;
97 }
98
99 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
100 {
101     DWORD last_error;
102     void *ret;
103
104     /*
105      * TlsGetValue clears the last error even on success, so that callers may
106      * distinguish it successfully returning NULL or failing. It is documented
107      * to never fail if the argument is a valid index from TlsAlloc, so we do
108      * not need to handle this.
109      *
110      * However, this error-mangling behavior interferes with the caller's use of
111      * GetLastError. In particular SSL_get_error queries the error queue to
112      * determine whether the caller should look at the OS's errors. To avoid
113      * destroying state, save and restore the Windows error.
114      *
115      * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
116      */
117     last_error = GetLastError();
118     ret = TlsGetValue(*key);
119     SetLastError(last_error);
120     return ret;
121 }
122
123 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
124 {
125     if (TlsSetValue(*key, val) == 0)
126         return 0;
127
128     return 1;
129 }
130
131 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
132 {
133     if (TlsFree(*key) == 0)
134         return 0;
135
136     return 1;
137 }
138
139 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
140 {
141     return GetCurrentThreadId();
142 }
143
144 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
145 {
146     return (a == b);
147 }
148
149 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
150 {
151     *ret = InterlockedExchangeAdd(val, amount) + amount;
152     return 1;
153 }
154
155 #endif