ferritls-rustls 0.5.0

rustls CryptoProvider adapter backed by ferritls-core (pure Rust)
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
//! TLS 1.3 密码套件装配(M6)。
//!
//! HKDF 复用 rustls 内建 [`rustls::crypto::tls13::HkdfUsingHmac`],包装
//! 本 crate 实现的 [`rustls::crypto::hmac::Hmac`](不在边界内重写密钥
//! 调度);AEAD 为 ferritls-core 的 GCM/CCM/ChaCha20-Poly1305 适配。
//!
//! 注意:`quic` 字段为 `None` = 本套件不参与 QUIC 握手(QUIC packet
//! protection 是 M8+ 项)。

use rustls::crypto::CipherSuiteCommon;
use rustls::crypto::cipher::{
    AeadKey, InboundOpaqueMessage, InboundPlainMessage, Iv, MessageDecrypter, MessageEncrypter,
    Nonce, PrefixedPayload, Tls13AeadAlgorithm, UnsupportedOperationError, make_tls13_aad,
};
use rustls::crypto::hash::{Context, Hash, HashAlgorithm, Output};
use rustls::crypto::hmac::{self, Hmac, Key};
use rustls::crypto::tls13::HkdfUsingHmac;
use rustls::{
    ConnectionTrafficSecrets, ContentType, Error, ProtocolVersion, SupportedCipherSuite,
    Tls13CipherSuite,
};

use ferritls_core::chacha20poly1305::ChaCha20Poly1305;
use ferritls_core::gcm::{Aes128Gcm, Aes256Gcm};

use ferritls_core::ccm::Aes128CcmTls;

/// 套件清单(顺序即偏好)。同时被 `tests/api.rs` 断言,防止清单与
/// 文档漂移。
pub const TLS13_SUITE_NAMES: &[&str] = &[
    "TLS_AES_128_GCM_SHA256",
    "TLS_AES_256_GCM_SHA384",
    "TLS_CHACHA20_POLY1305_SHA256",
    "TLS_AES_128_CCM_SHA256",
];

// ---------------------------------------------------------------------------
// Hash 适配(SHA-256 / SHA-384)
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct Sha256Hash;

struct Sha256Ctx(ferritls_core::sha2::Sha256);

impl Context for Sha256Ctx {
    fn fork_finish(&self) -> Output {
        Output::new(&self.0.clone().finalize())
    }

    fn fork(&self) -> Box<dyn Context> {
        Box::new(Self(self.0.clone()))
    }

    fn finish(self: Box<Self>) -> Output {
        Output::new(&self.0.finalize())
    }

    fn update(&mut self, data: &[u8]) {
        self.0.update(data);
    }
}

impl Hash for Sha256Hash {
    fn start(&self) -> Box<dyn Context> {
        Box::new(Sha256Ctx(ferritls_core::sha2::Sha256::new()))
    }

    fn hash(&self, data: &[u8]) -> Output {
        Output::new(&ferritls_core::sha2::Sha256::one_shot(data))
    }

    fn output_len(&self) -> usize {
        32
    }

    fn algorithm(&self) -> HashAlgorithm {
        HashAlgorithm::SHA256
    }
}

#[derive(Debug)]
struct Sha384Hash;

struct Sha384Ctx(ferritls_core::sha2::Sha384);

impl Context for Sha384Ctx {
    fn fork_finish(&self) -> Output {
        Output::new(&self.0.clone().finalize())
    }

    fn fork(&self) -> Box<dyn Context> {
        Box::new(Self(self.0.clone()))
    }

    fn finish(self: Box<Self>) -> Output {
        Output::new(&self.0.finalize())
    }

    fn update(&mut self, data: &[u8]) {
        self.0.update(data);
    }
}

impl Hash for Sha384Hash {
    fn start(&self) -> Box<dyn Context> {
        Box::new(Sha384Ctx(ferritls_core::sha2::Sha384::new()))
    }

