diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index 2564ee993d6..a619b3ccf32 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -154,6 +154,7 @@ void init_executor(void) /* {{{ */ EG(ticks_count) = 0; ZVAL_UNDEF(&EG(user_error_handler)); + ZVAL_UNDEF(&EG(user_exception_handler)); EG(current_execute_data) = NULL; diff --git a/appveyor/build_task.bat b/appveyor/build_task.bat index 6a2bf2dbc08..1bcbbb77543 100644 --- a/appveyor/build_task.bat +++ b/appveyor/build_task.bat @@ -22,6 +22,13 @@ echo Updating dependencies in %DEPS_DIR% cmd /c phpsdk_deps --update --no-backup --branch %BRANCH% --stability %STABILITY% --deps %DEPS_DIR% --crt %PHP_BUILD_CRT% if %errorlevel% neq 0 exit /b 3 +rem Something went wrong, most likely when concurrent builds were to fetch deps +rem updates. It might be, that some locking mechanism is needed. +if not exist "%DEPS_DIR%" ( + cmd /c phpsdk_deps --update --force --no-backup --branch %BRANCH% --stability %STABILITY% --deps %DEPS_DIR% +) +if %errorlevel% neq 0 exit /b 3 + cmd /c buildconf.bat --force if %errorlevel% neq 0 exit /b 3 diff --git a/ext/mysqli/tests/mysqli_reap_async_query.phpt b/ext/mysqli/tests/mysqli_reap_async_query.phpt index e3858e61722..3f3bce32926 100644 --- a/ext/mysqli/tests/mysqli_reap_async_query.phpt +++ b/ext/mysqli/tests/mysqli_reap_async_query.phpt @@ -38,7 +38,7 @@ if (!$IS_MYSQLND) function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax) { - if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 1000))) + if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 2000))) printf("[%03d + 1] There should be %d links ready to read from, %d ready\n", $offset, $exp_ready, $tmp); diff --git a/ext/sodium/libsodium.c b/ext/sodium/libsodium.c index 10c23310bd6..6f71946fc18 100644 --- a/ext/sodium/libsodium.c +++ b/ext/sodium/libsodium.c @@ -23,11 +23,10 @@ #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" +#include "php_libsodium.h" #include "zend_exceptions.h" #include -#include "php_libsodium.h" - #include #define PHP_SODIUM_ZSTR_TRUNCATE(zs, len) do { ZSTR_LEN(zs) = (len); } while(0) @@ -160,6 +159,16 @@ ZEND_BEGIN_ARG_INFO_EX(AI_MaybeKeyAndLength, 0, 0, 0) ZEND_ARG_INFO(0, length) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(AI_KXClientSession, 0, 0, 2) + ZEND_ARG_INFO(0, client_keypair) + ZEND_ARG_INFO(0, server_key) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(AI_KXServerSession, 0, 0, 2) + ZEND_ARG_INFO(0, server_keypair) + ZEND_ARG_INFO(0, client_key) +ZEND_END_ARG_INFO() + #if defined(HAVE_CRYPTO_AEAD_AES256GCM) && defined(crypto_aead_aes256gcm_KEYBYTES) && \ (defined(__amd64) || defined(__amd64__) || defined(__x86_64__) || defined(__i386__) || \ defined(_M_AMD64) || defined(_M_IX86)) @@ -196,7 +205,12 @@ const zend_function_entry sodium_functions[] = { PHP_FE(sodium_crypto_box_seal_open, AI_StringAndKey) #endif PHP_FE(sodium_crypto_box_secretkey, AI_Key) - PHP_FE(sodium_crypto_kx, AI_FourStrings) + PHP_FE(sodium_crypto_kx_keypair, AI_None) + PHP_FE(sodium_crypto_kx_publickey, AI_Key) + PHP_FE(sodium_crypto_kx_secretkey, AI_Key) + PHP_FE(sodium_crypto_kx_seed_keypair, AI_String) + PHP_FE(sodium_crypto_kx_client_session_keys, AI_KXClientSession) + PHP_FE(sodium_crypto_kx_server_session_keys, AI_KXServerSession) PHP_FE(sodium_crypto_generichash, AI_StringAndMaybeKeyAndLength) PHP_FE(sodium_crypto_generichash_init, AI_MaybeKeyAndLength) PHP_FE(sodium_crypto_generichash_update, AI_StateByReferenceAndString) @@ -369,12 +383,23 @@ PHP_MINIT_FUNCTION(sodium) crypto_box_NONCEBYTES, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SEEDBYTES", crypto_box_SEEDBYTES, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_BYTES", - crypto_kx_BYTES, CONST_CS | CONST_PERSISTENT); +#ifndef crypto_kx_SEEDBYTES +# define crypto_kx_SEEDBYTES 32 +# define crypto_kx_SESSIONKEYBYTES 32 +# define crypto_kx_PUBLICKEYBYTES 32 +# define crypto_kx_SECRETKEYBYTES 32 +#endif + REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SEEDBYTES", + crypto_kx_SEEDBYTES, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SESSIONKEYBYTES", + crypto_kx_SESSIONKEYBYTES, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_PUBLICKEYBYTES", crypto_kx_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SECRETKEYBYTES", crypto_kx_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_KEYPAIRBYTES", + crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES, + CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES", crypto_generichash_BYTES, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES_MIN", @@ -476,7 +501,7 @@ PHP_FUNCTION(sodium_memzero) } ZVAL_DEREF(buf_zv); if (Z_TYPE_P(buf_zv) != IS_STRING) { - zend_throw_exception(sodium_exception_ce, "memzero: a PHP string is required", 0); + zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0); return; } if (Z_REFCOUNTED_P(buf_zv) && Z_REFCOUNT_P(buf_zv) == 1) { @@ -503,7 +528,7 @@ PHP_FUNCTION(sodium_increment) } ZVAL_DEREF(val_zv); if (Z_TYPE_P(val_zv) != IS_STRING) { - zend_throw_exception(sodium_exception_ce, "increment(): a PHP string is required", 0); + zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0); return; } @@ -534,7 +559,7 @@ PHP_FUNCTION(sodium_add) } ZVAL_DEREF(val_zv); if (Z_TYPE_P(val_zv) != IS_STRING) { - zend_throw_exception(sodium_exception_ce, "add(): PHP strings are required", 0); + zend_throw_exception(sodium_exception_ce, "PHP strings are required", 0); return; } @@ -542,7 +567,7 @@ PHP_FUNCTION(sodium_add) val = (unsigned char *) Z_STRVAL(*val_zv); val_len = Z_STRLEN(*val_zv); if (val_len != addv_len) { - zend_throw_exception(sodium_exception_ce, "add(): values must have the same length", 0); + zend_throw_exception(sodium_exception_ce, "values must have the same length", 0); return; } c = 0U; @@ -566,7 +591,7 @@ PHP_FUNCTION(sodium_memcmp) return; } if (len1 != len2) { - zend_throw_exception(sodium_exception_ce, "memcmp(): arguments have different sizes", 0); + zend_throw_exception(sodium_exception_ce, "arguments have different sizes", 0); } else { RETURN_LONG(sodium_memcmp(buf1, buf2, len1)); } @@ -587,8 +612,7 @@ PHP_FUNCTION(sodium_crypto_shorthash) } if (key_len != crypto_shorthash_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_shorthash(): key size should be " - "CRYPTO_SHORTHASH_KEYBYTES bytes", + "key size should be CRYPTO_SHORTHASH_KEYBYTES bytes", 0); return; } @@ -596,7 +620,7 @@ PHP_FUNCTION(sodium_crypto_shorthash) if (crypto_shorthash((unsigned char *) ZSTR_VAL(hash), msg, (unsigned long long) msg_len, key) != 0) { zend_string_free(hash); - zend_throw_exception(sodium_exception_ce, "crypto_shorthash(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(hash)[crypto_shorthash_BYTES] = 0; @@ -622,15 +646,13 @@ PHP_FUNCTION(sodium_crypto_secretbox) } if (nonce_len != crypto_secretbox_NONCEBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_secretbox(): nonce size should be " - "CRYPTO_SECRETBOX_NONCEBYTES bytes", + "nonce size should be CRYPTO_SECRETBOX_NONCEBYTES bytes", 0); return; } if (key_len != crypto_secretbox_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_secretbox(): key size should be " - "CRYPTO_SECRETBOX_KEYBYTES bytes", + "key size should be CRYPTO_SECRETBOX_KEYBYTES bytes", 0); return; } @@ -643,7 +665,7 @@ PHP_FUNCTION(sodium_crypto_secretbox) msg, (unsigned long long) msg_len, nonce, key) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_secretbox(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(ciphertext)[msg_len + crypto_secretbox_MACBYTES] = 0; @@ -669,15 +691,13 @@ PHP_FUNCTION(sodium_crypto_secretbox_open) } if (nonce_len != crypto_secretbox_NONCEBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_secretbox_open(): nonce size should be " - "CRYPTO_SECRETBOX_NONCEBYTES bytes", + "nonce size should be CRYPTO_SECRETBOX_NONCEBYTES bytes", 0); return; } if (key_len != crypto_secretbox_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_secretbox_open(): key size should be " - "CRYPTO_SECRETBOX_KEYBYTES bytes", + "key size should be CRYPTO_SECRETBOX_KEYBYTES bytes", 0); return; } @@ -714,13 +734,13 @@ PHP_FUNCTION(sodium_crypto_generichash) } if (hash_len < crypto_generichash_BYTES_MIN || hash_len > crypto_generichash_BYTES_MAX) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash(): unsupported output length", 0); + zend_throw_exception(sodium_exception_ce, "unsupported output length", 0); return; } if (key_len != 0 && (key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX)) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash(): unsupported key length", 0); + zend_throw_exception(sodium_exception_ce, "unsupported key length", 0); return; } hash = zend_string_alloc(hash_len, 0); @@ -728,7 +748,7 @@ PHP_FUNCTION(sodium_crypto_generichash) msg, (unsigned long long) msg_len, key, (size_t) key_len) != 0) { zend_string_free(hash); - zend_throw_exception(sodium_exception_ce, "crypto_generichash(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(hash)[hash_len] = 0; @@ -752,18 +772,18 @@ PHP_FUNCTION(sodium_crypto_generichash_init) } if (hash_len < crypto_generichash_BYTES_MIN || hash_len > crypto_generichash_BYTES_MAX) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_init(): unsupported output length", 0); + zend_throw_exception(sodium_exception_ce, "unsupported output length", 0); return; } if (key_len != 0 && (key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX)) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_init(): unsupported key length", 0); + zend_throw_exception(sodium_exception_ce, "unsupported key length", 0); return; } if (crypto_generichash_init((void *) &state_tmp, key, (size_t) key_len, (size_t) hash_len) != 0) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_init(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } state = zend_string_alloc(state_len, 0); @@ -789,20 +809,20 @@ PHP_FUNCTION(sodium_crypto_generichash_update) } ZVAL_DEREF(state_zv); if (Z_TYPE_P(state_zv) != IS_STRING) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_update: a reference to a state is required", 0); + zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0); return; } sodium_separate_string(state_zv); state = (unsigned char *) Z_STRVAL(*state_zv); state_len = Z_STRLEN(*state_zv); if (state_len != sizeof (crypto_generichash_state)) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_update(): incorrect state length", 0); + zend_throw_exception(sodium_exception_ce, "incorrect state length", 0); return; } memcpy(&state_tmp, state, sizeof state_tmp); if (crypto_generichash_update((void *) &state_tmp, msg, (unsigned long long) msg_len) != 0) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_update(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } memcpy(state, &state_tmp, state_len); @@ -826,19 +846,19 @@ PHP_FUNCTION(sodium_crypto_generichash_final) } ZVAL_DEREF(state_zv); if (Z_TYPE_P(state_zv) != IS_STRING) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_final: a reference to a state is required", 0); + zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0); return; } sodium_separate_string(state_zv); state = (unsigned char *) Z_STRVAL(*state_zv); state_len = Z_STRLEN(*state_zv); if (state_len != sizeof (crypto_generichash_state)) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_final(): incorrect state length", 0); + zend_throw_exception(sodium_exception_ce, "incorrect state length", 0); return; } if (hash_len < crypto_generichash_BYTES_MIN || hash_len > crypto_generichash_BYTES_MAX) { - zend_throw_exception(sodium_exception_ce, "crypto_generichash_final(): unsupported output length", 0); + zend_throw_exception(sodium_exception_ce, "unsupported output length", 0); return; } hash = zend_string_alloc(hash_len, 0); @@ -847,7 +867,7 @@ PHP_FUNCTION(sodium_crypto_generichash_final) (unsigned char *) ZSTR_VAL(hash), (size_t) hash_len) != 0) { zend_string_free(hash); - zend_throw_exception(sodium_exception_ce, "crypto_generichash_final(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } sodium_memzero(state, state_len); @@ -868,7 +888,7 @@ PHP_FUNCTION(sodium_crypto_box_keypair) crypto_box_SECRETKEYBYTES, (unsigned char *) ZSTR_VAL(keypair)) != 0) { zend_string_free(keypair); - zend_throw_exception(sodium_exception_ce, "crypto_box_keypair(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(keypair)[keypair_len] = 0; @@ -889,7 +909,6 @@ PHP_FUNCTION(sodium_crypto_box_seed_keypair) } if (seed_len != crypto_box_SEEDBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_seed_keypair(): " "seed should be CRYPTO_BOX_SEEDBYTES bytes", 0); return; @@ -901,7 +920,7 @@ PHP_FUNCTION(sodium_crypto_box_seed_keypair) (unsigned char *) ZSTR_VAL(keypair), seed) != 0) { zend_string_free(keypair); - zend_throw_exception(sodium_exception_ce, "crypto_box_seed_keypair(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(keypair)[keypair_len] = 0; @@ -925,14 +944,12 @@ PHP_FUNCTION(sodium_crypto_box_keypair_from_secretkey_and_publickey) } if (secretkey_len != crypto_box_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_keypair_from_secretkey_and_publickey(): " "secretkey should be CRYPTO_BOX_SECRETKEYBYTES bytes", 0); return; } if (publickey_len != crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_keypair_from_secretkey_and_publickey(): " "publickey should be CRYPTO_BOX_PUBLICKEYBYTES bytes", 0); return; @@ -960,8 +977,7 @@ PHP_FUNCTION(sodium_crypto_box_secretkey) if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_secretkey(): keypair should be " - "CRYPTO_BOX_KEYPAIRBYTES bytes", + "keypair should be CRYPTO_BOX_KEYPAIRBYTES bytes", 0); return; } @@ -985,8 +1001,7 @@ PHP_FUNCTION(sodium_crypto_box_publickey) if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_publickey(): keypair should be " - "CRYPTO_BOX_KEYPAIRBYTES bytes", + "keypair should be CRYPTO_BOX_KEYPAIRBYTES bytes", 0); return; } @@ -1010,8 +1025,7 @@ PHP_FUNCTION(sodium_crypto_box_publickey_from_secretkey) } if (secretkey_len != crypto_box_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_publickey_from_secretkey(): key should be " - "CRYPTO_BOX_SECRETKEYBYTES bytes", + "key should be CRYPTO_BOX_SECRETKEYBYTES bytes", 0); return; } @@ -1046,15 +1060,13 @@ PHP_FUNCTION(sodium_crypto_box) } if (nonce_len != crypto_box_NONCEBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box(): nonce size should be " - "CRYPTO_BOX_NONCEBYTES bytes", + "nonce size should be CRYPTO_BOX_NONCEBYTES bytes", 0); return; } if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box(): keypair size should be " - "CRYPTO_BOX_KEYPAIRBYTES bytes", + "keypair size should be CRYPTO_BOX_KEYPAIRBYTES bytes", 0); return; } @@ -1069,7 +1081,7 @@ PHP_FUNCTION(sodium_crypto_box) (unsigned long long) msg_len, nonce, publickey, secretkey) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_box(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(ciphertext)[msg_len + crypto_box_MACBYTES] = 0; @@ -1097,15 +1109,13 @@ PHP_FUNCTION(sodium_crypto_box_open) } if (nonce_len != crypto_box_NONCEBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_open(): nonce size should be " - "CRYPTO_BOX_NONCEBYTES bytes", + "nonce size should be CRYPTO_BOX_NONCEBYTES bytes", 0); return; } if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_open(): keypair size should be " - "CRYPTO_BOX_KEYBYTES bytes", + "keypair size should be CRYPTO_BOX_KEYBYTES bytes", 0); return; } @@ -1142,8 +1152,7 @@ PHP_FUNCTION(sodium_crypto_box_seal) } if (publickey_len != crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_seal(): public key size should be " - "CRYPTO_BOX_PUBLICKEYBYTES bytes", + "public key size should be CRYPTO_BOX_PUBLICKEYBYTES bytes", 0); return; } @@ -1155,7 +1164,7 @@ PHP_FUNCTION(sodium_crypto_box_seal) if (crypto_box_seal((unsigned char *) ZSTR_VAL(ciphertext), msg, (unsigned long long) msg_len, publickey) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_box_seal(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(ciphertext)[msg_len + crypto_box_SEALBYTES] = 0; @@ -1180,8 +1189,7 @@ PHP_FUNCTION(sodium_crypto_box_seal_open) } if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_box_seal_open(): keypair size should be " - "CRYPTO_BOX_KEYBYTES bytes", + "keypair size should be CRYPTO_BOX_KEYBYTES bytes", 0); return; } @@ -1214,7 +1222,7 @@ PHP_FUNCTION(sodium_crypto_sign_keypair) crypto_sign_SECRETKEYBYTES, (unsigned char *) ZSTR_VAL(keypair)) != 0) { zend_string_free(keypair); - zend_throw_exception(sodium_exception_ce, "crypto_sign_keypair(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(keypair)[keypair_len] = 0; @@ -1235,7 +1243,6 @@ PHP_FUNCTION(sodium_crypto_sign_seed_keypair) } if (seed_len != crypto_sign_SEEDBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_seed_keypair(): " "seed should be CRYPTO_SIGN_SEEDBYTES bytes", 0); return; @@ -1247,7 +1254,7 @@ PHP_FUNCTION(sodium_crypto_sign_seed_keypair) (unsigned char *) ZSTR_VAL(keypair), seed) != 0) { zend_string_free(keypair); - zend_throw_exception(sodium_exception_ce, "crypto_sign_seed_keypair(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(keypair)[keypair_len] = 0; @@ -1271,14 +1278,12 @@ PHP_FUNCTION(sodium_crypto_sign_keypair_from_secretkey_and_publickey) } if (secretkey_len != crypto_sign_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_keypair_from_secretkey_and_publickey(): " "secretkey should be CRYPTO_SIGN_SECRETKEYBYTES bytes", 0); return; } if (publickey_len != crypto_sign_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_keypair_from_secretkey_and_publickey(): " "publickey should be CRYPTO_SIGN_PUBLICKEYBYTES bytes", 0); return; @@ -1305,7 +1310,6 @@ PHP_FUNCTION(sodium_crypto_sign_publickey_from_secretkey) } if (secretkey_len != crypto_sign_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_publickey_from_secretkey(): " "secretkey should be CRYPTO_SIGN_SECRETKEYBYTES bytes", 0); return; @@ -1315,7 +1319,7 @@ PHP_FUNCTION(sodium_crypto_sign_publickey_from_secretkey) if (crypto_sign_ed25519_sk_to_pk((unsigned char *) ZSTR_VAL(publickey), (const unsigned char *) secretkey) != 0) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_publickey_from_secretkey(): internal error", 0); + "internal error", 0); return; } ZSTR_VAL(publickey)[crypto_sign_PUBLICKEYBYTES] = 0; @@ -1336,8 +1340,7 @@ PHP_FUNCTION(sodium_crypto_sign_secretkey) if (keypair_len != crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_secretkey(): keypair should be " - "CRYPTO_SIGN_KEYPAIRBYTES bytes", + "keypair should be CRYPTO_SIGN_KEYPAIRBYTES bytes", 0); return; } @@ -1361,8 +1364,7 @@ PHP_FUNCTION(sodium_crypto_sign_publickey) if (keypair_len != crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_publickey(): keypair should be " - "CRYPTO_SIGN_KEYPAIRBYTES bytes", + "keypair should be CRYPTO_SIGN_KEYPAIRBYTES bytes", 0); return; } @@ -1391,8 +1393,7 @@ PHP_FUNCTION(sodium_crypto_sign) } if (secretkey_len != crypto_sign_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign(): secret key size should be " - "CRYPTO_SIGN_SECRETKEYBYTES bytes", + "secret key size should be CRYPTO_SIGN_SECRETKEYBYTES bytes", 0); return; } @@ -1406,7 +1407,7 @@ PHP_FUNCTION(sodium_crypto_sign) &msg_signed_real_len, msg, (unsigned long long) msg_len, secretkey) != 0) { zend_string_free(msg_signed); - zend_throw_exception(sodium_exception_ce, "crypto_sign(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } if (msg_signed_real_len <= 0U || msg_signed_real_len >= SIZE_MAX || @@ -1438,8 +1439,7 @@ PHP_FUNCTION(sodium_crypto_sign_open) } if (publickey_len != crypto_sign_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_open(): public key size should be " - "CRYPTO_SIGN_PUBLICKEYBYTES bytes", + "public key size should be CRYPTO_SIGN_PUBLICKEYBYTES bytes", 0); return; } @@ -1482,8 +1482,7 @@ PHP_FUNCTION(sodium_crypto_sign_detached) } if (secretkey_len != crypto_sign_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_detached(): secret key size should be " - "CRYPTO_SIGN_SECRETKEYBYTES bytes", + "secret key size should be CRYPTO_SIGN_SECRETKEYBYTES bytes", 0); return; } @@ -1493,7 +1492,7 @@ PHP_FUNCTION(sodium_crypto_sign_detached) &signature_real_len, msg, (unsigned long long) msg_len, secretkey) != 0) { zend_string_free(signature); - zend_throw_exception(sodium_exception_ce, "crypto_sign_detached()", 0); + zend_throw_exception(sodium_exception_ce, "signature creation failed", 0); return; } if (signature_real_len <= 0U || signature_real_len > crypto_sign_BYTES) { @@ -1523,15 +1522,13 @@ PHP_FUNCTION(sodium_crypto_sign_verify_detached) } if (signature_len != crypto_sign_BYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_verify_detached(): signature size should be " - "CRYPTO_SIGN_BYTES bytes", + "signature size should be CRYPTO_SIGN_BYTES bytes", 0); return; } if (publickey_len != crypto_sign_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_verify_detached(): public key size should be " - "CRYPTO_SIGN_PUBLICKEYBYTES bytes", + "public key size should be CRYPTO_SIGN_PUBLICKEYBYTES bytes", 0); return; } @@ -1559,7 +1556,7 @@ PHP_FUNCTION(sodium_crypto_stream) return; } if (ciphertext_len <= 0 || ciphertext_len >= SIZE_MAX) { - zend_throw_exception(sodium_exception_ce, "crypto_stream(): invalid length", 0); + zend_throw_exception(sodium_exception_ce, "invalid length", 0); return; } if (nonce_len != crypto_stream_NONCEBYTES) { @@ -1574,7 +1571,7 @@ PHP_FUNCTION(sodium_crypto_stream) if (crypto_stream((unsigned char *) ZSTR_VAL(ciphertext), (unsigned long long) ciphertext_len, nonce, key) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_stream(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(ciphertext)[ciphertext_len] = 0; @@ -1612,7 +1609,7 @@ PHP_FUNCTION(sodium_crypto_stream_xor) if (crypto_stream_xor((unsigned char *) ZSTR_VAL(ciphertext), msg, (unsigned long long) msg_len, nonce, key) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_stream_xor(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(ciphertext)[ciphertext_len] = 0; @@ -1638,7 +1635,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256) &opslimit, &memlimit) == FAILURE || hash_len <= 0 || hash_len >= SIZE_MAX || opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) { - zend_throw_exception(sodium_exception_ce, "crypto_pwhash_scryptsalsa208sha256(): invalid parameters", 0); + zend_throw_exception(sodium_exception_ce, "invalid parameters", 0); return; } if (passwd_len <= 0) { @@ -1664,7 +1661,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256) passwd, (unsigned long long) passwd_len, salt, (unsigned long long) opslimit, (size_t) memlimit) != 0) { zend_string_free(hash); - zend_throw_exception(sodium_exception_ce, "crypto_pwhash_scryptsalsa208sha256(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(hash)[hash_len] = 0; @@ -1685,7 +1682,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str) &opslimit, &memlimit) == FAILURE || opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) { zend_throw_exception(sodium_exception_ce, - "crypto_pwhash_scryptsalsa208sha256_str(): invalid parameters", + "invalid parameters", 0); return; } @@ -1706,7 +1703,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str) (ZSTR_VAL(hash_str), passwd, (unsigned long long) passwd_len, (unsigned long long) opslimit, (size_t) memlimit) != 0) { zend_string_free(hash_str); - zend_throw_exception(sodium_exception_ce, "crypto_pwhash_scryptsalsa208sha256_str(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(hash_str)[crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1] = 0; @@ -1725,7 +1722,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str_verify) &hash_str, &hash_str_len, &passwd, &passwd_len) == FAILURE) { zend_throw_exception(sodium_exception_ce, - "crypto_pwhash_scryptsalsa208sha256_str_verify(): invalid parameters", + "invalid parameters", 0); return; } @@ -1762,7 +1759,7 @@ PHP_FUNCTION(sodium_crypto_pwhash) &opslimit, &memlimit) == FAILURE || hash_len <= 0 || hash_len >= SIZE_MAX || opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) { - zend_throw_exception(sodium_exception_ce, "crypto_pwhash(): invalid parameters", 0); + zend_throw_exception(sodium_exception_ce, "invalid parameters", 0); return; } if (passwd_len <= 0) { @@ -1786,7 +1783,7 @@ PHP_FUNCTION(sodium_crypto_pwhash) (unsigned long long) opslimit, (size_t) memlimit, crypto_pwhash_alg_default()) != 0) { zend_string_free(hash); - zend_throw_exception(sodium_exception_ce, "crypto_pwhash(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(hash)[hash_len] = 0; @@ -1808,7 +1805,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_str) &opslimit, &memlimit) == FAILURE || opslimit <= 0 || memlimit <= 0 || memlimit > SIZE_MAX) { zend_throw_exception(sodium_exception_ce, - "crypto_pwhash_str(): invalid parameters", + "invalid parameters", 0); return; } @@ -1828,7 +1825,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_str) (ZSTR_VAL(hash_str), passwd, (unsigned long long) passwd_len, (unsigned long long) opslimit, (size_t) memlimit) != 0) { zend_string_free(hash_str); - zend_throw_exception(sodium_exception_ce, "crypto_pwhash_str(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(hash_str)[crypto_pwhash_STRBYTES - 1] = 0; @@ -1850,7 +1847,7 @@ PHP_FUNCTION(sodium_crypto_pwhash_str_verify) &hash_str, &hash_str_len, &passwd, &passwd_len) == FAILURE) { zend_throw_exception(sodium_exception_ce, - "crypto_pwhash_str_verify(): invalid parameters", + "invalid parameters", 0); return; } @@ -1898,7 +1895,6 @@ PHP_FUNCTION(sodium_crypto_aead_aes256gcm_encrypt) } if (npub_len != crypto_aead_aes256gcm_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_aes256gcm_encrypt(): " "public nonce size should be " "CRYPTO_AEAD_AES256GCM_NPUBBYTES bytes", 0); @@ -1906,7 +1902,6 @@ PHP_FUNCTION(sodium_crypto_aead_aes256gcm_encrypt) } if (secretkey_len != crypto_aead_aes256gcm_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_aes256gcm_encrypt(): " "secret key size should be " "CRYPTO_AEAD_AES256GCM_KEYBYTES bytes", 0); @@ -1923,7 +1918,7 @@ PHP_FUNCTION(sodium_crypto_aead_aes256gcm_encrypt) (unsigned long long) msg_len, ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_aead_aes256gcm_encrypt(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX || @@ -1961,7 +1956,6 @@ PHP_FUNCTION(sodium_crypto_aead_aes256gcm_decrypt) } if (npub_len != crypto_aead_aes256gcm_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_aes256gcm_decrypt(): " "public nonce size should be " "CRYPTO_AEAD_AES256GCM_NPUBBYTES bytes", 0); @@ -1969,7 +1963,6 @@ PHP_FUNCTION(sodium_crypto_aead_aes256gcm_decrypt) } if (secretkey_len != crypto_aead_aes256gcm_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_aes256gcm_decrypt(): " "secret key size should be " "CRYPTO_AEAD_AES256GCM_KEYBYTES bytes", 0); @@ -2024,7 +2017,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_encrypt) } if (npub_len != crypto_aead_chacha20poly1305_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_encrypt(): " "public nonce size should be " "CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES bytes", 0); @@ -2032,7 +2024,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_encrypt) } if (secretkey_len != crypto_aead_chacha20poly1305_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_encrypt(): " "secret key size should be " "CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES bytes", 0); @@ -2049,7 +2040,7 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_encrypt) (unsigned long long) msg_len, ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_aead_chacha20poly1305_encrypt(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX || @@ -2087,7 +2078,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_decrypt) } if (npub_len != crypto_aead_chacha20poly1305_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_decrypt(): " "public nonce size should be " "CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES bytes", 0); @@ -2095,7 +2085,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_decrypt) } if (secretkey_len != crypto_aead_chacha20poly1305_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_decrypt(): " "secret key size should be " "CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES bytes", 0); @@ -2150,7 +2139,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_encrypt) } if (npub_len != crypto_aead_chacha20poly1305_IETF_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_ietf_encrypt(): " "public nonce size should be " "CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES bytes", 0); @@ -2158,7 +2146,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_encrypt) } if (secretkey_len != crypto_aead_chacha20poly1305_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_ietf_encrypt(): " "secret key size should be " "CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES bytes", 0); @@ -2179,7 +2166,7 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_encrypt) (unsigned long long) msg_len, ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_aead_chacha20poly1305_ietf_encrypt(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX || @@ -2217,7 +2204,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_decrypt) } if (npub_len != crypto_aead_chacha20poly1305_IETF_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_ietf_decrypt(): " "public nonce size should be " "CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES bytes", 0); @@ -2225,7 +2211,6 @@ PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_decrypt) } if (secretkey_len != crypto_aead_chacha20poly1305_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_chacha20poly1305_ietf_decrypt(): " "secret key size should be " "CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES bytes", 0); @@ -2286,7 +2271,6 @@ PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt) } if (npub_len != crypto_aead_xchacha20poly1305_IETF_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_xchacha20poly1305_ietf_encrypt(): " "public nonce size should be " "CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES bytes", 0); @@ -2294,7 +2278,6 @@ PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt) } if (secretkey_len != crypto_aead_xchacha20poly1305_IETF_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_xchacha20poly1305_ietf_encrypt(): " "secret key size should be " "CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES bytes", 0); @@ -2315,7 +2298,7 @@ PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt) (unsigned long long) msg_len, ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) { zend_string_free(ciphertext); - zend_throw_exception(sodium_exception_ce, "crypto_aead_xchacha20poly1305_ietf_encrypt(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX || @@ -2353,7 +2336,6 @@ PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt) } if (npub_len != crypto_aead_xchacha20poly1305_IETF_NPUBBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_xchacha20poly1305_ietf_decrypt(): " "public nonce size should be " "CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES bytes", 0); @@ -2361,7 +2343,6 @@ PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt) } if (secretkey_len != crypto_aead_xchacha20poly1305_IETF_KEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_aead_xchacha20poly1305_ietf_decrypt(): " "secret key size should be " "CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES bytes", 0); @@ -2465,7 +2446,7 @@ PHP_FUNCTION(sodium_crypto_scalarmult) } if (n_len != crypto_scalarmult_SCALARBYTES || p_len != crypto_scalarmult_SCALARBYTES) { - zend_throw_exception(sodium_exception_ce, "crypto_scalarmult(): scalar and point must be " + zend_throw_exception(sodium_exception_ce, "scalar and point must be " "CRYPTO_SCALARMULT_SCALARBYTES bytes", 0); return; @@ -2473,7 +2454,7 @@ PHP_FUNCTION(sodium_crypto_scalarmult) q = zend_string_alloc(crypto_scalarmult_BYTES, 0); if (crypto_scalarmult((unsigned char *) ZSTR_VAL(q), n, p) != 0) { zend_string_free(q); - zend_throw_exception(sodium_exception_ce, "crypto_scalarmult(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(q)[crypto_scalarmult_BYTES] = 0; @@ -2481,56 +2462,197 @@ PHP_FUNCTION(sodium_crypto_scalarmult) RETURN_STR(q); } -PHP_FUNCTION(sodium_crypto_kx) +PHP_FUNCTION(sodium_crypto_kx_seed_keypair) +{ + unsigned char *sk; + unsigned char *pk; + unsigned char *seed; + size_t seed_len; + zend_string *keypair; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", + &seed, &seed_len) == FAILURE) { + return; + } + if (seed_len != crypto_kx_SEEDBYTES) { + zend_throw_exception(sodium_exception_ce, "seed must be CRYPTO_KX_SEEDBYTES bytes", 0); + return; + } + (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_PUBLICKEYBYTES ? 1 : -1]); + (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_SECRETKEYBYTES ? 1 : -1]); + keypair = zend_string_alloc(crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES, 0); + sk = (unsigned char *) ZSTR_VAL(keypair); + pk = sk + crypto_kx_SECRETKEYBYTES; + crypto_generichash(sk, crypto_kx_SECRETKEYBYTES, + seed, crypto_kx_SEEDBYTES, NULL, 0); + if (crypto_scalarmult_base(pk, sk) != 0) { + zend_throw_exception(sodium_exception_ce, "internal error", 0); + return; + } + ZSTR_VAL(keypair)[crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES] = 0; + RETURN_STR(keypair); +} + +PHP_FUNCTION(sodium_crypto_kx_keypair) +{ + unsigned char *sk; + unsigned char *pk; + zend_string *keypair; + + keypair = zend_string_alloc(crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES, 0); + sk = (unsigned char *) ZSTR_VAL(keypair); + pk = sk + crypto_kx_SECRETKEYBYTES; + randombytes_buf(sk, crypto_kx_SECRETKEYBYTES); + if (crypto_scalarmult_base(pk, sk) != 0) { + zend_throw_exception(sodium_exception_ce, "internal error", 0); + return; + } + RETURN_STR(keypair); +} + +PHP_FUNCTION(sodium_crypto_kx_secretkey) +{ + zend_string *secretkey; + unsigned char *keypair; + size_t keypair_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", + &keypair, &keypair_len) == FAILURE) { + return; + } + if (keypair_len != + crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) { + zend_throw_exception(sodium_exception_ce, + "keypair should be CRYPTO_KX_KEYPAIRBYTES bytes", + 0); + return; + } + secretkey = zend_string_alloc(crypto_kx_SECRETKEYBYTES, 0); + memcpy(ZSTR_VAL(secretkey), keypair, crypto_kx_SECRETKEYBYTES); + ZSTR_VAL(secretkey)[crypto_kx_SECRETKEYBYTES] = 0; + + RETURN_STR(secretkey); +} + +PHP_FUNCTION(sodium_crypto_kx_publickey) +{ + zend_string *publickey; + unsigned char *keypair; + size_t keypair_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", + &keypair, &keypair_len) == FAILURE) { + return; + } + if (keypair_len != + crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) { + zend_throw_exception(sodium_exception_ce, + "keypair should be CRYPTO_KX_KEYPAIRBYTES bytes", + 0); + return; + } + publickey = zend_string_alloc(crypto_kx_PUBLICKEYBYTES, 0); + memcpy(ZSTR_VAL(publickey), keypair + crypto_kx_SECRETKEYBYTES, + crypto_kx_PUBLICKEYBYTES); + ZSTR_VAL(publickey)[crypto_kx_PUBLICKEYBYTES] = 0; + + RETURN_STR(publickey); +} + +PHP_FUNCTION(sodium_crypto_kx_client_session_keys) { crypto_generichash_state h; - unsigned char q[crypto_scalarmult_BYTES]; - zend_string *sharedkey; - unsigned char *client_publickey; - unsigned char *publickey; - unsigned char *secretkey; - unsigned char *server_publickey; - size_t client_publickey_len; - size_t publickey_len; - size_t secretkey_len; - size_t server_publickey_len; + unsigned char q[crypto_scalarmult_BYTES]; + unsigned char *keypair; + unsigned char *client_sk; + unsigned char *client_pk; + unsigned char *server_pk; + unsigned char session_keys[2 * crypto_kx_SESSIONKEYBYTES]; + size_t keypair_len; + size_t server_pk_len; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss", - &secretkey, &secretkey_len, - &publickey, &publickey_len, - &client_publickey, &client_publickey_len, - &server_publickey, &server_publickey_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", + &keypair, &keypair_len, + &server_pk, &server_pk_len) == FAILURE) { return; } - if (secretkey_len != crypto_kx_SECRETKEYBYTES) { - zend_throw_exception(sodium_exception_ce, "crypto_kx(): secret key must be CRYPTO_KX_SECRETKEY bytes", 0); + if (keypair_len != crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) { + zend_throw_exception(sodium_exception_ce, "keypair must be CRYPTO_KX_KEYPAIRBYTES bytes", 0); return; } - if (publickey_len != crypto_kx_PUBLICKEYBYTES || - client_publickey_len != crypto_kx_PUBLICKEYBYTES || - server_publickey_len != crypto_kx_PUBLICKEYBYTES) { - zend_throw_exception(sodium_exception_ce, "crypto_kx(): public keys must be CRYPTO_KX_PUBLICKEY bytes", 0); + if (server_pk_len != crypto_kx_PUBLICKEYBYTES) { + zend_throw_exception(sodium_exception_ce, "public keys must be CRYPTO_KX_PUBLICKEYBYTES bytes", 0); return; } - (void) sizeof(int[crypto_scalarmult_SCALARBYTES == - crypto_kx_PUBLICKEYBYTES ? 1 : -1]); - (void) sizeof(int[crypto_scalarmult_SCALARBYTES == - crypto_kx_SECRETKEYBYTES ? 1 : -1]); - if (crypto_scalarmult(q, secretkey, publickey) != 0) { - zend_throw_exception(sodium_exception_ce, "crypto_kx(): internal error", 0); + client_sk = &keypair[0]; + client_pk = &keypair[crypto_kx_SECRETKEYBYTES]; + (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_PUBLICKEYBYTES ? 1 : -1]); + (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_SECRETKEYBYTES ? 1 : -1]); + if (crypto_scalarmult(q, client_sk, server_pk) != 0) { + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } - sharedkey = zend_string_alloc(crypto_kx_BYTES, 0); - crypto_generichash_init(&h, NULL, 0U, crypto_generichash_BYTES); + crypto_generichash_init(&h, NULL, 0U, 2 * crypto_kx_SESSIONKEYBYTES); crypto_generichash_update(&h, q, sizeof q); sodium_memzero(q, sizeof q); - crypto_generichash_update(&h, client_publickey, client_publickey_len); - crypto_generichash_update(&h, server_publickey, server_publickey_len); - crypto_generichash_final(&h, (unsigned char *) ZSTR_VAL(sharedkey), - crypto_kx_BYTES); - ZSTR_VAL(sharedkey)[crypto_kx_BYTES] = 0; + crypto_generichash_update(&h, client_pk, crypto_kx_PUBLICKEYBYTES); + crypto_generichash_update(&h, server_pk, crypto_kx_PUBLICKEYBYTES); + crypto_generichash_final(&h, session_keys, 2 * crypto_kx_SESSIONKEYBYTES); + array_init(return_value); + add_next_index_stringl(return_value, + (const char *) session_keys, + crypto_kx_SESSIONKEYBYTES); + add_next_index_stringl(return_value, + (const char *) session_keys + crypto_kx_SESSIONKEYBYTES, + crypto_kx_SESSIONKEYBYTES); +} - RETURN_STR(sharedkey); +PHP_FUNCTION(sodium_crypto_kx_server_session_keys) +{ + crypto_generichash_state h; + unsigned char q[crypto_scalarmult_BYTES]; + unsigned char *keypair; + unsigned char *server_sk; + unsigned char *server_pk; + unsigned char *client_pk; + unsigned char session_keys[2 * crypto_kx_SESSIONKEYBYTES]; + size_t keypair_len; + size_t client_pk_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", + &keypair, &keypair_len, + &client_pk, &client_pk_len) == FAILURE) { + return; + } + if (keypair_len != crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) { + zend_throw_exception(sodium_exception_ce, "keypair must be CRYPTO_KX_KEYPAIRBYTES bytes", 0); + return; + } + if (client_pk_len != crypto_kx_PUBLICKEYBYTES) { + zend_throw_exception(sodium_exception_ce, "public keys must be CRYPTO_KX_PUBLICKEYBYTES bytes", 0); + return; + } + server_sk = &keypair[0]; + server_pk = &keypair[crypto_kx_SECRETKEYBYTES]; + (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_PUBLICKEYBYTES ? 1 : -1]); + (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_SECRETKEYBYTES ? 1 : -1]); + if (crypto_scalarmult(q, server_sk, client_pk) != 0) { + zend_throw_exception(sodium_exception_ce, "internal error", 0); + return; + } + crypto_generichash_init(&h, NULL, 0U, 2 * crypto_kx_SESSIONKEYBYTES); + crypto_generichash_update(&h, q, sizeof q); + sodium_memzero(q, sizeof q); + crypto_generichash_update(&h, client_pk, crypto_kx_PUBLICKEYBYTES); + crypto_generichash_update(&h, server_pk, crypto_kx_PUBLICKEYBYTES); + crypto_generichash_final(&h, session_keys, 2 * crypto_kx_SESSIONKEYBYTES); + array_init(return_value); + add_next_index_stringl(return_value, + (const char *) session_keys + crypto_kx_SESSIONKEYBYTES, + crypto_kx_SESSIONKEYBYTES); + add_next_index_stringl(return_value, + (const char *) session_keys, + crypto_kx_SESSIONKEYBYTES); } PHP_FUNCTION(sodium_crypto_auth) @@ -2547,14 +2669,14 @@ PHP_FUNCTION(sodium_crypto_auth) return; } if (key_len != crypto_auth_KEYBYTES) { - zend_throw_exception(sodium_exception_ce, "crypto_auth(): key must be CRYPTO_AUTH_KEYBYTES bytes", 0); + zend_throw_exception(sodium_exception_ce, "key must be CRYPTO_AUTH_KEYBYTES bytes", 0); return; } mac = zend_string_alloc(crypto_auth_BYTES, 0); if (crypto_auth((unsigned char *) ZSTR_VAL(mac), (const unsigned char *) msg, msg_len, (const unsigned char *) key) != 0) { - zend_throw_exception(sodium_exception_ce, "crypto_auth(): internal error", 0); + zend_throw_exception(sodium_exception_ce, "internal error", 0); return; } ZSTR_VAL(mac)[crypto_auth_BYTES] = 0; @@ -2578,11 +2700,11 @@ PHP_FUNCTION(sodium_crypto_auth_verify) return; } if (key_len != crypto_auth_KEYBYTES) { - zend_throw_exception(sodium_exception_ce, "crypto_auth_verify(): key must be CRYPTO_AUTH_KEYBYTES bytes", 0); + zend_throw_exception(sodium_exception_ce, "key must be CRYPTO_AUTH_KEYBYTES bytes", 0); return; } if (mac_len != crypto_auth_BYTES) { - zend_throw_exception(sodium_exception_ce, "crypto_auth_verify(): authentication tag must be CRYPTO_AUTH_BYTES bytes", 0); + zend_throw_exception(sodium_exception_ce, "authentication tag must be CRYPTO_AUTH_BYTES bytes", 0); return; } if (crypto_auth_verify((const unsigned char *) mac, @@ -2605,7 +2727,6 @@ PHP_FUNCTION(sodium_crypto_sign_ed25519_sk_to_curve25519) } if (eddsakey_len != crypto_sign_SECRETKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_ed25519_sk_to_curve25519(): " "Ed25519 key should be CRYPTO_SIGN_SECRETKEYBYTES bytes", 0); return; @@ -2614,7 +2735,7 @@ PHP_FUNCTION(sodium_crypto_sign_ed25519_sk_to_curve25519) if (crypto_sign_ed25519_sk_to_curve25519((unsigned char *) ZSTR_VAL(ecdhkey), (const unsigned char *) eddsakey) != 0) { - zend_throw_exception(sodium_exception_ce, "crypto_sign_ed25519_sk_to_curve25519()", 0); + zend_throw_exception(sodium_exception_ce, "conversion failed", 0); return; } ZSTR_VAL(ecdhkey)[crypto_box_SECRETKEYBYTES] = 0; @@ -2634,7 +2755,6 @@ PHP_FUNCTION(sodium_crypto_sign_ed25519_pk_to_curve25519) } if (eddsakey_len != crypto_sign_PUBLICKEYBYTES) { zend_throw_exception(sodium_exception_ce, - "crypto_sign_ed25519_pk_to_curve25519(): " "Ed25519 key should be CRYPTO_SIGN_PUBLICKEYBYTES bytes", 0); return; @@ -2643,7 +2763,7 @@ PHP_FUNCTION(sodium_crypto_sign_ed25519_pk_to_curve25519) if (crypto_sign_ed25519_pk_to_curve25519((unsigned char *) ZSTR_VAL(ecdhkey), (const unsigned char *) eddsakey) != 0) { - zend_throw_exception(sodium_exception_ce, "crypto_sign_ed25519_pk_to_curve25519()", 0); + zend_throw_exception(sodium_exception_ce, "conversion failed", 0); return; } ZSTR_VAL(ecdhkey)[crypto_box_PUBLICKEYBYTES] = 0; @@ -2666,7 +2786,7 @@ PHP_FUNCTION(sodium_compare) return; } if (len1 != len2) { - zend_throw_exception(sodium_exception_ce, "compare(): arguments have different sizes", 0); + zend_throw_exception(sodium_exception_ce, "arguments have different sizes", 0); } else { RETURN_LONG(sodium_compare((const unsigned char *) buf1, (const unsigned char *) buf2, (size_t) len1)); diff --git a/ext/sodium/php_libsodium.h b/ext/sodium/php_libsodium.h index a24c3e4fa5c..6e6e3fbe2d8 100644 --- a/ext/sodium/php_libsodium.h +++ b/ext/sodium/php_libsodium.h @@ -59,7 +59,12 @@ PHP_FUNCTION(sodium_crypto_generichash); PHP_FUNCTION(sodium_crypto_generichash_final); PHP_FUNCTION(sodium_crypto_generichash_init); PHP_FUNCTION(sodium_crypto_generichash_update); -PHP_FUNCTION(sodium_crypto_kx); +PHP_FUNCTION(sodium_crypto_kx_client_session_keys); +PHP_FUNCTION(sodium_crypto_kx_keypair); +PHP_FUNCTION(sodium_crypto_kx_publickey); +PHP_FUNCTION(sodium_crypto_kx_secretkey); +PHP_FUNCTION(sodium_crypto_kx_seed_keypair); +PHP_FUNCTION(sodium_crypto_kx_server_session_keys); PHP_FUNCTION(sodium_crypto_pwhash); PHP_FUNCTION(sodium_crypto_pwhash_str); PHP_FUNCTION(sodium_crypto_pwhash_str_verify); @@ -96,15 +101,9 @@ PHP_FUNCTION(sodium_add); PHP_FUNCTION(sodium_memcmp); PHP_FUNCTION(sodium_memzero); -#ifndef crypto_kx_BYTES #define crypto_kx_BYTES crypto_scalarmult_BYTES -#endif -#ifndef crypto_kx_PUBLICKEYBYTES #define crypto_kx_PUBLICKEYBYTES crypto_scalarmult_SCALARBYTES -#endif -#ifndef crypto_kx_SECRETKEYBYTES #define crypto_kx_SECRETKEYBYTES crypto_scalarmult_SCALARBYTES -#endif #endif /* PHP_LIBSODIUM_H */ diff --git a/ext/sodium/tests/crypto_auth.phpt b/ext/sodium/tests/crypto_auth.phpt index 25ddff540f3..b2017580669 100644 --- a/ext/sodium/tests/crypto_auth.phpt +++ b/ext/sodium/tests/crypto_auth.phpt @@ -41,7 +41,7 @@ var_dump(sodium_crypto_auth_verify($badmac, $msg, $key)); ?> --EXPECT-- bool(true) -crypto_auth(): key must be CRYPTO_AUTH_KEYBYTES bytes +key must be CRYPTO_AUTH_KEYBYTES bytes bool(false) bool(false) bool(false) diff --git a/ext/sodium/tests/crypto_box.phpt b/ext/sodium/tests/crypto_box.phpt index ef357147144..ad17c9568be 100644 --- a/ext/sodium/tests/crypto_box.phpt +++ b/ext/sodium/tests/crypto_box.phpt @@ -142,7 +142,7 @@ bool(true) bool(true) bool(true) bool(true) -crypto_box(): keypair size should be CRYPTO_BOX_KEYPAIRBYTES bytes +keypair size should be CRYPTO_BOX_KEYPAIRBYTES bytes bool(true) string(17) "Hi, this is Alice" string(21) "Hi Alice! This is Bob" diff --git a/ext/sodium/tests/crypto_kx.phpt b/ext/sodium/tests/crypto_kx.phpt index ef8c5f7a634..a1dd00a0a73 100644 --- a/ext/sodium/tests/crypto_kx.phpt +++ b/ext/sodium/tests/crypto_kx.phpt @@ -4,34 +4,31 @@ Check for libsodium-based key exchange --FILE-- --EXPECT-- -string(64) "509a1580c2ee30c565317e29e0fea0b1c232e0ef3a7871d91dc64814b19a3bd2" -string(64) "509a1580c2ee30c565317e29e0fea0b1c232e0ef3a7871d91dc64814b19a3bd2" -bool(true) +string(128) "b85c84f9828524519d32b97cd3dda961fdba2dbf407ae4601e2129229aa463c224eaf70f070a925d6d5176f20495d4d90867624d9a10379e2a9aef0955c9bf4e" +string(128) "016e814c32b8b66225a403db45bf50fdd1966fb802c3115bf8aa90738c6a02de420ccdb534930fed9aaff12188bedc76e66251f399c404f2e4a15678fd4a484a" +string(64) "99a430e61d718b71979ebcea6735c4648bc828cfb456890aeda4b628b77d5ac7" +string(64) "99a430e61d718b71979ebcea6735c4648bc828cfb456890aeda4b628b77d5ac7" +string(64) "876bef865a5ab3f4ae569ea5aaefe5014c3ec22a558c0a2f0274aa9985bd328d" +string(64) "876bef865a5ab3f4ae569ea5aaefe5014c3ec22a558c0a2f0274aa9985bd328d" diff --git a/ext/sodium/tests/exception_trace_without_args.phpt b/ext/sodium/tests/exception_trace_without_args.phpt index c821df0eb56..f8f172c5443 100644 --- a/ext/sodium/tests/exception_trace_without_args.phpt +++ b/ext/sodium/tests/exception_trace_without_args.phpt @@ -14,7 +14,7 @@ do_memzero($x); ?> --EXPECTF-- -Fatal error: Uncaught SodiumException: memzero: a PHP string is required in %s:%d +Fatal error: Uncaught SodiumException: a PHP string is required in %s:%d Stack trace: #0 %s(%d): sodium_memzero() #1 %s(%d): do_memzero() diff --git a/ext/sodium/tests/inc_add.phpt b/ext/sodium/tests/inc_add.phpt index 2ab9b92696f..4397bdc44f0 100644 --- a/ext/sodium/tests/inc_add.phpt +++ b/ext/sodium/tests/inc_add.phpt @@ -43,12 +43,12 @@ var_dump($str, $str2); ?> --EXPECT-- -increment(): a PHP string is required +a PHP string is required string(3) "bbc" string(3) "abc" string(3) "bbc" string(3) "abc" -add(): PHP strings are required +PHP strings are required string(3) "cbc" string(3) "abc" string(3) "cbc" diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index 41654625a47..049b517c46d 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -72,6 +72,36 @@ static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ } } /* }}} */ +static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */ +{ + spl_filesystem_object *intern = spl_filesystem_from_obj(object); + + zend_objects_destroy_object(object); + + switch(intern->type) { + case SPL_FS_DIR: + if (intern->u.dir.dirp) { + php_stream_close(intern->u.dir.dirp); + intern->u.dir.dirp = NULL; + } + break; + case SPL_FS_FILE: + if (intern->u.file.stream) { + /* + if (intern->u.file.zcontext) { + zend_list_delref(Z_RESVAL_P(intern->zcontext)); + } + */ + if (!intern->u.file.stream->is_persistent) { + php_stream_close(intern->u.file.stream); + } else { + php_stream_pclose(intern->u.file.stream); + } + } + break; + } +} /* }}} */ + static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */ { spl_filesystem_object *intern = spl_filesystem_from_obj(object); @@ -92,26 +122,12 @@ static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */ case SPL_FS_INFO: break; case SPL_FS_DIR: - if (intern->u.dir.dirp) { - php_stream_close(intern->u.dir.dirp); - intern->u.dir.dirp = NULL; - } if (intern->u.dir.sub_path) { efree(intern->u.dir.sub_path); } break; case SPL_FS_FILE: if (intern->u.file.stream) { - /* - if (intern->u.file.zcontext) { - zend_list_delref(Z_RESVAL_P(intern->zcontext)); - } - */ - if (!intern->u.file.stream->is_persistent) { - php_stream_close(intern->u.file.stream); - } else { - php_stream_pclose(intern->u.file.stream); - } if (intern->u.file.open_mode) { efree(intern->u.file.open_mode); } @@ -3108,7 +3124,7 @@ PHP_MINIT_FUNCTION(spl_directory) spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone; spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast; spl_filesystem_object_handlers.get_debug_info = spl_filesystem_object_get_debug_info; - spl_filesystem_object_handlers.dtor_obj = zend_objects_destroy_object; + spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object; spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage; spl_ce_SplFileInfo->serialize = zend_class_serialize_deny; spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;