Skip to content

Commit

Permalink
Fix SRP Login
Browse files Browse the repository at this point in the history
- Derive key using SHA25(plain-text-password) via PBKDF2
- Pad g to size N while calculating M
- Handle password sent as base64 encoded data and account for
  absence of username
  • Loading branch information
abiligiri committed Oct 28, 2024
1 parent 2ed84ef commit 442c91a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Xcodes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 28;
DEVELOPMENT_ASSET_PATHS = "\"Xcodes/Preview Content\"";
DEVELOPMENT_TEAM = ZU6GR6B2FY;
DEVELOPMENT_TEAM = GNNPCAAT48;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = Xcodes/Resources/Info.plist;
Expand Down
28 changes: 16 additions & 12 deletions Xcodes/AppleAPI/Sources/AppleAPI/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class Client {
}

// let m1 = try client.processChallenge(salt: decodedSalt, publicKey: decodedB, isEncryptedPassword: true, encryptedPassword: encryptedPassword.hexEncodedString())
let encryptedPasswordString = String(data: encryptedPassword, encoding: .utf8)
let encryptedPasswordString = encryptedPassword.base64EncodedString()
let m1 = try client.processChallenge(salt: decodedSalt, publicKey: decodedB, isEncryptedPassword: true, encryptedPassword: encryptedPasswordString)

guard let m2 = client.HAMK else {
Expand Down Expand Up @@ -385,24 +385,28 @@ public class Client {

private func pbkdf2(password: String, saltData: Data, keyByteCount: Int, prf: CCPseudoRandomAlgorithm, rounds: Int) -> Data? {
guard let passwordData = password.data(using: .utf8) else { return nil }

let hashedPassword = SHA256.hash(data: passwordData)

var derivedKeyData = Data(repeating: 0, count: keyByteCount)
let derivedCount = derivedKeyData.count
let derivationStatus: Int32 = derivedKeyData.withUnsafeMutableBytes { derivedKeyBytes in
let keyBuffer: UnsafeMutablePointer<UInt8> =
derivedKeyBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)
return saltData.withUnsafeBytes { saltBytes -> Int32 in
let saltBuffer: UnsafePointer<UInt8> = saltBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)
return CCKeyDerivationPBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
password,
passwordData.count,
saltBuffer,
saltData.count,
prf,
UInt32(rounds),
keyBuffer,
derivedCount)
return hashedPassword.withUnsafeBytes { passwordBytes -> Int32 in
let passwordBuffer: UnsafePointer<UInt8> = passwordBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)
return CCKeyDerivationPBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
passwordBuffer,
passwordBytes.count,
saltBuffer,
saltData.count,
prf,
UInt32(rounds),
keyBuffer,
derivedCount)
}
}
}
return derivationStatus == kCCSuccess ? derivedKeyData : nil
Expand Down
13 changes: 10 additions & 3 deletions xcodes-srp/Sources/SRP/srp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ enum Implementation<HF: HashFunction> {

//M1 = H(H(N) XOR H(g) | H(I) | s | A | B | K)
static func calculate_M(group: Group, username: String, salt: Data, A: Data, B: Data, K: Data) -> Data {
let HN_xor_Hg = (H(group.N.serialize()) ^ H(group.g.serialize()))!
let serializedN = group.N.serialize()
let sizeN = serializedN.count
let HN_xor_Hg = (H(serializedN) ^ H(pad(group.g.serialize(), to: sizeN)))!
let HI = H(username.data(using: .utf8)!)
return H(HN_xor_Hg + HI + salt + A + B + K)
}
Expand All @@ -111,7 +113,12 @@ enum Implementation<HF: HashFunction> {

//x = H(s | H(I | ":" | P))
static func calculate_x(salt: Data, username: String, password: String) -> BigUInt {
return BigUInt(H(salt + H("\(username):\(password)".data(using: .utf8)!)))
if username.count > 0 {
return BigUInt(H(salt + H("\(username):\(password)".data(using: .utf8)!)))
}

let passwordData = Data(base64Encoded: password.data(using: .utf8)!)!
return BigUInt(H(salt + H(Data([0x3A]) + passwordData)))
}
}

Expand All @@ -122,4 +129,4 @@ func calculate_v(group: Group, x: BigUInt) -> BigUInt {

func randomBytes(_ count: Int) -> Data {
return Data((0..<count).map { _ in UInt8.random(in: 0...255) })
}
}

0 comments on commit 442c91a

Please sign in to comment.