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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

93.19
/src/lib/pubkey/rsa/rsa.cpp
1
/*
2
* RSA
3
* (C) 1999-2010,2015,2016,2018,2019,2023 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/rsa.h>
9

10
#include <botan/ber_dec.h>
11
#include <botan/der_enc.h>
12
#include <botan/reducer.h>
13
#include <botan/internal/blinding.h>
14
#include <botan/internal/divide.h>
15
#include <botan/internal/emsa.h>
16
#include <botan/internal/fmt.h>
17
#include <botan/internal/keypair.h>
18
#include <botan/internal/monty.h>
19
#include <botan/internal/monty_exp.h>
20
#include <botan/internal/parsing.h>
21
#include <botan/internal/pk_ops_impl.h>
22
#include <botan/internal/pss_params.h>
23
#include <botan/internal/workfactor.h>
24

25
#if defined(BOTAN_HAS_THREAD_UTILS)
26
   #include <botan/internal/thread_pool.h>
27
#endif
28

29
namespace Botan {
30

31
class RSA_Public_Data final {
32
   public:
33
      RSA_Public_Data(BigInt&& n, BigInt&& e) :
13,776✔
34
            m_n(n),
13,776✔
35
            m_e(e),
13,776✔
36
            m_monty_n(std::make_shared<Montgomery_Params>(m_n)),
13,776✔
37
            m_public_modulus_bits(m_n.bits()),
13,776✔
38
            m_public_modulus_bytes(m_n.bytes()) {}
13,776✔
39

40
      BigInt public_op(const BigInt& m) const {
26,192✔
41
         const size_t powm_window = 1;
26,192✔
42
         auto powm_m_n = monty_precompute(m_monty_n, m, powm_window, false);
26,192✔
43
         return monty_execute_vartime(*powm_m_n, m_e);
26,192✔
44
      }
26,192✔
45

46
      const BigInt& get_n() const { return m_n; }
27,566✔
47

48
      const BigInt& get_e() const { return m_e; }
3✔
49

50
      size_t public_modulus_bits() const { return m_public_modulus_bits; }
19,756✔
51

52
      size_t public_modulus_bytes() const { return m_public_modulus_bytes; }
29,831✔
53

54
   private:
55
      BigInt m_n;
56
      BigInt m_e;
57
      std::shared_ptr<const Montgomery_Params> m_monty_n;
58
      size_t m_public_modulus_bits;
59
      size_t m_public_modulus_bytes;
60
};
61

62
class RSA_Private_Data final {
63
   public:
64
      RSA_Private_Data(BigInt&& d, BigInt&& p, BigInt&& q, BigInt&& d1, BigInt&& d2, BigInt&& c) :
2,563✔
65
            m_d(d),
2,563✔
66
            m_p(p),
2,563✔
67
            m_q(q),
2,563✔
68
            m_d1(d1),
2,563✔
69
            m_d2(d2),
2,563✔
70
            m_c(c),
2,563✔
71
            m_mod_p(m_p),
2,563✔
72
            m_mod_q(m_q),
2,563✔
73
            m_monty_p(std::make_shared<Montgomery_Params>(m_p, m_mod_p)),
2,563✔
74
            m_monty_q(std::make_shared<Montgomery_Params>(m_q, m_mod_q)),
2,563✔
75
            m_p_bits(m_p.bits()),
2,563✔
76
            m_q_bits(m_q.bits()) {}
2,563✔
77

78
      const BigInt& get_d() const { return m_d; }
1✔
79

80
      const BigInt& get_p() const { return m_p; }
4,847✔
81

82
      const BigInt& get_q() const { return m_q; }
9,693✔
83

84
      const BigInt& get_d1() const { return m_d1; }
4,846✔
85

86
      const BigInt& get_d2() const { return m_d2; }
4,846✔
87

88
      const BigInt& get_c() const { return m_c; }
×
89

90
      const Modular_Reducer& mod_p() const { return m_mod_p; }
9,692✔
91

92
      const Modular_Reducer& mod_q() const { return m_mod_q; }
4,846✔
93

94
      const std::shared_ptr<const Montgomery_Params>& monty_p() const { return m_monty_p; }
4,846✔
95

96
      const std::shared_ptr<const Montgomery_Params>& monty_q() const { return m_monty_q; }
4,846✔
97

98
      size_t p_bits() const { return m_p_bits; }
1,323✔
99

100
      size_t q_bits() const { return m_q_bits; }
1,323✔
101

102
   private:
103
      BigInt m_d;
104
      BigInt m_p;
105
      BigInt m_q;
106
      BigInt m_d1;
107
      BigInt m_d2;
108
      BigInt m_c;
109

110
      Modular_Reducer m_mod_p;
111
      Modular_Reducer m_mod_q;
112
      std::shared_ptr<const Montgomery_Params> m_monty_p;
113
      std::shared_ptr<const Montgomery_Params> m_monty_q;
114
      size_t m_p_bits;
115
      size_t m_q_bits;
116
};
117

118
std::shared_ptr<const RSA_Public_Data> RSA_PublicKey::public_data() const { return m_public; }
12,191✔
119

120
const BigInt& RSA_PublicKey::get_int_field(std::string_view field) const {
8✔
121
   if(field == "n")
11✔
122
      return m_public->get_n();
3✔
123
   else if(field == "e")
5✔
124
      return m_public->get_e();
3✔
125
   else
126
      return Public_Key::get_int_field(field);
2✔
127
}
128

129
const BigInt& RSA_PublicKey::get_n() const { return m_public->get_n(); }
525✔
130

131
const BigInt& RSA_PublicKey::get_e() const { return m_public->get_e(); }
496✔
132

133
void RSA_PublicKey::init(BigInt&& n, BigInt&& e) {
14,576✔
134
   if(n.is_negative() || n.is_even() || n.bits() < 5 /* n >= 3*5 */ || e.is_negative() || e.is_even())
