Failed Conditions
Push — v7 ( 838dcb )
by Florent
04:30
created

AESGCM::getMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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 Jose\Algorithm\ContentEncryptionAlgorithmInterface;
15
16
abstract class AESGCM implements ContentEncryptionAlgorithmInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function encryptContent(string $data, string $cek, string $iv, ?string $aad, string $encoded_protected_header, ?string &$tag): string
22
    {
23
        $calculated_aad = $encoded_protected_header;
24
        if (null !== $aad) {
25
            $calculated_aad .= '.'.$aad;
26
        }
27
28
        $mode = $this->getMode($cek);
29
        $tag = null;
30
        $tag_length = 128;
31
32
        return openssl_encrypt($data, $mode, $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad, $tag_length / 8);
33
    }
34
35
    /**
36
     *  {@inheritdoc}
37
     */
38
    public function decryptContent(string $data, string $cek, string $iv, ?string $aad, string $encoded_protected_header, string $tag): string
39
    {
40
        $calculated_aad = $encoded_protected_header;
41
        if (null !== $aad) {
42
            $calculated_aad .= '.'.$aad;
43
        }
44
45
        $mode = $this->getMode($cek);
46
47
        return openssl_decrypt($data, $mode, $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad);
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getIVSize(): int
54
    {
55
        return 96;
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getCEKSize():int
62
    {
63
        return $this->getKeySize();
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    abstract protected function getKeySize(): int;
70
71
    /**
72
     * @param string $kek
73
     *
74
     * @return string
75
     */
76
    private function getMode(string $kek): string
77
    {
78
        $key_length = mb_strlen($kek, '8bit') * 8;
79
80
        return 'aes-'.($key_length).'-gcm';
81
    }
82
}
83