AuthorMessage
Lord_Zero
Ametuar
Posts: 122

ASM implementation of the MD5 hash algorithm is as follows (MD5.h):
Code:

;   MD5 calculator, asm implementation written by Albu Cristian, 2007
;
;   This program is free software; you can redistribute it and/or modify
;   it under the terms of the GNU General Public License as published by
;   the Free Software Foundation.
;
;   This program is distributed in the hope that it will be useful,
;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;   GNU General Public License for more details.
;
;   You should have received a copy of the GNU General Public License
;   along with this program; if not, write to the Free Software
;   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
;
;   For more information send and e-mail to cristian.albu@gmail.com
MD5start   PROTO
MD5end      PROTO   :DWORD,:DWORD
MD5NextBlock   PROTO   :DWORD,:DWORD,:DWORD
assume   ebx:ptr MD5context
MD5context   struct
   lasthash   dd   4 dup(?)
   lastblocksize   dd   ?
   count      dd   ?
   lastblock   db   64*4 dup(?)
MD5context   ends
MD5FF   macro   _b,_c,_d
   mov   edi,_c
   xor   edi,_d
   and   edi,_b
   xor   edi,_d
endm
MD5GG   macro   _b,_c,_d
   mov   edi,_c
   xor   edi,_b
   and   edi,_d
   xor   edi,_c
endm
MD5HH   macro   _b,_c,_d
   mov   edi,_c
   xor   edi,_b
   xor   edi,_d
endm
MD5II   macro   _b,_c,_d
   mov   edi,_d
   not   edi
   or   edi,_b
   xor   edi,_c
endm
MD5   macro   md5func,_a,_b,_c,_d,md_ptr,md_rol,md_rnd
   md5func   _b,_c,_d
   lea   _a,[_a+edi+md_rnd]
   add   _a,dword ptr [md_ptr]
   rol   _a,md_rol
   add   _a,_b
endm
MD5start   PROC
   invoke   GlobalAlloc,GPTR,sizeof MD5context
   assume   eax:ptr MD5context
   mov   dword ptr[eax],67452301h
   mov   dword ptr[eax+4],0efcdab89h
   mov   dword ptr[eax+8],98badcfeh
   mov   dword ptr[eax+12],10325476h
   mov   [eax].lastblocksize,0
   mov   [eax].count,0
   assume   eax:nothing
   ret
MD5start   ENDP
MD5end   PROC   uses esi edi ebx MD5_context:DWORD,lpBuf:DWORD
   mov   ebx,MD5_context
   mov   eax,[ebx].lastblocksize
   lea   esi,[ebx].lastblock
   lea   edi,[eax+esi]
   mov   ecx,64
   push   eax
   mov   al,0
   rep   stosb
   pop   eax
   push   esi
   lea   esi,[esi+eax]
   lea   edx,[eax*8]
   add   edx,[ebx].count
   shr   [ebx].lastblocksize,6
   inc   [ebx].lastblocksize
   mov   byte ptr[esi],80h
   and   eax,63
   .if al>=56
      and   esi,63 xor -1
      lea   esi,[esi+64]
      inc   [ebx].lastblocksize
   .else
      sub   esi,eax
   .endif
   mov   [esi+56],edx
   pop   esi
   mov   edi,MD5_context
   .while [ebx].lastblocksize
      push   ebx
      call   MD5hash
      pop   ebx
      lea   esi,[esi+64]
      dec   [ebx].lastblocksize
   .endw
   mov   esi,MD5_context
   mov   edi,lpBuf
   mov   ecx,4
   rep   movsd
   invoke   GlobalFree,MD5_context
   ret
MD5end   ENDP
MD5NextBlock   PROC   uses esi edi ebx MD5_context:DWORD,lpbuf:DWORD,buffersize:DWORD
   mov   esi,lpbuf
   mov   edi,MD5_context
   .while buffersize>=64
      call   MD5hash
      mov   ebx,MD5_context
      add   [ebx].count,64*8
      lea   esi,[esi+64]
      sub   buffersize,64
   .endw
   .if buffersize
      mov   ebx,MD5_context
      mov   ecx,[ebx].lastblocksize
      lea   edi,[ebx+ecx].lastblock
      mov   ecx,buffersize
      add   [ebx].lastblocksize,ecx
      rep   movsb
      .while [ebx].lastblocksize>=64
         sub   [ebx].lastblocksize,64
         lea   esi,[ebx].lastblock
         push   ebx
         mov   edi,MD5_context
         call   MD5hash
         pop   ebx
         add   [ebx].count,64*8
         mov   edi,esi
         lea   esi,[esi+64]
         mov   ecx,128
         rep   movsb
      .endw
   .endif
   ret
