From bc748e9db527d3dcd92a8803e1cf4a52d5b0fe4c Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 6 Aug 2026 07:43:41 +0100 Subject: [PATCH] Revert "(split: SHA512256) Enable CheckImplementationOnly and box properties where needed" This reverts commit d6384ee4548e797894add480dfc0ea2b2e55ba1a. --- Sources/CryptoExtras/CMakeLists.txt | 1 + .../BoringSSL/BoringSSLSHA512256Context.swift | 54 +++++++++---------- .../BoringSSLSHA512256HashFunction.swift | 43 +++++++++++++++ Tests/CryptoExtrasTests/SHA512256Tests.swift | 16 ------ 4 files changed, 68 insertions(+), 46 deletions(-) create mode 100644 Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256HashFunction.swift diff --git a/Sources/CryptoExtras/CMakeLists.txt b/Sources/CryptoExtras/CMakeLists.txt index 9c68be21..1a71ae68 100644 --- a/Sources/CryptoExtras/CMakeLists.txt +++ b/Sources/CryptoExtras/CMakeLists.txt @@ -34,6 +34,7 @@ add_library(CryptoExtras "ChaCha20CTR/BoringSSL/ChaCha20CTR_boring.swift" "ChaCha20CTR/ChaCha20CTR.swift" "Digests/BoringSSL/BoringSSLSHA512256Context.swift" + "Digests/BoringSSL/BoringSSLSHA512256HashFunction.swift" "Digests/SHA512256.swift" "Digests/SHA512256Digest.swift" "EC/Curve25519+PEM.swift" diff --git a/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift b/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift index 888b0e64..8fe33e1c 100644 --- a/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift +++ b/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift @@ -26,51 +26,45 @@ import Darwin @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) final class BoringSSLSHA512256Context { - // @_implementationOnly import => must use OpaquePointer for stored property. - private let _storage: OpaquePointer - private var context: UnsafeMutablePointer { - UnsafeMutablePointer(self._storage) - } + private var context: SHA512_CTX - /// 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) + init() { + guard let context = BoringSSLSHA512256HashFunction.initialize() else { + preconditionFailure("Unable to initialize digest state") + } + self.context = context } deinit { - self.context.zeroize() - self.context.deinitialize(count: 1) - self.context.deallocate() - } - - convenience init() { - let ptr = UnsafeMutablePointer.allocate(capacity: 1) - ptr.initialize(to: SHA512_CTX()) - self.init(takingOwnershipOf: ptr) - - guard CCryptoBoringSSL_SHA512_256_Init(self.context) == 1 else { - preconditionFailure("Unable to initialize digest state") + withUnsafeMutablePointer(to: &self.context) { + $0.zeroize() } } - convenience init(copying original: BoringSSLSHA512256Context) { - let ptr = UnsafeMutablePointer.allocate(capacity: 1) - ptr.initialize(to: original.context.pointee) - self.init(takingOwnershipOf: ptr) + init(copying original: BoringSSLSHA512256Context) { + self.context = original.context } func update(bufferPointer data: UnsafeRawBufferPointer) { - guard CCryptoBoringSSL_SHA512_256_Update(self.context, data.baseAddress, data.count) == 1 else { + guard BoringSSLSHA512256HashFunction.update(&self.context, data: data) else { preconditionFailure("Unable to update digest state") } } func finalize() -> SHA512256Digest { - var contextCopy = self.context.pointee - defer { withUnsafeMutablePointer(to: &contextCopy) { $0.zeroize() } } - return withUnsafeTemporaryAllocation(byteCount: SHA512256Digest.byteCount, alignment: 1) { digestPointer in - guard CCryptoBoringSSL_SHA512_256_Final(digestPointer.baseAddress, &contextCopy) == 1 else { + var copyContext = self.context + defer { + withUnsafeMutablePointer(to: ©Context) { + $0.zeroize() + } + } + return withUnsafeTemporaryAllocation(byteCount: BoringSSLSHA512256HashFunction.digestSize, alignment: 1) { + digestPointer in + defer { + digestPointer.zeroize() + } + + guard BoringSSLSHA512256HashFunction.finalize(©Context, digest: digestPointer) else { preconditionFailure("Unable to finalize digest state") } // We force unwrap here because if the digest size is wrong it's an internal error. diff --git a/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256HashFunction.swift b/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256HashFunction.swift new file mode 100644 index 00000000..d97a2369 --- /dev/null +++ b/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256HashFunction.swift @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCrypto open source project +// +// Copyright (c) 2026 Apple Inc. and the SwiftCrypto project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +@_implementationOnly import CCryptoBoringSSL +import Crypto + +struct BoringSSLSHA512256HashFunction { + static var digestSize: Int { + Int(SHA256_DIGEST_LENGTH) + } + + static func initialize() -> SHA512_CTX? { + var context = SHA512_CTX() + guard CCryptoBoringSSL_SHA512_256_Init(&context) == 1 else { + return nil + } + return context + } + + static func update(_ context: inout SHA512_CTX, data: UnsafeRawBufferPointer) -> Bool { + let result = CCryptoBoringSSL_SHA512_256_Update(&context, data.baseAddress, data.count) + return result == 1 + } + + static func finalize(_ context: inout SHA512_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool { + guard let baseAddress = digest.baseAddress, digest.count == Self.digestSize else { + return false + } + let result = CCryptoBoringSSL_SHA512_256_Final(baseAddress, &context) + return result == 1 + } +} diff --git a/Tests/CryptoExtrasTests/SHA512256Tests.swift b/Tests/CryptoExtrasTests/SHA512256Tests.swift index 9f817d0d..37ae51fd 100644 --- a/Tests/CryptoExtrasTests/SHA512256Tests.swift +++ b/Tests/CryptoExtrasTests/SHA512256Tests.swift @@ -87,22 +87,6 @@ final class SHA512256DigestTests: XCTestCase { XCTAssertEqual(digest, copyDigest) } - func testFinalizeIsNonDestructive() { - let message = Data([1, 2, 3, 4]) - let expected = Array(SHA512256.hash(data: message)) - - var hasher = SHA512256() - hasher.update(data: message) - let copy = hasher - - XCTAssertEqual(Array(hasher.finalize()), expected) - XCTAssertEqual(Array(hasher.finalize()), expected) - XCTAssertEqual(Array(copy.finalize()), expected) - - hasher.update(data: [5, 6, 7, 8]) - XCTAssertEqual(Array(hasher.finalize()), Array(SHA512256.hash(data: [1, 2, 3, 4, 5, 6, 7, 8]))) - } - func testBlockSize() { XCTAssertEqual(SHA512256.blockByteCount, 128) }