ciphern 0.2.1

Enterprise-grade cryptographic library
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Copyright (c) 2025 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

use crate::cipher::base_provider::BaseCipherProvider;
use crate::cipher::provider::SymmetricCipher;
use crate::error::{CryptoError, Result};
use crate::i18n::translate_with_args;
use crate::key::Key;
use crate::random::SecureRandom;
use crate::side_channel::SideChannelConfig;
use crate::types::Algorithm;
use aes_gcm::aead::consts::U12;
use aes_gcm::aead::{Aead, AeadCore, KeyInit, Payload};
use aes_gcm::aes::Aes192;
use aes_gcm::{Aes128Gcm, AesGcm};
use ring::aead::{Aad, LessSafeKey, Nonce, UnboundKey, AES_256_GCM};

/// 统一的 AES-GCM 提供者,支持 AES-128、AES-192 和 AES-256
pub struct AesGcmProvider {
    base: BaseCipherProvider,
    algorithm: Algorithm,
}

/// 类型别名,用于向后兼容
pub type Aes256GcmProvider = AesGcmProvider;

impl SymmetricCipher for AesGcmProvider {
    fn encrypt(&self, key: &Key, plaintext: &[u8], aad: Option<&[u8]>) -> Result<Vec<u8>> {
        if key.algorithm() != self.algorithm {
            return Err(CryptoError::UnsupportedAlgorithm(
                "Key algorithm mismatch".into(),
            ));
        }
        self.base
            .protect_operation(|| self.encrypt_internal(key, plaintext, aad))
    }

    fn decrypt(&self, key: &Key, ciphertext: &[u8], aad: Option<&[u8]>) -> Result<Vec<u8>> {
        if key.algorithm() != self.algorithm {
            return Err(CryptoError::UnsupportedAlgorithm(
                "Key algorithm mismatch".into(),
            ));
        }
        self.base
            .protect_operation(|| self.decrypt_internal(key, ciphertext, aad))
    }

    fn algorithm(&self) -> Algorithm {
        self.algorithm
    }

    fn encrypt_with_nonce(
        &self,
        key: &Key,
        plaintext: &[u8],
        nonce: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        if key.algorithm() != self.algorithm {
            return Err(CryptoError::UnsupportedAlgorithm(
                "Key algorithm mismatch".into(),
            ));
        }
        if nonce.len() != 12 {
            return Err(CryptoError::EncryptionFailed("Invalid nonce length".into()));
        }

        let operation = || {
            let secret = key.secret_bytes()?;

            match self.algorithm {
                Algorithm::AES128GCM => {
                    let cipher =
                        AesGcm::<aes_gcm::aes::Aes128, U12>::new_from_slice(secret.as_bytes())
                            .map_err(|_| CryptoError::EncryptionFailed("Invalid Key".into()))?;
                    let nonce_val = aes_gcm::Nonce::from_slice(nonce);

                    cipher
                        .encrypt(
                            nonce_val,
                            Payload {
                                msg: plaintext,
                                aad: aad.unwrap_or(&[]),
                            },
                        )
                        .map_err(|_| CryptoError::EncryptionFailed("Encryption failed".into()))
                }
                Algorithm::AES192GCM => {
                    let cipher = AesGcm::<Aes192, U12>::new_from_slice(secret.as_bytes())
                        .map_err(|_| CryptoError::EncryptionFailed("Invalid Key".into()))?;
                    let nonce_val = aes_gcm::Nonce::from_slice(nonce);

                    cipher
                        .encrypt(
                            nonce_val,
                            Payload {
                                msg: plaintext,
                                aad: aad.unwrap_or(&[]),
                            },
                        )
                        .map_err(|_| CryptoError::EncryptionFailed("Encryption failed".into()))
                }
                Algorithm::AES256GCM => {
                    let unbound_key = UnboundKey::new(&AES_256_GCM, secret.as_bytes())
                        .map_err(|_| CryptoError::EncryptionFailed("Invalid Key".into()))?;
                    let less_safe_key = LessSafeKey::new(unbound_key);
                    let nonce_val = Nonce::assume_unique_for_key(nonce.try_into().unwrap());

                    let mut in_out = plaintext.to_vec();
                    less_safe_key
                        .seal_in_place_append_tag(
                            nonce_val,
                            Aad::from(aad.unwrap_or(&[])),
                            &mut in_out,
                        )
                        .map_err(|_| CryptoError::EncryptionFailed("Seal failed".into()))?;

                    Ok(in_out)
                }
                _ => Err(CryptoError::UnsupportedAlgorithm(
                    "Unsupported AES algorithm".into(),
                )),
            }
        };

        self.base.protect_operation(operation)
    }
}

