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
Push — php72 ( efc120...19e113 )
by Joni
02:36
created

AESGCMAlgorithm   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A decrypt() 0 11 2
A _validateIV() 0 4 2
A headerParameters() 0 3 1
A _validateKey() 0 4 2
A ivSize() 0 3 1
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
     * @param string $key
67
     *
68
     * @throws \RuntimeException
69
     */
70 19
    final protected function _validateKey(string $key): void
71
    {
72 19
        if (strlen($key) !== $this->keySize()) {
73 1
            throw new \RuntimeException('Invalid key size.');
74
        }
75 18
    }
76
77
    /**
78
     * Check that IV is valid.
79
     *
80
     * @param string $iv
81
     *
82
     * @throws \RuntimeException
83
     */
84 18
    final protected function _validateIV(string $iv): void
85
    {
86 18
        if (strlen($iv) !== $this->ivSize()) {
87 1
            throw new \RuntimeException('Invalid IV length.');
88
        }
89 17
    }
90
}
91