1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! ChaCha20 stream cipher operations
//!
//! ChaCha20 is a stream cipher developed by Daniel J. Bernstein. It's designed to be
//! secure and efficient, with better diffusion properties than its predecessor Salsa20.
//!
//! ## Security Considerations
//!
//! - ChaCha20 uses an 8-byte nonce, which is relatively small. This means there's a
//! higher risk of nonce reuse if you're encrypting many messages with the same key.
//! - For most applications, consider using XChaCha20 instead, which has a larger nonce.
//! - This is a raw stream cipher without authentication. For authenticated encryption,
//! use `crypto_secretbox` instead.
use Key;
use crate::;
/// Number of bytes in a ChaCha20 key (32 bytes)
pub const KEYBYTES: usize = crypto_stream_chacha20_KEYBYTES as usize;
/// Number of bytes in a ChaCha20 nonce (8 bytes)
pub const NONCEBYTES: usize = crypto_stream_chacha20_NONCEBYTES as usize;
/// A nonce (number used once) for ChaCha20 operations
///
/// This struct represents a nonce for use with the ChaCha20 stream cipher.
/// A nonce must be unique for each message encrypted with the same key to maintain security.
/// ChaCha20 uses a 64-bit (8-byte) nonce, which is relatively small. For applications
/// that need to encrypt many messages with the same key, consider using XChaCha20 instead,
/// which has a larger nonce size (192 bits).
;
/// Generate a stream of random bytes using ChaCha20
///
/// This function generates a deterministic stream of pseudo-random bytes using the
/// ChaCha20 algorithm. The same (key, nonce) combination will always produce the
/// same stream of bytes.
///
/// # Arguments
/// * `len` - The number of bytes to generate
/// * `nonce` - The nonce to use
/// * `key` - The key to use
///
/// # Returns
/// * `Result<Vec<u8>>` - The generated stream of bytes
///
/// # Errors
/// Returns an error if the nonce is not exactly `NONCEBYTES` bytes
///
/// # Security Considerations
/// - The nonce must be unique for each stream generated with the same key.
/// - If you need to generate multiple streams with the same key, make sure to use
/// different nonces for each one.
///
/// # Example
/// ```
/// use libsodium_rs as sodium;
/// use sodium::crypto_stream;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a random key
/// let key = crypto_stream::Key::generate().unwrap();
///
/// // Create a nonce (in a real application, this should be unique for each stream)
/// let nonce = crypto_stream::chacha20::Nonce::from_bytes([0u8; crypto_stream::chacha20::NONCEBYTES]);
///
/// // Generate 32 bytes of pseudo-random data
/// let random_data = crypto_stream::chacha20::stream(32, &nonce, &key).unwrap();
/// assert_eq!(random_data.len(), 32);
/// ```
/// Encrypt or decrypt a message using ChaCha20
///
/// This function can be used for both encryption and decryption.
/// ChaCha20 is a stream cipher, so encryption and decryption are the same operation.
///
/// # Arguments
/// * `message` - The message to encrypt or decrypt
/// * `nonce` - The nonce to use
/// * `key` - The key to use
///
/// # Returns
/// * `Result<Vec<u8>>` - The encrypted or decrypted message
///
/// # Errors
/// Returns an error if the nonce is not exactly `NONCEBYTES` bytes
///
/// # Security Considerations
/// - The nonce must be unique for each message encrypted with the same key.
/// - If you encrypt multiple messages with the same key, make sure to use
/// different nonces for each one.
/// - This function does not provide authentication. An attacker could modify
/// the ciphertext, and the changes would be reflected in the decrypted message.
/// For authenticated encryption, use `crypto_secretbox` instead.
///
/// # Example
/// ```
/// use libsodium_rs as sodium;
/// use sodium::crypto_stream;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a random key
/// let key = crypto_stream::Key::generate().unwrap();
///
/// // Create a nonce (in a real application, this should be unique for each message)
/// let nonce = crypto_stream::chacha20::Nonce::generate();
///
/// // Message to encrypt
/// let message = b"This is a secret message";
///
/// // Encrypt the message
/// let encrypted = crypto_stream::chacha20::stream_xor(message, &nonce, &key).unwrap();
///
/// // Decrypt the message
/// let decrypted = crypto_stream::chacha20::stream_xor(&encrypted, &nonce, &key).unwrap();
///
/// assert_eq!(&decrypted, message);
/// ```