Skip to content

Commit

Permalink
fix substraction overflow (RustCrypto#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k authored and newpavlov committed Oct 3, 2018
1 parent c68c528 commit ca12c3a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions block-padding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ impl Padding for ZeroPadding {
/// assert_eq!(padded_msg, b"test\x02\x02");
/// assert_eq!(Pkcs7::unpad(&padded_msg).unwrap(), msg);
/// ```
/// ```
/// # use block_padding::{Pkcs7, Padding};
/// # let buffer = [0xff; 16];
/// assert!(Pkcs7::unpad(&buffer).is_err());
/// ```
///
/// In addition to conditions stated in the `Padding` trait documentation,
/// `pad_block` will return `PadError` if `block.len() > 255`, and in case of
Expand All @@ -156,7 +161,7 @@ impl Padding for Pkcs7 {
if data.is_empty() { Err(UnpadError)? }
let l = data.len();
let n = data[l-1];
if n == 0 { Err(UnpadError)? }
if n == 0 || n as usize > l { Err(UnpadError)? }
for v in &data[l-n as usize..l-1] {
if *v != n { Err(UnpadError)? }
}
Expand Down Expand Up @@ -188,6 +193,11 @@ impl Padding for Pkcs7 {
/// assert_eq!(padded_msg, b"test\x00\x02");
/// assert_eq!(AnsiX923::unpad(&padded_msg).unwrap(), msg);
/// ```
/// ```
/// # use block_padding::{AnsiX923, Padding};
/// # let buffer = [0xff; 16];
/// assert!(AnsiX923::unpad(&buffer).is_err());
/// ```
///
/// In addition to conditions stated in the `Padding` trait documentation,
/// `pad_block` will return `PadError` if `block.len() > 255`, and in case of
Expand All @@ -208,7 +218,7 @@ impl Padding for AnsiX923 {
if data.is_empty() { Err(UnpadError)? }
let l = data.len();
let n = data[l-1] as usize;
if n == 0 {
if n == 0 || n > l {
return Err(UnpadError)
}
for v in &data[l-n..l-1] {
Expand Down

0 comments on commit ca12c3a

Please sign in to comment.