NervGaz

November 17th, 2002, 21:48

My versions of TEA...

The original TEA version

Code:

Encrypt PROC v

WORD, k

WORD, n

WORD

mov edi,v

mov esi,k

mov ebx,[edi]

mov ecx,[edi+4]

xor eax,eax

.WHILE n > 0

add eax,9e3779b9h

mov edx,ecx

shl edx,4

add ebx,edx

mov edx,[esi]

xor edx,ecx

add ebx,edx

mov edx,ecx

shr edx,5

xor edx,eax

add ebx,edx

add ebx,[esi+4]

mov edx,ebx

shl edx,4

add ecx,edx

mov edx,[esi+8]

xor edx,ebx

add ecx,edx

mov edx,ebx

shr edx,5

xor edx,eax

add ecx,edx

add ecx,[esi+12]

dec n

.ENDW

mov [edi],ebx

mov [edi+4],ecx

ret

Encrypt endp

Decrypt PROC v

WORD, k

WORD, n

WORD

mov edi,v

mov esi,k

mov ebx,[edi]

mov ecx,[edi+4]

imul eax,n,9e3779b9h

.WHILE n > 0

mov edx,ebx

shl edx,4

sub ecx,edx

mov edx,[esi+8]

xor edx,ebx

sub ecx,edx

mov edx,ebx

shr edx,5

xor edx,eax

sub ecx,edx

sub ecx,[esi+12]

mov edx,ecx

shl edx,4

sub ebx,edx

mov edx,[esi]

xor edx,ecx

sub ebx,edx

mov edx,ecx

shr edx,5

xor edx,eax

sub ebx,edx

sub ebx,[esi+4]

sub eax,9e3779b9h

dec n

.ENDW

mov [edi],ebx

mov [edi+4],ecx

ret

Decrypt endp

The newer one I think it was called TEAn or something like that

Code:

tean PROC uses ecx ebx esi edi data

WORD,key

WORD,rounds

WORD

LOCAL limit

WORD

mov eax,data

mov ebx,rounds

mov edi,key

mov edx,[eax]

mov ecx,[eax+4]

xor eax,eax

dec eax

.IF ebx>80000000h

neg ebx

imul eax,ebx,9E3779B9h

.IF eax!=NULL

.WHILE eax!=NULL

; First 32 bit chunk

mov ebx,edx

shl ebx,4

mov esi,edx

sar esi,5

xor ebx,esi

mov esi,eax

xor esi,edx

add ebx,esi

mov esi,eax

sar esi,11

and esi,3

add ebx,[edi+esi*4]

sub ecx,ebx

; Second 32 bit chunk

sub eax,9E3779B9h

mov ebx,ecx

shl ebx,4

mov esi,ecx

sar esi,5

xor ebx,esi

mov esi,eax

xor esi,ecx

add ebx,esi

mov esi,eax

and esi,3

add ebx,[edi+esi*4]

sub edx,ebx

.ENDW

.ENDIF

.ELSE

inc eax

imul ebx,9E3779B9h

mov limit,ebx

.IF eax!=limit

.WHiLE eax!=limit

; First 32 bit chunk

mov ebx,ecx

shl ebx,4

mov esi,ecx

sar esi,5

xor ebx,esi

mov esi,eax

xor esi,ecx

add ebx,esi

mov esi,eax

and esi,3

add ebx,[edi+esi*4]

add edx,ebx

; Second 32 bit chunk

add eax,9E3779B9h

mov ebx,edx

shl ebx,4

mov esi,edx

sar esi,5

xor ebx,esi

mov esi,eax

xor esi,edx

add ebx,esi

mov esi,eax

sar esi,11

and esi,3

add ebx,[edi+esi*4]

add ecx,ebx

.ENDW

.ENDIF

.ENDIF

mov eax,data

mov [eax],edx

mov [eax+4],ecx

ret

tean ENDP

both of them could be optimized both for speed and size if I felt like it but the easiest oine would be to remove the possibilty to chose the amount of rounds and calculating a set limit value, but that probably won't happen