    fn hash(&self, data: &[u8]) -> Output {
        Output::new(&ferritls_core::sha2::Sha384::one_shot(data))
    }

    fn output_len(&self) -> usize {
        48
    }

    fn algorithm(&self) -> HashAlgorithm {
        HashAlgorithm::SHA384
    }
}

pub(crate) static SHA256_HASH: &dyn Hash = &Sha256Hash;
pub(crate) static SHA384_HASH: &dyn Hash = &Sha384Hash;

// ---------------------------------------------------------------------------
// Hmac 适配(rustls::crypto::hmac;供 HkdfUsingHmac 复用)
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct HmacSha256Impl;

#[derive(Debug)]
struct HmacSha256Key(Vec<u8>);

impl Key for HmacSha256Key {
    fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> hmac::Tag {
        let mut buf = Vec::with_capacity(first.len() + 16 * middle.len() + last.len());
        buf.extend_from_slice(first);
        for m in middle {
            buf.extend_from_slice(m);
        }
        buf.extend_from_slice(last);
        hmac::Tag::new(&ferritls_core::hmac::HmacSha256::one_shot(&self.0, &buf))
    }

    fn tag_len(&self) -> usize {
        32
    }
}

impl Hmac for HmacSha256Impl {
    fn with_key(&self, key: &[u8]) -> Box<dyn Key> {
        Box::new(HmacSha256Key(key.to_vec()))
    }

    fn hash_output_len(&self) -> usize {
        32
    }
}

#[derive(Debug)]
struct HmacSha384Impl;

#[derive(Debug)]
struct HmacSha384Key(Vec<u8>);

impl Key for HmacSha384Key {
    fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> hmac::Tag {
        let mut buf = Vec::with_capacity(first.len() + 16 * middle.len() + last.len());
        buf.extend_from_slice(first);
        for m in middle {
            buf.extend_from_slice(m);
        }
        buf.extend_from_slice(last);
        hmac::Tag::new(&ferritls_core::hmac::HmacSha384::one_shot(&self.0, &buf))
    }

    fn tag_len(&self) -> usize {
        48
    }
}

impl Hmac for HmacSha384Impl {
    fn with_key(&self, key: &[u8]) -> Box<dyn Key> {
        Box::new(HmacSha384Key(key.to_vec()))
    }

    fn hash_output_len(&self) -> usize {
        48
    }
}

static HMAC_SHA256_IMPL: HmacSha256Impl = HmacSha256Impl;
static HMAC_SHA384_IMPL: HmacSha384Impl = HmacSha384Impl;
static HKDF_USING_HMAC_SHA256: HkdfUsingHmac<'static> = HkdfUsingHmac(&HMAC_SHA256_IMPL);
static HKDF_USING_HMAC_SHA384: HkdfUsingHmac<'static> = HkdfUsingHmac(&HMAC_SHA384_IMPL);

pub(crate) static HKDF_SHA256_PROVIDER: &dyn rustls::crypto::tls13::Hkdf = &HKDF_USING_HMAC_SHA256;
pub(crate) static HKDF_SHA384_PROVIDER: &dyn rustls::crypto::tls13::Hkdf = &HKDF_USING_HMAC_SHA384;

// ---------------------------------------------------------------------------
// AEAD 适配:GCM
// ---------------------------------------------------------------------------

const TAG_LEN: usize = 16;

/// 从 `AeadKey` 提取定长密钥字节。
fn key_bytes<const N: usize>(key: &AeadKey) -> [u8; N] {
    let mut out = [0u8; N];
    out.copy_from_slice(key.as_ref());
    out
}

/// GCM 实例(按密钥长度选择 AES-128/256)。
enum GcmInstance {
    Aes128(Aes128Gcm),
    Aes256(Aes256Gcm),
}

