• 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.86
/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,830✔
49
}
3,830✔
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,180✔
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,180✔
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,180✔
64
   if(m_kdf) {
4,178✔
65
      return m_kdf->derive_key(key_len, z, salt, salt_len);
54✔
66
   }
67
   return z;
4,178✔
68
}
4,178✔
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) { m_hash->update(msg, msg_len); }
851✔
118

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

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

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

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

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

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

150
void PK_Ops::Verification_with_Hash::update(const uint8_t msg[], size_t msg_len) { m_hash->update(msg, msg_len); }
24,780✔
151

152
bool PK_Ops::Verification_with_Hash::is_valid_signature(const uint8_t sig[], size_t sig_len) {
23,794✔
153
   const secure_vector<uint8_t> msg = m_hash->final();
23,794✔
154
   return verify(msg.data(), msg.size(), sig, sig_len);
23,794✔
155
}
23,794✔
156

157
size_t PK_Ops::KEM_Encryption_with_KDF::shared_key_length(size_t desired_shared_key_len) const {
12✔
158
   if(m_kdf) {
12✔
159
      return desired_shared_key_len;
160
   } else {
161
      return this->raw_kem_shared_key_length();
×
162
   }
163
}
164

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

175
   secure_vector<uint8_t> raw_shared;
3,194✔
176
   this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
3,194✔
177

178
   BOTAN_ASSERT_EQUAL(out_encapsulated_key.size(),
3,194✔
179
                      this->encapsulated_key_length(),
180
                      "KEM produced encapsulated key with different length than expected");
181

182
   BOTAN_ASSERT_EQUAL(raw_shared.size(),
3,194✔
183
                      this->raw_kem_shared_key_length(),
184
                      "KEM produced shared key with different length than expected");
185

186
   out_shared_key = (m_kdf)
3,194✔
187
                       ? m_kdf->derive_key(desired_shared_key_len, raw_shared.data(), raw_shared.size(), salt, salt_len)
6,388✔
188
                       : raw_shared;
6,388✔
189
}
3,194✔
190

191
PK_Ops::KEM_Encryption_with_KDF::KEM_Encryption_with_KDF(std::string_view kdf) {
720✔
192
   if(kdf != "Raw") {
1,326✔
193
      m_kdf = KDF::create_or_throw(kdf);
114✔
194
   }
195
}
720✔
196

197
size_t PK_Ops::KEM_Decryption_with_KDF::shared_key_length(size_t desired_shared_key_len) const {
12✔
198
   if(m_kdf) {
12✔
199
      return desired_shared_key_len;
200
   } else {
201
      return this->raw_kem_shared_key_length();
×
202
   }
203
}
204

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

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

213
   BOTAN_ASSERT_EQUAL(raw_shared.size(),
3,206✔
214
                      this->raw_kem_shared_key_length(),
215
                      "KEM produced shared key with different length than expected");
216

217
   if(m_kdf) {
3,206✔
218
      return m_kdf->derive_key(desired_shared_key_len, raw_shared.data(), raw_shared.size(), salt, salt_len);
2,588✔
219
   }
220
   return raw_shared;
3,206✔
221
}
3,206✔
222

223
PK_Ops::KEM_Decryption_with_KDF::KEM_Decryption_with_KDF(std::string_view kdf) {
720✔
224
   if(kdf != "Raw") {
1,326✔
225
      m_kdf = KDF::create_or_throw(kdf);
114✔
226
   }
227
}
720✔
228

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