[GH-ISSUE #2023] Possible method to vastly speed up certain hashes #1362

Closed
opened 2026-10-14 03:45:47 +07:00 by bsakaguchi · 34 comments
Owner

Originally created by @jfoug on GitHub (Feb 4, 2016).
Original GitHub issue: https://github.com/openwall/john/issues/2023

Ok, this was found ONLY for VTP. But there may be others.

What was found is there is a LARGE cost per password, prior to the 'normal' hash. So we have 2 things. One, we have vast improvements, for multi-salt searches if we offload this large per password cost over to set_key() (if it is in crypt_all). However, with this method, we also see a reduction in speed for the 1 salt speed (in OMP I would assume).

Here are some timings:

Jim.Fougeron@1PQHGQ1 /c/projects/phpbb/johnripper/john-1.7.9/bleed64/src
$ ../run/john -test=4 -form=vtp
Will run 8 OpenMP threads
Benchmarking: vtp, "MD5 based authentication" VTP [MD5 32/64]... (8xOMP) DONE
Many salts:     20757 c/s real, 4163 c/s virtual
Only one salt:  20623 c/s real, 4196 c/s virtual


$ make -s
Make process completed.

$ ../run/john -test=4 -form=vtp
Will run 8 OpenMP threads
Benchmarking: vtp, "MD5 based authentication" VTP [MD5 32/64]... (8xOMP) DONE
Many salts:     617134 c/s real, 442970 c/s virtual
Only one salt:  4579 c/s real, 4914 c/s virtual

On the 2nd run, I offload the computation of the vtp_secret_derive() which does 1563 MD5 limbs which are nothing but the password appended to itself. So, this logic was moved from crypt_all, to set_key(). What we lose, is the OMP (which affects the 1-salt speed), but we 'gain' in that we do that code only 1 time per password. The 30x improvement in speed is for real (well, it will vary based upon how many salts we have).

So, here is the question. How would I know the salt-count by the time we get to set-key, and crypt-all.

I could put conditional logic into both, and if speed is 'lost', it would move that logic (the call to vtp_secret_derive() ) into the crypt_all, if there were only a couple salts. But if more than X number of salts were loaded, then speed is proportionally faster by stopping that logic in the crypt_all, and instead doing it within set_key().

One other 'option', is to set a dirty flag within set_key(), and then only calling the vtp_secret_derive() within crypt_all, BUT doing it within an omp_for loop.

I will test this idea out. It may be the proper way to go.

BUT, we should look at other salted formats and find ones where there is large amounts of work that depends ONLY upon the password, and see if we can cache that logic in some manner, so that we are not continually recomputing it for EVERY salt!

Originally created by @jfoug on GitHub (Feb 4, 2016). Original GitHub issue: https://github.com/openwall/john/issues/2023 Ok, this was found ONLY for VTP. But there may be others. What was found is there is a LARGE cost per password, prior to the 'normal' hash. So we have 2 things. One, we have vast improvements, for multi-salt searches if we offload this large per password cost over to set_key() (if it is in crypt_all). However, with this method, we also see a reduction in speed for the 1 salt speed (in OMP I would assume). Here are some timings: ``` Jim.Fougeron@1PQHGQ1 /c/projects/phpbb/johnripper/john-1.7.9/bleed64/src $ ../run/john -test=4 -form=vtp Will run 8 OpenMP threads Benchmarking: vtp, "MD5 based authentication" VTP [MD5 32/64]... (8xOMP) DONE Many salts: 20757 c/s real, 4163 c/s virtual Only one salt: 20623 c/s real, 4196 c/s virtual $ make -s Make process completed. $ ../run/john -test=4 -form=vtp Will run 8 OpenMP threads Benchmarking: vtp, "MD5 based authentication" VTP [MD5 32/64]... (8xOMP) DONE Many salts: 617134 c/s real, 442970 c/s virtual Only one salt: 4579 c/s real, 4914 c/s virtual ``` On the 2nd run, I offload the computation of the vtp_secret_derive() which does 1563 MD5 limbs which are nothing but the password appended to itself. So, this logic was moved from crypt_all, to set_key(). What we lose, is the OMP (which affects the 1-salt speed), but we 'gain' in that we do that code only 1 time per password. The 30x improvement in speed is for real (well, it will vary based upon how many salts we have). So, here is the question. How would I know the salt-count by the time we get to set-key, and crypt-all. I could put conditional logic into both, and if speed is 'lost', it would move that logic (the call to vtp_secret_derive() ) into the crypt_all, if there were only a couple salts. But if more than X number of salts were loaded, then speed is proportionally faster by stopping that logic in the crypt_all, and instead doing it within set_key(). One other 'option', is to set a dirty flag within set_key(), and then only calling the vtp_secret_derive() within crypt_all, BUT doing it within an omp_for loop. I will test this idea out. It may be the proper way to go. BUT, we should look at other salted formats and find ones where there is large amounts of work that depends ONLY upon the password, and see if we can cache that logic in some manner, so that we are not continually recomputing it for EVERY salt!
bsakaguchi 2026-10-14 03:45:47 +07:00
Author
Owner

@jfoug commented on GitHub (Feb 4, 2016):

Ok, moving it into crypt_all is by far the best of both worlds:

$ ../run/john -test=4 -form=vtp
Will run 8 OpenMP threads
Benchmarking: vtp, "MD5 based authentication" VTP [MD5 32/64]... (8xOMP) DONE
Many salts:     1128K c/s real, 431899 c/s virtual
Only one salt:  21501 c/s real, 4353 c/s virtual

Those speeds can be compared to the original test.

With this change, set_key sets a static bDirty to 1. Then in the crypt_all, I changed this code

-   unsigned char secret[16];
-   vtp_secret_derive(saved_key[index], saved_len[index], secret);
+   if (bDirty)
+      vtp_secret_derive(saved_key[index], saved_len[index], secret[index]);

Then at the bottom of crypt_all, I set bDirty to 0 (so the next salt with same passwords will NOT call that function). The array *secret[16] was already a global static array, used when I was doing this work in set_key. I use the same array, just load it in the GOMP code in crypt_all.

<!-- gh-comment-id:180096910 --> @jfoug commented on GitHub (Feb 4, 2016): Ok, moving it into crypt_all is by far the best of both worlds: ``` $ ../run/john -test=4 -form=vtp Will run 8 OpenMP threads Benchmarking: vtp, "MD5 based authentication" VTP [MD5 32/64]... (8xOMP) DONE Many salts: 1128K c/s real, 431899 c/s virtual Only one salt: 21501 c/s real, 4353 c/s virtual ``` Those speeds can be compared to the original test. With this change, set_key sets a static bDirty to 1. Then in the crypt_all, I changed this code ``` diff - unsigned char secret[16]; - vtp_secret_derive(saved_key[index], saved_len[index], secret); + if (bDirty) + vtp_secret_derive(saved_key[index], saved_len[index], secret[index]); ``` Then at the bottom of crypt_all, I set bDirty to 0 (so the next salt with same passwords will NOT call that function). The array *secret[16] was already a global static array, used when I was doing this work in set_key. I use the same array, just load it in the GOMP code in crypt_all.
Author
Owner

@magnumripper commented on GitHub (Feb 5, 2016):

CircleCI fails

Testing: vtp, "MD5 based authentication" VTP [MD5 32/64]... (2xOMP) FAILED (cmp_all(256))

BTW our codestyle advices against camelCase.

<!-- gh-comment-id:180230844 --> @magnumripper commented on GitHub (Feb 5, 2016): CircleCI fails ``` Testing: vtp, "MD5 based authentication" VTP [MD5 32/64]... (2xOMP) FAILED (cmp_all(256)) ``` BTW our codestyle advices against camelCase.
Author
Owner

@jfoug commented on GitHub (Feb 5, 2016):

I see both CI's failing. However, it works for me, non-OMP, and OMP for VC, cygwin64 and fedora22-x64. So I have NO IDEA what the problem could be on the CI.

<!-- gh-comment-id:180353317 --> @jfoug commented on GitHub (Feb 5, 2016): I see both CI's failing. However, it works for me, non-OMP, and OMP for VC, cygwin64 and fedora22-x64. So I have NO IDEA what the problem could be on the CI.
Author
Owner

@ghost commented on GitHub (Feb 5, 2016):

On super

$ ../run/john -test-full=0 -form=vtp
Will run 32 OpenMP threads
Testing: vtp, "MD5 based authentication" VTP [MD5 32/64]... (32xOMP) FAILED (cmp_all(256))
<!-- gh-comment-id:180403443 --> @ghost commented on GitHub (Feb 5, 2016): On super ``` $ ../run/john -test-full=0 -form=vtp Will run 32 OpenMP threads Testing: vtp, "MD5 based authentication" VTP [MD5 32/64]... (32xOMP) FAILED (cmp_all(256)) ```
Author
Owner

@jfoug commented on GitHub (Feb 5, 2016):

f1ec573 I really have no idea. But I changed logic to clear the dirty flag prior to entering the OMP block. I would think that the closing } should keep all threads from clearing that flag, but who knows. So I set a local var, and never reset that var again, then clear the dirty flag. If this does not fix things, then I really have NO idea. Again, it is working perfectly on my box, with or without OMP, cygwin and Unix alike.

