mirror of
https://github.com/apple/swift-crypto
synced 2026-11-25 04:47:47 +00:00
Revert "(split: SHA512256) Enable CheckImplementationOnly and box properties where needed"
This reverts commit d6384ee4548e797894add480dfc0ea2b2e55ba1a.
This commit is contained in:
1 parent
dbc7ea1646
commit
bc748e9db5
4 files changed
+68
-46
No files matched your search
@@ -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"
|
||||
|
||||
@@ -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<SHA512_CTX> {
|
||||
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<SHA512_CTX>) {
|
||||
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<SHA512_CTX>.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<SHA512_CTX>.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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user