@@ -292,7 +292,7 @@ internal struct SHA256: SASLAuthenticationMechanism {
292
292
/// authenticating user. If the closure throws, authentication
293
293
/// immediately fails with the thrown error.
294
294
internal init ( username: String , password: @escaping ( ) throws -> String ) {
295
- self . _impl = . init( username: username, passwordGrabber: { _ in try ( Array ( password ( ) . data ( using : . utf8) ! ) , [ ] ) } , bindingInfo: . unsupported)
295
+ self . _impl = . init( username: username, passwordGrabber: { _ in try ( Array ( password ( ) . utf8) , [ ] ) } , bindingInfo: . unsupported)
296
296
}
297
297
298
298
/// Set up a server-side `SCRAM-SHA-256` authentication.
@@ -338,7 +338,7 @@ internal struct SHA256_PLUS: SASLAuthenticationMechanism {
338
338
/// - channelBindingData: The appropriate data associated with the RFC5056
339
339
/// channel binding specified.
340
340
internal init ( username: String , password: @escaping ( ) throws -> String , channelBindingName: String , channelBindingData: [ UInt8 ] ) {
341
- self . _impl = . init( username: username, passwordGrabber: { _ in try ( Array ( password ( ) . data ( using : . utf8) ! ) , [ ] ) } , bindingInfo: . bind( channelBindingName, channelBindingData) )
341
+ self . _impl = . init( username: username, passwordGrabber: { _ in try ( Array ( password ( ) . utf8) , [ ] ) } , bindingInfo: . bind( channelBindingName, channelBindingData) )
342
342
}
343
343
344
344
/// Set up a server-side `SCRAM-SHA-256` authentication.
@@ -467,7 +467,7 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
467
467
468
468
// Calculate `AuthMessage`, `ClientSignature`, and `ClientProof`
469
469
let saltedPassword = Hi ( string: password, salt: serverSalt, iterations: serverIterations)
470
- let clientKey = HMAC< SHA256> . authenticationCode( for: " Client Key " . data ( using : . utf8) ! , using: . init( data: saltedPassword) )
470
+ let clientKey = HMAC< SHA256> . authenticationCode( for: Data ( " Client Key " . utf8) , using: . init( data: saltedPassword) )
471
471
let storedKey = SHA256 . hash ( data: Data ( clientKey) )
472
472
var authMessage = firstMessageBare; authMessage. append ( . comma) ; authMessage. append ( contentsOf: message) ; authMessage. append ( . comma) ; authMessage. append ( contentsOf: clientFinalNoProof)
473
473
let clientSignature = HMAC< SHA256> . authenticationCode( for: authMessage, using: . init( data: storedKey) )
@@ -501,7 +501,7 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
501
501
switch incomingAttributes. first {
502
502
case . v( let verifier) :
503
503
// Verify server signature
504
- let serverKey = HMAC< SHA256> . authenticationCode( for: " Server Key " . data ( using : . utf8) ! , using: . init( data: saltedPassword) )
504
+ let serverKey = HMAC< SHA256> . authenticationCode( for: Data ( " Server Key " . utf8) , using: . init( data: saltedPassword) )
505
505
let serverSignature = HMAC< SHA256> . authenticationCode( for: authMessage, using: . init( data: serverKey) )
506
506
507
507
guard Array ( serverSignature) == verifier else {
@@ -585,7 +585,7 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
585
585
guard nonce == repeatNonce else { throw SASLAuthenticationError . genericAuthenticationFailure }
586
586
587
587
// Compute client signature
588
- let clientKey = HMAC< SHA256> . authenticationCode( for: " Client Key " . data ( using : . utf8) ! , using: . init( data: saltedPassword) )
588
+ let clientKey = HMAC< SHA256> . authenticationCode( for: Data ( " Client Key " . utf8) , using: . init( data: saltedPassword) )
589
589
let storedKey = SHA256 . hash ( data: Data ( clientKey) )
590
590
var authMessage = clientBareFirstMessage; authMessage. append ( . comma) ; authMessage. append ( contentsOf: serverFirstMessage) ; authMessage. append ( . comma) ; authMessage. append ( contentsOf: message. dropLast ( proof. count + 3 ) )
591
591
let clientSignature = HMAC< SHA256> . authenticationCode( for: authMessage, using: . init( data: storedKey) )
@@ -604,7 +604,7 @@ fileprivate final class SASLMechanism_SCRAM_SHA256_Common {
604
604
guard storedKey == restoredKey else { throw SCRAMServerError . invalidProof }
605
605
606
606
// Compute server signature
607
- let serverKey = HMAC< SHA256> . authenticationCode( for: " Server Key " . data ( using : . utf8) ! , using: . init( data: saltedPassword) )
607
+ let serverKey = HMAC< SHA256> . authenticationCode( for: Data ( " Server Key " . utf8) , using: . init( data: saltedPassword) )
608
608
let serverSignature = HMAC< SHA256> . authenticationCode( for: authMessage, using: . init( data: serverKey) )
609
609
610
610
// Generate a `server-final-message`
@@ -644,11 +644,16 @@ private func Hi(string: [UInt8], salt: [UInt8], iterations: UInt32) -> [UInt8] {
644
644
let key = SymmetricKey ( data: string)
645
645
var Ui = HMAC< SHA256> . authenticationCode( for: salt + [ 0x00 , 0x00 , 0x00 , 0x01 ] , using: key) // salt + 0x00000001 as big-endian
646
646
var Hi = Array ( Ui)
647
-
647
+ var uiData = [ UInt8] ( )
648
+ uiData. reserveCapacity ( 32 )
649
+
648
650
Hi . withUnsafeMutableBytes { Hibuf -> Void in
649
651
for _ in 2 ... iterations {
650
- Ui = HMAC< SHA256> . authenticationCode( for: Data ( Ui) , using: key)
651
-
652
+ uiData. removeAll ( keepingCapacity: true )
653
+ uiData. append ( contentsOf: Ui)
654
+
655
+ Ui = HMAC< SHA256> . authenticationCode( for: uiData, using: key)
656
+
652
657
Ui . withUnsafeBytes { Uibuf -> Void in
653
658
for i in 0 ..< Uibuf . count { Hibuf [ i] ^= Uibuf [ i] }
654
659
}
0 commit comments