Let's take this PHP code. We use libsodium and libsodium-php.
You have $text
, $senderPrivateKey
, $recipientPublicKey
and a $nonce
. The latter was obviously created like this by just using Sodium:
$nonce = \Sodium\randombytes_buf($size);
Now this is the code which adds PKCS#7 padding:
/* determine random amount of PKCS7 padding */
$padBytes = mt_rand(1, 255);
/* append padding */
$textBytes .= str_repeat(chr($padBytes), $padBytes);
Afterwards this is encrypted using \Sodium\crypto_box_keypair_from_secretkey_and_publickey
and \Sodium\crypto_box
.
So is it okay to use mt_rand here? I mean in the PHP documentation it is discouraged to use this for any "cryptographic purposes". However here it is only used for choosing a ("random") character and the length of the padding.
So is this secure? Would using openssl_random_pseudo_bytes()
, random_bytes
(in PHP 7) or libsodiums \Sodium\randombytes_buf
(like it is used for the nonce) be more secure?
And is this a correct implementation of PKCS#7? Especially as the same variable is used for the length and the character of the padding.
Edit: As there was some confusing here some more information: Actually this implementation is only supposed to hide the file size (especially for short pieces of data) a bit to prevent an attacker from guessing the plain-text message. This is implemented in some open-source libraries related to Threema and here is an issue about this. So basically in the Threema client a cryptographically secure RNG is used where in this example here a unsecure PRNG is used. And this is what this question is about: Is it less secure to use a PRNG here?