diff --git a/Cargo.toml b/Cargo.toml index 6297af4..7096d77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sm4-gcm" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["Hatter Jiang"] repository = "https://git.hatter.ink/hatter/sm4-gcm" diff --git a/src/decryptor.rs b/src/decryptor.rs index 3e051bf..3c99890 100644 --- a/src/decryptor.rs +++ b/src/decryptor.rs @@ -13,7 +13,7 @@ pub fn sm4_gcm_decrypt(key: &Sm4Key, nonce: &[u8], ciphertext: &[u8]) -> Result< pub fn sm4_gcm_aad_decrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Result, String> { let mut decryptor = Sm4GcmStreamDecryptor::new(key, nonce); - if aad.len() > 0 { + if !aad.is_empty() { decryptor.init_adata(aad); } let mut msg1 = decryptor.update(ciphertext); @@ -59,7 +59,7 @@ impl Sm4GcmStreamDecryptor { } pub fn init_adata(&mut self, adata: &[u8]) { - if adata.len() > 0 { + if !adata.is_empty() { self.adata_len += adata.len(); self.ghash.update_padded(adata); } @@ -134,12 +134,12 @@ impl Sm4GcmStreamDecryptor { } fn calculate_tag(&mut self) -> Vec { - let mut bs = self.init_nonce.to_be_bytes().clone(); + let mut bs = self.init_nonce.to_be_bytes(); let block = Block::::from_mut_slice(&mut bs); self.cipher.encrypt_block(block); let ghash = self.ghash.clone().finalize(); let tag_trunk = ghash.as_slice(); - let y = u8to128(&tag_trunk) ^ u8to128(&block.as_slice()); + let y = u8to128(tag_trunk) ^ u8to128(block.as_slice()); y.to_be_bytes().to_vec() } @@ -147,7 +147,7 @@ impl Sm4GcmStreamDecryptor { let mut block = [0u8; BLOCK_SIZE]; let block = Block::::from_mut_slice(&mut block); self.cipher.encrypt_block(block); - u8to128(&block.as_slice()) + u8to128(block.as_slice()) } fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) { diff --git a/src/encryptor.rs b/src/encryptor.rs index 6e47b1d..11a9fe9 100644 --- a/src/encryptor.rs +++ b/src/encryptor.rs @@ -13,7 +13,7 @@ pub fn sm4_gcm_encrypt(key: &Sm4Key, nonce: &[u8], message: &[u8]) -> Vec { pub fn sm4_gcm_aad_encrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], message: &[u8]) -> Vec { let mut encryptor = Sm4GcmStreamEncryptor::new(key, nonce); - if aad.len() > 0 { + if !aad.is_empty() { encryptor.init_adata(aad); } let mut enc1 = encryptor.update(message); @@ -60,7 +60,7 @@ impl Sm4GcmStreamEncryptor { } pub fn init_adata(&mut self, adata: &[u8]) { - if adata.len() > 0 { + if !adata.is_empty() { self.adata_len += adata.len(); self.ghash.update_padded(adata); } @@ -126,12 +126,12 @@ impl Sm4GcmStreamEncryptor { } fn compute_tag(&mut self) -> Vec { - let mut bs = self.init_nonce.to_be_bytes().clone(); + let mut bs = self.init_nonce.to_be_bytes(); let block = Block::::from_mut_slice(&mut bs); self.cipher.encrypt_block(block); let ghash = self.ghash.clone().finalize(); let tag_trunk = ghash.as_slice(); - let y = u8to128(&tag_trunk) ^ u8to128(&block.as_slice()); + let y = u8to128(tag_trunk) ^ u8to128(block.as_slice()); y.to_be_bytes().to_vec() } @@ -139,7 +139,7 @@ impl Sm4GcmStreamEncryptor { let mut block = [0u8; BLOCK_SIZE]; let block = Block::::from_mut_slice(&mut block); self.cipher.encrypt_block(block); - u8to128(&block.as_slice()) + u8to128(block.as_slice()) } fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {