mirror of
https://github.com/openwall/john.git
synced 2026-12-03 18:45:21 +07:00
[GH-ISSUE #689] sha1crypt format: replace naive multiple gladman hmac sha1 with much better pbkdf2_hmac_sha1.h #339
Labels
No labels
bug
core branch
documentation
enhancement
enhancement
fixed - pending verify
fuzz bug
good first issue
invalid
invalid
maintenance/cleanup
new cracking mode
new format
new tool
non-trivial
notes/external issues
portability
question
RFC / discussion
testing
WONTFIX
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openwall/john#339
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jfoug on GitHub (Jul 2, 2014).
Original GitHub issue: https://github.com/openwall/john/issues/689
This could be made MUCH faster, along with the pbkdf2_hmac_sha1.h file also handles SIMD.
Switching from the naive gladman iterated mode, to the mode used in pbkdf2_hmac_sha1.h reduces the sha1's in half, nearly doubling the speed. The SIMD code can net 2.5-3.5x improvement in speed.
ALSO, we do have pbkdf2-hmac-sha1_fmt_plug.c We probably should see if we can remove crypt-sha1_fmt_plug totally, and handle these within the existing format (possibly only a change in prepare() or split() function to 'translate' the type)
@magnumripper commented on GitHub (Jul 3, 2014):
Is there anything in gladman_hmac that we don't have in pbkdf2-hmac-sha1? Maybe we should do a full cleanup and ditch the gladman code for good.
@jfoug commented on GitHub (Jul 3, 2014):
Yes we should
@magnumripper commented on GitHub (Aug 26, 2014):
The original issue is "invalid" though - because crypt-sha1 is not PBKDF2. It's "PBKDF1 from RFC 2898, but using hmac_sha1".
Can it be exploited in a similar way as PBKDF2-HMAC-xx?
@kholia commented on GitHub (Aug 27, 2014):
I like the gladman code since it is easy to use during the development of new plugins.
@jfoug commented on GitHub (Aug 27, 2014):
No, still valid, just not PBKDF2. The iterative hmac is still doing twice the work needed (plus more). The 'key' (1st sha1) is ALWAYS done with exact same buffer. Thus, we do that once, and then simply use the results of that to do the sha1 of the data buffer. Same concept as the PBKDF2, but in this case I think we do not append the buffer count or something. So by simply doing the hmac ourselves (in openssl), we would get about a 2.25 or better improvement in speed (the .25 is the time wasted setting up the 64 byte key value before the SHA1 code gets it). Right now, we are doing 4 sha1 hashes per iteration. With proper code, we would only be doing 2 (and none of the work on the pair of 64 byte ipad/opad buffers). This is the exact same concept as was done in the PBKDF2 code. I am pretty sure that any time you iterate over an HMAC where the key is static, you have this property.
If this was the algorithm:
Then we would be 100% forced into doing 4 SHA1 hashes per iteration, doubling the speed cost. BUT since we have a fixed PW, we compute the first half of each of the opad/ipad stuff, and store off the contexts, THEN we use those precomputed contexts and simply hash in the inout buffer on the first iteration, and then hash in the result buffer on iteration 1 to X.
The Gladman code while correct is very naive about this. I have brought this to his attention, but was told that this is 'supposed' to be time consuming, which I replied if speed was of no concern, then simply use interpretive Basic for everything. He did not reply, BUT has never taken time to improve his codes performance.
I might have a crack at this today, since the damn theme park I was going to is closed during the week :( I almost pulled a Clark Griswald and drove 350 miles with my daughter to a closed park, lol.
@jfoug commented on GitHub (Aug 27, 2014):
a96e547is a start. I simply used the code from the mscash2 wiki page I did (http://openwall.info/wiki/john/MSCash2_simple_code original reference code). The only difference is in pbkdf2, we accumulate all of the iteration output buffers, xoring them together. In pbkdf1, we only use the output buffer from the final iteration.Speed is not quite double. Not quite sure why. It may be that the gladman sha1 code is faster than the oSSL code on my machine.
Some other issues. There is validation code, and salt parsing in the crypt_all. The salt parsing is not 'too' bad, but the validation stuff should go away. Also, there is NO reason for crypt-sha_plug.c That code should be in the format..
Yes, I know there are warnings in my code. At this time, I do not care. I simply did it to show how trivial the change 'could' be to dump the naive Gladman code. If all you are doing is a single HMAC that is not in a chain where the password is the same, then the Gladman code is fine (for a CPU build). But for times when the first half of the HMACs are the same, then WHY compute it every time, i.e. VERY wasteful of CPU (GPU) resources.
Note, some of the reason I have left this only '1/2' done, is that I think we can EASILY change the pbkdf2_hmac_sha1.h file to handle pbkdf1 also. Simply wrap the xor code in a #define, and add a wrapped memcpy after the loop (again #defined the other way). Then we could defined PBKDF1_LOGIC before including that header to get the PBKDF1 logic. If that is not defined, then PBKDF2 logic would be used. Easy-peasy. But I have not done that just yet.
@jfoug commented on GitHub (Aug 27, 2014):
daa4919changes the pbkdf2-hmac-sha1.h file to also handle PBKDF1 algo using a #define. I changed the crypt-sha1_plug.c to now do the header include (common code). NOTE, this is only doing CPU at the moment. I have not added SIMD logic to it yet (but that should be trivial).Timings went from 19.1/s (original) to 34.7 (my hacked mscash logic from the wiki) to 34 using the common code in the header.
NOTE all 3 can still be build. There are just a couple things needing to be commented in or out. There is a #define in crypt-sha1_plug.c that will revert to naive Gladman code. Then at the very bottom of that file, if not in Gladman code, there are 2 different calls (one to the code function I added to the plug file, and the other in the common header file). One of those should be commented out, and the other not commented out.
I left the code the way it is so that @kholia can see that it is VERY easy to write this code, without having to revert to the non-optimal Gladman code. Yes, the code in the header did not do PBKDF1 until I saw the differences and added it. But it is REALLY REALLY trivial changes. If you want to prototype in Gladman code (especially if you get some pre-written code that uses it), fine. But once you get the algo correct, take the few minutes it takes and replace it with the better code that is in JtR. NOTE, this certainly will not always be the case, BUT it appears to be whenever someone uses most of the algorithms which do repetitive HMAC calls. Almost ALL algo's I have seen have had this bug (IMHO a bug). HMAC itself is buggy (IMHO). If they would have made ipad an opad each 8 bytes SHORTER, there would be no way to speed things up. Or it could be fixed with existing PBKDFx logic, by varying the password on every iteration. BUT there is no algo that does that, that I am aware of.
@magnumripper commented on GitHub (Aug 27, 2014):
Cool! At this low speed, maybe an OpenCL format would come handy. Not sure how common these hashes are? A 7970 can do two billion SHA1 per second and in this case there would be no bottlenecks... I think that would mean >155K c/s for 64000 rounds.
@jfoug commented on GitHub (Aug 27, 2014):
I would consider this bug now fixed with
c7fb3b8. I cleaned up the fmt a bit. I dropped the crypt-sha1_plug.c file totally. There were a couple issues in the sse2 code for PBKDF1 in pbkdf2_hmac_sha1.h that needed addressed (would never be compiled until I added it into this format).Speeds on my crappy lappy:
orig: 19.2/s
cpu: 34.4/s
sse2: 162/s
I think I need to add some to comments (my freeware statement), and I need to change algo string from PBKDF2 to PBKDF1. But other than that, I think we can stick a fork in this enhancement. The older diffs are still out there (done that way on purpose) to give @kholia insight into just how easy it is to use this 'better' code.
@magnumripper commented on GitHub (Aug 27, 2014):
This is at
d1a0455, 64-bit@magnumripper commented on GitHub (Aug 27, 2014):
Hmm what the h3ck is wrong with that #pragma?! It's correctly spelled and there are no invisible junk characters. The other warning is trivial to fix.
@jfoug commented on GitHub (Aug 27, 2014):
you are building an SIMD build, but not omp. I saw the error and ignored it. I can wrap another define around the pragma by itself. This was the same way the defines were in pbkdf2_hmac_sha1_fmt_plug.c and it does not trip a warning there. Not sure why.
@magnumripper commented on GitHub (Aug 27, 2014):
Yeah I just realized it. Fixed in
eccff2bBut the
FAILED (get_hash[0](1))remains - those warnings had nothing to do with that.@magnumripper commented on GitHub (Aug 27, 2014):
I thought it would work with OpenMP enabled but it still fails. So it's probably a 64-bit problem of some sort?
@jfoug commented on GitHub (Aug 27, 2014):
I will fire up 64 bit and get to the bottom of it. Yea, probably something in salt or somewhere. Possibly the snprintf is a problem. I will find it, should be easy.
@jfoug commented on GitHub (Aug 27, 2014):
I put some debugging code into get_salt: There is where the bug is (salt being made wrong).
ghost@ghostvb:/JtR/bleed/src$ ../run/john -test -form=sha1crypt
Will run 2 OpenMP threads
Benchmarking: sha1crypt, NetBSD's sha1crypt [PBKDF1-SHA1 4x SSE2]... (2xOMP)
cipher=$sha1$64000$wnUR8T1U$vt1TFQ50tBMFgkflAFAOer2CwdYZ salt=wnUR8T1U$sha1$64000 len=19 rounds=64000
cipher=$sha1$40000$jtNX3nZ2$hBNaIXkt4wBI2o5rsi8KejSjNqIq salt=jtNX3nZ2�$sha1$40000 len=21 rounds=40000
FAILED (get_hash0)
Now I just have to find out why.
@jfoug commented on GitHub (Aug 27, 2014):
Trivial and stupid bug (non terminated string)
3b7902a@magnumripper commented on GitHub (Aug 28, 2014):
👍
@jfoug commented on GitHub (Aug 28, 2014):
What was your prior speed?
@jfoug commented on GitHub (Aug 28, 2014):
Here were my final timings (crappy XOP laptop). All new timings are SSE2-XOP
32 bit OMP 343 / 89
32 bit nOMP 162
64 bit OMP 431 / 112
64 bit nOMP 227
Original:
32 bit nOMP 19.2
64 bit nOMP 23.4
This was about the improvement seen when I originally did this type change for mscash2 (about 8x to 10x, depending upon compiler/CPU)
@magnumripper commented on GitHub (Aug 28, 2014):
128 c/s, so 6.75x improvement. Still a really decent ROI.
@magnumripper commented on GitHub (Aug 28, 2014):
That math was wrong with an order of magnitude... It ended up as just 14.4K c/s in first try. At 64000 rounds we do 128002 SHA1's per key so at 2GH/s we should be able to bump it to around 17K c/s.