crabgraph 0.3.3

A safe, ergonomic, high-performance cryptographic library for Rust built on audited primitives
Documentation
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Streaming AEAD encryption for large files.
//!
//! This module provides streaming encryption and decryption capabilities for processing
//! large files that don't fit in memory. It uses the STREAM construction from
//! [Online Authenticated-Encryption and its Nonce-Reuse Misuse-Resistance](https://eprint.iacr.org/2015/189.pdf)
//! which provides nonce-reuse resistance.
//!
//! # Security Notes
//!
//! - Each chunk is independently authenticated
//! - The stream uses a nonce derivation function to ensure unique nonces per chunk
//! - The final chunk includes authentication of the entire stream
//! - Maximum file size depends on chunk counter size (default: 2^32 chunks)
//!
//! # Example
//!
//! ```
//! use crabgraph::aead::stream::{Aes256GcmStreamEncryptor, Aes256GcmStreamDecryptor};
//! use crabgraph::CrabResult;
//!
//! # fn main() -> CrabResult<()> {
//! // Generate a 32-byte key
//! let key = crabgraph::rand::secure_bytes(32)?;
//!
//! // Encrypt data in chunks
//! let plaintext = vec![b"Hello, world! ".repeat(1000)];
//! let mut encryptor = Aes256GcmStreamEncryptor::new(&key)?;
//! let nonce = encryptor.nonce();
//! let mut ciphertext_chunks = Vec::new();
//!
//! // Encrypt all but the last chunk
//! for chunk in &plaintext[..plaintext.len() - 1] {
//!     ciphertext_chunks.push(encryptor.encrypt_next(chunk)?);
//! }
//! // Encrypt the last chunk (this consumes the encryptor)
//! let last_chunk = &plaintext[plaintext.len() - 1];
//! ciphertext_chunks.push(encryptor.encrypt_last(last_chunk)?);
//!
//! // Decrypt data in chunks
//! let mut decryptor = Aes256GcmStreamDecryptor::from_nonce(&key, &nonce)?;
//! let mut decrypted = Vec::new();
//!
//! // Decrypt all but the last chunk
//! for chunk in &ciphertext_chunks[..ciphertext_chunks.len() - 1] {
//!     decrypted.extend_from_slice(&decryptor.decrypt_next(chunk)?);
//! }
//! // Decrypt the last chunk (this consumes the decryptor)
//! let last_encrypted = &ciphertext_chunks[ciphertext_chunks.len() - 1];
//! decrypted.extend_from_slice(&decryptor.decrypt_last(last_encrypted)?);
//!
//! // Verify the decrypted data matches the first (and only) chunk in plaintext
//! assert_eq!(&decrypted, plaintext[0].as_slice());
//! # Ok(())
//! # }
//! ```

use crate::errors::{CrabError, CrabResult};
use aes_gcm::{
    aead::{
        stream::{DecryptorBE32, EncryptorBE32, Nonce as StreamNonce, StreamBE32},
        Key, KeyInit,
    },
    Aes256Gcm,
};
use chacha20poly1305::ChaCha20Poly1305 as ChaCha20Poly1305Cipher;

type AesStreamNonce = StreamNonce<Aes256Gcm, StreamBE32<Aes256Gcm>>;
type ChaChaStreamNonce = StreamNonce<ChaCha20Poly1305Cipher, StreamBE32<ChaCha20Poly1305Cipher>>;

/// Default chunk size for streaming operations (64 KB)
pub const DEFAULT_CHUNK_SIZE: usize = 64 * 1024;

/// Maximum chunk size for streaming operations (1 MB)
pub const MAX_CHUNK_SIZE: usize = 1024 * 1024;

/// Streaming encryptor for AES-256-GCM.
///
/// Accepts raw 32-byte keys and produces a stream of encrypted chunks.
pub struct Aes256GcmStreamEncryptor {
    encryptor: EncryptorBE32<Aes256Gcm>,
    nonce: Vec<u8>,
}

