Botan  1.11.10
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().

21  {
22  PK_Encryptor_EME encryptor(key, padding);
23  PK_Decryptor_EME decryptor(key, padding);
24 
25  /*
26  Weird corner case, if the key is too small to encrypt anything at
27  all. This can happen with very small RSA keys with PSS
28  */
29  if(encryptor.maximum_input_size() == 0)
30  return true;
31 
32  std::vector<byte> plaintext =
33  unlock(rng.random_vec(encryptor.maximum_input_size() - 1));
34 
35  std::vector<byte> ciphertext = encryptor.encrypt(plaintext, rng);
36  if(ciphertext == plaintext)
37  return false;
38 
39  std::vector<byte> decrypted = unlock(decryptor.decrypt(ciphertext));
40 
41  return (plaintext == decrypted);
42  }
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95
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().

50  {
51  PK_Signer signer(key, padding);
52  PK_Verifier verifier(key, padding);
53 
54  std::vector<byte> message = unlock(rng.random_vec(16));
55 
56  std::vector<byte> signature;
57 
58  try
59  {
60  signature = signer.sign_message(message, rng);
61  }
62  catch(Encoding_Error)
63  {
64  return false;
65  }
66 
67  if(!verifier.verify_message(message, signature))
68  return false;
69 
70  // Now try to check a corrupt signature, ensure it does not succeed
71  ++message[0];
72 
73  if(verifier.verify_message(message, signature))
74  return false;
75 
76  return true;
77  }
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95