1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstraction for key stretching and key derivation objects like the HKDF algorithm. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Abstractions\MessageDigestion; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractKeyStretchingFunction as KeyStretchingAlgorithm; |
10
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\DerivationSaltingInterface as DerivationSalting; |
11
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\DerivationContextInterface as DerivationContext; |
12
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\DerivationDigestLengthInterface as DerivationDigestLength; |
13
|
|
|
use \CryptoManana\Core\Traits\MessageDigestion\DerivationSaltingTrait as DerivationSaltingCapabilities; |
14
|
|
|
use \CryptoManana\Core\Traits\MessageDigestion\DerivationContextTrait as DerivationContextualCapabilities; |
15
|
|
|
use \CryptoManana\Core\Traits\MessageDigestion\DerivationDigestLengthTrait as DerivationDigestLengthCapabilities; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AbstractKeyMaterialDerivationFunction - Abstraction for output key material derivation classes. |
19
|
|
|
* |
20
|
|
|
* @package CryptoManana\Core\Abstractions\MessageDigestion |
21
|
|
|
* |
22
|
|
|
* @mixin DerivationSaltingCapabilities |
23
|
|
|
* @mixin DerivationContextualCapabilities |
24
|
|
|
* @mixin DerivationDigestLengthCapabilities |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractKeyMaterialDerivationFunction extends KeyStretchingAlgorithm implements |
27
|
|
|
DerivationSalting, |
28
|
|
|
DerivationContext, |
29
|
|
|
DerivationDigestLength |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Derivation data salting capabilities. |
33
|
|
|
* |
34
|
|
|
* {@internal Reusable implementation of `DerivationSaltingInterface`. }} |
35
|
|
|
*/ |
36
|
|
|
use DerivationSaltingCapabilities; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Derivation application/context information salting capabilities. |
40
|
|
|
* |
41
|
|
|
* {@internal Reusable implementation of `DerivationContextInterface`. }} |
42
|
|
|
*/ |
43
|
|
|
use DerivationContextualCapabilities; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Derivation derivation control over the outputting digest length capabilities. |
47
|
|
|
* |
48
|
|
|
* {@internal Reusable implementation of `DerivationDigestLengthInterface`. }} |
49
|
|
|
*/ |
50
|
|
|
use DerivationDigestLengthCapabilities; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The derivation salt string property storage. |
54
|
|
|
* |
55
|
|
|
* @var string The derivation salting string value. |
56
|
|
|
*/ |
57
|
|
|
protected $derivationSalt = ''; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The derivation context/application information string property storage. |
61
|
|
|
* |
62
|
|
|
* @var string The derivation context/application information string value. |
63
|
|
|
*/ |
64
|
|
|
protected $contextualString = ''; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The derivation output digest size in bytes length property storage. |
68
|
|
|
* |
69
|
|
|
* @var int The derivation output digest size in bytes length value. |
70
|
|
|
*/ |
71
|
|
|
protected $outputLength = 0; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Calculates a hash value for the given data. |
75
|
|
|
* |
76
|
|
|
* @param string $data The input string. |
77
|
|
|
* |
78
|
|
|
* @return string The digest. |
79
|
|
|
* @throws \Exception Validation errors. |
80
|
|
|
*/ |
81
|
126 |
|
public function hashData($data) |
82
|
|
|
{ |
83
|
126 |
|
if (!is_string($data)) { |
84
|
14 |
|
throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
112 |
|
$data = $this->addSaltString(($data === '') ? ' ' : $data); |
88
|
|
|
|
89
|
112 |
|
$digest = hash_hkdf( |
90
|
112 |
|
static::ALGORITHM_NAME, |
91
|
|
|
$data, |
92
|
112 |
|
$this->outputLength, |
93
|
112 |
|
$this->contextualString, |
94
|
112 |
|
$this->derivationSalt |
95
|
|
|
); // The format here by default is `self::DIGEST_OUTPUT_RAW` |
96
|
|
|
|
97
|
112 |
|
if ($this->digestFormat !== self::DIGEST_OUTPUT_RAW) { |
98
|
112 |
|
$digest = bin2hex($digest); |
99
|
|
|
} |
100
|
|
|
|
101
|
112 |
|
$digest = $this->changeOutputFormat($digest); |
102
|
|
|
|
103
|
112 |
|
return $digest; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get debug information for the class instance. |
108
|
|
|
* |
109
|
|
|
* @return array Debug information. |
110
|
|
|
*/ |
111
|
22 |
|
public function __debugInfo() |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
22 |
|
'standard' => static::ALGORITHM_NAME, |
115
|
22 |
|
'type' => 'key stretching or key material derivation', |
116
|
22 |
|
'salt' => $this->salt, |
117
|
22 |
|
'mode' => $this->saltingMode, |
118
|
22 |
|
'derivation salt' => $this->derivationSalt, |
119
|
22 |
|
'context information string' => $this->contextualString, |
120
|
22 |
|
'digestion output length in bytes' => $this->outputLength, |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|