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

testA192GCMEncryptAndDecrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
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\ContentEncryption\A128GCM;
14
use Jose\Algorithm\ContentEncryption\A192GCM;
15
use Jose\Algorithm\ContentEncryption\A256GCM;
16
17
/**
18
 * Class AESGCMContentEncryptionTest.
19
 *
20
 * @group Unit
21
 */
22
class AESGCMContentEncryptionTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testA128GCMEncryptAndDecrypt()
25
    {
26
        $header = Base64Url::encode(json_encode(['alg' => 'ECDH-ES', 'enc' => 'A128GCM']));
27
        $tag = null;
28
29
        $algorithm = new A128GCM();
30
31
        $cek = random_bytes(128 / 8);
32
        $iv = random_bytes(96 / 8);
33
        $plaintext = 'Live long and Prosper.';
34
35
        $cyphertext = $algorithm->encryptContent($plaintext, $cek, $iv, null, $header, $tag);
36
37
        $this->assertNotNull($tag);
38
        $this->assertEquals($plaintext, $algorithm->decryptContent($cyphertext, $cek, $iv, null, $header, $tag));
39
    }
40
41
    public function testA192GCMEncryptAndDecrypt()
42
    {
43
        $header = Base64Url::encode(json_encode(['alg' => 'ECDH-ES', 'enc' => 'A192GCM']));
44
        $tag = null;
45
46
        $algorithm = new A192GCM();
47
48
        $cek = random_bytes(192 / 8);
49
        $iv = random_bytes(96 / 8);
50
        $plaintext = 'Live long and Prosper.';
51
52
        $cyphertext = $algorithm->encryptContent($plaintext, $cek, $iv, null, $header, $tag);
53
54
        $this->assertNotNull($tag);
55
        $this->assertEquals($plaintext, $algorithm->decryptContent($cyphertext, $cek, $iv, null, $header, $tag));
56
    }
57
58
    public function testA256GCMEncryptAndDecrypt()
59
    {
60
        $header = Base64Url::encode(json_encode(['alg' => 'ECDH-ES', 'enc' => 'A256GCM']));
61
        $tag = null;
62
63
        $algorithm = new A256GCM();
64
65
        $cek = random_bytes(256 / 8);
66
        $iv = random_bytes(96 / 8);
67
        $plaintext = 'Live long and Prosper.';
68
69
        $cyphertext = $algorithm->encryptContent($plaintext, $cek, $iv, null, $header, $tag);
70
71
        $this->assertNotNull($tag);
72
        $this->assertEquals($plaintext, $algorithm->decryptContent($cyphertext, $cek, $iv, null, $header, $tag));
73
    }
74
75
    /**
76
     * @see https://tools.ietf.org/html/rfc7516#appendix-A.1
77
     */
78
    public function testA256GCMDecryptTestVector()
79
    {
80
        $algorithm = new A256GCM();
81
82
        $header = Base64Url::encode(json_encode(['alg' => 'RSA-OAEP', 'enc' => 'A256GCM']));
83
        $cek = $this->convertArrayToBinString([177, 161, 244, 128, 84, 143, 225, 115, 63, 180, 3, 255, 107, 154, 212, 246, 138, 7, 110, 91, 112, 46, 34, 105, 47, 130, 203, 46, 122, 234, 64, 252]);
84
        $iv = $this->convertArrayToBinString([227, 197, 117, 252, 2, 219, 233, 68, 180, 225, 77, 219]);
85
        $tag = $this->convertArrayToBinString([92, 80, 104, 49, 133, 25, 161, 215, 173, 101, 219, 211, 136, 91, 210, 145]);
86
        $cyphertext = $this->convertArrayToBinString([229, 236, 166, 241, 53, 191, 115, 196, 174, 43, 73, 109, 39, 122, 233, 96, 140, 206, 120, 52, 51, 237, 48, 11, 190, 219, 186, 80, 111, 104, 50, 142, 47, 167, 59, 61, 181, 127, 196, 21, 40, 82, 242, 32, 123, 143, 168, 226, 73, 216, 176, 144, 138, 247, 106, 60, 16, 205, 160, 109, 64, 63, 192]);
87
        $expected_plaintext = 'The true sign of intelligence is not knowledge but imagination.';
88
89
        $this->assertEquals('eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ', $header);
90
        $this->assertEquals($expected_plaintext, $algorithm->decryptContent($cyphertext, $cek, $iv, null, $header, $tag));
91
    }
92
93
    /**
94
     * @param array $data
95
     *
96
     * @return string
97
     */
98
    private function convertArrayToBinString(array $data)
99
    {
100
        foreach ($data as $key => $value) {
101
            $data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
102
        }
103
104
        return hex2bin(implode('', $data));
105
    }
106
}
107