impl Aes256GcmStreamEncryptor {
    /// Creates a new streaming encryptor from a 32-byte key.
    ///
    /// # Arguments
    ///
    /// * `key` - A 32-byte AES-256 key
    ///
    /// # Errors
    ///
    /// Returns an error if the key is not exactly 32 bytes.
    pub fn new(key: &[u8]) -> CrabResult<Self> {
        if key.len() != 32 {
            return Err(CrabError::invalid_input(format!(
                "AES-256 requires 32-byte key, got {}",
                key.len()
            )));
        }

        // Generate a 7-byte nonce for STREAM (AES-GCM needs 12 bytes, STREAM uses 5 for counter+flag)
        let nonce_bytes = crate::rand::secure_bytes(7)?;
        let nonce_array: &AesStreamNonce = nonce_bytes.as_slice().into();

        let key_array: &Key<Aes256Gcm> = key.into();
        let aead = Aes256Gcm::new(key_array);
        let encryptor = EncryptorBE32::from_aead(aead, nonce_array);

        Ok(Self {
            encryptor,
            nonce: nonce_bytes,
        })
    }

    /// Returns the nonce used for this stream.
    ///
    /// This nonce must be stored with the encrypted data to enable decryption.
    pub fn nonce(&self) -> Vec<u8> {
        self.nonce.clone()
    }

    /// Encrypts the next chunk of data.
    ///
    /// # Arguments
    ///
    /// * `plaintext` - The chunk of data to encrypt
    pub fn encrypt_next(&mut self, plaintext: &[u8]) -> CrabResult<Vec<u8>> {
        self.encryptor
            .encrypt_next(plaintext)
            .map_err(|e| CrabError::crypto_error(format!("Stream encryption failed: {}", e)))
    }

    /// Encrypts the last chunk and finalizes the stream.
    ///
    /// After calling this method, the encryptor cannot be used anymore.
    ///
    /// # Arguments
    ///
    /// * `plaintext` - The final chunk of data to encrypt (can be empty)
    pub fn encrypt_last(self, plaintext: &[u8]) -> CrabResult<Vec<u8>> {
        self.encryptor
            .encrypt_last(plaintext)
            .map_err(|e| CrabError::crypto_error(format!("Stream encryption failed: {}", e)))
    }
}

/// Streaming decryptor for AES-256-GCM.
pub struct Aes256GcmStreamDecryptor {
    decryptor: DecryptorBE32<Aes256Gcm>,
}

impl Aes256GcmStreamDecryptor {
    /// Creates a new streaming decryptor from a key and nonce.
    ///
    /// # Arguments
    ///
    /// * `key` - A 32-byte AES-256 key (must match the encryption key)
    /// * `nonce` - The 7-byte nonce from the encryption process
    ///
    /// # Errors
    ///
    /// Returns an error if the key or nonce have invalid lengths.
    pub fn from_nonce(key: &[u8], nonce: &[u8]) -> CrabResult<Self> {
        if key.len() != 32 {
            return Err(CrabError::invalid_input(format!(
                "AES-256 requires 32-byte key, got {}",
                key.len()
            )));
        }

        if nonce.len() != 7 {
            return Err(CrabError::invalid_input(format!(
                "Invalid nonce size for STREAM: expected 7, got {}",
                nonce.len()
            )));
        }

        let nonce_array: &AesStreamNonce = nonce.into();
        let key_array: &Key<Aes256Gcm> = key.into();
        let aead = Aes256Gcm::new(key_array);
        let decryptor = DecryptorBE32::from_aead(aead, nonce_array);

        Ok(Self { decryptor })
    }

    /// Decrypts the next chunk of data.
    ///
    /// # Arguments
    ///
    /// * `ciphertext` - The encrypted chunk (includes authentication tag)
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or if the chunk is corrupted.
    pub fn decrypt_next(&mut self, ciphertext: &[u8]) -> CrabResult<Vec<u8>> {
        self.decryptor
            .decrypt_next(ciphertext)
            .map_err(|e| CrabError::crypto_error(format!("Stream decryption failed: {}", e)))
    }

    /// Decrypts the last chunk and finalizes the stream.
    ///
    /// After calling this method, the decryptor cannot be used anymore.
    ///
    /// # Arguments
    ///
    /// * `ciphertext` - The final encrypted chunk (includes authentication tag)
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or if the chunk is corrupted.
    pub fn decrypt_last(self, ciphertext: &[u8]) -> CrabResult<Vec<u8>> {
        self.decryptor
            .decrypt_last(ciphertext)
            .map_err(|e| CrabError::crypto_error(format!("Stream decryption failed: {}", e)))
    }
}

/// Streaming encryptor for ChaCha20-Poly1305.
///
/// Accepts raw 32-byte keys and produces a stream of encrypted chunks.
pub struct ChaCha20Poly1305StreamEncryptor {
    encryptor: EncryptorBE32<ChaCha20Poly1305Cipher>,
    nonce: Vec<u8>,
}

impl ChaCha20Poly1305StreamEncryptor {
    /// Creates a new streaming encryptor from a 32-byte key.
    ///
    /// # Arguments
    ///
    /// * `key` - A 32-byte ChaCha20-Poly1305 key
    ///
    /// # Errors
    ///
    /// Returns an error if the key is not exactly 32 bytes.
    pub fn new(key: &[u8]) -> CrabResult<Self> {
        if key.len() != 32 {
            return Err(CrabError::invalid_input(format!(
                "ChaCha20-Poly1305 requires 32-byte key, got {}",
                key.len()
            )));
        }

        // Generate a 7-byte nonce for STREAM (ChaCha20Poly1305 needs 12 bytes, STREAM uses 5 for counter+flag)
        let nonce_bytes = crate::rand::secure_bytes(7)?;
        let nonce_array: &ChaChaStreamNonce = nonce_bytes.as_slice().into();

        let key_array: &Key<ChaCha20Poly1305Cipher> = key.into();
        let aead = ChaCha20Poly1305Cipher::new(key_array);
        let encryptor = EncryptorBE32::from_aead(aead, nonce_array);

        Ok(Self {
            encryptor,
            nonce: nonce_bytes,
        })
    }

    /// Returns the nonce used for this stream.
    ///
    /// This nonce must be stored with the encrypted data to enable decryption.
    pub fn nonce(&self) -> Vec<u8> {
        self.nonce.clone()
    }

    /// Encrypts the next chunk of data.
    ///
    /// # Arguments
    ///
    /// * `plaintext` - The chunk of data to encrypt
    pub fn encrypt_next(&mut self, plaintext: &[u8]) -> CrabResult<Vec<u8>> {
        self.encryptor
            .encrypt_next(plaintext)
            .map_err(|e| CrabError::crypto_error(format!("Stream encryption failed: {}", e)))
    }

    /// Encrypts the last chunk and finalizes the stream.
    ///
    /// After calling this method, the encryptor cannot be used anymore.
    ///
    /// # Arguments
    ///
    /// * `plaintext` - The final chunk of data to encrypt (can be empty)
    pub fn encrypt_last(self, plaintext: &[u8]) -> CrabResult<Vec<u8>> {
        self.encryptor
            .encrypt_last(plaintext)
            .map_err(|e| CrabError::crypto_error(format!("Stream encryption failed: {}", e)))
    }
}

/// Streaming decryptor for ChaCha20-Poly1305.
pub struct ChaCha20Poly1305StreamDecryptor {
    decryptor: DecryptorBE32<ChaCha20Poly1305Cipher>,
}

impl ChaCha20Poly1305StreamDecryptor {
    /// Creates a new streaming decryptor from a key and nonce.
    ///
    /// # Arguments
    ///
    /// * `key` - A 32-byte ChaCha20-Poly1305 key (must match the encryption key)
    /// * `nonce` - The 7-byte nonce from the encryption process
    ///
    /// # Errors
    ///
    /// Returns an error if the key or nonce have invalid lengths.
    pub fn from_nonce(key: &[u8], nonce: &[u8]) -> CrabResult<Self> {
        if key.len() != 32 {
            return Err(CrabError::invalid_input(format!(
                "ChaCha20-Poly1305 requires 32-byte key, got {}",
                key.len()
            )));
        }

        if nonce.len() != 7 {
            return Err(CrabError::invalid_input(format!(
                "Invalid nonce size for STREAM: expected 7, got {}",
                nonce.len()
            )));
        }

        let nonce_array: &ChaChaStreamNonce = nonce.into();
        let key_array: &Key<ChaCha20Poly1305Cipher> = key.into();
        let aead = ChaCha20Poly1305Cipher::new(key_array);
        let decryptor = DecryptorBE32::from_aead(aead, nonce_array);

        Ok(Self { decryptor })
    }

    /// Decrypts the next chunk of data.
    ///
    /// # Arguments
    ///
    /// * `ciphertext` - The encrypted chunk (includes authentication tag)
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or if the chunk is corrupted.
    pub fn decrypt_next(&mut self, ciphertext: &[u8]) -> CrabResult<Vec<u8>> {
        self.decryptor
            .decrypt_next(ciphertext)
            .map_err(|e| CrabError::crypto_error(format!("Stream decryption failed: {}", e)))
    }

