Coverage Report

Created: 2024-06-28 06:39

/src/botan/src/lib/pubkey/keypair/keypair.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Keypair Checks
3
* (C) 1999-2010 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/keypair.h>
9
10
#include <botan/pubkey.h>
11
#include <botan/rng.h>
12
13
namespace Botan::KeyPair {
14
15
/*
16
* Check an encryption key pair for consistency
17
*/
18
bool encryption_consistency_check(RandomNumberGenerator& rng,
19
                                  const Private_Key& private_key,
20
                                  const Public_Key& public_key,
21
0
                                  std::string_view padding) {
22
0
   PK_Encryptor_EME encryptor(public_key, rng, padding);
23
0
   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
0
   if(encryptor.maximum_input_size() == 0) {
30
0
      return true;
31
0
   }
32
33
0
   std::vector<uint8_t> plaintext;
34
0
   rng.random_vec(plaintext, encryptor.maximum_input_size() - 1);
35
36
0
   std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, rng);
37
0
   if(ciphertext == plaintext) {
38
0
      return false;
39
0
   }
40
41
0
   std::vector<uint8_t> decrypted = unlock(decryptor.decrypt(ciphertext));
42
43
0
   return (plaintext == decrypted);
44
0
}
45
46
/*
47
* Check a signature key pair for consistency
48
*/
49
bool signature_consistency_check(RandomNumberGenerator& rng,
50
                                 const Private_Key& private_key,
51
                                 const Public_Key& public_key,
52
0
                                 std::string_view padding) {
53
0
   PK_Signer signer(private_key, rng, padding);
54
0
   PK_Verifier verifier(public_key, padding);
55
56
0
   std::vector<uint8_t> message(32);
57
0
   rng.randomize(message.data(), message.size());
58
59
0
   std::vector<uint8_t> signature;
60
61
0
   try {
62
0
      signature = signer.sign_message(message, rng);
63
0
   } catch(Encoding_Error&) {
64
0
      return false;
65
0
   }
66
67
0
   if(!verifier.verify_message(message, signature)) {
68
0
      return false;
69
0
   }
70
71
   // Now try to check a corrupt signature, ensure it does not succeed
72
0
   ++signature[0];
73
74
0
   if(verifier.verify_message(message, signature)) {
75
0
      return false;
76
0
   }
77
78
0
   return true;
79
0
}
80
81
}  // namespace Botan::KeyPair