-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathEncrypterTest.php
executable file
·272 lines (221 loc) · 9.81 KB
/
EncrypterTest.php
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
<?php
namespace Illuminate\Tests\Encryption;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Encryption\Encrypter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class EncrypterTest extends TestCase
{
public function testEncryption(): void
{
$e = new Encrypter(str_repeat('a', 16));
$encrypted = $e->encrypt('foo');
$this->assertNotSame('foo', $encrypted);
$this->assertSame('foo', $e->decrypt($encrypted));
$encrypted = $e->encrypt('');
$this->assertSame('', $e->decrypt($encrypted));
$longString = str_repeat('a', 1000);
$encrypted = $e->encrypt($longString);
$this->assertSame($longString, $e->decrypt($encrypted));
$data = ['foo' => 'bar', 'baz' => 'qux'];
$encryptedArray = $e->encrypt($data);
$this->assertNotSame($data, $encryptedArray);
$this->assertSame($data, $e->decrypt($encryptedArray));
}
public function testRawStringEncryption()
{
$e = new Encrypter(str_repeat('a', 16));
$encrypted = $e->encryptString('foo');
$this->assertNotSame('foo', $encrypted);
$this->assertSame('foo', $e->decryptString($encrypted));
}
public function testRawStringEncryptionWithPreviousKeys()
{
$previous = new Encrypter(str_repeat('b', 16));
$previousValue = $previous->encryptString('foo');
$new = new Encrypter(str_repeat('a', 16));
$new->previousKeys([str_repeat('b', 16)]);
$decrypted = $new->decryptString($previousValue);
$this->assertSame('foo', $decrypted);
}
public function testItValidatesMacOnPerKeyBasis()
{
// Payload created with (key: str_repeat('b', 16)) but will
// "successfully" decrypt with (key: str_repeat('a', 16)), however it
// outputs a random binary string as it is not the correct key.
$encrypted = 'eyJpdiI6Ilg0dFM5TVRibEFqZW54c3lQdWJoVVE9PSIsInZhbHVlIjoiRGJpa2p2ZHI3eUs0dUtRakJneUhUUT09IiwibWFjIjoiMjBjZWYxODdhNThhOTk4MTk1NTc0YTE1MDgzODU1OWE0ZmQ4MDc5ZjMxYThkOGM1ZmM1MzlmYzBkYTBjMWI1ZiIsInRhZyI6IiJ9';
$new = new Encrypter(str_repeat('a', 16));
$new->previousKeys([str_repeat('b', 16)]);
$this->assertSame('foo', $new->decryptString($encrypted));
}
public function testEncryptionUsingBase64EncodedKey()
{
$e = new Encrypter(random_bytes(16));
$encrypted = $e->encrypt('foo');
$this->assertNotSame('foo', $encrypted);
$this->assertSame('foo', $e->decrypt($encrypted));
}
public function testEncryptedLengthIsFixed()
{
$e = new Encrypter(str_repeat('a', 16));
$lengths = [];
for ($i = 0; $i < 100; $i++) {
$lengths[] = strlen($e->encrypt('foo'));
}
$this->assertSame(min($lengths), max($lengths));
}
public function testWithCustomCipher()
{
$e = new Encrypter(str_repeat('b', 32), 'AES-256-GCM');
$encrypted = $e->encrypt('bar');
$this->assertNotSame('bar', $encrypted);
$this->assertSame('bar', $e->decrypt($encrypted));
$e = new Encrypter(random_bytes(32), 'AES-256-GCM');
$encrypted = $e->encrypt('foo');
$this->assertNotSame('foo', $encrypted);
$this->assertSame('foo', $e->decrypt($encrypted));
}
public function testCipherNamesCanBeMixedCase()
{
$upper = new Encrypter(str_repeat('b', 16), 'AES-128-GCM');
$encrypted = $upper->encrypt('bar');
$this->assertNotSame('bar', $encrypted);
$lower = new Encrypter(str_repeat('b', 16), 'aes-128-gcm');
$this->assertSame('bar', $lower->decrypt($encrypted));
$mixed = new Encrypter(str_repeat('b', 16), 'aEs-128-GcM');
$this->assertSame('bar', $mixed->decrypt($encrypted));
}
public function testThatAnAeadCipherIncludesTag()
{
$e = new Encrypter(str_repeat('b', 32), 'AES-256-GCM');
$encrypted = $e->encrypt('foo');
$data = json_decode(base64_decode($encrypted));
$this->assertEmpty($data->mac);
$this->assertNotEmpty($data->tag);
}
public function testThatAnAeadTagMustBeProvidedInFullLength()
{
$e = new Encrypter(str_repeat('b', 32), 'AES-256-GCM');
$encrypted = $e->encrypt('foo');
$data = json_decode(base64_decode($encrypted));
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('Could not decrypt the data.');
$data->tag = substr($data->tag, 0, 4);
$encrypted = base64_encode(json_encode($data));
$e->decrypt($encrypted);
}
public function testThatAnAeadTagCantBeModified()
{
$e = new Encrypter(str_repeat('b', 32), 'AES-256-GCM');
$encrypted = $e->encrypt('foo');
$data = json_decode(base64_decode($encrypted));
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('Could not decrypt the data.');
$data->tag[0] = $data->tag[0] === 'A' ? 'B' : 'A';
$encrypted = base64_encode(json_encode($data));
$e->decrypt($encrypted);
}
public function testThatANonAeadCipherIncludesMac()
{
$e = new Encrypter(str_repeat('b', 32), 'AES-256-CBC');
$encrypted = $e->encrypt('foo');
$data = json_decode(base64_decode($encrypted));
$this->assertEmpty($data->tag);
$this->assertNotEmpty($data->mac);
}
public function testDoNoAllowLongerKey()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.');
new Encrypter(str_repeat('z', 32));
}
public function testWithBadKeyLength()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.');
new Encrypter(str_repeat('a', 5));
}
public function testWithBadKeyLengthAlternativeCipher()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.');
new Encrypter(str_repeat('a', 16), 'AES-256-GCM');
}
public function testWithUnsupportedCipher()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.');
new Encrypter(str_repeat('c', 16), 'AES-256-CFB8');
}
public function testExceptionThrownWhenPayloadIsInvalid()
{
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('The payload is invalid.');
$e = new Encrypter(str_repeat('a', 16));
$payload = $e->encrypt('foo');
$payload = str_shuffle($payload);
$e->decrypt($payload);
}
public function testDecryptionExceptionIsThrownWhenUnexpectedTagIsAdded()
{
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('Unable to use tag because the cipher algorithm does not support AEAD.');
$e = new Encrypter(str_repeat('a', 16));
$payload = $e->encrypt('foo');
$decodedPayload = json_decode(base64_decode($payload));
$decodedPayload->tag = 'set-manually';
$e->decrypt(base64_encode(json_encode($decodedPayload)));
}
public function testExceptionThrownWithDifferentKey()
{
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('The MAC is invalid.');
$a = new Encrypter(str_repeat('a', 16));
$b = new Encrypter(str_repeat('b', 16));
$b->decrypt($a->encrypt('baz'));
}
public function testExceptionThrownWhenIvIsTooLong()
{
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('The payload is invalid.');
$e = new Encrypter(str_repeat('a', 16));
$payload = $e->encrypt('foo');
$data = json_decode(base64_decode($payload), true);
$data['iv'] .= $data['value'][0];
$data['value'] = substr($data['value'], 1);
$modified_payload = base64_encode(json_encode($data));
$e->decrypt($modified_payload);
}
public function testSupportedMethodAcceptsAnyCasing()
{
$key = str_repeat('a', 16);
$this->assertTrue(Encrypter::supported($key, 'AES-128-GCM'));
$this->assertTrue(Encrypter::supported($key, 'aes-128-CBC'));
$this->assertTrue(Encrypter::supported($key, 'aes-128-cbc'));
}
public static function provideTamperedData()
{
$validIv = base64_encode(str_repeat('.', 16));
return [
[['iv' => ['value_in_array'], 'value' => '', 'mac' => '']],
[['iv' => new class() {
}, 'value' => '', 'mac' => '']],
[['iv' => $validIv, 'value' => ['value_in_array'], 'mac' => '']],
[['iv' => $validIv, 'value' => new class() {
}, 'mac' => '']],
[['iv' => $validIv, 'value' => '', 'mac' => ['value_in_array']]],
[['iv' => $validIv, 'value' => '', 'mac' => null]],
[['iv' => $validIv, 'value' => '', 'mac' => '', 'tag' => ['value_in_array']]],
[['iv' => $validIv, 'value' => '', 'mac' => '', 'tag' => -1]],
];
}
#[DataProvider('provideTamperedData')]
public function testTamperedPayloadWillGetRejected($payload)
{
$this->expectException(DecryptException::class);
$this->expectExceptionMessage('The payload is invalid.');
$enc = new Encrypter(str_repeat('x', 16));
$enc->decrypt(base64_encode(json_encode($payload)));
}
}