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.

AESGCMAlgorithm::ivSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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