Search code examples
debuggingassemblysha256masm32

Debugging a SHA-256 implementation


I've been working on a SHA-256 implementation using MASM32, and got some source done. However, I can't get it working correctly and have looked at it, rewrote bits of it, and copied some source into the inline Delphi ASM and got it running perfectly, yet my original ASM source has problems. Given that I'm not incredibly experienced with it, Would it be possible for someone to look at the source and tell me if they see something I'm missing? I already did a Delphi implementation and have it working perfectly, so I know it's not the algorithm itself at fault but the ASM code itself.

I was planning on optimizing tasks after I simply got it working. But bear in mind that I am still learning (self-taught), so if you see something I do that is on the stupid side in this source, I'd like to be able to learn, too. But my main concern is getting it working since I'm not seeing where the error(s) are.

(Removed the ASM code for space concerns, since I know the problem now)

Edit: I figured out what the problem was. Which leads into the next logical question since I don't know: Why did this code cause a problem?

Changing the following at the end of the SHA256Loop macro:

ADD  h, ECX  
ADD  h, EBX  ; h := t1 + t2;

To this:

ADD  ECX, EBX  ; h := t1 + t2;
MOV  h, ECX

Fixed it. Why couldn't I do two ADD instructions to the memory and get the same result as the ADD to register and then a MOV to memory?


Solution

  • Your first example with two ADD instructions depends on the previous contents of h. The second example is independent of the previous contents of h. If the value of h is not guaranteed to be zero, then those two examples will behave differently.