Botan 2.19.1
Crypto and TLS for C&
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
12namespace Botan {
13
14namespace KeyPair {
15
16/*
17* Check an encryption key pair for consistency
18*/
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*/
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}
secure_vector< uint8_t > decrypt(const uint8_t in[], size_t length) const
Definition: pubkey.cpp:17
size_t maximum_input_size() const override
Definition: pubkey.cpp:110
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
Definition: pubkey.h:40
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition: pubkey.h:214
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition: pubkey.cpp:331
virtual void randomize(uint8_t output[], size_t length)=0
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:143
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:49
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:19
Definition: alg_id.cpp:13
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:72