impl AesGcmProvider {
    /// 创建一个新的 AES-256 GCM 提供者,使用默认配置(向后兼容)
    #[inline]
    pub fn new() -> Result<Self> {
        Ok(Self {
            base: BaseCipherProvider::new()?,
            algorithm: Algorithm::AES256GCM,
        })
    }

    /// 创建一个新的 AES-GCM 提供者,使用指定算法
    #[inline]
    pub fn with_algorithm(algorithm: Algorithm) -> Result<Self> {
        match algorithm {
            Algorithm::AES128GCM | Algorithm::AES192GCM | Algorithm::AES256GCM => Ok(Self {
                base: BaseCipherProvider::new()?,
                algorithm,
            }),
            _ => Err(CryptoError::UnsupportedAlgorithm(format!(
                "AesGcmProvider 不支持的算法: {:?}",
                algorithm
            ))),
        }
    }

    /// 创建一个新的 AES-GCM 提供者,使用自定义侧信道配置
    #[inline]
    #[allow(dead_code)]
    pub fn with_side_channel_config(config: SideChannelConfig) -> Result<Self> {
        Ok(Self {
            base: BaseCipherProvider::with_side_channel_config(config)?,
            algorithm: Algorithm::AES256GCM,
        })
    }

    /// 创建一个新的 AES-GCM 提供者,使用指定算法和侧信道配置
    #[inline]
    pub fn with_algorithm_and_config(
        algorithm: Algorithm,
        config: SideChannelConfig,
    ) -> Result<Self> {
        match algorithm {
            Algorithm::AES128GCM | Algorithm::AES192GCM | Algorithm::AES256GCM => Ok(Self {
                base: BaseCipherProvider::with_side_channel_config(config)?,
                algorithm,
            }),
            _ => Err(CryptoError::UnsupportedAlgorithm(format!(
                "AesGcmProvider 不支持的算法: {:?}",
                algorithm
            ))),
        }
    }

    fn encrypt_internal(&self, key: &Key, plaintext: &[u8], aad: Option<&[u8]>) -> Result<Vec<u8>> {
        let secret_bytes = key.secret_bytes()?;
        self.encrypt_core(secret_bytes.as_bytes(), plaintext, aad)
    }

    #[inline]
    fn encrypt_core(
        &self,
        key_bytes: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        match self.algorithm {
            Algorithm::AES128GCM => self.encrypt_aes128(key_bytes, plaintext, aad),
            Algorithm::AES192GCM => self.encrypt_aes192(key_bytes, plaintext, aad),
            Algorithm::AES256GCM => self.encrypt_with_aes256(key_bytes, plaintext, aad),
            _ => Err(CryptoError::UnsupportedAlgorithm(
                "Unsupported AES algorithm".into(),
            )),
        }
    }

    #[inline]
    fn encrypt_aes128(
        &self,
        key_bytes: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let cipher = Aes128Gcm::new_from_slice(key_bytes)
            .map_err(|_| CryptoError::EncryptionFailed("Invalid Key".into()))?;

        let nonce = Aes128Gcm::generate_nonce(&mut SecureRandom::new()?);
        let aad_ref = aad.unwrap_or(&[]);

        let ciphertext = cipher
            .encrypt(
                &nonce,
                Payload {
                    msg: plaintext,
                    aad: aad_ref,
                },
            )
            .map_err(|_| CryptoError::EncryptionFailed("Encryption failed".into()))?;

        let mut result = Vec::with_capacity(12 + ciphertext.len());
        result.extend_from_slice(nonce.as_slice());
        result.extend_from_slice(&ciphertext);
        Ok(result)
    }

    #[inline]
    fn encrypt_aes192(
        &self,
        key_bytes: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let cipher = AesGcm::<Aes192, U12>::new_from_slice(key_bytes)
            .map_err(|_| CryptoError::EncryptionFailed("Invalid Key".into()))?;

        let nonce = AesGcm::<Aes192, U12>::generate_nonce(&mut SecureRandom::new()?);
        let aad_ref = aad.unwrap_or(&[]);

        let ciphertext = cipher
            .encrypt(
                &nonce,
                Payload {
                    msg: plaintext,
                    aad: aad_ref,
                },
            )
            .map_err(|_| CryptoError::EncryptionFailed("Encryption failed".into()))?;

        let mut result = Vec::with_capacity(12 + ciphertext.len());
        result.extend_from_slice(nonce.as_slice());
        result.extend_from_slice(&ciphertext);
        Ok(result)
    }

