More native crypto
- Commit
fffdf0dbe7450ac2e79b1251319f168dff532774- Author
- Kabir Oberai <[email protected]>
- When
- 2024-11-16 07:13 UTC (2 years ago)
- Parents
b78eaca
Diff
Supersign/Sources/Supersign/GrandSlam/Crypto/SRPClient.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Crypto/SRPClient.swift b/Supersign/Sources/Supersign/GrandSlam/Crypto/SRPClient.swiftindex 3a91e87..0cccf06 100644--- a/Supersign/Sources/Supersign/GrandSlam/Crypto/SRPClient.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Crypto/SRPClient.swift@@ -9,10 +9,12 @@ import Foundation import CSupersign import Crypto+import _CryptoExtras struct SRPClient: ~Copyable { enum Errors: Error {+ case internalError case notProcessed } @@ -69,35 +71,65 @@ struct SRPClient: ~Copyable { mutating func processChallenge( withUsername username: String,- passkey: Data,+ password: String, salt: Data,- serverPublicKey key: Data- ) -> Data? {- var outResponse: UnsafeMutableRawPointer?- var outHAMK: UnsafeMutableRawPointer?+ iterations: Int,+ isLegacyProtocol: Bool,+ serverPublicKey B: Data+ ) throws -> Data {+ let hashedPassword = Data(SHA256.hash(data: Data(password.utf8)))+ let pbkdfInput = if isLegacyProtocol {+ Data(hashedPassword.map { String(format: "%02hhx", $0) }.joined(separator: "").utf8)+ } else {+ hashedPassword+ }+ let passkey = try KDF.Insecure.PBKDF2.deriveKey(+ from: pbkdfInput,+ salt: salt,+ using: .sha256,+ outputByteCount: SHA256.byteCount,+ unsafeUncheckedRounds: iterations+ ).withUnsafeBytes { Data($0) }++ let x = SHA256.hash(data: salt + SHA256.hash(data: ":".utf8 + passkey))+ var outClientKey: UnsafeMutableRawPointer?- let outLen = passkey.withUnsafeBytes { passkeyBuf in+ var outClientKeyLen = 0+ var outG: UnsafeMutableRawPointer?+ var outGLen = 0+ var outN: UnsafeMutableRawPointer?+ var outNLen = 0+ let success = x.withUnsafeBytes { xBuf in salt.withUnsafeBytes { saltBuf in- key.withUnsafeBytes { keyBuf in+ B.withUnsafeBytes { keyBuf in srp_client_process_challenge( raw,- username,- passkeyBuf.baseAddress!, passkeyBuf.count,- saltBuf.baseAddress, saltBuf.count,+ xBuf.baseAddress!, xBuf.count, keyBuf.baseAddress, keyBuf.count,- &outHAMK,- &outClientKey,- &outResponse+ &outClientKey, &outClientKeyLen,+ &outG, &outGLen,+ &outN, &outNLen ) } } }- guard outLen > 0, let outResponse, let outHAMK, let outClientKey else { return nil }- hamk = SymmetricKey(data: UnsafeRawBufferPointer(start: outHAMK, count: outLen))- free(outHAMK)- clientKey = SymmetricKey(data: UnsafeRawBufferPointer(start: outClientKey, count: outLen))- free(outClientKey)- return Data(bytesNoCopy: outResponse, count: outLen, deallocator: .free)+ guard success else { throw Errors.internalError }++ let rawK = Data(bytesNoCopy: outClientKey!, count: outClientKeyLen, deallocator: .free)+ let K = Data(SHA256.hash(data: rawK))+ let g = Data(bytesNoCopy: outG!, count: outGLen, deallocator: .free)+ let N = Data(bytesNoCopy: outN!, count: outNLen, deallocator: .free)+ clientKey = SymmetricKey(data: K)++ let gHash = SHA256.hash(data: Array(repeating: 0, count: SHA256.byteCount * 8 - g.count) + g)+ let NHash = SHA256.hash(data: N)+ let xorHash = zip(gHash, NHash).map { $0 ^ $1 }+ let HI = SHA256.hash(data: Data(username.utf8))+ let A = publicKey()++ let M = Data(SHA256.hash(data: xorHash + Data(HI) + salt + A + B + K))+ hamk = SymmetricKey(data: SHA256.hash(data: A + M + K))+ return M } }
Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swiftindex 590819d..365d016 100644--- a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift@@ -44,9 +44,6 @@ class GrandSlamAuthenticateOperation { } } - private var srpClient = SRPClient()- private var isSecondAttempt = false- private let decoder = PropertyListDecoder() let client: GrandSlamClient@@ -66,17 +63,10 @@ class GrandSlamAuthenticateOperation { self.twoFactorDelegate = twoFactorDelegate } - private func restartAuth() async throws -> GrandSlamLoginData {- srpClient = .init()- isSecondAttempt = true- return try await authenticate()- }- private func authenticateTwoFactor( mode: GrandSlamAuthMode?, loginData: GrandSlamLoginData ) async throws -> GrandSlamLoginData {- guard !isSecondAttempt else { throw Error.failedSecondaryAuth } let operation = GrandSlamTwoFactorAuthenticateOperation( client: client, mode: mode,@@ -84,12 +74,36 @@ class GrandSlamAuthenticateOperation { delegate: twoFactorDelegate ) try await operation.perform()- return try await self.restartAuth()+ return try await authenticate(isRetry: true) } - private func authenticateStage3(- completeResponse: GrandSlamAuthCompleteRequest.Decoder.Value- ) async throws -> GrandSlamLoginData {+ private func authenticate(isRetry: Bool) async throws -> GrandSlamLoginData {+ var srpClient = SRPClient()++ srpClient.add(string: GrandSlamAuthInitRequest.protocols.joined(separator: ","))+ srpClient.add(string: "|")++ let publicKey = srpClient.publicKey()+ let initRequest = GrandSlamAuthInitRequest(username: username, publicKey: publicKey)+ let initResponse = try await client.send(initRequest)++ srpClient.add(string: "|")+ srpClient.add(string: initResponse.selectedProtocol.rawValue)++ let m1Data = try srpClient.processChallenge(+ withUsername: username,+ password: password,+ salt: initResponse.salt,+ iterations: initResponse.iterations,+ isLegacyProtocol: initResponse.selectedProtocol == .s2k_fo,+ serverPublicKey: initResponse.serverPublicKey+ )++ let completeRequest = GrandSlamAuthCompleteRequest(+ username: username, cookie: initResponse.cookie, m1Data: m1Data+ )+ let completeResponse = try await client.send(completeRequest)+ srpClient.add(string: "|") srpClient.add(data: completeResponse.encryptedResponse) srpClient.add(string: "|")@@ -107,6 +121,7 @@ class GrandSlamAuthenticateOperation { if response.statusCode == 409 { // 2FA+ guard !isRetry else { throw Error.failedSecondaryAuth } let authMode = response.url.flatMap { GrandSlamAuthMode(rawValue: $0) } return try await authenticateTwoFactor( mode: authMode,@@ -117,51 +132,8 @@ class GrandSlamAuthenticateOperation { return loginData } - private func authenticateStage2(- initResponse: GrandSlamAuthInitRequest.Decoder.Value- ) async throws -> GrandSlamLoginData {- srpClient.add(string: "|")- srpClient.add(string: initResponse.selectedProtocol.rawValue)-- let hashedPassword = Data(SHA256.hash(data: Data(password.utf8)))- let pbkdfInput = switch initResponse.selectedProtocol {- case .s2k:- hashedPassword- case .s2k_fo:- Data(hashedPassword.map { String(format: "%02hhx", $0) }.joined(separator: "").utf8)- }- let passkey = try KDF.Insecure.PBKDF2.deriveKey(- from: pbkdfInput,- salt: initResponse.salt,- using: .sha256,- outputByteCount: SHA256.byteCount,- unsafeUncheckedRounds: initResponse.iterations- )-- let mDataRaw = srpClient.processChallenge(- withUsername: username,- passkey: passkey.withUnsafeBytes { Data($0) },- salt: initResponse.salt,- serverPublicKey: initResponse.serverPublicKey- )- guard let m1Data = mDataRaw else { throw Error.internalError }-- let completeRequest = GrandSlamAuthCompleteRequest(- username: username, cookie: initResponse.cookie, m1Data: m1Data- )- let response = try await client.send(completeRequest)- return try await self.authenticateStage3(completeResponse: response)- }- func authenticate() async throws -> GrandSlamLoginData {- srpClient.add(string: GrandSlamAuthInitRequest.protocols.joined(separator: ","))- srpClient.add(string: "|")-- let publicKey = srpClient.publicKey()-- let initRequest = GrandSlamAuthInitRequest(username: username, publicKey: publicKey)- let response = try await client.send(initRequest)- return try await self.authenticateStage2(initResponse: response)+ try await authenticate(isRetry: false) } }