impl GcmInstance {
    fn seal(&self, nonce: &Nonce, aad: &[u8], plaintext: &[u8]) -> Vec<u8> {
        match self {
            GcmInstance::Aes128(g) => g.seal(&nonce.0, aad, plaintext),
            GcmInstance::Aes256(g) => g.seal(&nonce.0, aad, plaintext),
        }
    }

    fn open(&self, nonce: &Nonce, aad: &[u8], ct_tag: &[u8]) -> Result<Vec<u8>, Error> {
        match self {
            GcmInstance::Aes128(g) => g.open(&nonce.0, aad, ct_tag),
            GcmInstance::Aes256(g) => g.open(&nonce.0, aad, ct_tag),
        }
        .map_err(|_| Error::DecryptError)
    }
}

fn gcm_of<const N: usize>(key: &AeadKey) -> GcmInstance {
    if N == 32 {
        GcmInstance::Aes256(Aes256Gcm::new(&key_bytes::<32>(key)))
    } else {
        GcmInstance::Aes128(Aes128Gcm::new(&key_bytes::<16>(key)))
    }
}

struct GcmEncrypter<const N: usize> {
    gcm: GcmInstance,
    iv: Iv,
    _n: std::marker::PhantomData<[u8; N]>,
}

struct GcmDecrypter<const N: usize> {
    gcm: GcmInstance,
    iv: Iv,
    _n: std::marker::PhantomData<[u8; N]>,
}

impl<const N: usize> GcmEncrypter<N> {
    fn seal(&self, nonce: &Nonce, aad: &[u8], plaintext: &[u8]) -> Vec<u8> {
        self.gcm.seal(nonce, aad, plaintext)
    }
}

impl<const N: usize> GcmDecrypter<N> {
    fn open(&self, nonce: &Nonce, aad: &[u8], ct_tag: &[u8]) -> Result<Vec<u8>, Error> {
        self.gcm.open(nonce, aad, ct_tag)
    }
}

impl<const N: usize> MessageEncrypter for GcmEncrypter<N> {
    fn encrypt(
        &mut self,
        msg: rustls::crypto::cipher::OutboundPlainMessage<'_>,
        seq: u64,
    ) -> Result<rustls::crypto::cipher::OutboundOpaqueMessage, Error> {
        let total_len = self.encrypted_payload_len(msg.payload.len());
        let nonce = Nonce::new(&self.iv, seq);
        let aad = make_tls13_aad(total_len);

        let mut plain = msg.payload.to_vec();
        plain.push(msg.typ.to_array()[0]);
        let sealed = self.seal(&nonce, &aad, &plain);

        let mut payload = PrefixedPayload::with_capacity(total_len);
        payload.extend_from_slice(&sealed);
        Ok(rustls::crypto::cipher::OutboundOpaqueMessage::new(
            ContentType::ApplicationData,
            // RFC 8446 §5.1:TLS 1.3 应用数据记录沿用 legacy 版本 0x0303
            ProtocolVersion::TLSv1_2,
            payload,
        ))
    }

    fn encrypted_payload_len(&self, payload_len: usize) -> usize {
        payload_len + 1 + TAG_LEN
    }
}

impl<const N: usize> MessageDecrypter for GcmDecrypter<N> {
    fn decrypt<'a>(
        &mut self,
        mut msg: InboundOpaqueMessage<'a>,
        seq: u64,
    ) -> Result<InboundPlainMessage<'a>, Error> {
        let payload = &mut msg.payload;
        if payload.len() < TAG_LEN + 1 {
            return Err(Error::DecryptError);
        }
        let nonce = Nonce::new(&self.iv, seq);
        let aad = make_tls13_aad(payload.len());
        let plain = self
            .open(&nonce, &aad, payload)
            .map_err(|_| Error::DecryptError)?;
        let plain_len = plain.len();
        payload[..plain_len].copy_from_slice(&plain);
        payload.truncate(plain_len);
        msg.into_tls13_unpadded_message()
    }
}

macro_rules! gcm_aead {
    ($name:ident, $key_len:expr, $secret:ident, $doc:expr) => {
        #[doc = $doc]
        #[derive(Debug)]
        pub struct $name;

        impl Tls13AeadAlgorithm for $name {
            fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
                Box::new(GcmEncrypter::<$key_len> {
                    gcm: gcm_of::<$key_len>(&key),
                    iv,
                    _n: std::marker::PhantomData,
                })
            }

            fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
                Box::new(GcmDecrypter::<$key_len> {
                    gcm: gcm_of::<$key_len>(&key),
                    iv,
                    _n: std::marker::PhantomData,
                })
            }

            fn key_len(&self) -> usize {
                $key_len
            }

            fn extract_keys(
                &self,
                key: AeadKey,
                iv: Iv,
            ) -> Result<ConnectionTrafficSecrets, UnsupportedOperationError> {
                Ok(ConnectionTrafficSecrets::$secret { key, iv })
            }
        }
    };
}

gcm_aead!(Gcm128Aead, 16, Aes128Gcm, "AES-128-GCM AEAD 适配(批准)。");
gcm_aead!(Gcm256Aead, 32, Aes256Gcm, "AES-256-GCM AEAD 适配(批准)。");

// ---------------------------------------------------------------------------
// AEAD 适配:ChaCha20-Poly1305(非批准,仅默认模式)
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub struct Chacha20Poly1305Aead;

struct ChachaEncrypter {
    aead: ChaCha20Poly1305,
    iv: Iv,
}

struct ChachaDecrypter {
    aead: ChaCha20Poly1305,
    iv: Iv,
}

impl MessageEncrypter for ChachaEncrypter {
    fn encrypt(
        &mut self,
        msg: rustls::crypto::cipher::OutboundPlainMessage<'_>,
        seq: u64,
    ) -> Result<rustls::crypto::cipher::OutboundOpaqueMessage, Error> {
        let total_len = self.encrypted_payload_len(msg.payload.len());
        let nonce = Nonce::new(&self.iv, seq);
        let aad = make_tls13_aad(total_len);

        let mut plain = msg.payload.to_vec();
        plain.push(msg.typ.to_array()[0]);
        let sealed = self.aead.seal(&nonce.0, &aad, &plain);

        let mut payload = PrefixedPayload::with_capacity(total_len);
        payload.extend_from_slice(&sealed);
        Ok(rustls::crypto::cipher::OutboundOpaqueMessage::new(
            ContentType::ApplicationData,
            ProtocolVersion::TLSv1_2,
            payload,
        ))
    }

    fn encrypted_payload_len(&self, payload_len: usize) -> usize {
        payload_len + 1 + TAG_LEN
    }
}

impl MessageDecrypter for ChachaDecrypter {
    fn decrypt<'a>(
        &mut self,
        mut msg: InboundOpaqueMessage<'a>,
        seq: u64,
    ) -> Result<InboundPlainMessage<'a>, Error> {
        let payload = &mut msg.payload;
        if payload.len() < TAG_LEN + 1 {
            return Err(Error::DecryptError);
        }
        let nonce = Nonce::new(&self.iv, seq);
        let aad = make_tls13_aad(payload.len());
        let plain = self
            .aead
            .open(&nonce.0, &aad, payload)
            .map_err(|_| Error::DecryptError)?;
        let plain_len = plain.len();
        payload[..plain_len].copy_from_slice(&plain);
        payload.truncate(plain_len);
        msg.into_tls13_unpadded_message()
    }
}

impl Tls13AeadAlgorithm for Chacha20Poly1305Aead {
    fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
        Box::new(ChachaEncrypter {
            aead: ChaCha20Poly1305::new(&key_bytes::<32>(&key)),
            iv,
        })
    }

    fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
        Box::new(ChachaDecrypter {
            aead: ChaCha20Poly1305::new(&key_bytes::<32>(&key)),
            iv,
        })
    }

    fn key_len(&self) -> usize {
        32
    }

    fn extract_keys(
        &self,
        key: AeadKey,
        iv: Iv,
    ) -> Result<ConnectionTrafficSecrets, UnsupportedOperationError> {
        Ok(ConnectionTrafficSecrets::Chacha20Poly1305 { key, iv })
    }
}

