;(C)2005 wek http://www.efton.sk ;Free for personal use. ;For commercial use contact wek@efton.sk ;Implements the *inverse* of 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: ; z -= ((y << 4) + key[2]) ^ ((y >> 5) + key[3]) ^ (y + sum); ; y -= ((z << 4) + key[0]) ^ ((z >> 5) + key[1]) ^ (z + sum); ;d5h=213 bytes including 16 bytes of key ;9436 cycles including ret ;Please note the DIFFERENT ARRANGEMENT OF THE 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: mov sum3,#0C6h ;sum = 0 mov sum2,#0EFh mov sum1,#037h mov sum0,#020h 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 dptr,#Key mov r4,y0 mov r5,y1 mov r6,y2 mov r7,y3 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: clr c mov a,sum0 ;sum -= delta subb a,#0B9h ;delta[0] mov sum0,a mov a,sum1 subb a,#079h ;delta[1] mov sum1,a mov a,sum2 subb a,#037h ;delta[2] mov sum2,a mov a,sum3 subb a,#09Eh ;delta[3] mov sum3,a sjmp TeaRound TeaSubSubRound2: jnb acc.1,TeaSubRound2 clr c mov a,z0 subb a,r4 mov z0,a mov r4,a mov a,z1 subb a,r5 mov z1,a mov r5,a mov a,z2 subb a,r6 mov z2,a mov r6,a mov a,z3 subb a,r7 mov z3,a mov r7,a jmp TeaSubRound TeaSubRound2: clr c mov a,y0 subb a,r4 mov y0,a mov a,y1 subb a,r5 mov y1,a mov a,y2 subb a,r6 mov y2,a mov a,y3 subb a,r7 mov y3,a cjne r2,#0,TeaRoundA ret Key: ;!!!! different key order !!!!! Key2: db 000h,000h,000h,000h Key3: db 000h,000h,000h,000h Key0: db 000h,000h,000h,000h Key1: db 000h,000h,000h,000h end