From e652730d73c04466c3e3540b0ff8aa8f190ece9f Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Mon, 23 Nov 2026 15:31:15 +0000 Subject: [PATCH] Fix the default Scrypt memory limit (#508) ### Motivation: If the caller passes no `maxMemory`, `KDF.Scrypt.deriveKey` sets the limit to `128 * blockSize * rounds * parallelism` plus a page. BoringSSL needs `128 * blockSize * (rounds + parallelism + 1)` bytes, so some valid parameters fail with `MEMORY_LIMIT_EXCEEDED`. For example, `rounds: 2, blockSize: 1024, parallelism: 1` gets a limit of 256 KiB plus a page and needs 512 KiB. ### Modifications: - Compute the default limit as `128 * blockSize * (rounds + parallelism + 1)` plus a page. Overflow still throws `CryptoKitError.incorrectParameterSize`. - Add a test for `rounds: 2, blockSize: 1024, parallelism: 1` with no `maxMemory`, and an overflow case for `parallelism: .max`. ### Result: The default limit covers the memory BoringSSL needs, so valid parameters no longer fail. Fixes #489. --- .../Scrypt/BoringSSL/Scrypt_boring.swift | 13 ++++++++----- Tests/CryptoExtrasTests/ScryptTests.swift | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift b/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift index 1f000ec7..ce855e15 100644 --- a/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift +++ b/Sources/CryptoExtras/Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift @@ -89,13 +89,16 @@ internal struct BoringSSLScrypt { 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. + // BoringSSL needs 128 * blockSize * (rounds + parallelism + 1) bytes; one page is added as slack. let (blockBytes, blockBytesOverflowed) = 128.multipliedReportingOverflow(by: blockSize) - let (laneBytes, laneBytesOverflowed) = blockBytes.multipliedReportingOverflow(by: rounds) - let (stateBytes, stateBytesOverflowed) = laneBytes.multipliedReportingOverflow(by: parallelism) + let (scratchBlockCount, scratchBlockCountOverflowed) = rounds.addingReportingOverflow(parallelism) + let (blockCount, blockCountOverflowed) = scratchBlockCount.addingReportingOverflow(1) + let (stateBytes, stateBytesOverflowed) = blockBytes.multipliedReportingOverflow(by: blockCount) let (computedLimit, computedLimitOverflowed) = stateBytes.addingReportingOverflow(getPageSize()) - guard !blockBytesOverflowed, !laneBytesOverflowed, !stateBytesOverflowed, !computedLimitOverflowed else { + guard + !blockBytesOverflowed, !scratchBlockCountOverflowed, !blockCountOverflowed, !stateBytesOverflowed, + !computedLimitOverflowed + else { throw CryptoKitError.incorrectParameterSize } memoryLimit = computedLimit diff --git a/Tests/CryptoExtrasTests/ScryptTests.swift b/Tests/CryptoExtrasTests/ScryptTests.swift index adb85ab4..fdc949eb 100644 --- a/Tests/CryptoExtrasTests/ScryptTests.swift +++ b/Tests/CryptoExtrasTests/ScryptTests.swift @@ -125,6 +125,20 @@ class ScryptTests: XCTestCase { XCTAssertEqual(bounded, unbounded) } + func testDeriveKeyWithDefaultMaxMemoryAndBlockSizeAboveRoundsSucceeds() throws { + // Needs 128 * 1024 * (2 + 1 + 1) bytes, more than 128 * blockSize * rounds * parallelism. + XCTAssertNoThrow( + try KDF.Scrypt.deriveKey( + from: [UInt8](), + salt: [UInt8](), + outputByteCount: 32, + rounds: 2, + blockSize: 1024, + parallelism: 1 + ) + ) + } + func testDeriveKeyWithOutOfRangeParametersThrows() throws { let password = Array("password".utf8) let salt = Array("salt".utf8) @@ -135,8 +149,9 @@ class ScryptTests: XCTestCase { (1024, -1, 1, 32), (1024, 8, -1, 32), (1024, 8, 1, -1), - // 128 * blockSize * rounds * parallelism overflows Int. + // 128 * blockSize * (rounds + parallelism + 1) overflows Int. (1 << 32, 1 << 30, 1, 32), + (1024, 8, .max, 32), ] { XCTAssertThrowsError( try KDF.Scrypt.deriveKey(