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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Cipher Kind

#[cfg(feature = "v1-aead-extra")]
use super::aeadcipher::{
    Aes128Ccm, Aes128GcmSiv, Aes128OcbTag128, Aes192OcbTag128, Aes256Ccm, Aes256GcmSiv,
    Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, XChacha20Poly1305,
};
#[cfg(feature = "v1-aead")]
use super::aeadcipher::{Aes128Gcm, Aes256Gcm, Chacha20Poly1305};
#[cfg(feature = "v1-stream")]
use super::streamcipher::{
    Aes128Cfb1,
    Aes128Cfb128,
    Aes128Cfb8,
    Aes128Ctr,
    Aes128Ofb,
    Aes192Cfb1,
    Aes192Cfb128,
    Aes192Cfb8,
    Aes192Ctr,
    Aes192Ofb,
    Aes256Cfb1,
    Aes256Cfb128,

    Aes256Cfb8,
    Aes256Ctr,
    Aes256Ofb,
    Camellia128Cfb1,
    Camellia128Cfb128,
    Camellia128Cfb8,
    Camellia128Ctr,
    Camellia128Ofb,
    Camellia192Cfb1,
    Camellia192Cfb128,
    Camellia192Cfb8,
    Camellia192Ctr,
    Camellia192Ofb,
    Camellia256Cfb1,
    Camellia256Cfb128,

    Camellia256Cfb8,
    Camellia256Ctr,
    Camellia256Ofb,
    Chacha20,
    // Rc4, Rc4Md5, Table,
};

/// Category of ciphers
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum CipherCategory {
    /// No encryption
    None,
    /// Stream ciphers is used for OLD ShadowSocks protocol, which uses stream ciphers to encrypt data payloads
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    Stream,
    /// AEAD ciphers is used in modern ShadowSocks protocol, which sends data in separate packets
    #[cfg(feature = "v1-aead")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
    Aead,
}

/// ShadowSocks cipher type
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum CipherKind {
    NONE,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    SS_TABLE,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    SS_RC4_MD5,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_128_CTR,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_192_CTR,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_256_CTR,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_128_CFB1,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_128_CFB8,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_128_CFB128,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_192_CFB1,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_192_CFB8,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_192_CFB128,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_256_CFB1,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_256_CFB8,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_256_CFB128,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_128_OFB,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_192_OFB,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    AES_256_OFB,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_128_CTR,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_192_CTR,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_256_CTR,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_128_CFB1,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_128_CFB8,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_128_CFB128,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_192_CFB1,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_192_CFB8,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_192_CFB128,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_256_CFB1,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_256_CFB8,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_256_CFB128,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_128_OFB,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_192_OFB,
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CAMELLIA_256_OFB,

    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    RC4,
    // NOTE: IETF 版本
    #[cfg(feature = "v1-stream")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-stream")))]
    CHACHA20,

    // AEAD Cipher
    #[cfg(feature = "v1-aead")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
    /// AEAD_AES_128_GCM
    AES_128_GCM,
    #[cfg(feature = "v1-aead")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
    /// AEAD_AES_256_GCM
    AES_256_GCM,

    // NOTE: IETF 版本
    #[cfg(feature = "v1-aead")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead")))]
    /// AEAD_CHACHA20_POLY1305
    CHACHA20_POLY1305,

    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_128_CCM
    AES_128_CCM,
    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_256_CCM
    AES_256_CCM,

    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_128_OCB_TAGLEN128
    AES_128_OCB_TAGLEN128,
    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_192_OCB_TAGLEN128
    AES_192_OCB_TAGLEN128,
    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_256_OCB_TAGLEN128
    AES_256_OCB_TAGLEN128,

    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_SIV_CMAC_256
    AES_SIV_CMAC_256,
    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_SIV_CMAC_384
    AES_SIV_CMAC_384,
    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_SIV_CMAC_512
    AES_SIV_CMAC_512,

    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_128_GCM_SIV
    AES_128_GCM_SIV,
    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_AES_256_GCM_SIV
    AES_256_GCM_SIV,

    #[cfg(feature = "v1-aead-extra")]
    #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
    /// AEAD_XCHACHA20_POLY1305
    XCHACHA20_POLY1305,
}

impl CipherKind {
    /// The category of the cipher
    pub fn category(&self) -> CipherCategory {
        #[cfg(feature = "v1-stream")]
        if self.is_stream() {
            return CipherCategory::Stream;
        }

        #[cfg(feature = "v1-aead")]
        if self.is_aead() {
            return CipherCategory::Aead;
        }

        CipherCategory::None
    }

