Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ var targetDependencies = [PackageDescription.Target.Dependency]()
var settings: [SwiftSetting]? = [
// Add build settings here
.enableExperimentalFeature("Lifetimes"),
.strictMemorySafety(),
.treatWarning("StrictMemorySafety", as: .error),
]

#if os(Linux)
Expand Down
12 changes: 6 additions & 6 deletions Sources/ArraySpanUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ extension Array where Element == UInt8 {
///
/// The span must contain exactly `count` bytes.
init(copying bytes: RawSpan) {
self.init(unsafeUninitializedCapacity: bytes.byteCount) { outputBuffer, initializedCount in
unsafe self.init(unsafeUninitializedCapacity: bytes.byteCount) { outputBuffer, initializedCount in
bytes.withUnsafeBytes { inputBuffer in
UnsafeMutableRawBufferPointer(outputBuffer).copyMemory(from: inputBuffer)
unsafe UnsafeMutableRawBufferPointer(outputBuffer).copyMemory(from: inputBuffer)
}
initializedCount = bytes.byteCount
}
Expand All @@ -38,7 +38,7 @@ extension InlineArray where Element == UInt8 {
precondition(count == bytes.byteCount)
self.init { outputSpan in
for i in 0..<count {
outputSpan.append(bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
outputSpan.append(unsafe bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
}
}
}
Expand All @@ -49,7 +49,7 @@ extension InlineArray where Element == UInt8 {
extension Hasher {
mutating func combine(bytes: RawSpan) {
bytes.withUnsafeBytes { buffer in
self.combine(bytes: buffer)
unsafe self.combine(bytes: buffer)
}
}
}
Expand All @@ -74,7 +74,7 @@ extension OutputRawSpan {
/// Appends the contents of the given raw span to this output span.
mutating func append(contentsOf bytes: RawSpan) {
for i in 0..<bytes.byteCount {
append(bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
append(unsafe bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
}
}
}
Expand All @@ -83,6 +83,6 @@ extension OutputRawSpan {
@available(SwiftTLS 0.1.0, *)
extension RawSpan {
subscript(index: Int) -> UInt8 {
unsafeLoad(fromByteOffset: index, as: UInt8.self)
unsafe unsafeLoad(fromByteOffset: index, as: UInt8.self)
}
}
8 changes: 4 additions & 4 deletions Sources/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct ByteBuffer {
var networkByteOrder = integer.bigEndian
withUnsafeBytes(of: &networkByteOrder) {
precondition($0.count == byteWidth)
self.backingData.append(contentsOf: $0)
unsafe self.backingData.append(contentsOf: $0)
}

return byteWidth
Expand All @@ -108,7 +108,7 @@ struct ByteBuffer {
var networkByteOrder = integer.bigEndian
withUnsafeBytes(of: &networkByteOrder) {
precondition($0.count == byteWidth)
self.backingData.replaceSubrange(index..<endIndex, with: $0)
unsafe self.backingData.replaceSubrange(index..<endIndex, with: $0)
}

return byteWidth
Expand All @@ -128,10 +128,10 @@ struct ByteBuffer {

_ = withUnsafeMutableBytes(of: &value) {
#if canImport(Foundation) && !SWIFTTLS_EMBEDDED
self.backingData.copyBytes(to: $0, from: self.readerIndex..<endIndex)
unsafe self.backingData.copyBytes(to: $0, from: self.readerIndex..<endIndex)
#else
// SwiftSystem Data is missing copyBytes(to: from:)
$0.copyBytes(from: self.backingData[self.readerIndex..<endIndex])
unsafe $0.copyBytes(from: self.backingData[self.readerIndex..<endIndex])
#endif
}
return IntegerType(bigEndian: value)
Expand Down
12 changes: 6 additions & 6 deletions Sources/Cryptography/HKDF+Label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

// Availability due to `CryptoKit`'s `HKDF`
Expand Down Expand Up @@ -48,9 +48,9 @@ extension HKDF {
serializedLabel.append(contentsOf: "tls13 ".utf8)
serializedLabel.append(contentsOf: label.utf8)

context.withUnsafeBytes { contextPointer in
unsafe context.withUnsafeBytes { contextPointer in
serializedLabel.append(UInt8(contextPointer.count))
serializedLabel.append(contentsOf: contextPointer)
unsafe serializedLabel.append(contentsOf: contextPointer)
}

return Self.expand(pseudoRandomKey: secret, info: serializedLabel, outputByteCount: length)
Expand All @@ -67,11 +67,11 @@ extension HKDF {

static func extract(inputKeyMaterial ikm: SymmetricKey, salt: SymmetricKey) -> HashedAuthenticationCode<H> {
// This wrapper helps us deal with the fact that these types don't neatly fit into what CryptoKit wants.
return salt.withUnsafeBytes { saltPointer in
return unsafe salt.withUnsafeBytes { saltPointer in
#if SWIFTTLS_EXCLAVEKIT
Self.extract(inputKeyMaterial: ikm, salt: Data(saltPointer))
unsafe Self.extract(inputKeyMaterial: ikm, salt: Data(saltPointer))
#else
Self.extract(inputKeyMaterial: ikm, salt: saltPointer)
unsafe Self.extract(inputKeyMaterial: ikm, salt: saltPointer)
#endif
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Cryptography/HashFunction+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

// Availability due to `CryptoKit`'s `HashFunction`
Expand All @@ -33,8 +33,8 @@ extension HashFunction {
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension HMAC {
static func authenticationCode<Bytes: ContiguousBytes>(bytes: Bytes, using key: SymmetricKey) -> HashedAuthenticationCode<H> {
return bytes.withUnsafeBytes {
Self.authenticationCode(for: $0, using: key)
return unsafe bytes.withUnsafeBytes {
unsafe Self.authenticationCode(for: $0, using: key)
}
}
}
8 changes: 4 additions & 4 deletions Sources/Cryptography/KeyScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif
#if canImport(Darwin) || SWIFTTLS_EXCLAVEKIT
import os.log
Expand Down Expand Up @@ -518,7 +518,7 @@ fileprivate struct SessionKeyManager<HF: HashFunction> {
var transcriptHasher = HF()
transcriptHasher.update(data: transcript.readableBytesView)
let hash = transcriptHasher.finalize()
hash.withUnsafeBytes { _ = buffer.writeBytes($0) }
unsafe hash.withUnsafeBytes { _ = unsafe buffer.writeBytes($0) }
return buffer
}

Expand Down Expand Up @@ -893,7 +893,7 @@ extension SessionKeyManager.State {
buffer.writeInteger(UInt8(0))

let hash = self.transcriptHasher.finalize()
hash.withUnsafeBytes { _ = buffer.writeBytes($0) }
unsafe hash.withUnsafeBytes { _ = unsafe buffer.writeBytes($0) }
return buffer
}
}
Expand Down Expand Up @@ -956,7 +956,7 @@ extension SessionKeyManager.State {
buffer.writeInteger(UInt8(0))

let hash = self.transcriptHasher.finalize()
hash.withUnsafeBytes { _ = buffer.writeBytes($0) }
unsafe hash.withUnsafeBytes { _ = unsafe buffer.writeBytes($0) }
return buffer
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Cryptography/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

/// A generic wrapper over the supported private-key types.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Cryptography/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

/// An opaque wrapper around the supported public-key types.
Expand Down
23 changes: 12 additions & 11 deletions Sources/DataUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension Data {
self = Data()
} else {
self = bytes.withUnsafeBytes { buffer in
Data(
unsafe Data(
UnsafeBufferPointer<UInt8>(
start: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
count: buffer.count
Expand All @@ -38,17 +38,17 @@ extension Data {
/// Appends the contents of the given span to this `Data` instance.
mutating func append(contentsOf bytes: RawSpan) {
bytes.withUnsafeBytes { buffer in
self.append(contentsOf: buffer)
unsafe self.append(contentsOf: buffer)
}
}

/// Calls `body` with a `RawSpan` describing the bytes.
///
/// Used as a workaround for SwiftSystem's `Data` missing the `bytes` property.
func withBytes<R, E: Error>(_ body: (RawSpan) throws(E) -> R) throws(E) -> R {
let result: Result<R, E> = self.withUnsafeBytes { bufferPointer in
let result: Result<R, E> = unsafe self.withUnsafeBytes { bufferPointer in
do throws(E) {
return .success(try body(bufferPointer.bytes))
return .success(try body(unsafe bufferPointer.bytes))
} catch {
return .failure(error)
}
Expand All @@ -62,33 +62,34 @@ extension Data {
//
// Use this wrapper to turn an UnsafeRawBufferPointer into something that conforms to
// DataProtocol. This whole type should go away once the issue is fixed.
struct UnsafeRawBufferPointerWrapper: DataProtocol, RandomAccessCollection, ContiguousBytes {
@unsafe
struct UnsafeRawBufferPointerWrapper: @unsafe DataProtocol, @unsafe RandomAccessCollection, ContiguousBytes {
var wrapped: UnsafeRawBufferPointer

typealias Element = UInt8
typealias Index = Int
typealias Regions = CollectionOfOne<UnsafeRawBufferPointerWrapper>

var regions: CollectionOfOne<UnsafeRawBufferPointerWrapper> {
CollectionOfOne(self)
unsafe CollectionOfOne(self)
}

var startIndex: Int { wrapped.startIndex }
var endIndex: Int { wrapped.endIndex }
var startIndex: Int { unsafe wrapped.startIndex }
var endIndex: Int { unsafe wrapped.endIndex }

subscript(index: Int) -> UInt8 {
get {
wrapped[index]
unsafe wrapped[index]
}
}

#if $Embedded
func withUnsafeBytes<R, E>(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R {
try wrapped.withUnsafeBytes(body)
try unsafe wrapped.withUnsafeBytes(body)
}
#else
func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
try wrapped.withUnsafeBytes(body)
try unsafe wrapped.withUnsafeBytes(body)
}
#endif
}
2 changes: 1 addition & 1 deletion Sources/Handshake/ExternalPreSharedKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

// Availability due to `RawSpan`
Expand Down
2 changes: 1 addition & 1 deletion Sources/Handshake/HandshakeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if canImport(Darwin) || SWIFTTLS_EXCLAVEKIT
Expand Down
2 changes: 1 addition & 1 deletion Sources/Handshake/HandshakeStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if canImport(Darwin) || SWIFTTLS_EXCLAVEKIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if canImport(Darwin) || SWIFTTLS_EXCLAVEKIT
Expand Down
2 changes: 1 addition & 1 deletion Sources/Handshake/PartialHandshakeResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

// Availability due to `RawSpan`
Expand Down
4 changes: 2 additions & 2 deletions Sources/Handshake/PeerCertificateBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if canImport(Darwin) || SWIFTTLS_EXCLAVEKIT
Expand Down Expand Up @@ -308,7 +308,7 @@ extension InputBuffer {
case 0:
let key = try TLSError.wrappingCryptoError { () throws(CryptoKitMetaError) in try self.readLengthPrefixed { buffer throws(CryptoKitMetaError) in
try buffer.bytes.withUnsafeBytes { (bytes) throws(CryptoKitMetaError) in
try P256.Signing.PublicKey(rawRepresentation: bytes)
try unsafe P256.Signing.PublicKey(rawRepresentation: bytes)
}
} }
guard let key = key else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Handshake/ServerHandshakeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if !SWIFTTLS_CLIENT_ONLY
Expand Down
2 changes: 1 addition & 1 deletion Sources/Handshake/ServerHandshakeStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if !SWIFTTLS_CLIENT_ONLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if canImport(Foundation) && !SWIFTTLS_EMBEDDED
Expand Down
6 changes: 3 additions & 3 deletions Sources/Handshake/SessionTicket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
#if canImport(CryptoKit)
import CryptoKit
#elseif canImport(Crypto)
@preconcurrency import Crypto
@preconcurrency @unsafe import Crypto
#endif

#if canImport(Darwin) || SWIFTTLS_EXCLAVEKIT
Expand Down Expand Up @@ -126,8 +126,8 @@ struct SessionTicket {
buffer.writeLengthPrefixedImmutableBuffer(self.nonce)
buffer.writeLengthPrefixedImmutableBuffer(self.ticket)

self.psk.withUnsafeBytes {
buffer.writeLengthPrefixedBytes($0)
unsafe self.psk.withUnsafeBytes {
unsafe buffer.writeLengthPrefixedBytes($0)
}

buffer.writeInteger(self.maxEarlyDataSize)
Expand Down
6 changes: 3 additions & 3 deletions Sources/InputBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ extension InputBuffer {
// Copy those bytes into a temporary value.
let align = MemoryLayout<Value>.alignment
return withUnsafeTemporaryAllocation(byteCount: byteCount, alignment: align) { valueBuffer in
var outputSpan = OutputRawSpan(
var outputSpan = unsafe OutputRawSpan(
buffer: valueBuffer,
initializedCount: 0
)
bytes.copy(to: &outputSpan)
let bytesWritten = outputSpan.finalize(for: valueBuffer)
let bytesWritten = unsafe outputSpan.finalize(for: valueBuffer)
precondition(bytesWritten == byteCount)
return valueBuffer.load(as: Value.self)
return unsafe valueBuffer.load(as: Value.self)
}
}

Expand Down
Loading
Loading