// ---------------------------------------------------------------------------
// AEAD 适配:AES-128-CCM(TLS 1.3 参数集,nonce 12 / L=3)
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub struct Ccm128Aead;

struct CcmEncrypter {
    ccm: Aes128CcmTls,
    iv: Iv,
}

struct CcmDecrypter {
    ccm: Aes128CcmTls,
    iv: Iv,
}

impl MessageEncrypter for CcmEncrypter {
    fn encrypt(
        &mut self,
        msg: rustls::crypto::cipher::OutboundPlainMessage<'_>,
        seq: u64,
    ) -> Result<rustls::crypto::cipher::OutboundOpaqueMessage, Error> {
        let total_len = self.encrypted_payload_len(msg.payload.len());
        let nonce = Nonce::new(&self.iv, seq);
        let aad = make_tls13_aad(total_len);

        let mut plain = msg.payload.to_vec();
        plain.push(msg.typ.to_array()[0]);
        let sealed = self
            .ccm
            .seal(&nonce.0, &aad, &plain)
            .map_err(|_| Error::EncryptError)?;

        let mut payload = PrefixedPayload::with_capacity(total_len);
        payload.extend_from_slice(&sealed);
        Ok(rustls::crypto::cipher::OutboundOpaqueMessage::new(
            ContentType::ApplicationData,
            ProtocolVersion::TLSv1_2,
            payload,
        ))
    }

    fn encrypted_payload_len(&self, payload_len: usize) -> usize {
        payload_len + 1 + TAG_LEN
    }
}

impl MessageDecrypter for CcmDecrypter {
    fn decrypt<'a>(
        &mut self,
        mut msg: InboundOpaqueMessage<'a>,
        seq: u64,
    ) -> Result<InboundPlainMessage<'a>, Error> {
        let payload = &mut msg.payload;
        if payload.len() < TAG_LEN + 1 {
            return Err(Error::DecryptError);
        }
        let nonce = Nonce::new(&self.iv, seq);
        let aad = make_tls13_aad(payload.len());
        let plain = self
            .ccm
            .open(&nonce.0, &aad, payload)
            .map_err(|_| Error::DecryptError)?;
        let plain_len = plain.len();
        payload[..plain_len].copy_from_slice(&plain);
        payload.truncate(plain_len);
        msg.into_tls13_unpadded_message()
    }
}

impl Tls13AeadAlgorithm for Ccm128Aead {
    fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
        Box::new(CcmEncrypter {
            ccm: Aes128CcmTls::new(&key_bytes::<16>(&key)),
            iv,
        })
    }

    fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
        Box::new(CcmDecrypter {
            ccm: Aes128CcmTls::new(&key_bytes::<16>(&key)),
            iv,
        })
    }

    fn key_len(&self) -> usize {
        16
    }

    fn extract_keys(
        &self,
        _key: AeadKey,
        _iv: Iv,
    ) -> Result<ConnectionTrafficSecrets, UnsupportedOperationError> {
        // ConnectionTrafficSecrets 无 CCM 变体:导出 traffic secrets(key
        // exporter / key log)对 CCM 套件不可用——与 AGENTS.md §4 记载一致
        Err(UnsupportedOperationError)
    }
}

// ---------------------------------------------------------------------------
// 套件静态表与装配
// ---------------------------------------------------------------------------

static GCM128_AEAD: Gcm128Aead = Gcm128Aead;
static GCM256_AEAD: Gcm256Aead = Gcm256Aead;
static CHACHA_AEAD: Chacha20Poly1305Aead = Chacha20Poly1305Aead;
static CCM128_AEAD: Ccm128Aead = Ccm128Aead;