    #[inline]
    fn encrypt_with_aes256(
        &self,
        key_bytes: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let unbound_key = UnboundKey::new(&AES_256_GCM, key_bytes)
            .map_err(|_| CryptoError::EncryptionFailed("Invalid Key".into()))?;
        let less_safe_key = LessSafeKey::new(unbound_key);

        let mut nonce_bytes = [0u8; 12];
        SecureRandom::new()?.fill(&mut nonce_bytes)?;
        let nonce = Nonce::assume_unique_for_key(nonce_bytes);

        let capacity = 12 + plaintext.len() + 16;
        let mut in_out = Vec::with_capacity(capacity);
        in_out.extend_from_slice(plaintext);
        less_safe_key
            .seal_in_place_append_tag(nonce, Aad::from(aad.unwrap_or(&[])), &mut in_out)
            .map_err(|_| CryptoError::EncryptionFailed("Seal failed".into()))?;

        let mut result = Vec::with_capacity(12 + in_out.len());
        result.extend_from_slice(&nonce_bytes);
        result.extend_from_slice(&in_out);

        Ok(result)
    }

    fn decrypt_internal(
        &self,
        key: &Key,
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let secret = key.secret_bytes()?;
        self.decrypt_core(secret.as_bytes(), ciphertext, aad)
    }

    #[inline]
    fn decrypt_core(
        &self,
        key_bytes: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        match self.algorithm {
            Algorithm::AES128GCM => self.decrypt_aes128(key_bytes, ciphertext, aad),
            Algorithm::AES192GCM => self.decrypt_aes192(key_bytes, ciphertext, aad),
            Algorithm::AES256GCM => self.decrypt_with_aes256(key_bytes, ciphertext, aad),
            _ => Err(CryptoError::UnsupportedAlgorithm(
                "Unsupported AES algorithm".into(),
            )),
        }
    }

    #[inline]
    fn decrypt_aes128(
        &self,
        key_bytes: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let cipher = Aes128Gcm::new_from_slice(key_bytes)
            .map_err(|_| CryptoError::DecryptionFailed("Invalid Key".into()))?;

        if ciphertext.len() < 12 {
            return Err(CryptoError::DecryptionFailed("Ciphertext too short".into()));
        }

        let (nonce_bytes, encrypted_data) = ciphertext.split_at(12);
        let nonce = nonce_bytes.into();

        cipher
            .decrypt(
                nonce,
                Payload {
                    msg: encrypted_data,
                    aad: aad.unwrap_or(&[]),
                },
            )
            .map_err(|_| CryptoError::DecryptionFailed("Decryption failed".into()))
    }

    #[inline]
    fn decrypt_aes192(
        &self,
        key_bytes: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let cipher = AesGcm::<Aes192, U12>::new_from_slice(key_bytes)
            .map_err(|_| CryptoError::DecryptionFailed("Invalid Key".into()))?;

        if ciphertext.len() < 12 {
            return Err(CryptoError::DecryptionFailed("Ciphertext too short".into()));
        }

        let (nonce_bytes, encrypted_data) = ciphertext.split_at(12);
        let nonce = nonce_bytes.into();

        cipher
            .decrypt(
                nonce,
                Payload {
                    msg: encrypted_data,
                    aad: aad.unwrap_or(&[]),
                },
            )
            .map_err(|_| CryptoError::DecryptionFailed("Decryption failed".into()))
    }

    #[inline]
    fn decrypt_with_aes256(
        &self,
        key_bytes: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        let unbound_key = UnboundKey::new(&AES_256_GCM, key_bytes)
            .map_err(|_| CryptoError::DecryptionFailed("Invalid Key".into()))?;
        let less_safe_key = LessSafeKey::new(unbound_key);

        if ciphertext.len() < 12 {
            return Err(CryptoError::DecryptionFailed("Ciphertext too short".into()));
        }

        let (nonce_bytes, encrypted_data) = ciphertext.split_at(12);
        let nonce = Nonce::try_assume_unique_for_key(nonce_bytes)
            .map_err(|_| CryptoError::DecryptionFailed("Invalid nonce".into()))?;

        let plaintext_len = ciphertext.len().saturating_sub(28);
        let mut in_out = Vec::with_capacity(plaintext_len);
        in_out.extend_from_slice(encrypted_data);
        less_safe_key
            .open_in_place(nonce, Aad::from(aad.unwrap_or(&[])), &mut in_out)
            .map_err(|_| CryptoError::DecryptionFailed("Open failed".into()))?;

        in_out.truncate(plaintext_len);
        Ok(in_out)
    }

