Botan  1.11.15
Functions
Botan::KeyPair Namespace Reference

Functions

bool encryption_consistency_check (RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
bool signature_consistency_check (RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)

Function Documentation

BOTAN_DLL bool Botan::KeyPair::encryption_consistency_check ( RandomNumberGenerator &  rng,
const Private_Key &  key,
const std::string &  padding 
)

Tests whether the key is consistent for encryption; whether encrypting and then decrypting gives to the original plaintext.

Parameters:
rngthe rng to use
keythe key to test
paddingthe encryption padding method to use
Returns:
true if consistent otherwise false

Definition at line 18 of file keypair.cpp.

References Botan::PK_Decryptor::decrypt(), Botan::PK_Encryptor::encrypt(), Botan::PK_Encryptor_EME::maximum_input_size(), Botan::RandomNumberGenerator::random_vec(), and Botan::unlock().

Referenced by Botan::ElGamal_PrivateKey::check_key().

   {
   PK_Encryptor_EME encryptor(key, padding);
   PK_Decryptor_EME decryptor(key, padding);

   /*
   Weird corner case, if the key is too small to encrypt anything at
   all. This can happen with very small RSA keys with PSS
   */
   if(encryptor.maximum_input_size() == 0)
      return true;

   std::vector<byte> plaintext =
      unlock(rng.random_vec(encryptor.maximum_input_size() - 1));

   std::vector<byte> ciphertext = encryptor.encrypt(plaintext, rng);
   if(ciphertext == plaintext)
      return false;

   std::vector<byte> decrypted = unlock(decryptor.decrypt(ciphertext));

   return (plaintext == decrypted);
   }
BOTAN_DLL bool Botan::KeyPair::signature_consistency_check ( RandomNumberGenerator &  rng,
const Private_Key &  key,
const std::string &  padding 
)

Tests whether the key is consistent for signatures; whether a signature can be created and then verified

Parameters:
rngthe rng to use
keythe key to test
paddingthe signature padding method to use
Returns:
true if consistent otherwise false

Definition at line 47 of file keypair.cpp.

References Botan::RandomNumberGenerator::random_vec(), Botan::PK_Signer::sign_message(), Botan::unlock(), and Botan::PK_Verifier::verify_message().

Referenced by Botan::NR_PrivateKey::check_key(), Botan::RSA_PrivateKey::check_key(), Botan::DSA_PrivateKey::check_key(), Botan::RW_PrivateKey::check_key(), and Botan::ECDSA_PrivateKey::check_key().

   {
   PK_Signer signer(key, padding);
   PK_Verifier verifier(key, padding);

   std::vector<byte> message = unlock(rng.random_vec(16));

   std::vector<byte> signature;

   try
      {
      signature = signer.sign_message(message, rng);
      }
   catch(Encoding_Error)
      {
      return false;
      }

   if(!verifier.verify_message(message, signature))
      return false;

   // Now try to check a corrupt signature, ensure it does not succeed
   ++message[0];

   if(verifier.verify_message(message, signature))
      return false;

   return true;
   }