    /// Check if the current cipher is `NONE`
    pub fn is_none(&self) -> bool {
        matches!(*self, CipherKind::NONE)
    }

    /// Check if the current cipher is a stream cipher
    #[cfg(feature = "v1-stream")]
    #[allow(clippy::match_like_matches_macro)]
    pub fn is_stream(&self) -> bool {
        use self::CipherKind::*;

        match *self {
            SS_TABLE | SS_RC4_MD5 | AES_128_CTR | AES_192_CTR | AES_256_CTR | AES_128_CFB1
            | AES_128_CFB8 | AES_128_CFB128 | AES_192_CFB1 | AES_192_CFB8 | AES_192_CFB128
            | AES_256_CFB1 | AES_256_CFB8 | AES_256_CFB128 | AES_128_OFB | AES_192_OFB
            | AES_256_OFB | CAMELLIA_128_CTR | CAMELLIA_192_CTR | CAMELLIA_256_CTR
            | CAMELLIA_128_CFB1 | CAMELLIA_128_CFB8 | CAMELLIA_128_CFB128 | CAMELLIA_192_CFB1
            | CAMELLIA_192_CFB8 | CAMELLIA_192_CFB128 | CAMELLIA_256_CFB1 | CAMELLIA_256_CFB8
            | CAMELLIA_256_CFB128 | CAMELLIA_128_OFB | CAMELLIA_192_OFB | CAMELLIA_256_OFB
            | RC4 | CHACHA20 => true,
            _ => false,
        }
    }

    /// Check if the current cipher is an AEAD cipher
    #[cfg(feature = "v1-aead")]
    pub fn is_aead(&self) -> bool {
        use self::CipherKind::*;

        match *self {
            AES_128_GCM | AES_256_GCM | CHACHA20_POLY1305 => true,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_CCM
            | AES_256_CCM
            | AES_128_OCB_TAGLEN128
            | AES_192_OCB_TAGLEN128
            | AES_256_OCB_TAGLEN128
            | AES_SIV_CMAC_256
            | AES_SIV_CMAC_384
            | AES_SIV_CMAC_512
            | AES_128_GCM_SIV
            | AES_256_GCM_SIV
            | XCHACHA20_POLY1305 => true,

            _ => false,
        }
    }

    /// Key length of the cipher
    pub fn key_len(&self) -> usize {
        use self::CipherKind::*;

        match *self {
            NONE => 0,

            #[cfg(feature = "v1-stream")]
            SS_TABLE => 0,
            // NOTE: RC4 密码本身支持 1..256 长度的 Key,
            //       但是 SS 这里把 Key 的长度限制在 16.
            #[cfg(feature = "v1-stream")]
            SS_RC4_MD5 => 16,

            #[cfg(feature = "v1-stream")]
            AES_128_CTR => Aes128Ctr::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_192_CTR => Aes192Ctr::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_256_CTR => Aes256Ctr::KEY_LEN,

            #[cfg(feature = "v1-stream")]
            AES_128_CFB1 => Aes128Cfb1::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_128_CFB8 => Aes128Cfb8::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_128_CFB128 => Aes128Cfb128::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_192_CFB1 => Aes192Cfb1::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_192_CFB8 => Aes192Cfb8::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_192_CFB128 => Aes192Cfb128::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_256_CFB1 => Aes256Cfb1::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_256_CFB8 => Aes256Cfb8::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_256_CFB128 => Aes256Cfb128::KEY_LEN,

            #[cfg(feature = "v1-stream")]
            AES_128_OFB => Aes128Ofb::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_192_OFB => Aes192Ofb::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            AES_256_OFB => Aes256Ofb::KEY_LEN,

            #[cfg(feature = "v1-stream")]
            CAMELLIA_128_CTR => Camellia128Ctr::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_192_CTR => Camellia192Ctr::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_256_CTR => Camellia256Ctr::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_128_CFB1 => Camellia128Cfb1::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_128_CFB8 => Camellia128Cfb8::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_128_CFB128 => Camellia128Cfb128::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_192_CFB1 => Camellia192Cfb1::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_192_CFB8 => Camellia192Cfb8::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_192_CFB128 => Camellia192Cfb128::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_256_CFB1 => Camellia256Cfb1::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_256_CFB8 => Camellia256Cfb8::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_256_CFB128 => Camellia256Cfb128::KEY_LEN,

            #[cfg(feature = "v1-stream")]
            CAMELLIA_128_OFB => Camellia128Ofb::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_192_OFB => Camellia192Ofb::KEY_LEN,
            #[cfg(feature = "v1-stream")]
            CAMELLIA_256_OFB => Camellia256Ofb::KEY_LEN,

            // NOTE: RC4 密码本身支持 1..256 长度的 Key,
            //       但是 SS 这里把 Key 的长度限制在 16.
            #[cfg(feature = "v1-stream")]
            RC4 => 16,
            #[cfg(feature = "v1-stream")]
            CHACHA20 => Chacha20::KEY_LEN,

            // AEAD
            #[cfg(feature = "v1-aead")]
            AES_128_GCM => Aes128Gcm::KEY_LEN,
            #[cfg(feature = "v1-aead")]
            AES_256_GCM => Aes256Gcm::KEY_LEN,

            #[cfg(feature = "v1-aead")]
            CHACHA20_POLY1305 => Chacha20Poly1305::KEY_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_CCM => Aes128Ccm::KEY_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_256_CCM => Aes256Ccm::KEY_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_OCB_TAGLEN128 => Aes128OcbTag128::KEY_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_192_OCB_TAGLEN128 => Aes192OcbTag128::KEY_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_256_OCB_TAGLEN128 => Aes256OcbTag128::KEY_LEN,
            // NOTE: 注意 AES_SIV_CMAC_256 的 KEY 是 两个 AES-128 的 Key.
            //       所以是 256.
            #[cfg(feature = "v1-aead-extra")]
            AES_SIV_CMAC_256 => AesSivCmac256::KEY_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_SIV_CMAC_384 => AesSivCmac384::KEY_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_SIV_CMAC_512 => AesSivCmac512::KEY_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_GCM_SIV => Aes128GcmSiv::KEY_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_256_GCM_SIV => Aes256GcmSiv::KEY_LEN,

            #[cfg(feature = "v1-aead-extra")]
            XCHACHA20_POLY1305 => XChacha20Poly1305::KEY_LEN,
        }
    }

    /// Stream Cipher's initializer vector length
    #[cfg(feature = "v1-stream")]
    pub fn iv_len(&self) -> usize {
        use self::CipherKind::*;

        match *self {
            NONE => 0,
            SS_TABLE => 0,
            // NOTE: RC4 密码本身没有 IV 概念,
            //       但是 SS 这里把 Key 的长度限制在 16.
            SS_RC4_MD5 => 16,
            AES_128_CTR => Aes128Ctr::IV_LEN,
            AES_192_CTR => Aes192Ctr::IV_LEN,
            AES_256_CTR => Aes256Ctr::IV_LEN,

            AES_128_CFB1 => Aes128Cfb1::IV_LEN,
            AES_128_CFB8 => Aes128Cfb8::IV_LEN,
            AES_128_CFB128 => Aes128Cfb128::IV_LEN,
            AES_192_CFB1 => Aes192Cfb1::IV_LEN,
            AES_192_CFB8 => Aes192Cfb8::IV_LEN,
            AES_192_CFB128 => Aes192Cfb128::IV_LEN,
            AES_256_CFB1 => Aes256Cfb1::IV_LEN,
            AES_256_CFB8 => Aes256Cfb8::IV_LEN,
            AES_256_CFB128 => Aes256Cfb128::IV_LEN,

            AES_128_OFB => Aes128Ofb::IV_LEN,
            AES_192_OFB => Aes192Ofb::IV_LEN,
            AES_256_OFB => Aes256Ofb::IV_LEN,

            CAMELLIA_128_CTR => Camellia128Ctr::IV_LEN,
            CAMELLIA_192_CTR => Camellia192Ctr::IV_LEN,
            CAMELLIA_256_CTR => Camellia256Ctr::IV_LEN,

            CAMELLIA_128_CFB1 => Camellia128Cfb1::IV_LEN,
            CAMELLIA_128_CFB8 => Camellia128Cfb8::IV_LEN,
            CAMELLIA_128_CFB128 => Camellia128Cfb128::IV_LEN,
            CAMELLIA_192_CFB1 => Camellia192Cfb1::IV_LEN,
            CAMELLIA_192_CFB8 => Camellia192Cfb8::IV_LEN,
            CAMELLIA_192_CFB128 => Camellia192Cfb128::IV_LEN,
            CAMELLIA_256_CFB1 => Camellia256Cfb1::IV_LEN,
            CAMELLIA_256_CFB8 => Camellia256Cfb8::IV_LEN,
            CAMELLIA_256_CFB128 => Camellia256Cfb128::IV_LEN,

            CAMELLIA_128_OFB => Camellia128Ofb::IV_LEN,
            CAMELLIA_192_OFB => Camellia192Ofb::IV_LEN,
            CAMELLIA_256_OFB => Camellia256Ofb::IV_LEN,
            // NOTE: RC4 doesn't have an IV
            RC4 => 0,
            CHACHA20 => Chacha20::NONCE_LEN,
            _ => panic!("only support Stream ciphers"),
        }
    }

    /// AEAD Cipher's TAG length
    #[cfg(feature = "v1-aead")]
    pub fn tag_len(&self) -> usize {
        use self::CipherKind::*;

        match *self {
            AES_128_GCM => Aes128Gcm::TAG_LEN,
            AES_256_GCM => Aes256Gcm::TAG_LEN,

            CHACHA20_POLY1305 => Chacha20Poly1305::TAG_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_CCM => Aes128Ccm::TAG_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_256_CCM => Aes256Ccm::TAG_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_OCB_TAGLEN128 => Aes128OcbTag128::TAG_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_192_OCB_TAGLEN128 => Aes192OcbTag128::TAG_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_256_OCB_TAGLEN128 => Aes256OcbTag128::TAG_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_SIV_CMAC_256 => AesSivCmac256::TAG_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_SIV_CMAC_384 => AesSivCmac384::TAG_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_SIV_CMAC_512 => AesSivCmac512::TAG_LEN,

            #[cfg(feature = "v1-aead-extra")]
            AES_128_GCM_SIV => Aes128GcmSiv::TAG_LEN,
            #[cfg(feature = "v1-aead-extra")]
            AES_256_GCM_SIV => Aes256GcmSiv::TAG_LEN,

            #[cfg(feature = "v1-aead-extra")]
            XCHACHA20_POLY1305 => XChacha20Poly1305::TAG_LEN,

            _ => panic!("only support AEAD ciphers"),
        }
    }

    /// AEAD Cipher's SALT length
    #[cfg(feature = "v1-aead")]
    pub fn salt_len(&self) -> usize {
        if !self.is_aead() {
            panic!("only support AEAD ciphers");
        }

        self.key_len()
    }
}

impl core::fmt::Display for CipherKind {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        f.write_str(match *self {
            CipherKind::NONE => "none",

            #[cfg(feature = "v1-stream")]
            CipherKind::SS_TABLE => "table",
            #[cfg(feature = "v1-stream")]
            CipherKind::SS_RC4_MD5 => "rc4-md5",

            #[cfg(feature = "v1-stream")]
            CipherKind::AES_128_CTR => "aes-128-ctr",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_192_CTR => "aes-192-ctr",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_256_CTR => "aes-256-ctr",

            #[cfg(feature = "v1-stream")]
            CipherKind::AES_128_CFB128 => "aes-128-cfb",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_128_CFB1 => "aes-128-cfb1",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_128_CFB8 => "aes-128-cfb8",

            #[cfg(feature = "v1-stream")]
            CipherKind::AES_192_CFB128 => "aes-192-cfb",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_192_CFB1 => "aes-192-cfb1",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_192_CFB8 => "aes-192-cfb8",

            #[cfg(feature = "v1-stream")]
            CipherKind::AES_256_CFB128 => "aes-256-cfb",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_256_CFB1 => "aes-256-cfb1",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_256_CFB8 => "aes-256-cfb8",

            #[cfg(feature = "v1-stream")]
            CipherKind::AES_128_OFB => "aes-128-ofb",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_192_OFB => "aes-192-ofb",
            #[cfg(feature = "v1-stream")]
            CipherKind::AES_256_OFB => "aes-256-ofb",

            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_128_CTR => "camellia-128-ctr",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_192_CTR => "camellia-192-ctr",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_256_CTR => "camellia-256-ctr",

            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_128_CFB128 => "camellia-128-cfb",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_128_CFB1 => "camellia-128-cfb1",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_128_CFB8 => "camellia-128-cfb8",

            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_192_CFB128 => "camellia-192-cfb",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_192_CFB1 => "camellia-192-cfb1",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_192_CFB8 => "camellia-192-cfb8",

            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_256_CFB128 => "camellia-256-cfb",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_256_CFB1 => "camellia-256-cfb1",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_256_CFB8 => "camellia-256-cfb8",

            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_128_OFB => "camellia-128-ofb",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_192_OFB => "camellia-192-ofb",
            #[cfg(feature = "v1-stream")]
            CipherKind::CAMELLIA_256_OFB => "camellia-256-ofb",

            #[cfg(feature = "v1-stream")]
            CipherKind::RC4 => "rc4",
            #[cfg(feature = "v1-stream")]
            CipherKind::CHACHA20 => "chacha20-ietf",

            #[cfg(feature = "v1-aead")]
            CipherKind::AES_128_GCM => "aes-128-gcm",
            #[cfg(feature = "v1-aead")]
            CipherKind::AES_256_GCM => "aes-256-gcm",

            #[cfg(feature = "v1-aead")]
            CipherKind::CHACHA20_POLY1305 => "chacha20-ietf-poly1305",

            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_128_CCM => "aes-128-ccm",
            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_256_CCM => "aes-256-ccm",

            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_128_GCM_SIV => "aes-128-gcm-siv",
            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_256_GCM_SIV => "aes-256-gcm-siv",

            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_128_OCB_TAGLEN128 => "aes-128-ocb-taglen128",
            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_192_OCB_TAGLEN128 => "aes-192-ocb-taglen128",
            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_256_OCB_TAGLEN128 => "aes-256-ocb-taglen128",

            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_SIV_CMAC_256 => "aes-siv-cmac-256",
            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_SIV_CMAC_384 => "aes-siv-cmac-384",
            #[cfg(feature = "v1-aead-extra")]
            CipherKind::AES_SIV_CMAC_512 => "aes-siv-cmac-512",

            #[cfg(feature = "v1-aead-extra")]
            CipherKind::XCHACHA20_POLY1305 => "xchacha20-ietf-poly1305",
        })
    }
}

/// Error while parsing `CipherKind` from string
#[derive(Debug, Clone)]
pub struct ParseCipherKindError;

impl core::str::FromStr for CipherKind {
    type Err = ParseCipherKindError;

    fn from_str(s: &str) -> Result<Self, ParseCipherKindError> {
        use self::CipherKind::*;

        match s.to_lowercase().as_str() {
            "plain" | "none" => Ok(NONE),

            #[cfg(feature = "v1-stream")]
            "table" | "" => Ok(SS_TABLE),
            #[cfg(feature = "v1-stream")]
            "rc4-md5" => Ok(SS_RC4_MD5),

            #[cfg(feature = "v1-stream")]
            "aes-128-ctr" => Ok(AES_128_CTR),
            #[cfg(feature = "v1-stream")]
            "aes-192-ctr" => Ok(AES_192_CTR),
            #[cfg(feature = "v1-stream")]
            "aes-256-ctr" => Ok(AES_256_CTR),

            #[cfg(feature = "v1-stream")]
            "aes-128-cfb" => Ok(AES_128_CFB128),
            #[cfg(feature = "v1-stream")]
            "aes-128-cfb1" => Ok(AES_128_CFB1),
            #[cfg(feature = "v1-stream")]
            "aes-128-cfb8" => Ok(AES_128_CFB8),
            #[cfg(feature = "v1-stream")]
            "aes-128-cfb128" => Ok(AES_128_CFB128),

            #[cfg(feature = "v1-stream")]
            "aes-192-cfb" => Ok(AES_192_CFB128),
            #[cfg(feature = "v1-stream")]
            "aes-192-cfb1" => Ok(AES_192_CFB1),
            #[cfg(feature = "v1-stream")]
            "aes-192-cfb8" => Ok(AES_192_CFB8),
            #[cfg(feature = "v1-stream")]
            "aes-192-cfb128" => Ok(AES_192_CFB128),

            #[cfg(feature = "v1-stream")]
            "aes-256-cfb" => Ok(AES_256_CFB128),
            #[cfg(feature = "v1-stream")]
            "aes-256-cfb1" => Ok(AES_256_CFB1),
            #[cfg(feature = "v1-stream")]
            "aes-256-cfb8" => Ok(AES_256_CFB8),
            #[cfg(feature = "v1-stream")]
            "aes-256-cfb128" => Ok(AES_256_CFB128),

            #[cfg(feature = "v1-stream")]
            "aes-128-ofb" => Ok(AES_128_OFB),
            #[cfg(feature = "v1-stream")]
            "aes-192-ofb" => Ok(AES_192_OFB),
            #[cfg(feature = "v1-stream")]
            "aes-256-ofb" => Ok(AES_256_OFB),

            #[cfg(feature = "v1-stream")]
            "camellia-128-ctr" => Ok(CAMELLIA_128_CTR),
            #[cfg(feature = "v1-stream")]
            "camellia-192-ctr" => Ok(CAMELLIA_192_CTR),
            #[cfg(feature = "v1-stream")]
            "camellia-256-ctr" => Ok(CAMELLIA_256_CTR),

            #[cfg(feature = "v1-stream")]
            "camellia-128-cfb" => Ok(CAMELLIA_128_CFB128),
            #[cfg(feature = "v1-stream")]
            "camellia-128-cfb1" => Ok(CAMELLIA_128_CFB1),
            #[cfg(feature = "v1-stream")]
            "camellia-128-cfb8" => Ok(CAMELLIA_128_CFB8),
            #[cfg(feature = "v1-stream")]
            "camellia-128-cfb128" => Ok(CAMELLIA_128_CFB128),

            #[cfg(feature = "v1-stream")]
            "camellia-192-cfb" => Ok(CAMELLIA_192_CFB128),
            #[cfg(feature = "v1-stream")]
            "camellia-192-cfb1" => Ok(CAMELLIA_192_CFB1),
            #[cfg(feature = "v1-stream")]
            "camellia-192-cfb8" => Ok(CAMELLIA_192_CFB8),
            #[cfg(feature = "v1-stream")]
            "camellia-192-cfb128" => Ok(CAMELLIA_192_CFB128),

            #[cfg(feature = "v1-stream")]
            "camellia-256-cfb" => Ok(CAMELLIA_256_CFB128),
            #[cfg(feature = "v1-stream")]
            "camellia-256-cfb1" => Ok(CAMELLIA_256_CFB1),
            #[cfg(feature = "v1-stream")]
            "camellia-256-cfb8" => Ok(CAMELLIA_256_CFB8),
            #[cfg(feature = "v1-stream")]
            "camellia-256-cfb128" => Ok(CAMELLIA_256_CFB128),

            #[cfg(feature = "v1-stream")]
            "camellia-128-ofb" => Ok(CAMELLIA_128_OFB),
            #[cfg(feature = "v1-stream")]
            "camellia-192-ofb" => Ok(CAMELLIA_192_OFB),
            #[cfg(feature = "v1-stream")]
            "camellia-256-ofb" => Ok(CAMELLIA_256_OFB),

            #[cfg(feature = "v1-stream")]
            "rc4" => Ok(RC4),
            #[cfg(feature = "v1-stream")]
            "chacha20-ietf" => Ok(CHACHA20),

            // AEAD Ciphers
            #[cfg(feature = "v1-aead")]
            "aes-128-gcm" => Ok(AES_128_GCM),
            #[cfg(feature = "v1-aead")]
            "aes-256-gcm" => Ok(AES_256_GCM),
            #[cfg(feature = "v1-aead")]
            "chacha20-ietf-poly1305" => Ok(CHACHA20_POLY1305),

            #[cfg(feature = "v1-aead-extra")]
            "aes-128-ccm" => Ok(AES_128_CCM),
            #[cfg(feature = "v1-aead-extra")]
            "aes-256-ccm" => Ok(AES_256_CCM),

            #[cfg(feature = "v1-aead-extra")]
            "aes-128-gcm-siv" => Ok(AES_128_GCM_SIV),
            #[cfg(feature = "v1-aead-extra")]
            "aes-256-gcm-siv" => Ok(AES_256_GCM_SIV),

            #[cfg(feature = "v1-aead-extra")]
            "aes-128-ocb-taglen128" => Ok(AES_128_OCB_TAGLEN128),
            #[cfg(feature = "v1-aead-extra")]
            "aes-192-ocb-taglen128" => Ok(AES_192_OCB_TAGLEN128),
            #[cfg(feature = "v1-aead-extra")]
            "aes-256-ocb-taglen128" => Ok(AES_256_OCB_TAGLEN128),

            #[cfg(feature = "v1-aead-extra")]
            "aes-siv-cmac-256" => Ok(AES_SIV_CMAC_256),
            #[cfg(feature = "v1-aead-extra")]
            "aes-siv-cmac-384" => Ok(AES_SIV_CMAC_384),
            #[cfg(feature = "v1-aead-extra")]
            "aes-siv-cmac-512" => Ok(AES_SIV_CMAC_512),

            #[cfg(feature = "v1-aead-extra")]
            "xchacha20-ietf-poly1305" => Ok(XCHACHA20_POLY1305),

            _ => Err(ParseCipherKindError),
        }
    }
}