mirror of
https://github.com/apple/swift-crypto
synced 2026-11-30 05:18:46 +00:00
Add SHA-512/256 digest implementation via BoringSSL ### Checklist - [x] I've run tests to see all new and existing tests pass - [x] I've followed the code style of the rest of the project - [x] I've read the [Contribution Guidelines](CONTRIBUTING.md) - [x] I've updated the documentation if necessary #### If you've made changes to `gyb` files - [N/A] I've run `.script/generate_boilerplate_files_with_gyb` and included updated generated files in a commit of this pull request ### Motivation: Neither CryptoKit nor swift-crypto currently explore the SHA-512/256 hashing function. SHA-512/256 provides the same 256-bit security level as SHA-256 but can be significantly faster on 64-bit platforms because it uses the SHA-512 internal structure, which operates on 64-bit words. ### Modifications: We modified CryptoExtras to include support using the BoringSSL implementation. ### Result: Swift ecosystem supports SHA-512/256. --------- Co-authored-by: Cory Benfield <lukasa@apple.com>
83 lines
2.5 KiB
Swift
83 lines
2.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
#endif
|
|
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
final class BoringSSLSHA512256Context {
|
|
private var context: SHA512_CTX
|
|
|
|
init() {
|
|
guard let context = BoringSSLSHA512256HashFunction.initialize() else {
|
|
preconditionFailure("Unable to initialize digest state")
|
|
}
|
|
self.context = context
|
|
}
|
|
|
|
deinit {
|
|
withUnsafeMutablePointer(to: &self.context) {
|
|
$0.zeroize()
|
|
}
|
|
}
|
|
|
|
init(copying original: BoringSSLSHA512256Context) {
|
|
self.context = original.context
|
|
}
|
|
|
|
func update(bufferPointer data: UnsafeRawBufferPointer) {
|
|
guard BoringSSLSHA512256HashFunction.update(&self.context, data: data) else {
|
|
preconditionFailure("Unable to update digest state")
|
|
}
|
|
}
|
|
|
|
func finalize() -> SHA512256Digest {
|
|
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.
|
|
return SHA512256Digest(bufferPointer: UnsafeRawBufferPointer(digestPointer))!
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutablePointer {
|
|
fileprivate func zeroize() {
|
|
let size = MemoryLayout.size(ofValue: Pointee.self)
|
|
memset_s(self, size, 0, size)
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutableRawBufferPointer {
|
|
fileprivate func zeroize() {
|
|
memset_s(self.baseAddress!, self.count, 0, self.count)
|
|
}
|
|
}
|