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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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

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

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

43
   return (plaintext == decrypted);
3✔
44
}
9✔
45

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

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

59
   std::vector<uint8_t> signature;
28✔
60

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

65
   if(!verifier.verify_message(message, signature)) {
28✔
66
      return false;
67
   }
68

69
   // Now try to check a corrupt signature, ensure it does not succeed
70
   ++signature[0];
28✔
71

72
   if(verifier.verify_message(message, signature)) {
28✔
73
      return false;
×
74
   }
75

76
   return true;
77
}
56✔
78

79
}  // 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