|
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
|
|
|
|