i've got a MD5 Hasher (roy|fleur one)

so it produce 4 dwords for the hash of a buffer

1st question:

with the hash dwords can i reteive the buffer (a string or something longer as a file)

2nd question:
in the smtp auth post from Djizeus on networking forum

it talk about a MD5 key (the smtp server send to the client hash dwords and the client use it as key to hash the password and send it to the server to auth)

how does it work where i put the key in the algo


here's his code:


;____________________________________________________________________________________________________________________________
; MD5hash : hashes a string using the md5 algorithm
;????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
; input :
; ptBuffer: pointer to the string buffer (doesn' t have to be zero-terminated, must be at least 64bytes large)
; dtBufferLength: length of the buffer
; ptMD5Result: pointer to a MD5RESULT structure
; output :
; ptMD5Result: contains the hash dwords in dtA, dtB, dtC, dtD
;____________________________________________________________________________________________________________________________
; roy|fleur
;????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

MD5hash proto :dword,:dword,:dword

MD5RESULT STRUCT
dtA dd ?
dtB dd ?
dtC dd ?
dtD dd ?
MD5RESULT ENDS

FF MACRO dta,dtb,dtc,dtd,x,s,t ; a = b + ((a + F(b,c,d) + x + t) << s )

mov eax,dtb
mov ebx,dtc
mov ecx,dtd

; F(x,y,z) = (x and y) or ((not x) and z)
and ebx,eax
not eax
and eax,ecx
or eax,ebx

add eax,dta
add eax,x
add eax,t

mov cl,s
rol eax,cl

add eax,dtb

mov dta,eax

ENDM

GG MACRO dta,dtb,dtc,dtd,x,s,t ; a = b + ((a + G(b,c,d) + x + t) << s)

mov eax,dtb
mov ebx,dtc
mov ecx,dtd

; G(x,y,z) = (x and z) or (y and (not z))
and eax,ecx
not ecx
and ecx,ebx
or eax,ecx

add eax,dta
add eax,x
add eax,t

mov cl,s
rol eax,cl

add eax,dtb

mov dta,eax

ENDM

HH MACRO dta,dtb,dtc,dtd,x,s,t ; a = b + ((a + H(b,c,d) + x + t) << s)

mov eax,dtb
mov ebx,dtc
mov ecx,dtd

; H(x,y,z) = x xor y xor z
xor eax,ebx
xor eax,ecx

add eax,dta
add eax,x
add eax,t

mov cl,s
rol eax,cl

add eax,dtb

mov dta,eax

ENDM

II MACRO dta,dtb,dtc,dtd,x,s,t ; a = b + ((a + I(b,c,d) + x + t) << s)

mov eax,dtb
mov ebx,dtc
mov ecx,dtd

; I(x,y,z) = y xor (x or (not z))
not ecx
or eax,ecx
xor eax,ebx

add eax,dta
add eax,x
add eax,t

mov cl,s
rol eax,cl

add eax,dtb

mov dta,eax

ENDM

.code

MD5hash proc uses eax ebx ecx edx edi esi,ptBuffer:dword,dtBufferLength:dword,ptMD5Result:dword
local dta:dword,dtb:dword,dtc:dword,dtd:dword

; phase I ? padding
mov edi,ptBuffer
mov eax,dtBufferLength

inc eax
add edi,eax
mov byte ptr [edi-1],080h

xor edx,edx

mov ebx,64
div ebx

neg edx
add edx,64

cmp edx,8
jae @f

add edx,64

@@: mov ecx,edx
xor al,al
rep stosb

mov eax,dtBufferLength

inc edx
add dtBufferLength,edx

xor edx,edx

mov ebx,8
mul ebx

mov dword ptr [edi-8],eax
mov dword ptr [edi-4],edx

mov edx,dtBufferLength

mov edi,ptBuffer

; phase II ? chaining variables initialization
mov esi,ptMD5Result
assume esi:ptr MD5RESULT

mov [esi].dtA,067452301h
mov [esi].dtB,0efcdab89h
mov [esi].dtC,098badcfeh
mov [esi].dtD,010325476h

; phase III ? hashing
hashloop: mov eax,[esi].dtA
mov dta,eax
mov eax,[esi].dtB
mov dtb,eax
mov eax,[esi].dtC
mov dtc,eax
mov eax,[esi].dtD
mov dtd,eax

; round 1
FF dta,dtb,dtc,dtd,dword ptr [edi+00*4],07,0d76aa478h
FF dtd,dta,dtb,dtc,dword ptr [edi+01*4],12,0e8c7b756h
FF dtc,dtd,dta,dtb,dword ptr [edi+02*4],17,0242070dbh
FF dtb,dtc,dtd,dta,dword ptr [edi+03*4],22,0c1bdceeeh
FF dta,dtb,dtc,dtd,dword ptr [edi+04*4],07,0f57c0fafh
FF dtd,dta,dtb,dtc,dword ptr [edi+05*4],12,04787c62ah
FF dtc,dtd,dta,dtb,dword ptr [edi+06*4],17,0a8304613h
FF dtb,dtc,dtd,dta,dword ptr [edi+07*4],22,0fd469501h
FF dta,dtb,dtc,dtd,dword ptr [edi+08*4],07,0698098d8h
FF dtd,dta,dtb,dtc,dword ptr [edi+09*4],12,08b44f7afh
FF dtc,dtd,dta,dtb,dword ptr [edi+10*4],17,0ffff5bb1h
FF dtb,dtc,dtd,dta,dword ptr [edi+11*4],22,0895cd7beh
FF dta,dtb,dtc,dtd,dword ptr [edi+12*4],07,06b901122h
FF dtd,dta,dtb,dtc,dword ptr [edi+13*4],12,0fd987193h
FF dtc,dtd,dta,dtb,dword ptr [edi+14*4],17,0a679438eh
FF dtb,dtc,dtd,dta,dword ptr [edi+15*4],22,049b40821h

; round 2
GG dta,dtb,dtc,dtd,dword ptr [edi+01*4],05,0f61e2562h
GG dtd,dta,dtb,dtc,dword ptr [edi+06*4],09,0c040b340h
GG dtc,dtd,dta,dtb,dword ptr [edi+11*4],14,0265e5a51h
GG dtb,dtc,dtd,dta,dword ptr [edi+00*4],20,0e9b6c7aah
GG dta,dtb,dtc,dtd,dword ptr [edi+05*4],05,0d62f105dh
GG dtd,dta,dtb,dtc,dword ptr [edi+10*4],09,002441453h
GG dtc,dtd,dta,dtb,dword ptr [edi+15*4],14,0d8a1e681h
GG dtb,dtc,dtd,dta,dword ptr [edi+04*4],20,0e7d3fbc8h
GG dta,dtb,dtc,dtd,dword ptr [edi+09*4],05,021e1cde6h
GG dtd,dta,dtb,dtc,dword ptr [edi+14*4],09,0c33707d6h
GG dtc,dtd,dta,dtb,dword ptr [edi+03*4],14,0f4d50d87h
GG dtb,dtc,dtd,dta,dword ptr [edi+08*4],20,0455a14edh
GG dta,dtb,dtc,dtd,dword ptr [edi+13*4],05,0a9e3e905h
GG dtd,dta,dtb,dtc,dword ptr [edi+02*4],09,0fcefa3f8h
GG dtc,dtd,dta,dtb,dword ptr [edi+07*4],14,0676f02d9h
GG dtb,dtc,dtd,dta,dword ptr [edi+12*4],20,08d2a4c8ah

; round 3
HH dta,dtb,dtc,dtd,dword ptr [edi+05*4],04,0fffa3942h
HH dtd,dta,dtb,dtc,dword ptr [edi+08*4],11,08771f681h
HH dtc,dtd,dta,dtb,dword ptr [edi+11*4],16,06d9d6122h
HH dtb,dtc,dtd,dta,dword ptr [edi+14*4],23,0fde5380ch
HH dta,dtb,dtc,dtd,dword ptr [edi+01*4],04,0a4beea44h
HH dtd,dta,dtb,dtc,dword ptr [edi+04*4],11,04bdecfa9h
HH dtc,dtd,dta,dtb,dword ptr [edi+07*4],16,0f6bb4b60h
HH dtb,dtc,dtd,dta,dword ptr [edi+10*4],23,0bebfbc70h
HH dta,dtb,dtc,dtd,dword ptr [edi+13*4],04,0289b7ec6h
HH dtd,dta,dtb,dtc,dword ptr [edi+00*4],11,0eaa127fah
HH dtc,dtd,dta,dtb,dword ptr [edi+03*4],16,0d4ef3085h
HH dtb,dtc,dtd,dta,dword ptr [edi+06*4],23,004881d05h
HH dta,dtb,dtc,dtd,dword ptr [edi+09*4],04,0d9d4d039h
HH dtd,dta,dtb,dtc,dword ptr [edi+12*4],11,0e6db99e5h
HH dtc,dtd,dta,dtb,dword ptr [edi+15*4],16,01fa27cf8h
HH dtb,dtc,dtd,dta,dword ptr [edi+02*4],23,0c4ac5665h

; round 4
II dta,dtb,dtc,dtd,dword ptr [edi+00*4],06,0f4292244h
II dtd,dta,dtb,dtc,dword ptr [edi+07*4],10,0432aff97h
II dtc,dtd,dta,dtb,dword ptr [edi+14*4],15,0ab9423a7h
II dtb,dtc,dtd,dta,dword ptr [edi+05*4],21,0fc93a039h
II dta,dtb,dtc,dtd,dword ptr [edi+12*4],06,0655b59c3h
II dtd,dta,dtb,dtc,dword ptr [edi+03*4],10,08f0ccc92h
II dtc,dtd,dta,dtb,dword ptr [edi+10*4],15,0ffeff47dh
II dtb,dtc,dtd,dta,dword ptr [edi+01*4],21,085845dd1h
II dta,dtb,dtc,dtd,dword ptr [edi+08*4],06,06fa87e4fh
II dtd,dta,dtb,dtc,dword ptr [edi+15*4],10,0fe2ce6e0h
II dtc,dtd,dta,dtb,dword ptr [edi+06*4],15,0a3014314h
II dtb,dtc,dtd,dta,dword ptr [edi+13*4],21,04e0811a1h
II dta,dtb,dtc,dtd,dword ptr [edi+04*4],06,0f7537e82h
II dtd,dta,dtb,dtc,dword ptr [edi+11*4],10,0bd3af235h
II dtc,dtd,dta,dtb,dword ptr [edi+02*4],15,02ad7d2bbh
II dtb,dtc,dtd,dta,dword ptr [edi+09*4],21,0eb86d391h

mov eax,dta
add [esi].dtA,eax
mov eax,dtb
add [esi].dtB,eax
mov eax,dtc
add [esi].dtC,eax
mov eax,dtd
add [esi].dtD,eax

add edi,64

sub edx,64
jnz hashloop

; phase IV ? results

mov ecx,4

@@: mov eax,dword ptr [esi]
xchg al,ah
rol eax,16
xchg al,ah
mov dword ptr [esi],eax

add esi,4

loop @b

ret

MD5hash endp


thanks to help me



Posted on 2002-11-24 13:27:04 by Thor0Asgard
Thor0Asgard,
Did you download the full zip-file from Iczelion's homepage?
If so have you checked out the full example file included inside? ?
When you open the md5asm.zip there is another zip-file inside
called md5hasher.zip. Wich contains a good example with .exe file.

However, if you have already checked out that example then I cannot
help you. Since I have never used MD5 at this moment.

EDIT:
But if you want to get the 'hash' of all the contents in a file I would
recommend doing it line by line. F.ex. Read oneline from the file, and
output a new hashed line to a new file. Until the end is reached.
I dont know if it handles big files therefore i recommend a 'line by line'
approch.
Posted on 2002-11-24 14:13:20 by natas
I recently did some playing around with MD5 hashing for a commercial product. The MD5 routines were in asm (of course), and they were being called from VB. As this particular code is no longer being used commercially, if you want i will see if i can dig it back out for you.

Why are the asm MD5 routines no longer being used commercially? Because Windows also offers MD5 hashing through the Crypto API, and the windows functions are *highly* optimised, to the point where they were beating the straight asm MD5 routines (the asm routines were optimised at the .386 level, which keeps them reasonably generic). As code using the Windows API is far easier to maintain and support, we went with that code.

So my suggestion is this: upon program load, check Windows for the presence of the MD5 functionality in the Crypto API, if it exists then use that, otherwise revert to using your asm function.

Posted on 2002-11-24 17:46:31 by sluggy
ok thanks if you can giv it and explain how it works it will fine it'll help lots of people
and i'll see on SDK (XP One) for the CryptoMD5 Api

but send me or post what you did
Posted on 2002-11-24 18:44:45 by Thor0Asgard
Well since we are on the subject. Ill post the api-calls refering to the windows
crypting routine. Goto the MSDN and search for them. And probably for some
visual basic code you could just visit http://www.planetsourcecode.com/vb/
and search for one of the api calls. Im shure you'll find it there. Good Luck!
Search keyword: Crypto Api

CRYPTIC-API-CALLS:
CryptAcquireContext, CryptCreateHash, CryptDeriveKey, CryptDestroyHash,
CryptDestroyKey, CryptEncrypt, CryptDecrypt, CryptExportKey, CryptGenKey,
CryptGetProvParam, CryptGetUserKey, CryptHashData, CryptReleaseContext
CryptSignHash, CryptVerifySignature
Posted on 2002-11-24 18:51:47 by natas
the rudeboy has such a lib. there's an example of using it with a file on this board.
Posted on 2002-11-25 06:02:33 by Hiroshimator
Just a comment about :





xchg al,ah
rol eax,16
xchg al,ah



Why not using "bswap eax" ?
Posted on 2002-11-25 06:15:17 by Axial
here's what i want to do:
1 Func Produce MD5 4 dwords hash from a buffer
1 Func with 4 dwords hash and specific key used to produce it reteive data into a buffer

is it possible and if how can i do please help me
Posted on 2002-11-25 06:57:08 by Thor0Asgard
Posted on 2002-11-25 07:02:46 by Hiroshimator
Hiro,
the MD5 asm algo i was talking about above was Rudeboy's, with some extra code around it to load in the file to be hashed. I know it probably could have been optimised for the latest cpu's, but it was quite a shock when we found that the windows functions were just as quick (our timings were based on hashing roughly 5000 different files across a network).
Posted on 2002-11-25 16:53:15 by sluggy
An example

But the CryptCreateHash function compatility requires Windows 95 OSR2 or Internet Explorer 3.02 installed...

It is a minimum requirement and most people meet it, but it won't probably work everywhere. :(
Posted on 2002-11-26 01:08:50 by JCP
requires Windows 95 OSR2 or Internet Explorer 3.02 installed... It is a minimum requirement and most people meet it, but it won't probably work everywhere
You will probably find that the only person on the planet who doesn't meet these requirements is Hutch ;)
Posted on 2002-11-26 04:58:36 by sluggy