Completed
Push — master ( 022625...2b82d4 )
by Florent
05:58
created

AESGCM   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 5 Features 0
Metric Value
wmc 12
c 10
b 5
f 0
lcom 1
cbo 1
dl 0
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMode() 0 4 1
B encryptContent() 0 27 5
A decryptContent() 0 21 4
A getIVSize() 0 4 1
A getCEKSize() 0 4 1
getKeySize() 0 1 ?
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Algorithm\ContentEncryption;
13
14
use AESGCM\AESGCM as GCM;
15
use Crypto\Cipher;
16
use Jose\Algorithm\ContentEncryptionAlgorithmInterface;
17
18
abstract class AESGCM implements ContentEncryptionAlgorithmInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function encryptContent($data, $cek, $iv, $aad, $encoded_protected_header, &$tag)
24
    {
25
        $calculated_aad = $encoded_protected_header;
26
        if (null !== $aad) {
27
            $calculated_aad .= '.'.$aad;
28
        }
29
30
        if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
31
            return openssl_encrypt($data, $this->getMode($cek), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad, 16);
32
        } elseif (class_exists('\Crypto\Cipher')) {
33
            $cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize());
34
            $calculated_aad = $encoded_protected_header;
35
            if (null !== $aad) {
36
                $calculated_aad .= '.'.$aad;
37
            }
38
39
            $cipher->setAAD($calculated_aad);
40
            $cyphertext = $cipher->encrypt($data, $cek, $iv);
41
            $tag = $cipher->getTag();
42
43
            return $cyphertext;
44
        }
45
46
        list($cyphertext, $tag) = GCM::encrypt($cek, $iv, $data, $calculated_aad);
47
48
        return $cyphertext;
49
    }
50
51
    /**
52
     *  {@inheritdoc}
53
     */
54
    public function decryptContent($data, $cek, $iv, $aad, $encoded_protected_header, $tag)
55
    {
56
        $calculated_aad = $encoded_protected_header;
57
        if (null !== $aad) {
58
            $calculated_aad .= '.'.$aad;
59
        }
60
61
        if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
62
            return openssl_decrypt($data, $this->getMode($cek), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
63
        } elseif (class_exists('\Crypto\Cipher')) {
64
            $cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize());
65
            $cipher->setTag($tag);
66
            $cipher->setAAD($calculated_aad);
67
68
            $plaintext = $cipher->decrypt($data, $cek, $iv);
69
70
            return $plaintext;
71
        }
72
73
        return GCM::decrypt($cek, $iv, $data, $calculated_aad, $tag);
74
    }
75
76
    /**
77
     * @param string $k
78
     *
79
     * @return string
80
     */
81
    private function getMode($k)
82
    {
83
        return 'aes-'.(8 *  mb_strlen($k, '8bit')).'-gcm';
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function getIVSize()
90
    {
91
        return 96;
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getCEKSize()
98
    {
99
        return $this->getKeySize();
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    abstract protected function getKeySize();
106
}
107