-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAESCryptoServiceProvider.php
More file actions
296 lines (254 loc) · 7.52 KB
/
AESCryptoServiceProvider.php
File metadata and controls
296 lines (254 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
namespace MayMeow\Cryptography;
use MayMeow\Cryptography\Exceptions\DecryptException;
use MayMeow\Cryptography\Exceptions\IvGenerateException;
class AESCryptoServiceProvider
{
public const CIPHER_TYPE_GCM = 'aes-256-gcm';
public const DEFAULT_GCM_TAG_LENGTH = 16;
protected string $cipher;
protected string $iv;
protected string $key;
protected string $tag = '';
public function __construct(?string $cipher = null)
{
if ($cipher == null) {
$this->cipher = static::CIPHER_TYPE_GCM;
} else {
$this->cipher = $cipher;
}
}
/**
* Set IV
*
* @param string $iv
* @return AESCryptoServiceProvider
*/
public function setIV(string $iv): AESCryptoServiceProvider
{
$this->iv = $iv;
return $this;
}
/**
* Set key needed for encryption
*
* @param string $key
* @return AESCryptoServiceProvider
*/
public function setKey(string $key): AESCryptoServiceProvider
{
$this->key = $key;
return $this;
}
/**
* Generate key
*
* @todo Change return type to string only, throw exception instead
* @return bool|string
*/
public function generateKey()
{
if (in_array($this->cipher, openssl_get_cipher_methods())) {
if ($key = openssl_random_pseudo_bytes(32)) {
$this->key = $key;
}
return $this->key;
}
return false;
}
/**
* Generate IV
*
* @todo Change return type to string only, throw exception instead
* @return bool|string
*/
public function generateIV(?string $cipher = null)
{
if ($cipher != null) {
$this->cipher = strtolower($cipher);
}
if (in_array($this->cipher, openssl_get_cipher_methods())) {
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
if ($iv = openssl_random_pseudo_bytes($ivLength)) {
$this->iv = $iv;
}
}
return $this->iv;
}
var_dump('Eroro');
return false;
}
/**
* Returns encrypted text
*
* @param string $plainText
* @param bool $legacy
* If true, returns IV-TAG-EncryptedData format
* If false, returns IV-EncryptedData-TAG format
* @return string
*/
public function encrypt(string $plainText, bool $legacy = false): string
{
$encryptedBytes = openssl_encrypt(
$plainText,
$this->cipher,
$this->key,
OPENSSL_RAW_DATA,
$this->iv,
$this->tag
);
return base64_encode($this->buildPayload(
iv: $this->iv,
tag: $this->tag,
encryptedData: $encryptedBytes,
legacy: $legacy
));
}
/**
* Build payload for encrypted data
*
* @param string $iv
* @param string $tag
* @param string $encryptedData
* @param bool $legacy
* If true, returns IV-TAG-EncryptedData format
* If false, returns IV-EncryptedData-TAG format
* @return string
*/
protected function buildPayload(string $iv, string $tag, string $encryptedData, bool $legacy): string
{
return $legacy
? $iv . $tag . $encryptedData // IV-TAG-EncryptedData
: $iv . $encryptedData . $tag; // IV-EncryptedData-TAG
}
/**
* Decrypt given text
*
* @param string $encryptedData
* @param bool $legacy
* If true, expects IV-TAG-EncryptedData format
* If false, expects IV-EncryptedData-TAG format
* @return string
* @throws DecryptException
* @throws IvGenerateException
*/
public function decrypt(string $encryptedData, bool $legacy = false): string
{
$c = base64_decode($encryptedData);
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
$iv_len = $ivLength;
} else {
throw new IvGenerateException();
}
[$this->iv, $encryptedBytes, $this->tag] = $this->parsePayload(
cipherText: $c,
ivLength: $iv_len,
legacy: $legacy
);
$decryptedText = openssl_decrypt(
$encryptedBytes,
$this->cipher,
$this->key,
OPENSSL_RAW_DATA,
$this->iv,
$this->tag
);
if ($decryptedText == false) {
throw new DecryptException();
}
return $decryptedText;
}
/**
* Parse payload from encrypted data
*
* @param string $cipherText
* @param int $ivLength
* @param bool $legacy
* If true, expects IV-TAG-EncryptedData format
* If false, expects IV-EncryptedData-TAG format
* @return array That contains IV, EncryptedData and TAG in that order
*/
protected function parsePayload(string $cipherText, int $ivLength, bool $legacy = false): array
{
$iv = substr($cipherText, 0, $ivLength);
$tagLength = static::DEFAULT_GCM_TAG_LENGTH;
return $legacy
? [$iv, substr($cipherText, $ivLength + $tagLength), substr($cipherText, $ivLength, $tagLength)]
: [$iv, substr($cipherText, $ivLength, -$tagLength), substr($cipherText, -$tagLength)];
}
/**
* Seal data using AES-256-CBC and public key
*
* Sealed data are array that contains encrypted data [1] and encrypted key [0]
* encrypted data also contains IV
*
* @param string $plain_text
* @param RSAParameters $rSAParameters
* @param bool $humanReadableData whether to return base64 encoded data
* @return array Sealed data
*/
public function seal(
string $plain_text,
RSAParameters $rSAParameters,
bool $humanReadableData = false
): array {
$this->generateIV('aes-256-cbc');
openssl_seal(
$plain_text,
$sealed_data,
$ekeys,
[$rSAParameters->getPublicKey()],
'aes-256-cbc',
$this->iv
);
$sealed_data = $this->iv . $sealed_data;
if ($humanReadableData) {
return [
base64_encode($ekeys[0]),
base64_encode($sealed_data)
];
};
return [
$ekeys[0],
$sealed_data
];
}
/**
* open function using AES-256-CBC and private key
*
* @param string $sealed_data
* @param string $ekeys
* @param RSAParameters $rSAParameters
* @return string Opened data
*/
public function open(
string $sealed_data,
string $ekeys,
RSAParameters $rSAParameters,
string $privateKeyPass,
string $salt
): string {
if (preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $sealed_data)) {
$sealed_data = base64_decode($sealed_data);
}
if (preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $ekeys)) {
$ekeys = base64_decode($ekeys);
}
if ($ivLength = openssl_cipher_iv_length('aes-256-cbc')) {
$iv_len = $ivLength;
} else {
throw new IvGenerateException();
}
$iv = substr($sealed_data, 0, $iv_len);
$encryptedData = substr($sealed_data, $iv_len);
openssl_open(
$encryptedData,
$open_data,
$ekeys,
$rSAParameters->getPrivateKey(passphrase: $privateKeyPass, salt: $salt),
'aes-256-cbc',
$iv
);
return $open_data;
}
}