    #[inline]
    #[allow(dead_code)]
    fn expand_key_protected(&self, key_bytes: &[u8]) -> Result<Vec<u8>> {
        self.base.expand_key_protected(key_bytes)
    }
}

impl Default for AesGcmProvider {
    fn default() -> Self {
        Self::new().unwrap_or_else(|e| {
            log::error!(
                "{}",
                translate_with_args(
                    "metrics.create_provider_failed",
                    &[("provider", "AesGcmProvider"), ("error", &e.to_string())]
                )
            );
            panic!(
                "{}",
                translate_with_args(
                    "metrics.init_security_component_failed",
                    &[("error", &e.to_string())]
                )
            )
        })
    }
}

impl Aes256GcmProvider {
    /// 获取侧信道防护统计信息
    #[allow(dead_code)]
    pub fn get_side_channel_stats(&self) -> Option<crate::side_channel::SideChannelStats> {
        self.base.get_side_channel_stats()
    }

    /// 检查是否启用了侧信道防护
    #[allow(dead_code)]
    pub fn is_side_channel_protected(&self) -> bool {
        self.base.is_side_channel_protected()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::key::Key;
    use crate::types::Algorithm;

    #[test]
    fn test_aes128_encryption_decryption() {
        let provider = AesGcmProvider::aes128();
        assert_eq!(provider.algorithm(), Algorithm::AES128GCM);

        let key_data = vec![0u8; 16]; // AES-128 uses 16-byte keys
        let key = Key::new_active(Algorithm::AES128GCM, key_data).unwrap();

        let plaintext = b"Hello, World! This is a test message.";
        let aad = b"additional authenticated data";

        // Encrypt
        let ciphertext = provider.encrypt(&key, plaintext, Some(aad)).unwrap();
        assert_ne!(ciphertext, plaintext);

        // Decrypt
        let decrypted = provider.decrypt(&key, &ciphertext, Some(aad)).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_aes192_encryption_decryption() {
        let provider = AesGcmProvider::aes192();
        assert_eq!(provider.algorithm(), Algorithm::AES192GCM);

        let key_data = vec![0u8; 24]; // AES-192 uses 24-byte keys
        let key = Key::new_active(Algorithm::AES192GCM, key_data).unwrap();

        let plaintext = b"Hello, World! This is a test message.";
        let aad = b"additional authenticated data";

        // Encrypt
        let ciphertext = provider.encrypt(&key, plaintext, Some(aad)).unwrap();
        assert_ne!(ciphertext, plaintext);

        // Decrypt
        let decrypted = provider.decrypt(&key, &ciphertext, Some(aad)).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_aes256_encryption_decryption() {
        let provider = AesGcmProvider::aes256();
        assert_eq!(provider.algorithm(), Algorithm::AES256GCM);

        let key_data = vec![0u8; 32]; // AES-256 uses 32-byte keys
        let key = Key::new_active(Algorithm::AES256GCM, key_data).unwrap();

        let plaintext = b"Hello, World! This is a test message.";
        let aad = b"additional authenticated data";

        // Encrypt
        let ciphertext = provider.encrypt(&key, plaintext, Some(aad)).unwrap();
        assert_ne!(ciphertext, plaintext);

        // Decrypt
        let decrypted = provider.decrypt(&key, &ciphertext, Some(aad)).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_backward_compatibility() {
        // Test that Aes256GcmProvider still works as a type alias
        let provider: Aes256GcmProvider = Aes256GcmProvider::new().unwrap();
        assert_eq!(provider.algorithm(), Algorithm::AES256GCM);

        let key_data = vec![0u8; 32];
        let key = Key::new_active(Algorithm::AES256GCM, key_data).unwrap();

        let plaintext = b"Backward compatibility test";
        let ciphertext = provider.encrypt(&key, plaintext, None).unwrap();
        let decrypted = provider.decrypt(&key, &ciphertext, None).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_aes_with_side_channel_protection() {
        // 使用自定义配置强制启用所有防护
        let config = SideChannelConfig {
            power_analysis_protection: true, // 强制启用电源分析防护
            constant_time_enabled: true,
            error_injection_protection: true,
            cache_protection: true,
            ..SideChannelConfig::default()
        };

        let provider = Aes256GcmProvider::with_side_channel_config(config).unwrap();
        assert!(provider.is_side_channel_protected());

        // Test basic encryption/decryption
        let key_data = vec![0u8; 32];
        let mut key = Key::new(Algorithm::AES256GCM, key_data).unwrap();

        // 激活密钥以使其有效
        key.activate(None).unwrap();

        let plaintext = b"Hello, World! This is a test message.";
        let aad = b"additional authenticated data";

        // Encrypt
        let ciphertext = provider.encrypt(&key, plaintext, Some(aad)).unwrap();
        assert_ne!(ciphertext, plaintext);

        // Decrypt
        let decrypted = provider.decrypt(&key, &ciphertext, Some(aad)).unwrap();
        assert_eq!(decrypted, plaintext);

        // Check stats
        let stats = provider.get_side_channel_stats().unwrap();
        println!("Side-channel stats: {:?}", stats);
        assert!(
            stats.timing_protections > 0
                || stats.masking_operations > 0
                || stats.error_detection_triggers > 0
                || stats.cache_flush_operations > 0
        );
    }

    #[test]
    fn test_aes_without_side_channel_protection() {
        let config = SideChannelConfig {
            power_analysis_protection: false,
            constant_time_enabled: false,
            error_injection_protection: false,
            cache_protection: false,
            timing_noise_enabled: false,
            masking_operations_enabled: false,
            redundancy_checks_enabled: false,
            cache_flush_enabled: false,
            ..SideChannelConfig::default()
        };

        let provider = Aes256GcmProvider::with_side_channel_config(config).unwrap();
        assert!(!provider.is_side_channel_protected());

        // Test basic encryption/decryption
        let key_data = vec![0u8; 32];
        let key = Key::new_active(Algorithm::AES256GCM, key_data).unwrap();
        let plaintext = b"Hello, World! This is a test message.";

        // Encrypt
        let ciphertext = provider.encrypt(&key, plaintext, None).unwrap();
        assert_ne!(ciphertext, plaintext);

        // Decrypt
        let decrypted = provider.decrypt(&key, &ciphertext, None).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_aes_wrong_algorithm_key() {
        let provider = Aes256GcmProvider::new().unwrap();
        let key_data = vec![0u8; 32];
        let wrong_key = Key::new(Algorithm::SM4GCM, key_data).unwrap();
        let plaintext = b"test";

        let result = provider.encrypt(&wrong_key, plaintext, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_aes_invalid_ciphertext() {
        let provider = Aes256GcmProvider::new().unwrap();
        let key_data = vec![0u8; 32];
        let key = Key::new(Algorithm::AES256GCM, key_data).unwrap();
        let invalid_ciphertext = b"too short";

        let result = provider.decrypt(&key, invalid_ciphertext, None);
        assert!(result.is_err());
    }
}

// Convenience constructors for different key lengths
impl AesGcmProvider {
    /// Create a new AES-128 GCM provider
    pub fn aes128() -> Self {
        Self::with_algorithm(Algorithm::AES128GCM)
            .expect("Failed to create AES-128 GCM provider - algorithm should be valid")
    }

    /// Create a new AES-192 GCM provider
    pub fn aes192() -> Self {
        Self::with_algorithm(Algorithm::AES192GCM)
            .expect("Failed to create AES-192 GCM provider - algorithm should be valid")
    }

    /// Create a new AES-256 GCM provider
    pub fn aes256() -> Self {
        Self::with_algorithm(Algorithm::AES256GCM)
            .expect("Failed to create AES-256 GCM provider - algorithm should be valid")
    }

    /// Create a new AES-128 GCM provider with custom side-channel configuration
    #[allow(dead_code)]
    pub fn aes128_with_config(config: SideChannelConfig) -> Self {
        Self::with_algorithm_and_config(Algorithm::AES128GCM, config)
            .expect("Failed to create AES-128 GCM provider - algorithm should be valid")
    }

    /// Create a new AES-192 GCM provider with custom side-channel configuration
    #[allow(dead_code)]
    pub fn aes192_with_config(config: SideChannelConfig) -> Self {
        Self::with_algorithm_and_config(Algorithm::AES192GCM, config)
            .expect("Failed to create AES-192 GCM provider - algorithm should be valid")
    }

    /// Create a new AES-256 GCM provider with custom side-channel configuration
    #[allow(dead_code)]
    pub fn aes256_with_config(config: SideChannelConfig) -> Self {
        Self::with_algorithm_and_config(Algorithm::AES256GCM, config)
            .expect("Failed to create AES-256 GCM provider - algorithm should be valid")
    }

    /// Get the algorithm this provider is configured for
    #[allow(dead_code)]
    pub fn algorithm_type(&self) -> Algorithm {
        self.algorithm
    }
}

crate::impl_cipher_provider!(AesGcmProvider, Algorithm::AES256GCM);