Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Signature/Algorithm/EdDSA.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Signature\Algorithm;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\JWK;
9 use Jose\Component\Core\Util\Base64UrlSafe;
10 use ParagonIE\Sodium\Core\Ed25519;
11 use RuntimeException;
12 use function assert;
13 use function extension_loaded;
14 use function in_array;
15 use function is_string;
16
17 final class EdDSA implements SignatureAlgorithm
18 {
19 public function __construct()
20 {
21 if (! extension_loaded('sodium')) {
22 throw new RuntimeException('The extension "sodium" is not available. Please install it to use this method');
23 }
24 }
25
26 public function allowedKeyTypes(): array
27 {
28 return ['OKP'];
29 }
30
31 /**
32 * @return non-empty-string
33 */
34 public function sign(JWK $key, string $input): string
35 {
36 $this->checkKey($key);
37 if (! $key->has('d')) {
38 throw new InvalidArgumentException('The EC key is not private');
39 }
40 $d = $key->get('d');
41 if (! is_string($d) || $d === '') {
42 throw new InvalidArgumentException('Invalid "d" parameter.');
43 }
44 if (! $key->has('x')) {
45 $x = self::getPublicKey($key);
46 } else {
47 $x = $key->get('x');
48 }
49 if (! is_string($x) || $x === '') {
50 throw new InvalidArgumentException('Invalid "x" parameter.');
51 }
52 /** @var non-empty-string $x */
53 $x = Base64UrlSafe::decodeNoPadding($x);
54 /** @var non-empty-string $d */
55 $d = Base64UrlSafe::decodeNoPadding($d);
56 $secret = $d . $x;
57
58 return match ($key->get('crv')) {
59 'Ed25519' => sodium_crypto_sign_detached($input, $secret),
60 default => throw new InvalidArgumentException('Unsupported curve'),
61 };
62 }
63
64 /**
65 * @param non-empty-string $signature
66 */
67 public function verify(JWK $key, string $input, string $signature): bool
68 {
69 $this->checkKey($key);
70 $x = $key->get('x');
71 if (! is_string($x)) {
72 throw new InvalidArgumentException('Invalid "x" parameter.');
73 }
74
75 /** @var non-empty-string $public */
76 $public = Base64UrlSafe::decodeNoPadding($x);
77
78 return match ($key->get('crv')) {
79 'Ed25519' => sodium_crypto_sign_verify_detached($signature, $input, $public),
80 default => throw new InvalidArgumentException('Unsupported curve'),
81 };
82 }
83
84 public function name(): string
85 {
86 return 'EdDSA';
87 }
88
89 private static function getPublicKey(JWK $key): string
90 {
91 $d = $key->get('d');
92 assert(is_string($d), 'Unsupported key type');
93
94 switch ($key->get('crv')) {
95 case 'Ed25519':
96 return Ed25519::publickey_from_secretkey($d);
97 case 'X25519':
98 if (extension_loaded('sodium')) {
99 return sodium_crypto_scalarmult_base($d);
100 }
101 // no break
102 default:
103 throw new InvalidArgumentException('Unsupported key type');
104 }
105 }
106
107 private function checkKey(JWK $key): void
108 {
109 if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
110 throw new InvalidArgumentException('Wrong key type.');
111 }
112 foreach (['x', 'crv'] as $k) {
113 if (! $key->has($k)) {
114 throw new InvalidArgumentException(sprintf('The key parameter "%s" is missing.', $k));
115 }
116 }
117 if ($key->get('crv') !== 'Ed25519') {
118 throw new InvalidArgumentException('Unsupported curve.');
119 }
120 }
121 }