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.
This commit is contained in:
Si Beaumont authored and GitHub committed 2026-11-23 15:31:15 +00:00
1 parent 3280a5f728
commit e652730d73
2 files changed
+24 -6

No files matched your search

@@ -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
+16 -1
View File
@@ -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(