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 Crypto\Cipher; |
15
|
|
|
use Jose\Algorithm\ContentEncryptionAlgorithmInterface; |
16
|
|
|
use Jose\Util\GCM; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
abstract class AESGCM implements ContentEncryptionAlgorithmInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function encryptContent($data, $cek, $iv, $aad, $encoded_protected_header, &$tag) |
27
|
|
|
{ |
28
|
|
|
$calculated_aad = $encoded_protected_header; |
29
|
|
|
if (null !== $aad) { |
30
|
|
|
$calculated_aad .= '.'.$aad; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (class_exists('\Crypto\Cipher')) { |
34
|
|
|
$cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize()); |
35
|
|
|
$calculated_aad = $encoded_protected_header; |
36
|
|
|
if (null !== $aad) { |
37
|
|
|
$calculated_aad .= '.'.$aad; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$cipher->setAAD($calculated_aad); |
41
|
|
|
$cyphertext = $cipher->encrypt($data, $cek, $iv); |
42
|
|
|
$tag = $cipher->getTag(); |
43
|
|
|
|
44
|
|
|
return $cyphertext; |
45
|
|
|
} elseif (version_compare(PHP_VERSION, '7.1.0') >= 0) { |
46
|
|
|
return openssl_encrypt($data, $this->getMode($cek), $cek, OPENSSL_RAW_DATA, $iv, $tag , $aad, 16); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
list($cyphertext, $tag) = GCM::encrypt($cek, $iv, $data, $calculated_aad); |
50
|
|
|
|
51
|
|
|
return $cyphertext; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function decryptContent($data, $cek, $iv, $aad, $encoded_protected_header, $tag) |
58
|
|
|
{ |
59
|
|
|
$calculated_aad = $encoded_protected_header; |
60
|
|
|
if (null !== $aad) { |
61
|
|
|
$calculated_aad .= '.'.$aad; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (class_exists('\Crypto\Cipher')) { |
65
|
|
|
$cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize()); |
66
|
|
|
$cipher->setTag($tag); |
67
|
|
|
$cipher->setAAD($calculated_aad); |
68
|
|
|
|
69
|
|
|
$plaintext = $cipher->decrypt($data, $cek, $iv); |
70
|
|
|
|
71
|
|
|
return $plaintext; |
72
|
|
|
} elseif (version_compare(PHP_VERSION, '7.1.0') >= 0) { |
73
|
|
|
return openssl_decrypt($data, $this->getMode($cek), $cek, OPENSSL_RAW_DATA, $iv, $tag , $aad); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return GCM::decrypt($cek, $iv, $data, $calculated_aad, $tag); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $k |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function getMode($k) |
85
|
|
|
{ |
86
|
|
|
return 'aes-'.(8 * strlen($k)).'-gcm'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getIVSize() |
93
|
|
|
{ |
94
|
|
|
return 96; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function getCEKSize() |
101
|
|
|
{ |
102
|
|
|
return $this->getKeySize(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
abstract protected function getKeySize(); |
109
|
|
|
} |
110
|
|
|
|