Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sha256 calculates wrong hashes on 16 bit target #19

Open
JoeMerten opened this issue Aug 28, 2017 · 3 comments
Open

Sha256 calculates wrong hashes on 16 bit target #19

JoeMerten opened this issue Aug 28, 2017 · 3 comments

Comments

@JoeMerten
Copy link

sha256("abc") should give:

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f200‌​15ad

but calculates wrong

2bb53935edbba17dc04a04854518754d8a66484491b585b0d0700cd2512f5420

instead.
See also https://stackoverflow.com/questions/22880627/sha256-implementation-in-c

It seems not to be an endianess issue. Any idea?

@karora
Copy link

karora commented Aug 16, 2018

We've also noticed this issue on a 16-bit platform so I was pleased to see this bug! It was low priority for us, but I finally got around to checking if there was an updated version here...

Digging a little into the code, it specifically says that you need to change the definition of "WORD" for 16-bit machines.
https://github.com/B-Con/crypto-algorithms/blob/master/sha256.h#L20

Did you make that adjustment?

I've changed the code in md5.h, sha1.h and sha256.h (so they all match) on our system to:

#include <stdint.h>
typedef uint32_t  WORD;

I can't test it until tomorrow, but I expect that's the issue - I'll report back tomorrow.

@youca04
Copy link

youca04 commented Jul 28, 2021

A long time after the fact making this comment, but I also needed an additional change in sha1.c and sha256.c as my compiler (NC308 Renesas) was not promoting the BYTEs to WORDs when performing the build of the WORDs from BYTES.

for (i = 0, j = 0; i < 16; ++i, j += 4) m[i] = ((WORD)data[j] << 24) | ((WORD)data[j + 1] << 16) | ((WORD)data[j + 2] << 8) | ((WORD)data[j + 3]);

WIthout this, the assembly code shows that the left shift operations were overflowing so was only the low byte (3).

@Weird733
Copy link

Do you slove this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants