XTEA block cipher implement in PHP and Javascript


In cryptography, XTEA (eXtended TEA) is a block cipher designed to correct weaknesses in TEA. The cipher’s designers were David Wheeler and Roger Needham of the Cambridge Computer Laboratory, and the algorithm was presented in an unpublished technical report in 1997 (Needham and Wheeler, 1997). It is not subject to any patents.[1]

Like TEA, XTEA is a 64-bit block Feistel cipher with a 128-bit key and a suggested 64 rounds. Several differences from TEA are apparent, including a somewhat more complex key-schedule and a rearrangement of the shifts, XORs, and additions.

Source: https://en.wikipedia.org/wiki/XTEA

We provide some function in PHP and Javascript:

PHP

<?php

/**
 * @author www.Tutorialspots.com
 * @copyright 2017
 */

class XTEA{
        /**
     * XTEA encrypt
     * @param $v 64bits
     * @param $k 128bits
     */
    public static function XTEAencrypt ($v, $k) {
        $v0=$v[0]; $v1=$v[1]; $sum=0;           /* set up */
        $delta=0x9e3779b9;                     /* a key schedule constant */
        $num_rounds = 64;
        $k0=$k[0]; $k1=$k[1]; $k2=$k[2]; $k3=$k[3];   /* cache key */
        for ($i=0; $i < $num_rounds; $i++) {                       /* basic cycle start */            
            $v0 += ((($v1<<4) ^ ($v1>>5)) + $v1) ^ ($sum + $k[$sum & 3]);
            $sum += $delta;
            $v1 += ((($v0 << 4) ^ ($v0 >> 5)) + $v0) ^ ($sum + $k[($sum>>11) & 3]);
        }                                              /* end cycle */
        $v[0]=$v0; $v[1]=$v1;
        return $v;
    }
    
    /**
     * XTEA decrypt
     * @param $v 64bits
     * @param $k 128bits
     */
    public static function XTEAdecrypt ($v, $k) {
        $num_rounds = 64;
        $delta=0x9e3779b9;                     /* a key schedule constant */
        $v0=$v[0]; $v1=$v[1]; $sum=$delta*$num_rounds;  /* set up */
        $k0=$k[0]; $k1=$k[1]; $k2=$k[2]; $k3=$k[3];   /* cache key */
        for ($i=0; $i<$num_rounds; $i++) {                         /* basic cycle start */
            $v1 -= ((($v0 << 4) ^ ($v0 >> 5)) + $v0) ^ ($sum + $k[($sum>>11) & 3]);
            $sum -= $delta;
            $v0 -= ((($v1 << 4) ^ ($v1 >> 5)) + $v1) ^ ($sum + $k[$sum & 3]);          
        }                                              /* end cycle */
        $v[0]=$v0; $v[1]=$v1;
        return $v;        
    }
}

?>

Javascript

var XTEA = {};
   /**
     * XTEA encrypt
     * @param v 64bits
     * @param k 128bits
     */
XTEA.XTEAencrypt = function (v, k) {
  var v0=v[0], v1=v[1], sum=0,           /* set up */
  delta=0x9e3779b9,                     /* a key schedule constant */
  num_rounds = 64,
  k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
  for (var i=0; i < num_rounds; i++) {                       /* basic cycle start */    
    v0 += (((v1<<4) ^ (v1>>5)) + v1) ^ (sum + k[sum & 3]);
    sum += delta;
    v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  }                                              /* end cycle */
  v[0]=v0; v[1]=v1;
  return v;
}

   /**
     * XTEA decrypt
     * @param v 64bits
     * @param k 128bits
     */
XTEA.XTEAdecrypt = function (v, k) {
  var num_rounds = 64,
  delta=0x9e3779b9,                     /* a key schedule constant */
  v0=v[0], v1=v[1], sum=num_rounds*delta,  /* set up */
  
  k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
  for (var i=0; i<num_rounds; i++) {                         /* basic cycle start */
    v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
    sum -= delta;
    v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);       
  }                                              /* end cycle */
  v[0]=v0; v[1]=v1;
  return v;
}

Example:

var x=XTEA.XTEAencrypt([65,65],[65,65,65,65]);
console.log(x);//[5141257873,-3630722929]
alert(XTEA.XTEAdecrypt(x,[65,65,65,65]));

Try it yourself

Read more:
TEA block cipher implement in PHP and Javascript
Test vectors TEA
XXTEA block cipher implement in PHP and Javascript

1 Comment

Leave a Reply