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(