From ab79b69fddcf6558fe85f269cf55d9beabd917df Mon Sep 17 00:00:00 2001 From: "Y. T. Chung" Date: Fri, 1 Jun 2018 08:58:25 +0800 Subject: [PATCH] Correctify naming of ***IetfPoly1305 --- src/crypto/aead.rs | 12 +++------ src/crypto/cipher.rs | 56 ++++++++++++++++++++++----------------- src/crypto/ring.rs | 4 +-- src/crypto/sodium.rs | 62 +++++++++++++++++++------------------------- 4 files changed, 65 insertions(+), 69 deletions(-) diff --git a/src/crypto/aead.rs b/src/crypto/aead.rs index 1d91fce7..29cdb560 100644 --- a/src/crypto/aead.rs +++ b/src/crypto/aead.rs @@ -37,14 +37,12 @@ pub fn new_aead_encryptor(t: CipherType, key: &[u8], nonce: &[u8]) -> BoxAeadEnc assert!(t.category() == CipherCategory::Aead); match t { - CipherType::Aes128Gcm | CipherType::Aes256Gcm | CipherType::ChaCha20Poly1305 => { + CipherType::Aes128Gcm | CipherType::Aes256Gcm | CipherType::ChaCha20IetfPoly1305 => { Box::new(RingAeadCipher::new(t, key, nonce, true)) } #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => { - Box::new(SodiumAeadCipher::new(t, key, nonce)) - } + CipherType::XChaCha20IetfPoly1305 => Box::new(SodiumAeadCipher::new(t, key, nonce)), #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv | CipherType::Aes256PmacSiv => Box::new(MiscreantCipher::new(t, key, nonce)), @@ -58,14 +56,12 @@ pub fn new_aead_decryptor(t: CipherType, key: &[u8], nonce: &[u8]) -> BoxAeadDec assert!(t.category() == CipherCategory::Aead); match t { - CipherType::Aes128Gcm | CipherType::Aes256Gcm | CipherType::ChaCha20Poly1305 => { + CipherType::Aes128Gcm | CipherType::Aes256Gcm | CipherType::ChaCha20IetfPoly1305 => { Box::new(RingAeadCipher::new(t, key, nonce, false)) } #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => { - Box::new(SodiumAeadCipher::new(t, key, nonce)) - } + CipherType::XChaCha20IetfPoly1305 => Box::new(SodiumAeadCipher::new(t, key, nonce)), #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv | CipherType::Aes256PmacSiv => Box::new(MiscreantCipher::new(t, key, nonce)), diff --git a/src/crypto/cipher.rs b/src/crypto/cipher.rs index 125caa9a..894d0df9 100644 --- a/src/crypto/cipher.rs +++ b/src/crypto/cipher.rs @@ -99,8 +99,8 @@ const CIPHER_PLAIN: &str = "plain"; const CIPHER_AES_128_GCM: &str = "aes-128-gcm"; const CIPHER_AES_256_GCM: &str = "aes-256-gcm"; -const CIPHER_CHACHA20_POLY1305: &str = "chacha20-ietf-poly1305"; -const CIPHER_XCHACHA20_POLY1305: &str = "xchacha20-ietf-poly1305"; +const CIPHER_CHACHA20_IETF_POLY1305: &str = "chacha20-ietf-poly1305"; +const CIPHER_XCHACHA20_IETF_POLY1305: &str = "xchacha20-ietf-poly1305"; /// ShadowSocks cipher type #[derive(Clone, Debug, Copy)] @@ -133,9 +133,9 @@ pub enum CipherType { Aes128Gcm, Aes256Gcm, - ChaCha20Poly1305, + ChaCha20IetfPoly1305, #[cfg(feature = "sodium")] - XChaCha20Poly1305, + XChaCha20IetfPoly1305, #[cfg(feature = "miscreant")] Aes128PmacSiv, @@ -173,10 +173,10 @@ impl CipherType { CipherType::Aes128Gcm => AES_128_GCM.key_len(), CipherType::Aes256Gcm => AES_256_GCM.key_len(), - CipherType::ChaCha20Poly1305 => CHACHA20_POLY1305.key_len(), + CipherType::ChaCha20IetfPoly1305 => CHACHA20_POLY1305.key_len(), #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => 32, + CipherType::XChaCha20IetfPoly1305 => 32, #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv => 32, @@ -226,18 +226,26 @@ impl CipherType { match *self { CipherType::Table | CipherType::Plain => 0, - CipherType::Aes128Cfb1 => symm::Cipher::aes_128_cfb1().iv_len() - .expect("iv_len should not be None"), - CipherType::Aes128Cfb8 => symm::Cipher::aes_128_cfb8().iv_len() - .expect("iv_len should not be None"), + CipherType::Aes128Cfb1 => { + symm::Cipher::aes_128_cfb1().iv_len() + .expect("iv_len should not be None") + } + CipherType::Aes128Cfb8 => { + symm::Cipher::aes_128_cfb8().iv_len() + .expect("iv_len should not be None") + } CipherType::Aes128Cfb | CipherType::Aes128Cfb128 => { symm::Cipher::aes_128_cfb128().iv_len() .expect("iv_len should not be None") } - CipherType::Aes256Cfb1 => symm::Cipher::aes_256_cfb1().iv_len() - .expect("iv_len should not be None"), - CipherType::Aes256Cfb8 => symm::Cipher::aes_256_cfb8().iv_len() - .expect("iv_len should not be None"), + CipherType::Aes256Cfb1 => { + symm::Cipher::aes_256_cfb1().iv_len() + .expect("iv_len should not be None") + } + CipherType::Aes256Cfb8 => { + symm::Cipher::aes_256_cfb8().iv_len() + .expect("iv_len should not be None") + } CipherType::Aes256Cfb | CipherType::Aes256Cfb128 => { symm::Cipher::aes_256_cfb128().iv_len() .expect("iv_len should not be None") @@ -255,9 +263,9 @@ impl CipherType { CipherType::Aes128Gcm => AES_128_GCM.nonce_len(), CipherType::Aes256Gcm => AES_256_GCM.nonce_len(), - CipherType::ChaCha20Poly1305 => CHACHA20_POLY1305.nonce_len(), + CipherType::ChaCha20IetfPoly1305 => CHACHA20_POLY1305.nonce_len(), #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => 24, + CipherType::XChaCha20IetfPoly1305 => 24, #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv => 8, @@ -286,10 +294,10 @@ impl CipherType { /// Get category of cipher pub fn category(&self) -> CipherCategory { match *self { - CipherType::Aes128Gcm | CipherType::Aes256Gcm | CipherType::ChaCha20Poly1305 => CipherCategory::Aead, + CipherType::Aes128Gcm | CipherType::Aes256Gcm | CipherType::ChaCha20IetfPoly1305 => CipherCategory::Aead, #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => CipherCategory::Aead, + CipherType::XChaCha20IetfPoly1305 => CipherCategory::Aead, #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv | CipherType::Aes256PmacSiv => CipherCategory::Aead, @@ -305,9 +313,9 @@ impl CipherType { match *self { CipherType::Aes128Gcm => AES_128_GCM.tag_len(), CipherType::Aes256Gcm => AES_256_GCM.tag_len(), - CipherType::ChaCha20Poly1305 => CHACHA20_POLY1305.tag_len(), + CipherType::ChaCha20IetfPoly1305 => CHACHA20_POLY1305.tag_len(), #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => 16, + CipherType::XChaCha20IetfPoly1305 => 16, #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv | CipherType::Aes256PmacSiv => 16, @@ -359,9 +367,9 @@ impl FromStr for CipherType { CIPHER_AES_128_GCM => Ok(CipherType::Aes128Gcm), CIPHER_AES_256_GCM => Ok(CipherType::Aes256Gcm), - CIPHER_CHACHA20_POLY1305 => Ok(CipherType::ChaCha20Poly1305), + CIPHER_CHACHA20_IETF_POLY1305 => Ok(CipherType::ChaCha20IetfPoly1305), #[cfg(feature = "sodium")] - CIPHER_XCHACHA20_POLY1305 => Ok(CipherType::XChaCha20Poly1305), + CIPHER_XCHACHA20_IETF_POLY1305 => Ok(CipherType::XChaCha20IetfPoly1305), #[cfg(feature = "miscreant")] CIPHER_AES_128_PMAC_SIV => Ok(CipherType::Aes128PmacSiv), @@ -402,9 +410,9 @@ impl Display for CipherType { CipherType::Aes128Gcm => write!(f, "{}", CIPHER_AES_128_GCM), CipherType::Aes256Gcm => write!(f, "{}", CIPHER_AES_256_GCM), - CipherType::ChaCha20Poly1305 => write!(f, "{}", CIPHER_CHACHA20_POLY1305), + CipherType::ChaCha20IetfPoly1305 => write!(f, "{}", CIPHER_CHACHA20_IETF_POLY1305), #[cfg(feature = "sodium")] - CipherType::XChaCha20Poly1305 => write!(f, "{}", CIPHER_XCHACHA20_POLY1305), + CipherType::XChaCha20IetfPoly1305 => write!(f, "{}", CIPHER_XCHACHA20_IETF_POLY1305), #[cfg(feature = "miscreant")] CipherType::Aes128PmacSiv => write!(f, "{}", CIPHER_AES_128_PMAC_SIV), diff --git a/src/crypto/ring.rs b/src/crypto/ring.rs index 648bd8b1..753cda99 100644 --- a/src/crypto/ring.rs +++ b/src/crypto/ring.rs @@ -67,7 +67,7 @@ impl RingAeadCipher { match t { CipherType::Aes128Gcm => seal_or_open!(AES_128_GCM), CipherType::Aes256Gcm => seal_or_open!(AES_256_GCM), - CipherType::ChaCha20Poly1305 => seal_or_open!(CHACHA20_POLY1305), + CipherType::ChaCha20IetfPoly1305 => seal_or_open!(CHACHA20_POLY1305), _ => panic!("unsupported cipher in ring {:?}", t), } } @@ -181,6 +181,6 @@ mod test { #[test] fn test_ring_chacha20poly1305() { - test_ring_aead(CipherType::ChaCha20Poly1305); + test_ring_aead(CipherType::ChaCha20IetfPoly1305); } } diff --git a/src/crypto/sodium.rs b/src/crypto/sodium.rs index e8e785df..e7966100 100644 --- a/src/crypto/sodium.rs +++ b/src/crypto/sodium.rs @@ -84,38 +84,30 @@ fn crypto_stream_xor_ic(t: CipherType, let ret = unsafe { match t { - CipherType::ChaCha20 => { - crypto_stream_chacha20_xor_ic(out.bytes_mut().as_mut_ptr(), - data.as_ptr(), - data.len() as c_ulonglong, - iv.as_ptr(), - ic as c_ulonglong, - key.as_ptr()) - } - CipherType::ChaCha20Ietf => { - crypto_stream_chacha20_ietf_xor_ic(out.bytes_mut().as_mut_ptr(), - data.as_ptr(), - data.len() as c_ulonglong, - iv.as_ptr(), - ic as uint32_t, - key.as_ptr()) - } - CipherType::Salsa20 => { - crypto_stream_salsa20_xor_ic(out.bytes_mut().as_mut_ptr(), - data.as_ptr(), - data.len() as c_ulonglong, - iv.as_ptr(), - ic as c_ulonglong, - key.as_ptr()) - } - CipherType::XSalsa20 => { - crypto_stream_xsalsa20_xor_ic(out.bytes_mut().as_mut_ptr(), - data.as_ptr(), - data.len() as c_ulonglong, - iv.as_ptr(), - ic as c_ulonglong, - key.as_ptr()) - } + CipherType::ChaCha20 => crypto_stream_chacha20_xor_ic(out.bytes_mut().as_mut_ptr(), + data.as_ptr(), + data.len() as c_ulonglong, + iv.as_ptr(), + ic as c_ulonglong, + key.as_ptr()), + CipherType::ChaCha20Ietf => crypto_stream_chacha20_ietf_xor_ic(out.bytes_mut().as_mut_ptr(), + data.as_ptr(), + data.len() as c_ulonglong, + iv.as_ptr(), + ic as uint32_t, + key.as_ptr()), + CipherType::Salsa20 => crypto_stream_salsa20_xor_ic(out.bytes_mut().as_mut_ptr(), + data.as_ptr(), + data.len() as c_ulonglong, + iv.as_ptr(), + ic as c_ulonglong, + key.as_ptr()), + CipherType::XSalsa20 => crypto_stream_xsalsa20_xor_ic(out.bytes_mut().as_mut_ptr(), + data.as_ptr(), + data.len() as c_ulonglong, + iv.as_ptr(), + ic as c_ulonglong, + key.as_ptr()), _ => unreachable!(), } }; @@ -186,7 +178,7 @@ impl AeadEncryptor for SodiumAeadCipher { assert!(output.len() >= buf_len); let ret = match self.cipher_type { - CipherType::XChaCha20Poly1305 => unsafe { + CipherType::XChaCha20IetfPoly1305 => unsafe { let mut len: c_ulonglong = output.len() as c_ulonglong; crypto_aead_xchacha20poly1305_ietf_encrypt(output.as_mut_ptr(), @@ -214,7 +206,7 @@ impl AeadDecryptor for SodiumAeadCipher { assert!(output.len() >= input.len() - tag_len); let ret = match self.cipher_type { - CipherType::XChaCha20Poly1305 => unsafe { + CipherType::XChaCha20IetfPoly1305 => unsafe { let mut len: c_ulonglong = output.len() as c_ulonglong; crypto_aead_xchacha20poly1305_ietf_decrypt(output.as_mut_ptr(), @@ -306,6 +298,6 @@ mod test { #[test] fn test_rust_crypto_cipher_xchacha20_ietf_poly1305() { - test_sodium_aead(CipherType::XChaCha20Poly1305); + test_sodium_aead(CipherType::XChaCha20IetfPoly1305); } }