Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible panic in internals::left_pad #262

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub enum Error {

/// Label too long.
LabelTooLong,

/// Invalid padding length.
InvalidPadLen,
}

#[cfg(feature = "std")]
Expand Down Expand Up @@ -87,6 +90,7 @@ impl core::fmt::Display for Error {
Error::Pkcs8(err) => write!(f, "{}", err),
Error::Internal => write!(f, "internal error"),
Error::LabelTooLong => write!(f, "label too long"),
Error::InvalidPadLen => write!(f, "invalid padding length"),
}
}
}
Expand Down
38 changes: 29 additions & 9 deletions src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,34 @@ pub fn unblind(key: &impl PublicKeyParts, m: &BigUint, unblinder: &BigUint) -> B

/// Returns a new vector of the given length, with 0s left padded.
#[inline]
pub fn left_pad(input: &[u8], size: usize) -> Vec<u8> {
let n = if input.len() > size {
size
} else {
input.len()
};
pub fn left_pad(input: &[u8], padded_len: usize) -> Result<Vec<u8>> {
if input.len() > padded_len {
return Err(Error::InvalidPadLen);
}

let mut out = vec![0u8; size];
out[size - n..].copy_from_slice(input);
out
let mut out = vec![0u8; padded_len];
out[padded_len - input.len()..].copy_from_slice(input);
Ok(out)
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_left_pad() {
const INPUT_LEN: usize = 3;
let input = vec![0u8; INPUT_LEN];

// input len < padded len
let padded = left_pad(&input, INPUT_LEN + 1).unwrap();
assert_eq!(padded.len(), INPUT_LEN + 1);

// input len == padded len
let padded = left_pad(&input, INPUT_LEN).unwrap();
assert_eq!(padded.len(), INPUT_LEN);

// input len > padded len
let padded = left_pad(&input, INPUT_LEN - 1);
assert!(padded.is_err());
}
}
10 changes: 3 additions & 7 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use num_bigint::BigUint;
use rand_core::CryptoRngCore;
use zeroize::Zeroize;

use crate::errors::{Error, Result};
use crate::errors::Result;
use crate::internals;
use crate::key::{RsaPrivateKey, RsaPublicKey};

Expand All @@ -29,16 +29,12 @@ impl EncryptionPrimitive for RsaPublicKey {
let mut c_bytes = c.to_bytes_be();
let ciphertext = internals::left_pad(&c_bytes, pad_size);

if pad_size < ciphertext.len() {
return Err(Error::Verification);
}

// clear out tmp values
m.zeroize();
c.zeroize();
c_bytes.zeroize();

Ok(ciphertext)
ciphertext
}
}

Expand All @@ -59,6 +55,6 @@ impl DecryptionPrimitive for RsaPrivateKey {
m.zeroize();
m_bytes.zeroize();

Ok(plaintext)
plaintext
}
}