Version 7 of TEA Streaming encryption extension

Updated 2004-05-23 00:50:19 by SRIV

What is it?

TEA stands for the Tiny Encryption Algorithm.

http://www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html

It is a simple/fast cypher that is supposedly quite secure.

Without going into a long dragged out crytptography primer, Im going to leave it to you to research unfamiliar topics. This is in my words, not in that of a professional cryptoanalyst. Feel free to chime in to correct me or elaborate.

Two common modes of cyphers are block cyphers and stream cyphers. I firsted implemented TEA in block cypher mode, first in pure tcl, then in c using critcl. Both worked well, the c version was naturally 100 times faster. The problem with a block cypher is that since you work with blocks of data (ie: 8 bytes), the data needs to be padded to a size divisible by eight, then the padding has to be removed during decryption. Both issues have many common solutions that work well, as did the one I chose.

When sending data over a channel like a socket, the padding becomes a nuisance to handle. A stream cypher works byte by byte, xoring the data byte with a crypted byte based off the key in some fashion. More specifically, CTR or counter mode, is a cypher method whereby you generate the crypted byte by encrypting a 128 bit counter value with your 128 bit key, incrementing the counter after all 8 bytes have been used to encrypt 8 bytes of data. The benifit of this is that you dont have to pad the data, you just discard the unused crypted counter bytes if any are left over. Anothe benefit of CTR mode is that to decrypt the data, you run in back through the encryption algorithm. Nifty, we save %50 on code :) .


Who cares, and what does it do?

In the c extension below, I create a tcl command named scrypt. The scrypt command takes data, a key and an optional counter value as its arguments. If not specified, the counter is set to zero.

 # scrypt.tcl  Steve Redler IV, steve A T sr-tech D O T com
 package provide scrypt 0.2 
 package require critcl
 #command to build lib:  tclkit critcl.kit -pkg scrypt.tcl

 critcl::ccode {  

  char encryptc (unsigned long *v, unsigned long *k) 
  {     
    /* v = datablock, k = key */

    unsigned long y=v[0],z=v[1],sum=0,       /* set up */
            delta=0x9e3779b9, n=32 ;         /* key schedule constant*/
   /*  printf("v0=%d v1=%d k0=%d k1=%d k2=%d k3=%d \n",v[0], v[1], k[0], k[1], k[2], k[3]);*/ 
    while (n-->0)
    {                                              /* basic cycle start*/
      y += (z<<4 ^ z>>5) + z ^ sum + k[sum&3];
      sum += delta ;    
      z += (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3] ;     /* end cycle */
    }
    v[0]=y ;
    v[1]=z ;  
  }  
 }



 critcl::ccommand scrypt {dummy ip objc objv} {
  int x, klen, datalen, ctrlen, bytenum;
  unsigned char *key;
  unsigned char k[16] = "0000000000000000";
  unsigned char *datain, *dataout;

  unsigned long long counter = 0;
  unsigned long long *ctraddr;

  unsigned char xorpad[8] = "00000000";
  unsigned long long *xorpadaddr;

  Tcl_Obj *resultPtr;


  ctraddr = &counter;
  xorpadaddr = &xorpad;


  if (objc != 4) {
    Tcl_WrongNumArgs(ip, 1, objv, "data key counter");
    return TCL_ERROR;
  }

  datain = Tcl_GetByteArrayFromObj(objv[1], &datalen);

  key = Tcl_GetByteArrayFromObj(objv[2], &klen);  

  Tcl_GetLongFromObj(ip, objv[3], &counter); 

  /* put key into correct var type */
                for (x = 0; x < 16 ; x++) {
    k[x] = key[x];
  }  


  /* datalen holds the length of the incomming crypted code */

  dataout = ckalloc (datalen);

  x = 8;

                for (bytenum = 0; bytenum < datalen ; bytenum++) {    
    if (x == 8) {       
      *xorpadaddr = *ctraddr;
      encryptc (xorpadaddr, &k);  
      x = 0;
      counter++;
    }

    dataout[bytenum] = xorpad[x] ^ datain[bytenum];

    x++;

  }  

  resultPtr = Tcl_NewByteArrayObj (dataout, (datalen));

  Tcl_SetObjResult (ip, resultPtr);

  ckfree (dataout);   

  return TCL_OK;
 }

I've tested it on Linux and Windows and it works well so far.

Examples: