diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-02-21 12:05:26 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-02-21 12:05:26 +0100 |
commit | 31514f6fddc8001906e0ba628cb772a9a54d2428 (patch) | |
tree | f37d15f400ae7540b4945da57d6da4fe5f7053ef | |
parent | 923a649ac169fc5210b730017e23557b74ccceaf (diff) |
openpgp: Remove bare implementations of serialized_len on MPIs.
- SerializeInto::serialized_len() provides the same.
-rw-r--r-- | openpgp/src/crypto/hash.rs | 2 | ||||
-rw-r--r-- | openpgp/src/crypto/mpis.rs | 134 | ||||
-rw-r--r-- | openpgp/src/parse/mpis.rs | 1 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 1 | ||||
-rw-r--r-- | tool/src/commands/dump.rs | 2 |
5 files changed, 6 insertions, 134 deletions
diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs index 78bbbbe4..5150eb29 100644 --- a/openpgp/src/crypto/hash.rs +++ b/openpgp/src/crypto/hash.rs @@ -245,6 +245,8 @@ impl<P, R> Hash for Key4<P, R> { /// Update the Hash with a hash of the key. fn hash(&self, hash: &mut Context) { + use crate::serialize::SerializeInto; + // We hash 8 bytes plus the MPIs. But, the len doesn't // include the tag (1 byte) or the length (2 bytes). let len = (9 - 3) + self.mpis().serialized_len(); diff --git a/openpgp/src/crypto/mpis.rs b/openpgp/src/crypto/mpis.rs index b78b513f..ace023d4 100644 --- a/openpgp/src/crypto/mpis.rs +++ b/openpgp/src/crypto/mpis.rs @@ -361,50 +361,6 @@ pub enum PublicKey { } impl PublicKey { - /// Number of octets all MPIs of this instance occupy when serialized. - pub fn serialized_len(&self) -> usize { - use self::PublicKey::*; - - // Fields are mostly MPIs that consist of two octets length - // plus the big endian value itself. All other field types are - // commented. - match self { - &RSA { ref e, ref n } => - 2 + e.value.len() + 2 + n.value.len(), - - &DSA { ref p, ref q, ref g, ref y } => - 2 + p.value.len() + 2 + q.value.len() + - 2 + g.value.len() + 2 + y.value.len(), - - &ElGamal { ref p, ref g, ref y } => - 2 + p.value.len() + - 2 + g.value.len() + 2 + y.value.len(), - - &EdDSA { ref curve, ref q } => - 2 + q.value.len() + - // one length octet plus the ASN.1 OID - 1 + curve.oid().len(), - - &ECDSA { ref curve, ref q } => - 2 + q.value.len() + - // one length octet plus the ASN.1 OID - 1 + curve.oid().len(), - - &ECDH { ref curve, ref q, .. } => - // one length octet plus the ASN.1 OID - 1 + curve.oid().len() + - 2 + q.value.len() + - // one octet length, one reserved and two algorithm identifier. - 4, - - &Unknown { ref mpis, ref rest } => - mpis.iter().map(|m| 2 + m.value.len()).sum::<usize>() - + rest.len(), - - __Nonexhaustive => unreachable!(), - } - } - /// Returns the length of the public key in bits. /// /// For finite field crypto this returns the size of the field we @@ -698,36 +654,6 @@ impl PartialEq for SecretKeyMaterial { impl Eq for SecretKeyMaterial {} impl SecretKeyMaterial { - /// Number of octets all MPIs of this instance occupy when serialized. - pub fn serialized_len(&self) -> usize { - use self::SecretKeyMaterial::*; - - // Fields are mostly MPIs that consist of two octets length - // plus the big endian value itself. All other field types are - // commented. - match self { - &RSA { ref d, ref p, ref q, ref u } => - 2 + d.value.len() + 2 + q.value.len() + - 2 + p.value.len() + 2 + u.value.len(), - - &DSA { ref x } => 2 + x.value.len(), - - &ElGamal { ref x } => 2 + x.value.len(), - - &EdDSA { ref scalar } => 2 + scalar.value.len(), - - &ECDSA { ref scalar } => 2 + scalar.value.len(), - - &ECDH { ref scalar } => 2 + scalar.value.len(), - - &Unknown { ref mpis, ref rest } => - mpis.iter().map(|m| 2 + m.value.len()).sum::<usize>() - + rest.len(), - - __Nonexhaustive => unreachable!(), - } - } - /// Returns, if known, the public-key algorithm for this secret /// key. pub fn algo(&self) -> Option<PublicKeyAlgorithm> { @@ -829,33 +755,6 @@ pub enum Ciphertext { } impl Ciphertext { - /// Number of octets all MPIs of this instance occupy when serialized. - pub fn serialized_len(&self) -> usize { - use self::Ciphertext::*; - - // Fields are mostly MPIs that consist of two octets length - // plus the big endian value itself. All other field types are - // commented. - match self { - &RSA { ref c } => - 2 + c.value.len(), - - &ElGamal { ref e, ref c } => - 2 + e.value.len() + 2 + c.value.len(), - - &ECDH { ref e, ref key } => - 2 + e.value.len() + - // one length octet plus ephemeral key - 1 + key.len(), - - &Unknown { ref mpis, ref rest } => - mpis.iter().map(|m| 2 + m.value.len()).sum::<usize>() - + rest.len(), - - __Nonexhaustive => unreachable!(), - } - } - /// Returns, if known, the public-key algorithm for this /// ciphertext. pub fn pk_algo(&self) -> Option<PublicKeyAlgorithm> { @@ -959,39 +858,6 @@ pub enum Signature { #[doc(hidden)] __Nonexhaustive, } -impl Signature { - /// Number of octets all MPIs of this instance occupy when serialized. - pub fn serialized_len(&self) -> usize { - use self::Signature::*; - - // Fields are mostly MPIs that consist of two octets length - // plus the big endian value itself. All other field types are - // commented. - match self { - &RSA { ref s } => - 2 + s.value.len(), - - &DSA { ref r, ref s } => - 2 + r.value.len() + 2 + s.value.len(), - - &ElGamal { ref r, ref s } => - 2 + r.value.len() + 2 + s.value.len(), - - &EdDSA { ref r, ref s } => - 2 + r.value.len() + 2 + s.value.len(), - - &ECDSA { ref r, ref s } => - 2 + r.value.len() + 2 + s.value.len(), - - &Unknown { ref mpis, ref rest } => - mpis.iter().map(|m| 2 + m.value.len()).sum::<usize>() - + rest.len(), - - __Nonexhaustive => unreachable!(), - } - } -} - impl Hash for Signature { /// Update the Hash with a hash of the MPIs. fn hash(&self, hash: &mut hash::Context) { diff --git a/openpgp/src/parse/mpis.rs b/openpgp/src/parse/mpis.rs index 35289e30..0dc6de4b 100644 --- a/openpgp/src/parse/mpis.rs +++ b/openpgp/src/parse/mpis.rs @@ -483,6 +483,7 @@ impl mpis::Signature { fn mpis_parse_test() { use super::Parse; use crate::PublicKeyAlgorithm::*; + use crate::serialize::SerializeInto; // Dummy RSA public key. { diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 52531262..5050b948 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -1171,6 +1171,7 @@ impl_parse_generic_packet!(Signature); #[test] fn signature_parser_test () { + use crate::serialize::SerializeInto; let data = crate::tests::message("sig.gpg"); { diff --git a/tool/src/commands/dump.rs b/tool/src/commands/dump.rs index 26483865..2ef4f1b0 100644 --- a/tool/src/commands/dump.rs +++ b/tool/src/commands/dump.rs @@ -501,6 +501,8 @@ impl PacketDumper { self.dump_mpis(output, &ii, &[&rest[..]], &["rest"])?; }, + mpis::Signature::__Nonexhaustive => unreachable!(), + } } }, |