Completed
Push — v2.0.x ( 11273c...f488cd )
by Florent
04:53 queued 01:28
created

AESCBC_HSContentEncryptionTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 165
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\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
 */
22
class AESCBC_HSContentEncryptionTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @see https://tools.ietf.org/html/rfc7516#appendix-B
26
     */
27
    public function testA128CBCHS256EncryptAndDecrypt()
28
    {
29
        $header = Base64Url::encode(json_encode(['alg' => 'A128KW', 'enc' => 'A128CBC-HS256']));
30
        $T = null;
31
        $algorithm = new A128CBCHS256();
32
33
        $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]);
34
        $iv = $this->convertArrayToBinString([3, 22, 60, 12, 43, 67, 104, 105, 108, 108, 105, 99, 111, 116, 104, 101]);
35
        $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]);
36
        $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]);
37
        $expected_T = $this->convertArrayToBinString([83, 73, 191, 98, 104, 205, 211, 128, 201, 189, 199, 133, 32, 38, 194, 85]);
38
39
        $cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, null, $header, $T);
40
41
        $this->assertEquals($expected_cyphertext, $cyphertext);
42
        $this->assertEquals($plaintext, $algorithm->decryptContent($cyphertext, $K, $iv, null, $header, $T));
43
        $this->assertEquals($expected_T, $T);
44
    }
45
46
    /**
47
     */
48
    public function testBadTag()
49
    {
50
        $header = Base64Url::encode(json_encode(['alg' => 'A128KW', 'enc' => 'A128CBC-HS256']));
51
        $algorithm = new A128CBCHS256();
52
53
        $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]);
54
        $iv = $this->convertArrayToBinString([3, 22, 60, 12, 43, 67, 104, 105, 108, 108, 105, 99, 111, 116, 104, 101]);
55
        $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]);
56
        $T = $this->convertArrayToBinString([83, 73, 191, 98, 104, 205, 211, 128, 201, 189, 199, 133, 32, 38, 194]);
57
58
        $this->assertNull($algorithm->decryptContent($cyphertext, $K, $iv, null, $header, $T));
59
    }
60
61
    /**
62
     * @param array $data
63
     *
64
     * @return string
65
     */
66
    private function convertArrayToBinString(array $data)
67
    {
68
        foreach ($data as $key => $value) {
69
            $data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
70
        }
71
72
        return hex2bin(implode('', $data));
73
    }
74
75
    /**
76
     * @see https://tools.ietf.org/html/rfc7518#appendix-B.1
77
     */
78
    public function testA128CBCHS256EncryptAndDecrypt_Bis()
79
    {
80
        $header = Base64Url::encode(json_encode(['alg' => 'A128KW', 'enc' => 'A128CBC-HS256']));
81
        $T = null;
82
        $algorithm = new A128CBCHS256();
83
84
        $K = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f');
85
        $iv = hex2bin('1af38c2dc2b96ffdd86694092341bc04');
86
        $plaintext = hex2bin('41206369706865722073797374656d206d757374206e6f7420626520726571756972656420746f206265207365637265742c20616e64206974206d7573742062652061626c6520746f2066616c6c20696e746f207468652068616e6473206f662074686520656e656d7920776974686f757420696e636f6e76656e69656e6365');
87
        $expected_cyphertext = hex2bin('c80edfa32ddf39d5ef00c0b468834279a2e46a1b8049f792f76bfe54b903a9c9a94ac9b47ad2655c5f10f9aef71427e2fc6f9b3f399a221489f16362c703233609d45ac69864e3321cf82935ac4096c86e133314c54019e8ca7980dfa4b9cf1b384c486f3a54c51078158ee5d79de59fbd34d848b3d69550a67646344427ade54b8851ffb598f7f80074b9473c82e2db');
88
        $expected_T = hex2bin('652c3fa36b0a7c5b3219fab3a30bc1c4');
89
        $aad = hex2bin('546865207365636f6e64207072696e6369706c65206f662041756775737465204b6572636b686f666673');
90
91
        $cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, $aad, $header, $T);
92
93
        $this->assertEquals($expected_cyphertext, $cyphertext);
94
95
        //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.
96
        $calc_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'calculateAuthenticationTag');
97
        $check_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'checkAuthenticationTag');
98
99
        $T = $calc_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad]);
100
        $this->assertEquals($expected_T, $T);
101
        $this->assertTrue($check_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad, $T]));
102
    }
103
104
    /**
105
     * @see https://tools.ietf.org/html/rfc7518#appendix-B.2
106
     */
107
    public function testA192CBC_HS384EncryptAndDecrypt()
