[GH-ISSUE #978] OpenCL formats: share more code with CPU formats #577

Closed
opened 2026-10-14 03:13:56 +07:00 by bsakaguchi · 35 comments
Owner

Originally created by @magnumripper on GitHub (Dec 30, 2014).
Original GitHub issue: https://github.com/openwall/john/issues/978

We (myself definitely included) keep forgetting to port CPU format fixes (most notably in valid) to the OpenCL/CUDA alternative or vice versa. We should put all such code in shared files so this can't even happen.

Sometimes the OpenCL formats differ (eg. less algos supported like in GPG) but that should still be fixed with one shared valid() that behaves a bit differently depending on some macro definition. But this means we need to put C code in headers - we can't use a shared function. I personally don't mind putting C code in headers and with latest improvements to Makefile targets we don't miss dependecies.

  • opencl_nt_fmt_plug.c
  • opencl_ntlmv2_fmt_plug.c
  • opencl_rar5_fmt_plug.c
  • opencl_oldoffice_fmt_plug.c
  • cuda_cryptsha512_fmt_plug.c partly done: move things like get_salt if possible, and
  • opencl_gpg_fmt_plug.c 46c55ea
  • cuda_mscash_fmt_plug.c ad2191f
  • opencl_mscash_fmt_plug.c ad2191f
  • opencl_mscash2_fmt_plug.c ad2191f
  • cuda_mscash2_fmt_plug.c ad2191f
  • opencl_encfs_fmt_plug.c f3a81f5
  • opencl_office2007_fmt_plug.c 77866c1
  • opencl_office2010_fmt_plug.c 77866c1
  • opencl_office2013_fmt_plug.c 77866c1
  • opencl_cryptmd5_fmt_plug.c 5c463ed
  • cuda_cryptmd5_fmt_plug.c 5c463ed
  • opencl_bf_fmt_plug.c 8c0a72f
  • opencl_sha1crypt_fmt_plug.c ca62969
  • cuda_cryptsha256_fmt_plug.c partly done: improved with a6f842c
  • opencl_cryptsha256_fmt_plug.c partly done: improved with a6f842c
  • opencl_cryptsha512_fmt_plug.c [To Claudio Ok] partly done: out of the base format files.
  • opencl_wpapsk_fmt_plug.c
  • cuda_wpapsk_fmt_plug.c
  • opencl_zip_fmt_plug.c 687dfd4
  • opencl_phpass_fmt_plug.c 687dfd4
  • cuda_phpass_fmt_plug.c 687dfd4
  • opencl_pbkdf2_hmac_md4_fmt_plug.c e3abe0d
  • opencl_pbkdf2_hmac_md5_fmt_plug.c 21e8c12
  • opencl_pbkdf2_hmac_sha1_fmt_plug.c eff5a92
  • opencl_pbkdf2_hmac_sha256_fmt_plug.c a8560ad
  • opencl_pbkdf2_hmac_sha512_fmt_plug.c c8be465
  • cuda_xsha512_fmt_plug.c ad2e472
  • opencl_xsha512_fmt_plug.c ad2e472
  • opencl_nsldaps_fmt_plug.c
  • opencl_rar_fmt_plug.c

END OF FIRST TODO PRIORITY HASHES

  • opencl_7z_fmt_plug.c
  • opencl_agilekeychain_fmt_plug.c
  • opencl_blockchain_fmt_plug.c
  • opencl_DES_fmt_plug.c
  • opencl_dmg_fmt_plug.c
  • opencl_keychain_fmt_plug.c
  • opencl_keyring_fmt_plug.c
  • opencl_krb5pa-md5_fmt_plug.c
  • opencl_krb5pa-sha1_fmt_plug.c
  • opencl_lotus5_fmt_plug.c
  • opencl_mysqlsha1_fmt_plug.c
  • opencl_o5logon_fmt_plug.c
  • opencl_odf_aes_fmt_plug.c
  • opencl_odf_fmt_plug.c
  • opencl_pwsafe_fmt_plug.c
  • cuda_pwsafe_fmt_plug.c
  • opencl_rakp_fmt_plug.c
  • opencl_rawmd4_fmt_plug.c
  • opencl_rawmd5_fmt_plug.c
  • opencl_rawsha1_fmt_plug.c
  • opencl_rawsha256_fmt_plug.c (not sure when)
  • opencl_rawsha512_fmt_plug.c 9493a85d66
  • opencl_rawsha512_ng_fmt_plug.c ad2e472
  • cuda_rawsha512_fmt_plug.c ad2e472
  • opencl_strip_fmt_plug.c 7858951b8b
  • opencl_sxc_fmt_plug.c
Originally created by @magnumripper on GitHub (Dec 30, 2014). Original GitHub issue: https://github.com/openwall/john/issues/978 We (myself definitely included) keep forgetting to port CPU format fixes (most notably in valid) to the OpenCL/CUDA alternative or vice versa. We should put all such code in shared files so this can't even happen. Sometimes the OpenCL formats differ (eg. less algos supported like in GPG) but that should still be fixed with one shared valid() that behaves a bit differently depending on some macro definition. But this means we need to put C code in headers - we can't use a shared function. I personally don't mind putting C code in headers and with latest improvements to Makefile targets we don't miss dependecies. - [ ] opencl_nt_fmt_plug.c - [ ] opencl_ntlmv2_fmt_plug.c - [ ] opencl_rar5_fmt_plug.c - [ ] opencl_oldoffice_fmt_plug.c - [ ] cuda_cryptsha512_fmt_plug.c `partly done: move things like get_salt if possible, and` - [x] opencl_gpg_fmt_plug.c 46c55ea - [x] cuda_mscash_fmt_plug.c ad2191f - [x] opencl_mscash_fmt_plug.c ad2191f - [x] opencl_mscash2_fmt_plug.c ad2191f - [x] cuda_mscash2_fmt_plug.c ad2191f - [x] opencl_encfs_fmt_plug.c f3a81f5 - [x] opencl_office2007_fmt_plug.c 77866c1 - [x] opencl_office2010_fmt_plug.c 77866c1 - [x] opencl_office2013_fmt_plug.c 77866c1 - [x] opencl_cryptmd5_fmt_plug.c 5c463ed - [x] cuda_cryptmd5_fmt_plug.c 5c463ed - [x] opencl_bf_fmt_plug.c 8c0a72f - [x] opencl_sha1crypt_fmt_plug.c ca62969 - [x] cuda_cryptsha256_fmt_plug.c `partly done: improved with a6f842c` - [x] opencl_cryptsha256_fmt_plug.c `partly done: improved with a6f842c` - [x] opencl_cryptsha512_fmt_plug.c `[To Claudio Ok] partly done: out of the base format files.` - [x] opencl_wpapsk_fmt_plug.c - [x] cuda_wpapsk_fmt_plug.c - [x] opencl_zip_fmt_plug.c 687dfd4 - [x] opencl_phpass_fmt_plug.c 687dfd4 - [x] cuda_phpass_fmt_plug.c 687dfd4 - [x] opencl_pbkdf2_hmac_md4_fmt_plug.c e3abe0d - [x] opencl_pbkdf2_hmac_md5_fmt_plug.c 21e8c12 - [x] opencl_pbkdf2_hmac_sha1_fmt_plug.c eff5a92 - [x] opencl_pbkdf2_hmac_sha256_fmt_plug.c a8560ad - [x] opencl_pbkdf2_hmac_sha512_fmt_plug.c c8be465 - [x] cuda_xsha512_fmt_plug.c ad2e472 - [x] opencl_xsha512_fmt_plug.c ad2e472 - [x] opencl_nsldaps_fmt_plug.c - [x] opencl_rar_fmt_plug.c END OF FIRST TODO PRIORITY HASHES - [ ] opencl_7z_fmt_plug.c - [ ] opencl_agilekeychain_fmt_plug.c - [x] opencl_blockchain_fmt_plug.c - [ ] opencl_DES_fmt_plug.c - [ ] opencl_dmg_fmt_plug.c - [x] opencl_keychain_fmt_plug.c - [ ] opencl_keyring_fmt_plug.c - [ ] opencl_krb5pa-md5_fmt_plug.c - [ ] opencl_krb5pa-sha1_fmt_plug.c - [ ] opencl_lotus5_fmt_plug.c - [ ] opencl_mysqlsha1_fmt_plug.c - [ ] opencl_o5logon_fmt_plug.c - [ ] opencl_odf_aes_fmt_plug.c - [x] opencl_odf_fmt_plug.c - [ ] opencl_pwsafe_fmt_plug.c - [ ] ~~cuda_pwsafe_fmt_plug.c~~ - [ ] opencl_rakp_fmt_plug.c - [ ] opencl_rawmd4_fmt_plug.c - [ ] opencl_rawmd5_fmt_plug.c - [ ] opencl_rawsha1_fmt_plug.c - [x] opencl_rawsha256_fmt_plug.c `(not sure when)` - [x] opencl_rawsha512_fmt_plug.c 9493a85d66c57a3d7659d7788283f4ad205102d6 - [x] opencl_rawsha512_ng_fmt_plug.c ad2e472 - [x] cuda_rawsha512_fmt_plug.c ad2e472 - [x] opencl_strip_fmt_plug.c 7858951b8be - [ ] ~~opencl_sxc_fmt_plug.c~~
bsakaguchi 2026-10-14 03:13:56 +07:00
Author
Owner

@jfoug commented on GitHub (Dec 30, 2014):

I do agree that it is less ugly putting C code in headers now with proper dependencies. It still makes sense to use non-static functions for many of these items. Even valid can be a common non-static function. Instead of #define, simply use runtime if() logic, then have a valid in each format, that calls the common valid, telling that common valid which hashes to look for.

I am really surprised about the layout of john. Solar got pretty anal about 'bloat' in the autoconfig stuff, BUT we have TONSSSS of bloat, in static functions, especially statics in headers that get included by multiple files. Ugly, ugly practice.

<!-- gh-comment-id:68371574 --> @jfoug commented on GitHub (Dec 30, 2014): I do agree that it is less ugly putting C code in headers now with proper dependencies. It still makes sense to use non-static functions for many of these items. Even valid can be a common non-static function. Instead of #define, simply use runtime if() logic, then have a valid in each format, that calls the common valid, telling that common valid which hashes to look for. I am really surprised about the layout of john. Solar got pretty anal about 'bloat' in the autoconfig stuff, BUT we have TONSSSS of bloat, in static functions, especially statics in headers that get included by multiple files. Ugly, ugly practice.
Author
Owner

@jfoug commented on GitHub (Dec 30, 2014):

I will start with the latest 2 that you noted.

NOTE, should we also address the cuda also ?? I would assume so.

Here are the functions that are good ones to put in common code. I will add to these as others are found.

valid()
get_salt()  (possibily)
prepare()
binary()
some tune cost functions
test[] array of data.
Other large 'common' data should also be moved.
Other 'common' functions that have little to do with the static data of the format.
<!-- gh-comment-id:68371795 --> @jfoug commented on GitHub (Dec 30, 2014): I will start with the latest 2 that you noted. NOTE, should we also address the cuda also ?? I would assume so. Here are the functions that are good ones to put in common code. I will add to these as others are found. ``` valid() get_salt() (possibily) prepare() binary() some tune cost functions test[] array of data. Other large 'common' data should also be moved. Other 'common' functions that have little to do with the static data of the format. ```
Author
Owner

@jfoug commented on GitHub (Dec 30, 2014):

What about this one: phpass
phpass-cpu is a thin format. prepare is mostly done in dyna
phpass-cuda and opencl, we could use a common valid.

Almost all the 'common' stuff in phpass-cpu is in dyna. I find no way to really do that in a common code way. But the cuda/opencl certainly could use common functions.

<!-- gh-comment-id:68383509 --> @jfoug commented on GitHub (Dec 30, 2014): What about this one: phpass phpass-cpu is a thin format. prepare is mostly done in dyna phpass-cuda and opencl, we could use a common valid. Almost all the 'common' stuff in phpass-cpu is in dyna. I find no way to really do that in a common code way. But the cuda/opencl certainly could use common functions.
Author
Owner

@magnumripper commented on GitHub (Dec 30, 2014):

I am really surprised about the layout of john. Solar got pretty anal about 'bloat' in the autoconfig stuff, BUT we have TONSSSS of bloat, in static functions, especially statics in headers that get included by multiple files. Ugly, ugly practice.

Is there any statics in headers in core john? I thought only Jumbo had that.

<!-- gh-comment-id:68398146 --> @magnumripper commented on GitHub (Dec 30, 2014): > I am really surprised about the layout of john. Solar got pretty anal about 'bloat' in the autoconfig stuff, BUT we have TONSSSS of bloat, in static functions, especially statics in headers that get included by multiple files. Ugly, ugly practice. Is there any statics in headers in core john? I thought only Jumbo had that.
Author
Owner

@magnumripper commented on GitHub (Dec 30, 2014):

NOTE, should we also address the cuda also ?? I would assume so.

Yes of course. Although CUDA is pretty much dead in Jumbo, no-one actively develops any CUDA stuff. And why would they, now that OpenCL is as good and non-proprietary.

Almost all the 'common' stuff in phpass-cpu is in dyna. I find no way to really do that in a common code way. But the cuda/opencl certainly could use common functions.

Right, the CUDA and OpenCL ones should share.

<!-- gh-comment-id:68398621 --> @magnumripper commented on GitHub (Dec 30, 2014): > NOTE, should we also address the cuda also ?? I would assume so. Yes of course. Although CUDA is pretty much dead in Jumbo, no-one actively develops any CUDA stuff. And why would they, now that OpenCL is as good _and_ non-proprietary. > Almost all the 'common' stuff in phpass-cpu is in dyna. I find no way to really do that in a common code way. But the cuda/opencl certainly could use common functions. Right, the CUDA and OpenCL ones should share.
Author
Owner

@jfoug commented on GitHub (Dec 30, 2014):

Is there any statics in headers in core john? I thought only Jumbo had that.

yes (only in jumbo, I think), but autoconf is also ONLY jumbo, and Solar has beat his chest listing that core will not get autoconf. Yet:

Solar got pretty anal about 'bloat' in the autoconfig stuff

<!-- gh-comment-id:68399196 --> @jfoug commented on GitHub (Dec 30, 2014): > Is there any statics in headers in core john? I thought only Jumbo had that. yes (only in jumbo, I think), but autoconf is also ONLY jumbo, and Solar has beat his chest listing that core will not get autoconf. Yet: > > Solar got pretty anal about 'bloat' in the autoconfig stuff
Author
Owner

@jfoug commented on GitHub (Dec 30, 2014):

Of the 2 CPU-opencl common code cleanups I have done, do you think either of these was any harder to do using a common non-static file, than a header with static functions? I personally find it much cleaner to put declaration stuff in the header (the salt struct, the function declarations, etc), and code into a *_common_plug.c file.

One of the biggest reasons for all the statics, is due to having such a COMMON interface, that many people will want to name their functions and data objects the same across formats. That is a no-no on C, since the linker will complain. C++ could easily work around that with namespaces, BUT the end result is 100% the same, duplicated functions both in code, AND in the stored and running binary image.

Now, in core, since it is SO tiny, this is really not that big of an issue. But with 200+ 'real' formats now in jumbo, it is a problem, and only getting worse as time goes on. I guess I will simply stop whining about the issue, and simply start to work on this myself, hopefully to get some 'prototypes' on how to easily do it, so that others can continue in that method. Simply 'always' assuming that all functions have to be static, is really pretty short sighted. Yes, many of them DO have to be static. People love the name crypt_all() as a function. That is just fine, since each format will have to write a specific special purpose function to do this, so having it be static and IN the .c file make 100% sense. But other ones that really 'should' be the same function in multiple formats, 'could' be static and in a .h file, and that does unify the code. However, it does bloat up the binary and memory image, and gains almost nothing. Yes, I do understand the need for inline, BUT not every function should be inlined. In fact, often very few functions get benefit from inlining. Inlining something with a call to some oSSL hash is really just silly.

I will just continue forward. This cleanup is not going to be done overnight. There are a LOT of formats to do, and these take a while. It is pretty easy work, but time consuming. When done (or the closer to done we get), things will be better, when someone lists something wrong in a prepare, split or valid, and we fix it, it (should) be fixed for all the formats, and that can ONLY be a good thing.

<!-- gh-comment-id:68400668 --> @jfoug commented on GitHub (Dec 30, 2014): Of the 2 CPU-opencl common code cleanups I have done, do you think either of these was any harder to do using a common non-static file, than a header with static functions? I personally find it much cleaner to put declaration stuff in the header (the salt struct, the function declarations, etc), and code into a *_common_plug.c file. One of the biggest reasons for all the statics, is due to having such a COMMON interface, that many people will want to name their functions and data objects the same across formats. That is a no-no on C, since the linker will complain. C++ could easily work around that with namespaces, BUT the end result is 100% the same, duplicated functions both in code, AND in the stored and running binary image. Now, in core, since it is SO tiny, this is really not that big of an issue. But with 200+ 'real' formats now in jumbo, it is a problem, and only getting worse as time goes on. I guess I will simply stop whining about the issue, and simply start to work on this myself, hopefully to get some 'prototypes' on how to easily do it, so that others can continue in that method. Simply 'always' assuming that all functions have to be static, is really pretty short sighted. Yes, many of them DO have to be static. People love the name crypt_all() as a function. That is just fine, since each format will have to write a specific special purpose function to do this, so having it be static and IN the .c file make 100% sense. But other ones that really 'should' be the same function in multiple formats, 'could' be static and in a .h file, and that does unify the code. However, it does bloat up the binary and memory image, and gains almost nothing. Yes, I do understand the need for inline, BUT not every function should be inlined. In fact, often very few functions get benefit from inlining. Inlining something with a call to some oSSL hash is really just silly. I will just continue forward. This cleanup is not going to be done overnight. There are a LOT of formats to do, and these take a while. It is pretty easy work, but time consuming. When done (or the closer to done we get), things will be better, when someone lists something wrong in a prepare, split or valid, and we fix it, it (should) be fixed for all the formats, and that can ONLY be a good thing.
Author
Owner

@magnumripper commented on GitHub (Dec 30, 2014):

I never said we shouldn't make non-static functions like office_valid() - do it any way you like! What I wrote was in the context of actual differences in the functions (as in GPG) and like you said, even that can be worked around.

<!-- gh-comment-id:68401337 --> @magnumripper commented on GitHub (Dec 30, 2014): I never said we shouldn't make non-static functions like office_valid() - do it any way you like! What I wrote was in the context of actual differences in the functions (as in GPG) and like you said, even that can be worked around.
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

@magnumripper do you have any idea where this hash came from? {"$1$saltstring$YmP55hH3qcHg2cCffyxrq/", "ala"},

It is in opencl-cryptmd5 The salt is 10 bytes long, but valid for MD5_fmt.c (and even my pass_gen.pl) list valid salt length as 8. I changed valid to accept 10 byte salts, put this hash into MD5_fmt.c and it works fine. BUT is this hash even valid? If so, what is the max salt length?

<!-- gh-comment-id:68413562 --> @jfoug commented on GitHub (Dec 31, 2014): @magnumripper do you have any idea where this hash came from? {"$1$saltstring$YmP55hH3qcHg2cCffyxrq/", "ala"}, It is in opencl-cryptmd5 The salt is 10 bytes long, but valid for MD5_fmt.c (and even my pass_gen.pl) list valid salt length as 8. I changed valid to accept 10 byte salts, put this hash into MD5_fmt.c and it works fine. BUT is this hash even valid? If so, what is the max salt length?
Author
Owner

@magnumripper commented on GitHub (Dec 31, 2014):

Git tells me @ukasz added it in 6dd9322, that's all I know.

<!-- gh-comment-id:68414572 --> @magnumripper commented on GitHub (Dec 31, 2014): Git tells me @ukasz added it in 6dd9322, that's all I know.
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

Both md5crypt and opencl only USE 8 bytes of salt. But opencl has this 10 byte salt in ST and it's valid did not Len check. I think I'm keeping 8 byte salt check in valid, and will fix that ST hash

<!-- gh-comment-id:68416178 --> @jfoug commented on GitHub (Dec 31, 2014): Both md5crypt and opencl only USE 8 bytes of salt. But opencl has this 10 byte salt in ST and it's valid did not Len check. I think I'm keeping 8 byte salt check in valid, and will fix that ST hash
Author
Owner

@magnumripper commented on GitHub (Dec 31, 2014):

If that limit is decreased to 8 the optimizers might end up with significantly faster code. GPUs are unpredictable - some changes make a lot of impact. And 8 is a whole lot more even than 10...

<!-- gh-comment-id:68416259 --> @magnumripper commented on GitHub (Dec 31, 2014): If that limit is decreased to 8 the optimizers might end up with significantly faster code. GPUs are unpredictable - some changes make a lot of impact. And 8 is a whole lot more even than 10...
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

It is only valid that's promiscuous in opencl. Setsalt only uses at most first 8 chars.

<!-- gh-comment-id:68416371 --> @jfoug commented on GitHub (Dec 31, 2014): It is only valid that's promiscuous in opencl. Setsalt only uses at most first 8 chars.
Author
Owner

@magnumripper commented on GitHub (Dec 31, 2014):

LOL, so that hash is a valid one for a salt of "saltstri"? I did wonder what the heck was going on.

<!-- gh-comment-id:68416928 --> @magnumripper commented on GitHub (Dec 31, 2014): LOL, so that hash is a valid one for a salt of "saltstri"? I did wonder what the heck was going on.
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

Yup, I cut off the 2 bytes, it now passes that 'proper' valid check, and the hash is fine.. IF we do have ITW hashes like this, we could fix them up in prepare/split

<!-- gh-comment-id:68417789 --> @jfoug commented on GitHub (Dec 31, 2014): Yup, I cut off the 2 bytes, it now passes that 'proper' valid check, and the hash is fine.. IF we do have ITW hashes like this, we could fix them up in prepare/split
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

I am at a roadblock. I have what I 'think' to be all the code needed, to get the {smd5} working within opencl, but it is not working :( Can you have a look.

I think the only diff is that {smd5} does not prefix itself to the salt like 1 or apr1 do. I made changes to the salt() (actually no change needed), and to the cryptmd5_kernel.cl, thinking I knew what I was doing. The only 'real' change, was in the kernel, I had to do an else if() to check for a 'a' character in ctx_update_prefix(), so that a 'null' prefix character (which the {smd5} would get, would not add any data to the buffer.

But if I put {smd5} hashes into the test string, they fail, and I do not know what I did wrong. If you do not have time for this, then I will have to make adjustments to valid, so that the GPU version will not validate {smd5} hashes, since they are not working.

<!-- gh-comment-id:68418024 --> @jfoug commented on GitHub (Dec 31, 2014): I am at a roadblock. I have what I 'think' to be all the code needed, to get the {smd5} working within opencl, but it is not working :( Can you have a look. I think the only diff is that {smd5} does not prefix itself to the salt like $1$ or $apr1$ do. I made changes to the salt() (actually no change needed), and to the cryptmd5_kernel.cl, thinking I knew what I was doing. The only 'real' change, was in the kernel, I had to do an else if() to check for a 'a' character in ctx_update_prefix(), so that a 'null' prefix character (which the {smd5} would get, would not add any data to the buffer. But if I put {smd5} hashes into the test string, they fail, and I do not know what I did wrong. If you do not have time for this, then I will have to make adjustments to valid, so that the GPU version will not validate {smd5} hashes, since they are not working.
Author
Owner

@magnumripper commented on GitHub (Dec 31, 2014):

Cool, just hand it to me. Maybe you can just commit it, but with those test vectors commented out?

<!-- gh-comment-id:68418249 --> @magnumripper commented on GitHub (Dec 31, 2014): Cool, just hand it to me. Maybe you can just commit it, but with those test vectors commented out?
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

My bad, I was editing the .cl in my other work folder (my VC folder). I had *.cl excluded from my win-merge so was not copying the fix. I thought I was editing the one in my normal compile test dir for cygwin.

Now with the right *.cl file there, I think it is working just fine. I will also have these changes into the cuda format.

NOTE, the salt() function is ripe for common code, but the salt structure is just a bit different. I will let that one slide. Even between cuda/opencl there are differences.

NOTE, I can not test cuda, so will be committing blind.

<!-- gh-comment-id:68419178 --> @jfoug commented on GitHub (Dec 31, 2014): My bad, I was editing the .cl in my other work folder (my VC folder). I had *.cl excluded from my win-merge so was not copying the fix. I thought I was editing the one in my normal compile test dir for cygwin. Now with the right *.cl file there, I think it is working just fine. I will also have these changes into the cuda format. NOTE, the salt() function is ripe for common code, but the salt structure is just a bit different. I will let that one slide. Even between cuda/opencl there are differences. NOTE, I can not test cuda, so will be committing blind.
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

Scratch that, I was wrong again. The {smd5} is not working (and likely would not be working for cuda). I have all the code there, but will have the ST vars commented out.

I will be committing soon. NOTE, valid() will say {smd5} is valid, so unless you can figure out why these are failing, I will have to change that, so that {smd5} is only valid for CPU.

<!-- gh-comment-id:68419403 --> @jfoug commented on GitHub (Dec 31, 2014): Scratch that, I was wrong again. The {smd5} is not working (and likely would not be working for cuda). I have all the code there, but will have the ST vars commented out. I will be committing soon. NOTE, valid() will say {smd5} is valid, so unless you can figure out why these are failing, I will have to change that, so that {smd5} is only valid for CPU.
Author
Owner

@magnumripper commented on GitHub (Dec 31, 2014):

I am pretty sure you know that in order for an OpenCL format to run-time compile your changed kernel, you still have to run "make" since that's what copies it to the ../run/kernels folder. Just putting it out so you don't fall for a silly mistake.

<!-- gh-comment-id:68419596 --> @magnumripper commented on GitHub (Dec 31, 2014): I am pretty sure you know that in order for an OpenCL format to run-time compile your _changed_ kernel, you still have to run "make" since that's what copies it to the ../run/kernels folder. Just putting it out so you don't fall for a silly mistake.
Author
Owner

@magnumripper commented on GitHub (Dec 31, 2014):

I'll probably be offline for 48+ hours from now. Happy new year :-P

<!-- gh-comment-id:68419689 --> @magnumripper commented on GitHub (Dec 31, 2014): I'll probably be offline for 48+ hours from now. Happy new year :-P
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

HNY to you also! Yes, I know, AND the file has to be touched. Some copying of lines only (using winmerge) does not touch the file, so I have to be careful with that also. But I touched ( by hand) the .cl and did a make.

<!-- gh-comment-id:68420612 --> @jfoug commented on GitHub (Dec 31, 2014): HNY to you also! Yes, I know, AND the file has to be touched. Some copying of lines only (using winmerge) does not touch the file, so I have to be careful with that also. But I touched ( by hand) the .cl and did a make.
Author
Owner

@jfoug commented on GitHub (Dec 31, 2014):

I ordered the todo list in what I 'think' is highest usage order. The bottom set of hashes is unordered. I have no idea about those hashes. But I think the top set is most used. Well, raw-md5 is probably most used, but since JtR's gpu code for these lags behind tools like hashcat, I really think few people use the JTR code for them, so they are on the 'do later' list.

<!-- gh-comment-id:68426942 --> @jfoug commented on GitHub (Dec 31, 2014): I ordered the todo list in what I 'think' is highest usage order. The bottom set of hashes is unordered. I have no idea about those hashes. But I think the top set is most used. Well, raw-md5 is probably most used, but since JtR's gpu code for these lags behind tools like hashcat, I really think few people use the JTR code for them, so they are on the 'do later' list.
Author
Owner

@magnumripper commented on GitHub (Feb 7, 2015):

I ticked off wpapsk, afaik they were properly done to start with.

<!-- gh-comment-id:73380690 --> @magnumripper commented on GitHub (Feb 7, 2015): I ticked off wpapsk, afaik they were properly done to start with.
Author
Owner

@magnumripper commented on GitHub (Mar 22, 2015):

http://www.openwall.com/lists/john-dev/2015/03/22/1

<!-- gh-comment-id:84655273 --> @magnumripper commented on GitHub (Mar 22, 2015): http://www.openwall.com/lists/john-dev/2015/03/22/1
Author
Owner

@ghost commented on GitHub (Mar 23, 2015):

Using the old way, we can discuss a path like this one for [RAW / X]-SHA512. BTW: -ng is slower than the regular version.

From 078d80e85ef2854e17dd9cecafe16019bde372d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Claudio=20Andr=C3=A9?= <claudioandre.br@gmail.com>
Date: Sun, 22 Mar 2015 22:35:55 -0300
Subject: [PATCH 2/2] OpenCL share code with CPU formats in RAW-SHA512.

---
 src/XSHA512_fmt_plug.c          |  79 +++----------------
 src/opencl_rawsha512.h          |   6 +-
 src/opencl_rawsha512_fmt_plug.c | 113 +++++++--------------------
 src/rawSHA512_common.h          | 166 ++++++++++++++++++++++++++++++++++++++++
 src/rawSHA512_fmt_plug.c        |  51 +-----------
 5 files changed, 208 insertions(+), 207 deletions(-)
 create mode 100644 src/rawSHA512_common.h

diff --git a/src/XSHA512_fmt_plug.c b/src/XSHA512_fmt_plug.c
index 5f2c200..1f9adf7 100644
--- a/src/XSHA512_fmt_plug.c
+++ b/src/XSHA512_fmt_plug.c
@@ -34,15 +34,11 @@ john_register_one(&fmt_XSHA512);
 #define FORMAT_LABEL           "xsha512"
 #define FORMAT_NAME            "Mac OS X 10.7"
 #define ALGORITHM_NAME         "SHA512 " SHA512_ALGORITHM_NAME
-#define FORMAT_TAG              "$LION$"
-#define TAG_LENGTH              (sizeof(FORMAT_TAG) - 1)
-

 #define BENCHMARK_COMMENT      ""
 #define BENCHMARK_LENGTH       0

 #define PLAINTEXT_LENGTH       107
-#define CIPHERTEXT_LENGTH      136

 #define BINARY_SIZE            64
 #define BINARY_ALIGN           8
@@ -64,6 +60,10 @@ john_register_one(&fmt_XSHA512);
 #undef PRECOMPUTE_CTX_FOR_SALT
 #endif

+#define _XSHA512_H
+#include "rawSHA512_common.h"
+#undef _XSHA512_H
+
 static struct fmt_tests tests[] = {
    {"bb0489df7b073e715f19f83fd52d08ede24243554450f7159dd65c100298a5820525b55320f48182491b72b4c4ba50d7b0e281c1d98e06591a5e9c6167f42a742f0359c7", "password"},
    {"$LION$74911f723bd2f66a3255e0af4b85c639776d510b63f0b939c432ab6e082286c47586f19b4e2f3aab74229ae124ccb11e916a7a1c9b29c64bd6b0fd6cbd22e7b1f0ba1673", "hello"},
@@ -137,67 +137,6 @@ static void done(void)
    MEM_FREE(saved_key);
 }

-static int valid(char *ciphertext, struct fmt_main *self)
-{
-   char *pos;
-
-   /* Require lowercase hex digits (assume ASCII) */
-   pos = ciphertext;
-   if (strncmp(pos, FORMAT_TAG, TAG_LENGTH))
-       return 0;
-   pos += 6;
-   while (atoi16[ARCH_INDEX(*pos)] != 0x7F && (*pos <= '9' || *pos >= 'a'))
-       pos++;
-   return !*pos && pos - ciphertext == CIPHERTEXT_LENGTH+6;
-}
-
-static char *prepare(char *split_fields[10], struct fmt_main *self) {
-   char Buf[200];
-   if (!strncmp(split_fields[1], FORMAT_TAG, TAG_LENGTH))
-       return split_fields[1];
-   if (split_fields[0] && strlen(split_fields[0]) == CIPHERTEXT_LENGTH) {
-       sprintf(Buf, "%s%s", FORMAT_TAG, split_fields[0]);
-       if (valid(Buf, self)) {
-           char *cp = mem_alloc_tiny(CIPHERTEXT_LENGTH+7, MEM_ALIGN_NONE);
-           strcpy(cp, Buf);
-           return cp;
-       }
-   }
-   if (strlen(split_fields[1]) == CIPHERTEXT_LENGTH) {
-       sprintf(Buf, "%s%s", FORMAT_TAG, split_fields[1]);
-       if (valid(Buf, self)) {
-           char *cp = mem_alloc_tiny(CIPHERTEXT_LENGTH+7, MEM_ALIGN_NONE);
-           strcpy(cp, Buf);
-           return cp;
-       }
-   }
-   return split_fields[1];
-}
-
-static void *get_binary(char *ciphertext)
-{
-   static union {
-       unsigned char c[BINARY_SIZE];
-       ARCH_WORD_64 dummy;
-   } buf;
-   unsigned char *out = buf.c;
-   char *p;
-   int i;
-
-   ciphertext += 6;
-   p = ciphertext + 8;
-   for (i = 0; i < sizeof(buf.c); i++) {
-       out[i] =
-           (atoi16[ARCH_INDEX(*p)] << 4) |
-           atoi16[ARCH_INDEX(p[1])];
-       p += 2;
-   }
-#ifdef SIMD_COEF_64
-   alter_endianity_to_BE64 (out, BINARY_SIZE/8);
-#endif
-   return out;
-}
-
 static void *salt(char *ciphertext)
 {
    static union {
@@ -208,7 +147,7 @@ static void *salt(char *ciphertext)
    char *p;
    int i;

-   ciphertext += TAG_LENGTH;
+   ciphertext += XSHA512_TAG_LENGTH;
    p = ciphertext;
    for (i = 0; i < sizeof(buf.c); i++) {
        out[i] =
@@ -467,10 +406,10 @@ struct fmt_main fmt_XSHA512 = {
        init,
        done,
        fmt_default_reset,
-       prepare,
-       valid,
-       fmt_default_split,
-       get_binary,
+       prepare_xsha512,
+       valid_xsha512,
+       split_xsha512,
+       binary_xsha512,
        salt,
 #if FMT_MAIN_VERSION > 11
        { NULL },
diff --git a/src/opencl_rawsha512.h b/src/opencl_rawsha512.h
index 9136d6c..5693b61 100644
--- a/src/opencl_rawsha512.h
+++ b/src/opencl_rawsha512.h
@@ -20,12 +20,10 @@

 //Constants.
 #define PLAINTEXT_LENGTH        55
-#define CIPHERTEXT_LENGTH_RAW   128
-#define CIPHERTEXT_LENGTH_X     136

 #define BUFFER_SIZE             56      /* PLAINTEXT_LENGTH multiple of 4 */
-#define BINARY_SIZE             4
-#define FULL_BINARY_SIZE        64
+#define SHORT_BINARY_SIZE   4
+#define BINARY_SIZE        64
 #define BINARY_ALIGN            4
 #define SALT_SIZE_RAW           0
 #define SALT_SIZE_X             4
diff --git a/src/opencl_rawsha512_fmt_plug.c b/src/opencl_rawsha512_fmt_plug.c
index 2c83624..299308e 100644
--- a/src/opencl_rawsha512_fmt_plug.c
+++ b/src/opencl_rawsha512_fmt_plug.c
@@ -32,6 +32,7 @@ john_register_one(&fmt_opencl_xsha512);

 #include "sha.h"
 #include "sha2.h"
+#include "johnswap.h"
 #include "common-opencl.h"
 #include "config.h"
 #include "options.h"
@@ -39,7 +40,7 @@ john_register_one(&fmt_opencl_xsha512);

 #define FORMAT_LABEL           "Raw-SHA512-opencl"
 #define FORMAT_NAME            ""
-#define RAW_FORMAT_TAG         "$SHA512$"
+
 #define X_FORMAT_LABEL         "XSHA512-opencl"
 #define X_FORMAT_NAME          "Mac OS X 10.7 salted"

@@ -68,20 +69,26 @@ static size_t offset = 0, offset_idx = 0;

 static int crypt_all(int *pcount, struct db_salt *_salt);

+#define _RAWSHA512_H
+#define _XSHA512_H
+#include "rawSHA512_common.h"
+#undef _RAWSHA512_H
+#undef _XSHA512_H
+
 //This file contains auto-tuning routine(s). It has to be included after formats definitions.
 #include "opencl-autotune.h"
 #include "memdbg.h"

 static struct fmt_tests raw_tests[] = {
    {"f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"},
-   {RAW_FORMAT_TAG "f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"},
+   {FORMAT_TAG "f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"},
    {"b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86", "password"},
    {"2c80f4c2b3db6b677d328775be4d38c8d8cd9a4464c3b6273644fb148f855e3db51bc33b54f3f6fa1f5f52060509f0e4d350bb0c7f51947728303999c6eff446", "john-user"},
    {"71ebcb1eccd7ea22bd8cebaec735a43f1f7164d003dacdeb06e0de4a6d9f64d123b00a45227db815081b1008d1a1bbad4c39bde770a2c23308ff1b09418dd7ed", "ALLCAPS"},
    {"82244918c2e45fbaa00c7c7d52eb61f309a37e2f33ea1fba78e61b4140efa95731eec849de02ee16aa31c82848b51fb7b7fbae62f50df6e150a8a85e70fa740c", "TestTESTt3st"},
    {"fa585d89c851dd338a70dcf535aa2a92fee7836dd6aff1226583e88e0996293f16bc009c652826e0fc5c706695a03cddce372f139eff4d13959da6f1f5d3eabe", "12345678"},
-   {RAW_FORMAT_TAG "fa585d89c851dd338a70dcf535aa2a92fee7836dd6aff1226583e88e0996293f16bc009c652826e0fc5c706695a03cddce372f139eff4d13959da6f1f5d3eabe", "12345678"},
-   {RAW_FORMAT_TAG "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", ""},
+   {FORMAT_TAG "fa585d89c851dd338a70dcf535aa2a92fee7836dd6aff1226583e88e0996293f16bc009c652826e0fc5c706695a03cddce372f139eff4d13959da6f1f5d3eabe", "12345678"},
+   {FORMAT_TAG "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", ""},
    {"c96f1c1260074832bd3068ddd29e733090285dfc65939555dbbcafb27834957d15d9c509481cc7df0e2a7e21429783ba573036b78f5284f9928b5fef02a791ef", "mot\xf6rhead"},
    {"aa3b7bdd98ec44af1f395bbd5f7f27a5cd9569d794d032747323bf4b1521fbe7725875a68b440abdf0559de5015baf873bb9c01cae63ecea93ad547a7397416e", "12345678901234567890"},
    {"db9981645857e59805132f7699e78bbcf39f69380a41aac8e6fa158a0593f2017ffe48764687aa855dae3023fcceefd51a1551d57730423df18503e80ba381ba", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"},
@@ -413,76 +420,20 @@ static void done(void) {
    HANDLE_CLERROR(clReleaseProgram(program[gpu_id]), "Release Program");
 }

-/* ------- Check if the ciphertext if a valid SHA-512 ------- */
-static int valid(char * ciphertext, struct fmt_main * self) {
-   char *p, *q;
-
-   p = ciphertext;
-   if (!strncmp(p, "$SHA512$", 8))
-       p += 8;
-
-   q = p;
-   while (atoi16[ARCH_INDEX(*q)] != 0x7F)
-       q++;
-   return !*q && q - p == CIPHERTEXT_LENGTH_RAW;
-}
-
-static char *split(char *ciphertext, int index, struct fmt_main *pFmt) {
-
-   static char * out;
-
-   if (!out) out = mem_alloc_tiny(8 + CIPHERTEXT_LENGTH_RAW + 1, MEM_ALIGN_WORD);
-
-   if (!strncmp(ciphertext, "$SHA512$", 8))
-       ciphertext += 8;
-
-   memcpy(out, "$SHA512$", 8);
-   memcpy(out + 8, ciphertext, CIPHERTEXT_LENGTH_RAW + 1);
-   strlwr(out + 8);
-   return out;
-}
-
-static int valid_x(char * ciphertext, struct fmt_main * self) {
-   char *p, *q;
-
-   p = ciphertext;
-   if (!strncmp(p, "$LION$", 6))
-       p += 6;
-
-   q = p;
-   while (atoi16[ARCH_INDEX(*q)] != 0x7F)
-       q++;
-   return !*q && q - p == CIPHERTEXT_LENGTH_X;
-}
-
-static char *split_x(char *ciphertext, int index, struct fmt_main *pFmt) {
-   static char * out;
-
-   if (!out) out = mem_alloc_tiny(8 + CIPHERTEXT_LENGTH_X + 1, MEM_ALIGN_WORD);
-
-   if (!strncmp(ciphertext, "$LION$", 6))
-       return ciphertext;
-
-   memcpy(out, "$LION$", 6);
-   memcpy(out + 6, ciphertext, CIPHERTEXT_LENGTH_X + 1);
-   strlwr(out + 6);
-   return out;
-}
-
 /* ------- To binary functions ------- */
-static void * get_binary(char *ciphertext) {
+static void * get_short_binary(char *ciphertext) {
    static unsigned char *out;
    uint64_t * b;
    char *p;
    int i;

-   if (!out) out = mem_alloc_tiny(FULL_BINARY_SIZE, MEM_ALIGN_WORD);
+   if (!out) out = mem_alloc_tiny(BINARY_SIZE, MEM_ALIGN_WORD);

    if (salted_format)
        ciphertext += 6;

    p = ciphertext + 8;
-   for (i = 0; i < FULL_BINARY_SIZE; i++) {
+   for (i = 0; i < BINARY_SIZE; i++) {
        out[i] =
                (atoi16[ARCH_INDEX(*p)] << 4) |
                 atoi16[ARCH_INDEX(p[1])];
@@ -495,22 +446,14 @@ static void * get_binary(char *ciphertext) {
 }

 static void * get_full_binary(char *ciphertext) {
-   static unsigned char *out;
-   char *p;
-   int i;
-
-   if (!out) out = mem_alloc_tiny(FULL_BINARY_SIZE, MEM_ALIGN_WORD);
+   unsigned char * out;

    if (salted_format)
-       ciphertext += 6;
+       out = binary_xsha512(ciphertext);
+   else
+       out = binary(ciphertext);

-   p = ciphertext + 8;
-   for (i = 0; i < FULL_BINARY_SIZE; i++) {
-       out[i] =
-               (atoi16[ARCH_INDEX(*p)] << 4) |
-                atoi16[ARCH_INDEX(p[1])];
-       p += 2;
-   }
+   alter_endianity_to_BE64 (out, BINARY_SIZE/8);

    return out;
 }
@@ -601,7 +544,7 @@ static int cmp_exact(char *source, int index) {
    crypt_one(index, &full_hash);

    binary = (uint64_t *) get_full_binary(source);
-   return !memcmp(binary, (void *) &full_hash, FULL_BINARY_SIZE);
+   return !memcmp(binary, (void *) &full_hash, BINARY_SIZE);
 }

 static int cmp_exact_x(char *source, int index) {
@@ -616,7 +559,7 @@ static int cmp_exact_x(char *source, int index) {
    crypt_one_x(index, &full_hash);

    binary = (uint64_t *) get_full_binary(source);
-   return !memcmp(binary, (void *) &full_hash, FULL_BINARY_SIZE);
+   return !memcmp(binary, (void *) &full_hash, BINARY_SIZE);
 }

 /* ------- Binary Hash functions group ------- */
@@ -674,7 +617,7 @@ struct fmt_main fmt_opencl_rawsha512 = {
        RAW_BENCHMARK_LENGTH,
        0,
        PLAINTEXT_LENGTH,
-       BINARY_SIZE,
+       SHORT_BINARY_SIZE,
        BINARY_ALIGN,
        SALT_SIZE_RAW,
        SALT_ALIGN_RAW,
@@ -692,7 +635,7 @@ struct fmt_main fmt_opencl_rawsha512 = {
        fmt_default_prepare,
        valid,
        split,
-       get_binary,
+       get_short_binary,
        fmt_default_salt,
 #if FMT_MAIN_VERSION > 11
        { NULL },
@@ -738,7 +681,7 @@ struct fmt_main fmt_opencl_xsha512 = {
        X_BENCHMARK_LENGTH,
        0,
        PLAINTEXT_LENGTH,
-       BINARY_SIZE,
+       SHORT_BINARY_SIZE,
        BINARY_ALIGN,
        SALT_SIZE_X,
        SALT_ALIGN_X,
@@ -753,10 +696,10 @@ struct fmt_main fmt_opencl_xsha512 = {
        init_x,
        done,
        reset,
-       fmt_default_prepare,
-       valid_x,
-       split_x,
-       get_binary,
+       prepare_xsha512,
+       valid_xsha512,
+       split_xsha512,
+       get_short_binary,
        get_salt,
 #if FMT_MAIN_VERSION > 11
        { NULL },
diff --git a/src/rawSHA512_common.h b/src/rawSHA512_common.h
new file mode 100644
index 0000000..76e7597
--- /dev/null
+++ b/src/rawSHA512_common.h
@@ -0,0 +1,166 @@
+/*
+ * This file is part of John the Ripper password cracker,
+ * Copyright (c) 2012 magnum
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted.
+ *
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
+ */
+
+#ifndef _COMMON_RAWSHA512_H
+#define _COMMON_RAWSHA512_H
+
+/* ------ Contains (at least) prepare(), valid() and split() ------ */
+#define XSHA512_FORMAT_TAG              "$LION$"
+#define XSHA512_TAG_LENGTH              (sizeof(XSHA512_FORMAT_TAG) - 1)
+#define XSHA512_CIPHERTEXT_LENGTH  136
+
+#define FORMAT_TAG         "$SHA512$"
+#define TAG_LENGTH         (sizeof(FORMAT_TAG) - 1)
+#define CIPHERTEXT_LENGTH      128
+
+/* ------- Check if the ciphertext if a valid SHA2 hash ------- */
+#ifdef _RAWSHA512_H
+static int valid(char *ciphertext, struct fmt_main *self)
+{
+   char *p, *q;
+
+   p = ciphertext;
+   if (!strncmp(p, FORMAT_TAG, TAG_LENGTH))
+       p += 8;
+
+   q = p;
+   while (atoi16[ARCH_INDEX(*q)] != 0x7F)
+       q++;
+   return !*q && q - p == CIPHERTEXT_LENGTH;
+}
+#endif
+
+#ifdef _XSHA512_H
+static int valid_xsha512(char *ciphertext, struct fmt_main *self)
+{
+   char *pos;
+
+   /* Require lowercase hex digits (assume ASCII) */
+   pos = ciphertext;
+   if (strncmp(pos, XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH))
+       return 0;
+   pos += 6;
+   while (atoi16[ARCH_INDEX(*pos)] != 0x7F && (*pos <= '9' || *pos >= 'a'))
+       pos++;
+   return !*pos && pos - ciphertext == XSHA512_CIPHERTEXT_LENGTH+6;
+}
+#endif
+
+/* ------- Split ------- */
+#ifdef _RAWSHA512_H
+static char *split(char *ciphertext, int index, struct fmt_main *self)
+{
+   static char out[TAG_LENGTH + CIPHERTEXT_LENGTH + 1];
+
+   if (!strncmp(ciphertext, FORMAT_TAG, TAG_LENGTH))
+       ciphertext += TAG_LENGTH;
+
+   memcpy(out, FORMAT_TAG, TAG_LENGTH);
+   memcpy(out + TAG_LENGTH, ciphertext, CIPHERTEXT_LENGTH + 1);
+   strlwr(out + TAG_LENGTH);
+   return out;
+}
+#endif
+
+#ifdef _XSHA512_H
+static char *split_xsha512(char *ciphertext, int index, struct fmt_main *pFmt) {
+   static char * out;
+
+   if (!out) out = mem_alloc_tiny(8 + XSHA512_CIPHERTEXT_LENGTH + 1, MEM_ALIGN_WORD);
+
+   if (!strncmp(ciphertext, XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH))
+       return ciphertext;
+
+   memcpy(out, XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH);
+   memcpy(out + XSHA512_TAG_LENGTH, ciphertext, XSHA512_CIPHERTEXT_LENGTH + 1);
+   strlwr(out + XSHA512_TAG_LENGTH);
+   return out;
+}
+#endif
+
+/* ------- Binary ------- */
+#ifdef _RAWSHA512_H
+static void *binary(char *ciphertext)
+{
+   static unsigned char *out;
+   int i;
+
+   if (!out)
+       out = mem_alloc_tiny(BINARY_SIZE, 8);
+
+   ciphertext += TAG_LENGTH;
+   for (i = 0; i < BINARY_SIZE; i++) {
+       out[i] = atoi16[ARCH_INDEX(ciphertext[i*2])] * 16 +
+                 atoi16[ARCH_INDEX(ciphertext[i*2 + 1])];
+   }
+#ifdef SIMD_COEF_64
+   alter_endianity_to_BE64 (out, BINARY_SIZE/8);
+#endif
+   return out;
+}
+#endif
+
+#ifdef _XSHA512_H
+static void *binary_xsha512(char *ciphertext)
+{
+   static union {
+       unsigned char c[BINARY_SIZE];
+       ARCH_WORD_64 dummy;
+   } buf;
+   unsigned char *out = buf.c;
+   char *p;
+   int i;
+
+   ciphertext += 6;
+   p = ciphertext + 8;
+   for (i = 0; i < sizeof(buf.c); i++) {
+       out[i] =
+           (atoi16[ARCH_INDEX(*p)] << 4) |
+           atoi16[ARCH_INDEX(p[1])];
+       p += 2;
+   }
+#ifdef SIMD_COEF_64
+   alter_endianity_to_BE64 (out, BINARY_SIZE/8);
+#endif
+   return out;
+}
+#endif
+
+/* ------- Prepare ------- */
+#ifdef _XSHA512_H
+static char *prepare_xsha512(char *split_fields[10], struct fmt_main *self) {
+   char Buf[200];
+   if (!strncmp(split_fields[1], XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH))
+       return split_fields[1];
+   if (split_fields[0] && strlen(split_fields[0]) == XSHA512_CIPHERTEXT_LENGTH) {
+       sprintf(Buf, "%s%s", XSHA512_FORMAT_TAG, split_fields[0]);
+
+       if (valid_xsha512(Buf, self)) {
+           char *cp = mem_alloc_tiny(XSHA512_CIPHERTEXT_LENGTH+7,
+               MEM_ALIGN_NONE);
+           strcpy(cp, Buf);
+           return cp;
+       }
+   }
+   if (strlen(split_fields[1]) == XSHA512_CIPHERTEXT_LENGTH) {
+       sprintf(Buf, "%s%s", XSHA512_FORMAT_TAG, split_fields[1]);
+
+       if (valid_xsha512(Buf, self)) {
+           char *cp = mem_alloc_tiny(XSHA512_CIPHERTEXT_LENGTH+7,
+               MEM_ALIGN_NONE);
+           strcpy(cp, Buf);
+           return cp;
+       }
+   }
+   return split_fields[1];
+}
+#endif
+
+#endif
diff --git a/src/rawSHA512_fmt_plug.c b/src/rawSHA512_fmt_plug.c
index 0b5654a..b4e6606 100644
--- a/src/rawSHA512_fmt_plug.c
+++ b/src/rawSHA512_fmt_plug.c
@@ -41,9 +41,6 @@ john_register_one(&fmt_raw0_SHA512);

 #define FORMAT_LABEL       "Raw-SHA512"
 #define FORMAT_NAME        ""
-#define FORMAT_TAG              "$SHA512$"
-
-#define TAG_LENGTH             (sizeof(FORMAT_TAG) - 1)

 #ifdef SIMD_COEF_64
 #define ALGORITHM_NAME          SHA512_ALGORITHM_NAME
@@ -59,7 +56,6 @@ john_register_one(&fmt_raw0_SHA512);
 #else
 #define PLAINTEXT_LENGTH        125
 #endif
-#define CIPHERTEXT_LENGTH      128

 #define BINARY_SIZE                64
 #define BINARY_ALIGN           8
@@ -72,6 +68,9 @@ john_register_one(&fmt_raw0_SHA512);
 #else
 #define MAX_KEYS_PER_CRYPT     1
 #endif
+#define _RAWSHA512_H
+#include "rawSHA512_common.h"
+#undef _RAWSHA512_H

 static struct fmt_tests tests[] = {
    {"f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"},
@@ -148,51 +147,7 @@ static void done(void)
 #endif
 }

-static int valid(char *ciphertext, struct fmt_main *self)
-{
-   char *p, *q;
-
-   p = ciphertext;
-   if (!strncmp(p, FORMAT_TAG, TAG_LENGTH))
-       p += 8;
-
-   q = p;
-   while (atoi16[ARCH_INDEX(*q)] != 0x7F)
-       q++;
-   return !*q && q - p == CIPHERTEXT_LENGTH;
-}

-static char *split(char *ciphertext, int index, struct fmt_main *self)
-{
-   static char out[TAG_LENGTH + CIPHERTEXT_LENGTH + 1];
-
-   if (!strncmp(ciphertext, FORMAT_TAG, TAG_LENGTH))
-       ciphertext += TAG_LENGTH;
-
-   memcpy(out, FORMAT_TAG, TAG_LENGTH);
-   memcpy(out + TAG_LENGTH, ciphertext, CIPHERTEXT_LENGTH + 1);
-   strlwr(out + TAG_LENGTH);
-   return out;
-}
-
-static void *binary(char *ciphertext)
-{
-   static unsigned char *out;
-   int i;
-
-   if (!out)
-       out = mem_alloc_tiny(BINARY_SIZE, 8);
-
-   ciphertext += TAG_LENGTH;
-   for (i = 0; i < BINARY_SIZE; i++) {
-       out[i] = atoi16[ARCH_INDEX(ciphertext[i*2])] * 16 +
-                 atoi16[ARCH_INDEX(ciphertext[i*2 + 1])];
-   }
-#ifdef SIMD_COEF_64
-   alter_endianity_to_BE64 (out, BINARY_SIZE/8);
-#endif
-   return out;
-}

 #ifdef SIMD_COEF_64
 static int get_hash_0 (int index) { return crypt_out[index>>(SIMD_COEF_64>>1)][index&(SIMD_COEF_64-1)] & 0xf; }
-- 
1.9.1

<!-- gh-comment-id:84746864 --> @ghost commented on GitHub (Mar 23, 2015): Using the old way, we can discuss a path like this one for [RAW / X]-SHA512. BTW: -ng is slower than the regular version. ``` From 078d80e85ef2854e17dd9cecafe16019bde372d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claudio=20Andr=C3=A9?= <claudioandre.br@gmail.com> Date: Sun, 22 Mar 2015 22:35:55 -0300 Subject: [PATCH 2/2] OpenCL share code with CPU formats in RAW-SHA512. --- src/XSHA512_fmt_plug.c | 79 +++---------------- src/opencl_rawsha512.h | 6 +- src/opencl_rawsha512_fmt_plug.c | 113 +++++++-------------------- src/rawSHA512_common.h | 166 ++++++++++++++++++++++++++++++++++++++++ src/rawSHA512_fmt_plug.c | 51 +----------- 5 files changed, 208 insertions(+), 207 deletions(-) create mode 100644 src/rawSHA512_common.h diff --git a/src/XSHA512_fmt_plug.c b/src/XSHA512_fmt_plug.c index 5f2c200..1f9adf7 100644 --- a/src/XSHA512_fmt_plug.c +++ b/src/XSHA512_fmt_plug.c @@ -34,15 +34,11 @@ john_register_one(&fmt_XSHA512); #define FORMAT_LABEL "xsha512" #define FORMAT_NAME "Mac OS X 10.7" #define ALGORITHM_NAME "SHA512 " SHA512_ALGORITHM_NAME -#define FORMAT_TAG "$LION$" -#define TAG_LENGTH (sizeof(FORMAT_TAG) - 1) - #define BENCHMARK_COMMENT "" #define BENCHMARK_LENGTH 0 #define PLAINTEXT_LENGTH 107 -#define CIPHERTEXT_LENGTH 136 #define BINARY_SIZE 64 #define BINARY_ALIGN 8 @@ -64,6 +60,10 @@ john_register_one(&fmt_XSHA512); #undef PRECOMPUTE_CTX_FOR_SALT #endif +#define _XSHA512_H +#include "rawSHA512_common.h" +#undef _XSHA512_H + static struct fmt_tests tests[] = { {"bb0489df7b073e715f19f83fd52d08ede24243554450f7159dd65c100298a5820525b55320f48182491b72b4c4ba50d7b0e281c1d98e06591a5e9c6167f42a742f0359c7", "password"}, {"$LION$74911f723bd2f66a3255e0af4b85c639776d510b63f0b939c432ab6e082286c47586f19b4e2f3aab74229ae124ccb11e916a7a1c9b29c64bd6b0fd6cbd22e7b1f0ba1673", "hello"}, @@ -137,67 +137,6 @@ static void done(void) MEM_FREE(saved_key); } -static int valid(char *ciphertext, struct fmt_main *self) -{ - char *pos; - - /* Require lowercase hex digits (assume ASCII) */ - pos = ciphertext; - if (strncmp(pos, FORMAT_TAG, TAG_LENGTH)) - return 0; - pos += 6; - while (atoi16[ARCH_INDEX(*pos)] != 0x7F && (*pos <= '9' || *pos >= 'a')) - pos++; - return !*pos && pos - ciphertext == CIPHERTEXT_LENGTH+6; -} - -static char *prepare(char *split_fields[10], struct fmt_main *self) { - char Buf[200]; - if (!strncmp(split_fields[1], FORMAT_TAG, TAG_LENGTH)) - return split_fields[1]; - if (split_fields[0] && strlen(split_fields[0]) == CIPHERTEXT_LENGTH) { - sprintf(Buf, "%s%s", FORMAT_TAG, split_fields[0]); - if (valid(Buf, self)) { - char *cp = mem_alloc_tiny(CIPHERTEXT_LENGTH+7, MEM_ALIGN_NONE); - strcpy(cp, Buf); - return cp; - } - } - if (strlen(split_fields[1]) == CIPHERTEXT_LENGTH) { - sprintf(Buf, "%s%s", FORMAT_TAG, split_fields[1]); - if (valid(Buf, self)) { - char *cp = mem_alloc_tiny(CIPHERTEXT_LENGTH+7, MEM_ALIGN_NONE); - strcpy(cp, Buf); - return cp; - } - } - return split_fields[1]; -} - -static void *get_binary(char *ciphertext) -{ - static union { - unsigned char c[BINARY_SIZE]; - ARCH_WORD_64 dummy; - } buf; - unsigned char *out = buf.c; - char *p; - int i; - - ciphertext += 6; - p = ciphertext + 8; - for (i = 0; i < sizeof(buf.c); i++) { - out[i] = - (atoi16[ARCH_INDEX(*p)] << 4) | - atoi16[ARCH_INDEX(p[1])]; - p += 2; - } -#ifdef SIMD_COEF_64 - alter_endianity_to_BE64 (out, BINARY_SIZE/8); -#endif - return out; -} - static void *salt(char *ciphertext) { static union { @@ -208,7 +147,7 @@ static void *salt(char *ciphertext) char *p; int i; - ciphertext += TAG_LENGTH; + ciphertext += XSHA512_TAG_LENGTH; p = ciphertext; for (i = 0; i < sizeof(buf.c); i++) { out[i] = @@ -467,10 +406,10 @@ struct fmt_main fmt_XSHA512 = { init, done, fmt_default_reset, - prepare, - valid, - fmt_default_split, - get_binary, + prepare_xsha512, + valid_xsha512, + split_xsha512, + binary_xsha512, salt, #if FMT_MAIN_VERSION > 11 { NULL }, diff --git a/src/opencl_rawsha512.h b/src/opencl_rawsha512.h index 9136d6c..5693b61 100644 --- a/src/opencl_rawsha512.h +++ b/src/opencl_rawsha512.h @@ -20,12 +20,10 @@ //Constants. #define PLAINTEXT_LENGTH 55 -#define CIPHERTEXT_LENGTH_RAW 128 -#define CIPHERTEXT_LENGTH_X 136 #define BUFFER_SIZE 56 /* PLAINTEXT_LENGTH multiple of 4 */ -#define BINARY_SIZE 4 -#define FULL_BINARY_SIZE 64 +#define SHORT_BINARY_SIZE 4 +#define BINARY_SIZE 64 #define BINARY_ALIGN 4 #define SALT_SIZE_RAW 0 #define SALT_SIZE_X 4 diff --git a/src/opencl_rawsha512_fmt_plug.c b/src/opencl_rawsha512_fmt_plug.c index 2c83624..299308e 100644 --- a/src/opencl_rawsha512_fmt_plug.c +++ b/src/opencl_rawsha512_fmt_plug.c @@ -32,6 +32,7 @@ john_register_one(&fmt_opencl_xsha512); #include "sha.h" #include "sha2.h" +#include "johnswap.h" #include "common-opencl.h" #include "config.h" #include "options.h" @@ -39,7 +40,7 @@ john_register_one(&fmt_opencl_xsha512); #define FORMAT_LABEL "Raw-SHA512-opencl" #define FORMAT_NAME "" -#define RAW_FORMAT_TAG "$SHA512$" + #define X_FORMAT_LABEL "XSHA512-opencl" #define X_FORMAT_NAME "Mac OS X 10.7 salted" @@ -68,20 +69,26 @@ static size_t offset = 0, offset_idx = 0; static int crypt_all(int *pcount, struct db_salt *_salt); +#define _RAWSHA512_H +#define _XSHA512_H +#include "rawSHA512_common.h" +#undef _RAWSHA512_H +#undef _XSHA512_H + //This file contains auto-tuning routine(s). It has to be included after formats definitions. #include "opencl-autotune.h" #include "memdbg.h" static struct fmt_tests raw_tests[] = { {"f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"}, - {RAW_FORMAT_TAG "f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"}, + {FORMAT_TAG "f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"}, {"b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86", "password"}, {"2c80f4c2b3db6b677d328775be4d38c8d8cd9a4464c3b6273644fb148f855e3db51bc33b54f3f6fa1f5f52060509f0e4d350bb0c7f51947728303999c6eff446", "john-user"}, {"71ebcb1eccd7ea22bd8cebaec735a43f1f7164d003dacdeb06e0de4a6d9f64d123b00a45227db815081b1008d1a1bbad4c39bde770a2c23308ff1b09418dd7ed", "ALLCAPS"}, {"82244918c2e45fbaa00c7c7d52eb61f309a37e2f33ea1fba78e61b4140efa95731eec849de02ee16aa31c82848b51fb7b7fbae62f50df6e150a8a85e70fa740c", "TestTESTt3st"}, {"fa585d89c851dd338a70dcf535aa2a92fee7836dd6aff1226583e88e0996293f16bc009c652826e0fc5c706695a03cddce372f139eff4d13959da6f1f5d3eabe", "12345678"}, - {RAW_FORMAT_TAG "fa585d89c851dd338a70dcf535aa2a92fee7836dd6aff1226583e88e0996293f16bc009c652826e0fc5c706695a03cddce372f139eff4d13959da6f1f5d3eabe", "12345678"}, - {RAW_FORMAT_TAG "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", ""}, + {FORMAT_TAG "fa585d89c851dd338a70dcf535aa2a92fee7836dd6aff1226583e88e0996293f16bc009c652826e0fc5c706695a03cddce372f139eff4d13959da6f1f5d3eabe", "12345678"}, + {FORMAT_TAG "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", ""}, {"c96f1c1260074832bd3068ddd29e733090285dfc65939555dbbcafb27834957d15d9c509481cc7df0e2a7e21429783ba573036b78f5284f9928b5fef02a791ef", "mot\xf6rhead"}, {"aa3b7bdd98ec44af1f395bbd5f7f27a5cd9569d794d032747323bf4b1521fbe7725875a68b440abdf0559de5015baf873bb9c01cae63ecea93ad547a7397416e", "12345678901234567890"}, {"db9981645857e59805132f7699e78bbcf39f69380a41aac8e6fa158a0593f2017ffe48764687aa855dae3023fcceefd51a1551d57730423df18503e80ba381ba", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}, @@ -413,76 +420,20 @@ static void done(void) { HANDLE_CLERROR(clReleaseProgram(program[gpu_id]), "Release Program"); } -/* ------- Check if the ciphertext if a valid SHA-512 ------- */ -static int valid(char * ciphertext, struct fmt_main * self) { - char *p, *q; - - p = ciphertext; - if (!strncmp(p, "$SHA512$", 8)) - p += 8; - - q = p; - while (atoi16[ARCH_INDEX(*q)] != 0x7F) - q++; - return !*q && q - p == CIPHERTEXT_LENGTH_RAW; -} - -static char *split(char *ciphertext, int index, struct fmt_main *pFmt) { - - static char * out; - - if (!out) out = mem_alloc_tiny(8 + CIPHERTEXT_LENGTH_RAW + 1, MEM_ALIGN_WORD); - - if (!strncmp(ciphertext, "$SHA512$", 8)) - ciphertext += 8; - - memcpy(out, "$SHA512$", 8); - memcpy(out + 8, ciphertext, CIPHERTEXT_LENGTH_RAW + 1); - strlwr(out + 8); - return out; -} - -static int valid_x(char * ciphertext, struct fmt_main * self) { - char *p, *q; - - p = ciphertext; - if (!strncmp(p, "$LION$", 6)) - p += 6; - - q = p; - while (atoi16[ARCH_INDEX(*q)] != 0x7F) - q++; - return !*q && q - p == CIPHERTEXT_LENGTH_X; -} - -static char *split_x(char *ciphertext, int index, struct fmt_main *pFmt) { - static char * out; - - if (!out) out = mem_alloc_tiny(8 + CIPHERTEXT_LENGTH_X + 1, MEM_ALIGN_WORD); - - if (!strncmp(ciphertext, "$LION$", 6)) - return ciphertext; - - memcpy(out, "$LION$", 6); - memcpy(out + 6, ciphertext, CIPHERTEXT_LENGTH_X + 1); - strlwr(out + 6); - return out; -} - /* ------- To binary functions ------- */ -static void * get_binary(char *ciphertext) { +static void * get_short_binary(char *ciphertext) { static unsigned char *out; uint64_t * b; char *p; int i; - if (!out) out = mem_alloc_tiny(FULL_BINARY_SIZE, MEM_ALIGN_WORD); + if (!out) out = mem_alloc_tiny(BINARY_SIZE, MEM_ALIGN_WORD); if (salted_format) ciphertext += 6; p = ciphertext + 8; - for (i = 0; i < FULL_BINARY_SIZE; i++) { + for (i = 0; i < BINARY_SIZE; i++) { out[i] = (atoi16[ARCH_INDEX(*p)] << 4) | atoi16[ARCH_INDEX(p[1])]; @@ -495,22 +446,14 @@ static void * get_binary(char *ciphertext) { } static void * get_full_binary(char *ciphertext) { - static unsigned char *out; - char *p; - int i; - - if (!out) out = mem_alloc_tiny(FULL_BINARY_SIZE, MEM_ALIGN_WORD); + unsigned char * out; if (salted_format) - ciphertext += 6; + out = binary_xsha512(ciphertext); + else + out = binary(ciphertext); - p = ciphertext + 8; - for (i = 0; i < FULL_BINARY_SIZE; i++) { - out[i] = - (atoi16[ARCH_INDEX(*p)] << 4) | - atoi16[ARCH_INDEX(p[1])]; - p += 2; - } + alter_endianity_to_BE64 (out, BINARY_SIZE/8); return out; } @@ -601,7 +544,7 @@ static int cmp_exact(char *source, int index) { crypt_one(index, &full_hash); binary = (uint64_t *) get_full_binary(source); - return !memcmp(binary, (void *) &full_hash, FULL_BINARY_SIZE); + return !memcmp(binary, (void *) &full_hash, BINARY_SIZE); } static int cmp_exact_x(char *source, int index) { @@ -616,7 +559,7 @@ static int cmp_exact_x(char *source, int index) { crypt_one_x(index, &full_hash); binary = (uint64_t *) get_full_binary(source); - return !memcmp(binary, (void *) &full_hash, FULL_BINARY_SIZE); + return !memcmp(binary, (void *) &full_hash, BINARY_SIZE); } /* ------- Binary Hash functions group ------- */ @@ -674,7 +617,7 @@ struct fmt_main fmt_opencl_rawsha512 = { RAW_BENCHMARK_LENGTH, 0, PLAINTEXT_LENGTH, - BINARY_SIZE, + SHORT_BINARY_SIZE, BINARY_ALIGN, SALT_SIZE_RAW, SALT_ALIGN_RAW, @@ -692,7 +635,7 @@ struct fmt_main fmt_opencl_rawsha512 = { fmt_default_prepare, valid, split, - get_binary, + get_short_binary, fmt_default_salt, #if FMT_MAIN_VERSION > 11 { NULL }, @@ -738,7 +681,7 @@ struct fmt_main fmt_opencl_xsha512 = { X_BENCHMARK_LENGTH, 0, PLAINTEXT_LENGTH, - BINARY_SIZE, + SHORT_BINARY_SIZE, BINARY_ALIGN, SALT_SIZE_X, SALT_ALIGN_X, @@ -753,10 +696,10 @@ struct fmt_main fmt_opencl_xsha512 = { init_x, done, reset, - fmt_default_prepare, - valid_x, - split_x, - get_binary, + prepare_xsha512, + valid_xsha512, + split_xsha512, + get_short_binary, get_salt, #if FMT_MAIN_VERSION > 11 { NULL }, diff --git a/src/rawSHA512_common.h b/src/rawSHA512_common.h new file mode 100644 index 0000000..76e7597 --- /dev/null +++ b/src/rawSHA512_common.h @@ -0,0 +1,166 @@ +/* + * This file is part of John the Ripper password cracker, + * Copyright (c) 2012 magnum + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + */ + +#ifndef _COMMON_RAWSHA512_H +#define _COMMON_RAWSHA512_H + +/* ------ Contains (at least) prepare(), valid() and split() ------ */ +#define XSHA512_FORMAT_TAG "$LION$" +#define XSHA512_TAG_LENGTH (sizeof(XSHA512_FORMAT_TAG) - 1) +#define XSHA512_CIPHERTEXT_LENGTH 136 + +#define FORMAT_TAG "$SHA512$" +#define TAG_LENGTH (sizeof(FORMAT_TAG) - 1) +#define CIPHERTEXT_LENGTH 128 + +/* ------- Check if the ciphertext if a valid SHA2 hash ------- */ +#ifdef _RAWSHA512_H +static int valid(char *ciphertext, struct fmt_main *self) +{ + char *p, *q; + + p = ciphertext; + if (!strncmp(p, FORMAT_TAG, TAG_LENGTH)) + p += 8; + + q = p; + while (atoi16[ARCH_INDEX(*q)] != 0x7F) + q++; + return !*q && q - p == CIPHERTEXT_LENGTH; +} +#endif + +#ifdef _XSHA512_H +static int valid_xsha512(char *ciphertext, struct fmt_main *self) +{ + char *pos; + + /* Require lowercase hex digits (assume ASCII) */ + pos = ciphertext; + if (strncmp(pos, XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH)) + return 0; + pos += 6; + while (atoi16[ARCH_INDEX(*pos)] != 0x7F && (*pos <= '9' || *pos >= 'a')) + pos++; + return !*pos && pos - ciphertext == XSHA512_CIPHERTEXT_LENGTH+6; +} +#endif + +/* ------- Split ------- */ +#ifdef _RAWSHA512_H +static char *split(char *ciphertext, int index, struct fmt_main *self) +{ + static char out[TAG_LENGTH + CIPHERTEXT_LENGTH + 1]; + + if (!strncmp(ciphertext, FORMAT_TAG, TAG_LENGTH)) + ciphertext += TAG_LENGTH; + + memcpy(out, FORMAT_TAG, TAG_LENGTH); + memcpy(out + TAG_LENGTH, ciphertext, CIPHERTEXT_LENGTH + 1); + strlwr(out + TAG_LENGTH); + return out; +} +#endif + +#ifdef _XSHA512_H +static char *split_xsha512(char *ciphertext, int index, struct fmt_main *pFmt) { + static char * out; + + if (!out) out = mem_alloc_tiny(8 + XSHA512_CIPHERTEXT_LENGTH + 1, MEM_ALIGN_WORD); + + if (!strncmp(ciphertext, XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH)) + return ciphertext; + + memcpy(out, XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH); + memcpy(out + XSHA512_TAG_LENGTH, ciphertext, XSHA512_CIPHERTEXT_LENGTH + 1); + strlwr(out + XSHA512_TAG_LENGTH); + return out; +} +#endif + +/* ------- Binary ------- */ +#ifdef _RAWSHA512_H +static void *binary(char *ciphertext) +{ + static unsigned char *out; + int i; + + if (!out) + out = mem_alloc_tiny(BINARY_SIZE, 8); + + ciphertext += TAG_LENGTH; + for (i = 0; i < BINARY_SIZE; i++) { + out[i] = atoi16[ARCH_INDEX(ciphertext[i*2])] * 16 + + atoi16[ARCH_INDEX(ciphertext[i*2 + 1])]; + } +#ifdef SIMD_COEF_64 + alter_endianity_to_BE64 (out, BINARY_SIZE/8); +#endif + return out; +} +#endif + +#ifdef _XSHA512_H +static void *binary_xsha512(char *ciphertext) +{ + static union { + unsigned char c[BINARY_SIZE]; + ARCH_WORD_64 dummy; + } buf; + unsigned char *out = buf.c; + char *p; + int i; + + ciphertext += 6; + p = ciphertext + 8; + for (i = 0; i < sizeof(buf.c); i++) { + out[i] = + (atoi16[ARCH_INDEX(*p)] << 4) | + atoi16[ARCH_INDEX(p[1])]; + p += 2; + } +#ifdef SIMD_COEF_64 + alter_endianity_to_BE64 (out, BINARY_SIZE/8); +#endif + return out; +} +#endif + +/* ------- Prepare ------- */ +#ifdef _XSHA512_H +static char *prepare_xsha512(char *split_fields[10], struct fmt_main *self) { + char Buf[200]; + if (!strncmp(split_fields[1], XSHA512_FORMAT_TAG, XSHA512_TAG_LENGTH)) + return split_fields[1]; + if (split_fields[0] && strlen(split_fields[0]) == XSHA512_CIPHERTEXT_LENGTH) { + sprintf(Buf, "%s%s", XSHA512_FORMAT_TAG, split_fields[0]); + + if (valid_xsha512(Buf, self)) { + char *cp = mem_alloc_tiny(XSHA512_CIPHERTEXT_LENGTH+7, + MEM_ALIGN_NONE); + strcpy(cp, Buf); + return cp; + } + } + if (strlen(split_fields[1]) == XSHA512_CIPHERTEXT_LENGTH) { + sprintf(Buf, "%s%s", XSHA512_FORMAT_TAG, split_fields[1]); + + if (valid_xsha512(Buf, self)) { + char *cp = mem_alloc_tiny(XSHA512_CIPHERTEXT_LENGTH+7, + MEM_ALIGN_NONE); + strcpy(cp, Buf); + return cp; + } + } + return split_fields[1]; +} +#endif + +#endif diff --git a/src/rawSHA512_fmt_plug.c b/src/rawSHA512_fmt_plug.c index 0b5654a..b4e6606 100644 --- a/src/rawSHA512_fmt_plug.c +++ b/src/rawSHA512_fmt_plug.c @@ -41,9 +41,6 @@ john_register_one(&fmt_raw0_SHA512); #define FORMAT_LABEL "Raw-SHA512" #define FORMAT_NAME "" -#define FORMAT_TAG "$SHA512$" - -#define TAG_LENGTH (sizeof(FORMAT_TAG) - 1) #ifdef SIMD_COEF_64 #define ALGORITHM_NAME SHA512_ALGORITHM_NAME @@ -59,7 +56,6 @@ john_register_one(&fmt_raw0_SHA512); #else #define PLAINTEXT_LENGTH 125 #endif -#define CIPHERTEXT_LENGTH 128 #define BINARY_SIZE 64 #define BINARY_ALIGN 8 @@ -72,6 +68,9 @@ john_register_one(&fmt_raw0_SHA512); #else #define MAX_KEYS_PER_CRYPT 1 #endif +#define _RAWSHA512_H +#include "rawSHA512_common.h" +#undef _RAWSHA512_H static struct fmt_tests tests[] = { {"f342aae82952db35b8e02c30115e3deed3d80fdfdadacab336f0ba51ac54e297291fa1d6b201d69a2bd77e2535280f17a54fa1e527abc6e2eddba79ad3be11c0", "epixoip"}, @@ -148,51 +147,7 @@ static void done(void) #endif } -static int valid(char *ciphertext, struct fmt_main *self) -{ - char *p, *q; - - p = ciphertext; - if (!strncmp(p, FORMAT_TAG, TAG_LENGTH)) - p += 8; - - q = p; - while (atoi16[ARCH_INDEX(*q)] != 0x7F) - q++; - return !*q && q - p == CIPHERTEXT_LENGTH; -} -static char *split(char *ciphertext, int index, struct fmt_main *self) -{ - static char out[TAG_LENGTH + CIPHERTEXT_LENGTH + 1]; - - if (!strncmp(ciphertext, FORMAT_TAG, TAG_LENGTH)) - ciphertext += TAG_LENGTH; - - memcpy(out, FORMAT_TAG, TAG_LENGTH); - memcpy(out + TAG_LENGTH, ciphertext, CIPHERTEXT_LENGTH + 1); - strlwr(out + TAG_LENGTH); - return out; -} - -static void *binary(char *ciphertext) -{ - static unsigned char *out; - int i; - - if (!out) - out = mem_alloc_tiny(BINARY_SIZE, 8); - - ciphertext += TAG_LENGTH; - for (i = 0; i < BINARY_SIZE; i++) { - out[i] = atoi16[ARCH_INDEX(ciphertext[i*2])] * 16 + - atoi16[ARCH_INDEX(ciphertext[i*2 + 1])]; - } -#ifdef SIMD_COEF_64 - alter_endianity_to_BE64 (out, BINARY_SIZE/8); -#endif - return out; -} #ifdef SIMD_COEF_64 static int get_hash_0 (int index) { return crypt_out[index>>(SIMD_COEF_64>>1)][index&(SIMD_COEF_64-1)] & 0xf; } -- 1.9.1 ```
Author
Owner

@magnumripper commented on GitHub (Mar 23, 2015):

BTW: -ng is slower than the regular version.

The reason raw-sha256/512-ng formats are kept despite currently being slower, is they are more suitable for reversing steps and tricks like that, than the ones that use shared code from sse-intrinsics.c.

<!-- gh-comment-id:84875189 --> @magnumripper commented on GitHub (Mar 23, 2015): > BTW: -ng is slower than the regular version. The reason raw-sha256/512-ng formats are kept despite currently being slower, is they are more suitable for reversing steps and tricks like that, than the ones that use shared code from sse-intrinsics.c.
Author
Owner

@magnumripper commented on GitHub (Mar 23, 2015):

we can discuss a path like this one for [RAW / X]-SHA512

Looks fine to me. What's to discuss?

<!-- gh-comment-id:84879856 --> @magnumripper commented on GitHub (Mar 23, 2015): > we can discuss a path like this one for [RAW / X]-SHA512 Looks fine to me. What's to discuss?
Author
Owner

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

we can discuss a path like this one for [RAW / X]-SHA512

Looks fine to me. What's to discuss?

Well part of the problem with doing this, is we now have a header which MUST be placed after memdbg.h However, we have a rule memdbg.h should be the last header.

The reason it must be after memdbg.h, is because it is NOT a header, it is code, AND there is allocations within that code. Having a code style sheet, that I get thumped with often, and NOT having a 'do NOT put code into headers' rule in this coding style seems strange.

These should be done with proper change. A header file that declares the data/functions, and a c module that defines them.

<!-- gh-comment-id:188953787 --> @jfoug commented on GitHub (Feb 25, 2016): > > we can discuss a path like this one for [RAW / X]-SHA512 > > Looks fine to me. What's to discuss? Well part of the problem with doing this, is we now have a header which MUST be placed after memdbg.h However, we have a rule memdbg.h should be the last header. The reason it must be after memdbg.h, is because it is NOT a header, it is code, AND there is allocations within that code. Having a code style sheet, that I get thumped with often, and NOT having a 'do NOT put code into headers' rule in this coding style seems strange. These should be done with proper change. A header file that declares the data/functions, and a c module that defines them.
Author
Owner

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

If we call it rawSHA512_common.c instead of rawSHA512_common.h (which also hints reader that it contains code as opposed to just macros and definitions), we can simply say that memdbg.h must be the last of header (as in .h) files included.

Then again, we'd have to state that such .c files must not in turn include any .h headers - they'll need to be included by the mother file(s).

I'm not saying this is perfect, but it may be a way ahead.

<!-- gh-comment-id:189003939 --> @magnumripper commented on GitHub (Feb 25, 2016): If we call it rawSHA512_common.c instead of rawSHA512_common.h (which also hints reader that it _contains code_ as opposed to just macros and definitions), we can simply say that memdbg.h must be the last of _header_ (as in **.h**) files included. Then again, we'd have to state that such .c files must not in turn include any .h headers - they'll need to be included by the mother file(s). I'm not saying this is perfect, but it may be a way ahead.
Author
Owner

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

Oh, and for functions that are actually identical - yes of course they should be an actual C module that is not included but linked. Ie. foo_common.h with extern definitons and foo_common_plug.c with code.

<!-- gh-comment-id:189004380 --> @magnumripper commented on GitHub (Feb 25, 2016): Oh, and for functions that are actually identical - yes of course they should be an actual C module that is not included but linked. Ie. foo_common.h with extern definitons and foo_common_plug.c with code.
Author
Owner

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

That was my point exactly. The ONLY thing lost, is the ability to 'hide' the namespace with a static moniker. That is why in the past when I coded these 'global' functions, that i used something like:

RawSHA1_common_valid() as the name, vs valid()

Other than external name space, (and an extra .o module), I really see no downsides to putting the definition code into a .c file, with the declaration code into a .h file. That is what 'normal' convention is anyway.

<!-- gh-comment-id:189009048 --> @jfoug commented on GitHub (Feb 25, 2016): That was my point exactly. The ONLY thing lost, is the ability to 'hide' the namespace with a static moniker. That is why in the past when I coded these 'global' functions, that i used something like: RawSHA1_common_valid() as the name, vs valid() Other than external name space, (and an extra .o module), I really see no downsides to putting the definition code into a .c file, with the declaration code into a .h file. That is what 'normal' convention is anyway.
Author
Owner

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

And actually, this would (should) be part of the coding guidelines IMHO. It is not just here, there are other cases. I have searched a couple times for where/what some code is, then to later fine out it was in some 'header looking' object.

The one 'gotcha' we may not be able to work around, is the sph library. That one I think we really would have to use as it is, since it is not jtr native code.

<!-- gh-comment-id:189009896 --> @jfoug commented on GitHub (Feb 25, 2016): And actually, this would (should) be part of the coding guidelines IMHO. It is not just here, there are other cases. I have searched a couple times for where/what some code is, then to later fine out it was in some 'header looking' object. The one 'gotcha' we may not be able to work around, is the sph library. That one I think we really would have to use as it is, since it is not jtr native code.
Author
Owner

@solardiz commented on GitHub (May 25, 2019):

magnum, do we need to keep this issue open? The list of formats looks out of date.

<!-- gh-comment-id:495942612 --> @solardiz commented on GitHub (May 25, 2019): magnum, do we need to keep this issue open? The list of formats looks out of date.
Author
Owner

@magnumripper commented on GitHub (May 25, 2019):

Nah let's close it. It's sort of an ever-present issue.

<!-- gh-comment-id:495953013 --> @magnumripper commented on GitHub (May 25, 2019): Nah let's close it. It's sort of an ever-present issue.
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#577
No description provided.