aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/keypair/keypair.cpp
blob: 486577fc55f382eb20c3ce03b8e83dfcdf766dbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Keypair Checks
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/keypair.h>
#include <botan/look_pk.h>
#include <memory>

namespace Botan {

namespace KeyPair {

/*
* Check an encryption key pair for consistency
*/
void check_key(RandomNumberGenerator& rng,
               PK_Encryptor* encryptor, PK_Decryptor* decryptor)
   {
   if(encryptor->maximum_input_size() == 0)
      return;

   std::auto_ptr<PK_Encryptor> enc(encryptor);
   std::auto_ptr<PK_Decryptor> dec(decryptor);

   SecureVector<byte> message(enc->maximum_input_size() - 1);
   rng.randomize(message, message.size());

   SecureVector<byte> ciphertext = enc->encrypt(message, rng);
   if(ciphertext == message)
      throw Self_Test_Failure("Encryption key pair consistency failure");

   SecureVector<byte> message2 = dec->decrypt(ciphertext);
   if(message != message2)
      throw Self_Test_Failure("Encryption key pair consistency failure");
   }

/*
* Check a signature key pair for consistency
*/
void check_key(RandomNumberGenerator& rng,
               PK_Signer* signer, PK_Verifier* verifier)
   {
   std::auto_ptr<PK_Signer> sig(signer);
   std::auto_ptr<PK_Verifier> ver(verifier);

   SecureVector<byte> message(16);
   rng.randomize(message, message.size());

   SecureVector<byte> signature;

   try
      {
      signature = sig->sign_message(message, rng);
      }
   catch(Encoding_Error)
      {
      return;
      }

   if(!ver->verify_message(message, signature))
      throw Self_Test_Failure("Signature key pair consistency failure");

   ++message[0];
   if(ver->verify_message(message, signature))
      throw Self_Test_Failure("Signature key pair consistency failure");
   }

}

}