Skip to content

Commit

Permalink
Addressed Cargo Clippy comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
zlogic committed Jul 12, 2024
1 parent aa4ad62 commit 0f079c0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 94 deletions.
53 changes: 24 additions & 29 deletions src/ikev2/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,19 @@ pub struct TransformParameters {
dh: Option<Transform>,
esn: Option<Transform>,
protocol_id: message::IPSecProtocolID,
spi: message::SPI,
spi: message::Spi,
}

impl TransformParameters {
pub fn create_dh(&self) -> Result<DHTransformType, InitError> {
DHTransformType::init(
self.dh
.as_ref()
.ok_or_else(|| "DH not configured")?
.transform_type,
)
DHTransformType::init(self.dh.as_ref().ok_or("DH not configured")?.transform_type)
}

pub fn create_prf(&self, key: &[u8]) -> Result<PseudorandomTransform, InitError> {
PseudorandomTransform::init(
self.prf
.as_ref()
.ok_or_else(|| "PRF not configured")?
.ok_or("PRF not configured")?
.transform_type,
key,
)
Expand All @@ -62,7 +57,7 @@ impl TransformParameters {
self.protocol_id
}

pub fn spi(&self) -> message::SPI {
pub fn spi(&self) -> message::Spi {
self.spi
}

Expand Down Expand Up @@ -147,8 +142,8 @@ impl<'a> Iterator for TransformParametersIter<'a> {
}
}

pub fn choose_sa_parameters<'a>(
sa: &'a message::PayloadSecurityAssociation,
pub fn choose_sa_parameters(
sa: &message::PayloadSecurityAssociation,
) -> Option<(TransformParameters, u8)> {
sa.iter_proposals()
.flat_map(|prop| {
Expand Down Expand Up @@ -553,7 +548,7 @@ impl PseudorandomTransform {
}
// Following T-chunks.
next_data[0..hash.len()].copy_from_slice(&hash);
next_data[hash.len()..hash.len() + data.len()].copy_from_slice(&data);
next_data[hash.len()..hash.len() + data.len()].copy_from_slice(data);
next_data[hash.len() + data.len()] = t + 1;
let mut signer = new_hmac_sha256(key).map_err(|err| {
debug!("Failed to init SHA256 HMAC signer: {}", err);
Expand Down Expand Up @@ -785,15 +780,15 @@ impl CryptoStack {
let enc = params
.enc
.as_ref()
.ok_or_else(|| "Undefined encryption parameters")?;
.ok_or("Undefined encryption parameters")?;
let auth = params
.auth
.as_ref()
.map(|transform| transform.transform_type);
let prf = params
.prf
.as_ref()
.ok_or_else(|| "Undefined pseudorandom transform parameters")?
.ok_or("Undefined pseudorandom transform parameters")?
.transform_type;
Ok(CryptoStack {
derive_key,
Expand All @@ -817,9 +812,9 @@ impl CryptoStack {
+ self.auth_responder.signature_length()
}

pub fn encrypt_data<'a>(
pub fn encrypt_data(
&self,
data: &'a mut [u8],
data: &mut [u8],
msg_len: usize,
associated_data: &[u8],
) -> Result<(), CryptoError> {
Expand All @@ -841,7 +836,7 @@ impl CryptoStack {
let decrypted_slice = if decrypted_slice.len() >= padding_length {
&decrypted_slice[..decrypted_slice.len() - padding_length]
} else {
&decrypted_slice
decrypted_slice
};
Ok(decrypted_slice)
}
Expand All @@ -864,9 +859,9 @@ impl CryptoStack {
}

pub trait Encryption {
fn encrypt<'a>(
fn encrypt(
&self,
data: &'a mut [u8],
data: &mut [u8],
msg_len: usize,
associated_data: &[u8],
) -> Result<(), CryptoError>;
Expand Down Expand Up @@ -918,9 +913,9 @@ impl EncryptionType {
}
}

fn encrypt<'a>(
fn encrypt(
&self,
data: &'a mut [u8],
data: &mut [u8],
msg_len: usize,
associated_data: &[u8],
) -> Result<(), CryptoError> {
Expand Down Expand Up @@ -948,7 +943,7 @@ pub struct EncryptionAesCbc256 {
}

impl Encryption for EncryptionAesCbc256 {
fn encrypt<'a>(&self, data: &'a mut [u8], msg_len: usize, _: &[u8]) -> Result<(), CryptoError> {
fn encrypt(&self, data: &mut [u8], msg_len: usize, _: &[u8]) -> Result<(), CryptoError> {
let aes_cbc_cipher = cipher::Cipher::aes_256_cbc();
let mut ctx = cipher_ctx::CipherCtx::new().map_err(|err| {
debug!("Failed to init cipher context: {}", err);
Expand All @@ -968,7 +963,7 @@ impl Encryption for EncryptionAesCbc256 {
debug!("Failed to generate IV for AES CBC 256: {}", err);
"Failed to generate IV for AES CBC 256"
})?;
ctx.encrypt_init(Some(&aes_cbc_cipher), Some(&self.cipher_key), Some(iv))
ctx.encrypt_init(Some(aes_cbc_cipher), Some(&self.cipher_key), Some(iv))
.map_err(|err| {
debug!("Failed to init AES CBC 256 encryptor: {}", err);
"Failed to init AES CBC 256 encryptor"
Expand Down Expand Up @@ -1006,7 +1001,7 @@ impl Encryption for EncryptionAesCbc256 {
return Err("Message length is too short".into());
}
let iv = &data[..iv_size];
ctx.decrypt_init(Some(&aes_cbc_cipher), Some(&self.cipher_key), Some(iv))
ctx.decrypt_init(Some(aes_cbc_cipher), Some(&self.cipher_key), Some(iv))
.map_err(|err| {
debug!("Failed to init AES CBC 256 decryptor: {}", err);
"Failed to init AES CBC 256 decryptor"
Expand Down Expand Up @@ -1044,9 +1039,9 @@ pub struct EncryptionAesGcm256 {
}

impl Encryption for EncryptionAesGcm256 {
fn encrypt<'a>(
fn encrypt(
&self,
data: &'a mut [u8],
data: &mut [u8],
msg_len: usize,
associated_data: &[u8],
) -> Result<(), CryptoError> {
Expand All @@ -1072,7 +1067,7 @@ impl Encryption for EncryptionAesGcm256 {
debug!("Failed to init cipher context: {}", err);
"Failed to init cipher context"
})?;
match ctx.encrypt_init(Some(&aes_gcm_cipher), Some(&self.cipher_key), Some(&nonce)) {
match ctx.encrypt_init(Some(aes_gcm_cipher), Some(&self.cipher_key), Some(&nonce)) {
Ok(dec) => dec,
Err(err) => {
debug!("Failed to init AES GCM 16 256: {}", err);
Expand Down Expand Up @@ -1122,7 +1117,7 @@ impl Encryption for EncryptionAesGcm256 {
debug!("Failed to init cipher context: {}", err);
"Failed to init cipher context"
})?;
ctx.decrypt_init(Some(&aes_gcm_cipher), Some(&self.cipher_key), Some(&nonce))
ctx.decrypt_init(Some(aes_gcm_cipher), Some(&self.cipher_key), Some(&nonce))
.map_err(|err| {
debug!("Failed to init AES GCM 16 256: {}", err);
"Failed to init AES GCM 16 256"
Expand Down Expand Up @@ -1164,7 +1159,7 @@ impl Encryption for EncryptionAesGcm256 {
fn new_hmac_sha256(
key: &pkey::PKey<pkey::Private>,
) -> Result<sign::Signer, openssl::error::ErrorStack> {
sign::Signer::new(hash::MessageDigest::sha256(), &key)
sign::Signer::new(hash::MessageDigest::sha256(), key)
}

pub fn hash_sha1(data: &[u8]) -> Result<[u8; 160 / 8], CryptoError> {
Expand Down
38 changes: 19 additions & 19 deletions src/ikev2/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ impl fmt::Display for Flags {
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SPI {
pub enum Spi {
None,
U32(u32),
U64(u64),
}

impl SPI {
fn from_slice(spi: &[u8]) -> Result<SPI, FormatError> {
impl Spi {
fn from_slice(spi: &[u8]) -> Result<Spi, FormatError> {
if spi.len() == 4 {
let mut value = [0u8; 4];
value.copy_from_slice(spi);
Expand Down Expand Up @@ -120,7 +120,7 @@ impl SPI {
}
}

impl fmt::Display for SPI {
impl fmt::Display for Spi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::None => Ok(()),
Expand All @@ -130,7 +130,7 @@ impl fmt::Display for SPI {
}
}

impl fmt::Debug for SPI {
impl fmt::Debug for Spi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
Expand Down Expand Up @@ -277,7 +277,7 @@ impl InputMessage<'_> {
}

pub fn raw_data(&self) -> &[u8] {
&self.data
self.data
}
}

Expand Down Expand Up @@ -656,7 +656,7 @@ impl<'a> Iterator for PayloadIter<'a> {
}
let current_payload = self.next_payload;
let start_offset = self.start_offset;
let data = &self.data[..];
let data = self.data;
let next_payload = self.data[0];
self.next_payload = next_payload;
let payload_flags = self.data[1];
Expand Down Expand Up @@ -838,7 +838,7 @@ impl<'a> Iterator for SecurityAssociationIter<'a> {
debug!("Proposal overflow");
return None;
}
let data = &self.data[..];
let data = self.data;
self.data = &self.data[proposal_length..];
let proposal_num = data[4];
if proposal_num != self.next_proposal_num {
Expand All @@ -863,7 +863,7 @@ impl<'a> Iterator for SecurityAssociationIter<'a> {
return None;
}
let spi = &data[8..8 + spi_size];
let spi = match SPI::from_slice(spi) {
let spi = match Spi::from_slice(spi) {
Ok(spi) => spi,
Err(_) => {
return Some(Err("Unsupported SPI format".into()));
Expand All @@ -884,7 +884,7 @@ pub struct SecurityAssociationProposal<'a> {
proposal_num: u8,
protocol_id: IPSecProtocolID,
num_transforms: usize,
spi: SPI,
spi: Spi,
data: &'a [u8],
}

Expand All @@ -904,7 +904,7 @@ impl<'a> SecurityAssociationProposal<'a> {
self.protocol_id
}

pub fn spi(&self) -> SPI {
pub fn spi(&self) -> Spi {
self.spi
}
}
Expand Down Expand Up @@ -1087,7 +1087,7 @@ impl<'a> Iterator for SecurityAssociationTransformIter<'a> {
debug!("Transform overflow");
return None;
}
let data = &self.data[..];
let data = self.data;
self.data = &self.data[transform_length..];
if self.num_transforms == 0 && !self.data.is_empty() {
debug!("Packet has unaccounted transforms");
Expand Down Expand Up @@ -1302,7 +1302,7 @@ impl<'a> PayloadIdentification<'a> {
}

pub fn raw_value(&self) -> &[u8] {
&self.data
self.data
}

pub fn read_value(&self) -> &[u8] {
Expand Down Expand Up @@ -1360,7 +1360,7 @@ pub struct PayloadCertificate<'a> {

impl<'a> PayloadCertificate<'a> {
fn from_payload(data: &'a [u8]) -> Result<PayloadCertificate<'a>, FormatError> {
if data.len() < 1 {
if data.is_empty() {
debug!("Not enough data in certificate payload");
return Err("Not enough data in certificate payload".into());
}
Expand All @@ -1378,7 +1378,7 @@ impl<'a> PayloadCertificate<'a> {
}

pub fn read_value(&self) -> &[u8] {
&self.data
self.data
}
}

Expand All @@ -1388,7 +1388,7 @@ pub struct PayloadCertificateRequest<'a> {

impl<'a> PayloadCertificateRequest<'a> {
fn from_payload(data: &'a [u8]) -> Result<PayloadCertificateRequest<'a>, FormatError> {
if data.len() < 1 {
if data.is_empty() {
debug!("Not enough data in certificate request payload");
Err("Not enough data in certificate request payload".into())
} else {
Expand Down Expand Up @@ -1436,7 +1436,7 @@ pub struct PayloadAuthentication<'a> {

impl<'a> PayloadAuthentication<'a> {
fn from_payload(data: &'a [u8]) -> Result<PayloadAuthentication<'a>, FormatError> {
if data.len() < 1 {
if data.is_empty() {
debug!("Not enough data in authentication payload");
return Err("Not enough data in certificate payload".into());
}
Expand Down Expand Up @@ -1757,7 +1757,7 @@ impl<'a> Iterator for TrafficSelectorIter<'a> {
return None;
}

let data = &self.data[..];
let data = self.data;
self.data = &self.data[selector_length..];
self.num_selectors = self.num_selectors.saturating_sub(1);
let ts_type = match TrafficSelectorType::from_u8(data[0]) {
Expand Down Expand Up @@ -1921,7 +1921,7 @@ impl<'a> Iterator for ConfigurationAttributesIter<'a> {
debug!("Attribute overflow");
return None;
}
let data = &self.data[..];
let data = self.data;
self.data = &self.data[4 + attribute_length..];

let mut attribute_type = [0u8; 2];
Expand Down
Loading

0 comments on commit 0f079c0

Please sign in to comment.