MD5NextBlock   ENDP
MD5hash:mov   eax,[edi]
   mov   ebx,[edi+4]
   mov   ecx,[edi+8]
   mov   edx,[edi+12]
   push   edi
   MD5   MD5FF,eax,ebx,ecx,edx,esi+00*4,07,0d76aa478h
   MD5   MD5FF,edx,eax,ebx,ecx,esi+01*4,12,0e8c7b756h
   MD5   MD5FF,ecx,edx,eax,ebx,esi+02*4,17,0242070dbh
   MD5   MD5FF,ebx,ecx,edx,eax,esi+03*4,22,0c1bdceeeh
   MD5   MD5FF,eax,ebx,ecx,edx,esi+04*4,07,0f57c0fafh
   MD5   MD5FF,edx,eax,ebx,ecx,esi+05*4,12,04787c62ah
   MD5   MD5FF,ecx,edx,eax,ebx,esi+06*4,17,0a8304613h
   MD5   MD5FF,ebx,ecx,edx,eax,esi+07*4,22,0fd469501h
   MD5   MD5FF,eax,ebx,ecx,edx,esi+08*4,07,0698098d8h
   MD5   MD5FF,edx,eax,ebx,ecx,esi+09*4,12,08b44f7afh
   MD5   MD5FF,ecx,edx,eax,ebx,esi+10*4,17,0ffff5bb1h
   MD5   MD5FF,ebx,ecx,edx,eax,esi+11*4,22,0895cd7beh
   MD5   MD5FF,eax,ebx,ecx,edx,esi+12*4,07,06b901122h
   MD5   MD5FF,edx,eax,ebx,ecx,esi+13*4,12,0fd987193h
   MD5   MD5FF,ecx,edx,eax,ebx,esi+14*4,17,0a679438eh
   MD5   MD5FF,ebx,ecx,edx,eax,esi+15*4,22,049b40821h
   MD5   MD5GG,eax,ebx,ecx,edx,esi+01*4,05,0f61e2562h
   MD5   MD5GG,edx,eax,ebx,ecx,esi+06*4,09,0c040b340h
   MD5   MD5GG,ecx,edx,eax,ebx,esi+11*4,14,0265e5a51h
   MD5   MD5GG,ebx,ecx,edx,eax,esi+00*4,20,0e9b6c7aah
   MD5   MD5GG,eax,ebx,ecx,edx,esi+05*4,05,0d62f105dh
   MD5   MD5GG,edx,eax,ebx,ecx,esi+10*4,09,002441453h
   MD5   MD5GG,ecx,edx,eax,ebx,esi+15*4,14,0d8a1e681h
   MD5   MD5GG,ebx,ecx,edx,eax,esi+04*4,20,0e7d3fbc8h
   MD5   MD5GG,eax,ebx,ecx,edx,esi+09*4,05,021e1cde6h
   MD5   MD5GG,edx,eax,ebx,ecx,esi+14*4,09,0c33707d6h
   MD5   MD5GG,ecx,edx,eax,ebx,esi+03*4,14,0f4d50d87h
   MD5   MD5GG,ebx,ecx,edx,eax,esi+08*4,20,0455a14edh
   MD5   MD5GG,eax,ebx,ecx,edx,esi+13*4,05,0a9e3e905h
   MD5   MD5GG,edx,eax,ebx,ecx,esi+02*4,09,0fcefa3f8h
   MD5   MD5GG,ecx,edx,eax,ebx,esi+07*4,14,0676f02d9h
   MD5   MD5GG,ebx,ecx,edx,eax,esi+12*4,20,08d2a4c8ah
   MD5   MD5HH,eax,ebx,ecx,edx,esi+05*4,04,0fffa3942h
   MD5   MD5HH,edx,eax,ebx,ecx,esi+08*4,11,08771f681h
   MD5   MD5HH,ecx,edx,eax,ebx,esi+11*4,16,06d9d6122h
   MD5   MD5HH,ebx,ecx,edx,eax,esi+14*4,23,0fde5380ch
   MD5   MD5HH,eax,ebx,ecx,edx,esi+01*4,04,0a4beea44h
   MD5   MD5HH,edx,eax,ebx,ecx,esi+04*4,11,04bdecfa9h
   MD5   MD5HH,ecx,edx,eax,ebx,esi+07*4,16,0f6bb4b60h
   MD5   MD5HH,ebx,ecx,edx,eax,esi+10*4,23,0bebfbc70h
   MD5   MD5HH,eax,ebx,ecx,edx,esi+13*4,04,0289b7ec6h
   MD5   MD5HH,edx,eax,ebx,ecx,esi+00*4,11,0eaa127fah
   MD5   MD5HH,ecx,edx,eax,ebx,esi+03*4,16,0d4ef3085h
   MD5   MD5HH,ebx,ecx,edx,eax,esi+06*4,23,004881d05h
   MD5   MD5HH,eax,ebx,ecx,edx,esi+09*4,04,0d9d4d039h
   MD5   MD5HH,edx,eax,ebx,ecx,esi+12*4,11,0e6db99e5h
   MD5   MD5HH,ecx,edx,eax,ebx,esi+15*4,16,01fa27cf8h
   MD5   MD5HH,ebx,ecx,edx,eax,esi+02*4,23,0c4ac5665h
   MD5   MD5II,eax,ebx,ecx,edx,esi+00*4,06,0f4292244h
   MD5   MD5II,edx,eax,ebx,ecx,esi+07*4,10,0432aff97h
   MD5   MD5II,ecx,edx,eax,ebx,esi+14*4,15,0ab9423a7h
   MD5   MD5II,ebx,ecx,edx,eax,esi+05*4,21,0fc93a039h
   MD5   MD5II,eax,ebx,ecx,edx,esi+12*4,06,0655b59c3h
   MD5   MD5II,edx,eax,ebx,ecx,esi+03*4,10,08f0ccc92h
   MD5   MD5II,ecx,edx,eax,ebx,esi+10*4,15,0ffeff47dh
   MD5   MD5II,ebx,ecx,edx,eax,esi+01*4,21,085845dd1h
   MD5   MD5II,eax,ebx,ecx,edx,esi+08*4,06,06fa87e4fh
   MD5   MD5II,edx,eax,ebx,ecx,esi+15*4,10,0fe2ce6e0h
   MD5   MD5II,ecx,edx,eax,ebx,esi+06*4,15,0a3014314h
   MD5   MD5II,ebx,ecx,edx,eax,esi+13*4,21,04e0811a1h
   MD5   MD5II,eax,ebx,ecx,edx,esi+04*4,06,0f7537e82h
   MD5   MD5II,edx,eax,ebx,ecx,esi+11*4,10,0bd3af235h
   MD5   MD5II,ecx,edx,eax,ebx,esi+02*4,15,02ad7d2bbh
   MD5   MD5II,ebx,ecx,edx,eax,esi+09*4,21,0eb86d391h
   pop   edi
   add   [edi],eax
   add   [edi+4],ebx
   add   [edi+8],ecx
   add   [edi+12],edx
   ret
