-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathBoringSSLSHA512256Context.swift
More file actions
82 lines (71 loc) · 2.5 KB
/
BoringSSLSHA512256Context.swift
File metadata and controls
82 lines (71 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//===----------------------------------------------------------------------===//
//
// 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)
}
}