Botan 3.6.1
Crypto and TLS for C&
Botan::KeyPair Namespace Reference

Functions

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

Function Documentation

◆ encryption_consistency_check() [1/2]

bool Botan::KeyPair::encryption_consistency_check ( RandomNumberGenerator & rng,
const Private_Key & key,
std::string_view 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 51 of file keypair.h.

51 {
52 return encryption_consistency_check(rng, key, key, padding);
53}
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:18

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,
std::string_view 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 18 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
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}
void random_vec(std::span< uint8_t > v)
Definition rng.h:180

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,
std::string_view 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 63 of file keypair.h.

63 {
64 return signature_consistency_check(rng, key, key, padding);
65}
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49

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,
std::string_view 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 49 of file keypair.cpp.

52 {
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}
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().