diff --git a/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift b/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift index c6e2972a..1f000ec7 100644 --- a/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift +++ b/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift @@ -85,9 +85,21 @@ internal struct BoringSSLScrypt { var derivedKeyData = Data(count: outputByteCount) defer { derivedKeyData.withUnsafeMutableBytes { $0.zeroize() } } - // This computes the maximum amount of memory that will be used by the scrypt algorithm with an additional memory page to spare. This value will be used by the BoringSSL as the memory limit for the algorithm. - // An additional memory page is added to the computed value (using POSIX specification) to ensure that the memory limit is not too tight. - let maxMemory = maxMemory ?? (128 * rounds * blockSize * parallelism + getPageSize()) + let memoryLimit: Int + if let maxMemory { + memoryLimit = maxMemory + } else { + // This computes the maximum amount of memory that will be used by the scrypt algorithm with an additional memory page to spare. This value will be used by the BoringSSL as the memory limit for the algorithm. + // An additional memory page is added to the computed value (using POSIX specification) to ensure that the memory limit is not too tight. + let (blockBytes, blockBytesOverflowed) = 128.multipliedReportingOverflow(by: blockSize) + let (laneBytes, laneBytesOverflowed) = blockBytes.multipliedReportingOverflow(by: rounds) + let (stateBytes, stateBytesOverflowed) = laneBytes.multipliedReportingOverflow(by: parallelism) + let (computedLimit, computedLimitOverflowed) = stateBytes.addingReportingOverflow(getPageSize()) + guard !blockBytesOverflowed, !laneBytesOverflowed, !stateBytesOverflowed, !computedLimitOverflowed else { + throw CryptoKitError.incorrectParameterSize + } + memoryLimit = computedLimit + } let result = derivedKeyData.withUnsafeMutableBytes { derivedKeyBytes -> Int32 in let saltBytes: ContiguousBytes = salt.regions.count == 1 ? salt.regions.first! : Array(salt) @@ -103,7 +115,7 @@ internal struct BoringSSLScrypt { UInt64(rounds), UInt64(blockSize), UInt64(parallelism), - maxMemory, + memoryLimit, derivedKeyBytes.baseAddress!, derivedKeyBytes.count ) diff --git a/Sources/CryptoExtras/Key Derivation/Scrypt/Scrypt.swift b/Sources/CryptoExtras/Key Derivation/Scrypt/Scrypt.swift index e0bc93b8..57739b0e 100644 --- a/Sources/CryptoExtras/Key Derivation/Scrypt/Scrypt.swift +++ b/Sources/CryptoExtras/Key Derivation/Scrypt/Scrypt.swift @@ -35,10 +35,16 @@ extension KDF { /// - rounds: The number of rounds which should be used to perform key derivation. Must be a power of 2 less than `2^(128 * blockSize / 8)`. /// - blockSize: The block size to use for key derivation. /// - parallelism: The parallelism factor to use for key derivation. Must be a positive integer less than or equal to `((2^32 - 1) * 32) / (128 * blockSize)`. - /// - maxMemory: The maximum amount of memory allowed to use for key derivation. If not provided, the default value is computed for the provided parameters. + /// - maxMemory: The maximum amount of memory allowed to use for key derivation. Must be positive. If not provided, the default value is computed for the provided parameters. /// - Returns: The derived symmetric key. public static func deriveKey(from password: Passphrase, salt: Salt, outputByteCount: Int, rounds: Int, blockSize: Int, parallelism: Int, maxMemory: Int? = nil) throws -> SymmetricKey { - return try BackingScrypt.deriveKey(from: password, salt: salt, outputByteCount: outputByteCount, rounds: rounds, blockSize: blockSize, parallelism: parallelism) + guard rounds > 0, blockSize > 0, parallelism > 0, outputByteCount >= 0 else { + throw CryptoKitError.incorrectParameterSize + } + if let maxMemory, maxMemory <= 0 { + throw CryptoKitError.incorrectParameterSize + } + return try BackingScrypt.deriveKey(from: password, salt: salt, outputByteCount: outputByteCount, rounds: rounds, blockSize: blockSize, parallelism: parallelism, maxMemory: maxMemory) } } } diff --git a/Tests/CryptoExtrasTests/ScryptTests.swift b/Tests/CryptoExtrasTests/ScryptTests.swift index 40308dab..adb85ab4 100644 --- a/Tests/CryptoExtrasTests/ScryptTests.swift +++ b/Tests/CryptoExtrasTests/ScryptTests.swift @@ -84,4 +84,70 @@ class ScryptTests: XCTestCase { try orFail { try self.testRFCVector(vector) } } } + + func testDeriveKeyWithMaxMemoryBelowRequirementThrows() throws { + let password = Array("password".utf8) + let salt = Array("salt".utf8) + + // These parameters need 128 * blockSize * (rounds + parallelism + 1) bytes, just over 1 MiB. + // BoringSSL reads a limit of zero as "use the default limit", and a negative limit as a huge size_t. + for maxMemory in [1024, 0, -1] { + XCTAssertThrowsError( + try KDF.Scrypt.deriveKey( + from: password, + salt: salt, + outputByteCount: 32, + rounds: 1024, + blockSize: 8, + parallelism: 1, + maxMemory: maxMemory + ) + ) + } + + let bounded = try KDF.Scrypt.deriveKey( + from: password, + salt: salt, + outputByteCount: 32, + rounds: 1024, + blockSize: 8, + parallelism: 1, + maxMemory: 2 << 20 + ) + let unbounded = try KDF.Scrypt.deriveKey( + from: password, + salt: salt, + outputByteCount: 32, + rounds: 1024, + blockSize: 8, + parallelism: 1 + ) + XCTAssertEqual(bounded, unbounded) + } + + func testDeriveKeyWithOutOfRangeParametersThrows() throws { + let password = Array("password".utf8) + let salt = Array("salt".utf8) + + for (rounds, blockSize, parallelism, outputByteCount) in [ + // Negative values reach UInt64 conversions and Data(count:), both of which trap. + (-1, 8, 1, 32), + (1024, -1, 1, 32), + (1024, 8, -1, 32), + (1024, 8, 1, -1), + // 128 * blockSize * rounds * parallelism overflows Int. + (1 << 32, 1 << 30, 1, 32), + ] { + XCTAssertThrowsError( + try KDF.Scrypt.deriveKey( + from: password, + salt: salt, + outputByteCount: outputByteCount, + rounds: rounds, + blockSize: blockSize, + parallelism: parallelism + ) + ) + } + } }