AESCCM   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 47
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A allowedKeyTypes() 0 4 1
A encryptContent() 0 14 3
A decryptContent() 0 14 3
getMode() 0 1 ?
getTagLength() 0 1 ?
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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 Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm;
17
use RuntimeException;
18
19
abstract class AESCCM implements ContentEncryptionAlgorithm
20
{
21
    public function allowedKeyTypes(): array
22
    {
23
        return []; //Irrelevant
24
    }
25
26
    /**
27
     * @throws RuntimeException if the data cannot be encrypted
28
     */
29
    public function encryptContent(string $data, string $cek, string $iv, ?string $aad, string $encoded_protected_header, ?string &$tag = null): string
30
    {
31
        $calculated_aad = $encoded_protected_header;
32
        if (null !== $aad) {
33
            $calculated_aad .= '.'.$aad;
34
        }
35
        $tag = '';
36
        $result = openssl_encrypt($data, $this->getMode(), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad, $this->getTagLength());
37
        if (false === $result) {
38
            throw new RuntimeException('Unable to encrypt the content');
39
        }
40
41
        return $result;
42
    }
43
44
    /**
45
     * @throws RuntimeException if the data cannot be decrypted
46
     */
47
    public function decryptContent(string $data, string $cek, string $iv, ?string $aad, string $encoded_protected_header, string $tag): string
48
    {
49
        $calculated_aad = $encoded_protected_header;
50
        if (null !== $aad) {
51
            $calculated_aad .= '.'.$aad;
52
        }
53
54
        $result = openssl_decrypt($data, $this->getMode(), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
55
        if (false === $result) {
56
            throw new RuntimeException('Unable to decrypt the content');
57
        }
58
59
        return $result;
60
    }
61
62
    abstract protected function getMode(): string;
63
64
    abstract protected function getTagLength(): int;
65
}
66