Skip to content

Commit c8a23a5

Browse files
committed
Adopt C thread-locals
Motivation In preparation for adopting FoundationEssentials where available, we need to avoid using Thread as that isn't available. To that end, let's migrate to using C thread-locals Modifications Migrate to C thread-locals Result No need for Thread anymore.
1 parent d1c6b70 commit c8a23a5

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

NOTICE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This product contains test vectors from Google's wycheproof project.
3434

3535
---
3636

37-
This product contains a derivation of various scripts from SwiftNIO.
37+
This product contains a derivation of various files from SwiftNIO.
3838

3939
* LICENSE (Apache License 2.0):
4040
* https://www.apache.org/licenses/LICENSE-2.0

Sources/CCryptoBoringSSLShims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
add_library(CCryptoBoringSSLShims STATIC
1616
"shims.c"
17+
"threadlocal.c"
1718
)
1819

1920
target_include_directories(CCryptoBoringSSLShims PUBLIC

Sources/CCryptoBoringSSLShims/include/CCryptoBoringSSLShims.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftCrypto open source project
44
//
5-
// Copyright (c) 2019 Apple Inc. and the SwiftCrypto project authors
5+
// Copyright (c) 2019-2025 Apple Inc. and the SwiftCrypto project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -148,6 +148,36 @@ size_t CCryptoBoringSSLShims_EC_POINT_point2oct(const EC_GROUP *group,
148148
void *buf, size_t max_out,
149149
BN_CTX *ctx);
150150

151+
// MARK: Thread locals
152+
153+
extern _Thread_local void *_CCryptoBoringSSLShims_p256_ffac_ptr;
154+
extern _Thread_local void *_CCryptoBoringSSLShims_p384_ffac_ptr;
155+
extern _Thread_local void *_CCryptoBoringSSLShims_p521_ffac_ptr;
156+
157+
static inline void * _Nullable CCryptoBoringSSLShims_get_p256_ffac_ptr(void) {
158+
return _CCryptoBoringSSLShims_p256_ffac_ptr;
159+
}
160+
161+
static inline void CCryptoBoringSSLShims_set_p256_ffac_ptr(void * _Nonnull ptr) {
162+
_CCryptoBoringSSLShims_p256_ffac_ptr = ptr;
163+
}
164+
165+
static inline void * _Nullable CCryptoBoringSSLShims_get_p384_ffac_ptr(void) {
166+
return _CCryptoBoringSSLShims_p384_ffac_ptr;
167+
}
168+
169+
static inline void CCryptoBoringSSLShims_set_p384_ffac_ptr(void * _Nonnull ptr) {
170+
_CCryptoBoringSSLShims_p384_ffac_ptr = ptr;
171+
}
172+
173+
static inline void * _Nullable CCryptoBoringSSLShims_get_p521_ffac_ptr(void) {
174+
return _CCryptoBoringSSLShims_p521_ffac_ptr;
175+
}
176+
177+
static inline void CCryptoBoringSSLShims_set_p521_ffac_ptr(void * _Nonnull ptr) {
178+
_CCryptoBoringSSLShims_p521_ffac_ptr = ptr;
179+
}
180+
151181
#if defined(__cplusplus)
152182
}
153183
#endif // defined(__cplusplus)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftCrypto open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
#include <CCryptoBoringSSLShims.h>
15+
16+
// Once we support C23, this should become `thread_local`.
17+
// DO NOT TOUCH DIRECTLY, use the getters and setters from the header file.
18+
_Thread_local void * _Nullable _CCryptoBoringSSLShims_p256_ffac_ptr;
19+
_Thread_local void * _Nullable _CCryptoBoringSSLShims_p384_ffac_ptr;
20+
_Thread_local void * _Nullable _CCryptoBoringSSLShims_p521_ffac_ptr;

Sources/_CryptoExtras/ECToolbox/BoringSSL/ECToolbox_boring.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
@_implementationOnly import CCryptoBoringSSLShims
1415
import Crypto
1516
import CryptoBoringWrapper
1617
import Foundation
@@ -67,12 +68,11 @@ extension P256: OpenSSLSupportedNISTCurve {
6768

6869
@usableFromInline
6970
static var __ffac: FiniteFieldArithmeticContext {
70-
let key = "com.apple.swift-crypto.P256.__ffac"
71-
if let value = Thread.current.threadDictionary[key] as? FiniteFieldArithmeticContext {
72-
return value
71+
if let valuePtr = CCryptoBoringSSLShims_get_p256_ffac_ptr() {
72+
return Unmanaged<FiniteFieldArithmeticContext>.fromOpaque(valuePtr).takeUnretainedValue()
7373
}
7474
let value = try! FiniteFieldArithmeticContext(fieldSize: P256.group.order)
75-
Thread.current.threadDictionary[key] = value
75+
CCryptoBoringSSLShims_set_p256_ffac_ptr(Unmanaged.passRetained(value).toOpaque())
7676
return value
7777
}
7878
}
@@ -100,12 +100,11 @@ extension P384: OpenSSLSupportedNISTCurve {
100100

101101
@usableFromInline
102102
static var __ffac: FiniteFieldArithmeticContext {
103-
let key = "com.apple.swift-crypto.P384.__ffac"
104-
if let value = Thread.current.threadDictionary[key] as? FiniteFieldArithmeticContext {
105-
return value
103+
if let valuePtr = CCryptoBoringSSLShims_get_p384_ffac_ptr() {
104+
return Unmanaged<FiniteFieldArithmeticContext>.fromOpaque(valuePtr).takeUnretainedValue()
106105
}
107106
let value = try! FiniteFieldArithmeticContext(fieldSize: P384.group.order)
108-
Thread.current.threadDictionary[key] = value
107+
CCryptoBoringSSLShims_set_p384_ffac_ptr(Unmanaged.passRetained(value).toOpaque())
109108
return value
110109
}
111110
}
@@ -133,12 +132,11 @@ extension P521: OpenSSLSupportedNISTCurve {
133132

134133
@usableFromInline
135134
static var __ffac: FiniteFieldArithmeticContext {
136-
let key = "com.apple.swift-crypto.P521.__ffac"
137-
if let value = Thread.current.threadDictionary[key] as? FiniteFieldArithmeticContext {
138-
return value
135+
if let valuePtr = CCryptoBoringSSLShims_get_p521_ffac_ptr() {
136+
return Unmanaged<FiniteFieldArithmeticContext>.fromOpaque(valuePtr).takeUnretainedValue()
139137
}
140138
let value = try! FiniteFieldArithmeticContext(fieldSize: P521.group.order)
141-
Thread.current.threadDictionary[key] = value
139+
CCryptoBoringSSLShims_set_p521_ffac_ptr(Unmanaged.passRetained(value).toOpaque())
142140
return value
143141
}
144142
}

0 commit comments

Comments
 (0)