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

randombit / botan / 12965174500

25 Jan 2025 01:02PM UTC coverage: 91.219% (-0.008%) from 91.227%
12965174500

Pull #4592

github

web-flow
Merge 2e35a9bd1 into 79027d241
Pull Request #4592: Some initial refactoring for splitting public and private keys

93633 of 102646 relevant lines covered (91.22%)

11545786.96 hits per line

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

70.97
/src/lib/pubkey/pk_keys.cpp
1
/*
2
* PK Key Types
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/pk_keys.h>
9

10
#include <botan/der_enc.h>
11
#include <botan/hash.h>
12
#include <botan/hex.h>
13
#include <botan/pk_ops.h>
14
#include <botan/internal/fmt.h>
15

16
namespace Botan {
17

18
const BigInt& Asymmetric_Key::get_int_field(std::string_view field) const {
4✔
19
   throw Unknown_PK_Field_Name(algo_name(), field);
8✔
20
}
21

22
OID Asymmetric_Key::object_identifier() const {
3,349✔
23
   try {
3,349✔
24
      return OID::from_string(algo_name());
6,698✔
25
   } catch(Lookup_Error&) {
×
26
      throw Lookup_Error(fmt("Public key algorithm {} has no defined OIDs", algo_name()));
×
27
   }
×
28
}
29

30
Signature_Format Asymmetric_Key::_default_x509_signature_format() const {
16,530✔
31
   if(_signature_element_size()) {
16,530✔
32
      return Signature_Format::DerSequence;
33
   } else {
34
      return Signature_Format::Standard;
13,412✔
35
   }
36
}
37

38
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, std::string_view hash_name) {
37,166✔
39
   auto hash_fn = HashFunction::create_or_throw(hash_name);
37,166✔
40
   const std::string hex_hash = hex_encode(hash_fn->process(bits, bits_len));
74,332✔
41

42
   std::string fprint;
37,166✔
43

44
   for(size_t i = 0; i != hex_hash.size(); i += 2) {
1,003,686✔
45
      if(i != 0) {
966,520✔
46
         fprint.push_back(':');
929,354✔
47
      }
48

49
      fprint.push_back(hex_hash[i]);
966,520✔
50
      fprint.push_back(hex_hash[i + 1]);
966,520✔
51
   }
52

53
   return fprint;
37,166✔
54
}
74,332✔
55

56
std::vector<uint8_t> Public_Key::subject_public_key() const {
2,284✔
57
   std::vector<uint8_t> output;
2,284✔
58

59
   DER_Encoder(output)
2,284✔
60
      .start_sequence()
2,284✔
61
      .encode(algorithm_identifier())
4,568✔
62
      .encode(public_key_bits(), ASN1_Type::BitString)
4,568✔
63
      .end_cons();
2,284✔
64

65
   return output;
2,284✔
66
}
×
67

68
secure_vector<uint8_t> Private_Key::private_key_info() const {
1,623✔
69
   const size_t PKCS8_VERSION = 0;
1,623✔
70

71
   return DER_Encoder()
1,623✔
72
      .start_sequence()
1,623✔
73
      .encode(PKCS8_VERSION)
1,623✔
74
      .encode(pkcs8_algorithm_identifier())
3,246✔
75
      .encode(private_key_bits(), ASN1_Type::OctetString)
3,246✔
76
      .end_cons()
1,623✔
77
      .get_contents();
3,246✔
78
}
79

80
secure_vector<uint8_t> Private_Key::raw_private_key_bits() const {
1✔
81
   throw Not_Implemented(algo_name() + " does not implement raw_private_key_bits");
3✔
82
}
83

84
/*
85
* Hash of the X.509 subjectPublicKey encoding
86
*/
87
std::string Public_Key::fingerprint_public(std::string_view hash_algo) const {
18✔
88
   return create_hex_fingerprint(subject_public_key(), hash_algo);
36✔
89
}
90

91
/*
92
* Hash of the PKCS #8 encoding for this key object
93
*/
94
std::string Private_Key::fingerprint_private(std::string_view hash_algo) const {
16✔
95
   return create_hex_fingerprint(private_key_bits(), hash_algo);
32✔
96
}
97

98
std::unique_ptr<PK_Ops::Encryption> Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/,
×
99
                                                                     std::string_view /*params*/,
100
                                                                     std::string_view /*provider*/) const {
101
   throw Lookup_Error(fmt("{} does not support encryption", algo_name()));
×
102
}
103

104
std::unique_ptr<PK_Ops::KEM_Encryption> Public_Key::create_kem_encryption_op(std::string_view /*params*/,
×
105
                                                                             std::string_view /*provider*/) const {
106
   throw Lookup_Error(fmt("{} does not support KEM encryption", algo_name()));
×
107
}
108

109
std::unique_ptr<PK_Ops::Verification> Public_Key::create_verification_op(std::string_view /*params*/,
×
110
                                                                         std::string_view /*provider*/) const {
111
   throw Lookup_Error(fmt("{} does not support verification", algo_name()));
×
112
}
113

114
std::unique_ptr<PK_Ops::Verification> Public_Key::create_x509_verification_op(const AlgorithmIdentifier& /*params*/,
1✔
115
                                                                              std::string_view /*provider*/) const {
116
   throw Lookup_Error(fmt("{} does not support X.509 verification", algo_name()));
1✔
117
}
118

119
std::unique_ptr<PK_Ops::Decryption> Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/,
×
120
                                                                      std::string_view /*params*/,
121
                                                                      std::string_view /*provider*/) const {
122
   throw Lookup_Error(fmt("{} does not support decryption", algo_name()));
×
123
}
124

125
std::unique_ptr<PK_Ops::KEM_Decryption> Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
×
126
                                                                              std::string_view /*params*/,
127
                                                                              std::string_view /*provider*/) const {
128
   throw Lookup_Error(fmt("{} does not support KEM decryption", algo_name()));
×
129
}
130

131
std::unique_ptr<PK_Ops::Signature> Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/,
×
132
                                                                    std::string_view /*params*/,
133
                                                                    std::string_view /*provider*/) const {
134
   throw Lookup_Error(fmt("{} does not support signatures", algo_name()));
×
135
}
136

137
std::unique_ptr<PK_Ops::Key_Agreement> Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
×
138
                                                                            std::string_view /*params*/,
139
                                                                            std::string_view /*provider*/) const {
140
   throw Lookup_Error(fmt("{} does not support key agreement", algo_name()));
×
141
}
142

143
}  // namespace Botan
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