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
|
//===----------------------------------------------------------------------===//
//
// 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.md 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
protocol HashFunctionImplementationDetails: HashFunction where Digest: DigestPrivate {}
protocol BoringSSLBackedHashFunction: HashFunctionImplementationDetails {
static var digestType: DigestContext.DigestType { get }
}
extension Insecure.MD5: BoringSSLBackedHashFunction {
static var digestType: DigestContext.DigestType {
.md5
}
}
extension Insecure.SHA1: BoringSSLBackedHashFunction {
static var digestType: DigestContext.DigestType {
.sha1
}
}
extension SHA256: BoringSSLBackedHashFunction {
static var digestType: DigestContext.DigestType {
.sha256
}
}
extension SHA384: BoringSSLBackedHashFunction {
static var digestType: DigestContext.DigestType {
.sha384
}
}
extension SHA512: BoringSSLBackedHashFunction {
static var digestType: DigestContext.DigestType {
.sha512
}
}
struct OpenSSLDigestImpl<H: BoringSSLBackedHashFunction> {
private var context: DigestContext
init() {
self.context = DigestContext(digest: H.digestType)
}
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 {
// To have a non-destructive finalize operation we must allocate.
let copyContext = DigestContext(copying: self.context)
let digestBytes = copyContext.finalize()
return digestBytes.withUnsafeBytes {
// We force unwrap here because if the digest size is wrong it's an internal error.
H.Digest(bufferPointer: $0)!
}
}
}
class DigestContext {
private var contextPointer: UnsafeMutablePointer<EVP_MD_CTX>
init(digest: DigestType) {
// We force unwrap because we cannot recover from allocation failure.
self.contextPointer = CCryptoBoringSSL_EVP_MD_CTX_new()!
guard CCryptoBoringSSL_EVP_DigestInit(self.contextPointer, digest.dispatchTable) != 0 else {
// We can't do much but crash here.
fatalError("Unable to initialize digest state: \(CCryptoBoringSSL_ERR_get_error())")
}
}
init(copying original: DigestContext) {
// We force unwrap because we cannot recover from allocation failure.
self.contextPointer = CCryptoBoringSSL_EVP_MD_CTX_new()!
guard CCryptoBoringSSL_EVP_MD_CTX_copy(self.contextPointer, original.contextPointer) != 0 else {
// We can't do much but crash here.
fatalError("Unable to copy digest state: \(CCryptoBoringSSL_ERR_get_error())")
}
}
func update(data: UnsafeRawBufferPointer) {
guard let baseAddress = data.baseAddress else {
return
}
CCryptoBoringSSL_EVP_DigestUpdate(self.contextPointer, baseAddress, data.count)
}
// This finalize function is _destructive_: do not call it if you want to reuse the object!
func finalize() -> [UInt8] {
let digestSize = CCryptoBoringSSL_EVP_MD_size(self.contextPointer.pointee.digest)
var digestBytes = Array(repeating: UInt8(0), count: digestSize)
var count = UInt32(digestSize)
digestBytes.withUnsafeMutableBufferPointer { digestPointer in
assert(digestPointer.count == count)
CCryptoBoringSSL_EVP_DigestFinal(self.contextPointer, digestPointer.baseAddress, &count)
}
return digestBytes
}
deinit {
CCryptoBoringSSL_EVP_MD_CTX_free(self.contextPointer)
}
}
extension DigestContext {
struct DigestType {
var dispatchTable: OpaquePointer
private init(_ dispatchTable: OpaquePointer) {
self.dispatchTable = dispatchTable
}
static let md5 = DigestType(CCryptoBoringSSL_EVP_md5())
static let sha1 = DigestType(CCryptoBoringSSL_EVP_sha1())
static let sha256 = DigestType(CCryptoBoringSSL_EVP_sha256())
static let sha384 = DigestType(CCryptoBoringSSL_EVP_sha384())
static let sha512 = DigestType(CCryptoBoringSSL_EVP_sha512())
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|