|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The 3DES-168 (T-DES) encryption algorithm class. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\SymmetricEncryption; |
|
8
|
|
|
|
|
9
|
|
|
use CryptoManana\Core\Abstractions\MessageEncryption\AbstractBlockCipherAlgorithm as SymmetricBlockCipherAlgorithm; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class TripleDes - The 3DES-168 (T-DES) encryption algorithm object. |
|
13
|
|
|
* |
|
14
|
|
|
* @package CryptoManana\SymmetricEncryption |
|
15
|
|
|
*/ |
|
16
|
|
|
class TripleDes extends SymmetricBlockCipherAlgorithm |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The internal name of the algorithm. |
|
20
|
|
|
*/ |
|
21
|
|
|
const ALGORITHM_NAME = 'DES-EDE3'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The internal secret key size measured in raw bytes length for the algorithm |
|
25
|
|
|
* |
|
26
|
|
|
* @note For the current algorithm: 192 bits (24 bytes), but 168 bits (21 bytes) are usable |
|
27
|
|
|
*/ |
|
28
|
|
|
const KEY_SIZE = 24; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The internal initialization vector (IV) size measured in raw bytes length for the algorithm |
|
32
|
|
|
* |
|
33
|
|
|
* @note For the current algorithm: 64 bits (8 bytes) |
|
34
|
|
|
*/ |
|
35
|
|
|
const IV_SIZE = 8; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The internal operational block size measured in raw bytes length for the algorithm |
|
39
|
|
|
* |
|
40
|
|
|
* @note For the current algorithm: 64 bits (8 bytes) |
|
41
|
|
|
*/ |
|
42
|
|
|
const BLOCK_SIZE = 8; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* List of valid block operation modes. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array Block mode codes. |
|
48
|
|
|
*/ |
|
49
|
|
|
protected static $validBlockModes = [ |
|
50
|
|
|
self::CBC_MODE, |
|
51
|
|
|
self::CFB_MODE, |
|
52
|
|
|
self::OFB_MODE, |
|
53
|
|
|
self::ECB_MODE |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Fetch the correctly formatted internal encryption algorithm method name for the ECB naming bug at OpenSSL. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string The ECB compatible name for the currently complied OpenSSL library (inside PHP build). |
|
60
|
|
|
*/ |
|
61
|
1 |
|
private function calculateEcbModeCompatibleOpenSslName() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return in_array(strtolower(self::ALGORITHM_NAME), openssl_get_cipher_methods(), true) |
|
64
|
1 |
|
? self::ALGORITHM_NAME : self::ALGORITHM_NAME . '-' . self::ECB_MODE; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Fetch the correctly formatted internal encryption algorithm method name. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string The symmetric encryption algorithm standard. |
|
71
|
|
|
*/ |
|
72
|
33 |
|
protected function fetchAlgorithmMethodName() |
|
73
|
|
|
{ |
|
74
|
33 |
|
return ($this->mode === self::ECB_MODE) ? |
|
75
|
33 |
|
$this->calculateEcbModeCompatibleOpenSslName() : static::ALGORITHM_NAME . '-' . $this->mode; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Internal method for the validation of the system support of the given block operation mode. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $mode The block mode name. |
|
82
|
|
|
* |
|
83
|
|
|
* @throws \Exception Validation errors. |
|
84
|
|
|
* |
|
85
|
|
|
* @codeCoverageIgnore |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function validateBlockModeSupport($mode) |
|
88
|
|
|
{ |
|
89
|
|
|
$methodName = ($mode === self::ECB_MODE) ? |
|
90
|
|
|
$this->calculateEcbModeCompatibleOpenSslName() : static::ALGORITHM_NAME . '-' . $mode; |
|
91
|
|
|
|
|
92
|
|
|
if (!in_array(strtolower($methodName), openssl_get_cipher_methods(), true)) { |
|
93
|
|
|
throw new \RuntimeException( |
|
94
|
|
|
'The algorithm `' . $methodName . '`is not supported under the current system configuration.' |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|