[GH-ISSUE #2871] Replace uses of crypto_scrypt() with (y)escrypt_r() #1827

Closed
opened 2026-10-15 04:26:06 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @solardiz on GitHub (Nov 10, 2017).
Original GitHub issue: https://github.com/openwall/john/issues/2871

The crypto_scrypt() interface (de)allocates memory each time, which is unnecessary overhead when we use it repeatedly. We have a better interface that avoids this, and it's already used in scrypt_fmt.c. However, 3 other formats use crypto_scrypt(). Those should be switched to the better interface as well, using scrypt_fmt.c as an example. This should be easy to do - first task for a new contributor maybe?

$ fgrep 'crypto_scrypt(' *.c
django_scrypt_fmt_plug.c:		if (crypto_scrypt((unsigned char*)saved_key[index], strlen((char*)saved_key[index]),
ethereum_fmt_plug.c:				crypto_scrypt((unsigned char *)saved_key[index+i],
multibit_fmt_plug.c:			crypto_scrypt((const unsigned char*)password, (len + 1) * 2, salt_hardcoded, 8, 16384, 8, 1, key, 32);

(We'll also need to switch from escrypt to yescrypt as a separate task later, but this interface will remain almost the same, so the work on this issue won't be wasted.)

The speed improvement (maybe 30% or so) should be confirmed with benchmarks (both single- and multi-threaded).

A difficulty here is that at least multibit_fmt_plug.c combines very different hash versions in one format, which we might want to deal with separately (split it into two formats); for benchmarking and testing, we may temporarily comment out the wrong-version test vectors. Another issue is that this format only has one test vector that uses scrypt - we should add more.

Originally created by @solardiz on GitHub (Nov 10, 2017). Original GitHub issue: https://github.com/openwall/john/issues/2871 The `crypto_scrypt()` interface (de)allocates memory each time, which is unnecessary overhead when we use it repeatedly. We have a better interface that avoids this, and it's already used in `scrypt_fmt.c`. However, 3 other formats use `crypto_scrypt()`. Those should be switched to the better interface as well, using `scrypt_fmt.c` as an example. This should be easy to do - first task for a new contributor maybe? ``` $ fgrep 'crypto_scrypt(' *.c django_scrypt_fmt_plug.c: if (crypto_scrypt((unsigned char*)saved_key[index], strlen((char*)saved_key[index]), ethereum_fmt_plug.c: crypto_scrypt((unsigned char *)saved_key[index+i], multibit_fmt_plug.c: crypto_scrypt((const unsigned char*)password, (len + 1) * 2, salt_hardcoded, 8, 16384, 8, 1, key, 32); ``` (We'll also need to switch from escrypt to yescrypt as a separate task later, but this interface will remain almost the same, so the work on this issue won't be wasted.) The speed improvement (maybe 30% or so) should be confirmed with benchmarks (both single- and multi-threaded). A difficulty here is that at least `multibit_fmt_plug.c` combines very different hash versions in one format, which we might want to deal with separately (split it into two formats); for benchmarking and testing, we may temporarily comment out the wrong-version test vectors. Another issue is that this format only has one test vector that uses scrypt - we should add more.
Author
Owner

@solardiz commented on GitHub (Nov 12, 2017):

BTW, we have conceptually similar issues for Argon2 and POMELO (any others?)

<!-- gh-comment-id:343744364 --> @solardiz commented on GitHub (Nov 12, 2017): BTW, we have conceptually similar issues for Argon2 and POMELO (any others?)
Author
Owner

@solardiz commented on GitHub (Dec 22, 2017):

Also, we shouldn't silently ignore errors from the underlying hashes. Doing so like django_scrypt_fmt_plug.c currently does (set the output to all zeroes) may lead to false negatives. Doing so like ethereum_fmt_plug.c and multibit_fmt_plug.c currently do may lead to false negatives (likely) and false positives (unlikely, but possible).

scrypt_fmt.c does it almost right: sets a flag from within the maybe-parallel section (strictly speaking, we'd need a #pragma omp critical on that flag assignment, but in practice it's benign), then exits the process with an error message if the flag was set (the error message is too specific, though: while memory allocation failure is the most likely problem, we don't really know what the actual error was).

Similar issues probably apply to Argon2 and POMELO formats, and likely more. I did not check those.

<!-- gh-comment-id:353587107 --> @solardiz commented on GitHub (Dec 22, 2017): Also, we shouldn't silently ignore errors from the underlying hashes. Doing so like django_scrypt_fmt_plug.c currently does (set the output to all zeroes) may lead to false negatives. Doing so like ethereum_fmt_plug.c and multibit_fmt_plug.c currently do may lead to false negatives (likely) and false positives (unlikely, but possible). scrypt_fmt.c does it almost right: sets a flag from within the maybe-parallel section (strictly speaking, we'd need a `#pragma omp critical` on that flag assignment, but in practice it's benign), then exits the process with an error message if the flag was set (the error message is too specific, though: while memory allocation failure is the most likely problem, we don't really know what the actual error was). Similar issues probably apply to Argon2 and POMELO formats, and likely more. I did not check those.
Author
Owner

@solardiz commented on GitHub (Mar 15, 2018):

We'll also need to switch from escrypt to yescrypt as a separate task later

This could proceed now: yescrypt 1.0 is out, and should be integrated either in jumbo or possibly even in core (along with a (ye)scrypt format in core, although jumbo might then need to re-add the various many scrypt hash encodings that it currently supports and that are unsupported by the yescrypt 1.0 tree itself - it's just the encodings, not different hash computation, so re-addable on top of that tree and perhaps inside a format). If magnum or anyone has a preference here, let me know.

<!-- gh-comment-id:373466409 --> @solardiz commented on GitHub (Mar 15, 2018): > We'll also need to switch from escrypt to yescrypt as a separate task later This could proceed now: yescrypt 1.0 is out, and should be integrated either in jumbo or possibly even in core (along with a (ye)scrypt format in core, although jumbo might then need to re-add the various many scrypt hash encodings that it currently supports and that are unsupported by the yescrypt 1.0 tree itself - it's just the encodings, not different hash computation, so re-addable on top of that tree and perhaps inside a format). If magnum or anyone has a preference here, let me know.
Author
Owner

@solardiz commented on GitHub (Apr 11, 2019):

I replace the underlying code with current yescrypt post-1.0.3 in #3790, but in that PR I don't yet approach making other changes proposed in here.

<!-- gh-comment-id:482273555 --> @solardiz commented on GitHub (Apr 11, 2019): I replace the underlying code with current yescrypt post-1.0.3 in #3790, but in that PR I don't yet approach making other changes proposed in here.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1827
No description provided.