Tiny Encryption Algorithm ASM Project

Lately I've been working on my own implementation of the Tiny Encryption Algorithm. What is the Tiny Encryption Algorithm? WikiPedia answers, "The Tiny Encryption Algorithm (TEA) is a block cipher notable for its simplicity of description and implementation (typically a few lines of code)." Well, I've written my own implentation in assembly, enjoy.

In WikiPedia's example, the encrypt() routine looks like the following:

void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0=v[0], v1=v[1], sum=0, i;           /* set up */
    unsigned long delta=0x9e3779b9;                     /* a key schedule constant */
    unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
    for (i=0; i < 32; i++) {                            /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);  /* end cycle */
    }
    v[0]=v0; v[1]=v1;
}

Only ~10 lines, not bad. My implementation of the above in Intel Architecture 32-bit (IA32) Assembly came out as follows:

; tea.asm
; By Charles Hooper <hoopercharles@gmail.com>
;
; Tiny Encryption Algorithm implementation
;
 
section .data
	delta	dd	0x9e3779b9	; Magic constant
	sum	dd	0
	counter dd	0
 
section .bss
	v0:	resb	4	; 64-bit (2x32) block to encrypt
	v1:	resb	4
 
	k0:	resb	4	; 128-bit (4x32) key
	k1:	resb	4
	k2:	resb	4
	k3:	resb	4
 
section .text
	global main	; gcc entry point
 
main:
        push    ebp		; set up stack frame -- What?
	mov	ebp,esp		; again, what?
 
	;; Set up variables -- For simplicity, you could set your key and data you want to encrypt here.
	mov	dword [v0],0
	mov	dword [v1],0
	mov	dword [k0],0
	mov	dword [k1],0
	mov	dword [k2],0
	mov	dword [k3],0
 
tea_encrypt_loop:
	cmp	dword [counter],20h	; loop while counter < 32 (20h)
	je	near exit_tea_encrypt_loop
 
	mov	eax,[sum]	; sum += delta
	add	eax,[delta]
	mov	[sum],eax
 
	;; v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1)
	xor	eax,eax
	xor	ebx,ebx
 
	mov	eax,[v1]
	shl	eax,4
	add	eax,[k0]
 
	mov	ebx,[v1]
	add	ebx,[sum]
 
	xor	eax,ebx
 
	mov	ebx,[v1]
	shr	ebx,5
	add	ebx,[k1]
 
	xor	eax,ebx
 
	add	[v0],eax
	;; End
 
	;; v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3)
	xor	eax,eax
	xor	ebx,ebx
 
	mov	eax,[v0]
	shl	eax,4
	add	eax,[k2]
 
	mov	ebx,[v0]
	add	ebx,[sum]
 
	xor	eax,ebx
 
	mov	ebx,[v0]
	shr	ebx,5
	add	ebx,[k3]
 
	xor	eax,ebx
 
	add	[v1],eax
	;; End
 
	inc	dword [counter]
 
jmp	tea_encrypt_loop
 
exit_tea_encrypt_loop:
exit:
	mov esp,ebp
	pop ebp
 
	xor eax,eax		; exit(0)
	ret

It's a bit longer @ 92 lines including blank lines, but it works just as well! If you notice anything wrong, please leave a comment and let me know.

what is the advantages and disadvantages of tiny encryption algorithm.
and how about encryption and descryption table of it

NITHA,
That information is available on the Wikipedia page for the Tiny Encryption Algorithm :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <blockcode> <ul> <ol> <li> <dl> <dt> <dd> <br /> <p> <pre> <blockquote> <s>
  • Lines and paragraphs break automatically.

More information about formatting options