|
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\KeyEncryption\A128GCMKW; |
|
14
|
|
|
use Jose\Algorithm\KeyEncryption\A192GCMKW; |
|
15
|
|
|
use Jose\Algorithm\KeyEncryption\A256GCMKW; |
|
16
|
|
|
use Jose\Object\JWK; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @group AESGCMKW |
|
20
|
|
|
*/ |
|
21
|
|
|
class AESGCMKWKeyEncryptionTest extends \PHPUnit_Framework_TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testA128GCMKW() |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$this->isCryptooExtensionInstalled()) { |
|
29
|
|
|
$this->markTestSkipped('Crypto extension not available'); |
|
30
|
|
|
|
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
$header = []; |
|
34
|
|
|
$key = new JWK([ |
|
35
|
|
|
'kty' => 'oct', |
|
36
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')), |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
40
|
|
|
|
|
41
|
|
|
$aeskw = new A128GCMKW(); |
|
42
|
|
|
|
|
43
|
|
|
$wrapped_cek = $aeskw->encryptKey($key, $cek, $header); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertTrue(array_key_exists('iv', $header)); |
|
46
|
|
|
$this->assertTrue(array_key_exists('tag', $header)); |
|
47
|
|
|
$this->assertNotNull($header['iv']); |
|
48
|
|
|
$this->assertNotNull($header['tag']); |
|
49
|
|
|
$this->assertEquals($cek, $aeskw->decryptKey($key, $wrapped_cek, $header)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @expectedException \InvalidArgumentException |
|
54
|
|
|
* @expectedExceptionMessage The key is not valid |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testBadKey() |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->isCryptooExtensionInstalled()) { |
|
59
|
|
|
$this->markTestSkipped('Crypto extension not available'); |
|
60
|
|
|
|
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
$header = []; |
|
64
|
|
|
$key = new JWK([ |
|
65
|
|
|
'kty' => 'EC', |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
69
|
|
|
|
|
70
|
|
|
$aeskw = new A128GCMKW(); |
|
71
|
|
|
|
|
72
|
|
|
$aeskw->encryptKey($key, $cek, $header); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @expectedException \InvalidArgumentException |
|
77
|
|
|
* @expectedExceptionMessage Missing parameters 'iv' or 'tag'. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testMissingParameters() |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$this->isCryptooExtensionInstalled()) { |
|
82
|
|
|
$this->markTestSkipped('Crypto extension not available'); |
|
83
|
|
|
|
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
$header = []; |
|
87
|
|
|
$key = new JWK([ |
|
88
|
|
|
'kty' => 'oct', |
|
89
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')), |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
93
|
|
|
|
|
94
|
|
|
$aeskw = new A128GCMKW(); |
|
95
|
|
|
|
|
96
|
|
|
$aeskw->decryptKey($key, $cek, $header); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testA192GCMKW() |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$this->isCryptooExtensionInstalled()) { |
|
105
|
|
|
$this->markTestSkipped('Crypto extension not available'); |
|
106
|
|
|
|
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
$header = []; |
|
110
|
|
|
$key = new JWK([ |
|
111
|
|
|
'kty' => 'oct', |
|
112
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F1011121314151617')), |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
116
|
|
|
|
|
117
|
|
|
$aeskw = new A192GCMKW(); |
|
118
|
|
|
|
|
119
|
|
|
$wrapped_cek = $aeskw->encryptKey($key, $cek, $header); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertTrue(array_key_exists('iv', $header)); |
|
122
|
|
|
$this->assertTrue(array_key_exists('tag', $header)); |
|
123
|
|
|
$this->assertNotNull($header['iv']); |
|
124
|
|
|
$this->assertNotNull($header['tag']); |
|
125
|
|
|
$this->assertEquals($cek, $aeskw->decryptKey($key, $wrapped_cek, $header)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testA256GCMKW() |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->isCryptooExtensionInstalled()) { |
|
134
|
|
|
$this->markTestSkipped('Crypto extension not available'); |
|
135
|
|
|
|
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
$header = []; |
|
139
|
|
|
$key = new JWK([ |
|
140
|
|
|
'kty' => 'oct', |
|
141
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F')), |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
145
|
|
|
|
|
146
|
|
|
$aeskw = new A256GCMKW(); |
|
147
|
|
|
|
|
148
|
|
|
$wrapped_cek = $aeskw->encryptKey($key, $cek, $header); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertTrue(array_key_exists('iv', $header)); |
|
151
|
|
|
$this->assertTrue(array_key_exists('tag', $header)); |
|
152
|
|
|
$this->assertNotNull($header['iv']); |
|
153
|
|
|
$this->assertNotNull($header['tag']); |
|
154
|
|
|
$this->assertEquals($cek, $aeskw->decryptKey($key, $wrapped_cek, $header)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
private function isCryptooExtensionInstalled() |
|
158
|
|
|
{ |
|
159
|
|
|
return class_exists('\Crypto\Cipher'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|