mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Zero the entire pointee in UnsafeMutablePointer.zeroize (#491)
### Motivation: We have some helper functions to zero out memory after use. One of these helper functions provides a `zeroize()` method on `UnsafeMutablePointer`, which has a subtle bug. It zeros out `MemoryLayout.size(ofValue: Pointee.self)` instead of `MemoryLayout<Pointee>.size`, so only the first 8 bytes get zeroed. This bug is duplicated in CryptoExtras too in a fileprivate helper. ### Modifications: - Zero the entire pointee in UnsafeMutablePointer.zeroize - Copy Zeroization helpers from Crypto to CryptoExtras (without the `#if canImport(CryptoKit)` guard) ### Result: All the memory that should be zeroed by this helper is actually zeroed.
This commit is contained in:
1 parent
6a032cf17d
commit
57de8e7cb8
5 files changed
+83
-14
No files matched your search
@@ -28,7 +28,7 @@ protocol Zeroization {
|
||||
extension UnsafeMutablePointer: Zeroization {
|
||||
/// Zeroizes the pointee
|
||||
func zeroize() {
|
||||
let size = MemoryLayout.size(ofValue: Pointee.self)
|
||||
let size = MemoryLayout<Pointee>.size
|
||||
memset_s(self, size, 0, size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ add_library(CryptoExtras
|
||||
"Util/ThreadSpecific/ThreadPosix.swift"
|
||||
"Util/ThreadSpecific/ThreadSpecific.swift"
|
||||
"Util/ThreadSpecific/ThreadWindows.swift"
|
||||
"Util/Zeroization.swift"
|
||||
"ZKPs/DLEQ.swift"
|
||||
"ZKPs/Prover.swift"
|
||||
"ZKPs/Verifier.swift"
|
||||
|
||||
@@ -72,16 +72,3 @@ final class BoringSSLSHA512256Context {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
#else
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
protocol Zeroization {
|
||||
mutating func zeroize()
|
||||
}
|
||||
|
||||
extension UnsafeMutablePointer: Zeroization {
|
||||
/// Zeroizes the pointee
|
||||
func zeroize() {
|
||||
let size = MemoryLayout<Pointee>.size
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -217,6 +217,12 @@ final class SecureBytesTests: XCTestCase {
|
||||
XCTAssertEqual(base.count, 22)
|
||||
XCTAssertEqual(Array(copy), [0, 1, 2, 3, 4])
|
||||
}
|
||||
|
||||
func testZeroizePointerToMultiWordValueClearsAllBytes() {
|
||||
var value: (UInt64, UInt64, UInt64, UInt64) = (1, 2, 3, 4)
|
||||
withUnsafeMutablePointer(to: &value) { $0.zeroize() }
|
||||
XCTAssertEqual(withUnsafeBytes(of: value) { Array($0) }, Array(repeating: UInt8(0), count: 32))
|
||||
}
|
||||
}
|
||||
|
||||
#endif // canImport(CryptoKit)
|
||||
Reference in new issue
Block a user