1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Base64Url\Base64Url; |
13
|
|
|
use Jose\Algorithm\ContentEncryption\A128CBCHS256; |
14
|
|
|
use Jose\Algorithm\ContentEncryption\A192CBCHS384; |
15
|
|
|
use Jose\Algorithm\ContentEncryption\A256CBCHS512; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AESCBC_HSContentEncryptionTest. |
19
|
|
|
* |
20
|
|
|
* @group AESCBC |
21
|
|
|
* @group Unit |
22
|
|
|
*/ |
23
|
|
|
class AESCBC_HSContentEncryptionTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @see https://tools.ietf.org/html/rfc7516#appendix-B |
27
|
|
|
*/ |
28
|
|
|
public function testA128CBCHS256EncryptAndDecrypt() |
29
|
|
|
{ |
30
|
|
|
$header = Base64Url::encode(json_encode(['alg' => 'A128KW', 'enc' => 'A128CBC-HS256'])); |
31
|
|
|
$T = null; |
32
|
|
|
$algorithm = new A128CBCHS256(); |
33
|
|
|
|
34
|
|
|
$K = $this->convertArrayToBinString([4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207]); |
35
|
|
|
$iv = $this->convertArrayToBinString([3, 22, 60, 12, 43, 67, 104, 105, 108, 108, 105, 99, 111, 116, 104, 101]); |
36
|
|
|
$plaintext = $this->convertArrayToBinString([76, 105, 118, 101, 32, 108, 111, 110, 103, 32, 97, 110, 100, 32, 112, 114, 111, 115, 112, 101, 114, 46]); |
37
|
|
|
$expected_cyphertext = $this->convertArrayToBinString([40, 57, 83, 181, 119, 33, 133, 148, 198, 185, 243, 24, 152, 230, 6, 75, 129, 223, 127, 19, 210, 82, 183, 230, 168, 33, 215, 104, 143, 112, 56, 102]); |
38
|
|
|
$expected_T = $this->convertArrayToBinString([83, 73, 191, 98, 104, 205, 211, 128, 201, 189, 199, 133, 32, 38, 194, 85]); |
39
|
|
|
|
40
|
|
|
$cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, null, $header, $T); |
41
|
|
|
|
42
|
|
|
$this->assertEquals($expected_cyphertext, $cyphertext); |
43
|
|
|
$this->assertEquals($plaintext, $algorithm->decryptContent($cyphertext, $K, $iv, null, $header, $T)); |
44
|
|
|
$this->assertEquals($expected_T, $T); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @expectedException \InvalidArgumentException |
49
|
|
|
* @expectedExceptionMessage Unable to verify the tag. |
50
|
|
|
*/ |
51
|
|
|
public function testBadTag() |
52
|
|
|
{ |
53
|
|
|
$header = Base64Url::encode(json_encode(['alg' => 'A128KW', 'enc' => 'A128CBC-HS256'])); |
54
|
|
|
$algorithm = new A128CBCHS256(); |
55
|
|
|
|
56
|
|
|
$K = $this->convertArrayToBinString([4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207]); |
57
|
|
|
$iv = $this->convertArrayToBinString([3, 22, 60, 12, 43, 67, 104, 105, 108, 108, 105, 99, 111, 116, 104, 101]); |
58
|
|
|
$cyphertext = $this->convertArrayToBinString([40, 57, 83, 181, 119, 33, 133, 148, 198, 185, 243, 24, 152, 230, 6, 75, 129, 223, 127, 19, 210, 82, 183, 230, 168, 33, 215, 104, 143, 112, 56, 102]); |
59
|
|
|
$T = $this->convertArrayToBinString([83, 73, 191, 98, 104, 205, 211, 128, 201, 189, 199, 133, 32, 38, 194]); |
60
|
|
|
|
61
|
|
|
$algorithm->decryptContent($cyphertext, $K, $iv, null, $header, $T); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $data |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
private function convertArrayToBinString(array $data) |
70
|
|
|
{ |
71
|
|
|
foreach ($data as $key => $value) { |
72
|
|
|
$data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return hex2bin(implode('', $data)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @see https://tools.ietf.org/html/rfc7518#appendix-B.1 |
80
|
|
|
*/ |
81
|
|
|
public function testA128CBCHS256EncryptAndDecrypt_Bis() |
82
|
|
|
{ |
83
|
|
|
$header = Base64Url::encode(json_encode(['alg' => 'A128KW', 'enc' => 'A128CBC-HS256'])); |
84
|
|
|
$T = null; |
85
|
|
|
$algorithm = new A128CBCHS256(); |
86
|
|
|
|
87
|
|
|
$K = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'); |
88
|
|
|
$iv = hex2bin('1af38c2dc2b96ffdd86694092341bc04'); |
89
|
|
|
$plaintext = hex2bin('41206369706865722073797374656d206d757374206e6f7420626520726571756972656420746f206265207365637265742c20616e64206974206d7573742062652061626c6520746f2066616c6c20696e746f207468652068616e6473206f662074686520656e656d7920776974686f757420696e636f6e76656e69656e6365'); |
90
|
|
|
$expected_cyphertext = hex2bin('c80edfa32ddf39d5ef00c0b468834279a2e46a1b8049f792f76bfe54b903a9c9a94ac9b47ad2655c5f10f9aef71427e2fc6f9b3f399a221489f16362c703233609d45ac69864e3321cf82935ac4096c86e133314c54019e8ca7980dfa4b9cf1b384c486f3a54c51078158ee5d79de59fbd34d848b3d69550a67646344427ade54b8851ffb598f7f80074b9473c82e2db'); |
91
|
|
|
$expected_T = hex2bin('652c3fa36b0a7c5b3219fab3a30bc1c4'); |
92
|
|
|
$aad = hex2bin('546865207365636f6e64207072696e6369706c65206f662041756775737465204b6572636b686f666673'); |
93
|
|
|
|
94
|
|
|
$cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, $aad, $header, $T); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($expected_cyphertext, $cyphertext); |
97
|
|
|
|
98
|
|
|
//We invoke protected methods to test vectors directly. This is due to the encryption signature: this test case uses a string as AAD, but the algorithm uses the protected header. |
99
|
|
|
$calc_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'calculateAuthenticationTag'); |
100
|
|
|
$check_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'isTagValid'); |
101
|
|
|
|
102
|
|
|
$T = $calc_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad]); |
103
|
|
|
$this->assertEquals($expected_T, $T); |
104
|
|
|
$this->assertTrue($check_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad, $T])); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @see https://tools.ietf.org/html/rfc7518#appendix-B.2 |
109
|
|
|
*/ |
110
|
|
|
public function testA192CBC_HS384EncryptAndDecrypt() |
111
|
|
|
{ |
112
|
|
|
$header = Base64Url::encode(json_encode([])); |
113
|
|
|
$algorithm = new A192CBCHS384(); |
114
|
|
|
|
115
|
|
|
$K = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f'); |
116
|
|
|
$iv = hex2bin('1af38c2dc2b96ffdd86694092341bc04'); |
117
|
|
|
$plaintext = hex2bin('41206369706865722073797374656d206d757374206e6f7420626520726571756972656420746f206265207365637265742c20616e64206974206d7573742062652061626c6520746f2066616c6c20696e746f207468652068616e6473206f662074686520656e656d7920776974686f757420696e636f6e76656e69656e6365'); |
118
|
|
|
$expected_cyphertext = hex2bin('ea65da6b59e61edb419be62d19712ae5d303eeb50052d0dfd6697f77224c8edb000d279bdc14c1072654bd30944230c657bed4ca0c9f4a8466f22b226d1746214bf8cfc2400add9f5126e479663fc90b3bed787a2f0ffcbf3904be2a641d5c2105bfe591bae23b1d7449e532eef60a9ac8bb6c6b01d35d49787bcd57ef484927f280adc91ac0c4e79c7b11efc60054e3'); |
119
|
|
|
$expected_T = hex2bin('8490ac0e58949bfe51875d733f93ac2075168039ccc733d7'); |
120
|
|
|
$aad = hex2bin('546865207365636f6e64207072696e6369706c65206f662041756775737465204b6572636b686f666673'); |
121
|
|
|
|
122
|
|
|
$cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, $aad, $header, $T); |
123
|
|
|
|
124
|
|
|
$this->assertEquals($expected_cyphertext, $cyphertext); |
125
|
|
|
|
126
|
|
|
if (defined('HHVM_VERSION')) { |
127
|
|
|
$this->markTestSkipped('The remaining tests does not work on HHVM. The error raised does not concern the library itself.'); |
128
|
|
|
|
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
//We invoke protected methods to test vectors directly. This is due to the encryption signature: this test case uses a string as AAD, but the algorithm uses the protected header. |
133
|
|
|
$calc_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'calculateAuthenticationTag'); |
134
|
|
|
$check_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'isTagValid'); |
135
|
|
|
|
136
|
|
|
$T = $calc_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad]); |
137
|
|
|
$this->assertEquals($expected_T, $T); |
138
|
|
|
$this->assertTrue($check_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad, $T])); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @see https://tools.ietf.org/html/rfc7518#appendix-B.3 |
143
|
|
|
*/ |
144
|
|
|
public function testA256CBC_HS512EncryptAndDecrypt() |
145
|
|
|
{ |
146
|
|
|
$header = Base64Url::encode(json_encode([])); |
147
|
|
|
$algorithm = new A256CBCHS512(); |
148
|
|
|
|
149
|
|
|
$K = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f'); |
150
|
|
|
$iv = hex2bin('1af38c2dc2b96ffdd86694092341bc04'); |
151
|
|
|
$plaintext = hex2bin('41206369706865722073797374656d206d757374206e6f7420626520726571756972656420746f206265207365637265742c20616e64206974206d7573742062652061626c6520746f2066616c6c20696e746f207468652068616e6473206f662074686520656e656d7920776974686f757420696e636f6e76656e69656e6365'); |
152
|
|
|
$expected_cyphertext = hex2bin('4affaaadb78c31c5da4b1b590d10ffbd3dd8d5d302423526912da037ecbcc7bd822c301dd67c373bccb584ad3e9279c2e6d12a1374b77f077553df829410446b36ebd97066296ae6427ea75c2e0846a11a09ccf5370dc80bfecbad28c73f09b3a3b75e662a2594410ae496b2e2e6609e31e6e02cc837f053d21f37ff4f51950bbe2638d09dd7a4930930806d0703b1f6'); |
153
|
|
|
$expected_T = hex2bin('4dd3b4c088a7f45c216839645b2012bf2e6269a8c56a816dbc1b267761955bc5'); |
154
|
|
|
$aad = hex2bin('546865207365636f6e64207072696e6369706c65206f662041756775737465204b6572636b686f666673'); |
155
|
|
|
|
156
|
|
|
$cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, $aad, $header, $T); |
157
|
|
|
|
158
|
|
|
$this->assertEquals($expected_cyphertext, $cyphertext); |
159
|
|
|
|
160
|
|
|
if (defined('HHVM_VERSION')) { |
161
|
|
|
$this->markTestSkipped('The remaining tests does not work on HHVM. The error raised does not concern the library itself.'); |
162
|
|
|
|
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
//We invoke protected methods to test vectors directly. This is due to the encryption signature: this test case uses a string as AAD, but the algorithm uses the protected header. |
167
|
|
|
$calc_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'calculateAuthenticationTag'); |
168
|
|
|
$check_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'isTagValid'); |
169
|
|
|
|
170
|
|
|
$T = $calc_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad]); |
171
|
|
|
$this->assertEquals($expected_T, $T); |
172
|
|
|
$this->assertTrue($check_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad, $T])); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $class |
177
|
|
|
* @param string $name |
178
|
|
|
* |
179
|
|
|
* @return \ReflectionMethod |
180
|
|
|
*/ |
181
|
|
|
protected static function getMethod($class, $name) |
182
|
|
|
{ |
183
|
|
|
$class = new \ReflectionClass($class); |
184
|
|
|
$method = $class->getMethod($name); |
185
|
|
|
$method->setAccessible(true); |
186
|
|
|
|
187
|
|
|
return $method; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|