1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Btccom\JustEncrypt; |
4
|
|
|
|
5
|
|
|
use AESGCM\AESGCM; |
6
|
|
|
use BitWasp\Buffertools\Buffer; |
7
|
|
|
use BitWasp\Buffertools\BufferInterface; |
8
|
|
|
use BitWasp\Buffertools\Parser; |
9
|
|
|
|
10
|
|
|
class HeaderBlob |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $saltLen; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var BufferInterface |
19
|
|
|
*/ |
20
|
|
|
private $salt; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $iterations; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* HeaderBlob constructor. |
29
|
|
|
* @param int $saltLen |
30
|
|
|
* @param BufferInterface $salt |
31
|
|
|
* @param int $iterations |
32
|
|
|
*/ |
33
|
10 |
|
public function __construct($saltLen, BufferInterface $salt, $iterations) |
34
|
|
|
{ |
35
|
10 |
|
if ($salt->getSize() === 0) { |
36
|
|
|
throw new \RuntimeException('Salt must not be empty'); |
37
|
|
|
} |
38
|
|
|
|
39
|
10 |
|
if ($saltLen > 0x80) { |
40
|
|
|
throw new \RuntimeException('Salt too long'); |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
if ($saltLen !== $salt->getSize()) { |
44
|
|
|
throw new \RuntimeException('Mismatch in salt size'); |
45
|
|
|
} |
46
|
|
|
|
47
|
10 |
|
if (!(is_int($iterations) && $iterations >= 0 && $iterations < pow(2, 32))) { |
48
|
|
|
throw new \RuntimeException('Iterations must be a number between 1 and 2^32'); |
49
|
|
|
} |
50
|
|
|
|
51
|
10 |
|
$this->saltLen = $saltLen; |
52
|
10 |
|
$this->salt = $salt; |
53
|
10 |
|
$this->iterations = $iterations; |
54
|
10 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
1 |
|
public function getSaltLen() |
60
|
|
|
{ |
61
|
1 |
|
return $this->saltLen; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return BufferInterface |
66
|
|
|
*/ |
67
|
1 |
|
public function getSalt() |
68
|
|
|
{ |
69
|
1 |
|
return $this->salt; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
1 |
|
public function getIterations() |
76
|
|
|
{ |
77
|
1 |
|
return $this->iterations; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param BufferInterface $passphrase |
82
|
|
|
* @return BufferInterface |
83
|
|
|
*/ |
84
|
9 |
|
public function deriveKey(BufferInterface $passphrase) |
85
|
|
|
{ |
86
|
9 |
|
if ($passphrase->getSize() === 0) { |
87
|
|
|
throw new \RuntimeException('Password must not be empty'); |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
return new Buffer(hash_pbkdf2(KeyDerivation::HASHER, $passphrase->getBinary(), $this->salt->getBinary(), $this->iterations, KeyDerivation::KEYLEN_BITS / 8, true)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
5 |
|
public function getBinary() |
97
|
|
|
{ |
98
|
5 |
|
return pack('c', $this->saltLen) . $this->salt->getBinary() . pack('V', $this->iterations); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param BufferInterface $plainText |
103
|
|
|
* @param BufferInterface $passphrase |
104
|
|
|
* @param BufferInterface $iv |
105
|
|
|
* @return EncryptedBlob |
106
|
|
|
*/ |
107
|
3 |
|
public function encrypt(BufferInterface $plainText, BufferInterface $passphrase, BufferInterface $iv) |
108
|
|
|
{ |
109
|
3 |
|
$iv = $iv ?: new Buffer(random_bytes(Encryption::IVLEN_BYTES)); |
110
|
|
|
|
111
|
3 |
|
list ($ct, $tag) = AESGCM::encrypt( |
112
|
3 |
|
$this->deriveKey($passphrase)->getBinary(), |
113
|
3 |
|
$iv->getBinary(), |
114
|
3 |
|
$plainText->getBinary(), |
115
|
3 |
|
$this->getBinary() |
116
|
|
|
); |
117
|
|
|
|
118
|
3 |
|
return new EncryptedBlob($this, $iv, new Buffer($ct), new Buffer($tag)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Buffer |
123
|
|
|
*/ |
124
|
|
|
public function getBuffer() |
125
|
|
|
{ |
126
|
|
|
return new Buffer($this->getBinary()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Parser $parser |
131
|
|
|
* @return HeaderBlob |
132
|
|
|
*/ |
133
|
3 |
|
public static function fromParser(Parser $parser) |
134
|
|
|
{ |
135
|
3 |
|
$saltLen = unpack('c', $parser->readBytes(1)->getBinary())[1]; |
136
|
3 |
|
$salt = $parser->readBytes($saltLen); |
137
|
3 |
|
$iterations = unpack('V', $parser->readBytes(4)->getBinary())[1]; |
138
|
3 |
|
return new self($saltLen, $salt, $iterations); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|