Skip to content

Make SASL really fast #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
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
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let package = Package(
.target(name: "_ConnectionPoolModule"),
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "_CryptoExtras", package: "swift-crypto"),
.product(name: "Logging", package: "swift-log"),
.product(name: "Metrics", package: "swift-metrics"),
.product(name: "NIO", package: "swift-nio"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Crypto
import _CryptoExtras
import Foundation

extension UInt8 {
Expand Down Expand Up @@ -466,8 +467,8 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
// TODO: Perform `Normalize(password)`, aka the SASLprep profile (RFC4013) of stringprep (RFC3454)

// Calculate `AuthMessage`, `ClientSignature`, and `ClientProof`
let saltedPassword = Hi(string: password, salt: serverSalt, iterations: serverIterations)
let clientKey = HMAC<SHA256>.authenticationCode(for: Data("Client Key".utf8), using: .init(data: saltedPassword))
let saltedPassword = try Hi(string: password, salt: serverSalt, iterations: serverIterations)
let clientKey = HMAC<SHA256>.authenticationCode(for: Data("Client Key".utf8), using: saltedPassword)
let storedKey = SHA256.hash(data: Data(clientKey))
var authMessage = firstMessageBare; authMessage.append(.comma); authMessage.append(contentsOf: message); authMessage.append(.comma); authMessage.append(contentsOf: clientFinalNoProof)
let clientSignature = HMAC<SHA256>.authenticationCode(for: authMessage, using: .init(data: storedKey))
Expand All @@ -485,9 +486,11 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
var clientFinalMessage = clientFinalNoProof; clientFinalMessage.append(.comma)
guard let proofPart = SCRAMMessageParser.serialize([.p(Array(clientProof))]) else { throw SASLAuthenticationError.genericAuthenticationFailure }
clientFinalMessage.append(contentsOf: proofPart)


let saltedPasswordBytes = saltedPassword.withUnsafeBytes { [UInt8]($0) }

// Save state and send
self.state = .clientSentFinalMessage(saltedPassword: saltedPassword, authMessage: authMessage)
self.state = .clientSentFinalMessage(saltedPassword: saltedPasswordBytes, authMessage: authMessage)
return .continue(response: clientFinalMessage)
}

Expand Down Expand Up @@ -640,24 +643,12 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
HMAC() == output length of H().
````
*/
private func Hi(string: [UInt8], salt: [UInt8], iterations: UInt32) -> [UInt8] {
let key = SymmetricKey(data: string)
var Ui = HMAC<SHA256>.authenticationCode(for: salt + [0x00, 0x00, 0x00, 0x01], using: key) // salt + 0x00000001 as big-endian
var Hi = Array(Ui)
var uiData = [UInt8]()
uiData.reserveCapacity(32)

Hi.withUnsafeMutableBytes { Hibuf -> Void in
for _ in 2...iterations {
uiData.removeAll(keepingCapacity: true)
uiData.append(contentsOf: Ui)

Ui = HMAC<SHA256>.authenticationCode(for: uiData, using: key)

Ui.withUnsafeBytes { Uibuf -> Void in
for i in 0..<Uibuf.count { Hibuf[i] ^= Uibuf[i] }
}
}
}
return Hi
private func Hi(string: [UInt8], salt: [UInt8], iterations: UInt32) throws -> SymmetricKey {
try KDF.Insecure.PBKDF2.deriveKey(
from: string,
salt: salt,
using: .sha256,
outputByteCount: 32,
unsafeUncheckedRounds: Int(iterations)
)
}
Loading