-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathDigest_boring.swift
More file actions
213 lines (181 loc) · 7.6 KB
/
Digest_boring.swift
File metadata and controls
213 lines (181 loc) · 7.6 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019 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
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
protocol HashFunctionImplementationDetails: HashFunction where Digest: DigestPrivate {}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
protocol BoringSSLBackedHashFunction: HashFunctionImplementationDetails {
associatedtype Context
static var digestSize: Int { get }
static func initialize() -> Context?
static func update(_ context: inout Context, data: UnsafeRawBufferPointer) -> Bool
static func finalize(_ context: inout Context, digest: UnsafeMutableRawBufferPointer) -> Bool
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension Insecure.MD5: BoringSSLBackedHashFunction {
static var digestSize: Int {
Int(MD5_DIGEST_LENGTH)
}
static func initialize() -> MD5_CTX? {
var context = MD5_CTX()
guard CCryptoBoringSSL_MD5_Init(&context) == 1 else {
return nil
}
return context
}
static func update(_ context: inout MD5_CTX, data: UnsafeRawBufferPointer) -> Bool {
CCryptoBoringSSL_MD5_Update(&context, data.baseAddress, data.count) == 1
}
static func finalize(_ context: inout MD5_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
guard let baseAddress = digest.baseAddress, digest.count == Self.digestSize else { return false }
return CCryptoBoringSSL_MD5_Final(baseAddress, &context) == 1
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension Insecure.SHA1: BoringSSLBackedHashFunction {
static var digestSize: Int {
Int(SHA_DIGEST_LENGTH)
}
static func initialize() -> SHA_CTX? {
var context = SHA_CTX()
guard CCryptoBoringSSL_SHA1_Init(&context) == 1 else {
return nil
}
return context
}
static func update(_ context: inout SHA_CTX, data: UnsafeRawBufferPointer) -> Bool {
CCryptoBoringSSL_SHA1_Update(&context, data.baseAddress, data.count) == 1
}
static func finalize(_ context: inout SHA_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
guard let baseAddress = digest.baseAddress, digest.count == Self.digestSize else { return false }
return CCryptoBoringSSL_SHA1_Final(baseAddress, &context) == 1
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SHA256: BoringSSLBackedHashFunction {
static var digestSize: Int {
Int(SHA256_DIGEST_LENGTH)
}
static func initialize() -> SHA256_CTX? {
var context = SHA256_CTX()
guard CCryptoBoringSSL_SHA256_Init(&context) == 1 else {
return nil
}
return context
}
static func update(_ context: inout SHA256_CTX, data: UnsafeRawBufferPointer) -> Bool {
CCryptoBoringSSL_SHA256_Update(&context, data.baseAddress, data.count) == 1
}
static func finalize(_ context: inout SHA256_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
guard let baseAddress = digest.baseAddress, digest.count == Self.digestSize else { return false }
return CCryptoBoringSSL_SHA256_Final(baseAddress, &context) == 1
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SHA384: BoringSSLBackedHashFunction {
static var digestSize: Int {
Int(SHA384_DIGEST_LENGTH)
}
static func initialize() -> SHA512_CTX? {
var context = SHA512_CTX()
guard CCryptoBoringSSL_SHA384_Init(&context) == 1 else {
return nil
}
return context
}
static func update(_ context: inout SHA512_CTX, data: UnsafeRawBufferPointer) -> Bool {
CCryptoBoringSSL_SHA384_Update(&context, data.baseAddress, data.count) == 1
}
static func finalize(_ context: inout SHA512_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
guard let baseAddress = digest.baseAddress, digest.count == Self.digestSize else { return false }
return CCryptoBoringSSL_SHA384_Final(baseAddress, &context) == 1
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SHA512: BoringSSLBackedHashFunction {
static var digestSize: Int {
Int(SHA512_DIGEST_LENGTH)
}
static func initialize() -> SHA512_CTX? {
var context = SHA512_CTX()
guard CCryptoBoringSSL_SHA512_Init(&context) == 1 else {
return nil
}
return context
}
static func update(_ context: inout SHA512_CTX, data: UnsafeRawBufferPointer) -> Bool {
CCryptoBoringSSL_SHA512_Update(&context, data.baseAddress, data.count) == 1
}
static func finalize(_ context: inout SHA512_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
guard let baseAddress = digest.baseAddress, digest.count == Self.digestSize else { return false }
return CCryptoBoringSSL_SHA512_Final(baseAddress, &context) == 1
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
struct OpenSSLDigestImpl<H: BoringSSLBackedHashFunction>: @unchecked Sendable {
private var context: DigestContext<H>
init() {
self.context = DigestContext()
}
internal mutating func update(data: UnsafeRawBufferPointer) {
if !isKnownUniquelyReferenced(&self.context) {
self.context = DigestContext(copying: self.context)
}
self.context.update(data: data)
}
internal func finalize() -> H.Digest {
self.context.finalize()
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
private final class DigestContext<H: BoringSSLBackedHashFunction> {
private var context: H.Context
init() {
guard let context = H.initialize() else {
preconditionFailure("Unable to initialize digest state")
}
self.context = context
}
init(copying original: DigestContext) {
self.context = original.context
}
func update(data: UnsafeRawBufferPointer) {
guard H.update(&self.context, data: data) else {
preconditionFailure("Unable to update digest state")
}
}
func finalize() -> H.Digest {
var copyContext = self.context
defer {
withUnsafeMutablePointer(to: ©Context) { $0.zeroize() }
}
return withUnsafeTemporaryAllocation(byteCount: H.digestSize, alignment: 1) { digestPointer in
defer {
digestPointer.zeroize()
}
guard H.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 H.Digest(bufferPointer: UnsafeRawBufferPointer(digestPointer))!
}
}
deinit {
withUnsafeMutablePointer(to: &self.context) { $0.zeroize() }
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API