
                            * * * * *

                TEA, A TINY ENCRYPTION ALGORITHM

By David Wheeler and Roger Needham, Computer Laboratory, 
Cambridge University, England - November 1994.

The following block cipher is in the public domain and is/will be
published in: B.Preneel, ed., Proceedings of the 1994 K.U. Leuven
Workshop on Cryptographic Algorithms, Lecture Notes in Computer 
Science, Springer-Verlag, 1995.  This posting has been agreed by 
David Wheeler.



Encode Routine 

The routine, written in the C language, is used for encoding with

a key, k[0] - k[3].  Data is in v[0] and v[1]. 

void code(long* v, long* k)  
{   unsigned long y = v[0],
                  z = v[1], 
                  sum = 0,                           /* set up */
                  delta = 0x9e3779b9, 
                  n = 32;           /* a key schedule constant */

    while(n-- > 0)                        /* basic cycle start */
    {   sum += delta;
        y += (z << 4) + k[0] ^ z + sum ^ (z >> 5) + k[1];
        z += (y << 4) + k[2] ^ y + sum ^ (y >> 5) + k[3];
    }                                             /* end cycle */

    v[0] = y;
    v[1] = z;
}



Decode Routine 

void decode( long* v, long* k )  
{   unsigned long n = 32, 
                  sum, 
                  y = v[0], 
                  z=v[1],
                  delta = 0x9e3779b9;

    sum = delta << 5;
                                                /* start cycle */
    while(n-- > 0) 
    {   z -= (y << 4) + k[2] ^ y + sum ^ (y >> 5) + k[3]; 
        y -= (z << 4) + k[0] ^ z + sum ^ (z >> 5) + k[1];
        sum -= delta;
    }
                                                  /* end cycle */
    v[0] = y;
    v[1] = z;
}

