|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Trait implementation of the data padding capabilities and actions for asymmetric encryption algorithms. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageEncryption; |
|
8
|
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageEncryption\AsymmetricPaddingInterface as AsymmetricPaddingSpecification; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait AsymmetricPaddingTrait - Reusable implementation of `AsymmetricPaddingInterface`. |
|
13
|
|
|
* |
|
14
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\AsymmetricPaddingInterface The abstract specification. |
|
15
|
|
|
* |
|
16
|
|
|
* @package CryptoManana\Core\Traits\MessageEncryption |
|
17
|
|
|
* |
|
18
|
|
|
* @property int $padding The asymmetric data padding operation property. |
|
19
|
|
|
* @property bool $useChunks Flag for enabling/disabling data processing via chunks. |
|
20
|
|
|
* |
|
21
|
|
|
* @mixin AsymmetricPaddingSpecification |
|
22
|
|
|
*/ |
|
23
|
|
|
trait AsymmetricPaddingTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Setter for the asymmetric data padding operation property. |
|
27
|
|
|
* |
|
28
|
|
|
* @param int $padding The padding standard integer code value. |
|
29
|
|
|
* |
|
30
|
|
|
* @return $this The asymmetric encryption algorithm object. |
|
31
|
|
|
* @throws \Exception Validation errors. |
|
32
|
|
|
*/ |
|
33
|
32 |
|
public function setPaddingStandard($padding) |
|
34
|
|
|
{ |
|
35
|
|
|
$validPadding = [ |
|
36
|
32 |
|
self::PKCS1_PADDING, |
|
37
|
32 |
|
self::OAEP_PADDING, |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
32 |
|
$padding = filter_var( |
|
41
|
32 |
|
$padding, |
|
42
|
32 |
|
FILTER_VALIDATE_INT, |
|
43
|
|
|
[ |
|
44
|
|
|
"options" => [ |
|
45
|
32 |
|
"min_range" => 1, |
|
46
|
32 |
|
"max_range" => PHP_INT_MAX, |
|
47
|
|
|
], |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
32 |
|
if ($padding === false || !in_array($padding, $validPadding, true)) { |
|
52
|
8 |
|
throw new \InvalidArgumentException( |
|
53
|
8 |
|
'The digestion algorithm standard must be a valid integer bigger than 0.' |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
24 |
|
$this->padding = $padding; |
|
58
|
|
|
|
|
59
|
24 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Getter for the asymmetric data padding operation property. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string The padding standard integer code value. |
|
66
|
|
|
*/ |
|
67
|
24 |
|
public function getPaddingStandard() |
|
68
|
|
|
{ |
|
69
|
24 |
|
return $this->padding; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Getter for the minimum size of the padding bytes that are required/reserved by the algorithm. |
|
74
|
|
|
* |
|
75
|
|
|
* @return int The minimum reserved size of the padding bytes. |
|
76
|
|
|
*/ |
|
77
|
144 |
|
public function getPaddingReservedSize() |
|
78
|
|
|
{ |
|
79
|
|
|
/** |
|
80
|
|
|
* {@internal The reserved byte size for the PKCS1 standard is 11 and for the OAEP is 42. }} |
|
81
|
|
|
*/ |
|
82
|
144 |
|
return ($this->padding === OPENSSL_PKCS1_PADDING) ? 11 : 42; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Enable long data processing via small chunks. |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this The asymmetric encryption algorithm object. |
|
89
|
|
|
* |
|
90
|
|
|
* @internal Using data chunks with asymmetric algorithms is discouraged. |
|
91
|
|
|
*/ |
|
92
|
24 |
|
public function enableChunkProcessing() |
|
93
|
|
|
{ |
|
94
|
24 |
|
$this->useChunks = true; |
|
95
|
|
|
|
|
96
|
24 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Disable long data processing via small chunks. |
|
101
|
|
|
* |
|
102
|
|
|
* @return $this The asymmetric encryption algorithm object. |
|
103
|
|
|
* @internal Using data chunks with asymmetric algorithms is discouraged. |
|
104
|
|
|
*/ |
|
105
|
32 |
|
public function disableChunkProcessing() |
|
106
|
|
|
{ |
|
107
|
32 |
|
$this->useChunks = false; |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
32 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|