Skip to content
Merged
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion Sources/NIOSSH/TransportProtection/AESGCM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension AESGCMTransportProtection: NIOSSHTransportProtection {
}

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

// All good! A quick soundness check to verify that the length of the plaintext is ok.
Expand Down Expand Up @@ -342,3 +342,18 @@ extension Data {
self = self[contentStartIndex ..< contentEndIndex]
}
}

extension AES.GCM.SealedBox {
fileprivate init(nonce: SSHAESGCMNonce, ciphertext: ByteBufferView, tag: ByteBufferView) throws {
// As a workaround for a Swift Crypto inefficiency, we create the combined representation
// directly.
var combined: [UInt8] = []
combined.reserveCapacity(12 + ciphertext.count + tag.count)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment that 12 is the size of nonce or can we make it a static property on SSHAESGCMNonce?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


combined.append(contentsOf: nonce)
combined.append(contentsOf: ciphertext)
combined.append(contentsOf: tag)

try self.init(combined: combined)
}
}