|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Threema GmbH |
|
4
|
|
|
* @copyright Copyright (c) 2015-2016 Threema GmbH |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
namespace Threema\MsgApi\Tools; |
|
9
|
|
|
|
|
10
|
|
|
use Threema\Core\Exception; |
|
11
|
|
|
use Threema\Core\KeyPair; |
|
12
|
|
|
use /** @noinspection PhpUndefinedClassInspection */ |
|
13
|
|
|
Sodium; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Contains static methods to do various Threema cryptography related tasks. |
|
17
|
|
|
* Support libsodium < 0.2.0 (Statics) |
|
18
|
|
|
* |
|
19
|
|
|
* @package Threema\Core |
|
20
|
|
|
* @deprecated please update your libsodium package to >= 0.2.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class CryptToolSodiumDep extends CryptTool { |
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $data |
|
25
|
|
|
* @param string $nonce |
|
26
|
|
|
* @param string $senderPrivateKey |
|
27
|
|
|
* @param string $recipientPublicKey |
|
28
|
|
|
* @return string encrypted box |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function makeBox($data, $nonce, $senderPrivateKey, $recipientPublicKey) { |
|
31
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
32
|
|
|
$kp = Sodium::crypto_box_keypair_from_secretkey_and_publickey($senderPrivateKey, $recipientPublicKey); |
|
33
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
34
|
|
|
return Sodium::crypto_box($data, $nonce, $kp); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* make a secret box |
|
39
|
|
|
* |
|
40
|
|
|
* @param $data |
|
41
|
|
|
* @param $nonce |
|
42
|
|
|
* @param $key |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function makeSecretBox($data, $nonce, $key) { |
|
46
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
47
|
|
|
return Sodium::crypto_secretbox($data, $nonce, $key); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $box |
|
53
|
|
|
* @param string $recipientPrivateKey |
|
54
|
|
|
* @param string $senderPublicKey |
|
55
|
|
|
* @param string $nonce |
|
56
|
|
|
* @return null|string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function openBox($box, $recipientPrivateKey, $senderPublicKey, $nonce) { |
|
59
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
60
|
|
|
$kp = Sodium::crypto_box_keypair_from_secretkey_and_publickey($recipientPrivateKey, $senderPublicKey); |
|
61
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
62
|
|
|
return Sodium::crypto_box_open($box, $nonce, $kp); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* decrypt a secret box |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $box as binary |
|
69
|
|
|
* @param string $nonce as binary |
|
70
|
|
|
* @param string $key as binary |
|
71
|
|
|
* @return string as binary |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function openSecretBox($box, $nonce, $key) { |
|
74
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
75
|
|
|
return Sodium::crypto_secretbox_open($box, $nonce, $key); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Generate a new key pair. |
|
81
|
|
|
* |
|
82
|
|
|
* @return KeyPair the new key pair |
|
83
|
|
|
*/ |
|
84
|
|
|
final public function generateKeyPair() { |
|
85
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
86
|
|
|
$kp = Sodium::crypto_box_keypair(); |
|
87
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
88
|
|
|
return new KeyPair(Sodium::crypto_box_secretkey($kp), Sodium::crypto_box_publickey($kp)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $size |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function createRandom($size) { |
|
96
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
97
|
|
|
return Sodium::randombytes_buf($size); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Derive the public key |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $privateKey in binary |
|
104
|
|
|
* @return string public key as binary |
|
105
|
|
|
*/ |
|
106
|
|
|
final public function derivePublicKey($privateKey) { |
|
107
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
108
|
|
|
return Sodium::crypto_box_publickey_from_secretkey($privateKey); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Check if implementation supported |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function isSupported() { |
|
116
|
|
|
return true === extension_loaded("libsodium") |
|
117
|
|
|
&& method_exists('Sodium', 'sodium_version_string'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Validate crypt tool |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
* @throws Exception |
|
125
|
|
|
*/ |
|
126
|
|
|
public function validate() { |
|
127
|
|
|
if(false === $this->isSupported()) { |
|
128
|
|
|
throw new Exception('Sodium implementation not supported'); |
|
129
|
|
|
} |
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getName() { |
|
137
|
|
|
return 'Sodium'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Description of the CryptTool |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getDescription() { |
|
145
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
146
|
|
|
return 'Sodium '.Sodium::sodium_version_string().' (deprecated, please try to update libsodium to version 0.2.0 or higher)'; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|