mirror of
https://github.com/apple/swift-crypto
synced 2026-11-25 04:47:47 +00:00
### Motivation: Currently HMAC heap allocates twice per construction for temporary storage. `HMAC.init(key:)` builds the inner and outer pads using two `SecureBytes` allocations. This is a bottleneck for both HMAC and HKDF, which constructs an HMAC per output block. Both pads never escape the initializer, so neither needs to be on the heap at all. A scoped temporary allocation that zeroes after use does the same job with no allocations. ### Modifications: - Fix the Benchmarks package build (missing `Sendable` annotation) - Add benchmarks for HMAC and HKDF - Add scoped zeroing temporary allocation helpers - Use a scoped zeroing allocation for the HMAC pad ### Result: - `HMAC<SHA256>.authenticationCode`: was 5 allocations, now 3, ~20% faster. - `HKDF<SHA256>.deriveKey`, 1 KiB output: was 170 allocations, now 104, ~30% faster. ### Benchmarks: ``` hmac-sha256-authenticate (before) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 5 │ 5 │ 5 │ 5 │ 5 │ 5 │ 5 │ 5 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (ns) * │ 1170 │ 1251 │ 1251 │ 1271 │ 1340 │ 1340 │ 1340 │ 5 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ hmac-sha256-authenticate (after) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 3 │ 3 │ 3 │ 3 │ 3 │ 3 │ 3 │ 5 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (ns) * │ 870 │ 910 │ 970 │ 1030 │ 1030 │ 1030 │ 1030 │ 5 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ hkdf-sha256-derive (before) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 170 │ 170 │ 170 │ 170 │ 170 │ 170 │ 170 │ 10 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (μs) * │ 20 │ 20 │ 30 │ 30 │ 30 │ 30 │ 30 │ 10 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ hkdf-sha256-derive (after) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 104 │ 104 │ 104 │ 104 │ 104 │ 104 │ 104 │ 10 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (μs) * │ 10 │ 20 │ 20 │ 20 │ 20 │ 20 │ 20 │ 10 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ``` ### Credits: This PR is a re-work of the original contribution by @ptoffy. The merge-commit will show him as a co-author. --------- Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
80 lines
2.1 KiB
Swift
80 lines
2.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) 2019-2020 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 canImport(CryptoKit)
|
|
@_exported import CryptoKit
|
|
#else
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
protocol Zeroization {
|
|
mutating func zeroize()
|
|
}
|
|
|
|
extension UnsafeMutablePointer: Zeroization {
|
|
/// Zeroizes the pointee
|
|
func zeroize() {
|
|
let size = MemoryLayout.size(ofValue: Pointee.self)
|
|
memset_s(self, size, 0, size)
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutableRawBufferPointer: Zeroization {
|
|
func zeroize() {
|
|
memset_s(self.baseAddress!, self.count, 0, self.count)
|
|
}
|
|
}
|
|
|
|
extension Array: Zeroization where Element == UInt8 {
|
|
/// Zeroizes the array
|
|
mutating func zeroize() {
|
|
memset_s(&self, self.count, 0, self.count)
|
|
}
|
|
}
|
|
|
|
extension Data: Zeroization {
|
|
internal mutating func zeroize() {
|
|
_ = self.withUnsafeMutableBytes {
|
|
memset_s($0.baseAddress!, $0.count, 0, $0.count)
|
|
}
|
|
}
|
|
}
|
|
|
|
func withUnsafeTemporaryZeroingAllocation<R>(
|
|
byteCount: Int,
|
|
alignment: Int,
|
|
_ body: (UnsafeMutableRawBufferPointer) throws -> R
|
|
) rethrows -> R {
|
|
try withUnsafeTemporaryAllocation(byteCount: byteCount, alignment: alignment) { buffer in
|
|
defer { buffer.zeroize() }
|
|
return try body(buffer)
|
|
}
|
|
}
|
|
|
|
func withTemporaryZeroingAllocation<R>(
|
|
byteCount: Int,
|
|
alignment: Int,
|
|
_ body: (inout OutputRawSpan) throws -> R
|
|
) rethrows -> R {
|
|
try withUnsafeTemporaryZeroingAllocation(byteCount: byteCount, alignment: alignment) { buffer in
|
|
var output = OutputRawSpan(buffer: buffer, initializedCount: 0)
|
|
return try body(&output)
|
|
}
|
|
}
|
|
|
|
#endif // canImport(CryptoKit)
|