X-Git-Url: https://git.stricted.de/?p=GitHub%2FStricted%2Fspeedport-hybrid-php-api.git;a=blobdiff_plain;f=lib%2Ftrait%2FCryptLib.class.php;fp=lib%2Ftrait%2FCryptLib.class.php;h=fb89e96d257f5c56bce70dc5db398f2b35fbbe83;hp=eedecbbf954c904ed2a8c3b5b1cec6705c8d53d8;hb=8d0ef90a01ef074a0ab00e58d534195763cb09ec;hpb=d5fcf33db575918d2f4f1ba2d69008885c2a39a4 diff --git a/lib/trait/CryptLib.class.php b/lib/trait/CryptLib.class.php index eedecbb..fb89e96 100644 --- a/lib/trait/CryptLib.class.php +++ b/lib/trait/CryptLib.class.php @@ -31,14 +31,22 @@ trait CryptLib { $key = hex2bin($this->derivedk); $enc = hex2bin($data); - $factory = new CryptLib\Cipher\Factory(); - $aes = $factory->getBlockCipher('rijndael-128'); - $aes->setKey($key); - $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); - - $mode->decrypt($enc); - - return $mode->finish(); + if (PHP_VERSION_ID >= 70100) { + $ciphertext = substr($enc, 0, -8); + $tag = substr($enc, strlen($enc)-8); + + return openssl_decrypt($ciphertext, 'aes-128-ccm', $key, OPENSSL_RAW_DATA, $iv, $tag, $adata); + } + else { + $factory = new CryptLib\Cipher\Factory(); + $aes = $factory->getBlockCipher('rijndael-128'); + $aes->setKey($key); + $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); + + $mode->decrypt($enc); + + return $mode->finish(); + } } /** @@ -52,12 +60,23 @@ trait CryptLib { $adata = hex2bin(substr($this->challenge, 32, 16)); $key = hex2bin($this->derivedk); - $factory = new CryptLib\Cipher\Factory(); - $aes = $factory->getBlockCipher('rijndael-128'); - $aes->setKey($key); - $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); - $mode->encrypt($data); + if (empty($data)) { + return $data; + } - return bin2hex($mode->finish()); + if (PHP_VERSION_ID >= 70100) { + $tag = null; + $encdata = openssl_encrypt($data, 'aes-128-ccm', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv, $tag, $adata, 8); + return bin2hex($encdata . $tag); + } + else { + $factory = new CryptLib\Cipher\Factory(); + $aes = $factory->getBlockCipher('rijndael-128'); + $aes->setKey($key); + $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); + $mode->encrypt($data); + var_dump(bin2hex($mode->finish())); + return bin2hex($mode->finish()); + } } }