News:

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

btea (cause I just can't stop playing with TEA variants)

Started by NervGaz, November 27, 2009, 07:32:34 PM

Previous topic - Next topic

NervGaz

Just to give you guys something to play with, and as the topic states I just can't stop playing with TEA
variants. I've ported btea (Block TEA) from c to asm, and am kinda getting sick off it which only means
time to let others play. So fill your boots and rip it to shreds, who knows I might actually understand what
the hell you guys do in here as it's code I have a grasp of. :)

Original C

typedef unsigned __int32 uint32_t;
#define DELTA 0x9e3779b9
#define MX ((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z));

void btea(uint32_t *v, int n, uint32_t const k[4]) {
    uint32_t y, z, sum;
    unsigned p, rounds, e;
    if (n > 1) {          /* Coding Part */
      rounds = 6 + 52/n;
      sum = 0;
      z = v[n-1];
      do {
        sum += DELTA;
        e = (sum >> 2) & 3;
        for (p=0; p<n-1; p++)
          y = v[p+1], z = v[p] += MX;
        y = v[0];
        z = v[n-1] += MX;
      } while (--rounds);
    } else if (n < -1) {  /* Decoding Part */
      n = -n;
      rounds = 6 + 52/n;
      sum = rounds*DELTA;
      y = v[0];
      do {
        e = (sum >> 2) & 3;
        for (p=n-1; p>0; p--)
          z = v[p-1], y = v[p] -= MX;
        z = v[n-1];
        y = v[0] -= MX;
      } while ((sum -= DELTA) != 0);
    }
  }


ASM

MX MACRO
          ; ((z>>5^y<<2)
          mov ecx, [z]
          shr ecx, 5
          mov edx, [y]
          shl edx, 2
          xor ecx, edx
          ; (y>>3^z<<4))
          mov eax, [y]
          shr eax, 3
          mov edx, [z]
          shl edx, 4
          xor eax, edx
          ; ((z>>5^y<<2) _+_ (y>>3^z<<4))
          add ecx, eax
          ; ((sum^y)
          mov eax, [sum]
          xor eax, [y]
          ; (k[(p&3)^e] ^ z))
          mov edx, [p]
          and edx, 3
          xor edx, [e]
          mov esi, [k]
          mov edx, [esi+edx*4]
          xor edx, [z]
          ; ((sum^y) _+_ (k[(p&3)^e] ^ z))
          add eax, edx
          ; ((z>>5^y<<2) + (y>>3^z<<4)) _^_ ((sum^y) + (k[(p&3)^e] ^ z));
          xor ecx, eax
ENDM

btea PROC v:DWORD, n:DWORD, k:DWORD

LOCAL e:DWORD
LOCAL z:DWORD
LOCAL sum:DWORD
LOCAL rounds:DWORD
LOCAL y:DWORD
LOCAL p:DWORD



          ; if (n > 1) {      /* Coding Part */
          cmp [n], 1
          jle DecodePart
          ; rounds = 6 + 52/n;
          mov eax, 52
          cdq
          idiv  [n]
          add eax, 6
          mov [rounds], eax
          ; sum = 0;
          mov [sum], 0
          ; z = v[n-1];
          mov eax, [n]
          mov ecx, [v]
          mov edx, [ecx+eax*4-4]
          mov [z], edx
; do {
CodeLoop:
          ; sum += DELTA;
          mov eax, [sum]
          sub eax, 61C88647h
          mov [sum], eax
          ; e = (sum >> 2) & 3;
          mov ecx, [sum]
          shr ecx, 2
          and ecx, 3
          mov [e], ecx
          ; for (p=0; p<n-1; p++)
          mov [p], 0
          jmp  CC
CodeChunk1:
          mov edx, [p]
          add edx, 1
          mov [p], edx
CC:
          mov eax, [n]
          sub eax, 1
          cmp [p], eax
          jnb  CodeChunk2
          ; y = v[p+1]
          mov ecx, [p]
          mov edx, [v]
          mov eax, [edx+ecx*4+4]
          mov [y], eax
          MX
          ; z = v[p] +=
          mov eax, [p]
          mov edx, [v]
          add ecx, [edx+eax*4]
          mov eax, [p]
          mov edx, [v]
          mov [edx+eax*4], ecx
          mov eax, [p]
          mov ecx, [v]
          mov edx, [ecx+eax*4]
          mov [z], edx
          ; End of for loop
          jmp  CodeChunk1

CodeChunk2:
          ; y = v[0];
          mov eax, [v]
          mov ecx, [eax]
          mov [y], ecx
          MX
          ; z = v[n-1] +=
          mov eax, [n];
          mov edx, [v];
          add ecx, [edx+eax*4-4];
          mov eax, [n];
          mov edx, [v];
          mov [edx+eax*4-4], ecx
          mov eax, [n]
          mov ecx, [v]
          mov edx, [ecx+eax*4-4]
          mov [z], edx
          ; } while (--rounds);
          mov ecx, [rounds]
          sub ecx, 1
          mov [rounds], ecx
          jnz CodeLoop
          ; Finished
          jmp Done

DecodePart:
          ; } else if (n < -1) {
          cmp [n], -1
          jge Done
          ; n = -n;
          mov edx, [n]
          neg edx
          mov [n], edx
          ; rounds = 6 + 52/n;
          mov eax, 52
          cdq
          idiv  [n]
          add eax, 6
          mov [rounds], eax
          ; sum = rounds*DELTA;
          mov eax, [rounds]
          imul  eax, 9E3779B9h
          mov [sum], eax
          ; y = v[0];
          mov ecx, [v]
          mov edx, [ecx]
          mov [y], edx
;do {
DecodeLoop:
          ; e = (sum >> 2) & 3;
          mov eax, [sum]
          shr eax, 2
          and eax, 3
          mov [e], eax
          ; for (p=n-1; p>0; p--)
          mov ecx, [n]
          sub ecx, 1
          mov [p], ecx
          jmp  _DD
DecodeChunk1:
          mov edx, [p]
          sub edx, 1
          mov [p], edx
_DD:
          cmp [p], 0
          jbe  DecodeChunk2
          ; z = v[p-1];
          mov eax, [p]
          mov ecx, [v]
          mov edx, [ecx+eax*4-4]
          mov [z], edx
          MX
          ; y = v[p] -=
          mov edx, [p]
          mov eax, [v];
          mov edx, [eax+edx*4];
          sub edx, ecx;
          mov ecx, [p];
          mov eax, [v];
          mov [eax+ecx*4], edx;
          mov edx, [p]
          mov ecx, [v]
          mov eax, [ecx+edx*4]
          mov [y], eax
          ; End of for loop
          jmp  DecodeChunk1

DecodeChunk2:
          ; z = v[n-1];
          mov edx, [n]
          mov eax, [v]
          mov ecx, [eax+edx*4-4]
          mov [z], ecx
          MX
          ; y = v[0] -=
          mov edx, [v]
          mov eax, [edx]
          sub eax, ecx
          mov edx, [v]
          mov [edx], eax
          mov ecx, [v]
          mov eax, [ecx]
          mov [y], eax
          ; } while ((sum -= DELTA) != 0);
          mov ecx, [sum]
          add ecx, 61C88647h
          mov [sum], ecx
          jnz DecodeLoop
Done:
          ret
btea   endp


Call it as such:
Encryption
invoke  btea, offset data, size of data, offset key
Decryption
invoke  btea, offset data, -size of data, offset key

Enjoy


hutch--

NervGaz,

I just had a quick look through the algo and the code looks fine, there is not much you can do about the shifts if you need them but generally the code looks good. I can see a use for an algo of this type, especially in conjunction with a unique pad XOR encryption technique.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php