• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/src/lib/pubkey/keypair/keypair.cpp
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,
3✔
19
                                  const Private_Key& private_key,
20
                                  const Public_Key& public_key,
21
                                  std::string_view padding) {
22
   PK_Encryptor_EME encryptor(public_key, rng, padding);
3✔
23
   PK_Decryptor_EME decryptor(private_key, rng, padding);
3✔
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)
3✔
30
      return true;
31

32
   std::vector<uint8_t> plaintext;
3✔
33
   rng.random_vec(plaintext, encryptor.maximum_input_size() - 1);
3✔
34

35
   std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, rng);
3✔
36
   if(ciphertext == plaintext)
3✔
37
      return false;
38

39
   std::vector<uint8_t> decrypted = unlock(decryptor.decrypt(ciphertext));
3✔
40

41
   return (plaintext == decrypted);
3✔
42
}
9✔
43

44
/*
45
* Check a signature key pair for consistency
46
*/
47
bool signature_consistency_check(RandomNumberGenerator& rng,
28✔
48
                                 const Private_Key& private_key,
49
                                 const Public_Key& public_key,
50
                                 std::string_view padding) {
51
   PK_Signer signer(private_key, rng, padding);
28✔
52
   PK_Verifier verifier(public_key, padding);
28✔
53

54
   std::vector<uint8_t> message(32);
28✔
55
   rng.randomize(message.data(), message.size());
28✔
56

57
   std::vector<uint8_t> signature;
28✔
58

59
   try {
28✔
60
      signature = signer.sign_message(message, rng);
56✔
61
   } catch(Encoding_Error&) { return false; }
×
62

63
   if(!verifier.verify_message(message, signature))
28✔
64
      return false;
65

66
   // Now try to check a corrupt signature, ensure it does not succeed
67
   ++signature[0];
28✔
68

69
   if(verifier.verify_message(message, signature))
28✔
70
      return false;
×
71

72
   return true;
73
}
56✔
74

75
}  // namespace Botan::KeyPair
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc