1 <?php |
1 <?php |
|
2 |
|
3 require_once dirname(dirname(__FILE__)) . '/autoload.php'; |
2 |
4 |
3 /** |
5 /** |
4 * This file will monkey patch the pure-PHP implementation in place of the |
6 * This file will monkey patch the pure-PHP implementation in place of the |
5 * PECL functions and constants, but only if they do not already exist. |
7 * PECL functions and constants, but only if they do not already exist. |
6 * |
8 * |
7 * Thus, the functions or constants just proxy to the appropriate |
9 * Thus, the functions or constants just proxy to the appropriate |
8 * ParagonIE_Sodium_Compat method or class constant, respectively. |
10 * ParagonIE_Sodium_Compat method or class constant, respectively. |
9 */ |
11 */ |
10 foreach (array( |
12 foreach (array( |
|
13 'BASE64_VARIANT_ORIGINAL', |
|
14 'BASE64_VARIANT_ORIGINAL_NO_PADDING', |
|
15 'BASE64_VARIANT_URLSAFE', |
|
16 'BASE64_VARIANT_URLSAFE_NO_PADDING', |
11 'CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES', |
17 'CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES', |
12 'CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES', |
18 'CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES', |
13 'CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES', |
19 'CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES', |
14 'CRYPTO_AEAD_CHACHA20POLY1305_ABYTES', |
20 'CRYPTO_AEAD_CHACHA20POLY1305_ABYTES', |
15 'CRYPTO_AEAD_AES256GCM_KEYBYTES', |
21 'CRYPTO_AEAD_AES256GCM_KEYBYTES', |
27 'CRYPTO_BOX_PUBLICKEYBYTES', |
33 'CRYPTO_BOX_PUBLICKEYBYTES', |
28 'CRYPTO_BOX_KEYPAIRBYTES', |
34 'CRYPTO_BOX_KEYPAIRBYTES', |
29 'CRYPTO_BOX_MACBYTES', |
35 'CRYPTO_BOX_MACBYTES', |
30 'CRYPTO_BOX_NONCEBYTES', |
36 'CRYPTO_BOX_NONCEBYTES', |
31 'CRYPTO_BOX_SEEDBYTES', |
37 'CRYPTO_BOX_SEEDBYTES', |
|
38 'CRYPTO_KDF_BYTES_MIN', |
|
39 'CRYPTO_KDF_BYTES_MAX', |
|
40 'CRYPTO_KDF_CONTEXTBYTES', |
|
41 'CRYPTO_KDF_KEYBYTES', |
32 'CRYPTO_KX_BYTES', |
42 'CRYPTO_KX_BYTES', |
|
43 'CRYPTO_KX_KEYPAIRBYTES', |
|
44 'CRYPTO_KX_PRIMITIVE', |
33 'CRYPTO_KX_SEEDBYTES', |
45 'CRYPTO_KX_SEEDBYTES', |
34 'CRYPTO_KX_PUBLICKEYBYTES', |
46 'CRYPTO_KX_PUBLICKEYBYTES', |
35 'CRYPTO_KX_SECRETKEYBYTES', |
47 'CRYPTO_KX_SECRETKEYBYTES', |
|
48 'CRYPTO_KX_SESSIONKEYBYTES', |
36 'CRYPTO_GENERICHASH_BYTES', |
49 'CRYPTO_GENERICHASH_BYTES', |
37 'CRYPTO_GENERICHASH_BYTES_MIN', |
50 'CRYPTO_GENERICHASH_BYTES_MIN', |
38 'CRYPTO_GENERICHASH_BYTES_MAX', |
51 'CRYPTO_GENERICHASH_BYTES_MAX', |
39 'CRYPTO_GENERICHASH_KEYBYTES', |
52 'CRYPTO_GENERICHASH_KEYBYTES', |
40 'CRYPTO_GENERICHASH_KEYBYTES_MIN', |
53 'CRYPTO_GENERICHASH_KEYBYTES_MIN', |
54 'CRYPTO_SHORTHASH_BYTES', |
67 'CRYPTO_SHORTHASH_BYTES', |
55 'CRYPTO_SHORTHASH_KEYBYTES', |
68 'CRYPTO_SHORTHASH_KEYBYTES', |
56 'CRYPTO_SECRETBOX_KEYBYTES', |
69 'CRYPTO_SECRETBOX_KEYBYTES', |
57 'CRYPTO_SECRETBOX_MACBYTES', |
70 'CRYPTO_SECRETBOX_MACBYTES', |
58 'CRYPTO_SECRETBOX_NONCEBYTES', |
71 'CRYPTO_SECRETBOX_NONCEBYTES', |
|
72 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES', |
|
73 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES', |
|
74 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES', |
|
75 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH', |
|
76 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL', |
|
77 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY', |
|
78 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL', |
|
79 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX', |
59 'CRYPTO_SIGN_BYTES', |
80 'CRYPTO_SIGN_BYTES', |
60 'CRYPTO_SIGN_SEEDBYTES', |
81 'CRYPTO_SIGN_SEEDBYTES', |
61 'CRYPTO_SIGN_PUBLICKEYBYTES', |
82 'CRYPTO_SIGN_PUBLICKEYBYTES', |
62 'CRYPTO_SIGN_SECRETKEYBYTES', |
83 'CRYPTO_SIGN_SECRETKEYBYTES', |
63 'CRYPTO_SIGN_KEYPAIRBYTES', |
84 'CRYPTO_SIGN_KEYPAIRBYTES', |
66 'LIBRARY_VERSION_MAJOR', |
87 'LIBRARY_VERSION_MAJOR', |
67 'LIBRARY_VERSION_MINOR', |
88 'LIBRARY_VERSION_MINOR', |
68 'VERSION_STRING' |
89 'VERSION_STRING' |
69 ) as $constant |
90 ) as $constant |
70 ) { |
91 ) { |
71 if (!defined("SODIUM_$constant")) { |
92 if (!defined("SODIUM_$constant") && defined("ParagonIE_Sodium_Compat::$constant")) { |
72 define("SODIUM_$constant", constant("ParagonIE_Sodium_Compat::$constant")); |
93 define("SODIUM_$constant", constant("ParagonIE_Sodium_Compat::$constant")); |
73 } |
94 } |
74 } |
95 } |
75 |
96 if (!is_callable('sodium_add')) { |
|
97 /** |
|
98 * @see ParagonIE_Sodium_Compat::add() |
|
99 * @param string $val |
|
100 * @param string $addv |
|
101 * @return void |
|
102 * @throws SodiumException |
|
103 */ |
|
104 function sodium_add(&$val, $addv) |
|
105 { |
|
106 ParagonIE_Sodium_Compat::add($val, $addv); |
|
107 } |
|
108 } |
|
109 if (!is_callable('sodium_base642bin')) { |
|
110 /** |
|
111 * @see ParagonIE_Sodium_Compat::bin2base64() |
|
112 * @param string $string |
|
113 * @param int $variant |
|
114 * @param string $ignore |
|
115 * @return string |
|
116 * @throws SodiumException |
|
117 * @throws TypeError |
|
118 */ |
|
119 function sodium_base642bin($string, $variant, $ignore ='') |
|
120 { |
|
121 return ParagonIE_Sodium_Compat::base642bin($string, $variant, $ignore); |
|
122 } |
|
123 } |
|
124 if (!is_callable('sodium_bin2base64')) { |
|
125 /** |
|
126 * @see ParagonIE_Sodium_Compat::bin2base64() |
|
127 * @param string $string |
|
128 * @param int $variant |
|
129 * @return string |
|
130 * @throws SodiumException |
|
131 * @throws TypeError |
|
132 */ |
|
133 function sodium_bin2base64($string, $variant) |
|
134 { |
|
135 return ParagonIE_Sodium_Compat::bin2base64($string, $variant); |
|
136 } |
|
137 } |
76 if (!is_callable('sodium_bin2hex')) { |
138 if (!is_callable('sodium_bin2hex')) { |
77 /** |
139 /** |
78 * @see ParagonIE_Sodium_Compat::hex2bin() |
140 * @see ParagonIE_Sodium_Compat::hex2bin() |
79 * @param string $string |
141 * @param string $string |
80 * @return string |
142 * @return string |
532 * @throws TypeError |
599 * @throws TypeError |
533 */ |
600 */ |
534 function sodium_crypto_generichash_update(&$ctx, $message = '') |
601 function sodium_crypto_generichash_update(&$ctx, $message = '') |
535 { |
602 { |
536 ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message); |
603 ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message); |
|
604 } |
|
605 } |
|
606 if (!is_callable('sodium_crypto_kdf_keygen')) { |
|
607 /** |
|
608 * @see ParagonIE_Sodium_Compat::crypto_kdf_keygen() |
|
609 * @return string |
|
610 * @throws Exception |
|
611 */ |
|
612 function sodium_crypto_kdf_keygen() |
|
613 { |
|
614 return ParagonIE_Sodium_Compat::crypto_kdf_keygen(); |
|
615 } |
|
616 } |
|
617 if (!is_callable('sodium_crypto_kdf_derive_from_key')) { |
|
618 /** |
|
619 * @see ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key() |
|
620 * @param int $subkey_len |
|
621 * @param int $subkey_id |
|
622 * @param string $context |
|
623 * @param string $key |
|
624 * @return string |
|
625 * @throws Exception |
|
626 */ |
|
627 function sodium_crypto_kdf_derive_from_key($subkey_len, $subkey_id, $context, $key) |
|
628 { |
|
629 return ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key( |
|
630 $subkey_len, |
|
631 $subkey_id, |
|
632 $context, |
|
633 $key |
|
634 ); |
537 } |
635 } |
538 } |
636 } |
539 if (!is_callable('sodium_crypto_kx')) { |
637 if (!is_callable('sodium_crypto_kx')) { |
540 /** |
638 /** |
541 * @see ParagonIE_Sodium_Compat::crypto_kx() |
639 * @see ParagonIE_Sodium_Compat::crypto_kx() |
553 $my_secret, |
651 $my_secret, |
554 $their_public, |
652 $their_public, |
555 $client_public, |
653 $client_public, |
556 $server_public |
654 $server_public |
557 ); |
655 ); |
|
656 } |
|
657 } |
|
658 if (!is_callable('sodium_crypto_kx_seed_keypair')) { |
|
659 /** |
|
660 * @param string $seed |
|
661 * @return string |
|
662 * @throws Exception |
|
663 */ |
|
664 function sodium_crypto_kx_seed_keypair($seed) |
|
665 { |
|
666 return ParagonIE_Sodium_Compat::crypto_kx_seed_keypair($seed); |
|
667 } |
|
668 } |
|
669 if (!is_callable('sodium_crypto_kx_keypair')) { |
|
670 /** |
|
671 * @return string |
|
672 * @throws Exception |
|
673 */ |
|
674 function sodium_crypto_kx_keypair() |
|
675 { |
|
676 return ParagonIE_Sodium_Compat::crypto_kx_keypair(); |
|
677 } |
|
678 } |
|
679 if (!is_callable('sodium_crypto_kx_client_session_keys')) { |
|
680 /** |
|
681 * @param string $keypair |
|
682 * @param string $serverPublicKey |
|
683 * @return array{0: string, 1: string} |
|
684 * @throws SodiumException |
|
685 */ |
|
686 function sodium_crypto_kx_client_session_keys($keypair, $serverPublicKey) |
|
687 { |
|
688 return ParagonIE_Sodium_Compat::crypto_kx_client_session_keys($keypair, $serverPublicKey); |
|
689 } |
|
690 } |
|
691 if (!is_callable('sodium_crypto_kx_server_session_keys')) { |
|
692 /** |
|
693 * @param string $keypair |
|
694 * @param string $clientPublicKey |
|
695 * @return array{0: string, 1: string} |
|
696 * @throws SodiumException |
|
697 */ |
|
698 function sodium_crypto_kx_server_session_keys($keypair, $clientPublicKey) |
|
699 { |
|
700 return ParagonIE_Sodium_Compat::crypto_kx_server_session_keys($keypair, $clientPublicKey); |
|
701 } |
|
702 } |
|
703 if (!is_callable('sodium_crypto_kx_secretkey')) { |
|
704 /** |
|
705 * @param string $keypair |
|
706 * @return string |
|
707 * @throws Exception |
|
708 */ |
|
709 function sodium_crypto_kx_secretkey($keypair) |
|
710 { |
|
711 return ParagonIE_Sodium_Compat::crypto_kx_secretkey($keypair); |
|
712 } |
|
713 } |
|
714 if (!is_callable('sodium_crypto_kx_publickey')) { |
|
715 /** |
|
716 * @param string $keypair |
|
717 * @return string |
|
718 * @throws Exception |
|
719 */ |
|
720 function sodium_crypto_kx_publickey($keypair) |
|
721 { |
|
722 return ParagonIE_Sodium_Compat::crypto_kx_publickey($keypair); |
558 } |
723 } |
559 } |
724 } |
560 if (!is_callable('sodium_crypto_pwhash')) { |
725 if (!is_callable('sodium_crypto_pwhash')) { |
561 /** |
726 /** |
562 * @see ParagonIE_Sodium_Compat::crypto_pwhash() |
727 * @see ParagonIE_Sodium_Compat::crypto_pwhash() |
588 function sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit) |
753 function sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit) |
589 { |
754 { |
590 return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit); |
755 return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit); |
591 } |
756 } |
592 } |
757 } |
|
758 if (!is_callable('sodium_crypto_pwhash_str_needs_rehash')) { |
|
759 /** |
|
760 * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_needs_rehash() |
|
761 * @param string $hash |
|
762 * @param int $opslimit |
|
763 * @param int $memlimit |
|
764 * @return bool |
|
765 * |
|
766 * @throws SodiumException |
|
767 */ |
|
768 function sodium_crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit) |
|
769 { |
|
770 return ParagonIE_Sodium_Compat::crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit); |
|
771 } |
|
772 } |
593 if (!is_callable('sodium_crypto_pwhash_str_verify')) { |
773 if (!is_callable('sodium_crypto_pwhash_str_verify')) { |
594 /** |
774 /** |
595 * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify() |
775 * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify() |
596 * @param string $passwd |
776 * @param string $passwd |
597 * @param string $hash |
777 * @param string $hash |
719 } catch (Exception $ex) { |
900 } catch (Exception $ex) { |
720 return false; |
901 return false; |
721 } |
902 } |
722 } |
903 } |
723 } |
904 } |
|
905 if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_init_push')) { |
|
906 /** |
|
907 * @param string $key |
|
908 * @return array<int, string> |
|
909 * @throws SodiumException |
|
910 */ |
|
911 function sodium_crypto_secretstream_xchacha20poly1305_init_push($key) |
|
912 { |
|
913 return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_push($key); |
|
914 } |
|
915 } |
|
916 if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_push')) { |
|
917 /** |
|
918 * @param string $state |
|
919 * @param string $msg |
|
920 * @param string $aad |
|
921 * @param int $tag |
|
922 * @return string |
|
923 * @throws SodiumException |
|
924 */ |
|
925 function sodium_crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) |
|
926 { |
|
927 return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_push($state, $msg, $aad, $tag); |
|
928 } |
|
929 } |
|
930 if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_init_pull')) { |
|
931 /** |
|
932 * @param string $header |
|
933 * @param string $key |
|
934 * @return string |
|
935 * @throws Exception |
|
936 */ |
|
937 function sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key) |
|
938 { |
|
939 return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_pull($header, $key); |
|
940 } |
|
941 } |
|
942 if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_pull')) { |
|
943 /** |
|
944 * @param string $state |
|
945 * @param string $cipher |
|
946 * @param string $aad |
|
947 * @return bool|array{0: string, 1: int} |
|
948 * @throws SodiumException |
|
949 */ |
|
950 function sodium_crypto_secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') |
|
951 { |
|
952 return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_pull($state, $cipher, $aad); |
|
953 } |
|
954 } |
|
955 if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_rekey')) { |
|
956 /** |
|
957 * @param string $state |
|
958 * @return void |
|
959 * @throws SodiumException |
|
960 */ |
|
961 function sodium_crypto_secretstream_xchacha20poly1305_rekey(&$state) |
|
962 { |
|
963 ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_rekey($state); |
|
964 } |
|
965 } |
|
966 if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_keygen')) { |
|
967 /** |
|
968 * @return string |
|
969 * @throws Exception |
|
970 */ |
|
971 function sodium_crypto_secretstream_xchacha20poly1305_keygen() |
|
972 { |
|
973 return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_keygen(); |
|
974 } |
|
975 } |
724 if (!is_callable('sodium_crypto_shorthash')) { |
976 if (!is_callable('sodium_crypto_shorthash')) { |
725 /** |
977 /** |
726 * @see ParagonIE_Sodium_Compat::crypto_shorthash() |
978 * @see ParagonIE_Sodium_Compat::crypto_shorthash() |
727 * @param string $message |
979 * @param string $message |
728 * @param string $key |
980 * @param string $key |
769 * @throws TypeError |
1022 * @throws TypeError |
770 */ |
1023 */ |
771 function sodium_crypto_sign_detached($message, $sk) |
1024 function sodium_crypto_sign_detached($message, $sk) |
772 { |
1025 { |
773 return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $sk); |
1026 return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $sk); |
|
1027 } |
|
1028 } |
|
1029 if (!is_callable('sodium_crypto_sign_keypair_from_secretkey_and_publickey')) { |
|
1030 /** |
|
1031 * @see ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey() |
|
1032 * @param string $sk |
|
1033 * @param string $pk |
|
1034 * @return string |
|
1035 * @throws SodiumException |
|
1036 * @throws TypeError |
|
1037 */ |
|
1038 function sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk) |
|
1039 { |
|
1040 return ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk); |
774 } |
1041 } |
775 } |
1042 } |
776 if (!is_callable('sodium_crypto_sign_keypair')) { |
1043 if (!is_callable('sodium_crypto_sign_keypair')) { |
777 /** |
1044 /** |
778 * @see ParagonIE_Sodium_Compat::crypto_sign_keypair() |
1045 * @see ParagonIE_Sodium_Compat::crypto_sign_keypair() |
1017 function sodium_memzero(&$str) |
1285 function sodium_memzero(&$str) |
1018 { |
1286 { |
1019 ParagonIE_Sodium_Compat::memzero($str); |
1287 ParagonIE_Sodium_Compat::memzero($str); |
1020 } |
1288 } |
1021 } |
1289 } |
|
1290 if (!is_callable('sodium_pad')) { |
|
1291 /** |
|
1292 * @see ParagonIE_Sodium_Compat::pad() |
|
1293 * @param string $unpadded |
|
1294 * @param int $blockSize |
|
1295 * @return int |
|
1296 * @throws SodiumException |
|
1297 * @throws TypeError |
|
1298 */ |
|
1299 function sodium_pad($unpadded, $blockSize) |
|
1300 { |
|
1301 return ParagonIE_Sodium_Compat::pad($unpadded, $blockSize, true); |
|
1302 } |
|
1303 } |
|
1304 if (!is_callable('sodium_unpad')) { |
|
1305 /** |
|
1306 * @see ParagonIE_Sodium_Compat::pad() |
|
1307 * @param string $padded |
|
1308 * @param int $blockSize |
|
1309 * @return int |
|
1310 * @throws SodiumException |
|
1311 * @throws TypeError |
|
1312 */ |
|
1313 function sodium_unpad($padded, $blockSize) |
|
1314 { |
|
1315 return ParagonIE_Sodium_Compat::unpad($padded, $blockSize, true); |
|
1316 } |
|
1317 } |
1022 if (!is_callable('sodium_randombytes_buf')) { |
1318 if (!is_callable('sodium_randombytes_buf')) { |
1023 /** |
1319 /** |
1024 * @see ParagonIE_Sodium_Compat::randombytes_buf() |
1320 * @see ParagonIE_Sodium_Compat::randombytes_buf() |
1025 * @param int $amount |
1321 * @param int $amount |
1026 * @return string |
1322 * @return string |