In my project I am using Libsodium, in particular I am using the stream cryptography,trying it out gave me a few questions:
Nonce:
- What is it?
- What is it for?
- Does it have to be secret for the security of the message?
Header:
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
- What is it?
- What is it for?
- What does it contain?
- Does it have to be secret for the security of the message?
State:
crypto_secretstream_xchacha20poly1305_state state;
- What is it?
- What is it for?
In the documentation you can see 2 examples
The first concerns 2 functions, one for encrypting and the other for decrypting a file, what it does in brief is:
Function to encrypt:
- Open the original file and the target file.
- You initialise a state
- You first write the header inside the target file
- A loop is repeated until the end of the original file in which a block of the file is encrypted and the product is written into the target file.
Function to decrypt:
- Open the original file and the target file.
- You initialise a state
- The header is read and stored in a variable.
- A loop is repeated until the end of the original file where a block is taken, decrypted and the product is written to the target file.
The second example only concerns an encryption with a subsequent decryption of a message stream.
The difference between the two examples, apart from being in the fact that one concerns files while the other only concerns a message stream, lies in the fact that in the first example the header is stored in a file and then in the decryption it is read, while in the message stream it is not stored anywhere, so if the encryption and decryption phases were separated it would no longer work.
So in the case of the message stream, be it several messages OR JUST ONE, where do I put the header?