|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Aes.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
|
|
|
* Symmetric AES-256-CBC encryption functions powered by OpenSSL. |
|
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 Aes extends Cryptobase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* AES-256 cipher identifier that will be passed to openssl |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const CIPHER = 'aes-256-cbc'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Size of initialization vector in bytes |
|
38
|
|
|
* |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
const IVSIZE = 16; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Size of checksum in bytes |
|
45
|
|
|
* |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
const CKSIZE = 32; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Decrypt cyphertext |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $cyphertext Cyphertext to decrypt |
|
54
|
|
|
* @param string $password Password that should be used to decrypt input data |
|
55
|
|
|
* @param int $cost Number of extra HMAC iterations to perform on key |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function decrypt(string $cyphertext, string $password, int $cost = 0): string |
|
60
|
|
|
{ |
|
61
|
|
|
// Find the IV at the beginning of the cypher text |
|
62
|
|
|
$ivr = Str::substr($cyphertext, 0, self::IVSIZE); |
|
63
|
|
|
|
|
64
|
|
|
// Derive key from password |
|
65
|
8 |
|
$key = self::key($password, $ivr, $cost, self::mode()); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// Gather the checksum portion of the cypher text |
|
68
|
8 |
|
$sum = Str::substr($cyphertext, self::IVSIZE, self::CKSIZE); |
|
69
|
|
|
|
|
70
|
|
|
// Gather message portion of cyphertext after iv and checksum |
|
71
|
8 |
|
$msg = Str::substr($cyphertext, self::IVSIZE + self::CKSIZE); |
|
72
|
|
|
|
|
73
|
|
|
// Calculate verification checksum |
|
74
|
8 |
|
$chk = self::checksum($msg, $ivr, $key, self::mode()); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
// Verify HMAC before decrypting |
|
77
|
8 |
|
self::checksumVerify($chk, $sum); |
|
78
|
|
|
|
|
79
|
|
|
// Decrypt message and return |
|
80
|
8 |
|
return \openssl_decrypt($msg, static::CIPHER, $key, 1, $ivr); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
8 |
|
/** |
|
84
|
|
|
* Encrypt plaintext |
|
85
|
|
|
* |
|
86
|
6 |
|
* @param string $plaintext Plaintext string to encrypt. |
|
87
|
|
|
* @param string $password Password used to encrypt data. |
|
88
|
|
|
* @param int $cost Number of extra HMAC iterations to perform on key |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function encrypt(string $plaintext, string $password, int $cost = 0): string |
|
93
|
|
|
{ |
|
94
|
|
|
// Generate IV of appropriate size. |
|
95
|
|
|
$ivr = \random_bytes(self::IVSIZE); |
|
96
|
|
|
|
|
97
|
|
|
// Derive key from password |
|
98
|
7 |
|
$key = self::key($password, $ivr, $cost, self::mode()); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
// Encrypt the plaintext |
|
101
|
7 |
|
$msg = \openssl_encrypt($plaintext, static::CIPHER, $key, 1, $ivr); |
|
102
|
|
|
|
|
103
|
|
|
// If message could not be encrypted then throw an exception |
|
104
|
7 |
|
if ($msg === false) { |
|
105
|
|
|
throw new \exception('Could not encrypt the data.'); // @codeCoverageIgnore |
|
106
|
|
|
} |
|
107
|
7 |
|
|
|
108
|
|
|
// Create the cypher text prefix (iv + checksum) |
|
109
|
|
|
$prefix = $ivr . self::checksum($msg, $ivr, $key, self::mode()); |
|
|
|
|
|
|
110
|
7 |
|
|
|
111
|
|
|
// Return prefix + cyphertext |
|
112
|
|
|
return $prefix . $msg; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.