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
//! XChaCha20 stream cipher operations
//!
//! XChaCha20 is an extended nonce variant of the ChaCha20 stream cipher. It uses a 192-bit
//! (24-byte) nonce, which significantly reduces the risk of nonce reuse compared to the
//! original ChaCha20.
//!
//! ## Security Considerations
//!
//! - XChaCha20 is recommended over ChaCha20 for most applications due to its larger nonce size.
//! - This is a raw stream cipher without authentication. For authenticated encryption,
//! use `crypto_secretbox` instead.
use Key;
use crate::;
/// Number of bytes in an XChaCha20 key (32 bytes)
pub const KEYBYTES: usize = crypto_stream_xchacha20_KEYBYTES as usize;
/// Number of bytes in an XChaCha20 nonce (24 bytes)
pub const NONCEBYTES: usize = crypto_stream_xchacha20_NONCEBYTES as usize;
/// A nonce (number used once) for XChaCha20 operations
///
/// This struct represents a nonce for use with the XChaCha20 stream cipher.
/// A nonce must be unique for each message encrypted with the same key to maintain security.
/// XChaCha20 uses a 192-bit (24-byte) nonce, which makes it suitable for randomly generated
/// nonces as the probability of collision is extremely low.
;
/// Generate a stream of random bytes using XChaCha20
///
/// This function generates a deterministic stream of pseudo-random bytes using the
/// XChaCha20 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 should be unique for each stream generated with the same key.
/// - XChaCha20's large nonce size (24 bytes) makes it suitable for randomly generated nonces,
/// as the probability of collision is extremely low.
///
/// # 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::xchacha20::Nonce::from_bytes([0u8; crypto_stream::xchacha20::NONCEBYTES]);
///
/// // Generate 32 bytes of pseudo-random data
/// let random_data = crypto_stream::xchacha20::stream(32, &nonce, &key).unwrap();
/// assert_eq!(random_data.len(), 32);
/// ```
/// Encrypt or decrypt a message using XChaCha20
///
/// This function can be used for both encryption and decryption.
/// XChaCha20 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 should be unique for each message encrypted with the same key.
/// - XChaCha20's large nonce size (24 bytes) makes it suitable for randomly generated nonces,
/// as the probability of collision is extremely low.
/// - 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::xchacha20::Nonce::generate();
///
/// // Message to encrypt
/// let message = b"This is a secret message";
///
/// // Encrypt the message
/// let encrypted = crypto_stream::xchacha20::stream_xor(message, &nonce, &key).unwrap();
///
/// // Decrypt the message
/// let decrypted = crypto_stream::xchacha20::stream_xor(&encrypted, &nonce, &key).unwrap();
///
/// assert_eq!(&decrypted, message);
/// ```