[GH-ISSUE #2890] Possible LE dependancies in SIMD flat model. #1832

Closed
opened 2026-10-15 04:26:15 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @jfoug on GitHub (Nov 16, 2017).
Original GitHub issue: https://github.com/openwall/john/issues/2890

This was found when porting the LEET format to BE SIMD. It uses flat SIMD:

$ git diff leet_cc_fmt_plug.c
diff --git a/src/leet_cc_fmt_plug.c b/src/leet_cc_fmt_plug.c
index aba1b33ec..69cdc8d1f 100644
--- a/src/leet_cc_fmt_plug.c
+++ b/src/leet_cc_fmt_plug.c
@@ -265,11 +265,24 @@ static int crypt_all(int *pcount, struct db_salt *salt)
                        cp[saved_len[index+i]+cur_salt->saltlen] = 0x80;
                        in[i*16+15] = (saved_len[index+i]+cur_salt->saltlen)<<3;
                        memset(&cp[saved_len[index+i]+cur_salt->saltlen+1], 0, 120-(saved_len[index+i]+cur_salt->saltlen+1));
+#if !ARCH_LITTLE_ENDIAN
+                       // in non-little endian mode, we have strange behavior for BE formats using flat. ;)
+                       {
+                               uint64_t *p = (uint64_t*)cp;
+                               int j, til = ((saved_len[index+i]+cur_salt->saltlen+1)>>3)+1; // swap key, salt and 0x80
+                               for (j = 0; j < til; ++j)
+                                       p[j] = JOHNSWAP64(p[j]);
+                       }
+#endif
                }
                SIMDSHA512body(in, out, NULL, SSEi_FLAT_IN);
                for (i = 0; i < MAX_KEYS_PER_CRYPT; ++i)
+#if ARCH_LITTLE_ENDIAN==1
                        output1[i].p64[0] = JOHNSWAP64(out[((i/SIMD_COEF_64)*8*SIMD_COEF_64+i%SIMD_COEF_64)]);
 #else
+                       output1[i].p64[0] = out[((i/SIMD_COEF_64)*8*SIMD_COEF_64+i%SIMD_COEF_64)];
+#endif
+#else
                SHA512_CTX sctx;

                SHA512_Init(&sctx);

The output marshaling is not the issue. The issue is that after the flat buffer is constructed, I had to byte swap all of the data. NOTE, I did not byte swap the bit length in [15] This is because it was written as a 64 bit BE number, so was already in BE format.

The point here, is we may want to look into handling strcpy() data properly using SIMD-FLAT model if running on a BE system. I am not sure that we handle it gracefully.

this could be PEBCAK, I am not sure until I look. I just wrote this down, so I would not forget to look into it. But I would think to fix it, we just need to reverse our assumptions about swapping. When we swap for LE, we do not swap if building under BE, and vice versa. That way flat model just 'works'

Originally created by @jfoug on GitHub (Nov 16, 2017). Original GitHub issue: https://github.com/openwall/john/issues/2890 This was found when porting the LEET format to BE SIMD. It uses flat SIMD: ```diff $ git diff leet_cc_fmt_plug.c diff --git a/src/leet_cc_fmt_plug.c b/src/leet_cc_fmt_plug.c index aba1b33ec..69cdc8d1f 100644 --- a/src/leet_cc_fmt_plug.c +++ b/src/leet_cc_fmt_plug.c @@ -265,11 +265,24 @@ static int crypt_all(int *pcount, struct db_salt *salt) cp[saved_len[index+i]+cur_salt->saltlen] = 0x80; in[i*16+15] = (saved_len[index+i]+cur_salt->saltlen)<<3; memset(&cp[saved_len[index+i]+cur_salt->saltlen+1], 0, 120-(saved_len[index+i]+cur_salt->saltlen+1)); +#if !ARCH_LITTLE_ENDIAN + // in non-little endian mode, we have strange behavior for BE formats using flat. ;) + { + uint64_t *p = (uint64_t*)cp; + int j, til = ((saved_len[index+i]+cur_salt->saltlen+1)>>3)+1; // swap key, salt and 0x80 + for (j = 0; j < til; ++j) + p[j] = JOHNSWAP64(p[j]); + } +#endif } SIMDSHA512body(in, out, NULL, SSEi_FLAT_IN); for (i = 0; i < MAX_KEYS_PER_CRYPT; ++i) +#if ARCH_LITTLE_ENDIAN==1 output1[i].p64[0] = JOHNSWAP64(out[((i/SIMD_COEF_64)*8*SIMD_COEF_64+i%SIMD_COEF_64)]); #else + output1[i].p64[0] = out[((i/SIMD_COEF_64)*8*SIMD_COEF_64+i%SIMD_COEF_64)]; +#endif +#else SHA512_CTX sctx; SHA512_Init(&sctx); ``` The output marshaling is not the issue. The issue is that after the flat buffer is constructed, I had to byte swap all of the data. NOTE, I did not byte swap the bit length in [15] This is because it was written as a 64 bit BE number, so was already in BE format. The point here, is we may want to look into handling strcpy() data properly using SIMD-FLAT model if running on a BE system. I am not sure that we handle it gracefully. this could be PEBCAK, I am not sure until I look. I just wrote this down, so I would not forget to look into it. But I would think to fix it, we just need to reverse our assumptions about swapping. When we swap for LE, we do not swap if building under BE, and vice versa. That way flat model just 'works'
Author
Owner

@jfoug commented on GitHub (Nov 17, 2017):

'Some' of the port of leet was rolled back here 3d0042a82 The proper fix was added to simd_intrisics.c here: github.com/magnumripper/JohnTheRipper@3f450bd269

This issue is now closed. simd-intrisics.c handles flat input and flat output properly, endianity neutral.

<!-- gh-comment-id:345249777 --> @jfoug commented on GitHub (Nov 17, 2017): 'Some' of the port of leet was rolled back here 3d0042a82 The proper fix was added to simd_intrisics.c here: https://github.com/magnumripper/JohnTheRipper/commit/3f450bd2695f578384791a79592bd515b189c740 This issue is now closed. simd-intrisics.c handles flat input and flat output properly, endianity neutral.
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#1832
No description provided.