Files
Si Beaumont 57de8e7cb8 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.
2026-10-23 11:46:45 +00:00

229 lines
8.5 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
//
//===----------------------------------------------------------------------===//
import XCTest
#if canImport(CryptoKit)
// Skip tests that require @testable imports of CryptoKit.
#else
@testable import Crypto
final class SecureBytesTests: XCTestCase {
func testBasicSoundness() {
var first = SecureBytes()
var second = SecureBytes()
first.append(Data("hello".utf8))
second.append(Data("hello".utf8))
XCTAssertEqual(first, second)
first.append(Data("world".utf8))
second.append(Data("wrold".utf8))
XCTAssertNotEqual(first, second)
}
func testSimpleCollection() {
let base = SecureBytes(0..<100)
XCTAssertEqual(base.count, 100)
XCTAssertEqual(Array(base), Array(0..<100))
XCTAssertEqual(base.first, 0)
XCTAssertEqual(base.last, 99)
XCTAssertEqual(base.reduce(Int(0)) { Int($0) + Int($1) }, 4950)
}
func testSimpleBidirectionalCollection() {
let base = SecureBytes(0..<100)
let reversed = base.reversed()
XCTAssertEqual(Array(reversed), Array(stride(from: 99, through: 0, by: -1)))
}
func testSimpleRandomAccessCollection() {
// Not easy to test this, just try to move the indices around a bit.
let base = SecureBytes(0..<100)
let aMiddleIndex = base.index(base.startIndex, offsetBy: 48)
let aDifferentMiddleIndex = base.index(aMiddleIndex, offsetBy: 5)
XCTAssertEqual(base.distance(from: aMiddleIndex, to: aDifferentMiddleIndex), 5)
XCTAssertEqual(base[aMiddleIndex], 48)
XCTAssertEqual(base[aDifferentMiddleIndex], 48 + 5)
}
func testSimpleMutableCollection() {
var base = SecureBytes(repeating: 0, count: 5)
let offset = base.index(base.startIndex, offsetBy: 2)
base[offset] = 5
XCTAssertEqual(Array(base), [0, 0, 5, 0, 0])
}
func testSimpleRangeReplaceableCollection() {
// This test validates RangeReplaceableCollection and the value semantics all at once.
let base = SecureBytes(repeating: 0, count: 10)
let baseBytes = Array(repeating: UInt8(0), count: 10)
// There are a few ways we can "replace" a subrange. The first is to extend at the front by appending.
var copy = base
copy.insert(contentsOf: [1, 2, 3, 4], at: copy.startIndex)
XCTAssertEqual(Array(copy), [1, 2, 3, 4] + baseBytes)
XCTAssertEqual(Array(base), baseBytes)
XCTAssertNotEqual(copy, base)
// The second is to extend at the back.
copy = base
copy.append(contentsOf: [1, 2, 3, 4])
XCTAssertEqual(Array(copy), baseBytes + [1, 2, 3, 4])
XCTAssertEqual(Array(base), baseBytes)
XCTAssertNotEqual(copy, base)
// The third is to "shrink" by replacing a subrange in the middle.
copy = base
var aMiddleIndex = copy.index(copy.startIndex, offsetBy: 2)
var aDifferentMiddleIndex = copy.index(aMiddleIndex, offsetBy: 5)
copy.removeSubrange(aMiddleIndex..<aDifferentMiddleIndex)
XCTAssertEqual(copy.count, 5)
XCTAssertEqual(Array(copy), [0, 0, 0, 0, 0])
XCTAssertEqual(Array(base), baseBytes)
XCTAssertNotEqual(copy, base)
// The fourth is to replace a fixed size subrange with a different subrange of the same size.
copy = base
aMiddleIndex = copy.index(copy.startIndex, offsetBy: 2)
aDifferentMiddleIndex = copy.index(aMiddleIndex, offsetBy: 5)
copy.replaceSubrange(aMiddleIndex..<aDifferentMiddleIndex, with: [1, 2, 3, 4, 5])
XCTAssertEqual(copy.count, 10)
XCTAssertEqual(Array(copy), [0, 0, 1, 2, 3, 4, 5, 0, 0, 0])
XCTAssertEqual(Array(base), baseBytes)
XCTAssertNotEqual(copy, base)
// The fifth is to make the storage bigger.
copy = base
aMiddleIndex = copy.index(copy.startIndex, offsetBy: 2)
aDifferentMiddleIndex = copy.index(aMiddleIndex, offsetBy: 5)
copy.replaceSubrange(aMiddleIndex..<aDifferentMiddleIndex, with: [1, 2, 3, 4, 5, 6, 7])
XCTAssertEqual(copy.count, 12)
XCTAssertEqual(Array(copy), [0, 0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0])
XCTAssertEqual(Array(base), baseBytes)
XCTAssertNotEqual(copy, base)
}
func testResizingByMakingLarger() {
var base = SecureBytes(count: 12)
XCTAssertGreaterThanOrEqual(base.backing.allocatedCapacity, 16)
XCTAssertEqual(base.count, 12)
base.append(contentsOf: 0..<16)
XCTAssertGreaterThanOrEqual(base.backing.allocatedCapacity, 32)
XCTAssertEqual(base.count, 28)
base.append(contentsOf: 0..<4)
XCTAssertGreaterThanOrEqual(base.backing.allocatedCapacity, 32)
XCTAssertEqual(base.count, 32)
}
func testCountInitializerGeneratesSomewhatRandomData() {
let base = SecureBytes(count: 16)
XCTAssertGreaterThanOrEqual(base.backing.allocatedCapacity, 16)
XCTAssertEqual(base.count, 16)
XCTAssertNotEqual(Array(repeating: UInt8(0), count: 16), Array(base))
}
func testBackingBytesAreAppropriatelySized() {
var base = SecureBytes(repeating: 0, count: 10)
XCTAssertGreaterThanOrEqual(base.backing.allocatedCapacity, 16)
base.withUnsafeBytes { XCTAssertEqual($0.count, 10) }
base.withUnsafeMutableBytes { XCTAssertEqual($0.count, 10) }
base.backing._withVeryUnsafeMutableBytes { XCTAssertGreaterThanOrEqual($0.count, 16) }
XCTAssertEqual(base.bytes.byteCount, 10)
XCTAssertEqual(base.mutableBytes.byteCount, 10)
}
func testTheresOnlyOneRegion() {
var base = SecureBytes()
base.append(Data("hello".utf8))
base.append(Data("world".utf8))
XCTAssertEqual(base.regions.count, 1)
}
func testScaryInitializer() {
let base = SecureBytes(unsafeUninitializedCapacity: 5) { (scaryPointer, initializedCapacity) in
XCTAssertEqual(scaryPointer.count, 5)
scaryPointer.storeBytes(of: UInt32(0x01020304).bigEndian, as: UInt32.self)
initializedCapacity = 4
}
XCTAssertGreaterThanOrEqual(base.backing.allocatedCapacity, 8)
XCTAssertEqual(Array(base), [1, 2, 3, 4])
func testThrowingInitialization() throws {
_ = try SecureBytes(unsafeUninitializedCapacity: 5) { (_, _) in
throw CryptoKitError.incorrectKeySize
}
}
XCTAssertThrowsError(try testThrowingInitialization(), error: CryptoKitError.incorrectKeySize)
}
func testAppendingDataPerformsACoW() {
var base = SecureBytes(repeating: 0, count: 10)
let copy = base
base.append(Array("Hello, world".utf8).span.bytes)
XCTAssertEqual(base.count, 22)
XCTAssertEqual(copy.count, 10)
}
func testRequestingAMutablePointerPerformsACoW() {
var base = SecureBytes(repeating: 0, count: 10)
let copy = base
base.withUnsafeMutableBytes {
$0.storeBytes(of: UInt32(0x01020304).bigEndian, toByteOffset: 4, as: UInt32.self)
}
XCTAssertEqual(Array(base), [0, 0, 0, 0, 1, 2, 3, 4, 0, 0])
XCTAssertEqual(Array(copy), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
}
func testDataCausesCoWs() {
var base = SecureBytes(repeating: 0, count: 10)
let copy = Data(base)
XCTAssertEqual(base.count, copy.count)
base.append(Array("Hello, world".utf8).span.bytes)
XCTAssertEqual(base.count, 22)
XCTAssertEqual(copy.count, 10)
}
func testDataFromSlice() {
var base = SecureBytes(0..<10)
let copy = Data(base.prefix(5))
XCTAssertEqual(Array(copy), [0, 1, 2, 3, 4])
base.append(Array("Hello, world".utf8).span.bytes)
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)