News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

TEA (Tiny Encryption Algorithm) ASM Implementation

Started by travism, October 31, 2008, 10:37:51 AM

Previous topic - Next topic

travism

So I tried taking on the daunting task of implementing the tiny encryption algo in assembly... bad Idea as I started actually writing it i realized It wasn't a good idea, so Im posting what I got here to see if anyone can actually make it work or let me know what I screwed up on (which Im sure was alot)... but hey atleast Ill learn :) Anyways its long here it goes...dont laugh :P

Edit: In the comments I posted what its _suppose_ to do...


.data
szEncStr    db "Test String",0
szkey       db " ",0
.data?
; ------------------------------------------------------
; Set up
v           dword ?
v0          dword ?
v1          dword ?
; ------------------------------------------------------
; cache key
k           dword ?
k0          dword ?
k1          dword ?
k2          dword ?
k3          dword ?

sum         dword ?

.const
; ------------------------------------------------------
; key schedule constant
delta       equ 3779b9h

; ------------------------------------------------------
; set up for decryption
sumd        equ c6ef3720h

; ------------------------------------------------------
.code
start:
      ; ------------------------------------------------
      ; v0=v[0],v1=v[1], sum=0
      mov   v0,offset szEncStr
      mov   esi,dword ptr [v0+1]
      mov   v1,esi
      mov   sum,0
     
      mov   k0,offset szkey
      mov   esi,dword ptr[k0+1]
      mov   k1,esi
      mov   esi,dword ptr[k1+1]
      mov   k2,esi
      mov   esi,dword ptr[k2+2]
      mov   k2,esi
      mov   esi,dword ptr[k3+3]
      mov   k3,esi
     
      xor   ecx,ecx
      mov   edx,1
el:
      cmp   ecx,32
      jle   ee
     
      ; ------------------------------------------------
      ; sum += delta
      mov   edx,delta
      add   edx,1
      mov   sum,delta
     
      ; ------------------------------------------------
      ; ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1)
      shl   v1,4
      mov   eax,v1
      add   eax,k0
      mov   v1,eax
     
      mov   eax,v1
      add   eax,sum
      mov   ebx,v1
      imul  ebx,eax
      mov   v1,ebx
     
      shr   v1,5
      mov   ebx,v1
      add   ebx,k1
      mov   v1,ebx
     
      imul  eax,v1
      mov   v0,eax
      inc   v0
     
      ; ------------------------------------------------
      ; ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3)
      shl   v0,4
      mov   eax,v0
      add   eax,k2
      mov   v0,eax
     
      mov   ebx,v0
      add   ebx,sum
      imul  eax,ebx
      mov   v0,eax
     
      shr   v0,5
      mov   ebx,v0
      add   ebx,k3
      imul  eax,ebx
      mov   v1,eax
     
      ; ------------------------------------------------
      ; v0+=,v1+=
      inc   v0
      inc   v1
      inc   ecx
      jmp   el
ee:
     ; ------------------------------------------------
     ; v[0]=v0; v[1]=v1;
      mov   eax,v
      mov   ebx,v0
      mov   dword ptr[eax],ebx
      mov   ebx,v1
      mov   dword ptr[eax+1],ebx
end start

raymond

Take 2 minutes of your time to go through your code, noting after each instruction the value of each register which is changing (if any) and/or which is the next instruction to be performed based on current flag conditions when you have a conditional jump.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

cmpxchg

#2
http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm

didn't test the following code but it sure looks better. I also didn't get the point of multiplication in your code.


  xor  ebp, ebp          ;sum
  mov  edi, [buffer]     ;v0
  mov  esi, [buffer+4]   ;v1

  mov  rounds, 32

.single_round:
  add  ebp, 9e3779b9h    ;sum += 9e3779b9h

  ; ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1)

  mov  eax, esi
  mov  ecx, esi
  mov  ebx, esi          ;eax = ebx = ecx = v1 = esi
  ;--------------
  shl  eax, 4            ;v1 << 4
  shr  ecx, 5            ;v1 >> 5
  add  ebx, ebp          ;v1 + sum
  ;--------------
  add  eax, [k0]         ;(v1 << 4) + k0
  add  ecx, [k1]         ;(v1 >> 5) + k1
  ;--------------
  xor  ebx, eax
  xor  ebx, ecx
  add  edi, ebx          ;v0 += ... T E M P v1 ...

  ; ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3)

  mov  eax, edi
  mov  ecx, edi          ;eax = ecx = ebx = v0= edi
  mov  ebx, edi
  ;--------------
  shl  eax, 4
  shr  ecx, 5
  add  ebx, ebp
  ;--------------
  add  eax, [k2]         ;(v0 << 4) + k2
  add  ecx, [k3]         ;(v0 >> 5) + k3
  ;--------------
  xor  ebx, eax
  xor  ebx, ecx
  add  esi, ebx          ;v1 += ... T E M P v0 ...

  sub  rounds, 1
  jnz  .single_round

  mov  [buffer], edi
  mov  [buffer+4], esi


EDIT: this thread has xtea implementation in attachments

travism

very nice cmpxchg, I probably should just stick to small apps :) but I definentally see where i went wrong, thanks alot!