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 Jose\Algorithm\ContentEncryptionAlgorithmInterface; |
16
|
|
|
|
17
|
|
|
abstract class AESGCM implements ContentEncryptionAlgorithmInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function encryptContent($data, $cek, $iv, $aad, $encoded_protected_header, &$tag) |
23
|
|
|
{ |
24
|
|
|
$calculated_aad = $encoded_protected_header; |
25
|
|
|
if (null !== $aad) { |
26
|
|
|
$calculated_aad .= '.'.$aad; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
list($cyphertext, $tag) = GCM::encrypt($cek, $iv, $data, $calculated_aad); |
30
|
|
|
|
31
|
|
|
return $cyphertext; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function decryptContent($data, $cek, $iv, $aad, $encoded_protected_header, $tag) |
38
|
|
|
{ |
39
|
|
|
$calculated_aad = $encoded_protected_header; |
40
|
|
|
if (null !== $aad) { |
41
|
|
|
$calculated_aad .= '.'.$aad; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return GCM::decrypt($cek, $iv, $data, $calculated_aad, $tag); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
public function getIVSize() |
51
|
|
|
{ |
52
|
|
|
return 96; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
public function getCEKSize() |
59
|
|
|
{ |
60
|
|
|
return $this->getKeySize(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
abstract protected function getKeySize(); |
67
|
|
|
} |
68
|
|
|
|