23
23
use Jose \Component \Core \Util \Ecc \PrivateKey ;
24
24
use Jose \Component \Core \Util \ECKey ;
25
25
use Jose \Component \Encryption \Algorithm \KeyEncryption \Util \ConcatKDF ;
26
+ use RuntimeException ;
26
27
27
28
final class ECDHES implements KeyAgreement
28
29
{
@@ -74,6 +75,7 @@ public function calculateAgreementKey(JWK $private_key, JWK $public_key): string
74
75
75
76
return $ this ->convertDecToBin (EcDH::computeSharedKey ($ curve , $ pub_key , $ priv_key ));
76
77
case 'X25519 ' :
78
+ $ this ->checkSodiumIsAvailable ();
77
79
$ sKey = Base64Url::decode ($ private_key ->get ('d ' ));
78
80
$ recipientPublickey = Base64Url::decode ($ public_key ->get ('x ' ));
79
81
@@ -219,6 +221,7 @@ private function convertDecToBin(GMP $dec): string
219
221
*/
220
222
private function createOKPKey (string $ curve ): JWK
221
223
{
224
+ $ this ->checkSodiumIsAvailable ();
222
225
switch ($ curve ) {
223
226
case 'X25519 ' :
224
227
$ keyPair = sodium_crypto_box_keypair ();
@@ -243,4 +246,22 @@ private function createOKPKey(string $curve): JWK
243
246
'd ' => Base64Url::encode ($ d ),
244
247
]);
245
248
}
249
+
250
+ private function checkSodiumIsAvailable (): void
251
+ {
252
+ $ requiredFunctions = [
253
+ 'sodium_crypto_scalarmult ' ,
254
+ 'sodium_crypto_box_keypair ' ,
255
+ 'sodium_crypto_box_secretkey ' ,
256
+ 'sodium_crypto_box_publickey ' ,
257
+ 'sodium_crypto_sign_keypair ' ,
258
+ 'sodium_crypto_sign_secretkey ' ,
259
+ 'sodium_crypto_sign_publickey ' ,
260
+ ];
261
+ foreach ($ requiredFunctions as $ function ) {
262
+ if (!\function_exists ($ function )) {
263
+ throw new RuntimeException (sprintf ('The function "%s" is not available. Have you installed the Sodium extension ' , $ function ));
264
+ }
265
+ }
266
+ }
246
267
}
0 commit comments