| ... | @@ -471,6 +471,18 @@ fn indexAlpha( | ... | @@ -471,6 +471,18 @@ fn indexAlpha( |
| 471 | return ref_lane * lanes + @as(u32, @intCast(((s + m - (p + 1)) % lanes))); | 471 | return ref_lane * lanes + @as(u32, @intCast(((s + m - (p + 1)) % lanes))); |
| 472 | } | 472 | } |
| 473 | | 473 | |
| | 474 | fn blockCount(params: Params) u32 { |
| | 475 | return @max( |
| | 476 | params.m / (sync_points * params.p) * (sync_points * params.p), |
| | 477 | 2 * sync_points * params.p, |
| | 478 | ); |
| | 479 | } |
| | 480 | |
| | 481 | /// Compute the number of bytes of scratch memory `kdf` needs for `params`. |
| | 482 | pub fn calcSize(params: Params) usize { |
| | 483 | return blockCount(params) * @sizeOf([block_length]u64); |
| | 484 | } |
| | 485 | |
| 474 | /// Derives a key from the password, salt, and argon2 parameters. | 486 | /// Derives a key from the password, salt, and argon2 parameters. |
| 475 | /// | 487 | /// |
| 476 | /// Derived key has to be at least 4 bytes length. | 488 | /// Derived key has to be at least 4 bytes length. |
| ... | @@ -494,10 +506,7 @@ pub fn kdf( | ... | @@ -494,10 +506,7 @@ pub fn kdf( |
| 494 | if (params.m / 8 < params.p) return KdfError.WeakParameters; | 506 | if (params.m / 8 < params.p) return KdfError.WeakParameters; |
| 495 | | 507 | |
| 496 | var h0 = initHash(password, salt, params, derived_key.len, mode); | 508 | var h0 = initHash(password, salt, params, derived_key.len, mode); |
| 497 | const memory = @max( | 509 | const memory = blockCount(params); |
| 498 | params.m / (sync_points * params.p) * (sync_points * params.p), | | |
| 499 | 2 * sync_points * params.p, | | |
| 500 | ); | | |
| 501 | | 510 | |
| 502 | var blocks = try Blocks.initCapacity(allocator, memory); | 511 | var blocks = try Blocks.initCapacity(allocator, memory); |
| 503 | defer blocks.deinit(); | 512 | defer blocks.deinit(); |