Skip to content

Commit b3b6d9e

Browse files
hwamzzxyijun-lee
andauthored
Docs/chacha20.md (#89)
* docs/feistelcipher.md * docs/ feistelcipher.md * docs/ feistelcipher.md * ChaCha20.md * docs: add link to ChaCha20.md in Basic Cryptography.md --------- Co-authored-by: yijun-lee <yijunlee.000@gmail.com> Co-authored-by: Yijun Lee <119404328+yijun-lee@users.noreply.github.com>
1 parent 747249c commit b3b6d9e

5 files changed

Lines changed: 262 additions & 0 deletions

File tree

content/Basic Cryptography/Basic Cryptography.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
- [[Merkle Tree]]
2222
- [[Digital Signature]]
2323
- [[Schnorr Signature]]
24+
- [[ChaCha20]]
2425
- [[General LWE]]
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
## Intro
2+
ChaCha20 is a stream cipher developed by Daniel J. Bernstein. It is an improved version of Salsa20, which he previously designed, offering enhanced security and performance. ChaCha20 is widely used as a symmetric key cipher that balances speed and security, making it suitable for various protocols and applications, including TLS, SSH, VPN, and DNS encryption.
3+
4+
Unlike block ciphers such as [[AES]], ChaCha20 is a stream cipher that performs nonlinear transformations at the bit level. This characteristic allows it to operate efficiently in software without requiring hardware acceleration.
5+
6+
> [!Question] What is a Stream Cipher?
7+
> A stream cipher is a symmetric key encryption method that encrypts data in a continuous stream of bits or bytes. It generates a keystream of the same length as the plaintext and encrypts data by performing an XOR operation with the plaintext. This approach enables fast and efficient encryption, making stream ciphers well-suited for real-time communication and wireless security, where low latency and high speed are essential. However, if the keystream is repeated, security vulnerabilities can arise, making the use of a unique nonce critical.
8+
>
9+
> For more details, refer to [[Stream cipher]].
10+
11+
12+
13+
## Characteristics of ChaCha20
14+
ChaCha20 has the following characteristics:
15+
1. **Stream Cipher**
16+
Unlike block ciphers, which encrypt data in fixed-size blocks, ChaCha20 generates a key stream and encrypts plaintext by performing an XOR operation with it.
17+
18+
2. **High Speed and Simple Operations**
19+
ChaCha20 utilizes Add, Rotate, and XOR (ARX) operations, making its computations simple and allowing it to operate with high efficiency.
20+
21+
3. **Enhanced Security with Nonce-Based Encryption**
22+
Even when using the same encryption key, different nonces produce distinct key streams, enhancing security and preventing key stream reuse attacks.
23+
24+
25+
26+
## Design
27+
The basic structure of ChaCha20 is as follows:
28+
29+
- **State size**: 512 bits, consisting of 16 32-bit words
30+
- **Key size**: 256 bits
31+
- **Nonce size**: 96 bits
32+
- **Counter**: 32 bits
33+
34+
### Initial State Setup
35+
ChaCha20 represents its 512-bit state as a **4×4 matrix of 16 32-bit words**. The state consists of:
36+
37+
- Four **32-bit constants**
38+
- A **256-bit key** (eight 32-bit words)
39+
- A **32-bit counter**
40+
- A **96-bit nonce** (three 32-bit words)
41+
42+
During initialization, the state is set up using predefined constants and the given key, counter, and nonce.
43+
44+
### Quarter Round Function
45+
The Quarter Round (QR) function is used to perform ARX (Add-Rotate-XOR) operations. This function is applied repeatedly over 20 rounds—hence the name ChaCha'20', which signifies the 20-round transformation process.
46+
The 20 rounds are divided into Column Rounds and Diagonal Rounds.
47+
1. **Column Rounds**
48+
During Column Rounds, each column in the 4×4 matrix undergoes the Quarter Round transformation.
49+
Given the 4×4 matrix representation of the ChaCha20 state:
50+
```
51+
0 1 2 3
52+
4 5 6 7
53+
8 9 10 11
54+
12 13 14 15
55+
```
56+
The Quarter Round is applied to the following **column-wise** groups:
57+
58+
- **(0, 4, 8, 12)**
59+
- **(1, 5, 9, 13)**
60+
- **(2, 6, 10, 14)**
61+
- **(3, 7, 11, 15)**
62+
2. **Diagonal Rounds**
63+
Diagonal Rounds apply the Quarter Round transformation to elements grouped diagonally in the 4×4 state matrix.
64+
65+
Using the same matrix structure:
66+
```
67+
0 1 2 3
68+
4 5 6 7
69+
8 9 10 11
70+
12 13 14 15
71+
```
72+
The Quarter Round is applied to the following **diagonal-wise** groups:
73+
74+
- **(0, 5, 10, 15)**
75+
- **(1, 6, 11, 12)**
76+
- **(2, 7, 8, 13)**
77+
- **(3, 4, 9, 14)**
78+
79+
In total, 20 rounds of Quarter Round transformations are performed, alternating between Column Rounds and Diagonal Rounds, ten times each, to generate the final key stream.
80+
81+
The ARX operation follows a sequence of modular addition, bitwise XOR, and bitwise rotation:
82+
83+
```
84+
a += b; d ^= a; d <<<= 16;
85+
c += d; b ^= c; b <<<= 12;
86+
a += b; d ^= a; d <<<= 8;
87+
c += d; b ^= c; b <<<= 7;
88+
```
89+
Each Quarter Round operates on four words from the state matrix. For example, in the first column-wise combination (0, 4, 8, 12):
90+
91+
- **0 → `a`**
92+
- **4 → `b`**
93+
- **8 → `c`**
94+
- **12 → `d`**
95+
#### Operation Breakdown
96+
- `+=` represents **32-bit modular addition** (`mod 2³²`).
97+
- `⊕=` represents **bitwise XOR**.
98+
- `<<<=` denotes **left rotation** by the specified number of bits.
99+
Through these transformations, each Quarter Round substantially changes the state, ensuring strong diffusion and security.
100+
101+
102+
103+
## ChaCha20 vs. AES
104+
For more details on AES, refer to [[AES]].
105+
106+
Both AES and ChaCha20 are cryptographic algorithms that offer **high security**, but each has its unique advantages.
107+
### Performance Considerations
108+
AES benefits from hardware acceleration, such as AES-NI (AES New Instructions) in modern processors, making it significantly faster in environments where such optimizations are available. However, in software-only implementations, ChaCha20 generally outperforms AES in terms of speed and efficiency, especially on processors lacking dedicated cryptographic hardware.
109+
110+
### Security and Side-Channel Resistance
111+
AES is known to be secure when properly implemented, and it has been validated for encrypting classified documents by the U.S. government. However, some implementations of AES are vulnerable to side-channel attacks, such as cache-timing attacks and power analysis. While these attacks require sophisticated conditions to be successful, they pose a potential risk if AES is not carefully implemented.
112+
113+
ChaCha20, in contrast, is designed to be naturally resistant to side-channel attacks, making it a preferred choice in environments where side-channel resistance is critical. Like AES, ChaCha20 is considered to be cryptographically secure and widely trusted.
114+
115+
116+
## ChaCha20-Poly1305
117+
ChaCha20-Poly1305 is an AEAD (Authenticated Encryption with Associated Data) scheme that combines the ChaCha20 cipher with the Poly1305 message authentication code (MAC). This encryption method provides confidentiality, integrity, and authentication simultaneously and is widely used across various protocols.
118+
119+
ChaCha20-Poly1305 is known to perform faster than AES-GCM in software-based environments, making it a preferred choice in systems without hardware acceleration.
120+
> [!Question] Why is ChaCha20-Poly1305 needed?
121+
> Encryption alone, as in ChaCha20, does not detect message tampering. To address this, a separate Message Authentication Code (MAC) is required.
122+
> However, if authentication is performed separately after encryption, an attacker may attempt to bypass the MAC verification or manipulate ciphertext integrity.
123+
> AEAD (Authenticated Encryption with Associated Data) prevents such attacks by integrating authentication with encryption. ChaCha20-Poly1305 is one of the most well-known AEAD encryption schemes.
124+
125+
### Poly1305
126+
Poly1305 is an algorithm designed by Daniel J. Bernstein in 2005, the same cryptographer who developed ChaCha20.
127+
128+
It serves as a Message Authentication Code (MAC) that verifies the integrity of a message by generating a cryptographic tag.
129+
Poly1305 uses a 128-bit one-time key to prevent message forgery while providing a fast and efficient authentication mechanism.
130+
131+
132+
133+
## XChaCha20
134+
135+
XChaCha20 is an extended version of ChaCha20, a stream cipher that increases the nonce size from 96 bits to 192 bits. While the fundamental encryption mechanism remains identical to that of ChaCha20, the use of a longer nonce enhances security. This design prevents nonce reuse attacks and provides a safer environment for randomly generated nonces.
136+
137+
In the ChaCha20 encryption algorithm, avoiding nonce reuse is a crucial security requirement. However, in practical applications, a 96-bit nonce carries a risk of collision, leading to the following potential issues:
138+
3. Risk of collisions when using random nonces
139+
Although the probability of a collision when generating a 96-bit nonce randomly is extremely low, it is not entirely negligible. If a nonce is reused in ChaCha20, it can potentially lead to the direct leakage of the encryption key.
140+
4. Difficulties in using long-term keys
141+
When the same encryption key is used for an extended period, the risk of nonce duplication increases. Ensuring security when encrypting large volumes of data requires strict nonce management.
142+
143+
To address these concerns, XChaCha20 employs a 192-bit nonce, effectively eliminating the risk of collisions.
144+
145+
146+
147+
## Use cases
148+
### TLS (Transport Layer Security)
149+
* Google Chrome
150+
151+
### VPN (Virtual Private Network)
152+
* WireGuard VPN
153+
154+
### Disk & Storage Encryption
155+
* Google Adiantum
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
## Intro
2+
The Feistel Cipher is a cryptographic structure that divides the input into left and right blocks, repeatedly applying a round function to one block and combining its output with the other block.
3+
Typically, all elements of an encryption process must be invertible, as decryption requires reversing the encryption steps to recover the plaintext. However, the Feistel Cipher uniquely employs both invertible and non-invertible components.
4+
5+
The primary reason the Feistel Cipher can utilize non-invertible elements lies in its design, which is based on the XOR operation.
6+
> [!Question] The Invertibility of XOR
7+
> Due to the "self-inverse" property of XOR, performing the same XOR operation during the decryption phase restores the original data.
8+
> * Self-inverse property: A⊕B⊕B=A
9+
10+
As a result, the Feistel Cipher allows for more flexible designs compared to Non-Feistel Ciphers. Additionally, the use of non-invertible components simplifies management by making the encryption and decryption processes identical.
11+
12+
A well-known example of a Feistel Cipher is [[DES]] (Data Encryption Standard). In contrast, a prominent example of a Non-Feistel Cipher is [[AES]] (Advanced Encryption Standard).
13+
14+
15+
## Design
16+
### 1-Round Feistel Structure
17+
![[Feistel_cipher(1).png]]
18+
19+
The left side represents the encryption process, while the right side shows the decryption process.
20+
21+
#### Encryption Process
22+
23+
In the encryption process, the plaintext input $p = (L_0 \parallel R_0)$ is divided into $L_0$​ and $R_0$​. A function $F$ takes the key $k$ and $R_0$​ as its input. Then, $L_0$​ is XORed with the output of $F(k, R_0)$, producing the left half of the ciphertext $L_1$. Meanwhile, $R_0$​ remains unchanged and becomes the right half of the ciphertext $R_1$​.
24+
25+
The encryption process can be represented mathematically as follows:
26+
$$
27+
L_1 = L_0 \oplus F(k, R_0)
28+
$$
29+
$$
30+
R_1 = R_0
31+
$$
32+
33+
#### Decryption Process
34+
35+
In the decryption process, the input ciphertext $c = (L'_0|R'_0) = (L_1|R_1)$ is divided into $L'_0$​ and $R'_0$.
36+
$L'_0$ is XORed with $F(k, R'_0)$, resulting in the left half of the plaintext $L'_1$. Similarly, $R'_0$ remains unchanged and becomes the right half of the plaintext $R'_1$.
37+
38+
The decryption process can be represented mathematically as follows:
39+
$$
40+
L'_1 = L'_0 \oplus F(k, R'_0)
41+
$$$$
42+
R'_1 = R'_0
43+
$$
44+
45+
#### Observations
46+
47+
As you may have noticed, the encryption and decryption processes are identical except for their inputs and outputs being reversed.
48+
49+
Encryption:
50+
$$c = p \oplus (F(k, R_0) \parallel 0)$$
51+
Decryption:
52+
$$c \oplus (F(k, R'_0) \parallel 0) \\ = p \oplus (F(k, R_0) \parallel 0) \oplus (F(k, R'_0) \parallel 0) \\ = p \oplus (F(k, R_0) \parallel 0) \oplus (F(k, R'_0) \parallel 0) \\ = p \oplus 0 = p$$
53+
As mentioned in the [[#Intro]], the self-inverse property of the XOR operation ensures that $F(k, R'_0)$ is canceled out during decryption. This means the decryption process works correctly even without requiring the inverse function $F^{-1}$, allowing the use of non-invertible components in the Feistel Cipher.
54+
55+
#### Limitations of a 1-Round Feistel Structure
56+
57+
Analyzing the 1-round Feistel Cipher reveals a critical drawback: the right half of the plaintext, $R_0$, directly becomes the right half of the ciphertext, $R_1$, without undergoing any transformation. This exposes half of the plaintext in the ciphertext, significantly compromising security.
58+
59+
To address this issue, Feistel Cipher structures are designed with multiple rounds. In a multi-round Feistel Cipher, the left and right halves are swapped at the end of each round, effectively ensuring that all parts of the plaintext are processed. However, this design requires more rounds compared to Non-Feistel Ciphers to achieve the same level of security.
60+
61+
### Multi-Round Feistel Structure
62+
![[Feistel_cipher(2).png]]
63+
The diagram above illustrates a Feistel structure with multiple rounds, as opposed to a single round.
64+
65+
#### Encryption Process
66+
67+
Unlike the single-round Feistel structure, in a multi-round Feistel Cipher, the right half of the plaintext ($R_0$) does not directly become the right half of the ciphertext ($R_1$). Instead, it becomes the left half of the output ($L_1$). Additionally, the XOR computation that previously resulted in $L_1$ in the single-round structure now determines $R_1$. In other words, the outputs of the left and right halves are swapped after each round.
68+
In the second round, $L_1$ undergoes further encryption, ensuring that both $L_0$ and $R_0$ are processed through the encryption rounds.
69+
70+
The encryption process can be expressed as follows:
71+
$$L_1 = R_0$$
72+
$$
73+
R_1 = L_0 \oplus F(k_1, R_0)
74+
$$
75+
$$
76+
L_2 = L_1 \oplus F(k_2, R_1)
77+
$$
78+
$$
79+
R_2 = R_1
80+
$$
81+
82+
#### Decryption Process
83+
84+
The decryption process mirrors the encryption process, with the only difference being the reverse order of the keys used during the computation.
85+
$$
86+
L'_1 = R'_0 = R_2 = R_1
87+
$$
88+
$$
89+
R'_1 = L'_0 \oplus F(k_2, R'_0) = L_2 \oplus F(k_2, R_1)
90+
$$
91+
$$
92+
= L_1 \oplus F(k_2, R_1) \oplus F(k_2, R_1) = L_1 = R_0
93+
$$
94+
$$
95+
L'_2 = L'_1 \oplus F(k_1, R'_1) = R_1 \oplus F(k_1, R_0)
96+
$$
97+
$$
98+
= L_0 \oplus F(k_1, R_0) \oplus F(k_1, R_0) = L_0
99+
$$
100+
$$
101+
R'_2 = R'_1 = R_0
102+
$$
103+
104+
> [!Note]
105+
> A Feistel Cipher with two rounds provides the same level of security as a single round of a Non-Feistel Cipher. Therefore, Feistel Ciphers generally require a greater number of rounds to achieve comparable security.
106+
49.1 KB
Loading
133 KB
Loading

0 commit comments

Comments
 (0)