diff --git a/Sources/Crypto/Util/Zeroization.swift b/Sources/Crypto/Util/Zeroization.swift index a2476103..4d0c1458 100644 --- a/Sources/Crypto/Util/Zeroization.swift +++ b/Sources/Crypto/Util/Zeroization.swift @@ -28,7 +28,7 @@ protocol Zeroization { extension UnsafeMutablePointer: Zeroization { /// Zeroizes the pointee func zeroize() { - let size = MemoryLayout.size(ofValue: Pointee.self) + let size = MemoryLayout.size memset_s(self, size, 0, size) } } diff --git a/Sources/CryptoExtras/CMakeLists.txt b/Sources/CryptoExtras/CMakeLists.txt index 1b4fdb93..83934daa 100644 --- a/Sources/CryptoExtras/CMakeLists.txt +++ b/Sources/CryptoExtras/CMakeLists.txt @@ -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" diff --git a/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift b/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift index 8fe33e1c..c8bcab8e 100644 --- a/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.swift +++ b/Sources/CryptoExtras/Digests/BoringSSL/BoringSSLSHA512256Context.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) - } -} diff --git a/Sources/CryptoExtras/Util/Zeroization.swift b/Sources/CryptoExtras/Util/Zeroization.swift new file mode 100644 index 00000000..23172496 --- /dev/null +++ b/Sources/CryptoExtras/Util/Zeroization.swift @@ -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.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( + 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( + 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) + } +} diff --git a/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift b/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift index 04cbfe6e..0d9369b3 100644 --- a/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift +++ b/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift @@ -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)