|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Aes.php |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 5 |
|
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
|
|
|
/** |
|
31
|
|
|
* AES-256 cipher identifier that will be passed to openssl |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
const CIPHER = 'aes-256-cbc'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Size of initialization vector in bytes |
|
39
|
|
|
* |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
const IVSIZE = 16; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Size of checksum in bytes |
|
46
|
|
|
* |
|
47
|
|
|
* @var int |
|
48
|
|
|
*/ |
|
49
|
|
|
const CKSIZE = 32; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Decrypt cyphertext |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $cyphertext Cyphertext to decrypt |
|
55
|
|
|
* @param string $password Password that should be used to decrypt input data |
|
56
|
|
|
* @param int $cost Number of HMAC iterations to perform on key |
|
57
|
|
|
* |
|
58
|
|
|
* @return string|boolean Returns false on checksum validation failure |
|
59
|
|
|
*/ |
|
60
|
7 |
|
public static function decrypt($cyphertext, $password, $cost = 0) |
|
61
|
|
|
{ |
|
62
|
|
|
// Find the IV at the beginning of the cypher text |
|
63
|
7 |
|
$iv = Str::substr($cyphertext, 0, self::IVSIZE); |
|
64
|
|
|
|
|
65
|
|
|
// Gather the checksum portion of the cypher text |
|
66
|
7 |
|
$chksum = Str::substr($cyphertext, self::IVSIZE, self::CKSIZE); |
|
67
|
|
|
|
|
68
|
|
|
// Gather message portion of cyphertext after iv and checksum |
|
69
|
7 |
|
$message = Str::substr($cyphertext, self::IVSIZE + self::CKSIZE); |
|
70
|
|
|
|
|
71
|
|
|
// Derive key from password |
|
72
|
7 |
|
$key = self::key($password, $iv, $cost, 'rijndael-128', static::mode()); |
|
73
|
|
|
|
|
74
|
|
|
// Calculate verification checksum |
|
75
|
7 |
|
$verify = self::checksum($message, $iv, $key); |
|
76
|
|
|
|
|
77
|
|
|
// Verify HMAC before decrypting |
|
78
|
7 |
|
self::checksumVerify($verify, $chksum); |
|
79
|
|
|
|
|
80
|
|
|
// Decrypt message and return |
|
81
|
6 |
|
return \openssl_decrypt($message, static::CIPHER, $key, 1, $iv); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Encrypt plaintext |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $plaintext Plaintext string to encrypt. |
|
88
|
|
|
* @param string $password Password used to encrypt data. |
|
89
|
|
|
* @param int $cost Number of HMAC iterations to perform on key |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
6 |
|
public static function encrypt($plaintext, $password, $cost = 0) |
|
94
|
|
|
{ |
|
95
|
|
|
// Generate IV of appropriate size. |
|
96
|
6 |
|
$iv = Random::bytes(self::IVSIZE); |
|
97
|
|
|
|
|
98
|
|
|
// Derive key from password |
|
99
|
6 |
|
$key = self::key($password, $iv, $cost, 'rijndael-128', static::mode()); |
|
100
|
|
|
|
|
101
|
|
|
// Encrypt the plaintext |
|
102
|
6 |
|
$message = \openssl_encrypt($plaintext, static::CIPHER, $key, 1, $iv); |
|
103
|
|
|
|
|
104
|
|
|
// Create the cypher text prefix (iv + checksum) |
|
105
|
6 |
|
$prefix = $iv . self::checksum($message, $iv, $key); |
|
106
|
|
|
|
|
107
|
|
|
// Return prefix + cyphertext |
|
108
|
6 |
|
return $prefix . $message; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected static function mode() |
|
112
|
|
|
{ |
|
113
|
|
|
return substr(static::CIPHER, -3); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|