Botan 3.6.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/internal/keypair.h>
9
10#include <botan/pubkey.h>
11#include <botan/rng.h>
12
13namespace Botan::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 std::string_view padding) {
22 PK_Encryptor_EME encryptor(public_key, rng, padding);
23 PK_Decryptor_EME decryptor(private_key, rng, 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
33 std::vector<uint8_t> plaintext;
34 rng.random_vec(plaintext, 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
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 std::string_view padding) {
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 signature = signer.sign_message(message, rng);
63 } catch(Encoding_Error&) {
64 return false;
65 }
66
67 if(!verifier.verify_message(message, signature)) {
68 return false;
69 }
70
71 // Now try to check a corrupt signature, ensure it does not succeed
72 ++signature[0];
73
74 if(verifier.verify_message(message, signature)) {
75 return false;
76 }
77
78 return true;
79}
80
81} // namespace Botan::KeyPair
secure_vector< uint8_t > decrypt(const uint8_t in[], size_t length) const
Definition pubkey.cpp:23
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:37
std::vector< uint8_t > sign_message(const uint8_t in[], size_t length, RandomNumberGenerator &rng)
Definition pubkey.h:186
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
Definition pubkey.cpp:371
void randomize(std::span< uint8_t > output)
Definition rng.h:53
void random_vec(std::span< uint8_t > v)
Definition rng.h:180
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:18
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:75