;(C)2005 wek http://www.efton.sk ;Free for personal use. ;For commercial use contact wek@efton.sk ;Implements the original TEA algorithm by Needham&Wheeler in any '51 compatible mcu ;www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html ;uses r0,r4,r5,r6,r7,dptr ;uses r2 as round counter ;round: ; y += ((z << 4) + key[0]) ^ ((z >> 5) + key[1]) ^ (z + sum); ; z += ((y << 4) + key[2]) ^ ((y >> 5) + key[3]) ^ (y + sum); ;9350 cycles incl. ret ;206=CEh bytes incl. 16 bytes of key ;Key can be also in IRAM or XRAM, ;make the following modifications: replace all occurences of ; CODE IRAM XRAM ; mov dptr,#key mov r1,#key mov dptr,#key ; -- and -- ; clr a ; movc a,@a+dptr mov a,@r1 movx a,@dptr ; inc dptr inc r1 inc dptr ; DSEG AT 30h y0: DS 1 y1: DS 1 y2: DS 1 y3: DS 1 z0: DS 1 z1: DS 1 z2: DS 1 z3: DS 1 tmp0: DS 1 tmp1: DS 1 tmp2: DS 1 tmp3: DS 1 sum0: DS 1 sum1: DS 1 sum2: DS 1 sum3: DS 1 CSEG Tea: clr a mov sum0,a ;sum = 0 mov sum1,a mov sum2,a mov sum3,a mov r2,#32*4 ;nr of rounds *2 (because of trick with twice the main code, one for y and one for z; and another inside...) TeaRound: mov a,sum0 ;sum += delta add a,#0B9h ;delta[0] mov sum0,a mov a,sum1 addc a,#079h ;delta[1] mov sum1,a mov a,sum2 addc a,#037h ;delta[2] mov sum2,a mov a,sum3 addc a,#09Eh ;delta[3] mov sum3,a mov dptr,#key mov r4,z0 mov r5,z1 mov r6,z2 mov r7,z3 TeaSubRound: mov r0,#tmp3 ;tmp = z << 4 mov a,r7 swap a mov @r0,a ;@r0=tmp3 mov a,r6 swap a xchd a,@r0 ;@r0=tmp3 dec r0 mov @r0,a ;@r0=tmp2 mov a,r5 swap a xchd a,@r0 ;@r0=tmp2 dec r0 mov @r0,a ;@r0=tmp1 mov a,r4 swap a xchd a,@r0 ;@r0=tmp1 mov r0,a ;store highest 4 bits of z anl a,#0F0h mov tmp0,a mov a,r4 ;r7:r4 = z + sum add a,sum0 mov r4,a mov a,r5 addc a,sum1 mov r5,a mov a,r6 addc a,sum2 mov r6,a mov a,r7 addc a,sum3 mov r7,a TeaSubSubRound: clr a movc a,@a+dptr inc dptr add a,tmp0 xrl a,r4 mov r4,a clr a movc a,@a+dptr inc dptr addc a,tmp1 xrl a,r5 mov r5,a clr a movc a,@a+dptr inc dptr addc a,tmp2 xrl a,r6 mov r6,a clr a movc a,@a+dptr inc dptr addc a,tmp3 xrl a,r7 mov r7,a dec r2 mov a,r2 jnb acc.0,TeaSubSubRound2 mov a,r0 rrc a anl a,#07h xch a,tmp3 rrc a xch a,tmp2 rrc a xch a,tmp1 rrc a mov tmp0,a sjmp TeaSubSubRound TeaRoundA: sjmp TeaRound TeaSubSubRound2: jnb acc.1,TeaSubRound2 mov a,r4 add a,y0 mov y0,a mov r4,a mov a,r5 addc a,y1 mov y1,a mov r5,a mov a,r6 addc a,y2 mov y2,a mov r6,a mov a,r7 addc a,y3 mov y3,a mov r7,a sjmp TeaSubRound TeaSubRound2: mov a,r4 add a,z0 mov z0,a mov a,r5 addc a,z1 mov z1,a mov a,r6 addc a,z2 mov z2,a mov a,r7 addc a,z3 mov z3,a cjne r2,#0,TeaRoundA ret Key: db 000h,000h,000h,000h db 000h,000h,000h,000h db 000h,000h,000h,000h db 000h,000h,000h,000h end