Skip to content

Commit b5ffd28

Browse files
committed
support sm4-gcm and sm4-ccm
1 parent 31afeeb commit b5ffd28

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-crypto"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
authors = ["luozijun <[email protected]>"]
55
edition = "2018"
66
license = "MIT"

Diff for: src/v1/aeadcipher/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use crypto2::aeadcipher::{
22
Aes128Ccm, Aes128GcmSiv, Aes128OcbTag128, Aes192OcbTag128, Aes256Ccm, Aes256GcmSiv,
3-
Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512,
3+
Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, Sm4Ccm, Sm4Gcm,
44
};
55
#[cfg(not(all(
66
any(
@@ -157,6 +157,11 @@ impl_siv_cmac_cipher!(AesSivCmac512, AES_SIV_CMAC_512);
157157
#[cfg(feature = "v1-aead-extra")]
158158
impl_aead_cipher!(XChacha20Poly1305, XCHACHA20_POLY1305);
159159

160+
#[cfg(feature = "v1-aead-extra")]
161+
impl_aead_cipher!(Sm4Gcm, SM4_GCM);
162+
#[cfg(feature = "v1-aead-extra")]
163+
impl_aead_cipher!(Sm4Ccm, SM4_CCM);
164+
160165
macro_rules! aead_cipher_variant {
161166
($($(#[cfg($i_meta:meta)])? $name:ident @ $kind:ident,)+) => {
162167
enum AeadCipherInner {
@@ -273,6 +278,9 @@ aead_cipher_variant! {
273278
Chacha20Poly1305 @ CHACHA20_POLY1305,
274279

275280
#[cfg(feature = "v1-aead-extra")] XChacha20Poly1305 @ XCHACHA20_POLY1305,
281+
282+
#[cfg(feature = "v1-aead-extra")] Sm4Gcm @ SM4_GCM,
283+
#[cfg(feature = "v1-aead-extra")] Sm4Ccm @ SM4_CCM,
276284
}
277285

278286
pub struct AeadCipher {
@@ -283,7 +291,7 @@ pub struct AeadCipher {
283291

284292
impl AeadCipher {
285293
const N_MAX: usize = 24;
286-
294+
287295
pub fn new(kind: CipherKind, key: &[u8]) -> Self {
288296
let cipher = AeadCipherInner::new(kind, key);
289297
let nlen = std::cmp::min(cipher.ac_n_max(), Self::N_MAX);

Diff for: src/v1/cipher.rs

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ pub const fn available_ciphers() -> &'static [&'static str] {
125125
"aes-siv-cmac-512",
126126
#[cfg(feature = "v1-aead-extra")]
127127
"xchacha20-ietf-poly1305",
128+
#[cfg(feature = "v1-aead-extra")]
129+
"sm4-gcm",
130+
#[cfg(feature = "v1-aead-extra")]
131+
"sm4-ccm",
128132
]
129133
}
130134

Diff for: src/v1/kind.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#[cfg(feature = "v1-aead-extra")]
44
use super::aeadcipher::{
55
Aes128Ccm, Aes128GcmSiv, Aes128OcbTag128, Aes192OcbTag128, Aes256Ccm, Aes256GcmSiv,
6-
Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, XChacha20Poly1305,
6+
Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, Sm4Ccm, Sm4Gcm,
7+
XChacha20Poly1305,
78
};
89
#[cfg(feature = "v1-aead")]
910
use super::aeadcipher::{Aes128Gcm, Aes256Gcm, Chacha20Poly1305};
@@ -242,6 +243,15 @@ pub enum CipherKind {
242243
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
243244
/// AEAD_XCHACHA20_POLY1305
244245
XCHACHA20_POLY1305,
246+
247+
#[cfg(feature = "v1-aead-extra")]
248+
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
249+
/// AEAD_SM4_GCM
250+
SM4_GCM,
251+
#[cfg(feature = "v1-aead-extra")]
252+
#[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))]
253+
/// AEAD_SM4_CCM
254+
SM4_CCM,
245255
}
246256

247257
impl CipherKind {
@@ -303,7 +313,9 @@ impl CipherKind {
303313
| AES_SIV_CMAC_512
304314
| AES_128_GCM_SIV
305315
| AES_256_GCM_SIV
306-
| XCHACHA20_POLY1305 => true,
316+
| XCHACHA20_POLY1305
317+
| SM4_GCM
318+
| SM4_CCM => true,
307319

308320
_ => false,
309321
}
@@ -431,6 +443,11 @@ impl CipherKind {
431443

432444
#[cfg(feature = "v1-aead-extra")]
433445
XCHACHA20_POLY1305 => XChacha20Poly1305::KEY_LEN,
446+
447+
#[cfg(feature = "v1-aead-extra")]
448+
SM4_GCM => Sm4Gcm::KEY_LEN,
449+
#[cfg(feature = "v1-aead-extra")]
450+
SM4_CCM => Sm4Ccm::KEY_LEN,
434451
}
435452
}
436453

@@ -525,6 +542,11 @@ impl CipherKind {
525542
#[cfg(feature = "v1-aead-extra")]
526543
XCHACHA20_POLY1305 => XChacha20Poly1305::TAG_LEN,
527544

545+
#[cfg(feature = "v1-aead-extra")]
546+
SM4_GCM => Sm4Gcm::TAG_LEN,
547+
#[cfg(feature = "v1-aead-extra")]
548+
SM4_CCM => Sm4Ccm::TAG_LEN,
549+
528550
_ => panic!("only support AEAD ciphers"),
529551
}
530552
}
@@ -659,6 +681,11 @@ impl core::fmt::Display for CipherKind {
659681

660682
#[cfg(feature = "v1-aead-extra")]
661683
CipherKind::XCHACHA20_POLY1305 => "xchacha20-ietf-poly1305",
684+
685+
#[cfg(feature = "v1-aead-extra")]
686+
CipherKind::SM4_GCM => "sm4-gcm",
687+
#[cfg(feature = "v1-aead-extra")]
688+
CipherKind::SM4_CCM => "sm4-ccm",
662689
})
663690
}
664691
}
@@ -803,6 +830,11 @@ impl core::str::FromStr for CipherKind {
803830
#[cfg(feature = "v1-aead-extra")]
804831
"xchacha20-ietf-poly1305" => Ok(XCHACHA20_POLY1305),
805832

833+
#[cfg(feature = "v1-aead-extra")]
834+
"sm4-gcm" => Ok(SM4_GCM),
835+
#[cfg(feature = "v1-aead-extra")]
836+
"sm4-ccm" => Ok(SM4_CCM),
837+
806838
_ => Err(ParseCipherKindError),
807839
}
808840
}

0 commit comments

Comments
 (0)