summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-10-07 10:16:33 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-10-07 11:34:53 +0200
commit4f5d6c6ac24f39be532636dca4c79387a13ca856 (patch)
tree2995e3e5edfb3f3f3b7ceba4e039bcd7153c0048 /openpgp
parent86653161e813f5db0d1728fc25d94c282a9874cf (diff)
openpgp: Align MPI parsing functions with trait Parse.
- Change mpi::*::parse to take a Reader instead of a AsRef<u8>. The former is a more general interface.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/src/crypto/mpi.rs88
-rw-r--r--openpgp/src/parse/mpis.rs46
2 files changed, 48 insertions, 86 deletions
diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs
index 4964425e..3918a4c2 100644
--- a/openpgp/src/crypto/mpi.rs
+++ b/openpgp/src/crypto/mpi.rs
@@ -1091,31 +1091,24 @@ mod tests {
use std::io::Cursor;
use crate::PublicKeyAlgorithm::*;
- let buf = Vec::<u8>::default();
- let mut cur = Cursor::new(buf);
-
- pk.serialize(&mut cur).unwrap();
+ let mut buf = Vec::new();
+ pk.serialize(&mut buf).unwrap();
+ let cur = Cursor::new(buf);
#[allow(deprecated)]
let pk_ = match &pk {
PublicKey::RSA { .. } =>
- PublicKey::parse(
- RSAEncryptSign, cur.into_inner()).unwrap(),
+ PublicKey::parse(RSAEncryptSign, cur).unwrap(),
PublicKey::DSA { .. } =>
- PublicKey::parse(
- DSA, cur.into_inner()).unwrap(),
+ PublicKey::parse(DSA, cur).unwrap(),
PublicKey::ElGamal { .. } =>
- PublicKey::parse(
- ElGamalEncrypt, cur.into_inner()).unwrap(),
+ PublicKey::parse(ElGamalEncrypt, cur).unwrap(),
PublicKey::EdDSA { .. } =>
- PublicKey::parse(
- EdDSA, cur.into_inner()).unwrap(),
+ PublicKey::parse(EdDSA, cur).unwrap(),
PublicKey::ECDSA { .. } =>
- PublicKey::parse(
- ECDSA, cur.into_inner()).unwrap(),
+ PublicKey::parse(ECDSA, cur).unwrap(),
PublicKey::ECDH { .. } =>
- PublicKey::parse(
- ECDH, cur.into_inner()).unwrap(),
+ PublicKey::parse(ECDH, cur).unwrap(),
PublicKey::Unknown { .. } => unreachable!(),
PublicKey::__Nonexhaustive => unreachable!(),
@@ -1149,31 +1142,24 @@ mod tests {
use std::io::Cursor;
use crate::PublicKeyAlgorithm::*;
- let buf = Vec::<u8>::default();
- let mut cur = Cursor::new(buf);
-
- sk.serialize(&mut cur).unwrap();
+ let mut buf = Vec::new();
+ sk.serialize(&mut buf).unwrap();
+ let cur = Cursor::new(buf);
#[allow(deprecated)]
let sk_ = match &sk {
SecretKeyMaterial::RSA { .. } =>
- SecretKeyMaterial::parse(
- RSAEncryptSign, cur.into_inner()).unwrap(),
+ SecretKeyMaterial::parse(RSAEncryptSign, cur).unwrap(),
SecretKeyMaterial::DSA { .. } =>
- SecretKeyMaterial::parse(
- DSA, cur.into_inner()).unwrap(),
+ SecretKeyMaterial::parse(DSA, cur).unwrap(),
SecretKeyMaterial::EdDSA { .. } =>
- SecretKeyMaterial::parse(
- EdDSA, cur.into_inner()).unwrap(),
+ SecretKeyMaterial::parse(EdDSA, cur).unwrap(),
SecretKeyMaterial::ECDSA { .. } =>
- SecretKeyMaterial::parse(
- ECDSA, cur.into_inner()).unwrap(),
+ SecretKeyMaterial::parse(ECDSA, cur).unwrap(),
SecretKeyMaterial::ECDH { .. } =>
- SecretKeyMaterial::parse(
- ECDH, cur.into_inner()).unwrap(),
+ SecretKeyMaterial::parse(ECDH, cur).unwrap(),
SecretKeyMaterial::ElGamal { .. } =>
- SecretKeyMaterial::parse(
- ElGamalEncrypt, cur.into_inner()).unwrap(),
+ SecretKeyMaterial::parse(ElGamalEncrypt, cur).unwrap(),
SecretKeyMaterial::Unknown { .. } => unreachable!(),
SecretKeyMaterial::__Nonexhaustive => unreachable!(),
@@ -1188,22 +1174,18 @@ mod tests {
use std::io::Cursor;
use crate::PublicKeyAlgorithm::*;
- let buf = Vec::<u8>::default();
- let mut cur = Cursor::new(buf);
-
- ct.serialize(&mut cur).unwrap();
+ let mut buf = Vec::new();
+ ct.serialize(&mut buf).unwrap();
+ let cur = Cursor::new(buf);
#[allow(deprecated)]
let ct_ = match &ct {
Ciphertext::RSA { .. } =>
- Ciphertext::parse(
- RSAEncryptSign, cur.into_inner()).unwrap(),
+ Ciphertext::parse(RSAEncryptSign, cur).unwrap(),
Ciphertext::ElGamal { .. } =>
- Ciphertext::parse(
- ElGamalEncrypt, cur.into_inner()).unwrap(),
+ Ciphertext::parse(ElGamalEncrypt, cur).unwrap(),
Ciphertext::ECDH { .. } =>
- Ciphertext::parse(
- ECDH, cur.into_inner()).unwrap(),
+ Ciphertext::parse(ECDH, cur).unwrap(),
Ciphertext::Unknown { .. } => unreachable!(),
Ciphertext::__Nonexhaustive => unreachable!(),
@@ -1218,28 +1200,22 @@ mod tests {
use std::io::Cursor;
use crate::PublicKeyAlgorithm::*;
- let buf = Vec::<u8>::default();
- let mut cur = Cursor::new(buf);
-
- sig.serialize(&mut cur).unwrap();
+ let mut buf = Vec::new();
+ sig.serialize(&mut buf).unwrap();
+ let cur = Cursor::new(buf);
#[allow(deprecated)]
let sig_ = match &sig {
Signature::RSA { .. } =>
- Signature::parse(
- RSAEncryptSign, cur.into_inner()).unwrap(),
+ Signature::parse(RSAEncryptSign, cur).unwrap(),
Signature::DSA { .. } =>
- Signature::parse(
- DSA, cur.into_inner()).unwrap(),
+ Signature::parse(DSA, cur).unwrap(),
Signature::ElGamal { .. } =>
- Signature::parse(
- ElGamalEncryptSign, cur.into_inner()).unwrap(),
+ Signature::parse(ElGamalEncryptSign, cur).unwrap(),
Signature::EdDSA { .. } =>
- Signature::parse(
- EdDSA, cur.into_inner()).unwrap(),
+ Signature::parse(EdDSA, cur).unwrap(),
Signature::ECDSA { .. } =>
- Signature::parse(
- ECDSA, cur.into_inner()).unwrap(),
+ Signature::parse(ECDSA, cur).unwrap(),
Signature::Unknown { .. } => unreachable!(),
Signature::__Nonexhaustive => unreachable!(),
diff --git a/openpgp/src/parse/mpis.rs b/openpgp/src/parse/mpis.rs
index d3a95164..a9759b0d 100644
--- a/openpgp/src/parse/mpis.rs
+++ b/openpgp/src/parse/mpis.rs
@@ -22,15 +22,10 @@ impl mpi::PublicKey {
/// See [Section 3.2 of RFC 4880] for details.
///
/// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2
- pub fn parse<T: AsRef<[u8]>>(
- algo: PublicKeyAlgorithm, buf: T)
- -> Result<Self>
+ pub fn parse<R: Read>(algo: PublicKeyAlgorithm, reader: R) -> Result<Self>
{
- use std::io::Cursor;
-
- let cur = Cursor::new(buf);
let bio = buffered_reader::Generic::with_cookie(
- cur, None, Cookie::default());
+ reader, None, Cookie::default());
let mut php = PacketHeaderParser::new_naked(bio);
Self::_parse(algo, &mut php)
}
@@ -158,12 +153,12 @@ impl mpi::SecretKeyMaterial {
/// Parses secret key MPIs for `algo` plus their SHA1 checksum.
///
/// Fails if the checksum is wrong.
- pub fn parse_with_checksum<T: Read>(algo: PublicKeyAlgorithm,
- cur: T,
+ pub fn parse_with_checksum<R: Read>(algo: PublicKeyAlgorithm,
+ reader: R,
checksum: mpi::SecretKeyChecksum)
-> Result<Self> {
let bio = buffered_reader::Generic::with_cookie(
- cur, None, Cookie::default());
+ reader, None, Cookie::default());
let mut php = PacketHeaderParser::new_naked(bio);
Self::_parse(algo, &mut php, Some(checksum))
}
@@ -173,14 +168,10 @@ impl mpi::SecretKeyMaterial {
/// See [Section 3.2 of RFC 4880] for details.
///
/// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2
- pub fn parse<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T)
- -> Result<Self>
+ pub fn parse<R: Read>(algo: PublicKeyAlgorithm, reader: R) -> Result<Self>
{
- use std::io::Cursor;
-
- let cur = Cursor::new(buf);
let bio = buffered_reader::Generic::with_cookie(
- cur, None, Cookie::default());
+ reader, None, Cookie::default());
let mut php = PacketHeaderParser::new_naked(bio);
Self::_parse(algo, &mut php, None)
}
@@ -318,13 +309,10 @@ impl mpi::Ciphertext {
/// See [Section 3.2 of RFC 4880] for details.
///
/// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2
- pub fn parse<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T)
- -> Result<Self> {
- use std::io::Cursor;
-
- let cur = Cursor::new(buf);
+ pub fn parse<R: Read>(algo: PublicKeyAlgorithm, reader: R) -> Result<Self>
+ {
let bio = buffered_reader::Generic::with_cookie(
- cur, None, Cookie::default());
+ reader, None, Cookie::default());
let mut php = PacketHeaderParser::new_naked(bio);
Self::_parse(algo, &mut php)
}
@@ -402,13 +390,10 @@ impl mpi::Signature {
/// See [Section 3.2 of RFC 4880] for details.
///
/// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2
- pub fn parse<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T)
- -> Result<Self> {
- use std::io::Cursor;
-
- let cur = Cursor::new(buf);
+ pub fn parse<R: Read>(algo: PublicKeyAlgorithm, reader: R) -> Result<Self>
+ {
let bio = buffered_reader::Generic::with_cookie(
- cur, None, Cookie::default());
+ reader, None, Cookie::default());
let mut php = PacketHeaderParser::new_naked(bio);
Self::_parse(algo, &mut php)
}
@@ -507,13 +492,14 @@ impl mpi::Signature {
#[test]
fn mpis_parse_test() {
+ use std::io::Cursor;
use super::Parse;
use crate::PublicKeyAlgorithm::*;
use crate::serialize::MarshalInto;
// Dummy RSA public key.
{
- let buf = b"\x00\x01\x01\x00\x02\x02".to_vec();
+ let buf = Cursor::new("\x00\x01\x01\x00\x02\x02");
let mpis = mpi::PublicKey::parse(RSAEncryptSign, buf).unwrap();
//assert_eq!(mpis.serialized_len(), 6);
@@ -533,7 +519,7 @@ fn mpis_parse_test() {
// The number 2.
{
- let buf = b"\x00\x02\x02".to_vec();
+ let buf = Cursor::new("\x00\x02\x02");
let mpis = mpi::Ciphertext::parse(RSAEncryptSign, buf).unwrap();
assert_eq!(mpis.serialized_len(), 3);