<!-- gh-comment-id:180412092 --> @jfoug commented on GitHub (Feb 5, 2016): f1ec573 I really have no idea. But I changed logic to clear the dirty flag prior to entering the OMP block. I would think that the closing } should keep all threads from clearing that flag, but who knows. So I set a local var, and never reset that var again, then clear the dirty flag. If this does not fix things, then I really have NO idea. Again, it is working perfectly on my box, with or without OMP, cygwin and Unix alike.
Author
Owner

@jfoug commented on GitHub (Feb 5, 2016):

Still fails. I have no clue as to why...

<!-- gh-comment-id:180416253 --> @jfoug commented on GitHub (Feb 5, 2016): Still fails. I have no clue as to why...
Author
Owner

@jfoug commented on GitHub (Feb 5, 2016):

My bad. It is test-full failing. I get that error, so should be able to find this now.

<!-- gh-comment-id:180416829 --> @jfoug commented on GitHub (Feb 5, 2016): My bad. It is test-full failing. I get that error, so should be able to find this now.
Author
Owner

@jfoug commented on GitHub (Feb 5, 2016):

Found the bug. I was still using 'secret' which is now an array of buffers. Before it was just a single buffer local to the thread.

5adbc42 Circle CI is now working, and I am sure Travis will work also.

<!-- gh-comment-id:180438989 --> @jfoug commented on GitHub (Feb 5, 2016): Found the bug. I was still using 'secret' which is now an array of buffers. Before it was just a single buffer local to the thread. 5adbc42 Circle CI is now working, and I am sure Travis will work also.
Author
Owner

@jfoug commented on GitHub (Feb 8, 2016):

github.com/magnumripper/JohnTheRipper@0272d84083 AzureAD. Not a huge speed change, but now the MD4 code is being done in OMP. At some time we may also want to do the MD4 using SIMD, but I think we will have to create a 2nd OMP for loop that is above the current for loop, and only do that for SIMD code. Not sure it will make that huge of a difference, but it will make 'some'. It will certainly be less than 1% improvement, since there are 100 iteration of PBKDF2-sha256 behind each MD4 call. But getting the MD4 out of single threaded into OMP, did make an improvement, especially since it is oSSL MD4 code.

