.386
.model flat,stdcall
option casemap:none
mmov macro _d,_s,_tr
mov _tr,_s
mov _d,_tr
endm
.data
_T0 dd 0d76aa478h,0e8c7b756h,242070dbh,0c1bdceeeh,0f57c0fafh,4787c62ah,0a8304613h,0fd469501h
dd 698098d8h,08b44f7afh,0ffff5bb1h,0895cd7beh,6b901122h,0fd987193h,0a679438eh,49b40821h
_T1 dd 0f61e2562h,0c040b340h,265e5a51h,0e9b6c7aah,0d62f105dh,02441453h,0d8a1e681h,0e7d3fbc8h
dd 21e1cde6h,0c33707d6h,0f4d50d87h,455a14edh,0a9e3e905h,0fcefa3f8h,676f02d9h,08d2a4c8ah
_T2 dd 0fffa3942h,08771f681h,6d9d6122h,0fde5380ch,0a4beea44h,4bdecfa9h,0f6bb4b60h,0bebfbc70h
dd 289b7ec6h,0eaa127fah,0d4ef3085h,04881d05h,0d9d4d039h,0e6db99e5h,1fa27cf8h,0c4ac5665h
_T3 dd 0f4292244h,432aff97h,0ab9423a7h,0fc93a039h,655b59c3h,08f0ccc92h,0ffeff47dh,085845dd1h
dd 6fa87e4fh,0fe2ce6e0h,0a3014314h,4e0811a1h,0f7537e82h,0bd3af235h,2ad7d2bbh,0eb86d391h
_N0 dd 0,1
_N1 dd 1,5
_N2 dd 5,3
_N3 dd 0,7
.code
md5_initialize proc chain
mov eax,chain
mov dword ptr [eax],67452301h
mov dword ptr [eax + 4],0efcdab89h
mov dword ptr [eax + 8],98badcfeh
mov dword ptr [eax + 0ch],10325476h
ret
md5_initialize endp
md5_update proc uses ecx edx ebx esi edi chain,msg
local _a,_b,_c,_d,_i,_j,_n
local state[4]:dword
mov edx,chain
mov ebx,msg
mmov state[0],dword ptr [edx],eax
mmov state[4],dword ptr [edx + 4],eax
mmov state[8],dword ptr [edx + 8],eax
mmov state[0ch],dword ptr [edx + 0ch],eax
;F round
mov _n,offset _N0
mov edi,offset _T0
mov esi,@_F
mov edx,16110c07h
call @md5_round
;G round
mov _n,offset _N1
mov edi,offset _T1
mov edx,140e0905h
mov esi,@_G
call @md5_round
;H round
mov _n,offset _N2
mov edi,offset _T2
mov edx,17100b04h
mov esi,@_H
call @md5_round
;I round
mov _n,offset _N3
mov edi,offset _T3
mov edx,150f0a06h
mov esi,@_I
call @md5_round
mov edx,chain
mov eax,state[0]
add eax,dword ptr [edx]
mov dword ptr [edx],eax
mov eax,state[4]
add eax,dword ptr [edx + 4]
mov dword ptr [edx + 4],eax
mov eax,state[8]
add eax,dword ptr [edx + 8]
mov dword ptr [edx + 8],eax
mov eax,state[0ch]
add eax,dword ptr [edx + 0ch]
mov dword ptr [edx + 0ch],eax
xor eax,eax
ret
@md5_round:
mov ecx,_n
mov ecx,dword ptr [ecx]
mov _i,ecx
xor ecx,ecx
@@:
cmp ecx,16
jnb @f
mov _j,ecx
mov eax,ecx
and eax,3
sub eax,4
neg eax
and eax,3
push eax
push edx
mov edx,state[4 * eax]
mov _a,edx
inc eax
and eax,3
mov edx,state[4 * eax]
mov _b,edx
inc eax
and eax,3
mov edx,state[4 * eax]
mov _c,edx
inc eax
and eax,3
mov edx,state[4 * eax]
mov _d,edx
call esi
pop edx
add eax,_a
push ecx
mov ecx,dword ptr[edi + 4 * ecx]
add eax,ecx
mov ecx,_i
add eax,dword ptr [ebx + 4 * ecx]
movzx ecx,dl
rol eax,cl
pop ecx
add eax,_b
mov ecx,eax
pop eax
mov state[4 * eax],ecx
mov ecx,_n
mov ecx,dword ptr [ecx + 4]
add ecx,_i
and ecx,0fh
mov _i,ecx
ror edx,8
mov ecx,_j
inc ecx
jmp @b
@@:
retn
@_F:
mov eax,_b
mov edx,eax
not edx
and edx,_d
and eax,_c
or eax,edx
retn
@_G:
mov eax,_d
mov edx,eax
not edx
and edx,_c
and eax,_b
or eax,edx
retn
@_H:
mov eax,_b
xor eax,_c
xor eax,_d
retn
@_I:
mov eax,_d
not eax
or eax,_b
xor eax,_c
retn
md5_update endp
md5_final proc uses es ecx esi edi chain,tail,len
local _l:dword
local E[128]:byte
mmov es,ds,ax
mov ecx,len
mov ecx,dword ptr [ecx]
and ecx,3fh
mov _l,ecx
mov esi,tail
lea edi,E
cld
rep movsb
mov eax,56
cmp eax,_l
ja @f
mov eax,120
@@:
sub eax,_l
mov ecx,eax
cmp eax,0
jle @f
mov byte ptr [edi],80h
inc edi
dec ecx
xor al,al
rep stosb
@@:
mov eax,len
mov ecx,dword ptr [eax]
shl ecx,3
mov dword ptr [edi],ecx
mov ecx,dword ptr [eax + 4]
mov eax,dword ptr [eax]
shr eax,29
shl ecx,3
or eax,ecx
mov dword ptr [edi + 4],eax
lea eax,E
push eax
push chain
call md5_update
cmp _l,56
jbe @f
lea eax,E[64]
push eax
push chain
call md5_update
@@:
ret
md5_final endp
md5 proc uses ebx ecx msg,len,result
local _C[4]:dword,_L[2]:dword
lea eax,_C
push eax
call md5_initialize
mov ebx,msg
mov ecx,len
mov _L[0],ecx
mov _L[4],0
@@:
cmp ecx,64
jl @f
push ebx
lea eax,_C
push eax
call md5_update
add ebx,64
sub ecx,64
jmp @b
@@:
lea eax,_L
push eax
push ebx
lea eax,_C
push eax
call md5_final
lea ebx,_C
mov ecx,result
mmov dword ptr [ecx],dword ptr [ebx],eax
mmov dword ptr [ecx + 4],dword ptr [ebx + 4],eax
mmov dword ptr [ecx + 8],dword ptr [ebx + 8],eax
mmov dword ptr [ecx + 12],dword ptr [ebx + 12],eax
ret
md5 endp
end