GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch php72 (880eb0)
by Joni
05:58
created

AESGCMAlgorithm::_validateIV()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWE\EncryptionAlgorithm;
6
7
use Sop\GCM\Cipher\Cipher;
8
use Sop\GCM\Exception\AuthenticationException as GCMAuthException;
9
use Sop\GCM\GCM;
10
use Sop\JWX\JWE\ContentEncryptionAlgorithm;
11
use Sop\JWX\JWE\Exception\AuthenticationException;
12
use Sop\JWX\JWT\Parameter\EncryptionAlgorithmParameter;
13
14
/**
15
 * Base class for algorithms implementing AES in Galois/Counter mode.
16
 *
17
 * @see https://tools.ietf.org/html/rfc7518#section-5.3
18
 */
19
abstract class AESGCMAlgorithm implements ContentEncryptionAlgorithm
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 15
    public function encrypt(string $plaintext, string $key, string $iv,
25
        string $aad): array
26
    {
27 15
        $this->_validateKey($key);
28 14
        $this->_validateIV($iv);
29 13
        [$ciphertext, $auth_tag] = $this->_getGCM()
30 13
            ->encrypt($plaintext, $aad, $key, $iv);
31 13
        return [$ciphertext, $auth_tag];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 5
    public function decrypt(string $ciphertext, string $key, string $iv,
38
        string $aad, string $auth_tag): string
39
    {
40 5
        $this->_validateKey($key);
41 5
        $this->_validateIV($iv);
42
        try {
43 5
            $plaintext = $this->_getGCM()
44 5
                ->decrypt($ciphertext, $auth_tag, $aad, $key, $iv);
45 1
        } catch (GCMAuthException $e) {
46 1
            throw new AuthenticationException('Message authentication failed.');
47
        }
48 4
        return $plaintext;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 19
    public function ivSize(): int
55
    {
56 19
        return 12;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 5
    public function headerParameters(): array
63
    {
64 5
        return [EncryptionAlgorithmParameter::fromAlgorithm($this)];
65
    }
66
67
    /**
68
     * Get GCM Cipher instance.
69
     *
70
     * @return Cipher
71
     */
72
    abstract protected function _getGCMCipher(): Cipher;
73
74
    /**
75
     * Get GCM instance.
76
     *
77
     * @return GCM
78
     */
79 17
    final protected function _getGCM(): GCM
80
    {
81 17
        return new GCM($this->_getGCMCipher(), 16);
82
    }
83
84
    /**
85
     * Check that key is valid.
86
     *
87
     * @param string $key
88
     *
89
     * @throws \RuntimeException
90
     */
91 19
    final protected function _validateKey(string $key): void
92
    {
93 19
        if (strlen($key) !== $this->keySize()) {
94 1
            throw new \RuntimeException('Invalid key size.');
95
        }
96 18
    }
97
98
    /**
99
     * Check that IV is valid.
100
     *
101
     * @param string $iv
102
     *
103
     * @throws \RuntimeException
104
     */
105 18
    final protected function _validateIV(string $iv): void
106
    {
107 18
        if (strlen($iv) !== $this->ivSize()) {
108 1
            throw new \RuntimeException('Invalid IV length.');
109
        }
110 17
    }
111
}
112