Skip to content

Commit

Permalink
Merge pull request virtee#219 from larrydewey/cleanup
Browse files Browse the repository at this point in the history
cleanup: Removing unnecessary .into() calls
  • Loading branch information
larrydewey authored Aug 12, 2024
2 parents 89bfa12 + b071a66 commit 5c80637
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 60 deletions.
10 changes: 5 additions & 5 deletions src/certs/sev/ca/cert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl codicon::Decoder<()> for Certificate {
1 => Certificate {
v1: v1::Certificate::decode(reader, params)?,
},
_ => return Err(ErrorKind::InvalidData.into()),
_ => return Err(ErrorKind::InvalidData)?,
})
}
}
Expand All @@ -100,7 +100,7 @@ impl codicon::Encoder<()> for Certificate {
fn encode(&self, writer: impl Write, _: ()) -> Result<()> {
match self.version() {
1 => unsafe { self.v1.encode(writer, ()) },
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand All @@ -112,7 +112,7 @@ impl codicon::Encoder<Body> for Certificate {
fn encode(&self, writer: impl Write, _: Body) -> Result<()> {
match self.version() {
1 => unsafe { self.v1.encode(writer, Body) },
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl TryFrom<&Certificate> for Usage {
fn try_from(value: &Certificate) -> Result<Self> {
match value.version() {
1 => Ok(unsafe { value.v1.preamble.data.usage }),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand All @@ -168,7 +168,7 @@ impl TryFrom<&Certificate> for PublicKey<Usage> {
fn try_from(value: &Certificate) -> Result<Self> {
match value.version() {
1 => unsafe { value.v1.try_into() },
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/certs/sev/ca/cert/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ pub struct Preamble {
impl Preamble {
fn size(&self) -> Result<Size> {
if self.data.psize != self.data.msize {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

match u32::from_le(self.data.msize) {
2048 => Ok(Size::Small),
4096 => Ok(Size::Large),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/certs/sev/ca/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl codicon::Decoder<()> for Chain {
fn decode(mut reader: impl Read, _: ()) -> Result<Self> {
let ask = Certificate::decode(&mut reader, ())?;
if Usage::try_from(&ask)? != Usage::ASK {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

let ark = Certificate::decode(&mut reader, ())?;
if Usage::try_from(&ark)? != Usage::ARK {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

Ok(Self { ask, ark })
Expand Down
6 changes: 3 additions & 3 deletions src/certs/sev/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ macro_rules! prv_decoder {
let prv = pkey::PKey::private_key_from_der(&buf)?;
let key = PublicKey::try_from(params)?;
if !prv.public_eq(&key.key) {
return Err(ErrorKind::InvalidData.into());
return Err(ErrorKind::InvalidData)?;
}

Ok(PrivateKey {
Expand Down Expand Up @@ -108,7 +108,7 @@ where
let hash = sig.hash == self.hash;
let id = sig.id.is_none() || sig.id == self.id;
if !usage || !kind || !hash || !id {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

let mut ver = sign::Verifier::new(sig.hash, &self.key)?;
Expand All @@ -125,7 +125,7 @@ where
if ok {
Ok(())
} else {
Err(ErrorKind::NotFound.into())
Err(ErrorKind::NotFound)?
}
})?
}
Expand Down
18 changes: 9 additions & 9 deletions src/certs/sev/sev/cert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl codicon::Decoder<()> for Certificate {
1 => Certificate {
v1: v1::Certificate::decode(reader, params)?,
},
_ => return Err(ErrorKind::InvalidData.into()),
_ => return Err(ErrorKind::InvalidData)?,
})
}
}
Expand All @@ -91,7 +91,7 @@ impl codicon::Encoder<()> for Certificate {
fn encode(&self, mut writer: impl Write, _: ()) -> Result<()> {
match self.version() {
1 => unsafe { writer.save(&self.v1) },
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand All @@ -103,7 +103,7 @@ impl codicon::Encoder<Body> for Certificate {
fn encode(&self, mut writer: impl Write, _: Body) -> Result<()> {
match self.version() {
1 => unsafe { writer.save(&self.v1.body) },
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ impl TryFrom<&Certificate> for [Option<Signature>; 2] {
unsafe { &value.v1.sigs[0] }.try_into()?,
unsafe { &value.v1.sigs[1] }.try_into()?,
]),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand All @@ -154,7 +154,7 @@ impl TryFrom<&Certificate> for Usage {
fn try_from(value: &Certificate) -> Result<Self> {
match value.version() {
1 => Ok(unsafe { value.v1.body.data.key.usage }),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand All @@ -176,7 +176,7 @@ impl TryFrom<&Certificate> for PublicKey<Usage> {
1 => PublicKey::try_from(unsafe {
&std::ptr::addr_of!(value.v1.body.data.key).read_unaligned()
}),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand All @@ -195,7 +195,7 @@ impl Verifiable for (&Certificate, &Certificate) {
}
}

Err(ErrorKind::InvalidInput.into())
Err(ErrorKind::InvalidInput)?
}
}

Expand All @@ -213,7 +213,7 @@ impl Verifiable for (&ca::Certificate, &Certificate) {
}
}

Err(ErrorKind::InvalidInput.into())
Err(ErrorKind::InvalidInput)?
}
}

Expand All @@ -224,7 +224,7 @@ impl Signer<Certificate> for PrivateKey<Usage> {
fn sign(&self, target: &mut Certificate) -> Result<()> {
match target.version() {
1 => self.sign(unsafe { &mut target.v1 }),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/certs/sev/sev/cert/v1/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl TryFrom<Algorithm> for pkey::Id {
Algorithm::RSA_SHA256 | Algorithm::RSA_SHA384 => pkey::Id::RSA,
Algorithm::ECDSA_SHA256 | Algorithm::ECDSA_SHA384 => pkey::Id::EC,
Algorithm::ECDH_SHA256 | Algorithm::ECDH_SHA384 => pkey::Id::EC,
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
})
}
}
Expand All @@ -52,7 +52,7 @@ impl TryFrom<Algorithm> for hash::MessageDigest {
Ok(hash::MessageDigest::sha384())
}

_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
6 changes: 3 additions & 3 deletions src/certs/sev/sev/cert/v1/body/key/ecc/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Group {
Ok(match self {
Group::P256 => 32,
Group::P384 => 48,
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
})
}
}
Expand All @@ -29,7 +29,7 @@ impl TryFrom<Group> for nid::Nid {
Ok(match value {
Group::P256 => nid::Nid::X9_62_PRIME256V1,
Group::P384 => nid::Nid::SECP384R1,
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
})
}
}
Expand All @@ -42,7 +42,7 @@ impl TryFrom<nid::Nid> for Group {
Ok(match value {
nid::Nid::X9_62_PRIME256V1 => Group::P256,
nid::Nid::SECP384R1 => Group::P384,
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/certs/sev/sev/cert/v1/body/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl TryFrom<&PubKey> for pkey::PKey<pkey::Public> {
match v.algo.try_into()? {
pkey::Id::RSA => Ok(unsafe { &v.key.rsa }.try_into()?),
pkey::Id::EC => Ok(unsafe { &v.key.ecc }.try_into()?),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl PubKey {
Usage::PEK => Algorithm::ECDSA_SHA256,
Usage::CEK => Algorithm::ECDSA_SHA256,
Usage::PDH => Algorithm::ECDH_SHA256,
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
};

let (key, prv) = ecc::PubKey::generate(ecc::group::Group::P384)?;
Expand Down
2 changes: 1 addition & 1 deletion src/certs/sev/sev/cert/v1/body/key/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl PubKey {
match u32::from_le(self.modulus_size) {
2048 => Ok(256),
4096 => Ok(512),
_ => Err(ErrorKind::InvalidInput.into()),
_ => Err(ErrorKind::InvalidInput)?,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/certs/sev/sev/cert/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Signer<Certificate> for PrivateKey<Usage> {
} else if target.sigs[1].is_empty() {
&mut target.sigs[1]
} else {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
};

let mut sig = sign::Signer::new(self.hash, &self.key)?;
Expand Down
10 changes: 5 additions & 5 deletions src/certs/sev/sev/cert/v1/sig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl TryFrom<crate::certs::sev::Signature> for Signature {

fn try_from(value: crate::certs::sev::Signature) -> Result<Self> {
if value.id.is_some() {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

let (sig, algo) = match value.kind {
Expand All @@ -93,7 +93,7 @@ impl TryFrom<crate::certs::sev::Signature> for Signature {
match value.hash.type_() {
nid::Nid::SHA256 => (sig, Algorithm::RSA_SHA256),
nid::Nid::SHA384 => (sig, Algorithm::RSA_SHA384),
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
}
}

Expand All @@ -103,11 +103,11 @@ impl TryFrom<crate::certs::sev::Signature> for Signature {
match value.hash.type_() {
nid::Nid::SHA256 => (sig, Algorithm::ECDSA_SHA256),
nid::Nid::SHA384 => (sig, Algorithm::ECDSA_SHA384),
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
}
}

_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
};

Ok(Signature {
Expand All @@ -133,7 +133,7 @@ impl TryFrom<&Signature> for Option<crate::certs::sev::Signature> {
let sig = match kind {
pkey::Id::RSA => Vec::try_from(unsafe { &value.sig.rsa })?,
pkey::Id::EC => Vec::try_from(unsafe { &value.sig.ecdsa })?,
_ => return Err(ErrorKind::InvalidInput.into()),
_ => return Err(ErrorKind::InvalidInput)?,
};

Ok(Some(crate::certs::sev::Signature {
Expand Down
8 changes: 4 additions & 4 deletions src/certs/sev/sev/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ impl codicon::Decoder<()> for Chain {
fn decode(mut reader: impl Read, _: ()) -> Result<Self> {
let pdh = Certificate::decode(&mut reader, ())?;
if Usage::try_from(&pdh)? != Usage::PDH {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

let pek = Certificate::decode(&mut reader, ())?;
if Usage::try_from(&pek)? != Usage::PEK {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

let oca = Certificate::decode(&mut reader, ())?;
if Usage::try_from(&oca)? != Usage::OCA {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

let cek = Certificate::decode(&mut reader, ())?;
if Usage::try_from(&cek)? != Usage::CEK {
return Err(ErrorKind::InvalidInput.into());
return Err(ErrorKind::InvalidInput)?;
}

Ok(Self { pdh, pek, oca, cek })
Expand Down
8 changes: 3 additions & 5 deletions src/firmware/guest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::fs::{File, OpenOptions};
//
// if lower != 0 {
// match lower.into() {
// Indeterminate::Known(error) => return Err(error.into()),
// Indeterminate::Known(error) => return Err(error)?,
// Indeterminate::Unknown => return Err(UserApiError::Unknown),
// }
// }
Expand Down Expand Up @@ -149,10 +149,8 @@ impl Firmware {
if let Err(ioctl_error) = SNP_GET_EXT_REPORT.ioctl(&mut self.0, &mut guest_request) {
match guest_request.fw_err.into() {
VmmError::InvalidCertificatePageLength => (),
VmmError::RateLimitRetryRequest => {
return Err(VmmError::RateLimitRetryRequest.into())
}
_ => return Err(ioctl_error.into()),
VmmError::RateLimitRetryRequest => return Err(VmmError::RateLimitRetryRequest)?,
_ => return Err(ioctl_error)?,
}

// Eventually the code below will be moved back into this scope.
Expand Down
2 changes: 1 addition & 1 deletion src/launch/sev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl<U: AsRawFd, V: AsRawFd> Launcher<Finished, U, V> {
if let Indeterminate::Known(InvalidLen) = err {
len = first.len;
} else {
return Err(err.into());
return Err(err)?;
}
}

Expand Down
Loading

0 comments on commit 5c80637

Please sign in to comment.