assume   ebx:nothing

A small program that calculated the MD5 hash of a file:
Code:
.386
.model   flat,stdcall
option   casemap:none
include   \masm32\include\windows.inc
include   \masm32\include\user32.inc
include   \masm32\include\kernel32.inc
include   \masm32\include\comdlg32.inc
includelib   \masm32\lib\user32.lib
includelib   \masm32\lib\kernel32.lib
includelib   \masm32\lib\comdlg32.lib
.data?
   ofn   OPENFILENAME   <>
   fname   db      8192 dup(?)
   hFile   dd      ?
   bRead   dd      ?
   hash   db      16 dup(?)
.code
   _MD5   db   'MD5',0
include   MD5.h
start:
   lea   edi,ofn
   mov   ecx,sizeof ofn+1
   xor   eax,eax
   rep   stosb
   invoke   GetModuleHandle,0
   mov   ofn.hInstance,eax
   mov   ofn.lStructSize,sizeof OPENFILENAME
   mov   ofn.lpstrFile,offset fname
   mov   ofn.nMaxFile,8192
   invoke   GetOpenFileName,addr ofn
   .if eax
      invoke   CreateFile,addr fname,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0
      .if eax!=INVALID_HANDLE_VALUE
         mov   hFile,eax
         call   MD5start
         mov   esi,eax
         .while 1
            invoke   ReadFile,hFile,addr fname,8192,addr bRead,0
            .break .if bRead==0
            invoke   MD5NextBlock,esi,addr fname,bRead
         .endw
         invoke   MD5end,esi,addr hash
         invoke   CloseHandle,hFile
         lea   edi,fname
         lea   esi,hash
         mov   eax,':XEH'
         stosd
         mov   al,32
         stosb
         xor   ecx,ecx
         .while ecx<16
            lodsb
            mov   ah,al
            shr   al,4
            and   ax,0f0fh
            or   ax,3030h
            .if al>'9'
               add   al,7
            .endif
            .if ah>'9'
               add   ah,7
            .endif
            stosw
            inc   ecx
         .endw
         mov   ax,0a0dh
         stosw
         mov   eax,'esaB'
         stosd
         mov   eax,' :23'
         stosd
         xor   ebx,ebx
         xor   ecx,ecx
         mov   hash[24],0
         .while ebx<16
            .if cl>3
               mov   ax,word ptr hash[ebx]
               add   cl,5
               and   cl,7
               shl   al,cl
               push   ecx
               mov   ch,8
               sub   ch,cl
               mov   cl,ch
               shr   ah,cl
               or   al,ah
               pop   ecx
               inc   ebx
            .else
               mov   ch,3
               sub   ch,cl
               push   ecx
               mov   cl,ch
               mov   al,hash[ebx]
               shr   al,cl
               pop   ecx
               add   cl,5
               and   cl,7
               .if cl==0
                  inc   ebx
               .endif
            .endif
            and   al,1fh
            .if al>25
               sub   al,24+17
            .endif
            add   al,'A'
            stosb
         .endw
         mov   al,0
         stosb
         invoke   MessageBox,0,addr fname,addr _MD5,0
      .endif
   .endif
   invoke   ExitProcess,0
   ret
end   start

The sourcecode and a compiled executable can also be found here: http://downloads.sourceforge.net/hexhub/MD5.ZIP