Skip to content

Commit

Permalink
support sm4-gcm and sm4-ccm
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Apr 17, 2021
1 parent 31afeeb commit b5ffd28
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadowsocks-crypto"
version = "0.2.2"
version = "0.2.3"
authors = ["luozijun <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
12 changes: 10 additions & 2 deletions src/v1/aeadcipher/mod.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/v1/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}

Expand Down
36 changes: 34 additions & 2 deletions src/v1/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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"),
}
}
Expand Down Expand Up @@ -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",
})
}
}
Expand Down Expand Up @@ -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),
}
}
Expand Down

0 comments on commit b5ffd28

Please sign in to comment.