Botan 3.14.1
Crypto and TLS for C&
rsa.cpp
Go to the documentation of this file.
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/mutex.h>
13#include <botan/numthry.h>
14#include <botan/pss_params.h>
15#include <botan/internal/algorithm_spec.h>
16#include <botan/internal/barrett.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/divide.h>
19#include <botan/internal/fmt.h>
20#include <botan/internal/keypair.h>
21#include <botan/internal/mod_inv.h>
22#include <botan/internal/monty.h>
23#include <botan/internal/monty_exp.h>
24#include <botan/internal/mp_core.h>
25#include <botan/internal/parsing.h>
26#include <botan/internal/pk_ops_impl.h>
27#include <botan/internal/pk_options_impl.h>
28#include <botan/internal/sig_padding.h>
29#include <botan/internal/target_info.h>
30#include <botan/internal/workfactor.h>
31
32#if defined(BOTAN_HAS_THREAD_UTILS)
33 #include <botan/internal/thread_pool.h>
34#endif
35
36#if defined(BOTAN_HAS_THREAD_UTILS) && !defined(BOTAN_HAS_VALGRIND)
37 #define BOTAN_RSA_USE_ASYNC
38#endif
39
40namespace Botan {
41
42namespace {
43
44// This is an arbitrary upper bound of the size of a RSA modulus
45constexpr size_t MAX_RSA_BITS = 16384;
46
47} // namespace
48
49class RSA_Public_Data final {
50 public:
51 RSA_Public_Data(BigInt&& n, BigInt&& e) :
52 m_n(std::move(n)),
53 m_e(std::move(e)),
54 m_mod_n(Barrett_Reduction::for_public_modulus(m_n)),
55 m_monty_n(m_n, m_mod_n),
56 m_public_modulus_bits(m_n.bits()),
57 m_public_modulus_bytes(m_n.bytes()) {}
58
59 BigInt public_op(const BigInt& m) const {
60 const size_t powm_window = 1;
61 const auto powm_m_n = monty_precompute(m_monty_n, m, powm_window);
62 return monty_execute_vartime(*powm_m_n, m_e).value();
63 }
64
65 const BigInt& get_n() const { return m_n; }
66
67 const BigInt& get_e() const { return m_e; }
68
69 size_t public_modulus_bits() const { return m_public_modulus_bits; }
70
71 size_t public_modulus_bytes() const { return m_public_modulus_bytes; }
72
73 const Montgomery_Params& monty_n() const { return m_monty_n; }
74
75 const Barrett_Reduction& reducer_mod_n() const { return m_mod_n; }
76
77 private:
78 BigInt m_n;
79 BigInt m_e;
80 Barrett_Reduction m_mod_n;
81 const Montgomery_Params m_monty_n;
82 size_t m_public_modulus_bits;
83 size_t m_public_modulus_bytes;
84};
85
86namespace {
87
88/*
89* To recover the final value from the CRT representation (j1,j2)
90* we use Garner's algorithm:
91* c = q^-1 mod p (this is precomputed)
92* h = c*(j1-j2) mod p
93* r = h*q + j2
94*/
95BigInt crt_recombine(const Montgomery_Int& j1,
96 const Montgomery_Int& j2_p,
97 const BigInt& j2,
98 const Montgomery_Int& c_monty,
99 const BigInt& p,
100 const BigInt& q) {
101 // We skip CRT entirely if the primes are not balanced (same bitlength) so q is also of this size
102 const size_t p_words = p.sig_words();
103 BOTAN_ASSERT_NOMSG(p_words == q.sig_words());
104
105 const size_t n_words = 2 * p_words;
106
107 // Ensure sufficient storage
108 BOTAN_ASSERT_NOMSG(j1.repr().size() >= p_words);
109 BOTAN_ASSERT_NOMSG(j2_p.repr().size() >= p_words);
110 BOTAN_ASSERT_NOMSG(j2.size() >= p_words);
111
112 /*
113 * Compute h = (j1 - j2) * c mod p
114 *
115 * This doesn't quite match up with the "Smooth-CRT" proposal; there we would
116 * multiply by a precomputed c * R2, which would have the effect of both
117 * multiplying by c and immediately converting from Montgomery to standard form.
118 */
119 secure_vector<word> ws(2 * p_words);
120
121 const Montgomery_Int h_monty = (j1 - j2_p).mul(c_monty, ws);
122
123 const BigInt h = h_monty.value();
124 // Montgomery_Int always returns values sized to the modulus
125 BOTAN_ASSERT_NOMSG(h.size() >= p_words);
126 BOTAN_DEBUG_ASSERT(h.sig_words() <= p_words);
127
128 // Compute r = h * q
129 secure_vector<word> r(2 * p_words);
130
131 bigint_mul(r.data(), r.size(), h._data(), h.size(), p_words, q._data(), q.size(), p_words, ws.data(), ws.size());
132
133 // r += j2
134 const word carry = bigint_add2(r.data(), n_words, j2._data(), p_words);
135 // should not be possible since it would imply r > the public modulus
137
138 return BigInt::_from_words(r);
139}
140
141/*
142* Compute d + m*(p - 1), with lengths that depend only on the sizes of the
143* inputs, not on their values
144*/
145BigInt blind_exponent(const BigInt& d, const BigInt& m, const BigInt& p_minus_1) {
146 const size_t m_words = m.size();
147 const size_t p_words = p_minus_1.size();
148
149 secure_vector<word> z(m_words + p_words + 1);
150 BOTAN_ASSERT_NOMSG(d.size() <= z.size());
151
152 bigint_mul(z.data(), z.size(), m._data(), m_words, m_words, p_minus_1._data(), p_words, p_words, nullptr, 0);
153 const word carry = bigint_add2(z.data(), z.size(), d._data(), d.size());
155
156 return BigInt::_from_words(z);
157}
158
159} // namespace
160
161class RSA_Private_Data final {
162 public:
163 RSA_Private_Data(std::shared_ptr<const RSA_Public_Data> pub,
164 BigInt&& d,
165 BigInt&& p,
166 BigInt&& q,
167 BigInt&& d1,
168 BigInt&& d2,
169 BigInt&& c) :
170 m_public(std::move(pub)),
171 m_d(std::move(d)),
172 m_p(std::move(p)),
173 m_q(std::move(q)),
174 m_d1(std::move(d1)),
175 m_d2(std::move(d2)),
176 m_c(std::move(c)),
177 m_p_minus_1(m_p - 1),
178 m_q_minus_1(m_q - 1),
179 m_monty_p(m_p),
180 m_monty_q(m_q),
181 m_c_monty(m_monty_p, m_c),
182 m_p_bits(m_p.bits()),
183 m_q_bits(m_q.bits()) {}
184
185 const BigInt& get_d() const { return m_d; }
186
187 const BigInt& get_p() const { return m_p; }
188
189 const BigInt& get_q() const { return m_q; }
190
191 const BigInt& get_d1() const { return m_d1; }
192
193 const BigInt& get_d2() const { return m_d2; }
194
195 BigInt blinded_d1(const BigInt& m) const { return blind_exponent(m_d1, m, m_p_minus_1); }
196
197 BigInt blinded_d2(const BigInt& m) const { return blind_exponent(m_d2, m, m_q_minus_1); }
198
199 const BigInt& get_c() const { return m_c; }
200
201 const Montgomery_Int& get_c_monty() const { return m_c_monty; }
202
203 const Montgomery_Params& monty_p() const { return m_monty_p; }
204
205 const Montgomery_Params& monty_q() const { return m_monty_q; }
206
207 size_t p_bits() const { return m_p_bits; }
208
209 size_t q_bits() const { return m_q_bits; }
210
211 bool primes_imbalanced() const { return p_bits() != q_bits(); }
212
213 void _const_time_poison() const {
214 CT::poison_all(m_d,
215 m_p,
216 m_q,
217 m_d1,
218 m_d2,
219 m_c,
220 m_p_minus_1,
221 m_q_minus_1,
222 m_monty_p,
223 m_monty_q,
224 m_c_monty,
225 m_blinding_fwd,
226 m_blinding_inv);
227 }
228
229 void _const_time_unpoison() const {
231 m_p,
232 m_q,
233 m_d1,
234 m_d2,
235 m_c,
236 m_p_minus_1,
237 m_q_minus_1,
238 m_monty_p,
239 m_monty_q,
240 m_c_monty,
241 m_blinding_fwd,
242 m_blinding_inv);
243 }
244
245 /**
246 * Return the next pair of base blinding values (k^e mod n, k^-1 mod n)
247 *
248 * The blinding sequence is shared by all private key operations using this
249 * key. Each pair is the square of the previous one; after BlindingReinitInterval
250 * uses a fresh random k is chosen.
251 */
252 std::pair<BigInt, BigInt> next_blinding_pair(RandomNumberGenerator& rng) const {
253 const lock_guard_type<mutex_type> lock(m_blinding_mutex);
254
255 if(m_blinding_uses == 0 || m_blinding_uses >= BlindingReinitInterval) {
256 // TODO the fresh pair could be computed ahead of time (eg in the thread
257 // pool) so that no operation has to wait for it
258 const BigInt k(rng, m_public->public_modulus_bits() - 1);
259 CT::poison(k);
260 auto [fwd, inv] = fresh_blinding_pair(k);
261 m_blinding_fwd = std::move(fwd);
262 m_blinding_inv = std::move(inv);
263 m_blinding_uses = 0;
264 } else {
265 const auto& mod_n = m_public->reducer_mod_n();
266 m_blinding_fwd = mod_n.square(m_blinding_fwd);
267 m_blinding_inv = mod_n.square(m_blinding_inv);
268 }
269
270 m_blinding_uses += 1;
271 return {m_blinding_fwd, m_blinding_inv};
272 }
273
274 private:
275 static constexpr size_t BlindingReinitInterval = 64;
276
277 // (k^e mod n, k^-1 mod n) for a fresh blinding nonce k
278 std::pair<BigInt, BigInt> fresh_blinding_pair(const BigInt& k) const {
279 if(primes_imbalanced()) {
280 return {m_public->public_op(k), inverse_mod_rsa_public_modulus(k, m_public->get_n())};
281 }
282 return crt_blinding_pair(m_public->get_e(), k);
283 }
284
285 /*
286 * (k^e mod n, k^-1 mod n) via the CRT, with the two halves computed in
287 * parallel when possible. The exponent e is public so variable time
288 * exponentiation is fine; the two half-size inversions are ~2x cheaper
289 * than a single inversion modulo n.
290 */
291 std::pair<BigInt, BigInt> crt_blinding_pair(const BigInt& e, const BigInt& k) const {
292 const auto kp_monty = Montgomery_Int::from_wide_int(m_monty_p, k);
293 const auto kq_monty = Montgomery_Int::from_wide_int(m_monty_q, k);
294 const BigInt kp = kp_monty.value();
295 const BigInt kq = kq_monty.value();
296
297 // k is random and less than n, so this means we stumbled onto a factor of n
298 if(kp.is_zero() || kq.is_zero()) {
299 throw Internal_Error("Accidentally factored the public modulus");
300 }
301
302 // Returns (k^e, k^-1) modulo the prime, the former still in Montgomery form
303 auto half = [&e](const Montgomery_Int& k_monty, const BigInt& k_mod, const BigInt& prime) {
304 const size_t powm_window = 1;
305 auto fwd = monty_execute_vartime(*monty_precompute(k_monty, powm_window), e);
306 auto inv = inverse_mod_secret_prime(k_mod, prime);
307 return std::make_pair(std::move(fwd), std::move(inv));
308 };
309
310#if defined(BOTAN_RSA_USE_ASYNC)
311 // Precompute e.sig_words in the main thread, see rsa_private_op
312 e.sig_words();
313
314 auto future_p = Thread_Pool::global_instance().run([&]() { return half(kp_monty, kp, m_p); });
315#endif
316
317 const auto [fwd_q, inv_q] = half(kq_monty, kq, m_q);
318
319#if defined(BOTAN_RSA_USE_ASYNC)
320 const auto [fwd_p, inv_p] = future_p.get();
321#else
322 const auto [fwd_p, inv_p] = half(kp_monty, kp, m_p);
323#endif
324
325 const BigInt fwd_q_v = fwd_q.value();
326 const Montgomery_Int inv_p_monty(m_monty_p, inv_p);
327
328 return {
329 crt_recombine(fwd_p, Montgomery_Int::from_wide_int(m_monty_p, fwd_q_v), fwd_q_v, m_c_monty, m_p, m_q),
330 crt_recombine(inv_p_monty, Montgomery_Int::from_wide_int(m_monty_p, inv_q), inv_q, m_c_monty, m_p, m_q)};
331 }
332
333 std::shared_ptr<const RSA_Public_Data> m_public;
334 BigInt m_d;
335 BigInt m_p;
336 BigInt m_q;
337 BigInt m_d1;
338 BigInt m_d2;
339 BigInt m_c;
340 BigInt m_p_minus_1;
341 BigInt m_q_minus_1;
342
343 const Montgomery_Params m_monty_p;
344 const Montgomery_Params m_monty_q;
345 Montgomery_Int m_c_monty;
346 size_t m_p_bits;
347 size_t m_q_bits;
348
349 mutable mutex_type m_blinding_mutex;
350 mutable BigInt m_blinding_fwd;
351 mutable BigInt m_blinding_inv;
352 mutable size_t m_blinding_uses = 0;
353};
354
355std::shared_ptr<const RSA_Public_Data> RSA_PublicKey::public_data() const {
356 return m_public;
357}
358
359const BigInt& RSA_PublicKey::get_int_field(std::string_view field) const {
360 if(field == "n") {
361 return m_public->get_n();
362 } else if(field == "e") {
363 return m_public->get_e();
364 } else {
365 return Public_Key::get_int_field(field);
366 }
367}
368
369std::unique_ptr<Private_Key> RSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
370 return std::make_unique<RSA_PrivateKey>(rng, m_public->public_modulus_bits(), 65537);
371}
372
374 return m_public->get_n();
375}
376
378 return m_public->get_e();
379}
380
382 if(n.signum() <= 0 || n.is_even() || n.bits() < 384 || n.bits() > MAX_RSA_BITS) {
383 throw Decoding_Error("Invalid RSA public key modulus");
384 }
385 if(e.is_even() || e <= 1 || e >= n || e.bits() > 256) {
386 throw Decoding_Error("Invalid RSA public key exponent");
387 }
388 m_public = std::make_shared<RSA_Public_Data>(std::move(n), std::move(e));
389}
390
391RSA_PublicKey::RSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
392 // RFC 4055 Section 1.2 has that parameters MUST be NULL, but historical
393 // reasons make that difficult to enforce, so absent is also accepted.
394 //
395 // This only checks rsaEncryption; PSS/OAEP key identifiers have their own parameter encoding
396 if(alg_id.oid().registered_name() == "RSA" && !alg_id.parameters_are_null_or_empty()) {
397 throw Decoding_Error("Unexpected parameters for RSA public key");
398 }
399
400 BigInt n;
401 BigInt e;
403
404 init(std::move(n), std::move(e));
405}
406
411
412RSA_PublicKey::RSA_PublicKey(const BigInt& modulus, const BigInt& exponent) {
413 BigInt n = modulus;
414 BigInt e = exponent;
415 init(std::move(n), std::move(e));
416}
417
419 return m_public->public_modulus_bits();
420}
421
425
429
430std::vector<uint8_t> RSA_PublicKey::raw_public_key_bits() const {
431 throw Not_Implemented("an RSA public key does not provide a raw binary representation.");
432}
433
434std::vector<uint8_t> RSA_PublicKey::public_key_bits() const {
435 std::vector<uint8_t> output;
436 DER_Encoder der(output);
438
439 return output;
440}
441
442/*
443* Check RSA Public Parameters
444*/
445bool RSA_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
446 if(get_n() < 35 || get_n().is_even() || get_e() < 3 || get_e().is_even()) {
447 return false;
448 }
449 return true;
450}
451
452std::shared_ptr<const RSA_Private_Data> RSA_PrivateKey::private_data() const {
453 return m_private;
454}
455
457 return DER_Encoder()
459 .encode(static_cast<size_t>(0))
460 .encode(get_n())
461 .encode(get_e())
462 .encode(get_d())
463 .encode(get_p())
464 .encode(get_q())
465 .encode(get_d1())
466 .encode(get_d2())
467 .encode(get_c())
468 .end_cons()
469 .get_contents();
470}
471
473 return m_private->get_p();
474}
475
477 return m_private->get_q();
478}
479
481 return m_private->get_d();
482}
483
485 return m_private->get_c();
486}
487
489 return m_private->get_d1();
490}
491
493 return m_private->get_d2();
494}
495
496namespace {
497
498void check_rsa_private_key_params(const BigInt& n, const BigInt& p, const BigInt& q) {
499 if(n.bits() > MAX_RSA_BITS || p < 3 || q < 3 || p == q || p >= n || q >= n) {
500 throw Decoding_Error("Invalid RSA private key parameters");
501 }
502 if(p * q != n) {
503 throw Decoding_Error("Invalid RSA private key: p * q != n");
504 }
505}
506
507/*
508* Verify e*d == 1 mod lcm(p-1, q-1). Checking modulo p-1 and q-1 separately
509* is equivalent and avoids the (costly, constant time) gcd needed for the lcm.
510*/
511void check_rsa_secret_exponent(const BigInt& e, const BigInt& d, const BigInt& p, const BigInt& q) {
512 const auto scope = CT::scoped_poison(d, p, q);
513 const BigInt ed = e * d;
514 if(ct_modulo(ed, p - 1) != 1 || ct_modulo(ed, q - 1) != 1) {
515 throw Decoding_Error("Invalid RSA private key: e * d != 1 mod lcm(p-1, q-1)");
516 }
517}
518
519} // namespace
520
521void RSA_PrivateKey::init(BigInt&& d, BigInt&& p, BigInt&& q, BigInt&& d1, BigInt&& d2, BigInt&& c) {
522 check_rsa_private_key_params(get_n(), p, q);
523
524 /*
525 * RFC 8017 Section 3.2
526 *
527 * The RSA private exponent d is a positive integer less than n
528 * [...]
529 * the CRT exponents dP and dQ are positive integers less than p and q,
530 * respectively,
531 * [...]
532 * the CRT coefficient qInv is a positive integer less than p
533 */
534 if(d < 2 || d >= get_n()) {
535 throw Decoding_Error("Invalid RSA private key exponent");
536 }
537 if(d1 < 1 || d1 >= p || d2 < 1 || d2 >= q || c < 1 || c >= p) {
538 throw Decoding_Error("Invalid RSA private key CRT parameters");
539 }
540
541 m_private = std::make_shared<RSA_Private_Data>(
542 m_public, std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
543}
544
545RSA_PrivateKey::RSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
546 if(alg_id.oid().registered_name() == "RSA" && !alg_id.parameters_are_null_or_empty()) {
547 throw Decoding_Error("Unexpected parameters for RSA private key");
548 }
549
550 BigInt n;
551 BigInt e;
552 BigInt d;
553 BigInt p;
554 BigInt q;
555 BigInt d1;
556 BigInt d2;
557 BigInt c;
558
561 .decode_and_check<size_t>(0, "Unknown PKCS #1 key format version")
562 .decode(n)
563 .decode(e)
564 .decode(d)
565 .decode(p)
566 .decode(q)
567 .decode(d1)
568 .decode(d2)
569 .decode(c)
570 .end_cons()
571 .verify_end();
572
573 RSA_PublicKey::init(std::move(n), std::move(e));
574
575 RSA_PrivateKey::init(std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
576
577 check_rsa_secret_exponent(get_e(), get_d(), get_p(), get_q());
578}
579
581 const BigInt& prime1, const BigInt& prime2, const BigInt& exp, const BigInt& d_exp, const BigInt& mod) {
582 BigInt p = prime1;
583 BigInt q = prime2;
584 BigInt n = mod;
585 if(n.is_zero()) {
586 n = p * q;
587 }
588
589 BigInt e = exp;
590
591 RSA_PublicKey::init(std::move(n), std::move(e));
592
593 // Validated early since the arithmetic below depends on it
594 check_rsa_private_key_params(get_n(), p, q);
595
596 BigInt d = d_exp;
597 const bool compute_d = d.is_zero();
598
599 if(!compute_d) {
600 check_rsa_secret_exponent(get_e(), d, p, q);
601 }
602
603 CT::poison_all(p, q, d);
604
605 const BigInt p_minus_1 = p - 1;
606 const BigInt q_minus_1 = q - 1;
607
608 if(compute_d) {
609 const BigInt phi_n = lcm(p_minus_1, q_minus_1);
610 if(auto d_inv = compute_rsa_secret_exponent(get_e(), phi_n, p, q)) {
611 d = std::move(*d_inv);
612 } else {
613 throw Decoding_Error("Invalid RSA private key: e has no inverse modulo lcm(p-1, q-1)");
614 }
615 }
616
617 BigInt d1 = ct_modulo(d, p_minus_1);
618 BigInt d2 = ct_modulo(d, q_minus_1);
619
620 const BigInt q_mod_p = ct_modulo(q, p);
621 if(q_mod_p.is_zero()) {
622 throw Decoding_Error("Invalid RSA private key: p divides q");
623 }
624 BigInt c = inverse_mod_secret_prime(q_mod_p, p);
625
626 CT::unpoison_all(p, q, d, d1, d2, c);
627
628 RSA_PrivateKey::init(std::move(d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
629}
630
631/*
632* Create a RSA private key
633*/
635 constexpr size_t MIN_RSA_BITS = 1024;
636 constexpr size_t MOD_RSA_BITS = 8;
637
638 if(bits < MIN_RSA_BITS) {
639 throw Invalid_Argument(fmt("Cannot create an RSA key of {} bits: must be at least {} bits", bits, MIN_RSA_BITS));
640 } else if(bits > MAX_RSA_BITS) {
641 throw Invalid_Argument(
642 fmt("Cannot create an RSA key of {} bits: must be no more than {} bits", bits, MAX_RSA_BITS));
643 } else if(bits % MOD_RSA_BITS != 0) {
644 throw Invalid_Argument(
645 fmt("Cannot create an RSA key of {} bits: must be a multiple of {} bits", bits, MOD_RSA_BITS));
646 }
647
648 if(exp < 3 || exp % 2 == 0) {
649 throw Invalid_Argument("Invalid RSA encryption exponent");
650 }
651
652 const size_t p_bits = (bits + 1) / 2;
653 const size_t q_bits = bits - p_bits;
654
655 BigInt p;
656 BigInt q;
657 BigInt n;
658 BigInt e = BigInt::from_u64(exp);
659
660 for(size_t attempt = 0;; ++attempt) {
661 if(attempt > 10) {
662 throw Internal_Error("RNG failure during RSA key generation");
663 }
664
665 // TODO could generate primes in thread pool
666 p = generate_rsa_prime(rng, rng, p_bits, e);
667 q = generate_rsa_prime(rng, rng, q_bits, e);
668 CT::poison_all(p, q);
669
670 const BigInt diff = p - q;
671 if(diff.bits() < (bits / 2) - 100) {
672 continue;
673 }
674
675 n = p * q;
676 CT::unpoison(n);
677
678 if(n.bits() != bits) {
679 continue;
680 }
681
682 break;
683 }
684
685 const BigInt p_minus_1 = p - 1;
686 const BigInt q_minus_1 = q - 1;
687
688 const BigInt phi_n = lcm(p_minus_1, q_minus_1);
689 // This is guaranteed because p,q == 3 mod 4
691
692 // generate_rsa_prime ensures gcd(e, p-1) == gcd(e, q-1) == 1 so the inverse exists
693 auto d = compute_rsa_secret_exponent(e, phi_n, p, q);
694 BOTAN_ASSERT_NOMSG(d.has_value());
695
696 BigInt d1 = ct_modulo(*d, p_minus_1);
697 BigInt d2 = ct_modulo(*d, q_minus_1);
699
700 CT::unpoison_all(p, q, *d, d1, d2, c);
701
702 RSA_PublicKey::init(std::move(n), std::move(e));
703
704 RSA_PrivateKey::init(std::move(*d), std::move(p), std::move(q), std::move(d1), std::move(d2), std::move(c));
705}
706
707const BigInt& RSA_PrivateKey::get_int_field(std::string_view field) const {
708 if(field == "p") {
709 return m_private->get_p();
710 } else if(field == "q") {
711 return m_private->get_q();
712 } else if(field == "d") {
713 return m_private->get_d();
714 } else if(field == "c") {
715 return m_private->get_c();
716 } else if(field == "d1") {
717 return m_private->get_d1();
718 } else if(field == "d2") {
719 return m_private->get_d2();
720 } else {
721 return RSA_PublicKey::get_int_field(field);
722 }
723}
724
725std::unique_ptr<Public_Key> RSA_PrivateKey::public_key() const {
726 return std::make_unique<RSA_PublicKey>(get_n(), get_e());
727}
728
729/*
730* Check Private RSA Parameters
731*/
733 if(get_n() < 35 || get_n().is_even() || get_e() < 3 || get_e().is_even()) {
734 return false;
735 }
736
737 const auto scope = CT::scoped_poison(*m_private);
738
739 if(get_d() < 2 || get_p() < 3 || get_q() < 3) {
740 return false;
741 }
742
743 if(get_p() * get_q() != get_n()) {
744 return false;
745 }
746
747 if(get_p() == get_q()) {
748 return false;
749 }
750
751 if(get_d1() != ct_modulo(get_d(), get_p() - 1)) {
752 return false;
753 }
754 if(get_d2() != ct_modulo(get_d(), get_q() - 1)) {
755 return false;
756 }
758 return false;
759 }
760
761 const size_t prob = (strong) ? 128 : 12;
762
763 if(!is_prime(get_p(), rng, prob)) {
764 return false;
765 }
766 if(!is_prime(get_q(), rng, prob)) {
767 return false;
768 }
769
770 if(strong) {
771 if(ct_modulo(get_e() * get_d(), lcm(get_p() - 1, get_q() - 1)) != 1) {
772 return false;
773 }
774
775#if defined(BOTAN_HAS_PSS) && defined(BOTAN_HAS_SHA_256)
776 const std::string padding = "PSS(SHA-256)";
777#else
778 const std::string padding = "Raw";
779#endif
780
781 return KeyPair::signature_consistency_check(rng, *this, padding);
782 }
783
784 return true;
785}
786
787namespace {
788
789/**
790* RSA private (decrypt/sign) operation
791*/
792class RSA_Private_Operation {
793 protected:
794 size_t public_modulus_bits() const { return m_public->public_modulus_bits(); }
795
796 size_t public_modulus_bytes() const { return m_public->public_modulus_bytes(); }
797
798 explicit RSA_Private_Operation(const RSA_PrivateKey& rsa, RandomNumberGenerator& rng) :
799 m_public(rsa.public_data()),
800 m_private(rsa.private_data()),
801 m_rng(rng),
802 m_blinding_bits(64),
803 m_max_d1_bits(m_private->p_bits() + m_blinding_bits),
804 m_max_d2_bits(m_private->q_bits() + m_blinding_bits) {}
805
806 void raw_op(std::span<uint8_t> out, std::span<const uint8_t> input) {
807 // These early exits are fine because the invalidity is based only
808 // on public information, namely the ciphertext and the public modulus
809 if(input.size() > public_modulus_bytes()) {
810 throw Decoding_Error("RSA input is too long for this key");
811 }
812 const BigInt input_bn(input.data(), input.size());
813 if(input_bn.is_zero() || input_bn >= m_public->get_n()) {
814 throw Decoding_Error("RSA input is not in the valid range");
815 }
816
817 const auto scope = CT::scoped_poison(*m_private);
818
819 const auto [blind_fwd, blind_inv] = m_private->next_blinding_pair(m_rng);
820 const auto& mod_n = m_public->reducer_mod_n();
821
822 const BigInt recovered = mod_n.multiply(rsa_private_op(mod_n.multiply(input_bn, blind_fwd)), blind_inv);
823 // Any further secret handling of the output is up to the caller
824 CT::unpoison(recovered);
825 BOTAN_ASSERT(input_bn == m_public->public_op(recovered), "RSA consistency check");
826 BOTAN_ASSERT(m_public->public_modulus_bytes() == out.size(), "output size check");
827 recovered.serialize_to(out);
828 }
829
830 private:
831 BigInt rsa_private_op(const BigInt& m) const {
832 /*
833 All normal implementations generate p/q of the same bitlength,
834 so this should rarely occur in practice
835 */
836 if(m_private->primes_imbalanced()) {
837 return monty_exp(m_public->monty_n(), m, m_private->get_d(), m_public->get_n().bits()).value();
838 }
839
840 static constexpr size_t powm_window = 4;
841
842 // Compute this in main thread to avoid racing on the rng
843 const BigInt d1_mask(m_rng, m_blinding_bits);
844 CT::poison(d1_mask);
845
846#if defined(BOTAN_RSA_USE_ASYNC)
847 /*
848 * Precompute m.sig_words in the main thread before calling async. Otherwise
849 * the two threads race (during Barrett_Reduction::reduce) and while the output
850 * is correct in both threads, helgrind warns.
851 */
852 m.sig_words();
853
854 auto future_j1 = Thread_Pool::global_instance().run([this, &m, &d1_mask]() {
855#endif
856 const BigInt masked_d1 = m_private->blinded_d1(d1_mask);
857 const auto powm_d1_p =
858 monty_precompute(Montgomery_Int::from_wide_int(m_private->monty_p(), m), powm_window);
859 auto j1 = monty_execute(*powm_d1_p, masked_d1, m_max_d1_bits);
860
861#if defined(BOTAN_RSA_USE_ASYNC)
862 return j1;
863 });
864#endif
865
866 const BigInt d2_mask(m_rng, m_blinding_bits);
867 CT::poison(d2_mask);
868 const BigInt masked_d2 = m_private->blinded_d2(d2_mask);
869 const auto powm_d2_q = monty_precompute(Montgomery_Int::from_wide_int(m_private->monty_q(), m), powm_window);
870 const auto j2 = monty_execute(*powm_d2_q, masked_d2, m_max_d2_bits).value();
871
872#if defined(BOTAN_RSA_USE_ASYNC)
873 const auto j1 = future_j1.get();
874#endif
875
876 // Reduce j2 modulo p
877 const auto j2_p = Montgomery_Int::from_wide_int(m_private->monty_p(), j2);
878
879 return crt_recombine(j1, j2_p, j2, m_private->get_c_monty(), m_private->get_p(), m_private->get_q());
880 }
881
882 std::shared_ptr<const RSA_Public_Data> m_public;
883 std::shared_ptr<const RSA_Private_Data> m_private;
884
885 RandomNumberGenerator& m_rng;
886 const size_t m_blinding_bits;
887 const size_t m_max_d1_bits;
888 const size_t m_max_d2_bits;
889};
890
891class RSA_Signature_Operation final : public PK_Ops::Signature,
892 private RSA_Private_Operation {
893 public:
894 void update(std::span<const uint8_t> msg) override { m_padding->update(msg.data(), msg.size()); }
895
896 std::vector<uint8_t> sign(RandomNumberGenerator& rng) override {
897 const size_t max_input_bits = public_modulus_bits() - 1;
898 const auto msg = m_padding->raw_data();
899 const auto padded = m_padding->encoding_of(msg, max_input_bits, rng);
900
901 std::vector<uint8_t> out(public_modulus_bytes());
902 raw_op(out, padded);
903 return out;
904 }
905
906 size_t signature_length() const override { return public_modulus_bytes(); }
907
908 AlgorithmIdentifier algorithm_identifier() const override;
909
910 std::string hash_function() const override {
911 BOTAN_ASSERT_NONNULL(m_padding);
912 return m_padding->hash_function();
913 }
914
915 RSA_Signature_Operation(const RSA_PrivateKey& rsa,
916 const PK_Signature_Options_Reader& options,
917 RandomNumberGenerator& rng) :
918 RSA_Private_Operation(rsa, rng), m_padding(SignaturePaddingScheme::create_or_throw(options)) {}
919
920 private:
921 std::unique_ptr<SignaturePaddingScheme> m_padding;
922};
923
924AlgorithmIdentifier RSA_Signature_Operation::algorithm_identifier() const {
925 const std::string padding_name = m_padding->name();
926
927 try {
928 const std::string full_name = "RSA/" + padding_name;
929 const OID oid = OID::from_string(full_name);
930 // RFC 8017 Appendix A.2 specifies RSA signatures for most hashes use NULL parameter
931 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_NULL_PARAM);
932 } catch(Lookup_Error&) {}
933
934 if(padding_name.starts_with("PSS(")) {
935 const auto parameters = PSS_Params::from_padding_name(m_padding->name()).serialize();
936 return AlgorithmIdentifier("RSA/PSS", parameters);
937 }
938
939 throw Invalid_Argument(fmt("Signatures using RSA/{} are not supported", padding_name));
940}
941
942class RSA_Decryption_Operation final : public PK_Ops::Decryption_with_Padding,
943 private RSA_Private_Operation {
944 public:
945 RSA_Decryption_Operation(const RSA_PrivateKey& rsa,
946 const PK_Encryption_Options_Reader& options,
947 RandomNumberGenerator& rng) :
948 PK_Ops::Decryption_with_Padding(options), RSA_Private_Operation(rsa, rng) {}
949
950 size_t plaintext_length(size_t /*ctext_len*/) const override { return public_modulus_bytes(); }
951
952 size_t ciphertext_length(size_t /*ptext_len*/) const override { return public_modulus_bytes(); }
953
954 secure_vector<uint8_t> raw_decrypt(std::span<const uint8_t> input) override {
955 /*
956 * RFC 8017 7.1.2 and 7.2.2
957 *
958 * If the length of the ciphertext C is not k octets, output
959 * "decryption error" and stop.
960 */
961 if(input.size() != public_modulus_bytes()) {
962 throw Decoding_Error("RSA ciphertext is an incorrect size for this public key");
963 }
964 secure_vector<uint8_t> out(public_modulus_bytes());
965 raw_op(out, input);
966 return out;
967 }
968};
969
970class RSA_KEM_Decryption_Operation final : public PK_Ops::KEM_Decryption_with_KDF,
971 private RSA_Private_Operation {
972 public:
973 RSA_KEM_Decryption_Operation(const RSA_PrivateKey& key,
974 const PK_KEM_Options_Reader& options,
975 RandomNumberGenerator& rng) :
976 PK_Ops::KEM_Decryption_with_KDF(options, PK_Ops::KemSharedKeyQuality::RequiresKDF),
977 RSA_Private_Operation(key, rng) {}
978
979 size_t raw_kem_shared_key_length() const override { return public_modulus_bytes(); }
980
981 size_t encapsulated_key_length() const override { return public_modulus_bytes(); }
982
983 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) override {
984 /*
985 * RFC 9690 Section 8
986 *
987 * The RSA-KEM algorithm provides a fixed-length ciphertext. The recipient MUST
988 * check that the received byte string is the expected length [...]
989 *
990 * This length check is based only on public information (the encapsulated key and
991 * the public modulus) so an early exit does not leak anything.
992 */
993 if(encapsulated_key.size() != public_modulus_bytes()) {
994 throw Decoding_Error("Invalid RSA-KEM ciphertext length");
995 }
996 raw_op(out_shared_key, encapsulated_key);
997 }
998};
999
1000/**
1001* RSA public (encrypt/verify) operation
1002*/
1003class RSA_Public_Operation {
1004 public:
1005 explicit RSA_Public_Operation(const RSA_PublicKey& rsa) : m_public(rsa.public_data()) {}
1006
1007 size_t public_modulus_bits() const { return m_public->public_modulus_bits(); }
1008
1009 protected:
1010 BigInt public_op(const BigInt& m) const {
1011 if(m >= m_public->get_n()) {
1012 throw Decoding_Error("RSA public op - input is too large");
1013 }
1014
1015 return m_public->public_op(m);
1016 }
1017
1018 size_t public_modulus_bytes() const { return m_public->public_modulus_bytes(); }
1019
1020 const BigInt& get_n() const { return m_public->get_n(); }
1021
1022 private:
1023 std::shared_ptr<const RSA_Public_Data> m_public;
1024};
1025
1026class RSA_Encryption_Operation final : public PK_Ops::Encryption_with_Padding,
1027 private RSA_Public_Operation {
1028 public:
1029 RSA_Encryption_Operation(const RSA_PublicKey& rsa, const PK_Encryption_Options_Reader& options) :
1030 PK_Ops::Encryption_with_Padding(options), RSA_Public_Operation(rsa) {}
1031
1032 size_t ciphertext_length(size_t /*ptext_len*/) const override { return public_modulus_bytes(); }
1033
1034 size_t max_ptext_input_bits() const override { return public_modulus_bits() - 1; }
1035
1036 std::vector<uint8_t> raw_encrypt(std::span<const uint8_t> input, RandomNumberGenerator& /*rng*/) override {
1037 const BigInt input_bn(input);
1038 CT::poison(input_bn);
1039 const BigInt c = public_op(input_bn);
1040 CT::unpoison(c);
1041 return c.serialize(public_modulus_bytes());
1042 }
1043};
1044
1045class RSA_Verify_Operation final : public PK_Ops::Verification,
1046 private RSA_Public_Operation {
1047 public:
1048 void update(std::span<const uint8_t> msg) override { m_padding->update(msg.data(), msg.size()); }
1049
1050 bool is_valid_signature(std::span<const uint8_t> sig) override {
1051 const auto msg = m_padding->raw_data();
1052 const auto message_repr = recover_message_repr(sig.data(), sig.size());
1053 return m_padding->verify(message_repr, msg, public_modulus_bits() - 1);
1054 }
1055
1056 RSA_Verify_Operation(const RSA_PublicKey& rsa, const PK_Signature_Options_Reader& options) :
1057 RSA_Public_Operation(rsa), m_padding(SignaturePaddingScheme::create_or_throw(options)) {}
1058
1059 std::string hash_function() const override { return m_padding->hash_function(); }
1060
1061 private:
1062 std::vector<uint8_t> recover_message_repr(const uint8_t input[], size_t input_len) {
1063 // RFC 8017 8.1.2 and 8.2.2 state
1064 // If the length of the signature S is not k octets,
1065 // output "invalid signature" and stop.
1066 //
1067 if(input_len != public_modulus_bytes()) {
1068 throw Decoding_Error("RSA signature is an incorrect size for this public key");
1069 }
1070 const BigInt input_bn(input, input_len);
1071 return public_op(input_bn).serialize();
1072 }
1073
1074 std::unique_ptr<SignaturePaddingScheme> m_padding;
1075};
1076
1077class RSA_KEM_Encryption_Operation final : public PK_Ops::KEM_Encryption_with_KDF,
1078 private RSA_Public_Operation {
1079 public:
1080 RSA_KEM_Encryption_Operation(const RSA_PublicKey& key, const PK_KEM_Options_Reader& options) :
1081 PK_Ops::KEM_Encryption_with_KDF(options, PK_Ops::KemSharedKeyQuality::RequiresKDF),
1082 RSA_Public_Operation(key) {}
1083
1084 private:
1085 size_t raw_kem_shared_key_length() const override { return public_modulus_bytes(); }
1086
1087 size_t encapsulated_key_length() const override { return public_modulus_bytes(); }
1088
1089 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
1090 std::span<uint8_t> raw_shared_key,
1091 RandomNumberGenerator& rng) override {
1092 const BigInt r = BigInt::random_integer(rng, BigInt::one(), get_n());
1093 CT::poison(r);
1094 const BigInt c = public_op(r);
1095 CT::unpoison(c);
1096
1097 c.serialize_to(out_encapsulated_key);
1098 r.serialize_to(raw_shared_key);
1099 CT::unpoison(raw_shared_key);
1100 }
1101};
1102
1103} // namespace
1104
1105std::unique_ptr<PK_Ops::Encryption> RSA_PublicKey::_create_encryption_op(
1106 RandomNumberGenerator& rng, const PK_Encryption_Options_Reader& options) const {
1107 BOTAN_UNUSED(rng);
1109
1110 return std::make_unique<RSA_Encryption_Operation>(*this, options);
1111}
1112
1113std::unique_ptr<PK_Ops::KEM_Encryption> RSA_PublicKey::_create_kem_encryption_op(
1114 const PK_KEM_Options_Reader& options) const {
1116
1117 return std::make_unique<RSA_KEM_Encryption_Operation>(*this, options);
1118}
1119
1120std::unique_ptr<PK_Ops::Verification> RSA_PublicKey::_create_verification_op(
1121 const PK_Signature_Options_Reader& options) const {
1123
1124 return std::make_unique<RSA_Verify_Operation>(*this, options);
1125}
1126
1127namespace {
1128
1129PK_Signature_Options parse_rsa_signature_algorithm(const AlgorithmIdentifier& alg_id) {
1130 const auto oid_name = alg_id.oid().registered_name();
1131 if(!oid_name) {
1132 throw Decoding_Error("Unknown AlgorithmIdentifier for RSA X.509 signatures");
1133 }
1134
1135 const auto sig_info = split_on(*oid_name, '/');
1136
1137 if(sig_info.empty() || sig_info.size() != 2 || sig_info[0] != "RSA") {
1138 throw Decoding_Error("Unknown AlgorithmIdentifier for RSA X.509 signatures");
1139 }
1140
1141 const std::string& padding = sig_info[1];
1142
1143 if(padding != "PSS") {
1144 if(!alg_id.parameters_are_null_or_empty()) {
1145 throw Decoding_Error("Non-PSS RSA signature algorithm OID has unexpected parameters");
1146 }
1147 }
1148
1149 if(padding == "PSS") {
1150 // "MUST contain RSASSA-PSS-params"
1151 if(alg_id.parameters().empty()) {
1152 throw Decoding_Error("PSS params must be provided");
1153 }
1154
1155 const PSS_Params pss_params(alg_id.parameters());
1156
1157 // hash_algo must be SHA1, SHA2-224, SHA2-256, SHA2-384 or SHA2-512
1158 // We also support SHA-3 (is also supported by e.g. OpenSSL and bouncycastle)
1159 const auto hash_algo = pss_params.hash_algid().oid().registered_name();
1160 if(hash_algo != "SHA-1" && hash_algo != "SHA-224" && hash_algo != "SHA-256" && hash_algo != "SHA-384" &&
1161 hash_algo != "SHA-512" && hash_algo != "SHA-3(224)" && hash_algo != "SHA-3(256)" &&
1162 hash_algo != "SHA-3(384)" && hash_algo != "SHA-3(512)") {
1163 throw Decoding_Error("Unacceptable hash for PSS signatures");
1164 }
1165
1166 if(pss_params.mgf_algid().oid().registered_name() != "MGF1") {
1167 throw Decoding_Error("Unacceptable MGF for PSS signatures");
1168 }
1169
1170 // For MGF1, it is strongly RECOMMENDED that the underlying hash
1171 // function be the same as the one identified by hashAlgorithm
1172 if(pss_params.hash_algid() != pss_params.mgf_hash_algid()) {
1173 throw Decoding_Error("Unacceptable MGF hash for PSS signatures");
1174 }
1175
1176 if(pss_params.trailer_field() != 1) {
1177 throw Decoding_Error("Unacceptable trailer field for PSS signatures");
1178 }
1179
1180 return PK_Signature_Options().with_padding("PSS").with_hash(*hash_algo).with_salt_size(pss_params.salt_length());
1181 } else {
1182 const AlgorithmSpec spec(padding);
1183
1184 const auto m = spec.match("PKCS1v15({hash})");
1185 if(!m) {
1186 throw Decoding_Error("Unexpected OID for RSA signatures");
1187 }
1188
1189 return PK_Signature_Options().with_padding("PKCS1v15").with_hash(m->str("hash"));
1190 }
1191}
1192
1193} // namespace
1194
1195std::unique_ptr<PK_Ops::Verification> RSA_PublicKey::create_x509_verification_op(const AlgorithmIdentifier& alg_id,
1196 std::string_view provider) const {
1197 if(provider == "base" || provider.empty()) {
1198 return std::make_unique<RSA_Verify_Operation>(
1199 *this, PK_Options_Reader_Access::for_verification(parse_rsa_signature_algorithm(alg_id)));
1200 }
1201
1202 throw Provider_Not_Found(algo_name(), provider);
1203}
1204
1205std::unique_ptr<PK_Ops::Decryption> RSA_PrivateKey::_create_decryption_op(
1206 RandomNumberGenerator& rng, const PK_Encryption_Options_Reader& options) const {
1208
1209 return std::make_unique<RSA_Decryption_Operation>(*this, options, rng);
1210}
1211
1212std::unique_ptr<PK_Ops::KEM_Decryption> RSA_PrivateKey::_create_kem_decryption_op(
1213 RandomNumberGenerator& rng, const PK_KEM_Options_Reader& options) const {
1215
1216 return std::make_unique<RSA_KEM_Decryption_Operation>(*this, options, rng);
1217}
1218
1219std::unique_ptr<PK_Ops::Signature> RSA_PrivateKey::_create_signature_op(
1220 RandomNumberGenerator& rng, const PK_Signature_Options_Reader& options) const {
1222
1223 return std::make_unique<RSA_Signature_Operation>(*this, options, rng);
1224}
1225
1226} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
bool parameters_are_null_or_empty() const
Definition asn1_obj.h:720
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:693
const OID & oid() const
Definition asn1_obj.h:688
virtual const BigInt & get_int_field(std::string_view field) const
Definition pk_keys.cpp:20
virtual OID object_identifier() const
Definition pk_keys.cpp:24
static Limits DER()
Definition ber_dec.h:44
BER_Decoder & decode(bool &out)
Definition ber_dec.h:361
BER_Decoder & verify_end()
Definition ber_dec.cpp:471
BER_Decoder & end_cons()
Definition ber_dec.cpp:635
BER_Decoder start_sequence()
Definition ber_dec.h:279
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:698
size_t sig_words() const
Definition bigint.h:687
size_t size() const
Definition bigint.h:681
static BigInt _from_words(secure_vector< word > &words)
Definition bigint.h:1052
size_t bits() const
Definition bigint.cpp:308
static BigInt from_u64(uint64_t n)
Definition bigint.cpp:31
bool is_zero() const
Definition bigint.h:510
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:161
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
static Montgomery_Int from_wide_int(const Montgomery_Params &params, const BigInt &x)
Definition monty.cpp:241
BigInt value() const
Definition monty.cpp:279
std::optional< std::string > registered_name() const
Definition asn1_oid.cpp:149
virtual void update(std::span< const uint8_t > input)=0
static PK_Signature_Options_Reader for_verification(const PK_Signature_Options &options)
const BigInt & get_q() const
Definition rsa.cpp:476
const BigInt & get_int_field(std::string_view field) const override
Definition rsa.cpp:707
std::shared_ptr< const RSA_Private_Data > private_data() const
Definition rsa.cpp:452
const BigInt & get_c() const
Definition rsa.cpp:484
RSA_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition rsa.cpp:545
const BigInt & get_p() const
Definition rsa.cpp:472
const BigInt & get_d2() const
Definition rsa.cpp:492
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition rsa.cpp:732
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
Definition rsa.cpp:1219
const BigInt & get_d() const
Definition rsa.cpp:480
std::unique_ptr< PK_Ops::KEM_Decryption > _create_kem_decryption_op(RandomNumberGenerator &rng, const PK_KEM_Options_Reader &options) const override
Definition rsa.cpp:1212
secure_vector< uint8_t > private_key_bits() const override
Definition rsa.cpp:456
std::unique_ptr< Public_Key > public_key() const override
Definition rsa.cpp:725
const BigInt & get_d1() const
Definition rsa.cpp:488
std::unique_ptr< PK_Ops::Decryption > _create_decryption_op(RandomNumberGenerator &rng, const PK_Encryption_Options_Reader &options) const override
Definition rsa.cpp:1205
std::unique_ptr< PK_Ops::Encryption > _create_encryption_op(RandomNumberGenerator &rng, const PK_Encryption_Options_Reader &options) const override
Definition rsa.cpp:1105
std::unique_ptr< PK_Ops::KEM_Encryption > _create_kem_encryption_op(const PK_KEM_Options_Reader &options) const override
Definition rsa.cpp:1113
void init(BigInt &&n, BigInt &&e)
Definition rsa.cpp:381
size_t key_length() const override
Definition rsa.cpp:418
std::string algo_name() const override
Definition rsa.h:41
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &alg_id, std::string_view provider) const override
Definition rsa.cpp:1195
const BigInt & get_int_field(std::string_view field) const override
Definition rsa.cpp:359
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition rsa.cpp:445
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
Definition rsa.cpp:1120
const BigInt & get_n() const
Definition rsa.cpp:373
size_t estimated_strength() const override
Definition rsa.cpp:422
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const override
Definition rsa.cpp:369
std::vector< uint8_t > raw_public_key_bits() const override
Definition rsa.cpp:430
AlgorithmIdentifier algorithm_identifier() const override
Definition rsa.cpp:426
std::vector< uint8_t > public_key_bits() const override
Definition rsa.cpp:434
std::shared_ptr< const RSA_Public_Data > m_public
Definition rsa.h:90
std::shared_ptr< const RSA_Public_Data > public_data() const
Definition rsa.cpp:355
const BigInt & get_e() const
Definition rsa.cpp:377
bool supports_operation(PublicKeyOperation op) const override
Definition rsa.cpp:407
auto run(F &&f, Args &&... args) -> Joining_Future< std::invoke_result_t< F, Args... > >
static Thread_Pool & global_instance()
constexpr decltype(auto) driveby_unpoison(T &&v)
Definition ct_utils.h:243
constexpr void poison_all(const Ts &... ts)
Definition ct_utils.h:201
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:207
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49
BigInt inverse_mod_secret_prime(const BigInt &x, const BigInt &p)
Definition mod_inv.cpp:278
constexpr auto bigint_add2(W x[], size_t x_size, const W y[], size_t y_size) -> W
Definition mp_core.h:98
noop_mutex mutex_type
Definition mutex.h:40
std::optional< BigInt > compute_rsa_secret_exponent(const BigInt &e, const BigInt &phi_n, const BigInt &p, const BigInt &q)
Definition mod_inv.cpp:337
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
BigInt lcm(const BigInt &a, const BigInt &b)
Definition numthry.cpp:296
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:83
size_t low_zero_bits(const BigInt &n)
Definition numthry.cpp:194
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
Definition mp_karat.cpp:283
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:145
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:449
std::shared_ptr< const Montgomery_Exponentiation_State > monty_precompute(const Montgomery_Int &g, size_t window_bits)
BigInt ct_modulo(const BigInt &x, const BigInt &y)
Definition divide.cpp:262
void require_software_provider(const OptionsT &options, std::string_view algo_name)
PublicKeyOperation
Definition pk_keys.h:50
BigInt generate_rsa_prime(RandomNumberGenerator &keygen_rng, RandomNumberGenerator &prime_test_rng, size_t bits, const BigInt &coprime, size_t prob)
Definition make_prm.cpp:252
Montgomery_Int monty_execute_vartime(const Montgomery_Exponentiation_State &precomputed_state, const BigInt &k)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
lock_guard< T > lock_guard_type
Definition mutex.h:58
BigInt inverse_mod_rsa_public_modulus(const BigInt &x, const BigInt &n)
Definition mod_inv.cpp:302
size_t if_work_factor(size_t bits)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131
Montgomery_Int monty_exp(const Montgomery_Params &params_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
Definition monty_exp.h:70
Montgomery_Int monty_execute(const Montgomery_Exponentiation_State &precomputed_state, const BigInt &k, size_t max_k_bits)