|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cryptobase.php |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Dcrypt |
|
9
|
|
|
* @package Dcrypt |
|
10
|
|
|
* @author Michael Meyer (mmeyer2k) <[email protected]> |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
* @link https://github.com/mmeyer2k/dcrypt |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Dcrypt; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Provides functionality common to Dcrypt's block ciphers. |
|
19
|
|
|
* |
|
20
|
|
|
* @category Dcrypt |
|
21
|
|
|
* @package Dcrypt |
|
22
|
|
|
* @author Michael Meyer (mmeyer2k) <[email protected]> |
|
23
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
24
|
|
|
* @link https://github.com/mmeyer2k/dcrypt |
|
25
|
|
|
* @link https://apigen.ci/github/mmeyer2k/dcrypt/namespace-Dcrypt.html |
|
26
|
|
|
*/ |
|
27
|
|
|
class Cryptobase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* This string is used when hashing to ensure cross compatibility between |
|
31
|
|
|
* dcrypt\mcrypt and dcrypt\aes. Since v7, this is only needed for backwards |
|
32
|
|
|
* compatibility with older versions |
|
33
|
|
|
*/ |
|
34
|
|
|
const RIJNDA = 'rijndael-128'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Hardcoded hashing algo string. |
|
38
|
|
|
*/ |
|
39
|
|
|
const ALGO = 'sha256'; |
|
40
|
|
|
|
|
41
|
10 |
|
/** |
|
42
|
|
|
* Create a message authentication checksum. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $cyphertext Cyphertext that needs a checksum. |
|
45
|
10 |
|
* @param string $iv Initialization vector. |
|
46
|
|
|
* @param string $key HMAC key |
|
47
|
|
|
* @param string $mode Cipher mode (cbc, ctr) |
|
48
|
10 |
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected static function checksum(string $cyphertext, string $iv, string $key, string $mode): string |
|
52
|
|
|
{ |
|
53
|
10 |
|
// Prevent potentially large string concat by hmac-ing the cyphertext |
|
54
|
|
|
// by itself... |
|
55
|
|
|
$sum = \hash_hmac(self::ALGO, $cyphertext, $key, true); |
|
56
|
|
|
|
|
57
|
|
|
// If algo is unknown, throw an exception |
|
58
|
|
|
if ($sum === false) { |
|
59
|
|
|
throw new \exception("$algo is not supported by hash_hmac"); // @codeCoverageIgnore |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// ... then hash other elements with previous hmac and return |
|
63
|
|
|
return \hash_hmac(self::ALGO, $sum . $iv . $mode . self::RIJNDA, $key, true); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Transform password into key and perform iterative HMAC (if specified) |
|
68
|
|
|
* |
|
69
|
10 |
|
* @param string $password Encryption key |
|
70
|
|
|
* @param string $iv Initialization vector |
|
71
|
|
|
* @param int $cost Number of HMAC iterations to perform on key |
|
72
|
10 |
|
* @param string $mode Cipher mode (cbc, ctr) |
|
73
|
1 |
|
* |
|
74
|
1 |
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected static function key(string $password, string $iv, int $cost, string $mode): string |
|
77
|
10 |
|
{ |
|
78
|
|
|
// Perform key derivation |
|
79
|
|
|
return Hash::ihmac($iv . self::RIJNDA . $mode, $password, $cost, self::ALGO); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Verify checksum during decryption step and throw error if mismatching. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $calculated |
|
86
|
|
|
* @param string $supplied |
|
87
|
|
|
*/ |
|
88
|
|
|
protected static function checksumVerify(string $calculated, string $supplied) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!Str::equal($calculated, $supplied)) { |
|
91
|
|
|
$e = 'Decryption can not proceed due to invalid cyphertext checksum.'; |
|
92
|
10 |
|
throw new \InvalidArgumentException($e); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
10 |
|
/** |
|
97
|
10 |
|
* Return the encryption mode string. "cbc" or "ctr" |
|
98
|
10 |
|
* |
|
99
|
1 |
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected static function mode(): string |
|
102
|
|
|
{ |
|
103
|
10 |
|
return substr(static::CIPHER, -3); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|