43,120✔
135
      throw Decoding_Error("Invalid RSA public key parameters");
800✔
136
   m_public = std::make_shared<RSA_Public_Data>(std::move(n), std::move(e));
13,776✔
137
}
13,776✔
138

139
RSA_PublicKey::RSA_PublicKey(const AlgorithmIdentifier& /*unused*/, std::span<const uint8_t> key_bits) {
11,545✔
140
   BigInt n, e;
11,545✔
141
   BER_Decoder(key_bits).start_sequence().decode(n).decode(e).end_cons();
12,438✔
142

143
   init(std::move(n), std::move(e));
11,489✔
144
}
23,090✔
145

146
bool RSA_PublicKey::supports_operation(PublicKeyOperation op) const {
1,083✔
147
   return op == PublicKeyOperation::Signature || op == PublicKeyOperation::Encryption ||
1,083✔
148
          op == PublicKeyOperation::KeyEncapsulation;
1,083✔
149
}
150

151
RSA_PublicKey::RSA_PublicKey(const BigInt& modulus, const BigInt& exponent) {
521✔
152
   BigInt n = modulus;
521✔
153
   BigInt e = exponent;
521✔
154
   init(std::move(n), std::move(e));
521✔
155
}
1,042✔
156

157
size_t RSA_PublicKey::key_length() const { return m_public->public_modulus_bits(); }
4,943✔
158

159
size_t RSA_PublicKey::estimated_strength() const { return if_work_factor(key_length()); }
3,421✔
160

161
AlgorithmIdentifier RSA_PublicKey::algorithm_identifier() const {
403✔
162
   return AlgorithmIdentifier(object_identifier(), AlgorithmIdentifier::USE_NULL_PARAM);
403✔
163
}
164

165
std::vector<uint8_t> RSA_PublicKey::public_key_bits() const {
341✔
166
   std::vector<uint8_t> output;
341✔
167
   DER_Encoder der(output);
341✔
168
   der.start_sequence().encode(get_n()).encode(get_e()).end_cons();
341✔
169

170
   return output;
341✔
171
}
341✔
172

173
/*
174
* Check RSA Public Parameters
175
*/
176
bool RSA_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
9✔
177
   if(get_n() < 35 || get_n().is_even() || get_e() < 3 || get_e().is_even())
27✔
178
      return false;
×
179
   return true;
180
}
181

182
std::shared_ptr<const RSA_Private_Data> RSA_PrivateKey::private_data() const { return m_private; }
1,323✔
183

184
secure_vector<uint8_t> RSA_PrivateKey::private_key_bits() const {
94✔
185
   return DER_Encoder()
188✔
186
      .start_sequence()
94✔
187
      .encode(static_cast<size_t>(0))
94✔
188
      .encode(get_n())
94✔
189
      .encode(get_e())
94✔
190
      .encode(get_d())
94✔
191
      .encode(get_p())
94✔
192
      .encode(get_q())
94✔
193
      .encode(get_d1())
94✔
194
      .encode(get_d2())
94✔
195
      .encode(get_c())
94✔
196
      .end_cons()
94✔
197
      .get_contents();
188✔
198
}
199

200
const BigInt& RSA_PrivateKey::get_p() const { return m_private->get_p(); }
170✔
201

202
const BigInt& RSA_PrivateKey::get_q() const { return m_private->get_q(); }
169✔
203

