diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-06-28 13:59:48 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-06-28 13:59:48 +0200 |
commit | 1525eec1426a6a34b84809179d784e5cee3e8bfa (patch) | |
tree | 1e73534e95d14f776085df7ca6154057b8108712 /openpgp/src/crypto | |
parent | f214d55b7061e9d6006f915ee8fbedf29ddf6078 (diff) |
openpgp: Make struct MPI opaque.
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r-- | openpgp/src/crypto/asymmetric.rs | 30 | ||||
-rw-r--r-- | openpgp/src/crypto/ecdh.rs | 10 | ||||
-rw-r--r-- | openpgp/src/crypto/keygrip.rs | 12 | ||||
-rw-r--r-- | openpgp/src/crypto/mpis.rs | 14 | ||||
-rw-r--r-- | openpgp/src/crypto/sexp.rs | 8 |
5 files changed, 42 insertions, 32 deletions
diff --git a/openpgp/src/crypto/asymmetric.rs b/openpgp/src/crypto/asymmetric.rs index 839f0eaa..050d83a9 100644 --- a/openpgp/src/crypto/asymmetric.rs +++ b/openpgp/src/crypto/asymmetric.rs @@ -97,12 +97,12 @@ impl Signer for KeyPair { (RSAEncryptSign, &PublicKey::RSA { ref e, ref n }, &mpis::SecretKey::RSA { ref p, ref q, ref d, .. }) => { - let public = rsa::PublicKey::new(&n.value, &e.value)?; - let secret = rsa::PrivateKey::new(&d.value, &p.value, - &q.value, Option::None)?; + let public = rsa::PublicKey::new(n.value(), e.value())?; + let secret = rsa::PrivateKey::new(d.value(), p.value(), + q.value(), Option::None)?; // The signature has the length of the modulus. - let mut sig = vec![0u8; n.value.len()]; + let mut sig = vec![0u8; n.value().len()]; // As described in [Section 5.2.2 and 5.2.3 of RFC 4880], // to verify the signature, we need to encode the @@ -122,8 +122,8 @@ impl Signer for KeyPair { (DSA, &PublicKey::DSA { ref p, ref q, ref g, .. }, &mpis::SecretKey::DSA { ref x }) => { - let params = dsa::Params::new(&p.value, &q.value, &g.value); - let secret = dsa::PrivateKey::new(&x.value); + let params = dsa::Params::new(p.value(), q.value(), g.value()); + let secret = dsa::PrivateKey::new(x.value()); let sig = dsa::sign(¶ms, &secret, digest, &mut rng)?; @@ -147,9 +147,9 @@ impl Signer for KeyPair { // Padding has to be unconditionaly, otherwise we have a // secret-dependant branch. let missing = ed25519::ED25519_KEY_SIZE - .saturating_sub(scalar.value.len()); + .saturating_sub(scalar.value().len()); let mut sec = [0u8; ed25519::ED25519_KEY_SIZE]; - sec[missing..].copy_from_slice(&scalar.value[..]); + sec[missing..].copy_from_slice(scalar.value()); let res = ed25519::sign(public, &sec[..], digest, &mut sig); unsafe { @@ -173,13 +173,13 @@ impl Signer for KeyPair { let secret = match curve { Curve::NistP256 => ecc::Scalar::new::<ecc::Secp256r1>( - &scalar.value)?, + scalar.value())?, Curve::NistP384 => ecc::Scalar::new::<ecc::Secp384r1>( - &scalar.value)?, + scalar.value())?, Curve::NistP521 => ecc::Scalar::new::<ecc::Secp521r1>( - &scalar.value)?, + scalar.value())?, _ => return Err( Error::UnsupportedEllipticCurve(curve.clone()) @@ -220,11 +220,11 @@ impl Decryptor for KeyPair { (PublicKey::RSA{ ref e, ref n }, mpis::SecretKey::RSA{ ref p, ref q, ref d, .. }, mpis::Ciphertext::RSA{ ref c }) => { - let public = rsa::PublicKey::new(&n.value, &e.value)?; - let secret = rsa::PrivateKey::new(&d.value, &p.value, - &q.value, Option::None)?; + let public = rsa::PublicKey::new(n.value(), e.value())?; + let secret = rsa::PrivateKey::new(d.value(), p.value(), + q.value(), Option::None)?; let mut rand = Yarrow::default(); - rsa::decrypt_pkcs1(&public, &secret, &mut rand, &c.value)? + rsa::decrypt_pkcs1(&public, &secret, &mut rand, c.value())? .into() } diff --git a/openpgp/src/crypto/ecdh.rs b/openpgp/src/crypto/ecdh.rs index 3f0fb590..d8ba73f3 100644 --- a/openpgp/src/crypto/ecdh.rs +++ b/openpgp/src/crypto/ecdh.rs @@ -183,10 +183,10 @@ pub fn decrypt(recipient: &Key, recipient_sec: &SecretKey, // Reverse the scalar. See // https://lists.gnupg.org/pipermail/gnupg-devel/2018-February/033437.html. let missing = curve25519::CURVE25519_SIZE - .saturating_sub(scalar.value.len()); + .saturating_sub(scalar.value().len()); let mut r = [0u8; curve25519::CURVE25519_SIZE]; - r[missing..].copy_from_slice(&scalar.value[..]); + r[missing..].copy_from_slice(scalar.value()); r.reverse(); // Compute the shared point S = rV = rvG, where (r, R) @@ -213,7 +213,7 @@ pub fn decrypt(recipient: &Key, recipient_sec: &SecretKey, let V = ecc::Point::new::<ecc::Secp256r1>(&Vx, &Vy)?; let r = - ecc::Scalar::new::<ecc::Secp256r1>(&scalar.value[..])?; + ecc::Scalar::new::<ecc::Secp256r1>(scalar.value())?; (V, r) } @@ -221,7 +221,7 @@ pub fn decrypt(recipient: &Key, recipient_sec: &SecretKey, let V = ecc::Point::new::<ecc::Secp384r1>(&Vx, &Vy)?; let r = - ecc::Scalar::new::<ecc::Secp384r1>(&scalar.value[..])?; + ecc::Scalar::new::<ecc::Secp384r1>(scalar.value())?; (V, r) } @@ -229,7 +229,7 @@ pub fn decrypt(recipient: &Key, recipient_sec: &SecretKey, let V = ecc::Point::new::<ecc::Secp521r1>(&Vx, &Vy)?; let r = - ecc::Scalar::new::<ecc::Secp521r1>(&scalar.value[..])?; + ecc::Scalar::new::<ecc::Secp521r1>(scalar.value())?; (V, r) } diff --git a/openpgp/src/crypto/keygrip.rs b/openpgp/src/crypto/keygrip.rs index feccebc4..a9f3cd0a 100644 --- a/openpgp/src/crypto/keygrip.rs +++ b/openpgp/src/crypto/keygrip.rs @@ -57,9 +57,9 @@ impl PublicKey { where H: Hash + ::std::io::Write { write!(hash, "(1:{}{}:", - kind, mpi.value.len() + prefix.len()).unwrap(); + kind, mpi.value().len() + prefix.len()).unwrap(); hash.update(prefix); - hash.update(&mpi.value); + hash.update(mpi.value()); write!(hash, ")").unwrap(); } @@ -75,11 +75,11 @@ impl PublicKey { if i == 6 { q.clone() } else { ecc_param(curve, i) }; // Opaque encoding? - if m.value[0] == 0x40 { + if m.value()[0] == 0x40 { // Drop the prefix! - let mut p = Vec::from(m.value); + let mut p = Vec::from(m.value()); p.remove(0); - m.value = p.into(); + m = p.into(); } hash_sexp_mpi(hash, name, &[], &m); @@ -99,7 +99,7 @@ impl PublicKey { // overwhelming empirical evidence suggest that we // need to prepend a 0. hash.update(&[0]); - hash.update(&n.value); + hash.update(n.value()); }, &DSA { ref p, ref q, ref g, ref y } => { diff --git a/openpgp/src/crypto/mpis.rs b/openpgp/src/crypto/mpis.rs index d93223ca..c1871619 100644 --- a/openpgp/src/crypto/mpis.rs +++ b/openpgp/src/crypto/mpis.rs @@ -23,9 +23,9 @@ use nettle; #[derive(Clone, Hash)] pub struct MPI { /// Length of the integer in bits. - pub bits: usize, + bits: usize, /// Integer value as big-endian. - pub value: Box<[u8]>, + value: Box<[u8]>, } impl From<Vec<u8>> for MPI { @@ -73,6 +73,16 @@ impl MPI { } } + /// Returns the length of the MPI in bits. + pub fn bits(&self) -> usize { + self.bits + } + + /// Returns the value of this MPI. + pub fn value(&self) -> &[u8] { + &self.value + } + /// Update the Hash with a hash of the MPIs. pub fn hash<H: nettle::Hash>(&self, hash: &mut H) { let len = &[(self.bits >> 8) as u8 & 0xFF, self.bits as u8]; diff --git a/openpgp/src/crypto/sexp.rs b/openpgp/src/crypto/sexp.rs index 04fefd27..8bd958a6 100644 --- a/openpgp/src/crypto/sexp.rs +++ b/openpgp/src/crypto/sexp.rs @@ -51,7 +51,7 @@ impl Sexp { Sexp::String("rsa".into()), Sexp::List(vec![ Sexp::String("a".into()), - Sexp::String(c.value.as_ref().into())])])])), + Sexp::String(c.value().into())])])])), &Elgamal { ref e, ref c } => Ok(Sexp::List(vec![ @@ -60,10 +60,10 @@ impl Sexp { Sexp::String("elg".into()), Sexp::List(vec![ Sexp::String("a".into()), - Sexp::String(e.value.as_ref().into())]), + Sexp::String(e.value().into())]), Sexp::List(vec![ Sexp::String("b".into()), - Sexp::String(c.value.as_ref().into())])])])), + Sexp::String(c.value().into())])])])), &ECDH { ref e, ref key } => Ok(Sexp::List(vec![ @@ -75,7 +75,7 @@ impl Sexp { Sexp::String(key.as_ref().into())]), Sexp::List(vec![ Sexp::String("e".into()), - Sexp::String(e.value.as_ref().into())])])])), + Sexp::String(e.value().into())])])])), &Unknown { .. } => Err(Error::InvalidArgument( |