Completed
Push — develop ( b5844e...e46df6 )
by Florent
02:33
created

AESKWKeyEncryptionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 2 Features 1
Metric Value
wmc 5
c 2
b 2
f 1
lcom 1
cbo 6
dl 0
loc 95
rs 10
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\A128KW;
14
use Jose\Algorithm\KeyEncryption\A192KW;
15
use Jose\Algorithm\KeyEncryption\A256KW;
16
use Jose\Object\JWK;
17
18
/**
19
 * @group AESKW
20
 * @group Unit
21
 */
22
class AESKWKeyEncryptionTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testA128KW()
25
    {
26
        $header = [];
27
        $key = new JWK([
28
            'kty' => 'oct',
29
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')),
30
        ]);
31
32
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
33
34
        $aeskw = new A128KW();
35
36
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
37
38
        $this->assertEquals($wrapped_cek, hex2bin('11826840774D993FF9C2FA02CCA3CEA0E93B1E1CF96361F93EA6DC2F345194E7B30F964C79F9E61D'));
39
        $this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header));
40
    }
41
42
    /**
43
     * @expectedException \InvalidArgumentException
44
     * @expectedExceptionMessage  Wrong key type
45
     */
46
    public function testBadKey()
47
    {
48
        $header = [];
49
        $key = new JWK([
50
            'kty' => 'EC',
51
        ]);
52
53
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
54
55
        $aeskw = new A128KW();
56
57
        $aeskw->wrapKey($key, $cek, $header, $header);
58
    }
59
60
    public function testA192KW()
61
    {
62
        $header = [];
63
        $key = new JWK([
64
            'kty' => 'oct',
65
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F1011121314151617')),
66
        ]);
67
68
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
69
70
        $aeskw = new A192KW();
71
72
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
73
74
        $this->assertEquals($wrapped_cek, hex2bin('08861E000AABFA4479C7191F9DC51CCA37C50F16CC14441C6EA4980CFCE0F41D9285758C6F74AC6D'));
75
        $this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header));
76
    }
77
78
    public function testA256KW()
79
    {
80
        $header = [];
81
        $key = new JWK([
82
            'kty' => 'oct',
83
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F')),
84
        ]);
85
86
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
87
88
        $aeskw = new A256KW();
89
90
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
91
92
        $this->assertEquals($wrapped_cek, hex2bin('28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21'));
93
        $this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header));
94
    }
95
96
    /**
97
     * @expectedException \InvalidArgumentException
98
     * @expectedExceptionMessage The key size is not valid
99
     */
100
    public function testBadKeySize()
101
    {
102
        $header = [];
103
        $key = new JWK([
104
            'kty' => 'oct',
105
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')),
106
        ]);
107
108
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
109
110
        $aeskw = new A256KW();
111
112
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
113
114
        $aeskw->unwrapKey($key, $wrapped_cek, $header);
115
    }
116
}
117