From b5ffd28b84f7fcb0e9114dd0d02f7e70c029c3f2 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Sat, 17 Apr 2021 22:31:56 +0800 Subject: [PATCH] support sm4-gcm and sm4-ccm --- Cargo.toml | 2 +- src/v1/aeadcipher/mod.rs | 12 ++++++++++-- src/v1/cipher.rs | 4 ++++ src/v1/kind.rs | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bfd24ed..836550b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadowsocks-crypto" -version = "0.2.2" +version = "0.2.3" authors = ["luozijun "] edition = "2018" license = "MIT" diff --git a/src/v1/aeadcipher/mod.rs b/src/v1/aeadcipher/mod.rs index 7375fae..9e05086 100644 --- a/src/v1/aeadcipher/mod.rs +++ b/src/v1/aeadcipher/mod.rs @@ -1,6 +1,6 @@ pub use crypto2::aeadcipher::{ Aes128Ccm, Aes128GcmSiv, Aes128OcbTag128, Aes192OcbTag128, Aes256Ccm, Aes256GcmSiv, - Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, + Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, Sm4Ccm, Sm4Gcm, }; #[cfg(not(all( any( @@ -157,6 +157,11 @@ impl_siv_cmac_cipher!(AesSivCmac512, AES_SIV_CMAC_512); #[cfg(feature = "v1-aead-extra")] impl_aead_cipher!(XChacha20Poly1305, XCHACHA20_POLY1305); +#[cfg(feature = "v1-aead-extra")] +impl_aead_cipher!(Sm4Gcm, SM4_GCM); +#[cfg(feature = "v1-aead-extra")] +impl_aead_cipher!(Sm4Ccm, SM4_CCM); + macro_rules! aead_cipher_variant { ($($(#[cfg($i_meta:meta)])? $name:ident @ $kind:ident,)+) => { enum AeadCipherInner { @@ -273,6 +278,9 @@ aead_cipher_variant! { Chacha20Poly1305 @ CHACHA20_POLY1305, #[cfg(feature = "v1-aead-extra")] XChacha20Poly1305 @ XCHACHA20_POLY1305, + + #[cfg(feature = "v1-aead-extra")] Sm4Gcm @ SM4_GCM, + #[cfg(feature = "v1-aead-extra")] Sm4Ccm @ SM4_CCM, } pub struct AeadCipher { @@ -283,7 +291,7 @@ pub struct AeadCipher { impl AeadCipher { const N_MAX: usize = 24; - + pub fn new(kind: CipherKind, key: &[u8]) -> Self { let cipher = AeadCipherInner::new(kind, key); let nlen = std::cmp::min(cipher.ac_n_max(), Self::N_MAX); diff --git a/src/v1/cipher.rs b/src/v1/cipher.rs index bede4c6..f889701 100644 --- a/src/v1/cipher.rs +++ b/src/v1/cipher.rs @@ -125,6 +125,10 @@ pub const fn available_ciphers() -> &'static [&'static str] { "aes-siv-cmac-512", #[cfg(feature = "v1-aead-extra")] "xchacha20-ietf-poly1305", + #[cfg(feature = "v1-aead-extra")] + "sm4-gcm", + #[cfg(feature = "v1-aead-extra")] + "sm4-ccm", ] } diff --git a/src/v1/kind.rs b/src/v1/kind.rs index 271a1d2..37a53c2 100644 --- a/src/v1/kind.rs +++ b/src/v1/kind.rs @@ -3,7 +3,8 @@ #[cfg(feature = "v1-aead-extra")] use super::aeadcipher::{ Aes128Ccm, Aes128GcmSiv, Aes128OcbTag128, Aes192OcbTag128, Aes256Ccm, Aes256GcmSiv, - Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, XChacha20Poly1305, + Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512, Sm4Ccm, Sm4Gcm, + XChacha20Poly1305, }; #[cfg(feature = "v1-aead")] use super::aeadcipher::{Aes128Gcm, Aes256Gcm, Chacha20Poly1305}; @@ -242,6 +243,15 @@ pub enum CipherKind { #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))] /// AEAD_XCHACHA20_POLY1305 XCHACHA20_POLY1305, + + #[cfg(feature = "v1-aead-extra")] + #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))] + /// AEAD_SM4_GCM + SM4_GCM, + #[cfg(feature = "v1-aead-extra")] + #[cfg_attr(docrs, doc(cfg(feature = "v1-aead-extra")))] + /// AEAD_SM4_CCM + SM4_CCM, } impl CipherKind { @@ -303,7 +313,9 @@ impl CipherKind { | AES_SIV_CMAC_512 | AES_128_GCM_SIV | AES_256_GCM_SIV - | XCHACHA20_POLY1305 => true, + | XCHACHA20_POLY1305 + | SM4_GCM + | SM4_CCM => true, _ => false, } @@ -431,6 +443,11 @@ impl CipherKind { #[cfg(feature = "v1-aead-extra")] XCHACHA20_POLY1305 => XChacha20Poly1305::KEY_LEN, + + #[cfg(feature = "v1-aead-extra")] + SM4_GCM => Sm4Gcm::KEY_LEN, + #[cfg(feature = "v1-aead-extra")] + SM4_CCM => Sm4Ccm::KEY_LEN, } } @@ -525,6 +542,11 @@ impl CipherKind { #[cfg(feature = "v1-aead-extra")] XCHACHA20_POLY1305 => XChacha20Poly1305::TAG_LEN, + #[cfg(feature = "v1-aead-extra")] + SM4_GCM => Sm4Gcm::TAG_LEN, + #[cfg(feature = "v1-aead-extra")] + SM4_CCM => Sm4Ccm::TAG_LEN, + _ => panic!("only support AEAD ciphers"), } } @@ -659,6 +681,11 @@ impl core::fmt::Display for CipherKind { #[cfg(feature = "v1-aead-extra")] CipherKind::XCHACHA20_POLY1305 => "xchacha20-ietf-poly1305", + + #[cfg(feature = "v1-aead-extra")] + CipherKind::SM4_GCM => "sm4-gcm", + #[cfg(feature = "v1-aead-extra")] + CipherKind::SM4_CCM => "sm4-ccm", }) } } @@ -803,6 +830,11 @@ impl core::str::FromStr for CipherKind { #[cfg(feature = "v1-aead-extra")] "xchacha20-ietf-poly1305" => Ok(XCHACHA20_POLY1305), + #[cfg(feature = "v1-aead-extra")] + "sm4-gcm" => Ok(SM4_GCM), + #[cfg(feature = "v1-aead-extra")] + "sm4-ccm" => Ok(SM4_CCM), + _ => Err(ParseCipherKindError), } }