Failed Conditions
Push — v7 ( 64e5e5...d305ca )
by Florent
01:51
created

AESGCM   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A encryptContent() 0 15 2
A decryptContent() 0 16 2
A getIVSize() 0 4 1
A getCEKSize() 0 4 1
A checkKeyLength() 0 6 2
getKeySize() 0 1 ?
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Encryption\Algorithm\ContentEncryption;
15
16
use Assert\Assertion;
17
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithmInterface;
18
19
abstract class AESGCM implements ContentEncryptionAlgorithmInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function encryptContent(string $data, string $cek, string $iv, ?string $aad, string $encoded_protected_header, ?string &$tag): string
25
    {
26
        $calculated_aad = $encoded_protected_header;
27
        if (null !== $aad) {
28
            $calculated_aad .= '.'.$aad;
29
        }
30
31
        $keyLength = mb_strlen($cek, '8bit') * 8;
32
        $this->checkKeyLength($keyLength);
33
        $mode = sprintf('aes-%d-gcm', $keyLength);
34
        $C = openssl_encrypt($data, $mode, $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
35
        Assertion::true(false !== $C, 'Unable to encrypt the data.');
36
37
        return $C;
38
    }
39
40
    /**
41
     *  {@inheritdoc}
42
     */
43
    public function decryptContent(string $data, string $cek, string $iv, ?string $aad, string $encoded_protected_header, string $tag): string
44
    {
45
        $calculated_aad = $encoded_protected_header;
46
        if (null !== $aad) {
47
            $calculated_aad .= '.'.$aad;
48
        }
49
50
        $keyLength = mb_strlen($cek, '8bit') * 8;
51
        $this->checkKeyLength($keyLength);
52
53
        $mode = 'aes-'.($keyLength).'-gcm';
54
        $P = openssl_decrypt($data, $mode, $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
55
        Assertion::true(false !== $P, 'Unable to decrypt or to verify the tag.');
56
57
        return $P;
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function getIVSize(): int
64
    {
65
        return 96;
66
    }
67
68
    /**
69
     * @return int
70
     */
71
    public function getCEKSize(): int
72
    {
73
        return $this->getKeySize();
74
    }
75
76
    /**
77
     * @param int $keyLength
78
     */
79
    private function checkKeyLength(int $keyLength)
80
    {
81
        if (!in_array($keyLength, [16, 24, 32])) {
82
            throw new \InvalidArgumentException('Invalid key length. Allowed sizes are 128, 192 and 256 bits.');
83
        }
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    abstract protected function getKeySize(): int;
90
}
91