Botan 3.0.0-rc1
Crypto and TLS for C&
Functions
Botan::KeyPair Namespace Reference

Functions

bool encryption_consistency_check (RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
 
bool encryption_consistency_check (RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
 
bool signature_consistency_check (RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
 
bool signature_consistency_check (RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
 

Function Documentation

◆ encryption_consistency_check() [1/2]

bool Botan::KeyPair::encryption_consistency_check ( RandomNumberGenerator rng,
const Private_Key key,
const std::string &  padding 
)
inline

Tests whether the key is consistent for encryption; whether encrypting and then decrypting gives to the original plaintext.

Parameters
rngthe rng to use
keythe key to test
paddingthe encryption padding method to use
Returns
true if consistent otherwise false

Definition at line 56 of file keypair.h.

59 {
60 return encryption_consistency_check(rng, key, key, padding);
61 }
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:17

References encryption_consistency_check().

◆ encryption_consistency_check() [2/2]

bool Botan::KeyPair::encryption_consistency_check ( RandomNumberGenerator rng,
const Private_Key private_key,
const Public_Key public_key,
const std::string &  padding 
)

Tests whether the key is consistent for encryption; whether encrypting and then decrypting gives to the original plaintext.

Parameters
rngthe rng to use
private_keythe key to test
public_keythe key to test
paddingthe encryption padding method to use
Returns
true if consistent otherwise false

Definition at line 17 of file keypair.cpp.

21 {
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 std::vector<uint8_t> plaintext;
33 rng.random_vec(plaintext, encryptor.maximum_input_size() - 1);
34
35 std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, rng);
36 if(ciphertext == plaintext)
37 return false;
38
39 std::vector<uint8_t> decrypted = unlock(decryptor.decrypt(ciphertext));
40
41 return (plaintext == decrypted);
42 }
void random_vec(std::span< uint8_t > v)
Definition: rng.h:178
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:77

References Botan::PK_Decryptor::decrypt(), Botan::PK_Encryptor::encrypt(), Botan::PK_Encryptor_EME::maximum_input_size(), Botan::RandomNumberGenerator::random_vec(), and Botan::unlock().

Referenced by Botan::ElGamal_PrivateKey::check_key(), and encryption_consistency_check().

◆ signature_consistency_check() [1/2]

bool Botan::KeyPair::signature_consistency_check ( RandomNumberGenerator rng,
const Private_Key key,
const std::string &  padding 
)
inline

Tests whether the key is consistent for signatures; whether a signature can be created and then verified

Parameters
rngthe rng to use
keythe key to test
paddingthe signature padding method to use
Returns
true if consistent otherwise false

Definition at line 72 of file keypair.h.

75 {
76 return signature_consistency_check(rng, key, key, padding);
77 }
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:47

References signature_consistency_check().

◆ signature_consistency_check() [2/2]

bool Botan::KeyPair::signature_consistency_check ( RandomNumberGenerator rng,
const Private_Key private_key,
const Public_Key public_key,
const std::string &  padding 
)

Tests whether the key is consistent for signatures; whether a signature can be created and then verified

Parameters
rngthe rng to use
private_keythe key to test
public_keythe key to test
paddingthe signature padding method to use
Returns
true if consistent otherwise false

Definition at line 47 of file keypair.cpp.

51 {
52 PK_Signer signer(private_key, rng, padding);
53 PK_Verifier verifier(public_key, padding);
54
55 std::vector<uint8_t> message(32);
56 rng.randomize(message.data(), message.size());
57
58 std::vector<uint8_t> signature;
59
60 try
61 {
62 signature = signer.sign_message(message, rng);
63 }
64 catch(Encoding_Error&)
65 {
66 return false;
67 }
68
69 if(!verifier.verify_message(message, signature))
70 return false;
71
72 // Now try to check a corrupt signature, ensure it does not succeed
73 ++signature[0];
74
75 if(verifier.verify_message(message, signature))
76 return false;
77
78 return true;
79 }
void randomize(std::span< uint8_t > output)
Definition: rng.h:53

References Botan::RandomNumberGenerator::randomize(), Botan::PK_Signer::sign_message(), and Botan::PK_Verifier::verify_message().

Referenced by Botan::DSA_PrivateKey::check_key(), Botan::ECDSA_PrivateKey::check_key(), Botan::ECGDSA_PrivateKey::check_key(), Botan::ECKCDSA_PrivateKey::check_key(), Botan::RSA_PrivateKey::check_key(), Botan::SM2_PrivateKey::check_key(), and signature_consistency_check().