|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-02-21 13:15 UTC] thomaswouters+bugs dot php dot net at gmail dot com
Description:
------------
sodium_crypto_sign_detached() behaves irregular on a legacy system (I guess it might be related to an old gcc version - 4.7.2).
Using the function sodium_crypto_sign_detached() in php-fpm resulted in a fatal error:
PHP Fatal error: Uncaught SodiumException: secret key size should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes in /tmp/sodium.php
I was able to reproduce it with php command line on older systems by enabling opcache (-d "opcache.enable_cli=On") but not on Debian stretch, buster, Archlinux or Alpine.
Toggling opcache in php-fpm did not make a difference and the exception was thrown regardless.
When built with --enable-debug PHP throws an assertion error on `ZEND_ASSERT(ZSTR_VAL(signature)[signature_real_len] == 0);`:
Warning: String is not zero-terminated (*garbage*) in Unknown on line 0
After taking a look at sodium_crypto_sign() I've noticed that there's some extra code to zero-terminate the signed message:
PHP_SODIUM_ZSTR_TRUNCATE(msg_signed, (size_t) msg_signed_real_len);
ZSTR_VAL(msg_signed)[msg_signed_real_len] = 0;
I've replaced the assertion in sodium_crypto_sign_detached() with the following code and was unable to reproduce the SodiumException both on cli as php-fpm:
PHP_SODIUM_ZSTR_TRUNCATE(signature, (size_t) signature_real_len);
ZSTR_VAL(signature)[signature_real_len] = 0;
Test script:
---------------
<?php
$alice_sk = base64_decode('NNiJwjbkZ/5zUEj8KW8HENU34RVZ22XmvqFLj2xhlUa6ht6V6u/t97mfF6hW8UQgEvOdA/JSz/grVFVxoM5Y5g==');
$message = 'This is a test message.';
$signature = sodium_crypto_sign_detached($message, $alice_sk);
var_dump($signature);
Expected result:
----------------
string(88) "Fb5LHxwnrUnmNzdc01sEBJpgi+milnYjWagSiS4WfCmdjC4XOHIF753unPMSLAmmYQqjhS3raQfHs/02QQGoDA=="
Actual result:
--------------
Fatal error: Uncaught SodiumException: secret key size should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes in /tmp/sodium.php:4
Stack trace:
#0 /tmp/sodium.php(4): sodium_crypto_sign_detached()
#1 {main}
thrown in /tmp/sodium.php on line 4
Patchessodium_crypto_sign_detached-zero-termination.patch (last revision 2019-02-21 13:15 UTC by thomaswouters+bugs dot php dot net at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
Test script should have been: <?php $alice_sk = base64_decode('NNiJwjbkZ/5zUEj8KW8HENU34RVZ22XmvqFLj2xhlUa6ht6V6u/t97mfF6hW8UQgEvOdA/JSz/grVFVxoM5Y5g=='); $message = 'This is a test message.'; $signature = sodium_crypto_sign_detached($message, $alice_sk); var_dump(base64_encode($signature));