How to use Libsodium encryption (in PHP) in the most secure possible way? In a PHP project in which fool-proof encryption is fundamental, I'm using the Libsodium library.

Hope you guys can shine some light on the following questions.


For all of the following functions, it looks like the length of the encrypted message is directly proportional to the length of the unencrypted message:


sodium_crypto_secretbox
sodium_crypto_box
sodium_crypto_box_seal


It looks like no padding is used by default, correct ?
This means that if within my setup I use different types of messages which each have a different length, an attacker can easily derive what kind of message it's about (without however knowing the content of the message), right ?

To mitigate this risk, I want to implement padding and I am looking for some advise on how to do this.

Is appending a random length random content (using random_bytes) message to the unencrypted message a good idea ? Does it make a difference whether I append it to the end of the message, at the beginning of the message or somewhere in the middle (or a combination of all) ?
Are there more efficient ways to do this ?
If part(s) of the unencrypted messages are known, is it possible for an attacker to crack the encryption ? Or does it somehow becomes easier to do so ?

Example: if each encrypted message contains data delimited by for example "," or if each message starts/ends with a certain string or contains a certain string somewhere in the message, does this make attacks easier ?
I mean, even when using padding an attacker might still know for sure part of the message's content.

I previously asked a question about how safe the encryption is (How safe are Libsodium Crypto Boxes?). Someone replied that 2^128 tries would be required to crack it. Does the amount of tries decrease if part of the message is known ? If yes by how much does it decrease ?
What if someone knows the entire unencrypted message (for example the receiver) ? How easy/hard would it be for him to crack the senders' secret key ?
What about signed messages in Libsodium ? I know that signing does not encrypt the message but only appends a signature to it. How easy/hard is it to crack a SK based on a signature ? Could the receiver of a signed message potentially create a valid signature for a message not created by the sender ?
I read that nonces can be public without a problem. Nonetheless, does the fact that an attacker is in the possession of the nonce of a message decrease the cracking difficulty ?
Is it a good idea to change keypairs periodically to increase security ? Or doesn't this make sense ?
Does multi layer encryption make sense ? For example putting a crypto box inside another crypto box (with different keypairs off course). Or is this a bad idea ?
What about timing attacks ? I read that the time needed to decrypt/encrypt a message might give some clues about the SK. If Bob wants to crack Alices' SK, can he do so by analyzing how long it takes him to decrypt her message ? Or does it at least decrease the difficulty to do so ? If yes by how much ?
No need to implement padding, as it's already available. See the second example of the libsodium-PHP module.
Partially-known plaintext has no implication on security, especially in counter mode where the encryption of a block is independent from the previous blocks.
Creating a valid signature requires the secret key. Ed25519 has a 2^128 security target. The best attacks known actually cost more than 2^140 bit operations on average, and degrade quadratically in success probability as the number of bit operations drops.
Nonces can be public. Keeping them private doesn't significantly change the security level. However, nonces should never be used with different messages and the same key. See this page for a discussion on libsodium and nonces.
Yes. This doesn't make the cipher stronger, but mitigates the implications of a leaked secret key. If you are encrypting very large data streams, the secretstream API will automatically rotate the keys for you.
Salsa20 already has a huge key space. A salsa20-in-salsa20 cascade would have no benefits, and would be twice a slow.
Libsodium does its best to avoid side channels. Leaks are more likely to happen in the PHP code.