Botan 1.10.17

keypair.cpp
Go to the documentation of this file.
1 /*
2 * Keypair Checks
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
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 */
18 bool encryption_consistency_check(RandomNumberGenerator& rng,
19 const Private_Key& key,
20 const std::string& padding)
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 SecureVector<byte> plaintext =
33 rng.random_vec(encryptor.maximum_input_size() - 1);
34
35 SecureVector<byte> ciphertext = encryptor.encrypt(plaintext, rng);
36 if(ciphertext == plaintext)
37 return false;
38
39 SecureVector<byte> decrypted = decryptor.decrypt(ciphertext);
40
41 return (plaintext == decrypted);
42 }
43
44 /*
45 * Check a signature key pair for consistency
46 */
47 bool signature_consistency_check(RandomNumberGenerator& rng,
48 const Private_Key& key,
49 const std::string& padding)
50 {
51 PK_Signer signer(key, padding);
52 PK_Verifier verifier(key, padding);
53
54 SecureVector<byte> message = rng.random_vec(16);
55
56 SecureVector<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 }
78
79 }
80
81 }
Botan::KeyPair::signature_consistency_check
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.cpp:47
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::Private_Key
Definition: pk_keys.h:86
Botan::PK_Encryptor_EME::maximum_input_size
size_t maximum_input_size() const
Definition: pubkey.cpp:75
Botan::PK_Encryptor::encrypt
SecureVector< byte > encrypt(const byte in[], size_t length, RandomNumberGenerator &rng) const
Definition: pubkey.h:48
Botan::PK_Signer::sign_message
SecureVector< byte > sign_message(const byte in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.cpp:160
Botan::PK_Decryptor::decrypt
SecureVector< byte > decrypt(const byte in[], size_t length) const
Definition: pubkey.h:94
Botan::PK_Decryptor_EME
Definition: pubkey.h:423
Botan::KeyPair::encryption_consistency_check
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.cpp:18
Botan::PK_Verifier::verify_message
bool verify_message(const byte msg[], size_t msg_length, const byte sig[], size_t sig_length)
Definition: pubkey.cpp:283
Botan
Definition: algo_base.h:14
Botan::PK_Verifier
Definition: pubkey.h:211
Botan::PK_Encryptor_EME
Definition: pubkey.h:398
Botan::SecureVector< byte >
Botan::PK_Signer
Definition: pubkey.h:123
Botan::Encoding_Error
Definition: exceptn.h:129