<!-- gh-comment-id:181478301 --> @jfoug commented on GitHub (Feb 8, 2016): https://github.com/magnumripper/JohnTheRipper/commit/0272d840831cfb9d0af0709b11c0b70296c6893e AzureAD. Not a huge speed change, but now the MD4 code is being done in OMP. At some time we may also want to do the MD4 using SIMD, but I think we will have to create a 2nd OMP for loop that is above the current for loop, and only do that for SIMD code. Not sure it will make that huge of a difference, but it will make 'some'. It will certainly be less than 1% improvement, since there are 100 iteration of PBKDF2-sha256 behind each MD4 call. But getting the MD4 out of single threaded into OMP, did make an improvement, especially since it is oSSL MD4 code.
Author
Owner

@jfoug commented on GitHub (Feb 8, 2016):

3a94817 wow-srp. Cached off first SHA1.

ROLLED back. This was totally busted. UserID is part of salt. Not sure how it passed the ST code??? But this can NOT be cached off. 0ab06af

<!-- gh-comment-id:181500516 --> @jfoug commented on GitHub (Feb 8, 2016): 3a94817 wow-srp. Cached off first SHA1. ROLLED back. This was totally busted. UserID is part of salt. Not sure how it passed the ST code??? But this can NOT be cached off. 0ab06af
Author
Owner

@jfoug commented on GitHub (Feb 8, 2016):

eb887cf hsrp. At least doubled in speed (a little more actually due to improved buffer copy reductions).

<!-- gh-comment-id:181592642 --> @jfoug commented on GitHub (Feb 8, 2016): eb887cf hsrp. At least doubled in speed (a little more actually due to improved buffer copy reductions).
Author
Owner

@jfoug commented on GitHub (Feb 8, 2016):

IKE is ugly as hell, but I am pretty sure it can improved a bunch.

<!-- gh-comment-id:181598157 --> @jfoug commented on GitHub (Feb 8, 2016): IKE is ugly as hell, but I am pretty sure it can improved a bunch.
Author
Owner

@jfoug commented on GitHub (Feb 9, 2016):

512ec90 wbb3. Some improvement, but only 1 limb 1 bin2hex. There are 5 limbs and 3 bin2hex calls per crypt loop. So this is 'some' improvement, but not huge.

Note, wbb3 is MUCH faster when run in dynamic. I might make this thin, just to keep the 'format' hash, but change this to dyna.

<!-- gh-comment-id:182018013 --> @jfoug commented on GitHub (Feb 9, 2016): 512ec90 wbb3. Some improvement, but only 1 limb 1 bin2hex. There are 5 limbs and 3 bin2hex calls per crypt loop. So this is 'some' improvement, but not huge. Note, wbb3 is MUCH faster when run in dynamic. I might make this thin, just to keep the 'format' hash, but change this to dyna.
Author
Owner

@jfoug commented on GitHub (Feb 11, 2016):

Ok, here is a different format than I am used to. This format could be made vastly faster for SINGLE-salt, than for multi salts (as long as passwords were about same size, or we could group them). That is keystore hash.

