Commit 3220dff3 authored by Alex Ne's avatar Alex Ne

64 bit OS fix

parent 3cf5daa5
<?php
namespace X\Accounting;
class SessionChecksumError extends \X\ETrace\Notification {}
class SessionHacked extends \X\ETrace\Notification {}
use X\ETrace\System;
class SessionChecksumError extends \X\ETrace\Notification {
}
class SessionHacked extends \X\ETrace\Notification {
}
/**
* Session Manager
*
* Session struct:
* t = type(int)
* s = session_id
* cc = crypto code (спец код с возможностю прокрутки назад и вперед.)
* cs = crypto cheksum
* a = activation_time
* u = user_id
* hs = is_https(bool)
* Session struct:
* t = type(int)
* s = session_id
* cc = crypto code (спец код с возможностю прокрутки назад и вперед.)
* cs = crypto cheksum
* a = activation_time
* u = user_id
* hs = is_https(bool)
*
* crypto cheksum:
* cheksum = ((( a & ( ! s ) ) | cc ) ^ u ) >> (t ^ op1)
* crypto cheksum:
* cheksum = ((( a & ( ! s ) ) | cc ) ^ u ) >> (t ^ op1)
*
* crypto code:
* code = rand(0,time());
* next_code = (code >> op2) ^ op3
* prev_code = (code ^ op3) << op2
* crypto code:
* code = rand(0,time());
* next_code = (code >> op2) ^ op3
* prev_code = (code ^ op3) << op2
*/
class Session extends \X\Security\Crypto\IDEA {
//protected function BitwiseCROR($v, $c)
......@@ -65,6 +70,19 @@ class Session extends \X\Security\Crypto\IDEA {
return $this->session_data;
}
public function get_system_bits() {
switch (PHP_INT_SIZE) {
case 4:
return 32;
break;
case 8:
return 64;
break;
default:
throw new \X\ETrace\System("OS bits PROBLEM");
}
}
/**
* @return mixed
*/
......@@ -79,11 +97,25 @@ class Session extends \X\Security\Crypto\IDEA {
if (is_string($SessionString = $this->decrypt_b64($this->session))) {
if (is_array($session_data = $this->explode(gzuncompress($SessionString)))) {
if (isset($session_data["cs"])) {
if ($session_data["cs"] == $this->crypto_checksum($session_data)) {
$checksum_valid = false;
if (isset($session_data["b"])) {
if ($session_data["b"] == 32 && $session_data["cs"] == $this->crypto_checksum($session_data, 32)) $checksum_valid = true;
if ($session_data["b"] == 64 && $session_data["cs"] == $this->crypto_checksum($session_data, 64)) $checksum_valid = true;
} else {
if ($session_data["cs"] == $this->crypto_checksum($session_data, 32)) {
$checksum_valid = true;
$session_data["b"] = 32;
}
if ($session_data["cs"] == $this->crypto_checksum($session_data, 64)) {
$checksum_valid = true;
$session_data["b"] = 64;
}
}
if ($checksum_valid) {
$this->session_data = $session_data;
return true;
} else {
throw new SessionChecksumError("Checksum Error", ["in_function" => get_defined_vars(), "in_class" => $this]);
throw new SessionChecksumError("Checksum Error", ["in_function" => get_defined_vars(), "in_class" => $this->session]);
}
}
}
......@@ -99,8 +131,9 @@ class Session extends \X\Security\Crypto\IDEA {
if ( ! isset($session_data["cc"])) {
$session_data["cc"] = $this->crypto_code_new();
}
$session_data["cs"] = $this->crypto_checksum($session_data);
$this->session_data = $session_data;
$session_data["b"] = $this->get_system_bits();
$session_data["cs"] = $this->crypto_checksum($session_data, $session_data["b"]);
$this->session_data = $session_data;
return $this->session = $this->crypt_b64(gzcompress($this->implode($session_data)));
}
......@@ -138,26 +171,53 @@ class Session extends \X\Security\Crypto\IDEA {
* @param $code
*/
protected function crypto_code_next($code) {
return $this->BitwiseCROR($code, $this->param_crypto[1]) ^ $this->param_crypto[2];
if (isset($this->session_data["b"])) switch ($this->session_data["b"]) {
case 32:
return $this->BitwiseCROR($code, $this->param_crypto[1]) ^ $this->param_crypto[2];
break;
case 64;
return $this->BitwiseCROR64($code, $this->param_crypto[1]) ^ $this->param_crypto[2];
break;
default:
throw new System("OS bits not found.");
}
}
/**
* @param $code
*/
protected function crypto_code_prev($code) {
return $this->BitwiseCROL(($code ^ $this->param_crypto[2]), $this->param_crypto[1]);
if (isset($this->session_data["b"])) switch ($this->session_data["b"]) {
case 32:
return $this->BitwiseCROL(($code ^ $this->param_crypto[2]), $this->param_crypto[1]);
break;
case 64;
return $this->BitwiseCROL64(($code ^ $this->param_crypto[2]), $this->param_crypto[1]);
break;
default:
throw new System("OS bits not found.");
}
}
/**
* @return mixed
*/
protected function crypto_checksum($session_data) {
protected function crypto_checksum($session_data, $bits = 32) {
if (is_array($session_data)) {
if ( ! $this->check_data_colls($session_data)) {
throw new SessionHacked("Session data not full!", $session_data);
}
$D = array_map(function ($i) {return intval($i);}, $session_data);
return $this->BitwiseCROR(((($D["a"] & ( ! $D["s"])) | $D["cc"]) ^ $D["u"]), ($D["t"] ^ $this->param_crypto[0]));
$D = array_map(function($i) { return intval($i); }, $session_data);
switch ($bits) {
case 32:
return $this->BitwiseCROR(((($D["a"] & ( ! $D["s"])) | $D["cc"]) ^ $D["u"]), ($D["t"] ^ $this->param_crypto[0]));
break;
case 64:
return $this->BitwiseCROR(((($D["a"] & ( ! $D["s"])) | $D["cc"]) ^ $D["u"]), ($D["t"] ^ $this->param_crypto[0]));
break;
default:
throw new System("Count of bits wrong");
}
}
}
......@@ -165,10 +225,8 @@ class Session extends \X\Security\Crypto\IDEA {
* @param $Data
*/
protected function set_cookie($Data) {
setcookie($this->session_name,
$this->make_session($Data),
time() + (60 * 60 * 24 * 30 * 12 * 10), ////////////////////////////////// TIME LIVE COOKIE 10 years
"/"
setcookie($this->session_name, $this->make_session($Data), time() + (60 * 60 * 24 * 30 * 12 * 10), ////////////////////////////////// TIME LIVE COOKIE 10 years
"/"
);
}
......@@ -176,12 +234,13 @@ class Session extends \X\Security\Crypto\IDEA {
* @param $data
*/
protected function implode($data) {
array_walk($data, function (&$i, $k) {$i = implode(":", [$k, $i]);});
array_walk($data, function(&$i, $k) { $i = implode(":", [$k, $i]); });
return implode(";", $data);
}
/**
* @param $string
*
* @return mixed
*/
protected function explode($string) {
......@@ -189,7 +248,7 @@ class Session extends \X\Security\Crypto\IDEA {
$data = [];
foreach ($data_t as $value) {
list($k, $i) = explode(":", $value);
$data[$k] = $i;
$data[$k] = $i;
}
return $data;
}
......@@ -199,10 +258,10 @@ class Session extends \X\Security\Crypto\IDEA {
*
* class Session extends X\Accounting\Session
* {
* public __construct()
* {
* parent::__construct(Config::KEY, Config::NAME, Config::CRYPTO);
* }
* public __construct()
* {
* parent::__construct(Config::KEY, Config::NAME, Config::CRYPTO);
* }
* }
*
*
......
......@@ -24,6 +24,28 @@ trait BitwiseCyclicShift {
$c = $c % 32;
return $c ? ($v << $c) | ((($v >> 1) & 2147483647) >> (31 - $c)) : $v;
}
/**
* Побитовый циклический сдвиг вправо (64bit)
* @param int $v value
* @param int $c count
* @return int
*/
protected function BitwiseCROR64($v, $c) {
$c = $c % 64;
return $c ? ((($v >> 1) & PHP_INT_MAX) >> ($c - 1)) | ($v << (64 - $c)) : $v;
}
/**
* Побитовый циклический сдвиг влево (64bit)
* @param int $v value
* @param int $c count
* @return int
*/
protected function BitwiseCROL64($v, $c) {
$c = $c % 64;
return $c ? ($v << $c) | ((($v >> 1) & PHP_INT_MAX) >> (63 - $c)) : $v;
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment