Skip to content

Commit

Permalink
Merge pull request #424 from str4d/418-scrypt-very-fast-machines
Browse files Browse the repository at this point in the history
scrypt: Increase `log_n` until it is measurable
  • Loading branch information
str4d authored Dec 27, 2023
2 parents c537af2 + 967750c commit 0a64d38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions age/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ to 1.0.0 are beta releases.
- `impl TryFrom<Identity> for Recipient`

### Fixed
- `age::Encryptor::with_user_passphrase` will now re-measure the `scrypt` work
factor until it is measurable, instead of setting the work factor to maximum.
- `age::cli_common`:
- `UiCallbacks::confirm` no longer requires erasing the confirmation message
before it will accept a response.
Expand Down
15 changes: 11 additions & 4 deletions age/src/scrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const ENCRYPTED_FILE_KEY_BYTES: usize = FILE_KEY_BYTES + 16;
///
/// Guaranteed to return a valid work factor (less than 64).
fn target_scrypt_work_factor() -> u8 {
// Time a work factor that should always be fast.
let mut log_n = 10;

let duration: Option<Duration> = {
let measure_duration = |log_n| {
// Platforms that have a functional SystemTime::now():
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
{
Expand Down Expand Up @@ -61,6 +58,16 @@ fn target_scrypt_work_factor() -> u8 {
}
};

// Time a work factor that should always be fast.
let mut log_n = 10;
let mut duration: Option<Duration> = measure_duration(log_n);
while duration.map(|d| d.is_zero()).unwrap_or(false) {
// On some newer platforms, the work factor may be so fast that it is cannot be
// measured. Increase the work factor until we can measure something.
log_n += 1;
duration = measure_duration(log_n);
}

duration
.map(|mut d| {
// Use duration as a proxy for CPU usage, which scales linearly with N.
Expand Down

0 comments on commit 0a64d38

Please sign in to comment.