changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
21:48c4eec2b7e6 | 22:8c2e4d02f4ef |
---|---|
57 const BASE64_VARIANT_URLSAFE_NO_PADDING = 7; |
57 const BASE64_VARIANT_URLSAFE_NO_PADDING = 7; |
58 const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32; |
58 const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32; |
59 const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0; |
59 const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0; |
60 const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12; |
60 const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12; |
61 const CRYPTO_AEAD_AES256GCM_ABYTES = 16; |
61 const CRYPTO_AEAD_AES256GCM_ABYTES = 16; |
62 const CRYPTO_AEAD_AEGIS128L_KEYBYTES = 16; |
|
63 const CRYPTO_AEAD_AEGIS128L_NSECBYTES = 0; |
|
64 const CRYPTO_AEAD_AEGIS128L_NPUBBYTES = 16; |
|
65 const CRYPTO_AEAD_AEGIS128L_ABYTES = 32; |
|
66 const CRYPTO_AEAD_AEGIS256_KEYBYTES = 32; |
|
67 const CRYPTO_AEAD_AEGIS256_NSECBYTES = 0; |
|
68 const CRYPTO_AEAD_AEGIS256_NPUBBYTES = 32; |
|
69 const CRYPTO_AEAD_AEGIS256_ABYTES = 32; |
|
62 const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32; |
70 const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32; |
63 const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0; |
71 const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0; |
64 const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8; |
72 const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8; |
65 const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16; |
73 const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16; |
66 const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32; |
74 const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32; |
153 * @param string $val |
161 * @param string $val |
154 * @param string $addv |
162 * @param string $addv |
155 * @return void |
163 * @return void |
156 * @throws SodiumException |
164 * @throws SodiumException |
157 */ |
165 */ |
158 public static function add(&$val, $addv) |
166 public static function add( |
159 { |
167 #[\SensitiveParameter] |
168 &$val, |
|
169 #[\SensitiveParameter] |
|
170 $addv |
|
171 ) { |
|
160 $val_len = ParagonIE_Sodium_Core_Util::strlen($val); |
172 $val_len = ParagonIE_Sodium_Core_Util::strlen($val); |
161 $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv); |
173 $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv); |
162 if ($val_len !== $addv_len) { |
174 if ($val_len !== $addv_len) { |
163 throw new SodiumException('values must have the same length'); |
175 throw new SodiumException('values must have the same length'); |
164 } |
176 } |
179 * @param int $variant |
191 * @param int $variant |
180 * @param string $ignore |
192 * @param string $ignore |
181 * @return string |
193 * @return string |
182 * @throws SodiumException |
194 * @throws SodiumException |
183 */ |
195 */ |
184 public static function base642bin($encoded, $variant, $ignore = '') |
196 public static function base642bin( |
185 { |
197 #[\SensitiveParameter] |
198 $encoded, |
|
199 $variant, |
|
200 $ignore = '' |
|
201 ) { |
|
186 /* Type checks: */ |
202 /* Type checks: */ |
187 ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1); |
203 ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1); |
188 |
204 |
189 /** @var string $encoded */ |
205 /** @var string $encoded */ |
190 $encoded = (string) $encoded; |
206 $encoded = (string) $encoded; |
222 * @param string $decoded |
238 * @param string $decoded |
223 * @param int $variant |
239 * @param int $variant |
224 * @return string |
240 * @return string |
225 * @throws SodiumException |
241 * @throws SodiumException |
226 */ |
242 */ |
227 public static function bin2base64($decoded, $variant) |
243 public static function bin2base64( |
228 { |
244 #[\SensitiveParameter] |
245 $decoded, |
|
246 $variant |
|
247 ) { |
|
229 /* Type checks: */ |
248 /* Type checks: */ |
230 ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1); |
249 ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1); |
231 /** @var string $decoded */ |
250 /** @var string $decoded */ |
232 $decoded = (string) $decoded; |
251 $decoded = (string) $decoded; |
233 if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) { |
252 if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) { |
255 * @return string A hexadecimal-encoded string |
274 * @return string A hexadecimal-encoded string |
256 * @throws SodiumException |
275 * @throws SodiumException |
257 * @throws TypeError |
276 * @throws TypeError |
258 * @psalm-suppress MixedArgument |
277 * @psalm-suppress MixedArgument |
259 */ |
278 */ |
260 public static function bin2hex($string) |
279 public static function bin2hex( |
261 { |
280 #[\SensitiveParameter] |
281 $string |
|
282 ) { |
|
262 /* Type checks: */ |
283 /* Type checks: */ |
263 ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); |
284 ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); |
264 |
285 |
265 if (self::useNewSodiumAPI()) { |
286 if (self::useNewSodiumAPI()) { |
266 return (string) sodium_bin2hex($string); |
287 return (string) sodium_bin2hex($string); |
282 * If > 0 if the right operand is less than the left |
303 * If > 0 if the right operand is less than the left |
283 * @throws SodiumException |
304 * @throws SodiumException |
284 * @throws TypeError |
305 * @throws TypeError |
285 * @psalm-suppress MixedArgument |
306 * @psalm-suppress MixedArgument |
286 */ |
307 */ |
287 public static function compare($left, $right) |
308 public static function compare( |
288 { |
309 #[\SensitiveParameter] |
310 $left, |
|
311 #[\SensitiveParameter] |
|
312 $right |
|
313 ) { |
|
289 /* Type checks: */ |
314 /* Type checks: */ |
290 ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1); |
315 ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1); |
291 ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2); |
316 ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2); |
292 |
317 |
293 if (self::useNewSodiumAPI()) { |
318 if (self::useNewSodiumAPI()) { |
295 } |
320 } |
296 if (self::use_fallback('compare')) { |
321 if (self::use_fallback('compare')) { |
297 return (int) call_user_func('\\Sodium\\compare', $left, $right); |
322 return (int) call_user_func('\\Sodium\\compare', $left, $right); |
298 } |
323 } |
299 return ParagonIE_Sodium_Core_Util::compare($left, $right); |
324 return ParagonIE_Sodium_Core_Util::compare($left, $right); |
325 } |
|
326 |
|
327 /** |
|
328 * Authenticated Encryption with Associated Data: Decryption |
|
329 * |
|
330 * Algorithm: |
|
331 * AEGIS-128L |
|
332 * |
|
333 * @param string $ciphertext Encrypted message (with MAC appended) |
|
334 * @param string $assocData Authenticated Associated Data (unencrypted) |
|
335 * @param string $nonce Number to be used only Once; must be 32 bytes |
|
336 * @param string $key Encryption key |
|
337 * |
|
338 * @return string The original plaintext message |
|
339 * @throws SodiumException |
|
340 * @throws TypeError |
|
341 * @psalm-suppress MixedArgument |
|
342 * @psalm-suppress MixedInferredReturnType |
|
343 * @psalm-suppress MixedReturnStatement |
|
344 */ |
|
345 public static function crypto_aead_aegis128l_decrypt( |
|
346 $ciphertext = '', |
|
347 $assocData = '', |
|
348 $nonce = '', |
|
349 #[\SensitiveParameter] |
|
350 $key = '' |
|
351 ) { |
|
352 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
|
353 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
|
354 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); |
|
355 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); |
|
356 |
|
357 /* Input validation: */ |
|
358 if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) { |
|
359 throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS_128L_NPUBBYTES long'); |
|
360 } |
|
361 if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) { |
|
362 throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); |
|
363 } |
|
364 $ct_length = ParagonIE_Sodium_Core_Util::strlen($ciphertext); |
|
365 if ($ct_length < self::CRYPTO_AEAD_AEGIS128L_ABYTES) { |
|
366 throw new SodiumException('Message must be at least CRYPTO_AEAD_AEGIS128L_ABYTES long'); |
|
367 } |
|
368 |
|
369 $ct = ParagonIE_Sodium_Core_Util::substr( |
|
370 $ciphertext, |
|
371 0, |
|
372 $ct_length - self::CRYPTO_AEAD_AEGIS128L_ABYTES |
|
373 ); |
|
374 $tag = ParagonIE_Sodium_Core_Util::substr( |
|
375 $ciphertext, |
|
376 $ct_length - self::CRYPTO_AEAD_AEGIS128L_ABYTES, |
|
377 self::CRYPTO_AEAD_AEGIS128L_ABYTES |
|
378 ); |
|
379 return ParagonIE_Sodium_Core_AEGIS128L::decrypt($ct, $tag, $assocData, $key, $nonce); |
|
380 } |
|
381 |
|
382 /** |
|
383 * Authenticated Encryption with Associated Data: Encryption |
|
384 * |
|
385 * Algorithm: |
|
386 * AEGIS-128L |
|
387 * |
|
388 * @param string $plaintext Message to be encrypted |
|
389 * @param string $assocData Authenticated Associated Data (unencrypted) |
|
390 * @param string $nonce Number to be used only Once; must be 32 bytes |
|
391 * @param string $key Encryption key |
|
392 * |
|
393 * @return string Ciphertext with 32-byte authentication tag appended |
|
394 * @throws SodiumException |
|
395 * @throws TypeError |
|
396 * @psalm-suppress MixedArgument |
|
397 */ |
|
398 public static function crypto_aead_aegis128l_encrypt( |
|
399 #[\SensitiveParameter] |
|
400 $plaintext = '', |
|
401 $assocData = '', |
|
402 $nonce = '', |
|
403 #[\SensitiveParameter] |
|
404 $key = '' |
|
405 ) { |
|
406 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
|
407 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
|
408 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); |
|
409 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); |
|
410 |
|
411 /* Input validation: */ |
|
412 if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) { |
|
413 throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); |
|
414 } |
|
415 if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) { |
|
416 throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); |
|
417 } |
|
418 |
|
419 list($ct, $tag) = ParagonIE_Sodium_Core_AEGIS128L::encrypt($plaintext, $assocData, $key, $nonce); |
|
420 return $ct . $tag; |
|
421 } |
|
422 |
|
423 /** |
|
424 * Return a secure random key for use with the AEGIS-128L |
|
425 * symmetric AEAD interface. |
|
426 * |
|
427 * @return string |
|
428 * @throws Exception |
|
429 * @throws Error |
|
430 */ |
|
431 public static function crypto_aead_aegis128l_keygen() |
|
432 { |
|
433 return random_bytes(self::CRYPTO_AEAD_AEGIS128L_KEYBYTES); |
|
434 } |
|
435 |
|
436 /** |
|
437 * Authenticated Encryption with Associated Data: Decryption |
|
438 * |
|
439 * Algorithm: |
|
440 * AEGIS-256 |
|
441 * |
|
442 * @param string $ciphertext Encrypted message (with MAC appended) |
|
443 * @param string $assocData Authenticated Associated Data (unencrypted) |
|
444 * @param string $nonce Number to be used only Once; must be 32 bytes |
|
445 * @param string $key Encryption key |
|
446 * |
|
447 * @return string The original plaintext message |
|
448 * @throws SodiumException |
|
449 * @throws TypeError |
|
450 * @psalm-suppress MixedArgument |
|
451 * @psalm-suppress MixedInferredReturnType |
|
452 * @psalm-suppress MixedReturnStatement |
|
453 */ |
|
454 public static function crypto_aead_aegis256_decrypt( |
|
455 $ciphertext = '', |
|
456 $assocData = '', |
|
457 $nonce = '', |
|
458 #[\SensitiveParameter] |
|
459 $key = '' |
|
460 ) { |
|
461 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
|
462 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
|
463 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); |
|
464 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); |
|
465 |
|
466 /* Input validation: */ |
|
467 if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS256_NPUBBYTES) { |
|
468 throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS256_NPUBBYTES long'); |
|
469 } |
|
470 if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS256_KEYBYTES) { |
|
471 throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS256_KEYBYTES long'); |
|
472 } |
|
473 $ct_length = ParagonIE_Sodium_Core_Util::strlen($ciphertext); |
|
474 if ($ct_length < self::CRYPTO_AEAD_AEGIS256_ABYTES) { |
|
475 throw new SodiumException('Message must be at least CRYPTO_AEAD_AEGIS256_ABYTES long'); |
|
476 } |
|
477 |
|
478 $ct = ParagonIE_Sodium_Core_Util::substr( |
|
479 $ciphertext, |
|
480 0, |
|
481 $ct_length - self::CRYPTO_AEAD_AEGIS256_ABYTES |
|
482 ); |
|
483 $tag = ParagonIE_Sodium_Core_Util::substr( |
|
484 $ciphertext, |
|
485 $ct_length - self::CRYPTO_AEAD_AEGIS256_ABYTES, |
|
486 self::CRYPTO_AEAD_AEGIS256_ABYTES |
|
487 ); |
|
488 return ParagonIE_Sodium_Core_AEGIS256::decrypt($ct, $tag, $assocData, $key, $nonce); |
|
489 } |
|
490 |
|
491 /** |
|
492 * Authenticated Encryption with Associated Data: Encryption |
|
493 * |
|
494 * Algorithm: |
|
495 * AEGIS-256 |
|
496 * |
|
497 * @param string $plaintext Message to be encrypted |
|
498 * @param string $assocData Authenticated Associated Data (unencrypted) |
|
499 * @param string $nonce Number to be used only Once; must be 32 bytes |
|
500 * @param string $key Encryption key |
|
501 * |
|
502 * @return string Ciphertext with 32-byte authentication tag appended |
|
503 * @throws SodiumException |
|
504 * @throws TypeError |
|
505 * @psalm-suppress MixedArgument |
|
506 */ |
|
507 public static function crypto_aead_aegis256_encrypt( |
|
508 #[\SensitiveParameter] |
|
509 $plaintext = '', |
|
510 $assocData = '', |
|
511 $nonce = '', |
|
512 #[\SensitiveParameter] |
|
513 $key = '' |
|
514 ) { |
|
515 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
|
516 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
|
517 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); |
|
518 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); |
|
519 |
|
520 /* Input validation: */ |
|
521 if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS256_NPUBBYTES) { |
|
522 throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); |
|
523 } |
|
524 if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS256_KEYBYTES) { |
|
525 throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); |
|
526 } |
|
527 |
|
528 list($ct, $tag) = ParagonIE_Sodium_Core_AEGIS256::encrypt($plaintext, $assocData, $key, $nonce); |
|
529 return $ct . $tag; |
|
530 } |
|
531 |
|
532 /** |
|
533 * Return a secure random key for use with the AEGIS-256 |
|
534 * symmetric AEAD interface. |
|
535 * |
|
536 * @return string |
|
537 * @throws Exception |
|
538 * @throws Error |
|
539 */ |
|
540 public static function crypto_aead_aegis256_keygen() |
|
541 { |
|
542 return random_bytes(self::CRYPTO_AEAD_AEGIS256_KEYBYTES); |
|
300 } |
543 } |
301 |
544 |
302 /** |
545 /** |
303 * Is AES-256-GCM even available to use? |
546 * Is AES-256-GCM even available to use? |
304 * |
547 * |
349 */ |
592 */ |
350 public static function crypto_aead_aes256gcm_decrypt( |
593 public static function crypto_aead_aes256gcm_decrypt( |
351 $ciphertext = '', |
594 $ciphertext = '', |
352 $assocData = '', |
595 $assocData = '', |
353 $nonce = '', |
596 $nonce = '', |
597 #[\SensitiveParameter] |
|
354 $key = '' |
598 $key = '' |
355 ) { |
599 ) { |
356 if (!self::crypto_aead_aes256gcm_is_available()) { |
600 if (!self::crypto_aead_aes256gcm_is_available()) { |
357 throw new SodiumException('AES-256-GCM is not available'); |
601 throw new SodiumException('AES-256-GCM is not available'); |
358 } |
602 } |
406 * @throws SodiumException |
650 * @throws SodiumException |
407 * @throws TypeError |
651 * @throws TypeError |
408 * @psalm-suppress MixedArgument |
652 * @psalm-suppress MixedArgument |
409 */ |
653 */ |
410 public static function crypto_aead_aes256gcm_encrypt( |
654 public static function crypto_aead_aes256gcm_encrypt( |
655 #[\SensitiveParameter] |
|
411 $plaintext = '', |
656 $plaintext = '', |
412 $assocData = '', |
657 $assocData = '', |
413 $nonce = '', |
658 $nonce = '', |
659 #[\SensitiveParameter] |
|
414 $key = '' |
660 $key = '' |
415 ) { |
661 ) { |
416 if (!self::crypto_aead_aes256gcm_is_available()) { |
662 if (!self::crypto_aead_aes256gcm_is_available()) { |
417 throw new SodiumException('AES-256-GCM is not available'); |
663 throw new SodiumException('AES-256-GCM is not available'); |
418 } |
664 } |
482 */ |
728 */ |
483 public static function crypto_aead_chacha20poly1305_decrypt( |
729 public static function crypto_aead_chacha20poly1305_decrypt( |
484 $ciphertext = '', |
730 $ciphertext = '', |
485 $assocData = '', |
731 $assocData = '', |
486 $nonce = '', |
732 $nonce = '', |
733 #[\SensitiveParameter] |
|
487 $key = '' |
734 $key = '' |
488 ) { |
735 ) { |
489 /* Type checks: */ |
736 /* Type checks: */ |
490 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
737 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
491 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
738 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
559 * @throws SodiumException |
806 * @throws SodiumException |
560 * @throws TypeError |
807 * @throws TypeError |
561 * @psalm-suppress MixedArgument |
808 * @psalm-suppress MixedArgument |
562 */ |
809 */ |
563 public static function crypto_aead_chacha20poly1305_encrypt( |
810 public static function crypto_aead_chacha20poly1305_encrypt( |
811 #[\SensitiveParameter] |
|
564 $plaintext = '', |
812 $plaintext = '', |
565 $assocData = '', |
813 $assocData = '', |
566 $nonce = '', |
814 $nonce = '', |
815 #[\SensitiveParameter] |
|
567 $key = '' |
816 $key = '' |
568 ) { |
817 ) { |
569 /* Type checks: */ |
818 /* Type checks: */ |
570 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
819 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
571 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
820 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
636 */ |
885 */ |
637 public static function crypto_aead_chacha20poly1305_ietf_decrypt( |
886 public static function crypto_aead_chacha20poly1305_ietf_decrypt( |
638 $ciphertext = '', |
887 $ciphertext = '', |
639 $assocData = '', |
888 $assocData = '', |
640 $nonce = '', |
889 $nonce = '', |
890 #[\SensitiveParameter] |
|
641 $key = '' |
891 $key = '' |
642 ) { |
892 ) { |
643 /* Type checks: */ |
893 /* Type checks: */ |
644 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
894 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
645 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
895 ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); |
726 * @throws SodiumException |
976 * @throws SodiumException |
727 * @throws TypeError |
977 * @throws TypeError |
728 * @psalm-suppress MixedArgument |
978 * @psalm-suppress MixedArgument |
729 */ |
979 */ |
730 public static function crypto_aead_chacha20poly1305_ietf_encrypt( |
980 public static function crypto_aead_chacha20poly1305_ietf_encrypt( |
981 #[\SensitiveParameter] |
|
731 $plaintext = '', |
982 $plaintext = '', |
732 $assocData = '', |
983 $assocData = '', |
733 $nonce = '', |
984 $nonce = '', |
985 #[\SensitiveParameter] |
|
734 $key = '' |
986 $key = '' |
735 ) { |
987 ) { |
736 /* Type checks: */ |
988 /* Type checks: */ |
737 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
989 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
738 if (!is_null($assocData)) { |
990 if (!is_null($assocData)) { |
817 */ |
1069 */ |
818 public static function crypto_aead_xchacha20poly1305_ietf_decrypt( |
1070 public static function crypto_aead_xchacha20poly1305_ietf_decrypt( |
819 $ciphertext = '', |
1071 $ciphertext = '', |
820 $assocData = '', |
1072 $assocData = '', |
821 $nonce = '', |
1073 $nonce = '', |
1074 #[\SensitiveParameter] |
|
822 $key = '', |
1075 $key = '', |
823 $dontFallback = false |
1076 $dontFallback = false |
824 ) { |
1077 ) { |
825 /* Type checks: */ |
1078 /* Type checks: */ |
826 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
1079 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
889 * @throws SodiumException |
1142 * @throws SodiumException |
890 * @throws TypeError |
1143 * @throws TypeError |
891 * @psalm-suppress MixedArgument |
1144 * @psalm-suppress MixedArgument |
892 */ |
1145 */ |
893 public static function crypto_aead_xchacha20poly1305_ietf_encrypt( |
1146 public static function crypto_aead_xchacha20poly1305_ietf_encrypt( |
1147 #[\SensitiveParameter] |
|
894 $plaintext = '', |
1148 $plaintext = '', |
895 $assocData = '', |
1149 $assocData = '', |
896 $nonce = '', |
1150 $nonce = '', |
1151 #[\SensitiveParameter] |
|
897 $key = '', |
1152 $key = '', |
898 $dontFallback = false |
1153 $dontFallback = false |
899 ) { |
1154 ) { |
900 /* Type checks: */ |
1155 /* Type checks: */ |
901 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
1156 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
969 * @return string Message authentication code |
1224 * @return string Message authentication code |
970 * @throws SodiumException |
1225 * @throws SodiumException |
971 * @throws TypeError |
1226 * @throws TypeError |
972 * @psalm-suppress MixedArgument |
1227 * @psalm-suppress MixedArgument |
973 */ |
1228 */ |
974 public static function crypto_auth($message, $key) |
1229 public static function crypto_auth( |
975 { |
1230 $message, |
1231 #[\SensitiveParameter] |
|
1232 $key |
|
1233 ) { |
|
976 /* Type checks: */ |
1234 /* Type checks: */ |
977 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
1235 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
978 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); |
1236 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); |
979 |
1237 |
980 /* Input validation: */ |
1238 /* Input validation: */ |
1014 * @return bool TRUE if authenticated, FALSE otherwise |
1272 * @return bool TRUE if authenticated, FALSE otherwise |
1015 * @throws SodiumException |
1273 * @throws SodiumException |
1016 * @throws TypeError |
1274 * @throws TypeError |
1017 * @psalm-suppress MixedArgument |
1275 * @psalm-suppress MixedArgument |
1018 */ |
1276 */ |
1019 public static function crypto_auth_verify($mac, $message, $key) |
1277 public static function crypto_auth_verify( |
1020 { |
1278 $mac, |
1279 $message, |
|
1280 #[\SensitiveParameter] |
|
1281 $key |
|
1282 ) { |
|
1021 /* Type checks: */ |
1283 /* Type checks: */ |
1022 ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1); |
1284 ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1); |
1023 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); |
1285 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); |
1024 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
1286 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
1025 |
1287 |
1058 * @return string Ciphertext with 16-byte Poly1305 MAC |
1320 * @return string Ciphertext with 16-byte Poly1305 MAC |
1059 * @throws SodiumException |
1321 * @throws SodiumException |
1060 * @throws TypeError |
1322 * @throws TypeError |
1061 * @psalm-suppress MixedArgument |
1323 * @psalm-suppress MixedArgument |
1062 */ |
1324 */ |
1063 public static function crypto_box($plaintext, $nonce, $keypair) |
1325 public static function crypto_box( |
1064 { |
1326 $plaintext, |
1327 $nonce, |
|
1328 #[\SensitiveParameter] |
|
1329 $keypair |
|
1330 ) { |
|
1065 /* Type checks: */ |
1331 /* Type checks: */ |
1066 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
1332 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
1067 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
1333 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
1068 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); |
1334 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); |
1069 |
1335 |
1102 * decrypt |
1368 * decrypt |
1103 * @throws SodiumException |
1369 * @throws SodiumException |
1104 * @throws TypeError |
1370 * @throws TypeError |
1105 * @psalm-suppress MixedArgument |
1371 * @psalm-suppress MixedArgument |
1106 */ |
1372 */ |
1107 public static function crypto_box_seal($plaintext, $publicKey) |
1373 public static function crypto_box_seal( |
1108 { |
1374 #[\SensitiveParameter] |
1375 $plaintext, |
|
1376 $publicKey |
|
1377 ) { |
|
1109 /* Type checks: */ |
1378 /* Type checks: */ |
1110 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
1379 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
1111 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
1380 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
1112 |
1381 |
1113 /* Input validation: */ |
1382 /* Input validation: */ |
1140 * @throws TypeError |
1409 * @throws TypeError |
1141 * @psalm-suppress MixedArgument |
1410 * @psalm-suppress MixedArgument |
1142 * @psalm-suppress MixedInferredReturnType |
1411 * @psalm-suppress MixedInferredReturnType |
1143 * @psalm-suppress MixedReturnStatement |
1412 * @psalm-suppress MixedReturnStatement |
1144 */ |
1413 */ |
1145 public static function crypto_box_seal_open($ciphertext, $keypair) |
1414 public static function crypto_box_seal_open( |
1146 { |
1415 $ciphertext, |
1416 #[\SensitiveParameter] |
|
1417 $keypair |
|
1418 ) { |
|
1147 /* Type checks: */ |
1419 /* Type checks: */ |
1148 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
1420 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
1149 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2); |
1421 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2); |
1150 |
1422 |
1151 /* Input validation: */ |
1423 /* Input validation: */ |
1203 * @return string Keypair |
1475 * @return string Keypair |
1204 * @throws SodiumException |
1476 * @throws SodiumException |
1205 * @throws TypeError |
1477 * @throws TypeError |
1206 * @psalm-suppress MixedArgument |
1478 * @psalm-suppress MixedArgument |
1207 */ |
1479 */ |
1208 public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey) |
1480 public static function crypto_box_keypair_from_secretkey_and_publickey( |
1209 { |
1481 #[\SensitiveParameter] |
1482 $secretKey, |
|
1483 $publicKey |
|
1484 ) { |
|
1210 /* Type checks: */ |
1485 /* Type checks: */ |
1211 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
1486 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
1212 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
1487 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
1213 |
1488 |
1214 /* Input validation: */ |
1489 /* Input validation: */ |
1242 * @throws TypeError |
1517 * @throws TypeError |
1243 * @psalm-suppress MixedArgument |
1518 * @psalm-suppress MixedArgument |
1244 * @psalm-suppress MixedInferredReturnType |
1519 * @psalm-suppress MixedInferredReturnType |
1245 * @psalm-suppress MixedReturnStatement |
1520 * @psalm-suppress MixedReturnStatement |
1246 */ |
1521 */ |
1247 public static function crypto_box_open($ciphertext, $nonce, $keypair) |
1522 public static function crypto_box_open( |
1248 { |
1523 $ciphertext, |
1524 $nonce, |
|
1525 #[\SensitiveParameter] |
|
1526 $keypair |
|
1527 ) { |
|
1249 /* Type checks: */ |
1528 /* Type checks: */ |
1250 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
1529 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
1251 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
1530 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
1252 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); |
1531 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); |
1253 |
1532 |
1285 * @return string Your crypto_box public key |
1564 * @return string Your crypto_box public key |
1286 * @throws SodiumException |
1565 * @throws SodiumException |
1287 * @throws TypeError |
1566 * @throws TypeError |
1288 * @psalm-suppress MixedArgument |
1567 * @psalm-suppress MixedArgument |
1289 */ |
1568 */ |
1290 public static function crypto_box_publickey($keypair) |
1569 public static function crypto_box_publickey( |
1291 { |
1570 #[\SensitiveParameter] |
1571 $keypair |
|
1572 ) { |
|
1292 /* Type checks: */ |
1573 /* Type checks: */ |
1293 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
1574 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
1294 |
1575 |
1295 /* Input validation: */ |
1576 /* Input validation: */ |
1296 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { |
1577 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { |
1316 * @return string The corresponding X25519 public key |
1597 * @return string The corresponding X25519 public key |
1317 * @throws SodiumException |
1598 * @throws SodiumException |
1318 * @throws TypeError |
1599 * @throws TypeError |
1319 * @psalm-suppress MixedArgument |
1600 * @psalm-suppress MixedArgument |
1320 */ |
1601 */ |
1321 public static function crypto_box_publickey_from_secretkey($secretKey) |
1602 public static function crypto_box_publickey_from_secretkey( |
1322 { |
1603 #[\SensitiveParameter] |
1604 $secretKey |
|
1605 ) { |
|
1323 /* Type checks: */ |
1606 /* Type checks: */ |
1324 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
1607 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
1325 |
1608 |
1326 /* Input validation: */ |
1609 /* Input validation: */ |
1327 if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { |
1610 if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { |
1347 * @return string Your crypto_box secret key |
1630 * @return string Your crypto_box secret key |
1348 * @throws SodiumException |
1631 * @throws SodiumException |
1349 * @throws TypeError |
1632 * @throws TypeError |
1350 * @psalm-suppress MixedArgument |
1633 * @psalm-suppress MixedArgument |
1351 */ |
1634 */ |
1352 public static function crypto_box_secretkey($keypair) |
1635 public static function crypto_box_secretkey( |
1353 { |
1636 #[\SensitiveParameter] |
1637 $keypair |
|
1638 ) { |
|
1354 /* Type checks: */ |
1639 /* Type checks: */ |
1355 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
1640 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
1356 |
1641 |
1357 /* Input validation: */ |
1642 /* Input validation: */ |
1358 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { |
1643 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { |
1379 * @throws SodiumException |
1664 * @throws SodiumException |
1380 * @throws TypeError |
1665 * @throws TypeError |
1381 * @psalm-suppress MixedArgument |
1666 * @psalm-suppress MixedArgument |
1382 * @psalm-suppress UndefinedFunction |
1667 * @psalm-suppress UndefinedFunction |
1383 */ |
1668 */ |
1384 public static function crypto_box_seed_keypair($seed) |
1669 public static function crypto_box_seed_keypair( |
1385 { |
1670 #[\SensitiveParameter] |
1671 $seed |
|
1672 ) { |
|
1386 /* Type checks: */ |
1673 /* Type checks: */ |
1387 ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
1674 ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
1388 |
1675 |
1389 if (self::useNewSodiumAPI()) { |
1676 if (self::useNewSodiumAPI()) { |
1390 return (string) sodium_crypto_box_seed_keypair($seed); |
1677 return (string) sodium_crypto_box_seed_keypair($seed); |
1409 * @return string Raw binary |
1696 * @return string Raw binary |
1410 * @throws SodiumException |
1697 * @throws SodiumException |
1411 * @throws TypeError |
1698 * @throws TypeError |
1412 * @psalm-suppress MixedArgument |
1699 * @psalm-suppress MixedArgument |
1413 */ |
1700 */ |
1414 public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES) |
1701 public static function crypto_generichash( |
1415 { |
1702 $message, |
1703 #[\SensitiveParameter] |
|
1704 $key = '', |
|
1705 $length = self::CRYPTO_GENERICHASH_BYTES |
|
1706 ) { |
|
1416 /* Type checks: */ |
1707 /* Type checks: */ |
1417 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
1708 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
1418 if (is_null($key)) { |
1709 if (is_null($key)) { |
1419 $key = ''; |
1710 $key = ''; |
1420 } |
1711 } |
1453 * @throws TypeError |
1744 * @throws TypeError |
1454 * @psalm-suppress MixedArgument |
1745 * @psalm-suppress MixedArgument |
1455 * @psalm-suppress ReferenceConstraintViolation |
1746 * @psalm-suppress ReferenceConstraintViolation |
1456 * @psalm-suppress ConflictingReferenceConstraint |
1747 * @psalm-suppress ConflictingReferenceConstraint |
1457 */ |
1748 */ |
1458 public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES) |
1749 public static function crypto_generichash_final( |
1459 { |
1750 #[\SensitiveParameter] |
1751 &$ctx, |
|
1752 $length = self::CRYPTO_GENERICHASH_BYTES |
|
1753 ) { |
|
1460 /* Type checks: */ |
1754 /* Type checks: */ |
1461 ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1); |
1755 ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1); |
1462 ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2); |
1756 ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2); |
1463 |
1757 |
1464 if (self::useNewSodiumAPI()) { |
1758 if (self::useNewSodiumAPI()) { |
1498 * (To be 100% compatible with ext/libsodium) |
1792 * (To be 100% compatible with ext/libsodium) |
1499 * @throws SodiumException |
1793 * @throws SodiumException |
1500 * @throws TypeError |
1794 * @throws TypeError |
1501 * @psalm-suppress MixedArgument |
1795 * @psalm-suppress MixedArgument |
1502 */ |
1796 */ |
1503 public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES) |
1797 public static function crypto_generichash_init( |
1504 { |
1798 #[\SensitiveParameter] |
1799 $key = '', |
|
1800 $length = self::CRYPTO_GENERICHASH_BYTES |
|
1801 ) { |
|
1505 /* Type checks: */ |
1802 /* Type checks: */ |
1506 if (is_null($key)) { |
1803 if (is_null($key)) { |
1507 $key = ''; |
1804 $key = ''; |
1508 } |
1805 } |
1509 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1); |
1806 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1); |
1543 * @throws SodiumException |
1840 * @throws SodiumException |
1544 * @throws TypeError |
1841 * @throws TypeError |
1545 * @psalm-suppress MixedArgument |
1842 * @psalm-suppress MixedArgument |
1546 */ |
1843 */ |
1547 public static function crypto_generichash_init_salt_personal( |
1844 public static function crypto_generichash_init_salt_personal( |
1845 #[\SensitiveParameter] |
|
1548 $key = '', |
1846 $key = '', |
1549 $length = self::CRYPTO_GENERICHASH_BYTES, |
1847 $length = self::CRYPTO_GENERICHASH_BYTES, |
1550 $salt = '', |
1848 $salt = '', |
1551 $personal = '' |
1849 $personal = '' |
1552 ) { |
1850 ) { |
1589 * @throws SodiumException |
1887 * @throws SodiumException |
1590 * @throws TypeError |
1888 * @throws TypeError |
1591 * @psalm-suppress MixedArgument |
1889 * @psalm-suppress MixedArgument |
1592 * @psalm-suppress ReferenceConstraintViolation |
1890 * @psalm-suppress ReferenceConstraintViolation |
1593 */ |
1891 */ |
1594 public static function crypto_generichash_update(&$ctx, $message) |
1892 public static function crypto_generichash_update( |
1595 { |
1893 #[\SensitiveParameter] |
1894 &$ctx, |
|
1895 $message |
|
1896 ) { |
|
1596 /* Type checks: */ |
1897 /* Type checks: */ |
1597 ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1); |
1898 ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1); |
1598 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); |
1899 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); |
1599 |
1900 |
1600 if (self::useNewSodiumAPI()) { |
1901 if (self::useNewSodiumAPI()) { |
1633 */ |
1934 */ |
1634 public static function crypto_kdf_derive_from_key( |
1935 public static function crypto_kdf_derive_from_key( |
1635 $subkey_len, |
1936 $subkey_len, |
1636 $subkey_id, |
1937 $subkey_id, |
1637 $context, |
1938 $context, |
1939 #[\SensitiveParameter] |
|
1638 $key |
1940 $key |
1639 ) { |
1941 ) { |
1640 ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1); |
1942 ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1); |
1641 ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2); |
1943 ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2); |
1642 ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3); |
1944 ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3); |
1710 * @return string |
2012 * @return string |
1711 * @throws SodiumException |
2013 * @throws SodiumException |
1712 * @throws TypeError |
2014 * @throws TypeError |
1713 * @psalm-suppress MixedArgument |
2015 * @psalm-suppress MixedArgument |
1714 */ |
2016 */ |
1715 public static function crypto_kx($my_secret, $their_public, $client_public, $server_public, $dontFallback = false) |
2017 public static function crypto_kx( |
1716 { |
2018 #[\SensitiveParameter] |
2019 $my_secret, |
|
2020 $their_public, |
|
2021 $client_public, |
|
2022 $server_public, |
|
2023 $dontFallback = false |
|
2024 ) { |
|
1717 /* Type checks: */ |
2025 /* Type checks: */ |
1718 ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1); |
2026 ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1); |
1719 ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2); |
2027 ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2); |
1720 ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3); |
2028 ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3); |
1721 ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4); |
2029 ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4); |
1772 /** |
2080 /** |
1773 * @param string $seed |
2081 * @param string $seed |
1774 * @return string |
2082 * @return string |
1775 * @throws SodiumException |
2083 * @throws SodiumException |
1776 */ |
2084 */ |
1777 public static function crypto_kx_seed_keypair($seed) |
2085 public static function crypto_kx_seed_keypair( |
1778 { |
2086 #[\SensitiveParameter] |
2087 $seed |
|
2088 ) { |
|
1779 ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
2089 ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
1780 |
2090 |
1781 $seed = (string) $seed; |
2091 $seed = (string) $seed; |
1782 |
2092 |
1783 if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) { |
2093 if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) { |
1804 * @param string $keypair |
2114 * @param string $keypair |
1805 * @param string $serverPublicKey |
2115 * @param string $serverPublicKey |
1806 * @return array{0: string, 1: string} |
2116 * @return array{0: string, 1: string} |
1807 * @throws SodiumException |
2117 * @throws SodiumException |
1808 */ |
2118 */ |
1809 public static function crypto_kx_client_session_keys($keypair, $serverPublicKey) |
2119 public static function crypto_kx_client_session_keys( |
1810 { |
2120 #[\SensitiveParameter] |
2121 $keypair, |
|
2122 $serverPublicKey |
|
2123 ) { |
|
1811 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
2124 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
1812 ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2); |
2125 ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2); |
1813 |
2126 |
1814 $keypair = (string) $keypair; |
2127 $keypair = (string) $keypair; |
1815 $serverPublicKey = (string) $serverPublicKey; |
2128 $serverPublicKey = (string) $serverPublicKey; |
1846 * @param string $keypair |
2159 * @param string $keypair |
1847 * @param string $clientPublicKey |
2160 * @param string $clientPublicKey |
1848 * @return array{0: string, 1: string} |
2161 * @return array{0: string, 1: string} |
1849 * @throws SodiumException |
2162 * @throws SodiumException |
1850 */ |
2163 */ |
1851 public static function crypto_kx_server_session_keys($keypair, $clientPublicKey) |
2164 public static function crypto_kx_server_session_keys( |
1852 { |
2165 #[\SensitiveParameter] |
2166 $keypair, |
|
2167 $clientPublicKey |
|
2168 ) { |
|
1853 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
2169 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
1854 ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2); |
2170 ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2); |
1855 |
2171 |
1856 $keypair = (string) $keypair; |
2172 $keypair = (string) $keypair; |
1857 $clientPublicKey = (string) $clientPublicKey; |
2173 $clientPublicKey = (string) $clientPublicKey; |
1887 /** |
2203 /** |
1888 * @param string $kp |
2204 * @param string $kp |
1889 * @return string |
2205 * @return string |
1890 * @throws SodiumException |
2206 * @throws SodiumException |
1891 */ |
2207 */ |
1892 public static function crypto_kx_secretkey($kp) |
2208 public static function crypto_kx_secretkey( |
1893 { |
2209 #[\SensitiveParameter] |
2210 $kp |
|
2211 ) { |
|
1894 return ParagonIE_Sodium_Core_Util::substr( |
2212 return ParagonIE_Sodium_Core_Util::substr( |
1895 $kp, |
2213 $kp, |
1896 0, |
2214 0, |
1897 self::CRYPTO_KX_SECRETKEYBYTES |
2215 self::CRYPTO_KX_SECRETKEYBYTES |
1898 ); |
2216 ); |
1922 * @return string |
2240 * @return string |
1923 * @throws SodiumException |
2241 * @throws SodiumException |
1924 * @throws TypeError |
2242 * @throws TypeError |
1925 * @psalm-suppress MixedArgument |
2243 * @psalm-suppress MixedArgument |
1926 */ |
2244 */ |
1927 public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null) |
2245 public static function crypto_pwhash( |
1928 { |
2246 $outlen, |
2247 #[\SensitiveParameter] |
|
2248 $passwd, |
|
2249 $salt, |
|
2250 $opslimit, |
|
2251 $memlimit, |
|
2252 $alg = null |
|
2253 ) { |
|
1929 ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1); |
2254 ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1); |
1930 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2); |
2255 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2); |
1931 ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); |
2256 ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); |
1932 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4); |
2257 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4); |
1933 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5); |
2258 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5); |
1974 * @return string |
2299 * @return string |
1975 * @throws SodiumException |
2300 * @throws SodiumException |
1976 * @throws TypeError |
2301 * @throws TypeError |
1977 * @psalm-suppress MixedArgument |
2302 * @psalm-suppress MixedArgument |
1978 */ |
2303 */ |
1979 public static function crypto_pwhash_str($passwd, $opslimit, $memlimit) |
2304 public static function crypto_pwhash_str( |
1980 { |
2305 #[\SensitiveParameter] |
2306 $passwd, |
|
2307 $opslimit, |
|
2308 $memlimit |
|
2309 ) { |
|
1981 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2310 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
1982 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); |
2311 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); |
1983 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); |
2312 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); |
1984 |
2313 |
1985 if (self::useNewSodiumAPI()) { |
2314 if (self::useNewSodiumAPI()) { |
2001 * @param int $opslimit |
2330 * @param int $opslimit |
2002 * @param int $memlimit |
2331 * @param int $memlimit |
2003 * @return bool |
2332 * @return bool |
2004 * @throws SodiumException |
2333 * @throws SodiumException |
2005 */ |
2334 */ |
2006 public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit) |
2335 public static function crypto_pwhash_str_needs_rehash( |
2007 { |
2336 #[\SensitiveParameter] |
2337 $hash, |
|
2338 $opslimit, |
|
2339 $memlimit |
|
2340 ) { |
|
2008 ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1); |
2341 ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1); |
2009 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); |
2342 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); |
2010 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); |
2343 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); |
2011 |
2344 |
2012 // Just grab the first 4 pieces. |
2345 // Just grab the first 4 pieces. |
2030 * @return bool |
2363 * @return bool |
2031 * @throws SodiumException |
2364 * @throws SodiumException |
2032 * @throws TypeError |
2365 * @throws TypeError |
2033 * @psalm-suppress MixedArgument |
2366 * @psalm-suppress MixedArgument |
2034 */ |
2367 */ |
2035 public static function crypto_pwhash_str_verify($passwd, $hash) |
2368 public static function crypto_pwhash_str_verify( |
2036 { |
2369 #[\SensitiveParameter] |
2370 $passwd, |
|
2371 #[\SensitiveParameter] |
|
2372 $hash |
|
2373 ) { |
|
2037 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2374 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2038 ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2); |
2375 ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2); |
2039 |
2376 |
2040 if (self::useNewSodiumAPI()) { |
2377 if (self::useNewSodiumAPI()) { |
2041 return (bool) sodium_crypto_pwhash_str_verify($passwd, $hash); |
2378 return (bool) sodium_crypto_pwhash_str_verify($passwd, $hash); |
2057 * @param int $memlimit |
2394 * @param int $memlimit |
2058 * @return string |
2395 * @return string |
2059 * @throws SodiumException |
2396 * @throws SodiumException |
2060 * @throws TypeError |
2397 * @throws TypeError |
2061 */ |
2398 */ |
2062 public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit) |
2399 public static function crypto_pwhash_scryptsalsa208sha256( |
2063 { |
2400 $outlen, |
2401 #[\SensitiveParameter] |
|
2402 $passwd, |
|
2403 $salt, |
|
2404 $opslimit, |
|
2405 $memlimit |
|
2406 ) { |
|
2064 ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1); |
2407 ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1); |
2065 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2); |
2408 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2); |
2066 ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); |
2409 ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); |
2067 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4); |
2410 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4); |
2068 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5); |
2411 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5); |
2117 * @param int $memlimit |
2460 * @param int $memlimit |
2118 * @return string |
2461 * @return string |
2119 * @throws SodiumException |
2462 * @throws SodiumException |
2120 * @throws TypeError |
2463 * @throws TypeError |
2121 */ |
2464 */ |
2122 public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit) |
2465 public static function crypto_pwhash_scryptsalsa208sha256_str( |
2123 { |
2466 #[\SensitiveParameter] |
2467 $passwd, |
|
2468 $opslimit, |
|
2469 $memlimit |
|
2470 ) { |
|
2124 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2471 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2125 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); |
2472 ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); |
2126 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); |
2473 ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); |
2127 |
2474 |
2128 if (self::useNewSodiumAPI()) { |
2475 if (self::useNewSodiumAPI()) { |
2151 * @param string $hash |
2498 * @param string $hash |
2152 * @return bool |
2499 * @return bool |
2153 * @throws SodiumException |
2500 * @throws SodiumException |
2154 * @throws TypeError |
2501 * @throws TypeError |
2155 */ |
2502 */ |
2156 public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash) |
2503 public static function crypto_pwhash_scryptsalsa208sha256_str_verify( |
2157 { |
2504 #[\SensitiveParameter] |
2505 $passwd, |
|
2506 #[\SensitiveParameter] |
|
2507 $hash |
|
2508 ) { |
|
2158 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2509 ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); |
2159 ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2); |
2510 ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2); |
2160 |
2511 |
2161 if (self::useNewSodiumAPI()) { |
2512 if (self::useNewSodiumAPI()) { |
2162 return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify( |
2513 return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify( |
2188 * @return string |
2539 * @return string |
2189 * @throws SodiumException |
2540 * @throws SodiumException |
2190 * @throws TypeError |
2541 * @throws TypeError |
2191 * @psalm-suppress MixedArgument |
2542 * @psalm-suppress MixedArgument |
2192 */ |
2543 */ |
2193 public static function crypto_scalarmult($secretKey, $publicKey) |
2544 public static function crypto_scalarmult( |
2194 { |
2545 #[\SensitiveParameter] |
2546 $secretKey, |
|
2547 $publicKey |
|
2548 ) { |
|
2195 /* Type checks: */ |
2549 /* Type checks: */ |
2196 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
2550 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
2197 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
2551 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
2198 |
2552 |
2199 /* Input validation: */ |
2553 /* Input validation: */ |
2232 * @throws SodiumException |
2586 * @throws SodiumException |
2233 * @throws TypeError |
2587 * @throws TypeError |
2234 * @psalm-suppress TooFewArguments |
2588 * @psalm-suppress TooFewArguments |
2235 * @psalm-suppress MixedArgument |
2589 * @psalm-suppress MixedArgument |
2236 */ |
2590 */ |
2237 public static function crypto_scalarmult_base($secretKey) |
2591 public static function crypto_scalarmult_base( |
2238 { |
2592 #[\SensitiveParameter] |
2593 $secretKey |
|
2594 ) { |
|
2239 /* Type checks: */ |
2595 /* Type checks: */ |
2240 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
2596 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
2241 |
2597 |
2242 /* Input validation: */ |
2598 /* Input validation: */ |
2243 if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { |
2599 if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { |
2270 * @return string Ciphertext with Poly1305 MAC |
2626 * @return string Ciphertext with Poly1305 MAC |
2271 * @throws SodiumException |
2627 * @throws SodiumException |
2272 * @throws TypeError |
2628 * @throws TypeError |
2273 * @psalm-suppress MixedArgument |
2629 * @psalm-suppress MixedArgument |
2274 */ |
2630 */ |
2275 public static function crypto_secretbox($plaintext, $nonce, $key) |
2631 public static function crypto_secretbox( |
2276 { |
2632 #[\SensitiveParameter] |
2633 $plaintext, |
|
2634 $nonce, |
|
2635 #[\SensitiveParameter] |
|
2636 $key |
|
2637 ) { |
|
2277 /* Type checks: */ |
2638 /* Type checks: */ |
2278 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
2639 ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
2279 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2640 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2280 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2641 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2281 |
2642 |
2310 * @throws TypeError |
2671 * @throws TypeError |
2311 * @psalm-suppress MixedArgument |
2672 * @psalm-suppress MixedArgument |
2312 * @psalm-suppress MixedInferredReturnType |
2673 * @psalm-suppress MixedInferredReturnType |
2313 * @psalm-suppress MixedReturnStatement |
2674 * @psalm-suppress MixedReturnStatement |
2314 */ |
2675 */ |
2315 public static function crypto_secretbox_open($ciphertext, $nonce, $key) |
2676 public static function crypto_secretbox_open( |
2316 { |
2677 $ciphertext, |
2678 $nonce, |
|
2679 #[\SensitiveParameter] |
|
2680 $key |
|
2681 ) { |
|
2317 /* Type checks: */ |
2682 /* Type checks: */ |
2318 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
2683 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
2319 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2684 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2320 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2685 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2321 |
2686 |
2396 * @return string Original plaintext message |
2761 * @return string Original plaintext message |
2397 * @throws SodiumException |
2762 * @throws SodiumException |
2398 * @throws TypeError |
2763 * @throws TypeError |
2399 * @psalm-suppress MixedArgument |
2764 * @psalm-suppress MixedArgument |
2400 */ |
2765 */ |
2401 public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
2766 public static function crypto_secretbox_xchacha20poly1305_open( |
2402 { |
2767 $ciphertext, |
2768 $nonce, |
|
2769 #[\SensitiveParameter] |
|
2770 $key |
|
2771 ) { |
|
2403 /* Type checks: */ |
2772 /* Type checks: */ |
2404 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
2773 ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
2405 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2774 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2406 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2775 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2407 |
2776 |
2423 * @param string $key |
2792 * @param string $key |
2424 * @return array<int, string> Returns a state and a header. |
2793 * @return array<int, string> Returns a state and a header. |
2425 * @throws Exception |
2794 * @throws Exception |
2426 * @throws SodiumException |
2795 * @throws SodiumException |
2427 */ |
2796 */ |
2428 public static function crypto_secretstream_xchacha20poly1305_init_push($key) |
2797 public static function crypto_secretstream_xchacha20poly1305_init_push( |
2429 { |
2798 #[\SensitiveParameter] |
2799 $key |
|
2800 ) { |
|
2430 if (PHP_INT_SIZE === 4) { |
2801 if (PHP_INT_SIZE === 4) { |
2431 return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key); |
2802 return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key); |
2432 } |
2803 } |
2433 return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key); |
2804 return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key); |
2434 } |
2805 } |
2437 * @param string $header |
2808 * @param string $header |
2438 * @param string $key |
2809 * @param string $key |
2439 * @return string Returns a state. |
2810 * @return string Returns a state. |
2440 * @throws Exception |
2811 * @throws Exception |
2441 */ |
2812 */ |
2442 public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key) |
2813 public static function crypto_secretstream_xchacha20poly1305_init_pull( |
2443 { |
2814 $header, |
2815 #[\SensitiveParameter] |
|
2816 $key |
|
2817 ) { |
|
2444 if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) { |
2818 if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) { |
2445 throw new SodiumException( |
2819 throw new SodiumException( |
2446 'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes' |
2820 'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes' |
2447 ); |
2821 ); |
2448 } |
2822 } |
2458 * @param string $aad |
2832 * @param string $aad |
2459 * @param int $tag |
2833 * @param int $tag |
2460 * @return string |
2834 * @return string |
2461 * @throws SodiumException |
2835 * @throws SodiumException |
2462 */ |
2836 */ |
2463 public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) |
2837 public static function crypto_secretstream_xchacha20poly1305_push( |
2464 { |
2838 #[\SensitiveParameter] |
2839 &$state, |
|
2840 #[\SensitiveParameter] |
|
2841 $msg, |
|
2842 $aad = '', |
|
2843 $tag = 0 |
|
2844 ) { |
|
2465 if (PHP_INT_SIZE === 4) { |
2845 if (PHP_INT_SIZE === 4) { |
2466 return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push( |
2846 return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push( |
2467 $state, |
2847 $state, |
2468 $msg, |
2848 $msg, |
2469 $aad, |
2849 $aad, |
2483 * @param string $msg |
2863 * @param string $msg |
2484 * @param string $aad |
2864 * @param string $aad |
2485 * @return bool|array{0: string, 1: int} |
2865 * @return bool|array{0: string, 1: int} |
2486 * @throws SodiumException |
2866 * @throws SodiumException |
2487 */ |
2867 */ |
2488 public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '') |
2868 public static function crypto_secretstream_xchacha20poly1305_pull( |
2489 { |
2869 #[\SensitiveParameter] |
2870 &$state, |
|
2871 $msg, |
|
2872 $aad = '' |
|
2873 ) { |
|
2490 if (PHP_INT_SIZE === 4) { |
2874 if (PHP_INT_SIZE === 4) { |
2491 return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull( |
2875 return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull( |
2492 $state, |
2876 $state, |
2493 $msg, |
2877 $msg, |
2494 $aad |
2878 $aad |
2513 /** |
2897 /** |
2514 * @param string $state |
2898 * @param string $state |
2515 * @return void |
2899 * @return void |
2516 * @throws SodiumException |
2900 * @throws SodiumException |
2517 */ |
2901 */ |
2518 public static function crypto_secretstream_xchacha20poly1305_rekey(&$state) |
2902 public static function crypto_secretstream_xchacha20poly1305_rekey( |
2519 { |
2903 #[\SensitiveParameter] |
2904 &$state |
|
2905 ) { |
|
2520 if (PHP_INT_SIZE === 4) { |
2906 if (PHP_INT_SIZE === 4) { |
2521 ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state); |
2907 ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state); |
2522 } else { |
2908 } else { |
2523 ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state); |
2909 ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state); |
2524 } |
2910 } |
2534 * @throws TypeError |
2920 * @throws TypeError |
2535 * @psalm-suppress MixedArgument |
2921 * @psalm-suppress MixedArgument |
2536 * @psalm-suppress MixedInferredReturnType |
2922 * @psalm-suppress MixedInferredReturnType |
2537 * @psalm-suppress MixedReturnStatement |
2923 * @psalm-suppress MixedReturnStatement |
2538 */ |
2924 */ |
2539 public static function crypto_shorthash($message, $key) |
2925 public static function crypto_shorthash( |
2540 { |
2926 $message, |
2927 #[\SensitiveParameter] |
|
2928 $key |
|
2929 ) { |
|
2541 /* Type checks: */ |
2930 /* Type checks: */ |
2542 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
2931 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
2543 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); |
2932 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); |
2544 |
2933 |
2545 /* Input validation: */ |
2934 /* Input validation: */ |
2584 * @throws TypeError |
2973 * @throws TypeError |
2585 * @psalm-suppress MixedArgument |
2974 * @psalm-suppress MixedArgument |
2586 * @psalm-suppress MixedInferredReturnType |
2975 * @psalm-suppress MixedInferredReturnType |
2587 * @psalm-suppress MixedReturnStatement |
2976 * @psalm-suppress MixedReturnStatement |
2588 */ |
2977 */ |
2589 public static function crypto_sign($message, $secretKey) |
2978 public static function crypto_sign( |
2590 { |
2979 $message, |
2980 #[\SensitiveParameter] |
|
2981 $secretKey |
|
2982 ) { |
|
2591 /* Type checks: */ |
2983 /* Type checks: */ |
2592 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
2984 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
2593 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2); |
2985 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2); |
2594 |
2986 |
2595 /* Input validation: */ |
2987 /* Input validation: */ |
2620 * @throws TypeError |
3012 * @throws TypeError |
2621 * @psalm-suppress MixedArgument |
3013 * @psalm-suppress MixedArgument |
2622 * @psalm-suppress MixedInferredReturnType |
3014 * @psalm-suppress MixedInferredReturnType |
2623 * @psalm-suppress MixedReturnStatement |
3015 * @psalm-suppress MixedReturnStatement |
2624 */ |
3016 */ |
2625 public static function crypto_sign_open($signedMessage, $publicKey) |
3017 public static function crypto_sign_open( |
2626 { |
3018 $signedMessage, |
3019 $publicKey |
|
3020 ) { |
|
2627 /* Type checks: */ |
3021 /* Type checks: */ |
2628 ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1); |
3022 ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1); |
2629 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
3023 ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); |
2630 |
3024 |
2631 /* Input validation: */ |
3025 /* Input validation: */ |
2677 * @param string $sk |
3071 * @param string $sk |
2678 * @param string $pk |
3072 * @param string $pk |
2679 * @return string |
3073 * @return string |
2680 * @throws SodiumException |
3074 * @throws SodiumException |
2681 */ |
3075 */ |
2682 public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk) |
3076 public static function crypto_sign_keypair_from_secretkey_and_publickey( |
2683 { |
3077 #[\SensitiveParameter] |
3078 $sk, |
|
3079 $pk |
|
3080 ) { |
|
2684 ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1); |
3081 ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1); |
2685 ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1); |
3082 ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1); |
2686 $sk = (string) $sk; |
3083 $sk = (string) $sk; |
2687 $pk = (string) $pk; |
3084 $pk = (string) $pk; |
2688 |
3085 |
2706 * @return string Keypair |
3103 * @return string Keypair |
2707 * @throws SodiumException |
3104 * @throws SodiumException |
2708 * @throws TypeError |
3105 * @throws TypeError |
2709 * @psalm-suppress MixedArgument |
3106 * @psalm-suppress MixedArgument |
2710 */ |
3107 */ |
2711 public static function crypto_sign_seed_keypair($seed) |
3108 public static function crypto_sign_seed_keypair( |
2712 { |
3109 #[\SensitiveParameter] |
3110 $seed |
|
3111 ) { |
|
2713 ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
3112 ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
2714 |
3113 |
2715 if (self::useNewSodiumAPI()) { |
3114 if (self::useNewSodiumAPI()) { |
2716 return sodium_crypto_sign_seed_keypair($seed); |
3115 return sodium_crypto_sign_seed_keypair($seed); |
2717 } |
3116 } |
2735 * @return string Public key |
3134 * @return string Public key |
2736 * @throws SodiumException |
3135 * @throws SodiumException |
2737 * @throws TypeError |
3136 * @throws TypeError |
2738 * @psalm-suppress MixedArgument |
3137 * @psalm-suppress MixedArgument |
2739 */ |
3138 */ |
2740 public static function crypto_sign_publickey($keypair) |
3139 public static function crypto_sign_publickey( |
2741 { |
3140 #[\SensitiveParameter] |
3141 $keypair |
|
3142 ) { |
|
2742 /* Type checks: */ |
3143 /* Type checks: */ |
2743 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
3144 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
2744 |
3145 |
2745 /* Input validation: */ |
3146 /* Input validation: */ |
2746 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) { |
3147 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) { |
2766 * @return string The corresponding Ed25519 public key |
3167 * @return string The corresponding Ed25519 public key |
2767 * @throws SodiumException |
3168 * @throws SodiumException |
2768 * @throws TypeError |
3169 * @throws TypeError |
2769 * @psalm-suppress MixedArgument |
3170 * @psalm-suppress MixedArgument |
2770 */ |
3171 */ |
2771 public static function crypto_sign_publickey_from_secretkey($secretKey) |
3172 public static function crypto_sign_publickey_from_secretkey( |
2772 { |
3173 #[\SensitiveParameter] |
3174 $secretKey |
|
3175 ) { |
|
2773 /* Type checks: */ |
3176 /* Type checks: */ |
2774 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
3177 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); |
2775 |
3178 |
2776 /* Input validation: */ |
3179 /* Input validation: */ |
2777 if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) { |
3180 if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) { |
2797 * @return string Secret key |
3200 * @return string Secret key |
2798 * @throws SodiumException |
3201 * @throws SodiumException |
2799 * @throws TypeError |
3202 * @throws TypeError |
2800 * @psalm-suppress MixedArgument |
3203 * @psalm-suppress MixedArgument |
2801 */ |
3204 */ |
2802 public static function crypto_sign_secretkey($keypair) |
3205 public static function crypto_sign_secretkey( |
2803 { |
3206 #[\SensitiveParameter] |
3207 $keypair |
|
3208 ) { |
|
2804 /* Type checks: */ |
3209 /* Type checks: */ |
2805 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
3210 ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); |
2806 |
3211 |
2807 /* Input validation: */ |
3212 /* Input validation: */ |
2808 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) { |
3213 if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) { |
2831 * @return string Digital signature |
3236 * @return string Digital signature |
2832 * @throws SodiumException |
3237 * @throws SodiumException |
2833 * @throws TypeError |
3238 * @throws TypeError |
2834 * @psalm-suppress MixedArgument |
3239 * @psalm-suppress MixedArgument |
2835 */ |
3240 */ |
2836 public static function crypto_sign_detached($message, $secretKey) |
3241 public static function crypto_sign_detached( |
2837 { |
3242 $message, |
3243 #[\SensitiveParameter] |
|
3244 $secretKey |
|
3245 ) { |
|
2838 /* Type checks: */ |
3246 /* Type checks: */ |
2839 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3247 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
2840 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2); |
3248 ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2); |
2841 |
3249 |
2842 /* Input validation: */ |
3250 /* Input validation: */ |
2939 * @return string |
3347 * @return string |
2940 * @throws SodiumException |
3348 * @throws SodiumException |
2941 * @throws TypeError |
3349 * @throws TypeError |
2942 * @psalm-suppress MixedArgument |
3350 * @psalm-suppress MixedArgument |
2943 */ |
3351 */ |
2944 public static function crypto_sign_ed25519_sk_to_curve25519($sk) |
3352 public static function crypto_sign_ed25519_sk_to_curve25519( |
2945 { |
3353 #[\SensitiveParameter] |
3354 $sk |
|
3355 ) { |
|
2946 /* Type checks: */ |
3356 /* Type checks: */ |
2947 ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1); |
3357 ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1); |
2948 |
3358 |
2949 /* Input validation: */ |
3359 /* Input validation: */ |
2950 if (ParagonIE_Sodium_Core_Util::strlen($sk) < self::CRYPTO_SIGN_SEEDBYTES) { |
3360 if (ParagonIE_Sodium_Core_Util::strlen($sk) < self::CRYPTO_SIGN_SEEDBYTES) { |
2981 * optional for security) |
3391 * optional for security) |
2982 * @throws SodiumException |
3392 * @throws SodiumException |
2983 * @throws TypeError |
3393 * @throws TypeError |
2984 * @psalm-suppress MixedArgument |
3394 * @psalm-suppress MixedArgument |
2985 */ |
3395 */ |
2986 public static function crypto_stream($len, $nonce, $key) |
3396 public static function crypto_stream( |
2987 { |
3397 $len, |
3398 $nonce, |
|
3399 #[\SensitiveParameter] |
|
3400 $key |
|
3401 ) { |
|
2988 /* Type checks: */ |
3402 /* Type checks: */ |
2989 ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); |
3403 ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); |
2990 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3404 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
2991 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3405 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
2992 |
3406 |
3028 * Encrypt then MAC) |
3442 * Encrypt then MAC) |
3029 * @throws SodiumException |
3443 * @throws SodiumException |
3030 * @throws TypeError |
3444 * @throws TypeError |
3031 * @psalm-suppress MixedArgument |
3445 * @psalm-suppress MixedArgument |
3032 */ |
3446 */ |
3033 public static function crypto_stream_xor($message, $nonce, $key) |
3447 public static function crypto_stream_xor( |
3034 { |
3448 #[\SensitiveParameter] |
3449 $message, |
|
3450 $nonce, |
|
3451 #[\SensitiveParameter] |
|
3452 $key |
|
3453 ) { |
|
3035 /* Type checks: */ |
3454 /* Type checks: */ |
3036 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3455 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3037 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3456 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3038 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3457 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3039 |
3458 |
3083 * optional for security) |
3502 * optional for security) |
3084 * @throws SodiumException |
3503 * @throws SodiumException |
3085 * @throws TypeError |
3504 * @throws TypeError |
3086 * @psalm-suppress MixedArgument |
3505 * @psalm-suppress MixedArgument |
3087 */ |
3506 */ |
3088 public static function crypto_stream_xchacha20($len, $nonce, $key, $dontFallback = false) |
3507 public static function crypto_stream_xchacha20( |
3089 { |
3508 $len, |
3509 $nonce, |
|
3510 #[\SensitiveParameter] |
|
3511 $key, |
|
3512 $dontFallback = false |
|
3513 ) { |
|
3090 /* Type checks: */ |
3514 /* Type checks: */ |
3091 ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); |
3515 ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); |
3092 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3516 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3093 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3517 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3094 |
3518 |
3128 * @param bool $dontFallback |
3552 * @param bool $dontFallback |
3129 * @throws SodiumException |
3553 * @throws SodiumException |
3130 * @throws TypeError |
3554 * @throws TypeError |
3131 * @psalm-suppress MixedArgument |
3555 * @psalm-suppress MixedArgument |
3132 */ |
3556 */ |
3133 public static function crypto_stream_xchacha20_xor($message, $nonce, $key, $dontFallback = false) |
3557 public static function crypto_stream_xchacha20_xor( |
3134 { |
3558 #[\SensitiveParameter] |
3559 $message, |
|
3560 $nonce, |
|
3561 #[\SensitiveParameter] |
|
3562 $key, |
|
3563 $dontFallback = false |
|
3564 ) { |
|
3135 /* Type checks: */ |
3565 /* Type checks: */ |
3136 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3566 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3137 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3567 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3138 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3568 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
3139 |
3569 |
3174 * @param bool $dontFallback |
3604 * @param bool $dontFallback |
3175 * @throws SodiumException |
3605 * @throws SodiumException |
3176 * @throws TypeError |
3606 * @throws TypeError |
3177 * @psalm-suppress MixedArgument |
3607 * @psalm-suppress MixedArgument |
3178 */ |
3608 */ |
3179 public static function crypto_stream_xchacha20_xor_ic($message, $nonce, $counter, $key, $dontFallback = false) |
3609 public static function crypto_stream_xchacha20_xor_ic( |
3180 { |
3610 #[\SensitiveParameter] |
3611 $message, |
|
3612 $nonce, |
|
3613 $counter, |
|
3614 #[\SensitiveParameter] |
|
3615 $key, |
|
3616 $dontFallback = false |
|
3617 ) { |
|
3181 /* Type checks: */ |
3618 /* Type checks: */ |
3182 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3619 ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
3183 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3620 ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
3184 ParagonIE_Sodium_Core_Util::declareScalarType($counter, 'int', 3); |
3621 ParagonIE_Sodium_Core_Util::declareScalarType($counter, 'int', 3); |
3185 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); |
3622 ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); |
3224 * @throws SodiumException |
3661 * @throws SodiumException |
3225 * @throws TypeError |
3662 * @throws TypeError |
3226 * @psalm-suppress TooFewArguments |
3663 * @psalm-suppress TooFewArguments |
3227 * @psalm-suppress MixedArgument |
3664 * @psalm-suppress MixedArgument |
3228 */ |
3665 */ |
3229 public static function hex2bin($string, $ignore = '') |
3666 public static function hex2bin( |
3230 { |
3667 #[\SensitiveParameter] |
3668 $string, |
|
3669 $ignore = '' |
|
3670 ) { |
|
3231 /* Type checks: */ |
3671 /* Type checks: */ |
3232 ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); |
3672 ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); |
3233 ParagonIE_Sodium_Core_Util::declareScalarType($ignore, 'string', 2); |
3673 ParagonIE_Sodium_Core_Util::declareScalarType($ignore, 'string', 2); |
3234 |
3674 |
3235 if (self::useNewSodiumAPI()) { |
3675 if (self::useNewSodiumAPI()) { |
3251 * @return void |
3691 * @return void |
3252 * @throws SodiumException |
3692 * @throws SodiumException |
3253 * @throws TypeError |
3693 * @throws TypeError |
3254 * @psalm-suppress MixedArgument |
3694 * @psalm-suppress MixedArgument |
3255 */ |
3695 */ |
3256 public static function increment(&$var) |
3696 public static function increment( |
3257 { |
3697 #[\SensitiveParameter] |
3698 &$var |
|
3699 ) { |
|
3258 /* Type checks: */ |
3700 /* Type checks: */ |
3259 ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); |
3701 ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); |
3260 |
3702 |
3261 if (self::useNewSodiumAPI()) { |
3703 if (self::useNewSodiumAPI()) { |
3262 sodium_increment($var); |
3704 sodium_increment($var); |
3285 * @param string $str |
3727 * @param string $str |
3286 * @return bool |
3728 * @return bool |
3287 * |
3729 * |
3288 * @throws SodiumException |
3730 * @throws SodiumException |
3289 */ |
3731 */ |
3290 public static function is_zero($str) |
3732 public static function is_zero( |
3291 { |
3733 #[\SensitiveParameter] |
3734 $str |
|
3735 ) { |
|
3292 $d = 0; |
3736 $d = 0; |
3293 for ($i = 0; $i < 32; ++$i) { |
3737 for ($i = 0; $i < 32; ++$i) { |
3294 $d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]); |
3738 $d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]); |
3295 } |
3739 } |
3296 return ((($d - 1) >> 31) & 1) === 1; |
3740 return ((($d - 1) >> 31) & 1) === 1; |
3340 * @return int |
3784 * @return int |
3341 * @throws SodiumException |
3785 * @throws SodiumException |
3342 * @throws TypeError |
3786 * @throws TypeError |
3343 * @psalm-suppress MixedArgument |
3787 * @psalm-suppress MixedArgument |
3344 */ |
3788 */ |
3345 public static function memcmp($left, $right) |
3789 public static function memcmp( |
3346 { |
3790 #[\SensitiveParameter] |
3791 $left, |
|
3792 #[\SensitiveParameter] |
|
3793 $right |
|
3794 ) { |
|
3347 /* Type checks: */ |
3795 /* Type checks: */ |
3348 ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1); |
3796 ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1); |
3349 ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2); |
3797 ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2); |
3350 |
3798 |
3351 if (self::useNewSodiumAPI()) { |
3799 if (self::useNewSodiumAPI()) { |
3369 * @return void |
3817 * @return void |
3370 * @throws SodiumException (Unless libsodium is installed) |
3818 * @throws SodiumException (Unless libsodium is installed) |
3371 * @throws TypeError |
3819 * @throws TypeError |
3372 * @psalm-suppress TooFewArguments |
3820 * @psalm-suppress TooFewArguments |
3373 */ |
3821 */ |
3374 public static function memzero(&$var) |
3822 public static function memzero( |
3375 { |
3823 #[\SensitiveParameter] |
3824 &$var |
|
3825 ) { |
|
3376 /* Type checks: */ |
3826 /* Type checks: */ |
3377 ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); |
3827 ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); |
3378 |
3828 |
3379 if (self::useNewSodiumAPI()) { |
3829 if (self::useNewSodiumAPI()) { |
3380 /** @psalm-suppress MixedArgument */ |
3830 /** @psalm-suppress MixedArgument */ |
3400 * @param int $blockSize |
3850 * @param int $blockSize |
3401 * @param bool $dontFallback |
3851 * @param bool $dontFallback |
3402 * @return string |
3852 * @return string |
3403 * @throws SodiumException |
3853 * @throws SodiumException |
3404 */ |
3854 */ |
3405 public static function pad($unpadded, $blockSize, $dontFallback = false) |
3855 public static function pad( |
3406 { |
3856 #[\SensitiveParameter] |
3857 $unpadded, |
|
3858 $blockSize, |
|
3859 $dontFallback = false |
|
3860 ) { |
|
3407 /* Type checks: */ |
3861 /* Type checks: */ |
3408 ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1); |
3862 ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1); |
3409 ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2); |
3863 ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2); |
3410 |
3864 |
3411 $unpadded = (string) $unpadded; |
3865 $unpadded = (string) $unpadded; |
3486 * @param int $blockSize |
3940 * @param int $blockSize |
3487 * @param bool $dontFallback |
3941 * @param bool $dontFallback |
3488 * @return string |
3942 * @return string |
3489 * @throws SodiumException |
3943 * @throws SodiumException |
3490 */ |
3944 */ |
3491 public static function unpad($padded, $blockSize, $dontFallback = false) |
3945 public static function unpad( |
3492 { |
3946 #[\SensitiveParameter] |
3947 $padded, |
|
3948 $blockSize, |
|
3949 $dontFallback = false |
|
3950 ) { |
|
3493 /* Type checks: */ |
3951 /* Type checks: */ |
3494 ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1); |
3952 ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1); |
3495 ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2); |
3953 ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2); |
3496 |
3954 |
3497 $padded = (string) $padded; |
3955 $padded = (string) $padded; |
3641 * @param string $p |
4099 * @param string $p |
3642 * @param bool $dontFallback |
4100 * @param bool $dontFallback |
3643 * @return bool |
4101 * @return bool |
3644 * @throws SodiumException |
4102 * @throws SodiumException |
3645 */ |
4103 */ |
3646 public static function ristretto255_is_valid_point($p, $dontFallback = false) |
4104 public static function ristretto255_is_valid_point( |
3647 { |
4105 #[\SensitiveParameter] |
4106 $p, |
|
4107 $dontFallback = false |
|
4108 ) { |
|
3648 if (self::useNewSodiumAPI() && !$dontFallback) { |
4109 if (self::useNewSodiumAPI() && !$dontFallback) { |
3649 return sodium_crypto_core_ristretto255_is_valid_point($p); |
4110 return sodium_crypto_core_ristretto255_is_valid_point($p); |
3650 } |
4111 } |
3651 try { |
4112 try { |
3652 $r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes($p); |
4113 $r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes($p); |
3665 * @param string $q |
4126 * @param string $q |
3666 * @param bool $dontFallback |
4127 * @param bool $dontFallback |
3667 * @return string |
4128 * @return string |
3668 * @throws SodiumException |
4129 * @throws SodiumException |
3669 */ |
4130 */ |
3670 public static function ristretto255_add($p, $q, $dontFallback = false) |
4131 public static function ristretto255_add( |
3671 { |
4132 #[\SensitiveParameter] |
4133 $p, |
|
4134 #[\SensitiveParameter] |
|
4135 $q, |
|
4136 $dontFallback = false |
|
4137 ) { |
|
3672 if (self::useNewSodiumAPI() && !$dontFallback) { |
4138 if (self::useNewSodiumAPI() && !$dontFallback) { |
3673 return sodium_crypto_core_ristretto255_add($p, $q); |
4139 return sodium_crypto_core_ristretto255_add($p, $q); |
3674 } |
4140 } |
3675 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add($p, $q); |
4141 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add($p, $q); |
3676 } |
4142 } |
3680 * @param string $q |
4146 * @param string $q |
3681 * @param bool $dontFallback |
4147 * @param bool $dontFallback |
3682 * @return string |
4148 * @return string |
3683 * @throws SodiumException |
4149 * @throws SodiumException |
3684 */ |
4150 */ |
3685 public static function ristretto255_sub($p, $q, $dontFallback = false) |
4151 public static function ristretto255_sub( |
3686 { |
4152 #[\SensitiveParameter] |
4153 $p, |
|
4154 #[\SensitiveParameter] |
|
4155 $q, |
|
4156 $dontFallback = false |
|
4157 ) { |
|
3687 if (self::useNewSodiumAPI() && !$dontFallback) { |
4158 if (self::useNewSodiumAPI() && !$dontFallback) { |
3688 return sodium_crypto_core_ristretto255_sub($p, $q); |
4159 return sodium_crypto_core_ristretto255_sub($p, $q); |
3689 } |
4160 } |
3690 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub($p, $q); |
4161 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub($p, $q); |
3691 } |
4162 } |
3695 * @param bool $dontFallback |
4166 * @param bool $dontFallback |
3696 * @return string |
4167 * @return string |
3697 * |
4168 * |
3698 * @throws SodiumException |
4169 * @throws SodiumException |
3699 */ |
4170 */ |
3700 public static function ristretto255_from_hash($r, $dontFallback = false) |
4171 public static function ristretto255_from_hash( |
3701 { |
4172 #[\SensitiveParameter] |
4173 $r, |
|
4174 $dontFallback = false |
|
4175 ) { |
|
3702 if (self::useNewSodiumAPI() && !$dontFallback) { |
4176 if (self::useNewSodiumAPI() && !$dontFallback) { |
3703 return sodium_crypto_core_ristretto255_from_hash($r); |
4177 return sodium_crypto_core_ristretto255_from_hash($r); |
3704 } |
4178 } |
3705 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash($r); |
4179 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash($r); |
3706 } |
4180 } |
3737 * @param string $s |
4211 * @param string $s |
3738 * @param bool $dontFallback |
4212 * @param bool $dontFallback |
3739 * @return string |
4213 * @return string |
3740 * @throws SodiumException |
4214 * @throws SodiumException |
3741 */ |
4215 */ |
3742 public static function ristretto255_scalar_invert($s, $dontFallback = false) |
4216 public static function ristretto255_scalar_invert( |
3743 { |
4217 #[\SensitiveParameter] |
4218 $s, |
|
4219 $dontFallback = false |
|
4220 ) { |
|
3744 if (self::useNewSodiumAPI() && !$dontFallback) { |
4221 if (self::useNewSodiumAPI() && !$dontFallback) { |
3745 return sodium_crypto_core_ristretto255_scalar_invert($s); |
4222 return sodium_crypto_core_ristretto255_scalar_invert($s); |
3746 } |
4223 } |
3747 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert($s); |
4224 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert($s); |
3748 } |
4225 } |
3750 * @param string $s |
4227 * @param string $s |
3751 * @param bool $dontFallback |
4228 * @param bool $dontFallback |
3752 * @return string |
4229 * @return string |
3753 * @throws SodiumException |
4230 * @throws SodiumException |
3754 */ |
4231 */ |
3755 public static function ristretto255_scalar_negate($s, $dontFallback = false) |
4232 public static function ristretto255_scalar_negate( |
3756 { |
4233 #[\SensitiveParameter] |
4234 $s, |
|
4235 $dontFallback = false |
|
4236 ) { |
|
3757 if (self::useNewSodiumAPI() && !$dontFallback) { |
4237 if (self::useNewSodiumAPI() && !$dontFallback) { |
3758 return sodium_crypto_core_ristretto255_scalar_negate($s); |
4238 return sodium_crypto_core_ristretto255_scalar_negate($s); |
3759 } |
4239 } |
3760 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate($s); |
4240 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate($s); |
3761 } |
4241 } |
3764 * @param string $s |
4244 * @param string $s |
3765 * @param bool $dontFallback |
4245 * @param bool $dontFallback |
3766 * @return string |
4246 * @return string |
3767 * @throws SodiumException |
4247 * @throws SodiumException |
3768 */ |
4248 */ |
3769 public static function ristretto255_scalar_complement($s, $dontFallback = false) |
4249 public static function ristretto255_scalar_complement( |
3770 { |
4250 #[\SensitiveParameter] |
4251 $s, |
|
4252 $dontFallback = false |
|
4253 ) { |
|
3771 if (self::useNewSodiumAPI() && !$dontFallback) { |
4254 if (self::useNewSodiumAPI() && !$dontFallback) { |
3772 return sodium_crypto_core_ristretto255_scalar_complement($s); |
4255 return sodium_crypto_core_ristretto255_scalar_complement($s); |
3773 } |
4256 } |
3774 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement($s); |
4257 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement($s); |
3775 } |
4258 } |
3779 * @param string $y |
4262 * @param string $y |
3780 * @param bool $dontFallback |
4263 * @param bool $dontFallback |
3781 * @return string |
4264 * @return string |
3782 * @throws SodiumException |
4265 * @throws SodiumException |
3783 */ |
4266 */ |
3784 public static function ristretto255_scalar_add($x, $y, $dontFallback = false) |
4267 public static function ristretto255_scalar_add( |
3785 { |
4268 #[\SensitiveParameter] |
4269 $x, |
|
4270 #[\SensitiveParameter] |
|
4271 $y, |
|
4272 $dontFallback = false |
|
4273 ) { |
|
3786 if (self::useNewSodiumAPI() && !$dontFallback) { |
4274 if (self::useNewSodiumAPI() && !$dontFallback) { |
3787 return sodium_crypto_core_ristretto255_scalar_add($x, $y); |
4275 return sodium_crypto_core_ristretto255_scalar_add($x, $y); |
3788 } |
4276 } |
3789 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add($x, $y); |
4277 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add($x, $y); |
3790 } |
4278 } |
3794 * @param string $y |
4282 * @param string $y |
3795 * @param bool $dontFallback |
4283 * @param bool $dontFallback |
3796 * @return string |
4284 * @return string |
3797 * @throws SodiumException |
4285 * @throws SodiumException |
3798 */ |
4286 */ |
3799 public static function ristretto255_scalar_sub($x, $y, $dontFallback = false) |
4287 public static function ristretto255_scalar_sub( |
3800 { |
4288 #[\SensitiveParameter] |
4289 $x, |
|
4290 #[\SensitiveParameter] |
|
4291 $y, |
|
4292 $dontFallback = false |
|
4293 ) { |
|
3801 if (self::useNewSodiumAPI() && !$dontFallback) { |
4294 if (self::useNewSodiumAPI() && !$dontFallback) { |
3802 return sodium_crypto_core_ristretto255_scalar_sub($x, $y); |
4295 return sodium_crypto_core_ristretto255_scalar_sub($x, $y); |
3803 } |
4296 } |
3804 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub($x, $y); |
4297 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub($x, $y); |
3805 } |
4298 } |
3809 * @param string $y |
4302 * @param string $y |
3810 * @param bool $dontFallback |
4303 * @param bool $dontFallback |
3811 * @return string |
4304 * @return string |
3812 * @throws SodiumException |
4305 * @throws SodiumException |
3813 */ |
4306 */ |
3814 public static function ristretto255_scalar_mul($x, $y, $dontFallback = false) |
4307 public static function ristretto255_scalar_mul( |
3815 { |
4308 #[\SensitiveParameter] |
4309 $x, |
|
4310 #[\SensitiveParameter] |
|
4311 $y, |
|
4312 $dontFallback = false |
|
4313 ) { |
|
3816 if (self::useNewSodiumAPI() && !$dontFallback) { |
4314 if (self::useNewSodiumAPI() && !$dontFallback) { |
3817 return sodium_crypto_core_ristretto255_scalar_mul($x, $y); |
4315 return sodium_crypto_core_ristretto255_scalar_mul($x, $y); |
3818 } |
4316 } |
3819 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul($x, $y); |
4317 return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul($x, $y); |
3820 } |
4318 } |
3824 * @param string $p |
4322 * @param string $p |
3825 * @param bool $dontFallback |
4323 * @param bool $dontFallback |
3826 * @return string |
4324 * @return string |
3827 * @throws SodiumException |
4325 * @throws SodiumException |
3828 */ |
4326 */ |
3829 public static function scalarmult_ristretto255($n, $p, $dontFallback = false) |
4327 public static function scalarmult_ristretto255( |
3830 { |
4328 #[\SensitiveParameter] |
4329 $n, |
|
4330 #[\SensitiveParameter] |
|
4331 $p, |
|
4332 $dontFallback = false |
|
4333 ) { |
|
3831 if (self::useNewSodiumAPI() && !$dontFallback) { |
4334 if (self::useNewSodiumAPI() && !$dontFallback) { |
3832 return sodium_crypto_scalarmult_ristretto255($n, $p); |
4335 return sodium_crypto_scalarmult_ristretto255($n, $p); |
3833 } |
4336 } |
3834 return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255($n, $p); |
4337 return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255($n, $p); |
3835 } |
4338 } |
3839 * @param string $p |
4342 * @param string $p |
3840 * @param bool $dontFallback |
4343 * @param bool $dontFallback |
3841 * @return string |
4344 * @return string |
3842 * @throws SodiumException |
4345 * @throws SodiumException |
3843 */ |
4346 */ |
3844 public static function scalarmult_ristretto255_base($n, $dontFallback = false) |
4347 public static function scalarmult_ristretto255_base( |
3845 { |
4348 #[\SensitiveParameter] |
4349 $n, |
|
4350 $dontFallback = false |
|
4351 ) { |
|
3846 if (self::useNewSodiumAPI() && !$dontFallback) { |
4352 if (self::useNewSodiumAPI() && !$dontFallback) { |
3847 return sodium_crypto_scalarmult_ristretto255_base($n); |
4353 return sodium_crypto_scalarmult_ristretto255_base($n); |
3848 } |
4354 } |
3849 return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base($n); |
4355 return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base($n); |
3850 } |
4356 } |
3853 * @param string $s |
4359 * @param string $s |
3854 * @param bool $dontFallback |
4360 * @param bool $dontFallback |
3855 * @return string |
4361 * @return string |
3856 * @throws SodiumException |
4362 * @throws SodiumException |
3857 */ |
4363 */ |
3858 public static function ristretto255_scalar_reduce($s, $dontFallback = false) |
4364 public static function ristretto255_scalar_reduce( |
3859 { |
4365 #[\SensitiveParameter] |
4366 $s, |
|
4367 $dontFallback = false |
|
4368 ) { |
|
3860 if (self::useNewSodiumAPI() && !$dontFallback) { |
4369 if (self::useNewSodiumAPI() && !$dontFallback) { |
3861 return sodium_crypto_core_ristretto255_scalar_reduce($s); |
4370 return sodium_crypto_core_ristretto255_scalar_reduce($s); |
3862 } |
4371 } |
3863 return ParagonIE_Sodium_Core_Ristretto255::sc_reduce($s); |
4372 return ParagonIE_Sodium_Core_Ristretto255::sc_reduce($s); |
3864 } |
4373 } |
3908 * @param string $val |
4417 * @param string $val |
3909 * @param string $addv |
4418 * @param string $addv |
3910 * @return void |
4419 * @return void |
3911 * @throws SodiumException |
4420 * @throws SodiumException |
3912 */ |
4421 */ |
3913 public static function sub(&$val, $addv) |
4422 public static function sub( |
3914 { |
4423 #[\SensitiveParameter] |
4424 &$val, |
|
4425 #[\SensitiveParameter] |
|
4426 $addv |
|
4427 ) { |
|
3915 $val_len = ParagonIE_Sodium_Core_Util::strlen($val); |
4428 $val_len = ParagonIE_Sodium_Core_Util::strlen($val); |
3916 $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv); |
4429 $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv); |
3917 if ($val_len !== $addv_len) { |
4430 if ($val_len !== $addv_len) { |
3918 throw new SodiumException('values must have the same length'); |
4431 throw new SodiumException('values must have the same length'); |
3919 } |
4432 } |