108
    {
109
        $header = Base64Url::encode(json_encode([]));
110
        $algorithm = new A192CBCHS384();
111
112
        $K = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f');
113
        $iv = hex2bin('1af38c2dc2b96ffdd86694092341bc04');
114
        $plaintext = hex2bin('41206369706865722073797374656d206d757374206e6f7420626520726571756972656420746f206265207365637265742c20616e64206974206d7573742062652061626c6520746f2066616c6c20696e746f207468652068616e6473206f662074686520656e656d7920776974686f757420696e636f6e76656e69656e6365');
115
        $expected_cyphertext = hex2bin('ea65da6b59e61edb419be62d19712ae5d303eeb50052d0dfd6697f77224c8edb000d279bdc14c1072654bd30944230c657bed4ca0c9f4a8466f22b226d1746214bf8cfc2400add9f5126e479663fc90b3bed787a2f0ffcbf3904be2a641d5c2105bfe591bae23b1d7449e532eef60a9ac8bb6c6b01d35d49787bcd57ef484927f280adc91ac0c4e79c7b11efc60054e3');
116
        $expected_T = hex2bin('8490ac0e58949bfe51875d733f93ac2075168039ccc733d7');
117
        $aad = hex2bin('546865207365636f6e64207072696e6369706c65206f662041756775737465204b6572636b686f666673');
118
119
        $cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, $aad, $header, $T);
120
121
        $this->assertEquals($expected_cyphertext, $cyphertext);
122
123
        if (defined('HHVM_VERSION')) {
124
            $this->markTestSkipped('The remaining tests does not work on HHVM. The error raised does not concern the library itself.');
125
126
            return;
127
        }
128
129
        //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.
130
        $calc_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'calculateAuthenticationTag');
131
        $check_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'checkAuthenticationTag');
132
133
        $T = $calc_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad]);
134
        $this->assertEquals($expected_T, $T);
135
        $this->assertTrue($check_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad, $T]));
136
    }
137
138
    /**
139
     * @see https://tools.ietf.org/html/rfc7518#appendix-B.3
140
     */
141
    public function testA256CBC_HS512EncryptAndDecrypt()
142
    {
143
        $header = Base64Url::encode(json_encode([]));
144
        $algorithm = new A256CBCHS512();
145
146
        $K = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f');
147
        $iv = hex2bin('1af38c2dc2b96ffdd86694092341bc04');
148
        $plaintext = hex2bin('41206369706865722073797374656d206d757374206e6f7420626520726571756972656420746f206265207365637265742c20616e64206974206d7573742062652061626c6520746f2066616c6c20696e746f207468652068616e6473206f662074686520656e656d7920776974686f757420696e636f6e76656e69656e6365');
149
        $expected_cyphertext = hex2bin('4affaaadb78c31c5da4b1b590d10ffbd3dd8d5d302423526912da037ecbcc7bd822c301dd67c373bccb584ad3e9279c2e6d12a1374b77f077553df829410446b36ebd97066296ae6427ea75c2e0846a11a09ccf5370dc80bfecbad28c73f09b3a3b75e662a2594410ae496b2e2e6609e31e6e02cc837f053d21f37ff4f51950bbe2638d09dd7a4930930806d0703b1f6');
150
        $expected_T = hex2bin('4dd3b4c088a7f45c216839645b2012bf2e6269a8c56a816dbc1b267761955bc5');
151
        $aad = hex2bin('546865207365636f6e64207072696e6369706c65206f662041756775737465204b6572636b686f666673');
152
153
        $cyphertext = $algorithm->encryptContent($plaintext, $K, $iv, $aad, $header, $T);
154
155
        $this->assertEquals($expected_cyphertext, $cyphertext);
156
157
        if (defined('HHVM_VERSION')) {
158
            $this->markTestSkipped('The remaining tests does not work on HHVM. The error raised does not concern the library itself.');
159
160
            return;
161
        }
162
163
        //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.
164
        $calc_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'calculateAuthenticationTag');
165
        $check_method = self::getMethod('\Jose\Algorithm\ContentEncryption\A128CBCHS256', 'checkAuthenticationTag');
166
167
        $T = $calc_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad]);
168
        $this->assertEquals($expected_T, $T);
169
        $this->assertTrue($check_method->invokeArgs($algorithm, [$cyphertext, $K, $iv, null, $aad, $T]));
170
    }
171
172
    /**
173
     * @param string $class
174
     * @param string $name
175
     *
176
     * @return \ReflectionMethod
177
     */
178
    protected static function getMethod($class, $name)
179
    {
180
        $class = new \ReflectionClass($class);
181
        $method = $class->getMethod($name);
182
        $method->setAccessible(true);
183
184
        return $method;
185
    }
186
}
187