blob: 526e468cfec58e12846902e4ca8dbef86cf6f1bc [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/rsa.h>
58
59#include <openssl/bn.h>
60#include <openssl/engine.h>
61#include <openssl/err.h>
62#include <openssl/ex_data.h>
63#include <openssl/mem.h>
64#include <openssl/obj.h>
65
66#include "internal.h"
67
68
69extern const RSA_METHOD RSA_default_method;
70
71RSA *RSA_new(void) { return RSA_new_method(NULL); }
72
73RSA *RSA_new_method(const ENGINE *engine) {
74 RSA *rsa = (RSA *)OPENSSL_malloc(sizeof(RSA));
75 if (rsa == NULL) {
76 OPENSSL_PUT_ERROR(RSA, RSA_new_method, ERR_R_MALLOC_FAILURE);
77 return NULL;
78 }
79
80 memset(rsa, 0, sizeof(RSA));
81
82 if (engine) {
83 rsa->meth = ENGINE_get_RSA_method(engine);
84 }
85
86 if (rsa->meth == NULL) {
87 rsa->meth = (RSA_METHOD*) &RSA_default_method;
88 }
89 METHOD_ref(rsa->meth);
90
91 rsa->references = 1;
92 rsa->flags = rsa->meth->flags;
93
94 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, rsa, &rsa->ex_data)) {
95 METHOD_unref(rsa->meth);
96 OPENSSL_free(rsa);
97 return NULL;
98 }
99
100 if (rsa->meth->init && !rsa->meth->init(rsa)) {
101 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, rsa, &rsa->ex_data);
102 METHOD_unref(rsa->meth);
103 OPENSSL_free(rsa);
104 return NULL;
105 }
106
107 return rsa;
108}
109
110void RSA_free(RSA *rsa) {
111 unsigned u;
112
113 if (rsa == NULL) {
114 return;
115 }
116
117 if (CRYPTO_add(&rsa->references, -1, CRYPTO_LOCK_RSA) > 0) {
118 return;
119 }
120
121 if (rsa->meth->finish) {
122 rsa->meth->finish(rsa);
123 }
124 METHOD_unref(rsa->meth);
125
126 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, rsa, &rsa->ex_data);
127
128 if (rsa->n != NULL)
129 BN_clear_free(rsa->n);
130 if (rsa->e != NULL)
131 BN_clear_free(rsa->e);
132 if (rsa->d != NULL)
133 BN_clear_free(rsa->d);
134 if (rsa->p != NULL)
135 BN_clear_free(rsa->p);
136 if (rsa->q != NULL)
137 BN_clear_free(rsa->q);
138 if (rsa->dmp1 != NULL)
139 BN_clear_free(rsa->dmp1);
140 if (rsa->dmq1 != NULL)
141 BN_clear_free(rsa->dmq1);
142 if (rsa->iqmp != NULL)
143 BN_clear_free(rsa->iqmp);
144 for (u = 0; u < rsa->num_blindings; u++) {
145 BN_BLINDING_free(rsa->blindings[u]);
146 }
147 if (rsa->blindings != NULL)
148 OPENSSL_free(rsa->blindings);
149 if (rsa->blindings_inuse != NULL)
150 OPENSSL_free(rsa->blindings_inuse);
151 OPENSSL_free(rsa);
152}
153
154int RSA_up_ref(RSA *rsa) {
155 CRYPTO_add(&rsa->references, 1, CRYPTO_LOCK_RSA);
156 return 1;
157}
158
159int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
160 if (rsa->meth->keygen) {
161 return rsa->meth->keygen(rsa, bits, e_value, cb);
162 }
163
164 return RSA_default_method.keygen(rsa, bits, e_value, cb);
165}
166
167int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
168 const uint8_t *in, size_t in_len, int padding) {
169 if (rsa->meth->encrypt) {
170 return rsa->meth->encrypt(rsa, out_len, out, max_out, in, in_len, padding);
171 }
172
173 return RSA_default_method.encrypt(rsa, out_len, out, max_out, in, in_len,
174 padding);
175}
176
177int RSA_public_encrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
178 int padding) {
179 size_t out_len;
180
181 if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
182 return -1;
183 }
184
185 return out_len;
186}
187
188int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
189 const uint8_t *in, size_t in_len, int padding) {
190 if (rsa->meth->sign_raw) {
191 return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
192 }
193
194 return RSA_default_method.sign_raw(rsa, out_len, out, max_out, in, in_len,
195 padding);
196}
197
198int RSA_private_encrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
199 int padding) {
200 size_t out_len;
201
202 if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
203 return -1;
204 }
205
206 return out_len;
207}
208
209int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
210 const uint8_t *in, size_t in_len, int padding) {
211 if (rsa->meth->decrypt) {
212 return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
213 }
214
215 return RSA_default_method.decrypt(rsa, out_len, out, max_out, in, in_len,
216 padding);
217}
218
219int RSA_private_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
220 int padding) {
221 size_t out_len;
222
223 if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
224 return -1;
225 }
226
227 return out_len;
228}
229
230int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
231 const uint8_t *in, size_t in_len, int padding) {
232 if (rsa->meth->verify_raw) {
233 return rsa->meth->verify_raw(rsa, out_len, out, max_out, in, in_len, padding);
234 }
235
236 return RSA_default_method.verify_raw(rsa, out_len, out, max_out, in, in_len,
237 padding);
238}
239
240int RSA_public_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
241 int padding) {
242 size_t out_len;
243
244 if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
245 return -1;
246 }
247
248 return out_len;
249}
250
251unsigned RSA_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400252 if (rsa->meth->size) {
253 return rsa->meth->size(rsa);
254 }
255
256 return RSA_default_method.size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700257}
258
259int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
260 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
261 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, new_func,
262 dup_func, free_func);
263}
264
265int RSA_set_ex_data(RSA *d, int idx, void *arg) {
266 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
267}
268
269void *RSA_get_ex_data(const RSA *d, int idx) {
270 return CRYPTO_get_ex_data(&d->ex_data, idx);
271}
272
273/* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
274 * the length of an MD5 and SHA1 hash. */
275static const unsigned SSL_SIG_LENGTH = 36;
276
277/* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
278 * to be signed with PKCS#1. */
279struct pkcs1_sig_prefix {
280 /* nid identifies the hash function. */
281 int nid;
282 /* len is the number of bytes of |bytes| which are valid. */
283 uint8_t len;
284 /* bytes contains the DER bytes. */
285 uint8_t bytes[19];
286};
287
288/* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
289 * different hash functions. */
290static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
291 {
292 NID_md5,
293 18,
294 {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
295 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
296 },
297 {
298 NID_sha1,
299 15,
300 {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
301 0x00, 0x04, 0x14},
302 },
303 {
304 NID_sha224,
305 19,
306 {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
307 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
308 },
309 {
310 NID_sha256,
311 19,
312 {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
313 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
314 },
315 {
316 NID_sha384,
317 19,
318 {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
319 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
320 },
321 {
322 NID_sha512,
323 19,
324 {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
325 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
326 },
327 {
328 NID_ripemd160,
329 14,
330 {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31,
331 0x04, 0x14},
332 },
333 {
334 NID_undef, 0, {0},
335 },
336};
337
338/* TODO(fork): mostly new code, needs careful review. */
339
340/* pkcs1_prefixed_msg builds a PKCS#1, prefixed version of |msg| for the given
341 * hash function and sets |out_msg| to point to it. On successful return,
342 * |*out_msg| may be allocated memory and, if so, |*is_alloced| will be 1. */
343static int pkcs1_prefixed_msg(uint8_t **out_msg, size_t *out_msg_len,
344 int *is_alloced, int hash_nid, const uint8_t *msg,
345 size_t msg_len) {
346 unsigned i;
347 const uint8_t* prefix = NULL;
348 unsigned prefix_len;
349 uint8_t *signed_msg;
350 unsigned signed_msg_len;
351
352 if (hash_nid == NID_md5_sha1) {
353 /* Special case: SSL signature, just check the length. */
354 if (msg_len != SSL_SIG_LENGTH) {
355 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_INVALID_MESSAGE_LENGTH);
356 return 0;
357 }
358
359 *out_msg = (uint8_t*) msg;
360 *out_msg_len = SSL_SIG_LENGTH;
361 *is_alloced = 0;
362 return 1;
363 }
364
365 for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
366 const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
367 if (sig_prefix->nid == hash_nid) {
368 prefix = sig_prefix->bytes;
369 prefix_len = sig_prefix->len;
370 break;
371 }
372 }
373
374 if (prefix == NULL) {
375 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_UNKNOWN_ALGORITHM_TYPE);
376 return 0;
377 }
378
379 signed_msg_len = prefix_len + msg_len;
380 if (signed_msg_len < prefix_len) {
381 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_TOO_LONG);
382 return 0;
383 }
384
385 signed_msg = OPENSSL_malloc(signed_msg_len);
386 if (!signed_msg) {
387 OPENSSL_PUT_ERROR(RSA, RSA_sign, ERR_R_MALLOC_FAILURE);
388 return 0;
389 }
390
391 memcpy(signed_msg, prefix, prefix_len);
392 memcpy(signed_msg + prefix_len, msg, msg_len);
393
394 *out_msg = signed_msg;
395 *out_msg_len = signed_msg_len;
396 *is_alloced = 1;
397
398 return 1;
399}
400
401int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
402 unsigned *out_len, RSA *rsa) {
403 const unsigned rsa_size = RSA_size(rsa);
404 int ret = 0;
405 uint8_t *signed_msg;
406 size_t signed_msg_len;
407 int signed_msg_is_alloced = 0;
408 size_t size_t_out_len;
409
410 if (rsa->meth->sign) {
411 return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
412 }
413
414 if (!pkcs1_prefixed_msg(&signed_msg, &signed_msg_len, &signed_msg_is_alloced,
415 hash_nid, in, in_len)) {
416 return 0;
417 }
418
419 if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
420 signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
421 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
422 goto finish;
423 }
424
425 if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
426 signed_msg_len, RSA_PKCS1_PADDING)) {
427 *out_len = size_t_out_len;
428 ret = 1;
429 }
430
431finish:
432 if (signed_msg_is_alloced) {
433 OPENSSL_free(signed_msg);
434 }
435 return ret;
436}
437
438int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
439 const uint8_t *sig, size_t sig_len, RSA *rsa) {
440 const size_t rsa_size = RSA_size(rsa);
441 uint8_t *buf = NULL;
442 int ret = 0;
443 uint8_t *signed_msg = NULL;
444 size_t signed_msg_len, len;
445 int signed_msg_is_alloced = 0;
446
447 if (rsa->meth->verify) {
448 return rsa->meth->verify(hash_nid, msg, msg_len, sig, sig_len, rsa);
449 }
450
451 if (sig_len != rsa_size) {
452 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_WRONG_SIGNATURE_LENGTH);
453 return 0;
454 }
455
456 if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
457 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_INVALID_MESSAGE_LENGTH);
458 return 0;
459 }
460
461 buf = OPENSSL_malloc(rsa_size);
462 if (!buf) {
463 OPENSSL_PUT_ERROR(RSA, RSA_verify, ERR_R_MALLOC_FAILURE);
464 return 0;
465 }
466
467 if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
468 RSA_PKCS1_PADDING)) {
469 goto out;
470 }
471
472 if (!pkcs1_prefixed_msg(&signed_msg, &signed_msg_len, &signed_msg_is_alloced,
473 hash_nid, msg, msg_len)) {
474 goto out;
475 }
476
477 if (len != signed_msg_len || CRYPTO_memcmp(buf, signed_msg, len) != 0) {
478 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_BAD_SIGNATURE);
479 goto out;
480 }
481
482 ret = 1;
483
484out:
485 if (buf != NULL) {
486 OPENSSL_free(buf);
487 }
488 if (signed_msg_is_alloced) {
489 OPENSSL_free(signed_msg);
490 }
491 return ret;
492}
Adam Langley409766d2014-06-20 12:00:00 -0700493
494static void bn_free_and_null(BIGNUM **bn) {
495 if (*bn == NULL) {
496 return;
497 }
498
499 BN_free(*bn);
500 *bn = NULL;
501}
502
503int RSA_recover_crt_params(RSA *rsa) {
504 BN_CTX *ctx;
505 BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
506 int ok = 0;
507
508 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
509 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_EMPTY_PUBLIC_KEY);
510 return 0;
511 }
512
513 if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
514 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params,
515 RSA_R_CRT_PARAMS_ALREADY_GIVEN);
516 return 0;
517 }
518
519 /* This uses the algorithm from section 9B of the RSA paper:
520 * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
521
522 ctx = BN_CTX_new();
523 if (ctx == NULL) {
524 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
525 return 0;
526 }
527
528 BN_CTX_start(ctx);
529 totient = BN_CTX_get(ctx);
530 rem = BN_CTX_get(ctx);
531 multiple = BN_CTX_get(ctx);
532 p_plus_q = BN_CTX_get(ctx);
533 p_minus_q = BN_CTX_get(ctx);
534
535 if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
536 p_minus_q == NULL) {
537 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
538 goto err;
539 }
540
541 /* ed-1 is a small multiple of φ(n). */
542 if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
543 !BN_sub_word(totient, 1) ||
544 /* φ(n) =
545 * pq - p - q + 1 =
546 * n - (p + q) + 1
547 *
548 * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
549 * close. But, when we calculate the quotient, we'll be truncating it
550 * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
551 * which the totient cannot be. So we add one to the estimate.
552 *
553 * Consider ed-1 as:
554 *
555 * multiple * (n - (p+q) + 1) =
556 * multiple*n - multiple*(p+q) + multiple
557 *
558 * When we divide by n, the first term becomes multiple and, since
559 * multiple and p+q is tiny compared to n, the second and third terms can
560 * be ignored. Thus I claim that subtracting one from the estimate is
561 * sufficient. */
562 !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
563 !BN_add_word(multiple, 1) ||
564 !BN_div(totient, rem, totient, multiple, ctx)) {
565 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
566 goto err;
567 }
568
569 if (!BN_is_zero(rem)) {
570 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_BAD_RSA_PARAMETERS);
571 goto err;
572 }
573
574 rsa->p = BN_new();
575 rsa->q = BN_new();
576 rsa->dmp1 = BN_new();
577 rsa->dmq1 = BN_new();
578 rsa->iqmp = BN_new();
579 if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
580 NULL || rsa->iqmp == NULL) {
581 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
582 goto err;
583 }
584
585 /* φ(n) = n - (p + q) + 1 =>
586 * n - totient + 1 = p + q */
587 if (!BN_sub(p_plus_q, rsa->n, totient) ||
588 !BN_add_word(p_plus_q, 1) ||
589 /* p - q = sqrt((p+q)^2 - 4n) */
590 !BN_sqr(rem, p_plus_q, ctx) ||
591 !BN_lshift(multiple, rsa->n, 2) ||
592 !BN_sub(rem, rem, multiple) ||
593 !BN_sqrt(p_minus_q, rem, ctx) ||
594 /* q is 1/2 (p+q)-(p-q) */
595 !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
596 !BN_rshift1(rsa->q, rsa->q) ||
597 !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
598 !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
599 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
600 goto err;
601 }
602
603 if (BN_cmp(multiple, rsa->n) != 0) {
604 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_INTERNAL_ERROR);
605 goto err;
606 }
607
608 if (!BN_sub(rem, rsa->p, BN_value_one()) ||
609 !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
610 !BN_sub(rem, rsa->q, BN_value_one()) ||
611 !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
612 !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
613 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
614 goto err;
615 }
616
617 ok = 1;
618
619err:
620 BN_CTX_end(ctx);
621 BN_CTX_free(ctx);
622 if (!ok) {
623 bn_free_and_null(&rsa->p);
624 bn_free_and_null(&rsa->q);
625 bn_free_and_null(&rsa->dmp1);
626 bn_free_and_null(&rsa->dmq1);
627 bn_free_and_null(&rsa->iqmp);
628 }
629 return ok;
630}