Skip to content

Commit baa05dc

Browse files
authored
Improve Sealed Box construction (#132)
AES.GCM.SealedBox is expensive to construct from parts, in part because it's not inlinable. We can construct the combined representation directly and improve performance substantially by replacing 8 allocations with 3.
1 parent 3213dc6 commit baa05dc

File tree

6 files changed

+31
-16
lines changed

6 files changed

+31
-16
lines changed

Sources/NIOSSH/TransportProtection/AESGCM.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extension AESGCMTransportProtection: NIOSSHTransportProtection {
9999
}
100100

101101
// Ok, let's try to decrypt this data.
102-
let sealedBox = try AES.GCM.SealedBox(nonce: AES.GCM.Nonce(data: self.inboundNonce), ciphertext: ciphertextView, tag: tagView)
102+
let sealedBox = try AES.GCM.SealedBox(nonce: self.inboundNonce, ciphertext: ciphertextView, tag: tagView)
103103
plaintext = try AES.GCM.open(sealedBox, using: self.inboundEncryptionKey, authenticating: lengthView)
104104

105105
// All good! A quick soundness check to verify that the length of the plaintext is ok.
@@ -342,3 +342,18 @@ extension Data {
342342
self = self[contentStartIndex ..< contentEndIndex]
343343
}
344344
}
345+
346+
extension AES.GCM.SealedBox {
347+
fileprivate init(nonce: SSHAESGCMNonce, ciphertext: ByteBufferView, tag: ByteBufferView) throws {
348+
// As a workaround for a Swift Crypto inefficiency, we create the combined representation
349+
// directly.
350+
var combined: [UInt8] = []
351+
combined.reserveCapacity(nonce.count + ciphertext.count + tag.count)
352+
353+
combined.append(contentsOf: nonce)
354+
combined.append(contentsOf: ciphertext)
355+
combined.append(contentsOf: tag)
356+
357+
try self.init(combined: combined)
358+
}
359+
}

docker/docker-compose.2004.55.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ services:
1212
test:
1313
image: swift-nio-ssh:20.04-5.5
1414
environment:
15-
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=270900
16-
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1158050
17-
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=65150
15+
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=240900
16+
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1108050
17+
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=55100
1818
#- SANITIZER_ARG=--sanitize=thread
1919
#- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
2020

docker/docker-compose.2004.56.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ services:
1212
test:
1313
image: swift-nio-ssh:20.04-5.6
1414
environment:
15-
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=267850
16-
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1100050
17-
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=65100
15+
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=237850
16+
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1050050
17+
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=55050
1818
#- SANITIZER_ARG=--sanitize=thread
1919
#- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
2020

docker/docker-compose.2204.57.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ services:
1212
test:
1313
image: swift-nio-ssh:22.04-5.7
1414
environment:
15-
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=255850
16-
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1068050
17-
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=61050
15+
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=225800
16+
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1018050
17+
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=51000
1818
#- SANITIZER_ARG=--sanitize=thread
1919
#- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
2020

docker/docker-compose.2204.58.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ services:
1111
test:
1212
image: swift-nio-ssh:22.04-5.8
1313
environment:
14-
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=249850
15-
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1055050
16-
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=59050
14+
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=219800
15+
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1005050
16+
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=49000
1717
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
1818
#- SANITIZER_ARG=--sanitize=thread
1919
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors

docker/docker-compose.2204.main.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ services:
1111
test:
1212
image: swift-nio-ssh:22.04-main
1313
environment:
14-
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=249850
15-
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1055050
16-
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=59050
14+
- MAX_ALLOCS_ALLOWED_client_server_many_small_commands_per_connection=219800
15+
- MAX_ALLOCS_ALLOWED_client_server_one_command_per_connection=1005050
16+
- MAX_ALLOCS_ALLOWED_client_server_streaming_large_message_in_small_chunks=49000
1717
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
1818
#- SANITIZER_ARG=--sanitize=thread
1919
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors

0 commit comments

Comments
 (0)