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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

93.1
/src/lib/pubkey/pk_ops.cpp
1
/*
2
* PK Operation Types
3
* (C) 2010,2015,2023 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/internal/pk_ops_impl.h>
9

10
#include <botan/hash.h>
11
#include <botan/rng.h>
12
#include <botan/internal/bit_ops.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/parsing.h>
15
#include <botan/internal/scan_name.h>
16
#include <sstream>
17

18
#if defined(BOTAN_HAS_RAW_HASH_FN)
19
   #include <botan/internal/raw_hash.h>
20
#endif
21

22
namespace Botan {
23

24
AlgorithmIdentifier PK_Ops::Signature::algorithm_identifier() const {
×
25
   throw Not_Implemented("This signature scheme does not have an algorithm identifier available");
×
26
}
27

28
PK_Ops::Encryption_with_EME::Encryption_with_EME(std::string_view eme) : m_eme(EME::create(eme)) {}
198✔
29

30
size_t PK_Ops::Encryption_with_EME::max_input_bits() const {
139✔
31
   return 8 * m_eme->maximum_input_size(max_ptext_input_bits());
139✔
32
}
33

34
secure_vector<uint8_t> PK_Ops::Encryption_with_EME::encrypt(const uint8_t msg[],
266✔
35
                                                            size_t msg_len,
36
                                                            RandomNumberGenerator& rng) {
37
   const size_t max_raw = max_ptext_input_bits();
266✔
38
   const auto encoded = m_eme->encode(msg, msg_len, max_raw, rng);
266✔
39
   return raw_encrypt(encoded.data(), encoded.size(), rng);
266✔
40
}
266✔
41

42
PK_Ops::Decryption_with_EME::Decryption_with_EME(std::string_view eme) : m_eme(EME::create(eme)) {}
246✔
43

44
secure_vector<uint8_t> PK_Ops::Decryption_with_EME::decrypt(uint8_t& valid_mask,
3,842✔
45
                                                            const uint8_t ciphertext[],
46
                                                            size_t ciphertext_len) {
47
   const secure_vector<uint8_t> raw = raw_decrypt(ciphertext, ciphertext_len);
3,842✔
48
   return m_eme->unpad(valid_mask, raw.data(), raw.size());
3,827✔
49
}
3,827✔
50

51
PK_Ops::Key_Agreement_with_KDF::Key_Agreement_with_KDF(std::string_view kdf) {
2,597✔
52
   if(kdf != "Raw") {
5,146✔
53
      m_kdf = KDF::create_or_throw(kdf);
48✔
54
   }
55
}
2,597✔
56

57
secure_vector<uint8_t> PK_Ops::Key_Agreement_with_KDF::agree(
4,182✔
58
   size_t key_len, const uint8_t w[], size_t w_len, const uint8_t salt[], size_t salt_len) {
59
   if(salt_len > 0 && m_kdf == nullptr) {
4,182✔
60
      throw Invalid_Argument("PK_Key_Agreement::derive_key requires a KDF to use a salt");
×
61
   }
62

63
   secure_vector<uint8_t> z = raw_agree(w, w_len);
4,182✔
64
   if(m_kdf) {
4,180✔
65
      return m_kdf->derive_key(key_len, z, salt, salt_len);
56✔
66
   }
67
   return z;
4,180✔
68
}
4,180✔
69

70
namespace {
71

72
std::unique_ptr<HashFunction> create_signature_hash(std::string_view padding) {
13,382✔
73
   if(auto hash = HashFunction::create(padding)) {
13,382✔
74
      return hash;
9,071✔
75
   }
9,071✔
76

77
   SCAN_Name req(padding);
4,311✔
78

79
   if(req.algo_name() == "EMSA1" && req.arg_count() == 1) {
12,933✔
80
      if(auto hash = HashFunction::create(req.arg(0))) {
10✔
81
         return hash;
5✔
82
      }
5✔
83
   }
84

85
#if defined(BOTAN_HAS_RAW_HASH_FN)
86
   if(req.algo_name() == "Raw") {
8,612✔
87
      if(req.arg_count() == 0) {
56✔
88
         return std::make_unique<RawHashFunction>("Raw", 0);
41✔
89
      }
90

91
      if(req.arg_count() == 1) {
15✔
92
         if(auto hash = HashFunction::create(req.arg(0))) {
30✔
93
            return std::make_unique<RawHashFunction>(std::move(hash));
15✔
94
         }
15✔
95
      }
96
   }
97
#endif
98

99
   throw Algorithm_Not_Found(padding);
4,250✔
100
}
4,311✔
101

102
}  // namespace
103

104
PK_Ops::Signature_with_Hash::Signature_with_Hash(std::string_view hash) :
561✔
105
      Signature(), m_hash(create_signature_hash(hash)) {}
561✔
106

107
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
108
std::string PK_Ops::Signature_with_Hash::rfc6979_hash_function() const {
473✔
109
   std::string hash = m_hash->name();
473✔
110
   if(hash != "Raw") {
473✔
111
      return hash;
938✔
112
   }
113
   return "SHA-512";
8✔
114
}
473✔
115
#endif
116

117
void PK_Ops::Signature_with_Hash::update(const uint8_t msg[], size_t msg_len) {
853✔
118
   m_hash->update(msg, msg_len);
853✔
119
}
853✔
120

121
secure_vector<uint8_t> PK_Ops::Signature_with_Hash::sign(RandomNumberGenerator& rng) {
599✔
122
   const secure_vector<uint8_t> msg = m_hash->final();
599✔
123
   return raw_sign(msg.data(), msg.size(), rng);
599✔
124
}
599✔
125

126
PK_Ops::Verification_with_Hash::Verification_with_Hash(std::string_view padding) :
12,821✔
127
      Verification(), m_hash(create_signature_hash(padding)) {}
12,821✔
128

129
PK_Ops::Verification_with_Hash::Verification_with_Hash(const AlgorithmIdentifier& alg_id,
1,785✔
130
                                                       std::string_view pk_algo,
131
                                                       bool allow_null_parameters) {
1,785✔
132
   const auto oid_info = split_on(alg_id.oid().to_formatted_string(), '/');
1,785✔
133

134
   if(oid_info.size() != 2 || oid_info[0] != pk_algo) {
1,785✔
135
      throw Decoding_Error(
11✔
136
         fmt("Unexpected AlgorithmIdentifier OID {} in association with {} key", alg_id.oid(), pk_algo));
22✔
137
   }
138

139
   if(!alg_id.parameters_are_empty()) {
1,774✔
140
      if(alg_id.parameters_are_null()) {
44✔
141
         if(!allow_null_parameters) {
44✔
142
            throw Decoding_Error(fmt("Unexpected NULL AlgorithmIdentifier parameters for {}", pk_algo));
82✔
143
         }
144
      } else {
145
         throw Decoding_Error(fmt("Unexpected AlgorithmIdentifier parameters for {}", pk_algo));
×
146
      }
147
   }
148

149
   m_hash = HashFunction::create_or_throw(oid_info[1]);
1,733✔
150
}
1,837✔
151

152
void PK_Ops::Verification_with_Hash::update(const uint8_t msg[], size_t msg_len) {
24,782✔
153
   m_hash->update(msg, msg_len);
24,782✔
154
}
24,782✔
155

156
bool PK_Ops::Verification_with_Hash::is_valid_signature(const uint8_t sig[], size_t sig_len) {
23,795✔
157
   const secure_vector<uint8_t> msg = m_hash->final();
23,795✔
158
   return verify(msg.data(), msg.size(), sig, sig_len);
23,795✔
159
}
23,795✔
160

161
size_t PK_Ops::KEM_Encryption_with_KDF::shared_key_length(size_t desired_shared_key_len) const {
12✔
162
   if(m_kdf) {
12✔
163
      return desired_shared_key_len;
164
   } else {
165
      return this->raw_kem_shared_key_length();
×
166
   }
167
}
168

169
void PK_Ops::KEM_Encryption_with_KDF::kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
3,198✔
170
                                                  secure_vector<uint8_t>& out_shared_key,
171
                                                  size_t desired_shared_key_len,
172
                                                  RandomNumberGenerator& rng,
173
                                                  const uint8_t salt[],
174
                                                  size_t salt_len) {
175
   if(salt_len > 0 && m_kdf == nullptr) {
3,198✔
176
      throw Invalid_Argument("PK_KEM_Encryptor::encrypt requires a KDF to use a salt");
×
177
   }
178

179
   secure_vector<uint8_t> raw_shared;
3,198✔
180
   this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
3,198✔
181

182
   BOTAN_ASSERT_EQUAL(out_encapsulated_key.size(),
3,198✔
183
                      this->encapsulated_key_length(),
184
                      "KEM produced encapsulated key with different length than expected");
185

186
   BOTAN_ASSERT_EQUAL(raw_shared.size(),
3,198✔
187
                      this->raw_kem_shared_key_length(),
188
                      "KEM produced shared key with different length than expected");
189

190
   out_shared_key = (m_kdf)
3,198✔
191
                       ? m_kdf->derive_key(desired_shared_key_len, raw_shared.data(), raw_shared.size(), salt, salt_len)
6,396✔
192
                       : raw_shared;
6,396✔
193
}
3,198✔
194

195
PK_Ops::KEM_Encryption_with_KDF::KEM_Encryption_with_KDF(std::string_view kdf) {
720✔
196
   if(kdf != "Raw") {
1,326✔
197
      m_kdf = KDF::create_or_throw(kdf);
114✔
198
   }
199
}
720✔
200

201
size_t PK_Ops::KEM_Decryption_with_KDF::shared_key_length(size_t desired_shared_key_len) const {
12✔
202
   if(m_kdf) {
12✔
203
      return desired_shared_key_len;
204
   } else {
205
      return this->raw_kem_shared_key_length();
×
206
   }
207
}
208

209
secure_vector<uint8_t> PK_Ops::KEM_Decryption_with_KDF::kem_decrypt(
3,216✔
210
   const uint8_t encap_key[], size_t len, size_t desired_shared_key_len, const uint8_t salt[], size_t salt_len) {
211
   if(salt_len > 0 && m_kdf == nullptr) {
3,216✔
212
      throw Invalid_Argument("PK_KEM_Decryptor::decrypt requires a KDF to use a salt");
×
213
   }
214

215
   secure_vector<uint8_t> raw_shared = this->raw_kem_decrypt(encap_key, len);
3,216✔
216

217
   BOTAN_ASSERT_EQUAL(raw_shared.size(),
3,210✔
218
                      this->raw_kem_shared_key_length(),
219
                      "KEM produced shared key with different length than expected");
220

221
   if(m_kdf) {
3,210✔
222
      return m_kdf->derive_key(desired_shared_key_len, raw_shared.data(), raw_shared.size(), salt, salt_len);
2,592✔
223
   }
224
   return raw_shared;
3,210✔
225
}
3,210✔
226

227
PK_Ops::KEM_Decryption_with_KDF::KEM_Decryption_with_KDF(std::string_view kdf) {
720✔
228
   if(kdf != "Raw") {
1,326✔
229
      m_kdf = KDF::create_or_throw(kdf);
114✔
230
   }
231
}
720✔
232

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