Authenticated Encryption With Associated Data
This is a form of symmetric encryption, that assures both confidentiality and authenticity of the data to be encrypted as well as associated data that will not be encrypted.
Offered here are three implementations of (x)ChaCha20-Poly1305 construction:
ChaCha20Poly1305 - uses 64bit nonce, safe to encrypt
ChaCha20Poly1305-IETF - uses 96bit nonce (standardised by RFC8439
XChaCha20Poly1305 - uses 192bit nonce - recommended choice
The only difference is the size of the nonce, and how is the nonce used.
(x)ChaCha20 is a streaming cipher develop by Daniel J. Bernstein. He is also the author of Poly1305 a fast Message Authentication Code system
Libsodium offers two additional variants for each of the aforementioned variants:
Combined
Detached
Combined mode returns encrypted data and tag as one UByteArray, while detached mode returns them as separate UByteArrays. To be kotlin idiomatic we are returning detached tag and encrypted data inside a wrapper data class AeadEncryptedDataAndTag
Also provided are key generation convenience functions for each variant. (Which is in practice the same, since the keys same length for each variant)
Functions
Check if authentication data (tag) is correct, then decrypt the message and return decrypted data. Using ChaChaPoly1305 (64 bit nonce)
Check if authentication data (tag) is correct, then decrypt the message and return decrypted data. Using xChaChaPoly1305 (64 bit nonce)
Encrypt the message and return encrypted data and tag using ChaChaPoly1305 (64 bit nonce)
Encrypt the message and return encrypted data and tag using ChaChaPoly1305 (64 bit nonce) as separate arrays (but wrapped inside AeadEncryptedDataAndTag)
Check if authentication data (tag) is correct, then decrypt the message and return decrypted data. Using ChaChaPoly1305-IETF (96 bit nonce)
Check if authentication data (tag) is correct, then decrypt the message and return decrypted data. Using xChaChaPoly1305 (96 bit nonce)
Encrypt the message and return encrypted data and tag using ChaChaPoly1305-IETF (96 bit nonce)
Encrypt the message and return encrypted data and tag using ChaChaPoly1305-IETF (96 bit nonce) as separate arrays (but wrapped inside AeadEncryptedDataAndTag)
Generate a random 32 byte key for use with ChaCha20Poly1305-IETF
Generate a random 32 byte key for use with ChaCha20Poly1305
Check if authentication data (tag) is correct, then decrypt the message and return decrypted data. Using xChaChaPoly1305 (192 bit nonce)
Check if authentication data (tag) is correct, then decrypt the message and return decrypted data. Using xChaChaPoly1305 (192 bit nonce)
Encrypt the message and return encrypted data and tag using xChaChaPoly1305 (192 bit nonce)
Encrypt the message and return encrypted data and tag using xChaChaPoly1305 (192 bit nonce) as separate arrays (but wrapped inside AeadEncryptedDataAndTag)
Generate a random 32byte key for use with xChaCha20Poly1305