Botan 2.19.5 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

  rng | the rng to use
  key | the key to test
  padding | the encryption padding method to use

Returns
  true if consistent otherwise false

Definition at line 58 of file keypair.h.

61 {
62 return encryption_consistency_check(rng, key, key, padding);
63 }
Botan::KeyPair::encryption_consistency_check
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.h:58

References encryption_consistency_check().

Referenced by 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

  rng | the rng to use
  private_key | the key to test
  public_key | the key to test
  padding | the encryption padding method to use

Returns
  true if consistent otherwise false

Definition at line 19 of file keypair.cpp.

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 }
Botan::PK_Decryptor_EME
Definition: pubkey.h:580
Botan::PK_Encryptor_EME
Definition: pubkey.h:530
Botan::RandomNumberGenerator::random_vec
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:143
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:72

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().

## ◆ 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

  rng | the rng to use
  key | the key to test
  padding | the signature padding method to use

Returns
  true if consistent otherwise false

Definition at line 74 of file keypair.h.

77 {
78 return signature_consistency_check(rng, key, key, padding);
79 }
Botan::KeyPair::signature_consistency_check
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.h:74

References signature_consistency_check().

Referenced by 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

  rng | the rng to use
  private_key | the key to test
  public_key | the key to test
  padding | the signature padding method to use

Returns
  true if consistent otherwise false

Definition at line 49 of file keypair.cpp.

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 }
Botan::Encoding_Error
Definition: exceptn.h:185
Botan::PK_Signer
Definition: pubkey.h:167
Botan::PK_Verifier
Definition: pubkey.h:299
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0

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(), and Botan::SM2_PrivateKey::check_key().