The hash is simple. It is just the password BE extended ascii (not utf8 to utf16be, I have tested using java's keytool), a small constant whitener, and then a salt, and the salt is very large (can be up to 800k or so).

So, for single salt (if we KNEW it was single salt), we could setup the entire buffer properly, and then only adjust a couple of bytes for new passwords. Then SIMD the pre-made buffer each time. No buffer setup, just raw calls into the SIMD-sha1 code.

But for multi salts, this optimization is not ideal. It would not be good to create salt buffers for every salt and every password length, we would quickly run out of memory. BUT for a single salt, we COULD do this (might want to reduce PLAINTEXT length a bit)

But do we know we have only 1 salt loaded (and probably would need to know at init() time??) I am not sure we have a method. Yes, I know (think), that set_salt() will only be called 1 time, once we get past the ST code. But I am not 100% sure that is known up front (at init) and not sure how easy it is to distinguish if/when we have only a single salt.

<!-- gh-comment-id:182720450 --> @jfoug commented on GitHub (Feb 11, 2016): Ok, here is a different format than I am used to. This format could be made vastly faster for SINGLE-salt, than for multi salts (as long as passwords were about same size, or we could group them). That is keystore hash. The hash is simple. It is just the password BE extended ascii (not utf8 to utf16be, I have tested using java's keytool), a small constant whitener, and then a salt, and the salt is very large (can be up to 800k or so). So, for single salt (if we KNEW it was single salt), we could setup the entire buffer properly, and then only adjust a couple of bytes for new passwords. Then SIMD the pre-made buffer each time. No buffer setup, just raw calls into the SIMD-sha1 code. But for multi salts, this optimization is not ideal. It would not be good to create salt buffers for every salt and every password length, we would quickly run out of memory. BUT for a single salt, we COULD do this (might want to reduce PLAINTEXT length a bit) But do we know we have only 1 salt loaded (and probably would need to know at init() time??) I am not sure we have a method. Yes, I know (think), that set_salt() will only be called 1 time, once we get past the ST code. But I am not 100% sure that is known up front (at init) and not sure how easy it is to distinguish if/when we have only a single salt.
Author
Owner

@magnumripper commented on GitHub (Feb 11, 2016):

That is interesting. Why limit it to just 1 salt though? A format like keystore is most often attacked one at a time, or just a few - never "100 million" as may happen in the raw case.

Doing, say, 10 x 16 x 800KB (10 salts, 16 lengths) would not be a problem at all and actually 100 x 16 x 800KB would still work fine on most systems. So I'd say just do it - don't limit it to some low number of salts. If you run out of memory, you'll need to split your input file or use some option eg. --user.

Anyway to answer your question you won't know number of salts until reset(db_main) is called - after all salts were loaded into db. So if you really want to limit this, you can leave get_salt() unchanged but do the extra/new preparation in reset() if salt count is less than 100 or so.

<!-- gh-comment-id:182751202 --> @magnumripper commented on GitHub (Feb 11, 2016): That is interesting. Why limit it to just 1 salt though? A format like keystore is most often attacked one at a time, or just a few - never "100 million" as may happen in the raw case. Doing, say, 10 x 16 x 800KB (10 salts, 16 lengths) would not be a problem at all and actually 100 x 16 x 800KB would still work fine on most systems. So I'd say just do it - don't limit it to some low number of salts. If you run out of memory, you'll need to split your input file or use some option eg. `--user`. Anyway to answer your question you won't know number of salts until `reset(db_main)` is called - after all salts were loaded into db. So if you really want to limit this, you can leave `get_salt()` unchanged but do the extra/new preparation in `reset()` if salt count is less than 100 or so.
Author
Owner

@magnumripper commented on GitHub (Feb 11, 2016):

Another thing you can do is skip preparing buffers for short lengths. For example, you can do lengths up to 5 with the "current" code (small keyspace anyway), length 6..13 using your prepared buffer optimization and still support length 14 and above - using the current/naive code.

<!-- gh-comment-id:182752814 --> @magnumripper commented on GitHub (Feb 11, 2016): Another thing you can do is skip preparing buffers for short lengths. For example, you can do lengths up to 5 with the "current" code (small keyspace anyway), length 6..13 using your prepared buffer optimization and still support length 14 and above - using the current/naive code.
Author
Owner

@magnumripper commented on GitHub (Feb 11, 2016):

Hmm the only thing you'd gain is byte-wise data shuffling overhead. You could get away with just two buffers instead: One for each alignment that we may be "at" after the whitener. This would be almost as effective but draw a lot less memory than preparing all lengths.

<!-- gh-comment-id:182755238 --> @magnumripper commented on GitHub (Feb 11, 2016): Hmm the only thing you'd gain is byte-wise data shuffling overhead. You could get away with just **two** buffers instead: One for each alignment that we may be "at" after the whitener. This would be almost as effective but draw a lot less memory than preparing all lengths.
Author
Owner

@jfoug commented on GitHub (Feb 11, 2016):

I have never used the reset(salt_db) before. Looks good. There are other formats which also have a faster method of doing single salt, vs the method more optimized for multi-salts. So even in them, if we KNOW that there is only 1 salt, they could set themselves up for different behavior.

But this format, which has a VERY heavy cost of salt, which is opposite of normal, is certainly different than the 'normal' john.

Now, the '800kb' salt length came from the salt structure. The sample hashes I have are much smaller. But they are also empty keystores. It may be that the length of hash increases as more keys are placed into the keystore, I really do not know, but will look at it. But I have created 2 other hashes, and they all have about the same length salt as the first hash.

<!-- gh-comment-id:182865062 --> @jfoug commented on GitHub (Feb 11, 2016): I have never used the reset(salt_db) before. Looks good. There are other formats which also have a faster method of doing single salt, vs the method more optimized for multi-salts. So even in them, if we KNOW that there is only 1 salt, they could set themselves up for different behavior. But this format, which has a VERY heavy cost of salt, which is opposite of normal, is certainly different than the 'normal' john. Now, the '800kb' salt length came from the salt structure. The sample hashes I have are much smaller. But they are also empty keystores. It may be that the length of hash increases as more keys are placed into the keystore, I really do not know, but will look at it. But I have created 2 other hashes, and they all have about the same length salt as the first hash.
Author
Owner

@jfoug commented on GitHub (Feb 11, 2016):

Should reset() be called before starting benchmarking for single salt and multi salt running? I do now understand reset() usage and or implications. It does look like it causes an autotune inside opencl code. But I would imagine there might be different tuning possibly even within opencl depending upon number of salts.

<!-- gh-comment-id:182873148 --> @jfoug commented on GitHub (Feb 11, 2016): Should reset() be called before starting benchmarking for single salt and multi salt running? I do now understand reset() usage and or implications. It does look like it causes an autotune inside opencl code. But I would imagine there might be different tuning possibly even within opencl depending upon number of salts.
Author
Owner

@magnumripper commented on GitHub (Feb 11, 2016):

Reset is called before self-test (with the "fake db" made from test vectors) and then again (with the real database) before actual cracking starts.

If needed, you can tell which it is:

    if (db->real == NULL || db->real != db)
        /* db is made from test vectors */
    else
        /* db is the real thing */
<!-- gh-comment-id:182979503 --> @magnumripper commented on GitHub (Feb 11, 2016): Reset is called before self-test (with the "fake db" made from test vectors) and then again (with the real database) before actual cracking starts. If needed, you can tell which it is: ``` c if (db->real == NULL || db->real != db) /* db is made from test vectors */ else /* db is the real thing */ ```
Author
Owner

@jfoug commented on GitHub (Feb 11, 2016):

I am going to start on this (keystore). I think I will make these assumptions:

1 pw length 0 to 3 will be done with legacy CTX logic
2 pw length >24 will be done with legacy CTX logic.
3 when reset is called, salt records will be 'built'.
3a 1gb memory usage limit ???
3b we build omp_t sets of 4-24 salt records.
3c we hash the salt record (incremental number is fine, or a pointer), so that we can quickly find the right data set.
3d only the first block needs to be and OMP_T set. The remainder can be a single buffer, and all threads can share it since it is read-only
4 we have a pretty large set of passwords in a 'group'
5 we have a sort (and then unsort) in crypt_all for password lengths.
6 in the OMP loop of crypt_all, for 4>= pw lenth =<24 we ONLY adjust the few bytes of password within the first block of SIMD built logic.

With the above logic, we should see VERY good gains for SIMD. I was really thinking this format could not get any gains for SIMD due to the salt-switch being the largest time comsumption.

Now, for CTX model, there is little to no gain worrying about this, since we do not pre-fab buffers. In CTX mode, caching the password load, is about the only gain we can get, and it is almost imperceptible since the time spent hashing the salt is so much larger than any cache savings.

<!-- gh-comment-id:183039449 --> @jfoug commented on GitHub (Feb 11, 2016): I am going to start on this (keystore). I think I will make these assumptions: 1 pw length 0 to 3 will be done with legacy CTX logic 2 pw length >24 will be done with legacy CTX logic. 3 when reset is called, salt records will be 'built'. 3a 1gb memory usage limit ??? 3b we build omp_t sets of 4-24 salt records. 3c we hash the salt record (incremental number is fine, or a pointer), so that we can quickly find the right data set. 3d only the first block needs to be and OMP_T set. The remainder can be a single buffer, and all threads can share it since it is read-only 4 we have a pretty large set of passwords in a 'group' 5 we have a sort (and then unsort) in crypt_all for password lengths. 6 in the OMP loop of crypt_all, for 4>= pw lenth =<24 we ONLY adjust the few bytes of password within the first block of SIMD built logic. With the above logic, we should see VERY good gains for SIMD. I was really thinking this format could not get any gains for SIMD due to the salt-switch being the largest time comsumption. Now, for CTX model, there is little to no gain worrying about this, since we do not pre-fab buffers. In CTX mode, caching the password load, is about the only gain we can get, and it is almost imperceptible since the time spent hashing the salt is so much larger than any cache savings.
Author
Owner

@magnumripper commented on GitHub (Feb 11, 2016):

Sayantan added a member sequential_num or something like that to struct db_salt. Maybe that can be used for 3c.

<!-- gh-comment-id:183080247 --> @magnumripper commented on GitHub (Feb 11, 2016): Sayantan added a member `sequential_num` or something like that to struct db_salt. Maybe that can be used for 3c.
Author
Owner

@jfoug commented on GitHub (Feb 12, 2016):

I am putting a pointer into the salt. So when I create my object in the reset() function for this salt, I just point the object in the salt object owned by jtr TO the actual object to use. Easy peasy. Here are the data structures I am starting (the original salt is simply modified.

#ifdef SIMD_COEF_32
static struct custom_preload {
    // 24 character passwords fit into 1 'first_blk' item. They are
    // 48 byts of password, and 16 bytes of appended whitener. That
    // fits.
    ARCH_WORD_32 (*first_blk)[21][SHA_BUF_SIZ*NBKEYS];  // handles password lengths of 4 to 24 (21 elements).
    ARCH_WORD_32 *remaining_data[21];
    int block_count[21]; // number of sha blocks in remaining_data.
    unsigned char sha1_data[20];    // to validate if we have this one loaded before.
    struct custom_preload *next;
} *salt_preload;
#endif

static struct custom_salt {
    int target;
    int data_length;
    int count;
    int keysize;
#ifdef SIMD_COEF_32
    void *ptr;  // we will point to a pre-built salt record if there is one.
    unsigned char sha_of_data[20]; // this is the SHA of the data block. We use this for pre-building salt.
#endif
    unsigned char data[SZ];
    //unsigned char keydata[SZ];
} *keystore_cur_salt;
<!-- gh-comment-id:183119809 --> @jfoug commented on GitHub (Feb 12, 2016): I am putting a pointer into the salt. So when I create my object in the reset() function for this salt, I just point the object in the salt object owned by jtr TO the actual object to use. Easy peasy. Here are the data structures I am starting (the original salt is simply modified. ``` C #ifdef SIMD_COEF_32 static struct custom_preload { // 24 character passwords fit into 1 'first_blk' item. They are // 48 byts of password, and 16 bytes of appended whitener. That // fits. ARCH_WORD_32 (*first_blk)[21][SHA_BUF_SIZ*NBKEYS]; // handles password lengths of 4 to 24 (21 elements). ARCH_WORD_32 *remaining_data[21]; int block_count[21]; // number of sha blocks in remaining_data. unsigned char sha1_data[20]; // to validate if we have this one loaded before. struct custom_preload *next; } *salt_preload; #endif static struct custom_salt { int target; int data_length; int count; int keysize; #ifdef SIMD_COEF_32 void *ptr; // we will point to a pre-built salt record if there is one. unsigned char sha_of_data[20]; // this is the SHA of the data block. We use this for pre-building salt. #endif unsigned char data[SZ]; //unsigned char keydata[SZ]; } *keystore_cur_salt; ```
Author
Owner

@jfoug commented on GitHub (Feb 12, 2016):

Ok, it appears that void reset(struct db_main *db) should actually be void reset(const struct db_main *db)

If I set the ptr var within reset(), then I get this:

Will run 8 OpenMP threads
Self test failed (Could not find salt in db - salt() return inconsistent?)
Loaded 1 password hash (keystore, Java KeyStore [SHA1 128/128 SSE4.1 4x])
Press any key to continue . . .

Not an unworkable problem. I can set the ptr inside of set_salt(), if it is not already set (should be null), and if I can not find the salt pre-loaded, I will set ptr to -1, so that it is not searched for again.

Then in crypt_all, if ptr is -1, we will fall back to CTX for the entire batch. It does take a bit of memory. For my OMP8 (with PARA=1) it is 44mb per salt (which I think is wrong). I think it should be:

34 limbs x 4 SIMD x 64 bytes x 21 char lengths = 183k (for the buffers past the first).
1 limb x 4 SIMD x 64 bytes x 21 char lengths x 8 threads = 43k for the first block array.

So, it 'looks' like I should only be allocating about 225k or so per salt (PARA would also be multiplied to this). I must be counting memory incorrectly or something. Well, it should be an easy fix.

But, I think I have made pretty good headway. Just have to work out a few items, and I think it will be 'golden'.

<!-- gh-comment-id:183170894 --> @jfoug commented on GitHub (Feb 12, 2016): Ok, it appears that `void reset(struct db_main *db)` should actually be `void reset(const struct db_main *db)` If I set the ptr var within reset(), then I get this: ``` Will run 8 OpenMP threads Self test failed (Could not find salt in db - salt() return inconsistent?) Loaded 1 password hash (keystore, Java KeyStore [SHA1 128/128 SSE4.1 4x]) Press any key to continue . . . ``` Not an unworkable problem. I can set the ptr inside of set_salt(), if it is not already set (should be null), and if I can not find the salt pre-loaded, I will set ptr to -1, so that it is not searched for again. Then in crypt_all, if ptr is -1, we will fall back to CTX for the entire batch. It does take a bit of memory. For my OMP8 (with PARA=1) it is 44mb per salt (which I think is wrong). I think it should be: 34 limbs x 4 SIMD x 64 bytes x 21 char lengths = 183k (for the buffers past the first). 1 limb x 4 SIMD x 64 bytes x 21 char lengths x 8 threads = 43k for the first block array. So, it 'looks' like I should only be allocating about 225k or so per salt (PARA would also be multiplied to this). I must be counting memory incorrectly or something. Well, it should be an easy fix. But, I think I have made pretty good headway. Just have to work out a few items, and I think it will be 'golden'.
Author
Owner

@jfoug commented on GitHub (Feb 12, 2016):

Ok, found my problem, instead of 8 'threads', I was using the omp_t var, which had the OMP_SCALE multipled in. So that alloc was 1024 times larger than it should have been, lol. Now, I am 229000 bytes per salt. That is not bad at all. I would think we could easily handle 2000 salts at this level, ,or 1000 if PARA is 2 (or AVX2, etc). I am thinking of adding 500mg or 1gb max allocation in the salt buffer.

But like you said, for most 'real' work, that would be plenty. If there are MORE salts than this, I will simply emit a STRONG warning, to split the input up into smaller count of salts. Or, I guess I could just keep going, emitting a warning, but just keep working, and the machine will handle it or not.

<!-- gh-comment-id:183176661 --> @jfoug commented on GitHub (Feb 12, 2016): Ok, found my problem, instead of 8 'threads', I was using the omp_t var, which had the OMP_SCALE multipled in. So that alloc was 1024 times larger than it should have been, lol. Now, I am 229000 bytes per salt. That is not bad at all. I would think we could easily handle 2000 salts at this level, ,or 1000 if PARA is 2 (or AVX2, etc). I am thinking of adding 500mg or 1gb max allocation in the salt buffer. But like you said, for most 'real' work, that would be plenty. If there are MORE salts than this, I will simply emit a STRONG warning, to split the input up into smaller count of salts. Or, I guess I could just keep going, emitting a warning, but just keep working, and the machine will handle it or not.
Author
Owner

@magnumripper commented on GitHub (Feb 12, 2016):

Ok, it appears that void reset(struct db_main *db) should actually be void reset(const struct db_main *db)

If I set the ptr var within reset(), then I get this:

Will run 8 OpenMP threads
Self test failed (Could not find salt in db - salt() return inconsistent?)
Loaded 1 password hash (keystore, Java KeyStore [SHA1 128/128 SSE4.1 4x])
Press any key to continue . . .

The self-test calls get_salt() again, and verifies that the output is 100% same as what's in the db. If your dyna salt offset and comp_size are correct, there should be no problem (this pointer should obviously not be included in what is compared).

<!-- gh-comment-id:183243077 --> @magnumripper commented on GitHub (Feb 12, 2016): > Ok, it appears that void reset(struct db_main *db) should actually be void reset(const struct db_main *db) > > If I set the ptr var within reset(), then I get this: > > ``` > Will run 8 OpenMP threads > Self test failed (Could not find salt in db - salt() return inconsistent?) > Loaded 1 password hash (keystore, Java KeyStore [SHA1 128/128 SSE4.1 4x]) > Press any key to continue . . . > ``` The self-test calls get_salt() again, and verifies that the output is 100% same as what's in the db. If your dyna salt offset and comp_size are correct, there should be no problem (this pointer should obviously not be included in what is compared).
Author
Owner

@jfoug commented on GitHub (Feb 12, 2016):

I am just moving the code into get_salt(). I already have duplication checking, so there should be no problem putting it there.

<!-- gh-comment-id:183326334 --> @jfoug commented on GitHub (Feb 12, 2016): I am just moving the code into get_salt(). I already have duplication checking, so there should be no problem putting it there.
Author
Owner

@jfoug commented on GitHub (Feb 12, 2016):

Just moving the buffer-building and linkage of the ptr member into get_salt() does the trick. reset was not needed.
Note, I may make this into a dyna salt anyway. There are 2 HUGE buffers inlined in the salt. They could easily be pointers. One buffer is not used, and I have simply commented it out. But the other 'could' be put into a pointer. Also, for SIMD runs, where we have pre-built salt data, we really do not need the current salt structure at all. Just returning a pointer to the created SIMD salt structure is all that would be needed, IF we blindly build all salts into these pre-built buffers, not worrying about memory.

<!-- gh-comment-id:183354937 --> @jfoug commented on GitHub (Feb 12, 2016): Just moving the buffer-building and linkage of the ptr member into get_salt() does the trick. reset was not needed. Note, I may make this into a dyna salt anyway. There are 2 HUGE buffers inlined in the salt. They could easily be pointers. One buffer is not used, and I have simply commented it out. But the other 'could' be put into a pointer. Also, for SIMD runs, where we have pre-built salt data, we really do not need the current salt structure at all. Just returning a pointer to the created SIMD salt structure is all that would be needed, IF we blindly build all salts into these pre-built buffers, not worrying about memory.
Author
Owner

@jfoug commented on GitHub (Feb 12, 2016):

here is the first version. SIMD gets proper speeds, EVEN for multi salts!!!

$ OMP_NUM_THREADS=4 ../run/john -test=3 -form=keystore
Will run 4 OpenMP threads
Benchmarking: keystore, Java KeyStore [SHA1 128/128 AVX 4x]... (4xOMP) DONE
Many salts:     1531K c/s real, 412201 c/s virtual
Only one salt:  1488K c/s real, 412831 c/s virtual


$ make -s
Make process completed.

$ OMP_NUM_THREADS=4 ../run/john -test=3 -form=keystore
Will run 4 OpenMP threads
Benchmarking: keystore, Java KeyStore [SHA1 32/64]... (4xOMP) DONE
Many salts:     567188 c/s real, 160019 c/s virtual
Only one salt:  546636 c/s real, 155579 c/s virtual
<!-- gh-comment-id:183462171 --> @jfoug commented on GitHub (Feb 12, 2016): here is the first version. SIMD gets proper speeds, EVEN for multi salts!!! ``` $ OMP_NUM_THREADS=4 ../run/john -test=3 -form=keystore Will run 4 OpenMP threads Benchmarking: keystore, Java KeyStore [SHA1 128/128 AVX 4x]... (4xOMP) DONE Many salts: 1531K c/s real, 412201 c/s virtual Only one salt: 1488K c/s real, 412831 c/s virtual $ make -s Make process completed. $ OMP_NUM_THREADS=4 ../run/john -test=3 -form=keystore Will run 4 OpenMP threads Benchmarking: keystore, Java KeyStore [SHA1 32/64]... (4xOMP) DONE Many salts: 567188 c/s real, 160019 c/s virtual Only one salt: 546636 c/s real, 155579 c/s virtual ```
Author
Owner

@jfoug commented on GitHub (Feb 13, 2016):

I think this gets the SIMD code working properly (that and some corrections to pass_gen.pl to generate better valid hashes).

github.com/magnumripper/JohnTheRipper@84e701ce95

<!-- gh-comment-id:183766596 --> @jfoug commented on GitHub (Feb 13, 2016): I think this gets the SIMD code working properly (that and some corrections to pass_gen.pl to generate better valid hashes). https://github.com/magnumripper/JohnTheRipper/commit/84e701ce95b8bafd9a4476fd5f850c6a9201d684
Author
Owner

@jfoug commented on GitHub (Feb 14, 2016):

keystore-cpu is holding it's own. Also, I am pretty sure keystore-opencl is broken, except if only working on 1 salt.

$ ../run/john -test=3 -form=keystore
Will run 8 OpenMP threads
Benchmarking: keystore, Java KeyStore [SHA1 256/256 AVX2 8x]... (8xOMP) DONE
Many salts:     6558K c/s real, 894516 c/s virtual
Only one salt:  5700K c/s real, 887123 c/s virtual

$ ../run/john -test=3 -form=keystore-opencl
Will run 8 OpenMP threads
Device 0: Tahiti [AMD Radeon R9 200 / HD 7900 Series]
Benchmarking: keystore-opencl, Java KeyStore [SHA1 32/64]... (8xOMP) DONE
Raw:    10732K c/s real, 92222K c/s virtual

# rebuilt with 3 test cases, and BENCH_LENGTH set to 0
$ make -s
Make process completed.

$ ../run/john -test=3 -form=keystore-opencl
Will run 8 OpenMP threads
Device 0: Tahiti [AMD Radeon R9 200 / HD 7900 Series]
Benchmarking: keystore-opencl, Java KeyStore [SHA1 32/64]... (8xOMP) FAILED (cmp_all(3))

So, 1 salt is only about 2x faster on my Tahiti, than on my Haswell in SIMD. Not bad. But I am almost certain, keystore-opencl is not going to work right for multi-salts

Validated a problem:

$ ./jtrts.pl keystore
-------------------------------------------------------------------------------
- JtR-TestSuite (jtrts). Version 1.13, Dec 21, 2014.  By, Jim Fougeron & others
- Testing:  John the Ripper 1.8.0.6-jumbo-1-1999-g84e701c+ OMP [cygwin 64-bit AVX2-ac]
--------------------------------------------------------------------------------
John Jumbo build detected.
form=keystore                     guesses: 1500 0:00:00:00 DONE  [PASSED]
.pot CHK:keystore                 guesses: 1500 0:00:00:00 DONE  [PASSED] (1500 val-pwd)
All tests passed without error.  Performed 1 tests.  Time used was 8 seconds

$ ./jtrts.pl keystore-opencl --pass=-skip
-------------------------------------------------------------------------------
- JtR-TestSuite (jtrts). Version 1.13, Dec 21, 2014.  By, Jim Fougeron & others
- Testing:  John the Ripper 1.8.0.6-jumbo-1-1999-g84e701c+ OMP [cygwin 64-bit AVX2-ac]
--------------------------------------------------------------------------------
John Jumbo build detected.
form=keystore                     guesses: 1431 -show=1431 0:00:00:17 DONE : Expected count(s) (1500)  [!!!FAILED1!!!  ret_val=0]
.pot CHK:keystore                 guesses: 1431 0:00:00:16 DONE  [PASSED] (1431 val-pwd)
Some tests had Errors. Performed 1 tests.  1 errors
Time used was 50 seconds
<!-- gh-comment-id:183781737 --> @jfoug commented on GitHub (Feb 14, 2016): keystore-cpu is holding it's own. Also, I am pretty sure keystore-opencl is broken, except if only working on 1 salt. ``` $ ../run/john -test=3 -form=keystore Will run 8 OpenMP threads Benchmarking: keystore, Java KeyStore [SHA1 256/256 AVX2 8x]... (8xOMP) DONE Many salts: 6558K c/s real, 894516 c/s virtual Only one salt: 5700K c/s real, 887123 c/s virtual $ ../run/john -test=3 -form=keystore-opencl Will run 8 OpenMP threads Device 0: Tahiti [AMD Radeon R9 200 / HD 7900 Series] Benchmarking: keystore-opencl, Java KeyStore [SHA1 32/64]... (8xOMP) DONE Raw: 10732K c/s real, 92222K c/s virtual # rebuilt with 3 test cases, and BENCH_LENGTH set to 0 $ make -s Make process completed. $ ../run/john -test=3 -form=keystore-opencl Will run 8 OpenMP threads Device 0: Tahiti [AMD Radeon R9 200 / HD 7900 Series] Benchmarking: keystore-opencl, Java KeyStore [SHA1 32/64]... (8xOMP) FAILED (cmp_all(3)) ``` So, 1 salt is only about 2x faster on my Tahiti, than on my Haswell in SIMD. Not bad. But I am almost certain, keystore-opencl is not going to work right for multi-salts Validated a problem: ``` $ ./jtrts.pl keystore ------------------------------------------------------------------------------- - JtR-TestSuite (jtrts). Version 1.13, Dec 21, 2014. By, Jim Fougeron & others - Testing: John the Ripper 1.8.0.6-jumbo-1-1999-g84e701c+ OMP [cygwin 64-bit AVX2-ac] -------------------------------------------------------------------------------- John Jumbo build detected. form=keystore guesses: 1500 0:00:00:00 DONE [PASSED] .pot CHK:keystore guesses: 1500 0:00:00:00 DONE [PASSED] (1500 val-pwd) All tests passed without error. Performed 1 tests. Time used was 8 seconds $ ./jtrts.pl keystore-opencl --pass=-skip ------------------------------------------------------------------------------- - JtR-TestSuite (jtrts). Version 1.13, Dec 21, 2014. By, Jim Fougeron & others - Testing: John the Ripper 1.8.0.6-jumbo-1-1999-g84e701c+ OMP [cygwin 64-bit AVX2-ac] -------------------------------------------------------------------------------- John Jumbo build detected. form=keystore guesses: 1431 -show=1431 0:00:00:17 DONE : Expected count(s) (1500) [!!!FAILED1!!! ret_val=0] .pot CHK:keystore guesses: 1431 0:00:00:16 DONE [PASSED] (1431 val-pwd) Some tests had Errors. Performed 1 tests. 1 errors Time used was 50 seconds ```
Author
Owner

@jfoug commented on GitHub (Feb 14, 2016):

keystore-opencl fixed and in the end, was MUCH easier with the multi-salt problem than SIMD build This is because in the GPU, we really only deal with 1 item at a time, each time (just like we do for oSSL), so the intermixing of PW lengths, and salts was not a problem.

However, the ability to 'gain' any performance gain by pre-computing the constant string of the salt (per password length), can not be done on the GPU, so overall performance gain is not super large. I did get 2.5x improvement in the speed on the GPU, so that puts GPU performance (my 7990 tahiti) at about 3x over my Haswell. Not great, BUT it is faster. All of my changes to squeeze out 2.5x in speed on the GPU version had nothing to do with algorithm optimization. it was mostly due to reduction in CPU to GPU memory movement and some better usage of variables on the GPU kernel.

<!-- gh-comment-id:183937468 --> @jfoug commented on GitHub (Feb 14, 2016): keystore-opencl fixed and in the end, was MUCH easier with the multi-salt problem than SIMD build This is because in the GPU, we really only deal with 1 item at a time, each time (just like we do for oSSL), so the intermixing of PW lengths, and salts was not a problem. However, the ability to 'gain' any performance gain by pre-computing the constant string of the salt (per password length), can not be done on the GPU, so overall performance gain is not super large. I did get 2.5x improvement in the speed on the GPU, so that puts GPU performance (my 7990 tahiti) at about 3x over my Haswell. Not great, BUT it is faster. All of my changes to squeeze out 2.5x in speed on the GPU version had nothing to do with algorithm optimization. it was mostly due to reduction in CPU to GPU memory movement and some better usage of variables on the GPU kernel.
Author
Owner

@jfoug commented on GitHub (Sep 11, 2016):

wbb3 in dynamic format (note not done in thin 'yet', since dyna has OMP disabled). This one would be fine in OMP, but it does work with fork (about 4x-5x faster on my test system).

7399705

<!-- gh-comment-id:246154896 --> @jfoug commented on GitHub (Sep 11, 2016): wbb3 in dynamic format (note not done in thin 'yet', since dyna has OMP disabled). This one would be fine in OMP, but it does work with fork (about 4x-5x faster on my test system). 7399705
Author
Owner

@magnumripper commented on GitHub (Dec 7, 2021):

I stumbled upon this and had a look at it. All seems to be straightened up and committed five years ago, so closing.

<!-- gh-comment-id:987465698 --> @magnumripper commented on GitHub (Dec 7, 2021): I stumbled upon this and had a look at it. All seems to be straightened up and committed five years ago, so closing.
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#1362
No description provided.