|
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
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$gcm = new GCM(); |
|
48
|
|
|
list($cyphertext, $tag) = $gcm->gcm_encrypt($cek, $iv, $data, $calculated_aad); |
|
49
|
|
|
|
|
50
|
|
|
return $cyphertext; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function decryptContent($data, $cek, $iv, $aad, $encoded_protected_header, $tag) |
|
57
|
|
|
{ |
|
58
|
|
|
$calculated_aad = $encoded_protected_header; |
|
59
|
|
|
if (null !== $aad) { |
|
60
|
|
|
$calculated_aad .= '.'.$aad; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (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
|
|
|
$gcm = new GCM(); |
|
74
|
|
|
return $gcm->gcm_decrypt($cek, $iv, $data, $calculated_aad, $tag); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getIVSize() |
|
81
|
|
|
{ |
|
82
|
|
|
return 96; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getCEKSize() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getKeySize(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
abstract protected function getKeySize(); |
|
97
|
|
|
} |
|
98
|
|
|
|