From 4304fd7dcfd91cf50b431307f05bbb55c11daefb Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Mon, 13 Jul 2026 16:16:14 +0100 Subject: [PATCH] (split: ECDSASignature) Enable CheckImplementationOnly and box properties where needed We've long since used `@_implementationOnly import` (IOI) for our vendored C modules (BoringSSL and libXKCP) in an attempt to not leak our dependency into downstream builds. This is important because leaking might cause symbol collisions (build failures) or, worse, layout mismatches (latent runtime failures). IOI was intended to be used with library evolution, which this project does not support, and use of IOI without library evolution has been soft-deprecated. Users are encouraged to move to `internal import` (II), which is usually the right choice for Swift packages. But II does _not_ provide the same guarantees as IOI and, for this project, we really need the protections given by IOI so we will continue to use it, even with library evolution disabled. One of the reasons for the soft-deprecation was the implementation of IOI used an incomplete checker, that won't catch all interface leaks without library evolution. However, Swift has also introduced a new feature, `CheckImplementationOnly` (CIO), which enables the stricter interface checking that we need. Enabling CIO for this project today actually exposes a number of places where we _are_ leaking the C types into our interface in ways that could cause build- or runtime problems in downstream projects, and rightly causes the build to fail. These types will need to have their C struct members boxed behind an `OpaquePointer`. This patch enables CIO, and adds the required boxing to the offending types. The public API is unchanged. Additional refactoring is also done to ensure a single designated initializer, responsible for the allocation, forcing all convenience initializers through an `init(takingOwnershipOf:)` spelling, to reduce the chances of introducing future double free bugs in complex initializers. This patch also adds some benchmarks for some of the paths that would be implicated by these changes. While they show the expected increase in allocations, there's only a modest-to-negligible performance regression, since the majority of the compute is dominated by the time spent doing the crypto routines in the C library themselves. --- .../BoringSSL/ECDSASignature_boring.swift | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift b/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift index d5e0ebdc..1886fbbb 100644 --- a/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift @@ -26,10 +26,22 @@ import Foundation /// A wrapper around BoringSSL's ECDSA_SIG with some lifetime management. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) class ECDSASignature { - private var _baseSig: UnsafeMutablePointer + // @_implementationOnly import => must use OpaquePointer for stored property. + private let _storage: OpaquePointer + private var _baseSig: UnsafeMutablePointer { UnsafeMutablePointer(self._storage) } - init(contiguousDERBytes derBytes: ContiguousBuffer) throws { - self._baseSig = try derBytes.withUnsafeBytes { bytesPtr in + /// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit. + /// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers. + init(takingOwnershipOf pointer: UnsafeMutablePointer) { + self._storage = OpaquePointer(pointer) + } + + deinit { + CCryptoBoringSSL_ECDSA_SIG_free(self._baseSig) + } + + convenience init(contiguousDERBytes derBytes: ContiguousBuffer) throws { + let sig = try derBytes.withUnsafeBytes { bytesPtr in guard let sig = CCryptoBoringSSLShims_ECDSA_SIG_from_bytes(bytesPtr.baseAddress, bytesPtr.count) else { @@ -37,10 +49,11 @@ class ECDSASignature { } return sig } + self.init(takingOwnershipOf: sig) } @usableFromInline - init(rawRepresentation: Data) throws { + convenience init(rawRepresentation: Data) throws { let half = rawRepresentation.count / 2 let r = try ArbitraryPrecisionInteger(bytes: rawRepresentation.prefix(half)) let s = try ArbitraryPrecisionInteger(bytes: rawRepresentation.suffix(half)) @@ -48,7 +61,7 @@ class ECDSASignature { throw CryptoKitError.internalBoringSSLError() } - self._baseSig = sig + self.init(takingOwnershipOf: sig) try r.withUnsafeBignumPointer { rPtr in try s.withUnsafeBignumPointer { sPtr in @@ -75,14 +88,6 @@ class ECDSASignature { } } - init(takingOwnershipOf pointer: UnsafeMutablePointer) { - self._baseSig = pointer - } - - deinit { - CCryptoBoringSSL_ECDSA_SIG_free(self._baseSig) - } - @usableFromInline var components: (r: ArbitraryPrecisionInteger, s: ArbitraryPrecisionInteger) { var rPtr: UnsafePointer?