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   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 17
dl 0
loc 66
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A headerParameters() 0 3 1
A ivSize() 0 3 1
A decrypt() 0 11 2
A _validateIV() 0 4 2
A _validateKey() 0 4 2
A encrypt() 0 6 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