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
11namespace Botan {
12
13namespace KeyPair {
14
15/*
16* Check an encryption key pair for consistency
17*/
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*/
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}
SecureVector< byte > decrypt(const byte in[], size_t length) const
Definition pubkey.h:94
size_t maximum_input_size() const
Definition pubkey.cpp:75
SecureVector< byte > encrypt(const byte in[], size_t length, RandomNumberGenerator &rng) const
Definition pubkey.h:48
SecureVector< byte > sign_message(const byte in[], size_t length, RandomNumberGenerator &rng)
Definition pubkey.cpp:160
bool verify_message(const byte msg[], size_t msg_length, const byte sig[], size_t sig_length)
Definition pubkey.cpp:283
SecureVector< byte > random_vec(size_t bytes)
Definition rng.h:40
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition keypair.cpp:18
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition keypair.cpp:47