llcrypto: Define a "ValidatableSignature" type

This type wraps a signature, a public key, and the signed object.
It's meant to be useful for implementing SelfSigned objects.
This commit is contained in:
Nick Mathewson 2020-09-03 09:23:10 -04:00
parent 481e5e5a63
commit 4ec97aa613
3 changed files with 68 additions and 1 deletions

View File

@ -26,10 +26,10 @@ ed25519-dalek = "1.0.0"
sha-1 = "0.9.1"
sha2 = "0.9.1"
sha3 = "0.9.1"
signature = "1.2.2"
[dev-dependencies]
hex-literal = "0.3.1"
signature = "1.2.2"
# This is a magic crate that runs the tests and checks the format
# before it lets you commit or push. It installs git hooks for this whenever

View File

@ -21,4 +21,44 @@ pub mod curve25519 {
/// uses the ed25519 trait and the Signature trait.
pub mod ed25519 {
pub use ed25519_dalek::{ExpandedSecretKey, Keypair, PublicKey, SecretKey, Signature};
/// An ed25519 signature, plus the document that it signs and its
/// public key.
pub struct ValidatableEd25159Signature {
key: PublicKey,
sig: Signature,
// TODO: It's not so good to have this included here; it would
// be better to have a patch to ed25519_dalek to pre-hash this.
entire_text_of_signed_thing: Vec<u8>,
}
impl ValidatableEd25159Signature {
/// Create a new ValidatableEd25519Signature
pub fn new(key: PublicKey, sig: &[u8], text: &[u8]) -> Result<Self, signature::Error> {
use std::convert::TryInto;
Ok(ValidatableEd25159Signature {
key,
sig: sig.try_into()?,
entire_text_of_signed_thing: text.into(),
})
}
}
impl super::ValidatableSignature for ValidatableEd25159Signature {
fn is_valid(&self) -> bool {
use signature::Verifier;
self.key
.verify(&self.entire_text_of_signed_thing[..], &self.sig)
.is_ok()
}
}
}
/// Type for a validatable signature.
///
/// It necessarily includes the signature, the public key, and (a hash
/// of?) the document being checked.
pub trait ValidatableSignature {
/// Check whether this signature is a correct signature for the document.
fn is_valid(&self) -> bool;
}

View File

@ -73,6 +73,7 @@ impl RSAIdentity {
///
/// This implementation is a simple wrapper so that we can define new
/// methods and traits on the type.
#[derive(Clone)]
pub struct PublicKey(rsa::RSAPublicKey);
/// An RSA private key.
pub struct PrivateKey(rsa::RSAPrivateKey);
@ -180,3 +181,29 @@ impl PublicKey {
RSAIdentity { id }
}
}
/// blah blah
pub struct ValidatableRSASignature {
key: PublicKey,
sig: Vec<u8>,
expected_hash: Vec<u8>,
}
impl ValidatableRSASignature {
/// klasjdkljsd
pub fn new(key: &PublicKey, sig: &[u8], expected_hash: &[u8]) -> Self {
ValidatableRSASignature {
key: key.clone(),
sig: sig.into(),
expected_hash: expected_hash.into(),
}
}
}
impl super::ValidatableSignature for ValidatableRSASignature {
fn is_valid(&self) -> bool {
self.key
.verify(&self.expected_hash[..], &self.sig[..])
.is_ok()
}
}