diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-07-31 17:26:46 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-07-31 17:58:01 +0200 |
commit | 690ca36b3a3f61f88be25f9c86f8b55ab4562ea9 (patch) | |
tree | 508ee361d8a8c0eee031db5af1c87d09457b6145 | |
parent | c078883804835ea3ec975b9e8beee8cae7f367c1 (diff) |
crypto: Implement (Partial)Ord, (Partial)Eq for ProtectedMPI.
-rw-r--r-- | openpgp/src/crypto/mpi.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs index 6b5bec4b..120572dc 100644 --- a/openpgp/src/crypto/mpi.rs +++ b/openpgp/src/crypto/mpi.rs @@ -281,6 +281,26 @@ impl From<MPI> for ProtectedMPI { } } +impl PartialOrd for ProtectedMPI { + fn partial_cmp(&self, other: &ProtectedMPI) -> Option<Ordering> { + Some(self.secure_memcmp(other)) + } +} + +impl Ord for ProtectedMPI { + fn cmp(&self, other: &ProtectedMPI) -> Ordering { + self.partial_cmp(other).unwrap() + } +} + +impl PartialEq for ProtectedMPI { + fn eq(&self, other: &ProtectedMPI) -> bool { + self.cmp(other) == Ordering::Equal + } +} + +impl Eq for ProtectedMPI {} + impl std::hash::Hash for ProtectedMPI { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.value.hash(state); @@ -299,6 +319,14 @@ impl ProtectedMPI { pub fn value(&self) -> &[u8] { &self.value } + + /// Securely compares two MPIs in constant time. + fn secure_memcmp(&self, other: &Self) -> Ordering { + (self.value.len() as i32).cmp(&(other.value.len() as i32)) + .then( + // Protected compares in constant time. + self.value.cmp(&other.value)) + } } impl fmt::Debug for ProtectedMPI { |