Skip to content

Commit

Permalink
Merge pull request #21 from citahub/cbc_padding
Browse files Browse the repository at this point in the history
add cbc pkcs7 padding & skip clippy result_unit_err check
  • Loading branch information
Pencil-Yao authored Jan 7, 2021
2 parents e0610a7 + d0f3563 commit 6656219
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/sm2/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ impl EccCtx {
ret
}

#[allow(clippy::result_unit_err)]
pub fn bytes_to_point(&self, b: &[u8]) -> Result<Point, ()> {
let ctx = &self.fctx;

Expand Down
1 change: 1 addition & 0 deletions src/sm2/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl FieldCtx {
}

// Square root of a field element
#[allow(clippy::result_unit_err)]
pub fn sqrt(&self, g: &FieldElem) -> Result<FieldElem, ()> {
// p = 4 * u + 3
// u = u + 1
Expand Down
3 changes: 3 additions & 0 deletions src/sm2/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Signature {
Ok(Signature { r, s })
}

#[allow(clippy::result_unit_err)]
pub fn der_decode_raw(buf: &[u8]) -> Result<Signature, ()> {
if buf[0] != 0x02 {
return Err(());
Expand Down Expand Up @@ -303,6 +304,7 @@ impl SigCtx {
curve.mul(&sk, &curve.generator())
}

#[allow(clippy::result_unit_err)]
pub fn load_pubkey(&self, buf: &[u8]) -> Result<Point, ()> {
self.curve.bytes_to_point(buf)
}
Expand All @@ -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<BigUint, ()> {
if buf.len() != 32 {
return Err(());
Expand Down
28 changes: 16 additions & 12 deletions src/sm4/cipher_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<u8> {
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<u8> = Vec::new();
let mut vec_buf = [0; 16];
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6656219

Please sign in to comment.