From 613e08e3a047708bedf1cb1f78fefe239e71301d Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Tue, 13 Feb 2024 08:17:59 +0100 Subject: [PATCH] Fix EdDSA algorithm --- src/Library/Signature/Algorithm/EdDSA.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Library/Signature/Algorithm/EdDSA.php b/src/Library/Signature/Algorithm/EdDSA.php index 1c12b6e9..e24c41e6 100644 --- a/src/Library/Signature/Algorithm/EdDSA.php +++ b/src/Library/Signature/Algorithm/EdDSA.php @@ -40,7 +40,7 @@ public function sign(JWK $key, string $input): string throw new InvalidArgumentException('Invalid "d" parameter.'); } if (! $key->has('x')) { - $x = sodium_crypto_sign_publickey_from_secretkey($d); + $x = $this->getPublicKey($key); } else { $x = $key->get('x'); } @@ -84,6 +84,21 @@ public function name(): string return 'EdDSA'; } + private static function getPublicKey(JWK $key): string + { + switch ($key->get('crv')) { + case 'Ed25519': + return Ed25519::publickey_from_secretkey($key->get('d')); + case 'X25519': + if (extension_loaded('sodium')) { + return sodium_crypto_scalarmult_base($key->get('d')); + } + // no break + default: + throw new InvalidArgumentException('Unsupported key type'); + } + } + private function checkKey(JWK $key): void { if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {