Hi,

I found this XTEA (m)asm implementation from http://www.simonshepherd.supanet.com/tea.htm and I have added some code  to make it run (basicly the main proc). Now i'm wondering if somebody could confirm me that the algo is really providing  correct results?

Best regards,
Sami

.486
.MODEL FLAT, STDCALL
option casemap:none

include d:\masm32\include\windows.inc
include d:\masm32\include\kernel32.inc
include d:\masm32\include\advapi32.inc
include d:\masm32\include\user32.inc
include d:\masm32\include\masm32.inc
includelib d:\masm32\lib\kernel32.lib
includelib d:\masm32\lib\advapi32.lib
includelib d:\masm32\lib\user32.lib
includelib d:\masm32\lib\masm32.lib


Main PROTO
XTEA_Encipher PROTO
XTEA_Decipher PROTO

CTEXT MACRO text:VARARG
    LOCAL TxtName
    .data
    TxtName BYTE text,0
    .code
  EXITM <ADDR TxtName>
ENDM

.const
Delta equ 09E3779B9h ; Delta
DIS equ 0C6EF3720h ; Initial value of sum in decipher routine
N equ 32 ; n (number of iterations)

.data

v dd 0, 0
w dd ?, ? ; Results
k dd 0,0,0,0 ; Key
Password db "1234567890123456"

PlainText db "aaaaaaaa", 0

.data?
.code
Start:
invoke Main
invoke ExitProcess, eax

Main proc
LOCAL szTmp[1024] :BYTE
LOCAL szHex1[12] :BYTE
LOCAL szHex2[12] :BYTE

invoke StdOut, CTEXT("XTEA Test", 13, 10)

; invoke MemCopy, ADDR Password, ADDR k, 16

invoke MemCopy, ADDR PlainText, ADDR v, 8

invoke XTEA_Encipher
invoke dw2hex, DWORD PTR , ADDR szHex1
invoke dw2hex, DWORD PTR + 4, ADDR szHex2
invoke wsprintf, ADDR szTmp, CTEXT("%s %s"), ADDR szHex1, ADDR szHex2
invoke StdOut, ADDR szTmp
xor eax, eax

Exitus:
ret
Main endp

XTEA_Encipher proc

PUSHAD ; Save registers

MOV EAX, v[0 * 4] ; EAX -> y
MOV EBX, v[1 * 4] ; EBX -> z
MOV ECX, N ; ECX -> n
XOR EDX, EDX ; EDX -> sum

EnStart:

MOV ESI, EBX
MOV EDI, ESI

SHL ESI, 4 ; z << 4
SHR EDI, 5 ; z >> 5
XOR EDI, ESI ; z << 4 ^ z >> 5
ADD EDI, EBX ; (z << 4 ^ z >> 5) + z

MOV ESI, EDX
AND ESI, 3 ; sum & 3
MOV ESI, k ; k
ADD ESI, EDX ; sum + k
XOR ESI, EDI ; (z << 4 ^ z >> 5) + z ^ sum + k
ADD EAX, ESI ; y += (z << 4 ^ z >> 5) + z ^ sum + k

ADD EDX, Delta ; sum += delta

MOV ESI, EAX
MOV EDI, ESI

SHL ESI, 4 ; y << 4
SHR EDI, 5 ; y >> 5
XOR EDI, ESI ; y << 4 ^ y >> 5
ADD EDI, EAX ; (y << 4 ^ y >> 5) + y

MOV ESI, EDX
SHR ESI, 11 ; sum >> 11
AND ESI, 3 ; sum >> 11 & 3
MOV ESI, k ; k
ADD ESI, EDX ; sum + k
XOR ESI, EDI ; (y << 4 ^ y >> 5) + y ^ sum + k;
ADD EBX, ESI ; z += (y << 4 ^ y >> 5) + y ^ sum + k;

LOOP EnStart

MOV w[0 * 4], EAX
MOV w[1 * 4], EBX

POPAD ; Restore registers
RET

XTEA_Encipher endp

XTEA_Decipher proc

PUSHAD ; Save registers

MOV EAX, v[0 * 4] ; EAX -> y
MOV EBX, v[1 * 4] ; EBX -> z
MOV ECX, N ; ECX -> n
MOV EDX, DIS ; EDX -> sum

DeStart:

MOV ESI, EAX
MOV EDI, ESI

SHL ESI, 4 ; y << 4
SHR EDI, 5 ; y >> 5
XOR EDI, ESI ; y << 4 ^ y >> 5
ADD EDI, EAX ; (y << 4 ^ y >> 5) + y

MOV ESI, EDX
SHR ESI, 11 ; sum >> 11
AND ESI, 3 ; sum >> 11 & 3
MOV ESI, k ; k
ADD ESI, EDX ; sum + k
XOR ESI, EDI ; (y << 4 ^ y >> 5) + y ^ sum + k
SUB EBX, ESI ; z -= (y << 4 ^ y >> 5) + y ^ sum + k

SUB EDX, Delta ; sum -= delta

MOV ESI, EBX
MOV EDI, ESI

SHL ESI, 4 ; z << 4
SHR EDI, 5 ; z >> 5
XOR EDI, ESI ; z << 4 ^ z >> 5
ADD EDI, EBX ; (z << 4 ^ z >> 5) + z

MOV ESI, EDX
AND ESI, 3 ; sum & 3
MOV ESI, k ; k
ADD ESI, EDX ; sum + k
XOR ESI, EDI ; (z << 4 ^ z >> 5) + z ^ sum + k
SUB EAX, ESI ; y -= (z << 4 ^ z >> 5) + z ^ sum + k

LOOP DeStart

MOV w[0 * 4], EAX
MOV w[1 * 4], EBX

POPAD ; Restore registers
RET

XTEA_Decipher endp

end Start
Posted on 2006-05-18 14:06:57 by SamiP
Assuming that this page has correct test vectors, you could test against those :)
Posted on 2006-05-19 05:00:56 by f0dder
Thank you f0dder, you are a life saver!

I spent countles of hours googling around without founding how can I verify that this algo gives correct results and now it's proven.
Posted on 2006-05-20 15:23:43 by SamiP