2323use Jose \Component \Core \Util \Ecc \PrivateKey ;
2424use Jose \Component \Core \Util \ECKey ;
2525use Jose \Component \Encryption \Algorithm \KeyEncryption \Util \ConcatKDF ;
26+ use RuntimeException ;
2627
2728final class ECDHES implements KeyAgreement
2829{
@@ -74,6 +75,7 @@ public function calculateAgreementKey(JWK $private_key, JWK $public_key): string
7475
7576 return $ this ->convertDecToBin (EcDH::computeSharedKey ($ curve , $ pub_key , $ priv_key ));
7677 case 'X25519 ' :
78+ $ this ->checkSodiumIsAvailable ();
7779 $ sKey = Base64Url::decode ($ private_key ->get ('d ' ));
7880 $ recipientPublickey = Base64Url::decode ($ public_key ->get ('x ' ));
7981
@@ -219,6 +221,7 @@ private function convertDecToBin(GMP $dec): string
219221 */
220222 private function createOKPKey (string $ curve ): JWK
221223 {
224+ $ this ->checkSodiumIsAvailable ();
222225 switch ($ curve ) {
223226 case 'X25519 ' :
224227 $ keyPair = sodium_crypto_box_keypair ();
@@ -243,4 +246,22 @@ private function createOKPKey(string $curve): JWK
243246 'd ' => Base64Url::encode ($ d ),
244247 ]);
245248 }
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+ }
246267}
0 commit comments