Digital Signature

From Crypto++ Wiki

Jump to: navigation, search

Signature Schemes are used to:

  • Confirm Validity
  • Approve a Document
  • Declare Ownership or Authorship

Contents

Creating a Signature

A Public Key Cryptosystem can be used as a Signature Scheme as follows:

Verifying a Signature

A Public Key Cryptosystem would verify a signature by:

  • Encrypt the signature
  • Verify the resulting hash with that of the original document

Examples

ESIGN Signature and Verification

// Quote of the Day by Francis Jeffrey
std::string message( "Opinions founded on prejudice are always\n " \
                     "sustained with the greatest of violence" );

///////////////////////////////////////
// Pseudo Random Number Generator
AutoSeededRandomPool rng;
 
///////////////////////////////////////
// Key Generation
InvertibleESIGNFunction keys;

// Modulus size should must be a multiple of 3
//   k = 32 by default
keys.GenerateRandomWithKeySize( rng, 384 * 3 );

///////////////////////////////////////
// Signature
ESIGN< SHA >::Signer signer( keys );

// Set up for SignMessage()
size_t length = signer.MaxSignatureLength();
byte* signature = new byte[ length ];
if( NULL == signature ) { return -1; }

// Sign...
signer.SignMessage( rng, (const byte*) message.c_str(),
    message.length(), signature );

///////////////////////////////////////
// Verification
ESIGN< SHA >::Verifier verifier( signer );
bool result = verifier.VerifyMessage( (const byte*)message.c_str(),
    message.length(), signature, signer.SignatureLength() );

///////////////////////////////////////
// Result
if( true == result )
{
    cout << "Message Verified" << endl;
}
else
{
    cout << "Message Verification Failed" << endl;
}

if( NULL != signature ) { delete[] signature; }

RSA Signature

CryptoPP::AutoSeededRandomPool rng;
std::string message = "Yoda said, Do or Do Not. There is not try.";

// Input: Private Key
std::string PrivateKeyFile = "key.pv";
      
// Output: Signed Message M
std::string SignedFile = "message.sig";

CryptoPP::FileSource privFile( PrivateKeyFile.c_str(), true,
   new CryptoPP::HexDecoder);
CryptoPP::RSASSA_PKCS1v15_SHA_Signer priv(privFile);
      
// Sign Away...
CryptoPP::StringSource( message, true,
   new CryptoPP::SignerFilter( rng, priv,
      new CryptoPP::HexEncoder(
         new CryptoPP::FileSink( SignedFile.c_str() )
      ) // HexEncoder
   ) // SignerFilter
); // StringSource

RSA Signature Verification

std::string PublicKeyFile = "key.pb";
std::string SignatureFile = "message.sig";
std::string message = "Yoda said, Do or Do Not. There is no try.";

// Inputs
CryptoPP::FileSource pubFile( PublicKeyFile.c_str(), true
   new CryptoPP::HexDecoder );
CryptoPP::FileSource SignatureFile( SignatureFile.c_str()
   true, new CryptoPP::HexDecoder);

// Verifier Object
CryptoPP::RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);

// Sanity Check
if (SignatureFile.MaxRetrievable() != pub.SignatureLength )
   { throw error; }

CryptoPP::SecByteBlock signature( pub.SignatureLength() )
SignatureFile.Get( signature, signature.size() );

// Setup
CryptoPP::VerifierFilter *verifierFilter =
   new CryptoPP::VerifierFilter(pub);
verifierFilter->Put(signature, pub.SignatureLength());

// Invoke Verifier
CryptoPP::StringSource( message, true, verifierFilter );

// Paydirt
if( false == verifierFilter->GetLastResult() )
   { throw error; }

Downloads

ESIGN Signature and Verification

ESIGN.zip - Demonstrates ESIGN Signature and Verification - 4.0 Kb

RSA Signature

RSASign.zip - Loads a DER and Hex Encoded Private Key from disk, signs a std::string message, and then writes the signature to disk after Hex Encoding.

RSA Signature Verification

RSASignVer.zip - Loads a Hex encoded Signature from disk, loads a DER and Hex Encoded Public Key from disk, and then verifies the signature.

RSA Probabilistic Signature Scheme with Recovery

RSAPSSR.zip - Demonstrates PSSR using RSA with OAEP and BER Encoded Keys - 3.6Kb

Personal tools