Cryptography - ChaCha20 Encryption Algorithm



Encryption is one of the most effective ways to secure data, and ChaCha20 is one of the most widely utilised encryption algorithms available today. ChaCha20 is a secure and fast encryption method that can be implemented in a variety of applications.

This chapter will discuss the ChaCha20 encryption algorithm, how it works, and how it differs from other encryption algorithms.

History of ChaCha20

Daniel J. Bernstein developed two algorithms: Poly1305 and ChaCha20, between 2005 and 2008. Between 2013 and 2014, variations of these algorithms were included into an IETF proposal for use in TLS and DTLS, which Google favoured for security and performance.

They were ultimately accepted by OpenSSH. RFCs for protocols such as TLS, DTLS, and IPsec adopted the combined method in 2015. Cloudflare has also adopted it. In June 2018, RFC 7539 was replaced by RFC 8439.

What is the ChaCha20 Encryption?

ChaCha20 is a symmetric encryption approach that encrypts and decrypts data with the same 256-bit key.

Daniel J. Bernstein, a well-known cryptographer, created it as a stream cipher back in 2008.

The ChaCha20 encryption algorithm is intended to offer a balance of speed and security.

It is designed to handle known attacks like differential cryptanalysis and linear cryptanalysis. Furthermore, it is highly parallelizable, making it easily transferable to multi-core CPUs and other high-performance computing platforms.

How does it Work?

ChaCha20 is a stream cipher, which encrypts data in continuous streams rather than fixed-size blocks.

It produces a continuous keystream of pseudo-random bits, which are subsequently XORed with the plaintext data to form the ciphertext.

Algorithm

Here are the basic steps in the ChaCha20 encryption algorithm −

  • The first step is key generation. In this, the ChaCha20 algorithm creates a 256-bit key using a user-supplied key and a randomly produced 96-bit nonce.
  • The second step if Initialization in which the algorithm uses the key and nonce to set up the cipher's state.
  • Third step is data encryption in which ChaCha20 encrypts each data block with the current cipher state, which is changed after each block is processed.
  • And the last step is Output in which the ciphertext is generated by XORing the plaintext with the output of the data encryption step.

Key Generation

Private keys for ChaCha do not have to take a specific shape; they simply need to be (crypto-secure) random bits of the needed size. Other methods, like RSA or EC, need the values to meet certain mathematical conditions, but ChaCha keys do not.

However, it is critical to ensure that the key is generated correctly, as otherwise it can be a means of attack - and possibly a very easy one to attack if the key creation is not "random enough".

Here are a few rules that you should consider while implementing ChaCha key generation −

  • If possible, use a CSPRNG (Cryptographically Secured Pseudo Random Number Generator) or an HSM (Hardware Security Module).
  • Otherwise, make sure to correctly seed your random number generator.
  • Always source a key generator with new randomness; don't produce numerous keys using the same random number seed.
  • Produce keys where they are going to be needed and kept; for example, don't produce keys on the server to use on the client; instead, generate them on the client side.
  • Keep private keys secure.
  • Avoid transferring private keys.
  • It is strongly advised to renegotiate or rotate keys as frequently as possible. Don't see a Private Key as something "permanently" connected to a person or a node, but rather something temporary that can change on a regular basis.

How to use passwords to encrypt and decrypt?

ChaCha is typically used in a way that generates the key from the password that a user must enter to encrypt/decrypt data. Because the key is of set length (256 bits, or 32 bytes), you cannot use the password as the key directly, as this would result in insecure and impractical limits on the password that the user must choose.

Instead, a key derivation algorithm generates a ChaCha-compatible key from a password. PBKDF2 (Password Based Key Derivation Function 2) is an advanced key derivation function.

Advantages of ChaCha20

Below are some advantages of ChaCha20 which we need to consider while using this technique −

  • ChaCha20 is one of the faster encryption algorithms available, making it ideal for use on a wide range of devices, specially mobile and low-power IoT devices.
  • ChaCha20 is designed to be highly secure and resilient to known attacks. It is built on principles similar to the Salsa20 encryption method, which has been extensively tested and considered as highly secure.
  • ChaCha20 is very parallelizable, which makes it ideal for multi-core processors and high-performance computing systems.
  • Compared to other encryption algorithms, ChaCha20 is very easy to implement, making it a convincing option for developers.

Applications of ChaCha20

ChaCha20 encryption is used in a variety of applications, such as −

  • Secure Communications: ChaCha20 encrypts communications between individuals, as seen in secure messaging apps or VPN.
  • File Encryption: It encrypts files that are saved on a device or transmitted over a network.
  • IoT Security: ChaCha20 protects IoT devices, which typically have limited computing power and require lightweight encryption techniques.
  • Web Security: It can protect web traffic, like HTTPS connections.

What is XChaCha20?

A cryptographic nonce is an arbitrary value that is used once to ensure that an operation (like encryption or hashing) is unique. eXtended-nonce ChaCa20 (XChaCha20) is a variation of ChaCha20 that uses a 192-bit nonce rather than a 96-bit nonce. This makes selecting a random nonce much more secure, as there is virtually no chance that it will be reused.

("Bits" represents the size or length of cryptographic keys. To put it simply, the higher the bit length, it will more secure the key.)

However, there is no formally recognised standard for XChaCha20, and the most recent attempt to establish one failed in 2020. This has resulted in a sluggish acceptance of the somewhat more secure form.

Alternatives of ChaCa20

  • AES (Advanced Encryption Standard)
  • RC4 (Rivest Cipher 4)
  • Salsa20
  • Blowfish
  • Twofish
  • Camellia
Advertisements