From 56f26fd2330d97c3235b2e5115432c8a1fdbdd30 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 6 Aug 2026 07:42:28 +0100 Subject: [PATCH] Revert "(split: XWing) Enable CheckImplementationOnly and box properties where needed" This reverts commit 1f780b4d25c02733d1433581c3b7e4d8b657d7a5. --- .../Crypto/KEM/BoringSSL/XWing_boring.swift | 51 +++++++------------ 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift b/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift index 61bf9b4b..332e58cf 100644 --- a/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift +++ b/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift @@ -133,41 +133,24 @@ struct OpenSSLXWingPrivateKeyImpl: Sendable { extension OpenSSLXWingPrivateKeyImpl { @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) final class Backing: @unchecked Sendable { - // @_implementationOnly import => must use OpaquePointer for stored property. - private let _storage: OpaquePointer - var privateKey: UnsafeMutablePointer { - UnsafeMutablePointer(self._storage) + private var privateKey: XWING_private_key + + init(privateKey: XWING_private_key) { + self.privateKey = privateKey } - /// 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. - private init(takingOwnershipOf pointer: UnsafeMutablePointer) { - self._storage = OpaquePointer(pointer) - } - - deinit { - self.privateKey.deinitialize(count: 1) - self.privateKey.deallocate() - } - - convenience init(privateKey: XWING_private_key) { - let pk = UnsafeMutablePointer.allocate(capacity: 1) - pk.initialize(to: privateKey) - self.init(takingOwnershipOf: pk) - } - - convenience init() throws { - self.init(privateKey: XWING_private_key()) + init() throws { + self.privateKey = .init() try withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) { - let rc = CCryptoBoringSSL_XWING_generate_key($0.baseAddress, self.privateKey) + let rc = CCryptoBoringSSL_XWING_generate_key($0.baseAddress, &self.privateKey) if rc != 1 { throw CryptoKitError.internalBoringSSLError() } } } - convenience init(bytes: D) throws { - self.init(privateKey: XWING_private_key()) + init(bytes: D) throws { + self.privateKey = .init() // The first bytes are the private key (in "seed representation"), the latter bytes are the public key. try bytes.withUnsafeBytes { ptr in @@ -180,7 +163,7 @@ extension OpenSSLXWingPrivateKeyImpl { var cbs = CBS() CCryptoBoringSSL_CBS_init(&cbs, privateKeyBytes.baseAddress, privateKeyBytes.count) - let rc = CCryptoBoringSSL_XWING_parse_private_key(self.privateKey, &cbs) + let rc = CCryptoBoringSSL_XWING_parse_private_key(&self.privateKey, &cbs) guard rc == 1 else { throw CryptoKitError.internalBoringSSLError() } @@ -190,8 +173,8 @@ extension OpenSSLXWingPrivateKeyImpl { } } - convenience init(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws { - self.init(privateKey: XWING_private_key()) + init(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws { + self.privateKey = .init() let seedRepresentation: ContiguousBytes = seedRepresentation.regions.count == 1 ? seedRepresentation.regions.first! : Array(seedRepresentation) @@ -204,7 +187,7 @@ extension OpenSSLXWingPrivateKeyImpl { var cbs = CBS() CCryptoBoringSSL_CBS_init(&cbs, privateKeyBytes.baseAddress, privateKeyBytes.count) - let rc = CCryptoBoringSSL_XWING_parse_private_key(self.privateKey, &cbs) + let rc = CCryptoBoringSSL_XWING_parse_private_key(&self.privateKey, &cbs) guard rc == 1 else { throw CryptoKitError.internalBoringSSLError() } @@ -219,7 +202,7 @@ extension OpenSSLXWingPrivateKeyImpl { withUnsafeTemporaryAllocation(byteCount: Int(XWING_PRIVATE_KEY_BYTES), alignment: 1) { var cbb = CBB() CCryptoBoringSSL_CBB_init_fixed(&cbb, $0.baseAddress, $0.count) - let rc = CCryptoBoringSSL_XWING_marshal_private_key(&cbb, self.privateKey) + let rc = CCryptoBoringSSL_XWING_marshal_private_key(&cbb, &self.privateKey) precondition(rc == 1) return Data($0.prefix(CCryptoBoringSSL_CBB_len(&cbb))) } @@ -239,7 +222,7 @@ extension OpenSSLXWingPrivateKeyImpl { var publicKey: Data { withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) { - let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, self.privateKey) + let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, &self.privateKey) precondition(rc == 1) return Data($0) } @@ -247,7 +230,7 @@ extension OpenSSLXWingPrivateKeyImpl { private var publicKeyDigest: SHA3_256Digest { withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) { - let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, self.privateKey) + let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, &self.privateKey) precondition(rc == 1) return SHA3_256.hash(bufferPointer: UnsafeRawBufferPointer($0)) } @@ -269,7 +252,7 @@ extension OpenSSLXWingPrivateKeyImpl { let rc = CCryptoBoringSSL_XWING_decap( sharedSecretBytes.baseAddress, encapsulatedSecretBytes.baseAddress, - self.privateKey + &self.privateKey ) guard rc == 1 else { throw CryptoKitError.internalBoringSSLError()