Completed
Push — develop ( 686594...b5844e )
by Florent
03:11
created

AESGCMKWKeyEncryptionTest::testA128GCMKW()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 1
Metric Value
c 3
b 3
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
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
 * @group Unit
21
 */
22
class AESGCMKWKeyEncryptionTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testA128GCMKW()
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 A128GCMKW();
35
36
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
37
38
        $this->assertTrue(array_key_exists('iv', $header));
39
        $this->assertTrue(array_key_exists('tag', $header));
40
        $this->assertNotNull($header['iv']);
41
        $this->assertNotNull($header['tag']);
42
        $this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header));
43
    }
44
45
    /**
46
     * @expectedException \InvalidArgumentException
47
     * @expectedExceptionMessage Wrong key type.
48
     */
49
    public function testBadKey()
50
    {
51
        $header = [];
52
        $key = new JWK([
53
            'kty' => 'EC',
54
        ]);
55
56
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
57
58
        $aeskw = new A128GCMKW();
59
60
        $aeskw->wrapKey($key, $cek, $header, $header);
61
    }
62
63
    /**
64
     * @expectedException \InvalidArgumentException
65
     * @expectedExceptionMessage Parameter "iv" is missing.
66
     */
67
    public function testMissingParameters()
68
    {
69
        $header = [];
70
        $key = new JWK([
71
            'kty' => 'oct',
72
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')),
73
        ]);
74
75
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
76
77
        $aeskw = new A128GCMKW();
78
79
        $aeskw->unwrapKey($key, $cek, $header);
80
    }
81
82
    public function testA192GCMKW()
83
    {
84
        $header = [];
85
        $key = new JWK([
86
            'kty' => 'oct',
87
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F1011121314151617')),
88
        ]);
89
90
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
91
92
        $aeskw = new A192GCMKW();
93
94
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
95
96
        $this->assertTrue(array_key_exists('iv', $header));
97
        $this->assertTrue(array_key_exists('tag', $header));
98
        $this->assertNotNull($header['iv']);
99
        $this->assertNotNull($header['tag']);
100
        $this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header));
101
    }
102
103
    public function testA256GCMKW()
104
    {
105
        $header = [];
106
        $key = new JWK([
107
            'kty' => 'oct',
108
            'k'   => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F')),
109
        ]);
110
111
        $cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F');
112
113
        $aeskw = new A256GCMKW();
114
115
        $wrapped_cek = $aeskw->wrapKey($key, $cek, $header, $header);
116
117
        $this->assertTrue(array_key_exists('iv', $header));
118
        $this->assertTrue(array_key_exists('tag', $header));
119
        $this->assertNotNull($header['iv']);
120
        $this->assertNotNull($header['tag']);
121
        $this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header));
122
    }
123
}
124