static TLS13_AES_128_GCM_SHA256: Tls13CipherSuite = Tls13CipherSuite {
    common: CipherSuiteCommon {
        suite: rustls::CipherSuite::TLS13_AES_128_GCM_SHA256,
        hash_provider: SHA256_HASH,
        // draft-irtf-aead-limits-08 §5.1.1(与 rustls ring provider 一致)
        confidentiality_limit: 1 << 24,
    },
    hkdf_provider: HKDF_SHA256_PROVIDER,
    aead_alg: &GCM128_AEAD,
    quic: None,
};

static TLS13_AES_256_GCM_SHA384: Tls13CipherSuite = Tls13CipherSuite {
    common: CipherSuiteCommon {
        suite: rustls::CipherSuite::TLS13_AES_256_GCM_SHA384,
        hash_provider: SHA384_HASH,
        confidentiality_limit: 1 << 24,
    },
    hkdf_provider: HKDF_SHA384_PROVIDER,
    aead_alg: &GCM256_AEAD,
    quic: None,
};

static TLS13_CHACHA20_POLY1305_SHA256: Tls13CipherSuite = Tls13CipherSuite {
    common: CipherSuiteCommon {
        suite: rustls::CipherSuite::TLS13_CHACHA20_POLY1305_SHA256,
        hash_provider: SHA256_HASH,
        // draft-irtf-aead-limits-08 §5.2.1
        confidentiality_limit: u64::MAX,
    },
    hkdf_provider: HKDF_SHA256_PROVIDER,
    aead_alg: &CHACHA_AEAD,
    quic: None,
};

static TLS13_AES_128_CCM_SHA256: Tls13CipherSuite = Tls13CipherSuite {
    common: CipherSuiteCommon {
        suite: rustls::CipherSuite::TLS13_AES_128_CCM_SHA256,
        hash_provider: SHA256_HASH,
        confidentiality_limit: 1 << 23,
    },
    hkdf_provider: HKDF_SHA256_PROVIDER,
    aead_alg: &CCM128_AEAD,
    quic: None,
};

/// `TLS_AES_128_GCM_SHA256`(批准)。
pub fn tls13_aes_128_gcm_sha256() -> SupportedCipherSuite {
    SupportedCipherSuite::Tls13(&TLS13_AES_128_GCM_SHA256)
}

/// `TLS_AES_256_GCM_SHA384`(批准)。
pub fn tls13_aes_256_gcm_sha384() -> SupportedCipherSuite {
    SupportedCipherSuite::Tls13(&TLS13_AES_256_GCM_SHA384)
}

/// `TLS_CHACHA20_POLY1305_SHA256`(非批准,仅默认模式)。
pub fn tls13_chacha20_poly1305_sha256() -> SupportedCipherSuite {
    SupportedCipherSuite::Tls13(&TLS13_CHACHA20_POLY1305_SHA256)
}

/// `TLS_AES_128_CCM_SHA256`(批准,SP 800-52r2 面向受限环境)。
pub fn tls13_aes_128_ccm_sha256() -> SupportedCipherSuite {
    SupportedCipherSuite::Tls13(&TLS13_AES_128_CCM_SHA256)
}

/// 默认模式全部套件(偏好序)。
pub fn all_tls13_suites() -> Vec<SupportedCipherSuite> {
    vec![
        tls13_aes_128_gcm_sha256(),
        tls13_aes_256_gcm_sha384(),
        tls13_chacha20_poly1305_sha256(),
        tls13_aes_128_ccm_sha256(),
    ]
}

/// 批准模式套件(无 ChaCha20-Poly1305)。
pub fn fips_tls13_suites() -> Vec<SupportedCipherSuite> {
    vec![
        tls13_aes_128_gcm_sha256(),
        tls13_aes_256_gcm_sha384(),
        tls13_aes_128_ccm_sha256(),
    ]
}