Botan  2.1.0
Crypto and TLS for C++11
keypair.cpp
Go to the documentation of this file.
1 /*
2 * Keypair Checks
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/keypair.h>
9 #include <botan/pubkey.h>
10 
11 namespace Botan {
12 
13 namespace KeyPair {
14 
15 /*
16 * Check an encryption key pair for consistency
17 */
19  const Private_Key& private_key,
20  const Public_Key& public_key,
21  const std::string& padding)
22  {
23  PK_Encryptor_EME encryptor(public_key, rng, padding);
24  PK_Decryptor_EME decryptor(private_key, rng, padding);
25 
26  /*
27  Weird corner case, if the key is too small to encrypt anything at
28  all. This can happen with very small RSA keys with PSS
29  */
30  if(encryptor.maximum_input_size() == 0)
31  return true;
32 
33  std::vector<uint8_t> plaintext =
34  unlock(rng.random_vec(encryptor.maximum_input_size() - 1));
35 
36  std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, rng);
37  if(ciphertext == plaintext)
38  return false;
39 
40  std::vector<uint8_t> decrypted = unlock(decryptor.decrypt(ciphertext));
41 
42  return (plaintext == decrypted);
43  }
44 
45 /*
46 * Check a signature key pair for consistency
47 */
49  const Private_Key& private_key,
50  const Public_Key& public_key,
51  const std::string& padding)
52  {
53  PK_Signer signer(private_key, rng, padding);
54  PK_Verifier verifier(public_key, padding);
55 
56  std::vector<uint8_t> message(32);
57  rng.randomize(message.data(), message.size());
58 
59  std::vector<uint8_t> signature;
60 
61  try
62  {
63  signature = signer.sign_message(message, rng);
64  }
65  catch(Encoding_Error&)
66  {
67  return false;
68  }
69 
70  if(!verifier.verify_message(message, signature))
71  return false;
72 
73  // Now try to check a corrupt signature, ensure it does not succeed
74  ++signature[0];
75 
76  if(verifier.verify_message(message, signature))
77  return false;
78 
79  return true;
80  }
81 
82 }
83 
84 }
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:133
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:18
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition: pubkey.cpp:280
virtual void randomize(uint8_t output[], size_t length)=0
secure_vector< uint8_t > decrypt(const uint8_t in[], size_t length) const
Definition: pubkey.cpp:16
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.h:209
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
Definition: pubkey.h:46
size_t maximum_input_size() const override
Definition: pubkey.cpp:104
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:48
Definition: alg_id.cpp:13
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125