Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 #include <botan/rng.h>
11
12 namespace Botan {
13
14 namespace KeyPair {
15
16 /*
17 * Check an encryption key pair for consistency
18 */
19 bool encryption_consistency_check(RandomNumberGenerator& rng,
20 const Private_Key& private_key,
21 const Public_Key& public_key,
22 const std::string& padding)
23 {
24 PK_Encryptor_EME encryptor(public_key, rng, padding);
25 PK_Decryptor_EME decryptor(private_key, rng, padding);
26
27 /*
28 Weird corner case, if the key is too small to encrypt anything at
29 all. This can happen with very small RSA keys with PSS
30 */
31 if(encryptor.maximum_input_size() == 0)
32 return true;
33
34 std::vector<uint8_t> plaintext;
35 rng.random_vec(plaintext, encryptor.maximum_input_size() - 1);
36
37 std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, rng);
38 if(ciphertext == plaintext)
39 return false;
40
41 std::vector<uint8_t> decrypted = unlock(decryptor.decrypt(ciphertext));
42
43 return (plaintext == decrypted);
44 }
45
46 /*
47 * Check a signature key pair for consistency
48 */
49 bool signature_consistency_check(RandomNumberGenerator& rng,
50 const Private_Key& private_key,
51 const Public_Key& public_key,
52 const std::string& padding)
53 {
54 PK_Signer signer(private_key, rng, padding);
55 PK_Verifier verifier(public_key, padding);
56
57 std::vector<uint8_t> message(32);
58 rng.randomize(message.data(), message.size());
59
60 std::vector<uint8_t> signature;
61
62 try
63 {
64 signature = signer.sign_message(message, rng);
65 }
66 catch(Encoding_Error&)
67 {
68 return false;
69 }
70
71 if(!verifier.verify_message(message, signature))
72 return false;
73
74 // Now try to check a corrupt signature, ensure it does not succeed
75 ++signature[0];
76
77 if(verifier.verify_message(message, signature))
78 return false;
79
80 return true;
81 }
82
83 }
84
85 }
Botan::RandomNumberGenerator::random_vec
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:141
Botan::PK_Verifier::verify_message
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition: pubkey.cpp:324
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0
Botan::Private_Key
Definition: pk_keys.h:181
Botan::PK_Decryptor::decrypt
secure_vector< uint8_t > decrypt(const uint8_t in[], size_t length) const
Definition: pubkey.cpp:17
Botan::KeyPair::encryption_consistency_check
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:19
Botan::PK_Signer::sign_message
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.h:214
Botan::KeyPair::signature_consistency_check
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:49
Botan::PK_Encryptor::encrypt
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
Definition: pubkey.h:40
Botan::PK_Decryptor_EME
Definition: pubkey.h:579
Botan::PK_Encryptor_EME::maximum_input_size
size_t maximum_input_size() const override
Definition: pubkey.cpp:110
Botan
Definition: alg_id.cpp:13
Botan::PK_Verifier
Definition: pubkey.h:298
Botan::PK_Encryptor_EME
Definition: pubkey.h:529
Botan::Public_Key
Definition: pk_keys.h:29
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:72
Botan::Encoding_Error
Definition: exceptn.h:184
Botan::PK_Signer
Definition: pubkey.h:166