From owner-freebsd-questions@FreeBSD.ORG Fri Aug 8 10:05:51 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id EE7701065679 for ; Fri, 8 Aug 2008 10:05:51 +0000 (UTC) (envelope-from merv@merv.org.uk) Received: from queueout02-winn.ispmail.ntl.com (queueout02-winn.ispmail.ntl.com [81.103.221.56]) by mx1.freebsd.org (Postfix) with ESMTP id 773CD8FC1D for ; Fri, 8 Aug 2008 10:05:51 +0000 (UTC) (envelope-from merv@merv.org.uk) Received: from aamtaout03-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout03-winn.ispmail.ntl.com with ESMTP id <20080808094633.VXEH10791.mtaout03-winn.ispmail.ntl.com@aamtaout03-winn.ispmail.ntl.com> for ; Fri, 8 Aug 2008 10:46:33 +0100 Received: from localhost.home ([86.3.143.73]) by aamtaout03-winn.ispmail.ntl.com with ESMTP id <20080808094633.QXOP29597.aamtaout03-winn.ispmail.ntl.com@localhost.home> for ; Fri, 8 Aug 2008 10:46:33 +0100 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost.home (Postfix) with ESMTP id 374A5B846 for ; Fri, 8 Aug 2008 10:46:36 +0100 (BST) Message-ID: <489C15F8.9090800@merv.org.uk> Date: Fri, 08 Aug 2008 10:46:32 +0100 From: merv User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) MIME-Version: 1.0 To: freebsd-questions@freebsd.org Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Subject: Inconsistent behavior. PHP5 FreeBSD server Vs PHP5 Windows Server. X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-questions@freebsd.org List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Aug 2008 10:05:52 -0000 Hi, I have a strange problem with PHP5 on FreeBSD. When run on a FreeBSD server the decrypt function of a xTea encryption library does not work correctly. While the same PHP code runs without problem on a Windows Server. Has anybody experienced similar problems? I am at a dead end any help would be much appreciated. Windows output: xTea A secret message. Meet at 21:00 by the old bridge to talk about the new plan. hK/xEOKqgx+Tfb7tCndxFH/3HTck+cy3+y1uMa/DUWNgg7I91/QeG2BceCmtaDYmFjPRAczqCHCc LHMWiGE0ZQV+QC+f3xcJWvtGxLIdDHY= A secret message. Meet at 21:00 by the old bridge to talk about the new plan. FreeBSD output: xTea A secret message. Meet at 21:00 by the old bridge to talk about the new plan. hK/xEOKqgx+Tfb7tCndxFH/3HTck+cy3+y1uMa/DUWNgg7I91/QeG2BceCmtaDYmFjPRAczqCHCc LHMWiGE0ZQV+QC+f3xcJWvtGxLIdDHY= fscf[[*x5L$lB%T GXq-H>(;H8 Note encryption works fine it is possible to encrypt the message on FreeBSD and decrypt the message on Windows. The message also decrypts correctly using a Javascript implementation of xTea. The only part that fails is decrypt under FreeBSD. Is this expected and common? PHP source:

xTea

xTEA.php: 8) { // $res .= crypt8(substr($str,0,8),$key,$encrypt,$itr); $res .= JScrypt8(substr($str,0,8),$key,$encrypt,$itr); $str = substr($str,8); } if (strlen($str)>0) { while (strlen($str)<8) { $str .= ' '; } // $res .= crypt8($str,$key,$encrypt,$itr); $res .= JScrypt8($str,$key,$encrypt,$itr); } return rtrim($res,' '); } //Four-byte truncate function fbt($x) { $x = $x&0x0FFFFFFFF; return $x<0?0x0100000000+$x:$x; } function JScrypt8($oct,$key,$encrypt,$itr) { $y=0; $z=0; $k=array(); $k[0]=$k[1]=$k[2]=$k[3]=0; $d=0x9E3779B9; $sum=$encrypt?0:($d*$itr); $res=""; for ($i=0; $i<8; ) { $y=fbt(($y<<8)+(ord($oct{$i})&0xFF)); $k[$i&3]=fbt(($k[$i&3]<<8)+ord($key{$i})); $k[$i&3]=fbt(($k[$i&3]<<8)+ord($key{$i+8})); $i++; $z=fbt(($z<<8)+(ord($oct{$i})&0xFF)); $k[$i&3]=fbt(($k[$i&3]<<8)+ord($key{$i})); $k[$i&3]=fbt(($k[$i&3]<<8)+ord($key{$i+8})); $i++; } if ($encrypt) { while ($itr-->0) { $y = fbt(($y+fbt(($z*16)^floor($z/32))+fbt($z^$sum)+$k[$sum&3])); $sum=$sum+$d; $z = fbt(($z+fbt(($y*16)^floor($y/32))+fbt($y^$sum)+$k[($sum>>11)&3])); } } else { while ($itr-->0) { $z = fbt($z-fbt(fbt(($y*16)^floor($y/32))+fbt($y^$sum)+$k[($sum>>11)&3])); $sum=$sum-$d; $y = fbt($y-fbt(fbt(($z*16)^floor($z/32))+fbt($z^$sum)+$k[$sum&3])); } } for ($i=4; $i-->0; ) { $res .= chr(fbt(($y&0xFF000000)>>24)); $y = $y<<8; $res .= chr(fbt(($z&0xFF000000)>>24)); $z=$z<<8; } return $res; } function crypt8($oct,$key,$encrypt,$itr) { $y=0; $z=0; $k=array(); $k[0]=$k[1]=$k[2]=$k[3]=0; $d=0x9E3779B9; $sum=$encrypt?0:($d*$itr)&0x0ffffffff; $res=""; for ($i=0; $i<8; ) { $y=($y<<8)+(ord($oct{$i})&0xFF); $k[$i&3]=($k[$i&3]<<8)+ord($key{$i}); $k[$i&3]=($k[$i&3]<<8)+ord($key{$i+8}); $i++; $z=($z<<8)+(ord($oct{$i})&0xFF); $k[$i&3]=($k[$i&3]<<8)+ord($key{$i}); $k[$i&3]=($k[$i&3]<<8)+ord($key{$i+8}); $i++; } if ($encrypt) { while ($itr-->0) { $y = ($y+(($z<<4)^($z>>5))+($z^$sum)+$k[$sum&3])&0x0ffffffff; $sum=$sum+$d; $z = ($z+(($y<<4)^($y>>5))+($y^$sum)+$k[($sum>>11)&3])&0x0ffffffff; } } else { while ($itr-->0) { $z = ($z+0x0100000000-(((($y<<4)^($y>>5))+($y^$sum)+$k[($sum>>11)&3])&0x0ffffffff))&0x0ffffffff; $sum=($sum+0x0100000000-$d)&0x0ffffffff; $y = ($y+0x0100000000-(((($z<<4)^($z>>5))+($z^$sum)+$k[$sum&3])&0x0ffffffff))&0x0ffffffff; } } for ($i=4; $i-->0; ) { $res .= chr(($y&0xFF000000)>>24); $y = $y<<8; $res .= chr(($z&0xFF000000)>>24); $z=$z<<8; } return $res; } ?>