do some changes to support other speedport router
[GitHub/Stricted/speedport-hybrid-php-api.git] / lib / trait / CryptLib.class.php
1 <?php
2 /**
3 * @author Jan Altensen (Stricted)
4 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
5 * @copyright 2015 Jan Altensen (Stricted)
6 */
7 trait CryptLib {
8 /**
9 * decrypt data from router
10 *
11 * @param string $data
12 * @return array
13 */
14 private function decrypt ($data) {
15 $iv = hex2bin(substr($this->challenge, 16, 16));
16 $adata = hex2bin(substr($this->challenge, 32, 16));
17 $key = hex2bin($this->derivedk);
18 $enc = hex2bin($data);
19
20 $factory = new CryptLib\Cipher\Factory();
21 $aes = $factory->getBlockCipher('rijndael-128');
22 $aes->setKey($key);
23 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
24
25 $mode->decrypt($enc);
26
27 return $mode->finish();
28 }
29
30 /**
31 * decrypt data for the router
32 *
33 * @param string $data
34 * @return string
35 */
36 private function encrypt ($data) {
37 $iv = hex2bin(substr($this->challenge, 16, 16));
38 $adata = hex2bin(substr($this->challenge, 32, 16));
39 $key = hex2bin($this->derivedk);
40
41 $factory = new CryptLib\Cipher\Factory();
42 $aes = $factory->getBlockCipher('rijndael-128');
43 $aes->setKey($key);
44 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
45 $mode->encrypt($data);
46
47 return bin2hex($mode->finish());
48 }
49 }