    /// Decrypts the last chunk and finalizes the stream.
    ///
    /// After calling this method, the decryptor cannot be used anymore.
    ///
    /// # Arguments
    ///
    /// * `ciphertext` - The final encrypted chunk (includes authentication tag)
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or if the chunk is corrupted.
    pub fn decrypt_last(self, ciphertext: &[u8]) -> CrabResult<Vec<u8>> {
        self.decryptor
            .decrypt_last(ciphertext)
            .map_err(|e| CrabError::crypto_error(format!("Stream decryption failed: {}", e)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_aes256gcm_stream_encrypt_decrypt() -> CrabResult<()> {
        let key = crate::rand::secure_bytes(32)?;
        let plaintext_chunks = vec![b"Hello, ".as_slice(), b"world!".as_slice()];

        // Encrypt
        let mut encryptor = Aes256GcmStreamEncryptor::new(&key)?;
        let nonce = encryptor.nonce();

        let mut ciphertext_chunks = Vec::new();
        for chunk in &plaintext_chunks {
            ciphertext_chunks.push(encryptor.encrypt_next(chunk)?);
        }
        ciphertext_chunks.push(encryptor.encrypt_last(b"")?);

        // Decrypt
        let mut decryptor = Aes256GcmStreamDecryptor::from_nonce(&key, &nonce)?;
        let mut decrypted = Vec::new();

        let last_idx = ciphertext_chunks.len() - 1;
        for (i, chunk) in ciphertext_chunks.iter().enumerate() {
            if i == last_idx {
                decrypted.extend_from_slice(&decryptor.decrypt_last(chunk)?);
                break; // Decryptor is consumed, no more iterations
            } else {
                decrypted.extend_from_slice(&decryptor.decrypt_next(chunk)?);
            }
        }

        let expected: Vec<u8> = plaintext_chunks.concat();
        assert_eq!(decrypted, expected);

        Ok(())
    }

    #[test]
    fn test_chacha20poly1305_stream_encrypt_decrypt() -> CrabResult<()> {
        let key = crate::rand::secure_bytes(32)?;
        let plaintext_chunks = vec![b"First chunk".as_slice(), b"Second chunk".as_slice()];

        // Encrypt
        let mut encryptor = ChaCha20Poly1305StreamEncryptor::new(&key)?;
        let nonce = encryptor.nonce();

        let mut ciphertext_chunks = Vec::new();
        for chunk in &plaintext_chunks {
            ciphertext_chunks.push(encryptor.encrypt_next(chunk)?);
        }
        ciphertext_chunks.push(encryptor.encrypt_last(b"Final chunk")?);

        // Decrypt
        let mut decryptor = ChaCha20Poly1305StreamDecryptor::from_nonce(&key, &nonce)?;
        let mut decrypted = Vec::new();

        let last_idx = ciphertext_chunks.len() - 1;
        for (i, chunk) in ciphertext_chunks.iter().enumerate() {
            if i == last_idx {
                decrypted.extend_from_slice(&decryptor.decrypt_last(chunk)?);
                break; // Decryptor is consumed, no more iterations
            } else {
                decrypted.extend_from_slice(&decryptor.decrypt_next(chunk)?);
            }
        }

        let mut expected: Vec<u8> = plaintext_chunks.concat();
        expected.extend_from_slice(b"Final chunk");
        assert_eq!(decrypted, expected);

        Ok(())
    }

    #[test]
    fn test_stream_large_data() -> CrabResult<()> {
        let key = crate::rand::secure_bytes(32)?;
        let chunk_size = 1024;
        let num_chunks = 100;

        // Generate large plaintext
        let mut plaintext = Vec::new();
        for i in 0..num_chunks {
            plaintext.extend_from_slice(&vec![i as u8; chunk_size]);
        }

        // Encrypt in chunks
        let mut encryptor = Aes256GcmStreamEncryptor::new(&key)?;
        let nonce = encryptor.nonce();

        let mut ciphertext_chunks = Vec::new();
        for chunk in plaintext.chunks(chunk_size) {
            ciphertext_chunks.push(encryptor.encrypt_next(chunk)?);
        }
        ciphertext_chunks.push(encryptor.encrypt_last(&[])?);

        // Decrypt in chunks
        let mut decryptor = Aes256GcmStreamDecryptor::from_nonce(&key, &nonce)?;
        let mut decrypted = Vec::new();

        let last_idx = ciphertext_chunks.len() - 1;
        for (i, chunk) in ciphertext_chunks.iter().enumerate() {
            if i == last_idx {
                decrypted.extend_from_slice(&decryptor.decrypt_last(chunk)?);
                break; // Decryptor is consumed, no more iterations
            } else {
                decrypted.extend_from_slice(&decryptor.decrypt_next(chunk)?);
            }
        }

        assert_eq!(decrypted, plaintext);

        Ok(())
    }

    #[test]
    fn test_stream_authentication_failure() -> CrabResult<()> {
        let key = crate::rand::secure_bytes(32)?;

        // Encrypt
        let mut encryptor = Aes256GcmStreamEncryptor::new(&key)?;
        let nonce = encryptor.nonce();

        let chunk1 = encryptor.encrypt_next(b"Hello")?;
        let mut chunk2 = encryptor.encrypt_last(b"World")?;

        // Corrupt the second chunk
        chunk2[0] ^= 1;

        // Decrypt
        let mut decryptor = Aes256GcmStreamDecryptor::from_nonce(&key, &nonce)?;
        decryptor.decrypt_next(&chunk1)?;

        // Should fail authentication
        let result = decryptor.decrypt_last(&chunk2);
        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn test_stream_empty_chunks() -> CrabResult<()> {
        let key = crate::rand::secure_bytes(32)?;

        // Encrypt empty data
        let encryptor = Aes256GcmStreamEncryptor::new(&key)?;
        let nonce = encryptor.nonce();

        let chunk = encryptor.encrypt_last(b"")?;

        // Decrypt
        let decryptor = Aes256GcmStreamDecryptor::from_nonce(&key, &nonce)?;
        let decrypted = decryptor.decrypt_last(&chunk)?;

        assert_eq!(decrypted, b"");

        Ok(())
    }
}