Debian Codegraph @

lib/3rdParty/composer/paragonie/sodium_compat/src/Core/Ed25519.php

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) {
5 return;
6 }
7 if (!class_exists('ParagonIE_Sodium_Core_Curve25519', false)) {
8 require_once dirname(__FILE__) . '/Curve25519.php';
9 }
10
11 /**
12 * Class ParagonIE_Sodium_Core_Ed25519
13 */
14 abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519
15 {
16 const KEYPAIR_BYTES = 96;
17 const SEED_BYTES = 32;
18 const SCALAR_BYTES = 32;
19
20 /**
21 * @internal You should not use this directly from another application
22 *
23 * @return string (96 bytes)
24 * @throws Exception
25 * @throws SodiumException
26 * @throws TypeError
27 */
28 public static function keypair(): string
29 {
30 $seed = random_bytes(self::SEED_BYTES);
31 $pk = '';
32 $sk = '';
33 self::seed_keypair($pk, $sk, $seed);
34 return $sk . $pk;
35 }
36
37 /**
38 * @internal You should not use this directly from another application
39 *
40 * @param string $pk
41 * @param string $sk
42 * @param string $seed
43 * @return string
44 * @throws SodiumException
45 * @throws TypeError
46 */
47 public static function seed_keypair(
48 string &$pk,
49 string &$sk,
50 #[SensitiveParameter]
51 string $seed
52 ): string {
53 if (self::strlen($seed) !== self::SEED_BYTES) {
54 throw new SodiumException('crypto_sign keypair seed must be CRYPTO_SIGN_SEEDBYTES bytes long');
55 }
56 $pk = self::publickey_from_secretkey($seed);
57 $sk = $seed . $pk;
58 return $sk;
59 }
60
61 /**
62 * @internal You should not use this directly from another application
63 *
64 * @param string $keypair
65 * @return string
66 * @throws TypeError
67 */
68 public static function secretkey(
69 #[SensitiveParameter]
70 string $keypair
71 ): string {
72 if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
73 throw new RangeException('crypto_sign keypair must be 96 bytes long');
74 }
75 return self::substr($keypair, 0, 64);
76 }
77
78 /**
79 * @internal You should not use this directly from another application
80 *
81 * @param string $keypair
82 * @return string
83 * @throws TypeError
84 */
85 public static function publickey(
86 #[SensitiveParameter]
87 string $keypair
88 ): string {
89 if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
90 throw new RangeException('crypto_sign keypair must be 96 bytes long');
91 }
92 return self::substr($keypair, 64, 32);
93 }
94
95 /**
96 * @internal You should not use this directly from another application
97 *
98 * @param string $sk
99 * @return string
100 * @throws SodiumException
101 * @throws TypeError
102 */
103 public static function publickey_from_secretkey(
104 #[SensitiveParameter]
105 string $sk
106 ): string {
107 /** @var string $sk */
108 $sk = hash('sha512', self::substr($sk, 0, 32), true);
109 $sk[0] = self::intToChr(
110 self::chrToInt($sk[0]) & 248
111 );
112 $sk[31] = self::intToChr(
113 (self::chrToInt($sk[31]) & 63) | 64
114 );
115 return self::sk_to_pk($sk);
116 }
117
118 /**
119 * Returns TRUE if $A represents a point on the order of the Edwards25519 prime order subgroup.
120 * Returns FALSE if $A is on a different subgroup.
121 *
122 * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A
123 * @return bool
124 */
125 public static function is_on_main_subgroup(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A): bool
126 {
127 $p1 = self::ge_mul_l($A);
128 $t = self::fe_sub($p1->Y, $p1->Z);
129 return self::fe_isnonzero($p1->X) && self::fe_isnonzero($t);
130 }
131
132 /**
133 * @param string $pk
134 * @return string
135 * @throws SodiumException
136 * @throws TypeError
137 */
138 public static function pk_to_curve25519(
139 string $pk
140 ): string {
141 if (self::strlen($pk) !== 32) {
142 throw new SodiumException('Argument 1 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes');
143 }
144 if (self::small_order($pk)) {
145 throw new SodiumException('Public key is on a small order');
146 }
147 $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32));
148 // check that A * L == identity point
149 if (!self::is_on_main_subgroup($A)) {
150 throw new SodiumException('Public key is not on a member of the main subgroup');
151 }
152 $one_minux_y = self::fe_invert(
153 self::fe_sub(
154 self::fe_1(),
155 $A->Y
156 )
157 );
158 $x = self::fe_mul(
159 self::fe_add(self::fe_1(), $A->Y),
160 $one_minux_y
161 );
162 return self::fe_tobytes($x);
163 }
164
165 /**
166 * @internal You should not use this directly from another application
167 *
168 * @param string $sk
169 * @return string
170 * @throws SodiumException
171 * @throws TypeError
172 */
173 public static function sk_to_pk(
174 #[SensitiveParameter]
175 string $sk
176 ): string {
177 return self::ge_p3_tobytes(
178 self::ge_scalarmult_base(
179 self::substr($sk, 0, 32)
180 )
181 );
182 }
183
184 /**
185 * @internal You should not use this directly from another application
186 *
187 * @param string $message
188 * @param string $sk
189 * @return string
190 * @throws SodiumException
191 * @throws TypeError
192 */
193 public static function sign(
194 string $message,
195 #[SensitiveParameter]
196 string $sk
197 ): string {
198 $signature = self::sign_detached($message, $sk);
199 return $signature . $message;
200 }
201
202 /**
203 * @internal You should not use this directly from another application
204 *
205 * @param string $message A signed message
206 * @param string $pk Public key
207 * @return string Message (without signature)
208 * @throws SodiumException
209 * @throws TypeError
210 */
211 public static function sign_open(
212 string $message,
213 string $pk
214 ): string {
215 $signature = self::substr($message, 0, 64);
216
217 /** @var string $message */
218 $message = self::substr($message, 64);
219
220 if (self::verify_detached($signature, $message, $pk)) {
221 return $message;
222 }
223 throw new SodiumException('Invalid signature');
224 }
225
226 /**
227 * @internal You should not use this directly from another application
228 *
229 * @param string $message
230 * @param string $sk
231 * @return string
232 * @throws SodiumException
233 * @throws TypeError
234 */
235 public static function sign_detached(
236 string $message,
237 #[SensitiveParameter]
238 string $sk
239 ): string {
240 if (self::strlen($sk) !== 64) {
241 throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
242 }
243 $az = hash('sha512', self::substr($sk, 0, 32), true);
244
245 $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
246 $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
247
248 $hs = hash_init('sha512');
249 hash_update($hs, self::substr($az, 32, 32));
250 hash_update($hs, $message);
251 $nonceHash = hash_final($hs, true);
252
253 $pk = self::substr($sk, 32, 32);
254
255 $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
256 $sig = self::ge_p3_tobytes(
257 self::ge_scalarmult_base($nonce)
258 );
259
260 $hs = hash_init('sha512');
261 hash_update($hs, self::substr($sig, 0, 32));
262 hash_update($hs, self::substr($pk, 0, 32));
263 hash_update($hs, $message);
264 $hramHash = hash_final($hs, true);
265
266 $hram = self::sc_reduce($hramHash);
267 $sigAfter = self::sc_muladd($hram, $az, $nonce);
268 $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
269
270 try {
271 ParagonIE_Sodium_Compat::memzero($az);
272 } catch (SodiumException) {
273 $az = null;
274 }
275 return $sig;
276 }
277
278 /**
279 * @internal You should not use this directly from another application
280 *
281 * @param string $sig
282 * @param string $message
283 * @param string $pk
284 * @return bool
285 * @throws SodiumException
286 * @throws TypeError
287 */
288 public static function verify_detached(
289 string $sig,
290 string $message,
291 string $pk
292 ): bool {
293 if (self::strlen($sig) !== 64) {
294 throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long');
295 }
296 if (self::strlen($pk) !== 32) {
297 throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long');
298 }
299 if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
300 throw new SodiumException('S >= L - Invalid signature');
301 }
302 if (self::small_order($sig)) {
303 throw new SodiumException('Signature is on too small of an order');
304 }
305 if ((self::chrToInt($sig[63]) & 224) !== 0) {
306 throw new SodiumException('Invalid signature');
307 }
308 $d = 0;
309 for ($i = 0; $i < 32; ++$i) {
310 $d |= self::chrToInt($pk[$i]);
311 }
312 if ($d === 0) {
313 throw new SodiumException('All zero public key');
314 }
315
316 /* The original value of ParagonIE_Sodium_Compat::$fastMult */
317 $orig = ParagonIE_Sodium_Compat::$fastMult;
318
319 // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
320 ParagonIE_Sodium_Compat::$fastMult = true;
321
322 $A = self::ge_frombytes_negate_vartime($pk);
323 if (!self::is_on_main_subgroup($A)) {
324 throw new SodiumException('Public key is not on main subgroup');
325 }
326
327 $hDigest = hash(
328 'sha512',
329 self::substr($sig, 0, 32) .
330 self::substr($pk, 0, 32) .
331 $message,
332 true
333 );
334 $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
335 $R = self::ge_double_scalarmult_vartime(
336 $h,
337 $A,
338 self::substr($sig, 32)
339 );
340 $rcheck = self::ge_tobytes($R);
341
342 // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
343 ParagonIE_Sodium_Compat::$fastMult = $orig;
344
345 return self::verify_32($rcheck, self::substr($sig, 0, 32));
346 }
347
348 /**
349 * @internal You should not use this directly from another application
350 *
351 * @param string $S
352 * @return bool
353 * @throws SodiumException
354 * @throws TypeError
355 */
356 public static function check_S_lt_L(string $S): bool
357 {
358 if (self::strlen($S) < 32) {
359 throw new SodiumException('Signature must be 32 bytes');
360 }
361 $L = array(
362 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
363 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
366 );
367 $c = 0;
368 $n = 1;
369 $i = 32;
370
371 /** @var array<int, int> $L */
372 do {
373 --$i;
374 $x = self::chrToInt($S[$i]);
375 $c |= (
376 (($x - $L[$i]) >> 8) & $n
377 );
378 $n &= (
379 (($x ^ $L[$i]) - 1) >> 8
380 );
381 } while ($i !== 0);
382 return $c === 0;
383 }
384
385 /**
386 * @param string $R
387 * @return bool
388 * @throws SodiumException
389 * @throws TypeError
390 */
391 public static function small_order(string $R): bool
392 {
393 /** @var array<int, array<int, int>> $blocklist */
394 $blocklist = array(
395 /* 0 (order 4) */
396 array(
397 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
398 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
400 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
401 ),
402 /* 1 (order 1) */
403 array(
404 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
408 ),
409 /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
410 array(
411 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
412 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
413 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
414 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
415 ),
416 /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
417 array(
418 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
419 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
420 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
421 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
422 ),
423 /* p-1 (order 2) */
424 array(
425 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
426 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
427 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
428 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
429 ),
430 /* p (order 4) */
431 array(
432 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
433 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
434 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
435 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
436 ),
437 /* p+1 (order 1) */
438 array(
439 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
440 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
441 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
442 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
443 ),
444 /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
445 array(
446 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
447 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
448 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
449 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
450 ),
451 /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
452 array(
453 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
454 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
455 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
457 ),
458 /* 2p-1 (order 2) */
459 array(
460 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
461 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
462 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
463 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
464 ),
465 /* 2p (order 4) */
466 array(
467 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
468 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
469 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
470 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
471 ),
472 /* 2p+1 (order 1) */
473 array(
474 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
475 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
476 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
477 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
478 )
479 );
480 $countBlocklist = count($blocklist);
481 for ($i = 0; $i < $countBlocklist; ++$i) {
482 $c = 0;
483 for ($j = 0; $j < 32; ++$j) {
484 $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j];
485 }
486 if ($c === 0) {
487 return true;
488 }
489 }
490 return false;
491 }
492
493 /**
494 * @param string $s
495 * @return string
496 * @throws SodiumException
497 */
498 public static function scalar_complement(
499 #[SensitiveParameter]
500 string $s
501 ): string {
502 $t_ = self::L . str_repeat("\x00", 32);
503 sodium_increment($t_);
504 $s_ = $s . str_repeat("\x00", 32);
505 ParagonIE_Sodium_Compat::sub($t_, $s_);
506 return self::sc_reduce($t_);
507 }
508
509 /**
510 * @return string
511 * @throws SodiumException
512 */
513 public static function scalar_random(): string
514 {
515 do {
516 $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES);
517 $r[self::SCALAR_BYTES - 1] = self::intToChr(
518 self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f
519 );
520 } while (
521 !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r)
522 );
523 return $r;
524 }
525
526 /**
527 * @param string $s
528 * @return string
529 * @throws SodiumException
530 */
531 public static function scalar_negate(
532 #[SensitiveParameter]
533 string $s
534 ): string {
535 $t_ = self::L . str_repeat("\x00", 32) ;
536 $s_ = $s . str_repeat("\x00", 32) ;
537 ParagonIE_Sodium_Compat::sub($t_, $s_);
538 return self::sc_reduce($t_);
539 }
540
541 /**
542 * @param string $a
543 * @param string $b
544 * @return string
545 * @throws SodiumException
546 */
547 public static function scalar_add(
548 #[SensitiveParameter]
549 string $a,
550 #[SensitiveParameter]
551 string $b
552 ): string {
553 $a_ = $a . str_repeat("\x00", 32);
554 $b_ = $b . str_repeat("\x00", 32);
555 ParagonIE_Sodium_Compat::add($a_, $b_);
556 return self::sc_reduce($a_);
557 }
558
559 /**
560 * @param string $x
561 * @param string $y
562 * @return string
563 * @throws SodiumException
564 */
565 public static function scalar_sub(
566 #[SensitiveParameter]
567 string $x,
568 #[SensitiveParameter]
569 string $y
570 ): string {
571 return self::scalar_add($x, self::scalar_negate($y));
572 }
573 }