Fix pointers to local buffers

This commit is contained in:
Davide De Rosa 2020-04-05 17:08:23 +02:00
parent a02857fdb9
commit deff855bbc
4 changed files with 21 additions and 6 deletions

View File

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Index out of range during negotiation (Grivus). [#143](https://github.com/passepartoutvpn/tunnelkit/pull/143)
- Handle server shutdown/restart (remote `--explicit-exit-notify`). [#131](https://github.com/passepartoutvpn/tunnelkit/issues/131)
- Pointer warnings from Xcode 11.4 upgrade.
## 2.2.1 (2019-12-14)

View File

@ -85,19 +85,25 @@ extension Data {
extension Data {
mutating func append(_ value: UInt16) {
var localValue = value
let buffer = UnsafeBufferPointer(start: &localValue, count: 1)
let buffer = withUnsafePointer(to: &localValue) {
return UnsafeBufferPointer(start: $0, count: 1)
}
append(buffer)
}
mutating func append(_ value: UInt32) {
var localValue = value
let buffer = UnsafeBufferPointer(start: &localValue, count: 1)
let buffer = withUnsafePointer(to: &localValue) {
return UnsafeBufferPointer(start: $0, count: 1)
}
append(buffer)
}
mutating func append(_ value: UInt64) {
var localValue = value
let buffer = UnsafeBufferPointer(start: &localValue, count: 1)
let buffer = withUnsafePointer(to: &localValue) {
return UnsafeBufferPointer(start: $0, count: 1)
}
append(buffer)
}

View File

@ -81,7 +81,9 @@ class EncryptionPerformanceTests: XCTestCase {
func testGCMEncryption() {
let suite = TestUtils.generateDataSuite(1000, 100000)
let ad: [UInt8] = [0x11, 0x22, 0x33, 0x44]
var flags = CryptoFlags(iv: nil, ivLength: 0, ad: ad, adLength: ad.count)
var flags = ad.withUnsafeBufferPointer {
return CryptoFlags(iv: nil, ivLength: 0, ad: $0.baseAddress, adLength: ad.count)
}
measure {
for data in suite {
let _ = try! self.gcmEncrypter.encryptData(data, flags: &flags)

View File

@ -81,7 +81,11 @@ class EncryptionTests: XCTestCase {
let packetId: [UInt8] = [0x56, 0x34, 0x12, 0x00]
let ad: [UInt8] = [0x00, 0x12, 0x34, 0x56]
var flags = CryptoFlags(iv: packetId, ivLength: packetId.count, ad: ad, adLength: ad.count)
var flags = packetId.withUnsafeBufferPointer { (iv) in
return ad.withUnsafeBufferPointer { (ad) in
return CryptoFlags(iv: iv.baseAddress, ivLength: packetId.count, ad: ad.baseAddress, adLength: ad.count)
}
}
let plain = Data(hex: "00112233445566778899")
let encrypted = try! client.encrypter().encryptData(plain, flags: &flags)
let decrypted = try! server.decrypter().decryptData(encrypted, flags: &flags)
@ -93,7 +97,9 @@ class EncryptionTests: XCTestCase {
let original = Data(hex: "0000000000")
let ad: [UInt8] = [UInt8](Data(hex: "38afa8f1162096081e000000015ba35373"))
var flags = CryptoFlags(iv: nil, ivLength: 0, ad: ad, adLength: ad.count)
var flags = ad.withUnsafeBufferPointer {
CryptoFlags(iv: nil, ivLength: 0, ad: $0.baseAddress, adLength: ad.count)
}
// let expEncrypted = Data(hex: "319bb8e7f8f7930cc4625079dd32a6ef9540c2fc001c53f909f712037ae9818af840b88714")
let encrypted = try! client.encrypter().encryptData(original, flags: &flags)