feat: fix clippy
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "sm4-gcm"
|
name = "sm4-gcm"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Hatter Jiang"]
|
authors = ["Hatter Jiang"]
|
||||||
repository = "https://git.hatter.ink/hatter/sm4-gcm"
|
repository = "https://git.hatter.ink/hatter/sm4-gcm"
|
||||||
|
|||||||
@@ -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<Vec<u8>, String> {
|
pub fn sm4_gcm_aad_decrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, String> {
|
||||||
let mut decryptor = Sm4GcmStreamDecryptor::new(key, nonce);
|
let mut decryptor = Sm4GcmStreamDecryptor::new(key, nonce);
|
||||||
if aad.len() > 0 {
|
if !aad.is_empty() {
|
||||||
decryptor.init_adata(aad);
|
decryptor.init_adata(aad);
|
||||||
}
|
}
|
||||||
let mut msg1 = decryptor.update(ciphertext);
|
let mut msg1 = decryptor.update(ciphertext);
|
||||||
@@ -59,7 +59,7 @@ impl Sm4GcmStreamDecryptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_adata(&mut self, adata: &[u8]) {
|
pub fn init_adata(&mut self, adata: &[u8]) {
|
||||||
if adata.len() > 0 {
|
if !adata.is_empty() {
|
||||||
self.adata_len += adata.len();
|
self.adata_len += adata.len();
|
||||||
self.ghash.update_padded(adata);
|
self.ghash.update_padded(adata);
|
||||||
}
|
}
|
||||||
@@ -134,12 +134,12 @@ impl Sm4GcmStreamDecryptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_tag(&mut self) -> Vec<u8> {
|
fn calculate_tag(&mut self) -> Vec<u8> {
|
||||||
let mut bs = self.init_nonce.to_be_bytes().clone();
|
let mut bs = self.init_nonce.to_be_bytes();
|
||||||
let block = Block::<Sm4Block>::from_mut_slice(&mut bs);
|
let block = Block::<Sm4Block>::from_mut_slice(&mut bs);
|
||||||
self.cipher.encrypt_block(block);
|
self.cipher.encrypt_block(block);
|
||||||
let ghash = self.ghash.clone().finalize();
|
let ghash = self.ghash.clone().finalize();
|
||||||
let tag_trunk = ghash.as_slice();
|
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()
|
y.to_be_bytes().to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ impl Sm4GcmStreamDecryptor {
|
|||||||
let mut block = [0u8; BLOCK_SIZE];
|
let mut block = [0u8; BLOCK_SIZE];
|
||||||
let block = Block::<Sm4Block>::from_mut_slice(&mut block);
|
let block = Block::<Sm4Block>::from_mut_slice(&mut block);
|
||||||
self.cipher.encrypt_block(block);
|
self.cipher.encrypt_block(block);
|
||||||
u8to128(&block.as_slice())
|
u8to128(block.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {
|
fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ pub fn sm4_gcm_encrypt(key: &Sm4Key, nonce: &[u8], message: &[u8]) -> Vec<u8> {
|
|||||||
|
|
||||||
pub fn sm4_gcm_aad_encrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], message: &[u8]) -> Vec<u8> {
|
pub fn sm4_gcm_aad_encrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], message: &[u8]) -> Vec<u8> {
|
||||||
let mut encryptor = Sm4GcmStreamEncryptor::new(key, nonce);
|
let mut encryptor = Sm4GcmStreamEncryptor::new(key, nonce);
|
||||||
if aad.len() > 0 {
|
if !aad.is_empty() {
|
||||||
encryptor.init_adata(aad);
|
encryptor.init_adata(aad);
|
||||||
}
|
}
|
||||||
let mut enc1 = encryptor.update(message);
|
let mut enc1 = encryptor.update(message);
|
||||||
@@ -60,7 +60,7 @@ impl Sm4GcmStreamEncryptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_adata(&mut self, adata: &[u8]) {
|
pub fn init_adata(&mut self, adata: &[u8]) {
|
||||||
if adata.len() > 0 {
|
if !adata.is_empty() {
|
||||||
self.adata_len += adata.len();
|
self.adata_len += adata.len();
|
||||||
self.ghash.update_padded(adata);
|
self.ghash.update_padded(adata);
|
||||||
}
|
}
|
||||||
@@ -126,12 +126,12 @@ impl Sm4GcmStreamEncryptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn compute_tag(&mut self) -> Vec<u8> {
|
fn compute_tag(&mut self) -> Vec<u8> {
|
||||||
let mut bs = self.init_nonce.to_be_bytes().clone();
|
let mut bs = self.init_nonce.to_be_bytes();
|
||||||
let block = Block::<Sm4Block>::from_mut_slice(&mut bs);
|
let block = Block::<Sm4Block>::from_mut_slice(&mut bs);
|
||||||
self.cipher.encrypt_block(block);
|
self.cipher.encrypt_block(block);
|
||||||
let ghash = self.ghash.clone().finalize();
|
let ghash = self.ghash.clone().finalize();
|
||||||
let tag_trunk = ghash.as_slice();
|
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()
|
y.to_be_bytes().to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +139,7 @@ impl Sm4GcmStreamEncryptor {
|
|||||||
let mut block = [0u8; BLOCK_SIZE];
|
let mut block = [0u8; BLOCK_SIZE];
|
||||||
let block = Block::<Sm4Block>::from_mut_slice(&mut block);
|
let block = Block::<Sm4Block>::from_mut_slice(&mut block);
|
||||||
self.cipher.encrypt_block(block);
|
self.cipher.encrypt_block(block);
|
||||||
u8to128(&block.as_slice())
|
u8to128(block.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {
|
fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {
|
||||||
|
|||||||
Reference in New Issue
Block a user