1 | <?php |
||
10 | trait CryptTrait |
||
11 | { |
||
12 | private $key; |
||
13 | private $authentication; |
||
14 | |||
15 | /** |
||
16 | * Set the keys to encrypt and authenticate. |
||
17 | * |
||
18 | * @param string $key The binary key |
||
19 | * |
||
20 | * @return self |
||
21 | */ |
||
22 | public function key($key) |
||
23 | { |
||
24 | $this->key = self::hkdf($key, 'KeyForEncryption'); |
||
25 | $this->authentication = self::hkdf($key, 'KeyForAuthentication'); |
||
26 | |||
27 | return $this; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Encrypt the given value. |
||
32 | * |
||
33 | * @param string $value |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | private function encrypt($value) |
||
38 | { |
||
39 | $this->checkKey(); |
||
40 | |||
41 | $iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); |
||
42 | $cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, json_encode($value), 'ctr', $iv); |
||
43 | $hmac = hash_hmac('sha256', $iv.$cipher, $this->authentication, true); |
||
44 | |||
45 | return base64_encode($hmac.$iv.$cipher); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Decrypt the given value. |
||
50 | * |
||
51 | * @param string $value |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | private function decrypt($value) |
||
56 | { |
||
57 | $this->checkKey(); |
||
58 | |||
59 | $decoded = base64_decode($value); |
||
60 | $hmac = mb_substr($decoded, 0, 32, '8bit'); |
||
61 | $iv = mb_substr($decoded, 32, 16, '8bit'); |
||
62 | $cipher = mb_substr($decoded, 48, null, '8bit'); |
||
63 | $calculated = hash_hmac('sha256', $iv.$cipher, $this->authentication, true); |
||
64 | |||
65 | if (Helpers::hashEquals($hmac, $calculated)) { |
||
66 | $value = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $cipher, 'ctr', $iv), "\0"); |
||
67 | |||
68 | return json_decode($value, true); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Check whether the key exists or not. |
||
74 | * |
||
75 | * @throws RuntimeException |
||
76 | */ |
||
77 | private function checkKey() |
||
90 | |||
91 | /** |
||
92 | * Generate a secure random key. |
||
93 | * |
||
94 | * @return string|null |
||
95 | */ |
||
96 | private static function secureRandomKey() |
||
97 | { |
||
98 | if (!function_exists('openssl_random_pseudo_bytes')) { |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | $secure = false; |
||
103 | $random = openssl_random_pseudo_bytes(16, $secure); |
||
104 | |||
105 | if ($secure) { |
||
106 | return $random; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get derived key |
||
112 | * http://tools.ietf.org/html/rfc5869. |
||
113 | * |
||
114 | * @param string $ikm Initial Keying Material |
||
115 | * @param string $info What sort of key are we deriving? |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | private static function hkdf($ikm, $info = '') |
||
135 | } |
||
136 |