-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support (non-standard) sm4-gcm, sm4-ccm in v1-aead-extra
- https://datatracker.ietf.org/doc/html/rfc8998 - shadowsocks/shadowsocks-libev#2424 If anyone interested in trying these non-standard ciphers, please take some time to get some background knowledge about the SM Cipher Suites.
- Loading branch information
Showing
6 changed files
with
412 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "shadowsocks-crypto" | ||
version = "0.5.2" | ||
version = "0.5.3" | ||
authors = ["luozijun <[email protected]>", "ty <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
|
@@ -12,14 +12,20 @@ rust-version = "1.61" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
default = [ | ||
"v1", | ||
"v1-aead", | ||
] | ||
default = ["v1", "v1-aead"] | ||
v1 = [] | ||
v1-stream = ["v1", "chacha20", "aes", "ctr", "camellia"] | ||
v1-aead = ["v1", "aes-gcm", "chacha20poly1305", "hkdf", "sha1"] | ||
v1-aead-extra = ["v1-aead", "aes-gcm-siv", "ccm", "aes"] | ||
v1-aead-extra = [ | ||
"v1-aead", | ||
"aes-gcm-siv", | ||
"ccm", | ||
"aes", | ||
"sm4", | ||
"ghash", | ||
"aead", | ||
"subtle", | ||
] | ||
v2 = ["aes", "aes-gcm", "blake3", "chacha20poly1305", "bytes"] | ||
v2-extra = ["v2", "chacha20poly1305/reduced-round"] | ||
|
||
|
@@ -42,6 +48,10 @@ aes = { version = "0.8", optional = true } | |
ctr = { version = "0.9", optional = true } | ||
bytes = { version = "1.3", optional = true } | ||
camellia = { version = "0.1", optional = true } | ||
sm4 = { version = "0.5", optional = true } | ||
ghash = { version = "0.5", optional = true } | ||
aead = { version = "0.5", optional = true } | ||
subtle = { version = "2.5", optional = true } | ||
|
||
#[target.'cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))'.dependencies] | ||
#md-5 = { version = "0.10", features = ["asm"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! SM4_CCM | ||
//! | ||
//! https://datatracker.ietf.org/doc/html/rfc8998 | ||
|
||
use ccm::{ | ||
aead::{generic_array::typenum::Unsigned, AeadCore, AeadInPlace, KeyInit, KeySizeUser}, | ||
consts::{U12, U16}, | ||
Ccm, | ||
Nonce, | ||
Tag, | ||
}; | ||
use sm4::Sm4; | ||
|
||
pub struct Sm4Ccm(Ccm<Sm4, U16, U12>); | ||
|
||
impl Sm4Ccm { | ||
pub fn new(key: &[u8]) -> Sm4Ccm { | ||
Sm4Ccm(Ccm::new_from_slice(key).expect("Sm4Ccm")) | ||
} | ||
|
||
pub fn key_size() -> usize { | ||
<Ccm<Sm4, U16, U12> as KeySizeUser>::KeySize::to_usize() | ||
} | ||
|
||
pub fn nonce_size() -> usize { | ||
<Ccm<Sm4, U16, U12> as AeadCore>::NonceSize::to_usize() | ||
} | ||
|
||
pub fn tag_size() -> usize { | ||
<Ccm<Sm4, U16, U12> as AeadCore>::TagSize::to_usize() | ||
} | ||
|
||
pub fn encrypt(&self, nonce: &[u8], plaintext_in_ciphertext_out: &mut [u8]) { | ||
let nonce = Nonce::from_slice(nonce); | ||
let (plaintext, out_tag) = | ||
plaintext_in_ciphertext_out.split_at_mut(plaintext_in_ciphertext_out.len() - Self::tag_size()); | ||
let tag = self | ||
.0 | ||
.encrypt_in_place_detached(nonce, &[], plaintext) | ||
.expect("AES_128_CCM encrypt"); | ||
out_tag.copy_from_slice(tag.as_slice()) | ||
} | ||
|
||
pub fn decrypt(&self, nonce: &[u8], ciphertext_in_plaintext_out: &mut [u8]) -> bool { | ||
let nonce = Nonce::from_slice(nonce); | ||
let (ciphertext, in_tag) = | ||
ciphertext_in_plaintext_out.split_at_mut(ciphertext_in_plaintext_out.len() - Self::tag_size()); | ||
let in_tag = Tag::from_slice(in_tag); | ||
self.0.decrypt_in_place_detached(nonce, &[], ciphertext, in_tag).is_ok() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use ccm::{ | ||
aead::{Aead, KeyInit, Payload}, | ||
consts::{U12, U16}, | ||
Ccm, | ||
}; | ||
use sm4::Sm4; | ||
|
||
#[test] | ||
fn test_sm4_ccm() { | ||
let iv = hex::decode("00001234567800000000ABCD").unwrap(); | ||
let key = hex::decode("0123456789ABCDEFFEDCBA9876543210").unwrap(); | ||
let plain_text = hex::decode("AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDEEEEEEEEEEEEEEEEFFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAA").unwrap(); | ||
let aad = hex::decode("FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2").unwrap(); | ||
let mut cipher_text = hex::decode("48AF93501FA62ADBCD414CCE6034D895DDA1BF8F132F042098661572E7483094FD12E518CE062C98ACEE28D95DF4416BED31A2F04476C18BB40C84A74B97DC5B").unwrap(); | ||
let mut tag = hex::decode("16842D4FA186F56AB33256971FA110F4").unwrap(); | ||
cipher_text.append(&mut tag); // postfix tag | ||
|
||
let nonce = super::Nonce::from_slice(&iv); | ||
|
||
let cipher = Ccm::<Sm4, U16, U12>::new_from_slice(&key).unwrap(); | ||
let plain_text_payload = Payload { | ||
msg: &plain_text, | ||
aad: &aad, | ||
}; | ||
let result = cipher.encrypt(nonce, plain_text_payload).unwrap(); | ||
|
||
assert_eq!(result, cipher_text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! SM4-GCM | ||
|
||
use aead::{AeadCore, AeadInPlace, Key, KeyInit, KeySizeUser}; | ||
use sm4::cipher::Unsigned; | ||
|
||
use super::sm4_gcm_cipher::{Nonce, Sm4Gcm as CryptoSm4Gcm, Tag}; | ||
|
||
pub struct Sm4Gcm(CryptoSm4Gcm); | ||
|
||
impl Sm4Gcm { | ||
pub fn new(key: &[u8]) -> Sm4Gcm { | ||
let key = Key::<CryptoSm4Gcm>::from_slice(key); | ||
Sm4Gcm(CryptoSm4Gcm::new(key)) | ||
} | ||
|
||
pub fn key_size() -> usize { | ||
<CryptoSm4Gcm as KeySizeUser>::KeySize::to_usize() | ||
} | ||
|
||
pub fn nonce_size() -> usize { | ||
<CryptoSm4Gcm as AeadCore>::NonceSize::to_usize() | ||
} | ||
|
||
pub fn tag_size() -> usize { | ||
<CryptoSm4Gcm as AeadCore>::TagSize::to_usize() | ||
} | ||
|
||
pub fn encrypt(&self, nonce: &[u8], plaintext_in_ciphertext_out: &mut [u8]) { | ||
let nonce = Nonce::from_slice(nonce); | ||
let (plaintext, out_tag) = | ||
plaintext_in_ciphertext_out.split_at_mut(plaintext_in_ciphertext_out.len() - Self::tag_size()); | ||
let tag = self | ||
.0 | ||
.encrypt_in_place_detached(nonce, &[], plaintext) | ||
.expect("SM4_GCM encrypt"); | ||
out_tag.copy_from_slice(tag.as_slice()) | ||
} | ||
|
||
pub fn decrypt(&self, nonce: &[u8], ciphertext_in_plaintext_out: &mut [u8]) -> bool { | ||
let nonce = Nonce::from_slice(nonce); | ||
let (ciphertext, in_tag) = | ||
ciphertext_in_plaintext_out.split_at_mut(ciphertext_in_plaintext_out.len() - Self::tag_size()); | ||
let in_tag = Tag::from_slice(in_tag); | ||
self.0.decrypt_in_place_detached(nonce, &[], ciphertext, in_tag).is_ok() | ||
} | ||
} |
Oops, something went wrong.