diff --git a/src/sm2/ecc.rs b/src/sm2/ecc.rs index beb4ad0..952ef82 100644 --- a/src/sm2/ecc.rs +++ b/src/sm2/ecc.rs @@ -485,6 +485,7 @@ impl EccCtx { ret } + #[allow(clippy::result_unit_err)] pub fn bytes_to_point(&self, b: &[u8]) -> Result { let ctx = &self.fctx; diff --git a/src/sm2/field.rs b/src/sm2/field.rs index 879ae35..05e5bea 100644 --- a/src/sm2/field.rs +++ b/src/sm2/field.rs @@ -239,6 +239,7 @@ impl FieldCtx { } // Square root of a field element + #[allow(clippy::result_unit_err)] pub fn sqrt(&self, g: &FieldElem) -> Result { // p = 4 * u + 3 // u = u + 1 diff --git a/src/sm2/signature.rs b/src/sm2/signature.rs index e7d56a9..aa3fbe0 100644 --- a/src/sm2/signature.rs +++ b/src/sm2/signature.rs @@ -48,6 +48,7 @@ impl Signature { Ok(Signature { r, s }) } + #[allow(clippy::result_unit_err)] pub fn der_decode_raw(buf: &[u8]) -> Result { if buf[0] != 0x02 { return Err(()); @@ -303,6 +304,7 @@ impl SigCtx { curve.mul(&sk, &curve.generator()) } + #[allow(clippy::result_unit_err)] pub fn load_pubkey(&self, buf: &[u8]) -> Result { self.curve.bytes_to_point(buf) } @@ -311,6 +313,7 @@ impl SigCtx { self.curve.point_to_bytes(p, compress) } + #[allow(clippy::result_unit_err)] pub fn load_seckey(&self, buf: &[u8]) -> Result { if buf.len() != 32 { return Err(()); diff --git a/src/sm4/cipher_mode.rs b/src/sm4/cipher_mode.rs index 82f2eaa..4fbabc8 100644 --- a/src/sm4/cipher_mode.rs +++ b/src/sm4/cipher_mode.rs @@ -199,9 +199,7 @@ impl SM4CipherMode { let ct = block_xor(&vec_buf, &data[i * 16..i * 16 + 16]); let enc = self.cipher.encrypt(&ct); - for j in enc.iter() { - out.push(*j); - } + out.extend_from_slice(&enc); vec_buf = enc; } @@ -211,18 +209,20 @@ impl SM4CipherMode { let ct = block_xor(&vec_buf, &last_block); let enc = self.cipher.encrypt(&ct); - - for j in enc.iter() { - out.push(*j); - } + out.extend_from_slice(&enc); + } else { + let ff_padding = block_xor(&vec_buf, &[0x10; 16]); + let enc = self.cipher.encrypt(&ff_padding); + out.extend_from_slice(&enc); } out } fn cbc_decrypt(&self, data: &[u8], iv: &[u8]) -> Vec { - let block_num = data.len() / 16; - assert_eq!(data.len() % 16, 0); + let data_len = data.len(); + let block_num = data_len / 16; + assert_eq!(data_len % 16, 0); let mut out: Vec = Vec::new(); let mut vec_buf = [0; 16]; @@ -239,6 +239,10 @@ impl SM4CipherMode { vec_buf.copy_from_slice(&data[i * 16..i * 16 + 16]); } + let last_u8 = out[data_len - 1]; + assert!(last_u8 <= 0x10 && last_u8 != 0); + out.resize(data_len - last_u8 as usize, 0); + out } } @@ -284,17 +288,17 @@ mod tests { let cmode = SM4CipherMode::new(&key, mode); - let pt = rand_data(16); + let pt = rand_data(10); let ct = cmode.encrypt(&pt[..], &iv); let new_pt = cmode.decrypt(&ct[..], &iv); assert_eq!(pt, new_pt); - let pt = rand_data(256); + let pt = rand_data(100); let ct = cmode.encrypt(&pt[..], &iv); let new_pt = cmode.decrypt(&ct[..], &iv); assert_eq!(pt, new_pt); - let pt = rand_data(4096); + let pt = rand_data(1000); let ct = cmode.encrypt(&pt[..], &iv); let new_pt = cmode.decrypt(&ct[..], &iv); assert_eq!(pt, new_pt);