Botan: Botan::KeyPair Namespace Reference

Botan 1.10.17

Functions
Botan::KeyPair Namespace Reference

## Functions
bool | encryption_consistency_check (RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
bool | signature_consistency_check (RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)

## Function Documentation

## ◆ encryption_consistency_check()

BOTAN_DLL bool Botan::KeyPair::encryption_consistency_check | ( | RandomNumberGenerator & | rng,
|  | const Private_Key & | 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
  key | the key to test
  padding | the 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(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 }

## ◆ signature_consistency_check()

BOTAN_DLL bool Botan::KeyPair::signature_consistency_check | ( | RandomNumberGenerator & | rng,
|  | const Private_Key & | 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
  key | the key to test
  padding | the signature padding method to use

Returns
  true if consistent otherwise false

Definition at line 47 of file keypair.cpp.

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 }