Insufficient password hashing iterations in Swift

Insufficient password hashing iterations in Swift

Description

Too few PBKDF2 iterations make leaked password hashes cheaper to guess offline. CryptoSwift's PBKDF1 uses MD5 or SHA-1 and is unsuitable for new password hashes at any iteration count.

Potential impact

  • Lower cost of password cracking.
  • Greater likelihood of recovering passwords from leaked hashes.
  • Increased risk of attacks that reuse compromised credentials.

Remediation

  • For new password storage, prefer Argon2id or scrypt. Reserve bcrypt for existing systems that need it. Migrate legacy PBKDF1 or PBKDF2-HMAC-SHA1 hashes when the user next signs in successfully.
  • If you use PBKDF2, set at least 600,000 iterations for HMAC-SHA256, 220,000 for HMAC-SHA512, or 1,400,000 for HMAC-SHA1 used for legacy compatibility.
  • Set the count explicitly in CryptoSwift. Enforce the minimum in configuration and review it as server performance and OWASP guidance change.

Examples

Before

swift
import CryptoSwift

func deriveWithTooFewIterations(
    password: [UInt8], salt: [UInt8]
) throws -> [UInt8] {
    return try PKCS5.PBKDF2(
        password: password,
        salt: salt,
        iterations: 90000,
        keyLength: 32,
        variant: .sha2(.sha256)
    ).calculate()
}

func deriveWithObsoletePBKDF1(password: [UInt8]) throws -> [UInt8] {
    let salt = (0..<8).map { _ in UInt8.random(in: 0...UInt8.max) }
    return try PKCS5.PBKDF1(
        password: password,
        salt: salt,
        iterations: 1000,
        keyLength: 16
    ).calculate()
}

After

swift
import CryptoSwift

func derivePasswordKey(password: [UInt8], salt: [UInt8]) throws -> [UInt8] {
    return try PKCS5.PBKDF2(
        password: password,
        salt: salt,
        iterations: 600000,
        keyLength: 32,
        variant: .sha2(.sha256)
    ).calculate()
}

derivePasswordKey uses PBKDF2-HMAC-SHA256 with 600,000 iterations and the supplied salt to derive a 32-byte key.

References