diff --git a/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift b/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift index 332e58cf..61bf9b4b 100644 --- a/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift +++ b/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift @@ -133,24 +133,41 @@ 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 { - private var privateKey: XWING_private_key - - init(privateKey: XWING_private_key) { - self.privateKey = privateKey + // @_implementationOnly import => must use OpaquePointer for stored property. + private let _storage: OpaquePointer + var privateKey: UnsafeMutablePointer { + UnsafeMutablePointer(self._storage) } - init() throws { - self.privateKey = .init() + /// 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()) 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() } } } - init(bytes: D) throws { - self.privateKey = .init() + convenience init(bytes: D) throws { + self.init(privateKey: XWING_private_key()) // The first bytes are the private key (in "seed representation"), the latter bytes are the public key. try bytes.withUnsafeBytes { ptr in @@ -163,7 +180,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() } @@ -173,8 +190,8 @@ extension OpenSSLXWingPrivateKeyImpl { } } - init(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws { - self.privateKey = .init() + convenience init(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws { + self.init(privateKey: XWING_private_key()) let seedRepresentation: ContiguousBytes = seedRepresentation.regions.count == 1 ? seedRepresentation.regions.first! : Array(seedRepresentation) @@ -187,7 +204,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() } @@ -202,7 +219,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))) } @@ -222,7 +239,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) } @@ -230,7 +247,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)) } @@ -252,7 +269,7 @@ extension OpenSSLXWingPrivateKeyImpl { let rc = CCryptoBoringSSL_XWING_decap( sharedSecretBytes.baseAddress, encapsulatedSecretBytes.baseAddress, - &self.privateKey + self.privateKey ) guard rc == 1 else { throw CryptoKitError.internalBoringSSLError()