From c78a61b4d0cc0ffbef25f7446c8b8e4090a80e0c Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 6 Aug 2026 07:41:56 +0100 Subject: [PATCH] Revert "(split: AEAD) Enable CheckImplementationOnly and box properties where needed" This reverts commit af3e61654b5d3a0e36ce575d15e27d79b6bd7184. --- .../AEAD/BoringSSLAEAD.swift | 81 ++++++++++--------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift b/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift index 6eacefbc..fa716701 100644 --- a/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift +++ b/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift @@ -44,33 +44,34 @@ extension BoringSSLAEAD { // rather than rely on defer statements for our cleanup. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) public class AEADContext { - // @_implementationOnly import => must use OpaquePointer for stored property. - private let _storage: OpaquePointer - private var context: UnsafeMutablePointer { UnsafeMutablePointer(self._storage) } + private var context: EVP_AEAD_CTX - private init(takingOwnershipOf pointer: UnsafeMutablePointer) { - self._storage = OpaquePointer(pointer) - } + public init(cipher: BoringSSLAEAD, key: Key) throws { + self.context = EVP_AEAD_CTX() - public convenience init(cipher: BoringSSLAEAD, key: Key) throws { - // Create the AEAD context with a default tag length using the given key. - guard - let context = key.withUnsafeBytes({ keyPointer in - CCryptoBoringSSL_EVP_AEAD_CTX_new( + let rc: CInt = key.withUnsafeBytes { keyPointer in + withUnsafeMutablePointer(to: &self.context) { contextPointer in + // Create the AEAD context with a default tag length using the given key. + CCryptoBoringSSLShims_EVP_AEAD_CTX_init( + contextPointer, cipher.boringSSLCipher, keyPointer.baseAddress, keyPointer.count, - 0 + 0, + nil ) - }) - else { + } + } + + guard rc == 1 else { throw CryptoBoringWrapperError.internalBoringSSLError() } - self.init(takingOwnershipOf: context) } deinit { - CCryptoBoringSSL_EVP_AEAD_CTX_free(context) + withUnsafeMutablePointer(to: &self.context) { contextPointer in + CCryptoBoringSSL_EVP_AEAD_CTX_cleanup(contextPointer) + } } } } @@ -163,11 +164,11 @@ extension BoringSSLAEAD.AEADContext { authenticatedData: RawSpan, tag: inout OutputRawSpan ) throws { - let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.pointee.aead) + let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead) precondition(tag.freeCapacity >= tagByteCount) var actualTagSize = tagByteCount - let rc = + let rc = withUnsafeMutablePointer(to: &self.context) { contextPointer in message.withUnsafeMutableBytes { messageBuffer in tag.withUnsafeMutableBytes { tagBuffer, tagInitializedCount in defer { @@ -177,7 +178,7 @@ extension BoringSSLAEAD.AEADContext { return authenticatedData.withUnsafeBytes { authenticatedDataBuffer in nonce.withUnsafeBytes { nonceBuffer in CCryptoBoringSSLShims_EVP_AEAD_CTX_seal_scatter( - self.context, + contextPointer, messageBuffer.baseAddress, tagBuffer.baseAddress! + tagInitializedCount, &actualTagSize, @@ -196,6 +197,7 @@ extension BoringSSLAEAD.AEADContext { } } } + } guard rc == 1 else { throw CryptoBoringWrapperError.internalBoringSSLError() @@ -209,7 +211,7 @@ extension BoringSSLAEAD.AEADContext { nonce: RawSpan, authenticatedData: RawSpan ) throws -> Data { - let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.pointee.aead) + let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead) // Form the combined represention of a sealed box with nonce + plaintext + tag. var combined = Data() @@ -312,22 +314,24 @@ extension BoringSSLAEAD.AEADContext { tag: RawSpan, authenticatedData: RawSpan ) throws { - let rc = message.withUnsafeMutableBytes { messageBuffer in - nonce.withUnsafeBytes { nonceBuffer in - tag.withUnsafeBytes { tagBuffer in - authenticatedData.withUnsafeBytes { adBuffer in - CCryptoBoringSSLShims_EVP_AEAD_CTX_open_gather( - self.context, - messageBuffer.baseAddress, - nonceBuffer.baseAddress, - nonceBuffer.count, - messageBuffer.baseAddress, - messageBuffer.count, - tagBuffer.baseAddress, - tagBuffer.count, - adBuffer.baseAddress, - adBuffer.count - ) + let rc = withUnsafePointer(to: &self.context) { contextPointer in + message.withUnsafeMutableBytes { messageBuffer in + nonce.withUnsafeBytes { nonceBuffer in + tag.withUnsafeBytes { tagBuffer in + authenticatedData.withUnsafeBytes { adBuffer in + CCryptoBoringSSLShims_EVP_AEAD_CTX_open_gather( + contextPointer, + messageBuffer.baseAddress, + nonceBuffer.baseAddress, + nonceBuffer.count, + messageBuffer.baseAddress, + messageBuffer.count, + tagBuffer.baseAddress, + tagBuffer.count, + adBuffer.baseAddress, + adBuffer.count + ) + } } } } @@ -425,9 +429,9 @@ extension BoringSSLAEAD.AEADContext { ) var writtenBytes = 0 - let rc = + let rc = withUnsafePointer(to: &self.context) { contextPointer in CCryptoBoringSSLShims_EVP_AEAD_CTX_open( - self.context, + contextPointer, outputBuffer.baseAddress, &writtenBytes, outputBuffer.count, @@ -438,6 +442,7 @@ extension BoringSSLAEAD.AEADContext { authenticatedData.baseAddress, authenticatedData.count ) + } guard rc == 1 else { // Ooops, error. Free the memory we allocated before we throw.