204
const BigInt& RSA_PrivateKey::get_d() const { return m_private->get_d(); }
137✔
205

206
const BigInt& RSA_PrivateKey::get_c() const { return m_private->get_c(); }
107✔
207

208
const BigInt& RSA_PrivateKey::get_d1() const { return m_private->get_d1(); }
107✔
209

210
const BigInt& RSA_PrivateKey::get_d2() const { return m_private->get_d2(); }
107✔
211

212
void RSA_PrivateKey::init(BigInt&& d, BigInt&& p, BigInt&& q, BigInt&& d1, BigInt&& d2, BigInt&& c) {
2,563✔
213
   m_private = std::make_shared<RSA_Private_Data>(
7,689✔
214
      std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
2,563✔
215
}
2,563✔
216

217
RSA_PrivateKey::RSA_PrivateKey(const AlgorithmIdentifier& /*unused*/, std::span<const uint8_t> key_bits) {
2,103✔
218
   BigInt n, e, d, p, q, d1, d2, c;
2,103✔
219

220
   BER_Decoder(key_bits)
2,126✔
221
      .start_sequence()
4,199✔
222
      .decode_and_check<size_t>(0, "Unknown PKCS #1 key format version")
4,188✔
223
      .decode(n)
2,092✔
224
      .decode(e)
2,092✔
225
      .decode(d)
2,089✔
226
      .decode(p)
2,089✔
227
      .decode(q)
2,089✔
228
      .decode(d1)
2,088✔
229
      .decode(d2)
2,088✔
230
      .decode(c)
2,085✔
231
      .end_cons();
2,085✔
232

233
   RSA_PublicKey::init(std::move(n), std::move(e));
2,082✔
234

235
   RSA_PrivateKey::init(std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
2,080✔
236
}
16,824✔
237

238
RSA_PrivateKey::RSA_PrivateKey(
428✔
239
   const BigInt& prime1, const BigInt& prime2, const BigInt& exp, const BigInt& d_exp, const BigInt& mod) {
428✔
240
   BigInt p = prime1;
428✔
241
   BigInt q = prime2;
428✔
242
   BigInt n = mod;
428✔
243
   if(n.is_zero())
856✔
244
      n = p * q;
427✔
245

246
   BigInt e = exp;
428✔
247

248
   BigInt d = d_exp;
428✔
249

250
   const BigInt p_minus_1 = p - 1;
428✔
251
   const BigInt q_minus_1 = q - 1;
428✔
252

253
   if(d.is_zero()) {
856✔
254
      const BigInt phi_n = lcm(p_minus_1, q_minus_1);
427✔
255
      d = inverse_mod(e, phi_n);
427✔
256
   }
427✔
257

258
   BigInt d1 = ct_modulo(d, p_minus_1);
428✔
259
   BigInt d2 = ct_modulo(d, q_minus_1);
428✔
260
   BigInt c = inverse_mod(q, p);
428✔
261

262
   RSA_PublicKey::init(std::move(n), std::move(e));
428✔
263

264
   RSA_PrivateKey::init(std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
428✔
265
}
4,280✔
266

267
/*
268
* Create a RSA private key
269
*/
270
RSA_PrivateKey::RSA_PrivateKey(RandomNumberGenerator& rng, size_t bits, size_t exp) {
56✔
271
   if(bits < 1024) {
56✔
272
      throw Invalid_Argument(fmt("Cannot create an RSA key only {} bits long", bits));
×
273
   }
274

275
   if(exp < 3 || exp % 2 == 0) {
56✔
276
      throw Invalid_Argument("Invalid RSA encryption exponent");
×
277
   }
278

279
   const size_t p_bits = (bits + 1) / 2;
56✔
280
   const size_t q_bits = bits - p_bits;
56✔
281

282
   BigInt p, q, n;
56✔
283
   BigInt e = BigInt::from_u64(exp);
56✔
284

285
   for(size_t attempt = 0;; ++attempt) {
11✔
286
      if(attempt > 10)
67✔
287
         throw Internal_Error("RNG failure during RSA key generation");
1✔
288

289
      // TODO could generate primes in thread pool
290
      p = generate_rsa_prime(rng, rng, p_bits, e);
66✔
291
      q = generate_rsa_prime(rng, rng, q_bits, e);
66✔
292

293
      const BigInt diff = p - q;
66✔
294
      if(diff.bits() < (bits / 2) - 100)
66✔
295
         continue;
11✔
296

297
      n = p * q;
55✔
298

299
      if(n.bits() != bits)
55✔
300
         continue;
×
301

302
      break;
55✔
303
   }
11✔
304

305
   const BigInt p_minus_1 = p - 1;
55✔
306
   const BigInt q_minus_1 = q - 1;
55✔
307

308
   const BigInt phi_n = lcm(p_minus_1, q_minus_1);
55✔
309
   // This is guaranteed because p,q == 3 mod 4
310
   BOTAN_DEBUG_ASSERT(low_zero_bits(phi_n) == 1);
55✔
311

312
   BigInt d = inverse_mod(e, phi_n);
55✔
313
   BigInt d1 = ct_modulo(d, p_minus_1);
55✔
314
   BigInt d2 = ct_modulo(d, q_minus_1);
55✔
315
   BigInt c = inverse_mod(q, p);
55✔
316

317
   RSA_PublicKey::init(std::move(n), std::move(e));
55✔
318

319
   RSA_PrivateKey::init(std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
55✔
320
}
609✔
321

322
const BigInt& RSA_PrivateKey::get_int_field(std::string_view field) const {
6✔
323
   if(field == "p")
10✔
324
      return m_private->get_p();
1✔
325
   else if(field == "q")
8✔
326
      return m_private->get_q();
1✔
327
   else if(field == "d")
6✔
328
      return m_private->get_d();
1✔
329
   else if(field == "c")
5✔
330
      return m_private->get_c();
×
331
   else if(field == "d1")
3✔
332
      return m_private->get_d1();
×
333
   else if(field == "d2")
3✔
334
      return m_private->get_d2();
×
335
   else
336
      return RSA_PublicKey::get_int_field(field);
3✔
337
}
338

339
std::unique_ptr<Public_Key> RSA_PrivateKey::public_key() const {
5✔
340
   return std::make_unique<RSA_PublicKey>(get_n(), get_e());
5✔
341
}
342

343
/*
344
* Check Private RSA Parameters
345
*/
346
bool RSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
11✔
347
   if(get_n() < 35 || get_n().is_even() || get_e() < 3 || get_e().is_even())
33✔
348
      return false;
×
349

350
   if(get_d() < 2 || get_p() < 3 || get_q() < 3)
11✔
351
      return false;
×
352

353
   if(get_p() * get_q() != get_n())
22✔
354
      return false;
355

356
   if(get_p() == get_q())
11✔
357
      return false;
358

359
   if(get_d1() != ct_modulo(get_d(), get_p() - 1))
33✔
360
      return false;
361
   if(get_d2() != ct_modulo(get_d(), get_q() - 1))
33✔
362
      return false;
363
   if(get_c() != inverse_mod(get_q(), get_p()))
22✔
364
      return false;
365

366
   const size_t prob = (strong) ? 128 : 12;
11✔
367

368
   if(!is_prime(get_p(), rng, prob))
11✔
369
      return false;
370
   if(!is_prime(get_q(), rng, prob))
10✔
371
      return false;
372

373
   if(strong) {
10✔
374
      if(ct_modulo(get_e() * get_d(), lcm(get_p() - 1, get_q() - 1)) != 1)
56✔
375
         return false;
376

377
      return KeyPair::signature_consistency_check(rng, *this, "EMSA4(SHA-256)");
8✔
378
   }
379

380
   return true;
381
}
382

383
namespace {
384

385
/**
386
* RSA private (decrypt/sign) operation
387
*/
388
class RSA_Private_Operation {
389
   protected:
390
      size_t public_modulus_bits() const { return m_public->public_modulus_bits(); }
1,499✔
391

392
      size_t public_modulus_bytes() const { return m_public->public_modulus_bytes(); }
5,260✔
393

394
      explicit RSA_Private_Operation(const RSA_PrivateKey& rsa, RandomNumberGenerator& rng) :
1,323✔
395
            m_public(rsa.public_data()),
1,323✔
396
            m_private(rsa.private_data()),
1,323✔
397
            m_blinder(
2,646✔
398
               m_public->get_n(),
1,323✔
399
               rng,
400
               [this](const BigInt& k) { return m_public->public_op(k); },
1,329✔
401
               [this](const BigInt& k) { return inverse_mod(k, m_public->get_n()); }),
1,329✔
402
            m_blinding_bits(64),
1,323✔
403
            m_max_d1_bits(m_private->p_bits() + m_blinding_bits),
1,323✔
404
            m_max_d2_bits(m_private->q_bits() + m_blinding_bits) {}
1,323✔
405

406
      secure_vector<uint8_t> raw_op(const uint8_t input[], size_t input_len) {
4,856✔
407
         if(input_len > public_modulus_bytes())
4,856✔
408
            throw Decoding_Error("RSA input is too long for this key");
×
409
         const BigInt input_bn(input, input_len);
4,856✔
410
         if(input_bn >= m_public->get_n())
4,856✔
411
            throw Decoding_Error("RSA input is too large for this key");
9✔
412

413
         // TODO: This should be a function on blinder
414
         // BigInt Blinder::run_blinded_function(std::function<BigInt, BigInt> fn, const BigInt& input);
415

416
         const BigInt recovered = m_blinder.unblind(rsa_private_op(m_blinder.blind(input_bn)));
9,702✔
417
         BOTAN_ASSERT(input_bn == m_public->public_op(recovered), "RSA consistency check");
14,538✔
418
         return BigInt::encode_1363(recovered, m_public->public_modulus_bytes());
4,846✔
419
      }
9,692✔
420

421
   private:
422
      BigInt rsa_private_op(const BigInt& m) const {
4,846✔
423
         /*
424
         TODO
425
         Consider using Montgomery reduction instead of Barrett, using
426
         the "Smooth RSA-CRT" method. https://eprint.iacr.org/2007/039.pdf
427
         */
428

429
         static constexpr size_t powm_window = 4;
4,846✔
430

431
         // Compute this in main thread to avoid racing on the rng
432
         const BigInt d1_mask(m_blinder.rng(), m_blinding_bits);
4,846✔
433

434
#if defined(BOTAN_HAS_THREAD_UTILS) && !defined(BOTAN_HAS_VALGRIND)
435
   #define BOTAN_RSA_USE_ASYNC
436
#endif
437

438
#if defined(BOTAN_RSA_USE_ASYNC)
439
         /*
440
         * Precompute m.sig_words in the main thread before calling async. Otherwise
441
         * the two threads race (during Modular_Reducer::reduce) and while the output
442
         * is correct in both threads, helgrind warns.
443
         */
444
         m.sig_words();
4,846✔
445

446
         auto future_j1 = Thread_Pool::global_instance().run([this, &m, &d1_mask]() {
4,846✔
447
#endif
448
            const BigInt masked_d1 = m_private->get_d1() + (d1_mask * (m_private->get_p() - 1));
9,692✔
449
            auto powm_d1_p = monty_precompute(m_private->monty_p(), m_private->mod_p().reduce(m), powm_window);
4,846✔
450
            BigInt j1 = monty_execute(*powm_d1_p, masked_d1, m_max_d1_bits);
4,846✔
451

452
#if defined(BOTAN_RSA_USE_ASYNC)
453
            return j1;
4,846✔
454
         });
14,538✔
455
#endif
456

457
         const BigInt d2_mask(m_blinder.rng(), m_blinding_bits);
4,846✔
458
         const BigInt masked_d2 = m_private->get_d2() + (d2_mask * (m_private->get_q() - 1));
9,692✔
459
         auto powm_d2_q = monty_precompute(m_private->monty_q(), m_private->mod_q().reduce(m), powm_window);
4,846✔
460
         const BigInt j2 = monty_execute(*powm_d2_q, masked_d2, m_max_d2_bits);
4,846✔
461

462
#if defined(BOTAN_RSA_USE_ASYNC)
463
         BigInt j1 = future_j1.get();
4,846✔
464
#endif
465

466
         /*
467
         * To recover the final value from the CRT representation (j1,j2)
468
         * we use Garner's algorithm:
469
         * c = q^-1 mod p (this is precomputed)
470
         * h = c*(j1-j2) mod p
471
         * m = j2 + h*q
472
         *
473
         * We must avoid leaking if j1 >= j2 or not, as doing so allows deriving
474
         * information about the secret prime. Do this by first adding p to j1,
475
         * which should ensure the subtraction of j2 does not underflow. But
476
         * this may still underflow if p and q are imbalanced in size.
477
         */
478

479
         j1 =
4,846✔
480
            m_private->mod_p().multiply(m_private->mod_p().reduce((m_private->get_p() + j1) - j2), m_private->get_c());
19,384✔
481
         return j1 * m_private->get_q() + j2;
9,692✔
482
      }
29,076✔
483

484
      std::shared_ptr<const RSA_Public_Data> m_public;
485
      std::shared_ptr<const RSA_Private_Data> m_private;
486

487
      // XXX could the blinder starting pair be shared?
488
      Blinder m_blinder;
489
      const size_t m_blinding_bits;
490
      const size_t m_max_d1_bits;
491
      const size_t m_max_d2_bits;
492
};
493

494
class RSA_Signature_Operation final : public PK_Ops::Signature,
×
495
                                      private RSA_Private_Operation {
496
   public:
497
      void update(const uint8_t msg[], size_t msg_len) override { m_emsa->update(msg, msg_len); }
1,500✔
498

499
      secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override {
1,499✔
500
         const size_t max_input_bits = public_modulus_bits() - 1;
1,499✔
501
         const auto msg = m_emsa->raw_data();
1,499✔
502
         const auto padded = m_emsa->encoding_of(msg, max_input_bits, rng);
1,499✔
503
         return raw_op(padded.data(), padded.size());
1,498✔
504
      }
2,995✔
505

506
      size_t signature_length() const override { return public_modulus_bytes(); }
268✔
507

508
      AlgorithmIdentifier algorithm_identifier() const override;
509

510
      std::string hash_function() const override { return m_emsa->hash_function(); }
115✔
511

512
      RSA_Signature_Operation(const RSA_PrivateKey& rsa, std::string_view padding, RandomNumberGenerator& rng) :
1,101✔
513
            RSA_Private_Operation(rsa, rng), m_emsa(EMSA::create_or_throw(padding)) {}
1,101✔
514

515
   private:
516
      std::unique_ptr<EMSA> m_emsa;
517
};
518

519
AlgorithmIdentifier RSA_Signature_Operation::algorithm_identifier() const {
74✔
520
   const std::string emsa_name = m_emsa->name();
74✔
521

522
   try {
74✔
523
      const std::string full_name = "RSA/" + emsa_name;
74✔
524
      const OID oid = OID::from_string(full_name);
74✔
525
      return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
53✔
526
   } catch(Lookup_Error&) {}
127✔
527

528
   if(emsa_name.starts_with("EMSA4(")) {
21✔
529
      auto parameters = PSS_Params::from_emsa_name(m_emsa->name()).serialize();
42✔
530
      return AlgorithmIdentifier("RSA/EMSA4", parameters);
21✔
531
   }
21✔
532

533
   throw Not_Implemented("No algorithm identifier defined for RSA with " + emsa_name);
×
534
}
74✔
535

536
class RSA_Decryption_Operation final : public PK_Ops::Decryption_with_EME,
×
537
                                       private RSA_Private_Operation {
538
   public:
539
      RSA_Decryption_Operation(const RSA_PrivateKey& rsa, std::string_view eme, RandomNumberGenerator& rng) :
211✔
540
            PK_Ops::Decryption_with_EME(eme), RSA_Private_Operation(rsa, rng) {}
211✔
541

542
      size_t plaintext_length(size_t /*ctext_len*/) const override { return public_modulus_bytes(); }
125✔
543

544
      secure_vector<uint8_t> raw_decrypt(const uint8_t input[], size_t input_len) override {
3,347✔
545
         return raw_op(input, input_len);
3,347✔
546
      }
547
};
548

549
class RSA_KEM_Decryption_Operation final : public PK_Ops::KEM_Decryption_with_KDF,
×
550
                                           private RSA_Private_Operation {
551
   public:
552
      RSA_KEM_Decryption_Operation(const RSA_PrivateKey& key, std::string_view kdf, RandomNumberGenerator& rng) :
11✔
553
            PK_Ops::KEM_Decryption_with_KDF(kdf), RSA_Private_Operation(key, rng) {}
11✔
554

555
      size_t raw_kem_shared_key_length() const override { return public_modulus_bytes(); }
11✔
556

557
      secure_vector<uint8_t> raw_kem_decrypt(const uint8_t encap_key[], size_t len) override {
11✔
558
         return raw_op(encap_key, len);
11✔
559
      }
560
};
561

562
/**
563
* RSA public (encrypt/verify) operation
564
*/
565
class RSA_Public_Operation {
×
566
   public:
567
      explicit RSA_Public_Operation(const RSA_PublicKey& rsa) : m_public(rsa.public_data()) {}
10,675✔
568

569
      size_t public_modulus_bits() const { return m_public->public_modulus_bits(); }
19,756✔
570

571
   protected:
572
      BigInt public_op(const BigInt& m) const {
20,044✔
573
         if(m >= m_public->get_n())
20,044✔
574
            throw Decoding_Error("RSA public op - input is too large");
27✔
575

576
         return m_public->public_op(m);
20,017✔
577
      }
578

579
      size_t public_modulus_bytes() const { return m_public->public_modulus_bytes(); }
20,287✔
580

581
      const BigInt& get_n() const { return m_public->get_n(); }
11✔
582

583
   private:
584
      std::shared_ptr<const RSA_Public_Data> m_public;
585
};
586

587
class RSA_Encryption_Operation final : public PK_Ops::Encryption_with_EME,
×
588
                                       private RSA_Public_Operation {
589
   public:
590
      RSA_Encryption_Operation(const RSA_PublicKey& rsa, std::string_view eme) :
182✔
591
            PK_Ops::Encryption_with_EME(eme), RSA_Public_Operation(rsa) {}
182✔
592

593
      size_t ciphertext_length(size_t /*ptext_len*/) const override { return public_modulus_bytes(); }
125✔
594

595
      size_t max_ptext_input_bits() const override { return public_modulus_bits() - 1; }
373✔
596

597
      secure_vector<uint8_t> raw_encrypt(const uint8_t input[],
250✔
598
                                         size_t input_len,
599
                                         RandomNumberGenerator& /*rng*/) override {
600
         BigInt input_bn(input, input_len);
250✔
601
         return BigInt::encode_1363(public_op(input_bn), public_modulus_bytes());
500✔
602
      }
250✔
603
};
604

605
class RSA_Verify_Operation final : public PK_Ops::Verification,
×
606
                                   private RSA_Public_Operation {
607
   public:
608
      void update(const uint8_t msg[], size_t msg_len) override { m_emsa->update(msg, msg_len); }
19,859✔
609

610
      bool is_valid_signature(const uint8_t sig[], size_t sig_len) override {
19,857✔
611
         const auto msg = m_emsa->raw_data();
19,857✔
612
         const auto message_repr = recover_message_repr(sig, sig_len);
19,857✔
613
         return m_emsa->verify(message_repr, msg, public_modulus_bits() - 1);
19,756✔
614
      }
39,467✔
615

616
      RSA_Verify_Operation(const RSA_PublicKey& rsa, std::string_view padding) :
10,675✔
617
            RSA_Public_Operation(rsa), m_emsa(EMSA::create_or_throw(padding)) {}
10,675✔
618

619
      std::string hash_function() const override { return m_emsa->hash_function(); }
8,693✔
620

621
   private:
622
      std::vector<uint8_t> recover_message_repr(const uint8_t input[], size_t input_len) {
19,857✔
623
         if(input_len > public_modulus_bytes())
19,857✔
624
            throw Decoding_Error("RSA signature too large to be valid for this key");
74✔
625
         BigInt input_bn(input, input_len);
19,783✔
626
         return BigInt::encode(public_op(input_bn));
39,539✔
627
      }
19,756✔
628

629
      std::unique_ptr<EMSA> m_emsa;
630
};
631

632
class RSA_KEM_Encryption_Operation final : public PK_Ops::KEM_Encryption_with_KDF,
×
633
                                           private RSA_Public_Operation {
634
   public:
635
      RSA_KEM_Encryption_Operation(const RSA_PublicKey& key, std::string_view kdf) :
11✔
636
            PK_Ops::KEM_Encryption_with_KDF(kdf), RSA_Public_Operation(key) {}
11✔
637

638
   private:
639
      size_t raw_kem_shared_key_length() const override { return public_modulus_bytes(); }
11✔
640

641
      size_t encapsulated_key_length() const override { return public_modulus_bytes(); }
22✔
642

643
      void raw_kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
11✔
644
                           secure_vector<uint8_t>& raw_shared_key,
645
                           RandomNumberGenerator& rng) override {
646
         const BigInt r = BigInt::random_integer(rng, 1, get_n());
11✔
647
         const BigInt c = public_op(r);
11✔
648

649
         out_encapsulated_key = BigInt::encode_1363(c, public_modulus_bytes());
22✔
650
         raw_shared_key = BigInt::encode_1363(r, public_modulus_bytes());
22✔
651
      }
22✔
652
};
653

654
}
655

656
std::unique_ptr<PK_Ops::Encryption> RSA_PublicKey::create_encryption_op(RandomNumberGenerator& /*rng*/,
551✔
657
                                                                        std::string_view params,
658
                                                                        std::string_view provider) const {
659
   if(provider == "base" || provider.empty())
675✔
660
      return std::make_unique<RSA_Encryption_Operation>(*this, params);
182✔
661
   throw Provider_Not_Found(algo_name(), provider);
738✔
662
}
663

664
std::unique_ptr<PK_Ops::KEM_Encryption> RSA_PublicKey::create_kem_encryption_op(std::string_view params,
11✔
665
                                                                                std::string_view provider) const {
666
   if(provider == "base" || provider.empty())
11✔
667
      return std::make_unique<RSA_KEM_Encryption_Operation>(*this, params);
11✔
668
   throw Provider_Not_Found(algo_name(), provider);
×
669
}
670

671
std::unique_ptr<PK_Ops::Verification> RSA_PublicKey::create_verification_op(std::string_view params,
4,035✔
672
                                                                            std::string_view provider) const {
673
   if(provider == "base" || provider.empty())
4,806✔
674
      return std::make_unique<RSA_Verify_Operation>(*this, params);
1,725✔
675

676
   throw Provider_Not_Found(algo_name(), provider);
4,620✔
677
}
678

679
namespace {
680

681
std::string parse_rsa_signature_algorithm(const AlgorithmIdentifier& alg_id) {
8,953✔
682
   const auto sig_info = split_on(alg_id.oid().to_formatted_string(), '/');
8,953✔
683

684
   if(sig_info.empty() || sig_info.size() != 2 || sig_info[0] != "RSA")
8,953✔
685
      throw Decoding_Error("Unknown AlgorithmIdentifier for RSA X.509 signatures");
1✔
686

687
   std::string padding = sig_info[1];
8,952✔
688

689
   if(padding == "EMSA4") {
8,952✔
690
      // "MUST contain RSASSA-PSS-params"
691
      if(alg_id.parameters().empty()) {
534✔
692
         throw Decoding_Error("PSS params must be provided");
×
693
      }
694

695
      PSS_Params pss_params(alg_id.parameters());
534✔
696

697
      // hash_algo must be SHA1, SHA2-224, SHA2-256, SHA2-384 or SHA2-512
698
      const std::string hash_algo = pss_params.hash_function();
534✔
699
      if(hash_algo != "SHA-1" && hash_algo != "SHA-224" && hash_algo != "SHA-256" && hash_algo != "SHA-384" &&
534✔
700
         hash_algo != "SHA-512") {
10✔
701
         throw Decoding_Error("Unacceptable hash for PSS signatures");
×
702
      }
703

704
      if(pss_params.mgf_function() != "MGF1") {
534✔
705
         throw Decoding_Error("Unacceptable MGF for PSS signatures");
×
706
      }
707

708
      // For MGF1, it is strongly RECOMMENDED that the underlying hash
709
      // function be the same as the one identified by hashAlgorithm
710
      //
711
      // Must be SHA1, SHA2-224, SHA2-256, SHA2-384 or SHA2-512
712
      if(pss_params.hash_algid() != pss_params.mgf_hash_algid()) {
534✔
713
         throw Decoding_Error("Unacceptable MGF hash for PSS signatures");
2✔
714
      }
715

716
      if(pss_params.trailer_field() != 1) {
532✔
717
         throw Decoding_Error("Unacceptable trailer field for PSS signatures");
×
718
      }
719

720
      padding += fmt("({},MGF1,{})", hash_algo, pss_params.salt_length());
1,588✔
721
   }
534✔
722

723
   return padding;
8,950✔
724
}
8,953✔
725

726
}
727

728
std::unique_ptr<PK_Ops::Verification> RSA_PublicKey::create_x509_verification_op(const AlgorithmIdentifier& alg_id,
8,953✔
729
                                                                                 std::string_view provider) const {
730
   if(provider == "base" || provider.empty())
8,953✔
731
      return std::make_unique<RSA_Verify_Operation>(*this, parse_rsa_signature_algorithm(alg_id));
9,485✔
732

733
   throw Provider_Not_Found(algo_name(), provider);
×
734
}
735

736
std::unique_ptr<PK_Ops::Decryption> RSA_PrivateKey::create_decryption_op(RandomNumberGenerator& rng,
655✔
737
                                                                         std::string_view params,
738
                                                                         std::string_view provider) const {
739
   if(provider == "base" || provider.empty())
804✔
740
      return std::make_unique<RSA_Decryption_Operation>(*this, params, rng);
211✔
741

742
   throw Provider_Not_Found(algo_name(), provider);
888✔
743
}
744

745
std::unique_ptr<PK_Ops::KEM_Decryption> RSA_PrivateKey::create_kem_decryption_op(RandomNumberGenerator& rng,
11✔
746
                                                                                 std::string_view params,
747
                                                                                 std::string_view provider) const {
748
   if(provider == "base" || provider.empty())
11✔
749
      return std::make_unique<RSA_KEM_Decryption_Operation>(*this, params, rng);
11✔
750

751
   throw Provider_Not_Found(algo_name(), provider);
×
752
}
753

754
std::unique_ptr<PK_Ops::Signature> RSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
1,902✔
755
                                                                       std::string_view params,
756
                                                                       std::string_view provider) const {
757
   if(provider == "base" || provider.empty())
2,170✔
758
      return std::make_unique<RSA_Signature_Operation>(*this, params, rng);
1,101✔
759

760
   throw Provider_Not_Found(algo_name(), provider);
1,602✔
761
}
762

763
}
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