xref: /dragonfly/crypto/libressl/crypto/rsa/rsa_lib.c (revision de0e0e4d)
1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: rsa_lib.c,v 1.43 2022/06/27 12:30:28 tb Exp $ */
2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3f5b1c8a1SJohn Marino  * All rights reserved.
4f5b1c8a1SJohn Marino  *
5f5b1c8a1SJohn Marino  * This package is an SSL implementation written
6f5b1c8a1SJohn Marino  * by Eric Young (eay@cryptsoft.com).
7f5b1c8a1SJohn Marino  * The implementation was written so as to conform with Netscapes SSL.
8f5b1c8a1SJohn Marino  *
9f5b1c8a1SJohn Marino  * This library is free for commercial and non-commercial use as long as
10f5b1c8a1SJohn Marino  * the following conditions are aheared to.  The following conditions
11f5b1c8a1SJohn Marino  * apply to all code found in this distribution, be it the RC4, RSA,
12f5b1c8a1SJohn Marino  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13f5b1c8a1SJohn Marino  * included with this distribution is covered by the same copyright terms
14f5b1c8a1SJohn Marino  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15f5b1c8a1SJohn Marino  *
16f5b1c8a1SJohn Marino  * Copyright remains Eric Young's, and as such any Copyright notices in
17f5b1c8a1SJohn Marino  * the code are not to be removed.
18f5b1c8a1SJohn Marino  * If this package is used in a product, Eric Young should be given attribution
19f5b1c8a1SJohn Marino  * as the author of the parts of the library used.
20f5b1c8a1SJohn Marino  * This can be in the form of a textual message at program startup or
21f5b1c8a1SJohn Marino  * in documentation (online or textual) provided with the package.
22f5b1c8a1SJohn Marino  *
23f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
24f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
25f5b1c8a1SJohn Marino  * are met:
26f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the copyright
27f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
28f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
29f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
30f5b1c8a1SJohn Marino  *    documentation and/or other materials provided with the distribution.
31f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this software
32f5b1c8a1SJohn Marino  *    must display the following acknowledgement:
33f5b1c8a1SJohn Marino  *    "This product includes cryptographic software written by
34f5b1c8a1SJohn Marino  *     Eric Young (eay@cryptsoft.com)"
35f5b1c8a1SJohn Marino  *    The word 'cryptographic' can be left out if the rouines from the library
36f5b1c8a1SJohn Marino  *    being used are not cryptographic related :-).
37f5b1c8a1SJohn Marino  * 4. If you include any Windows specific code (or a derivative thereof) from
38f5b1c8a1SJohn Marino  *    the apps directory (application code) you must include an acknowledgement:
39f5b1c8a1SJohn Marino  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40f5b1c8a1SJohn Marino  *
41f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42f5b1c8a1SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44f5b1c8a1SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45f5b1c8a1SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46f5b1c8a1SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47f5b1c8a1SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49f5b1c8a1SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f5b1c8a1SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51f5b1c8a1SJohn Marino  * SUCH DAMAGE.
52f5b1c8a1SJohn Marino  *
53f5b1c8a1SJohn Marino  * The licence and distribution terms for any publically available version or
54f5b1c8a1SJohn Marino  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55f5b1c8a1SJohn Marino  * copied and put under another distribution licence
56f5b1c8a1SJohn Marino  * [including the GNU Public Licence.]
57f5b1c8a1SJohn Marino  */
58f5b1c8a1SJohn Marino 
59f5b1c8a1SJohn Marino #include <stdio.h>
60f5b1c8a1SJohn Marino 
61f5b1c8a1SJohn Marino #include <openssl/opensslconf.h>
62f5b1c8a1SJohn Marino 
63f5b1c8a1SJohn Marino #include <openssl/bn.h>
64f5b1c8a1SJohn Marino #include <openssl/crypto.h>
65f5b1c8a1SJohn Marino #include <openssl/err.h>
66cca6fc52SDaniel Fojt #include <openssl/evp.h>
67f5b1c8a1SJohn Marino #include <openssl/lhash.h>
68f5b1c8a1SJohn Marino #include <openssl/rsa.h>
69f5b1c8a1SJohn Marino 
70cca6fc52SDaniel Fojt #include "evp_locl.h"
71*de0e0e4dSAntonio Huete Jimenez #include "rsa_locl.h"
72cca6fc52SDaniel Fojt 
73f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
74f5b1c8a1SJohn Marino #include <openssl/engine.h>
75f5b1c8a1SJohn Marino #endif
76f5b1c8a1SJohn Marino 
77f5b1c8a1SJohn Marino static const RSA_METHOD *default_RSA_meth = NULL;
78f5b1c8a1SJohn Marino 
79f5b1c8a1SJohn Marino RSA *
RSA_new(void)80f5b1c8a1SJohn Marino RSA_new(void)
81f5b1c8a1SJohn Marino {
82f5b1c8a1SJohn Marino 	RSA *r = RSA_new_method(NULL);
83f5b1c8a1SJohn Marino 
84f5b1c8a1SJohn Marino 	return r;
85f5b1c8a1SJohn Marino }
86f5b1c8a1SJohn Marino 
87f5b1c8a1SJohn Marino void
RSA_set_default_method(const RSA_METHOD * meth)88f5b1c8a1SJohn Marino RSA_set_default_method(const RSA_METHOD *meth)
89f5b1c8a1SJohn Marino {
90f5b1c8a1SJohn Marino 	default_RSA_meth = meth;
91f5b1c8a1SJohn Marino }
92f5b1c8a1SJohn Marino 
93f5b1c8a1SJohn Marino const RSA_METHOD *
RSA_get_default_method(void)94f5b1c8a1SJohn Marino RSA_get_default_method(void)
95f5b1c8a1SJohn Marino {
96f5b1c8a1SJohn Marino 	if (default_RSA_meth == NULL)
97f5b1c8a1SJohn Marino 		default_RSA_meth = RSA_PKCS1_SSLeay();
98f5b1c8a1SJohn Marino 
99f5b1c8a1SJohn Marino 	return default_RSA_meth;
100f5b1c8a1SJohn Marino }
101f5b1c8a1SJohn Marino 
102f5b1c8a1SJohn Marino const RSA_METHOD *
RSA_get_method(const RSA * rsa)103f5b1c8a1SJohn Marino RSA_get_method(const RSA *rsa)
104f5b1c8a1SJohn Marino {
105f5b1c8a1SJohn Marino 	return rsa->meth;
106f5b1c8a1SJohn Marino }
107f5b1c8a1SJohn Marino 
108f5b1c8a1SJohn Marino int
RSA_set_method(RSA * rsa,const RSA_METHOD * meth)109f5b1c8a1SJohn Marino RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
110f5b1c8a1SJohn Marino {
111f5b1c8a1SJohn Marino 	/*
112f5b1c8a1SJohn Marino 	 * NB: The caller is specifically setting a method, so it's not up to us
113f5b1c8a1SJohn Marino 	 * to deal with which ENGINE it comes from.
114f5b1c8a1SJohn Marino 	 */
115f5b1c8a1SJohn Marino 	const RSA_METHOD *mtmp;
116f5b1c8a1SJohn Marino 
117f5b1c8a1SJohn Marino 	mtmp = rsa->meth;
118f5b1c8a1SJohn Marino 	if (mtmp->finish)
119f5b1c8a1SJohn Marino 		mtmp->finish(rsa);
120f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
121f5b1c8a1SJohn Marino 	ENGINE_finish(rsa->engine);
122f5b1c8a1SJohn Marino 	rsa->engine = NULL;
123f5b1c8a1SJohn Marino #endif
124f5b1c8a1SJohn Marino 	rsa->meth = meth;
125f5b1c8a1SJohn Marino 	if (meth->init)
126f5b1c8a1SJohn Marino 		meth->init(rsa);
127f5b1c8a1SJohn Marino 	return 1;
128f5b1c8a1SJohn Marino }
129f5b1c8a1SJohn Marino 
130f5b1c8a1SJohn Marino RSA *
RSA_new_method(ENGINE * engine)131f5b1c8a1SJohn Marino RSA_new_method(ENGINE *engine)
132f5b1c8a1SJohn Marino {
133f5b1c8a1SJohn Marino 	RSA *ret;
134f5b1c8a1SJohn Marino 
135cca6fc52SDaniel Fojt 	if ((ret = calloc(1, sizeof(RSA))) == NULL) {
13672c33676SMaxim Ag 		RSAerror(ERR_R_MALLOC_FAILURE);
137f5b1c8a1SJohn Marino 		return NULL;
138f5b1c8a1SJohn Marino 	}
139f5b1c8a1SJohn Marino 
140f5b1c8a1SJohn Marino 	ret->meth = RSA_get_default_method();
141cca6fc52SDaniel Fojt 
142f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
143cca6fc52SDaniel Fojt 	if (engine != NULL) {
144f5b1c8a1SJohn Marino 		if (!ENGINE_init(engine)) {
14572c33676SMaxim Ag 			RSAerror(ERR_R_ENGINE_LIB);
146cca6fc52SDaniel Fojt 			goto err;
147f5b1c8a1SJohn Marino 		}
148f5b1c8a1SJohn Marino 		ret->engine = engine;
149cca6fc52SDaniel Fojt 	} else {
150f5b1c8a1SJohn Marino 		ret->engine = ENGINE_get_default_RSA();
151cca6fc52SDaniel Fojt 	}
152cca6fc52SDaniel Fojt 
153cca6fc52SDaniel Fojt 	if (ret->engine != NULL) {
154cca6fc52SDaniel Fojt 		if ((ret->meth = ENGINE_get_RSA(ret->engine)) == NULL) {
15572c33676SMaxim Ag 			RSAerror(ERR_R_ENGINE_LIB);
156cca6fc52SDaniel Fojt 			goto err;
157f5b1c8a1SJohn Marino 		}
158f5b1c8a1SJohn Marino 	}
159f5b1c8a1SJohn Marino #endif
160f5b1c8a1SJohn Marino 
161f5b1c8a1SJohn Marino 	ret->references = 1;
162f5b1c8a1SJohn Marino 	ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
163cca6fc52SDaniel Fojt 
164cca6fc52SDaniel Fojt 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
165cca6fc52SDaniel Fojt 		goto err;
166f5b1c8a1SJohn Marino 
167f5b1c8a1SJohn Marino 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
168cca6fc52SDaniel Fojt 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
169cca6fc52SDaniel Fojt 		goto err;
170cca6fc52SDaniel Fojt 	}
171cca6fc52SDaniel Fojt 
172cca6fc52SDaniel Fojt 	return ret;
173cca6fc52SDaniel Fojt 
174cca6fc52SDaniel Fojt  err:
175f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
176f5b1c8a1SJohn Marino 	ENGINE_finish(ret->engine);
177f5b1c8a1SJohn Marino #endif
178f5b1c8a1SJohn Marino 	free(ret);
179cca6fc52SDaniel Fojt 
180cca6fc52SDaniel Fojt 	return NULL;
181f5b1c8a1SJohn Marino }
182f5b1c8a1SJohn Marino 
183f5b1c8a1SJohn Marino void
RSA_free(RSA * r)184f5b1c8a1SJohn Marino RSA_free(RSA *r)
185f5b1c8a1SJohn Marino {
186f5b1c8a1SJohn Marino 	int i;
187f5b1c8a1SJohn Marino 
188f5b1c8a1SJohn Marino 	if (r == NULL)
189f5b1c8a1SJohn Marino 		return;
190f5b1c8a1SJohn Marino 
191f5b1c8a1SJohn Marino 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
192f5b1c8a1SJohn Marino 	if (i > 0)
193f5b1c8a1SJohn Marino 		return;
194f5b1c8a1SJohn Marino 
195f5b1c8a1SJohn Marino 	if (r->meth->finish)
196f5b1c8a1SJohn Marino 		r->meth->finish(r);
197f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
198f5b1c8a1SJohn Marino 	ENGINE_finish(r->engine);
199f5b1c8a1SJohn Marino #endif
200f5b1c8a1SJohn Marino 
201f5b1c8a1SJohn Marino 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
202f5b1c8a1SJohn Marino 
203f5b1c8a1SJohn Marino 	BN_clear_free(r->n);
204f5b1c8a1SJohn Marino 	BN_clear_free(r->e);
205f5b1c8a1SJohn Marino 	BN_clear_free(r->d);
206f5b1c8a1SJohn Marino 	BN_clear_free(r->p);
207f5b1c8a1SJohn Marino 	BN_clear_free(r->q);
208f5b1c8a1SJohn Marino 	BN_clear_free(r->dmp1);
209f5b1c8a1SJohn Marino 	BN_clear_free(r->dmq1);
210f5b1c8a1SJohn Marino 	BN_clear_free(r->iqmp);
211f5b1c8a1SJohn Marino 	BN_BLINDING_free(r->blinding);
212f5b1c8a1SJohn Marino 	BN_BLINDING_free(r->mt_blinding);
213cca6fc52SDaniel Fojt 	RSA_PSS_PARAMS_free(r->pss);
214f5b1c8a1SJohn Marino 	free(r);
215f5b1c8a1SJohn Marino }
216f5b1c8a1SJohn Marino 
217f5b1c8a1SJohn Marino int
RSA_up_ref(RSA * r)218f5b1c8a1SJohn Marino RSA_up_ref(RSA *r)
219f5b1c8a1SJohn Marino {
220f5b1c8a1SJohn Marino 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
221f5b1c8a1SJohn Marino 	return i > 1 ? 1 : 0;
222f5b1c8a1SJohn Marino }
223f5b1c8a1SJohn Marino 
224f5b1c8a1SJohn Marino int
RSA_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)225f5b1c8a1SJohn Marino RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
226f5b1c8a1SJohn Marino     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
227f5b1c8a1SJohn Marino {
228f5b1c8a1SJohn Marino 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
229f5b1c8a1SJohn Marino 	    new_func, dup_func, free_func);
230f5b1c8a1SJohn Marino }
231f5b1c8a1SJohn Marino 
232f5b1c8a1SJohn Marino int
RSA_set_ex_data(RSA * r,int idx,void * arg)233f5b1c8a1SJohn Marino RSA_set_ex_data(RSA *r, int idx, void *arg)
234f5b1c8a1SJohn Marino {
235f5b1c8a1SJohn Marino 	return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
236f5b1c8a1SJohn Marino }
237f5b1c8a1SJohn Marino 
238f5b1c8a1SJohn Marino void *
RSA_get_ex_data(const RSA * r,int idx)239f5b1c8a1SJohn Marino RSA_get_ex_data(const RSA *r, int idx)
240f5b1c8a1SJohn Marino {
241f5b1c8a1SJohn Marino 	return CRYPTO_get_ex_data(&r->ex_data, idx);
242f5b1c8a1SJohn Marino }
24372c33676SMaxim Ag 
244*de0e0e4dSAntonio Huete Jimenez int
RSA_security_bits(const RSA * rsa)245*de0e0e4dSAntonio Huete Jimenez RSA_security_bits(const RSA *rsa)
246*de0e0e4dSAntonio Huete Jimenez {
247*de0e0e4dSAntonio Huete Jimenez 	return BN_security_bits(RSA_bits(rsa), -1);
248*de0e0e4dSAntonio Huete Jimenez }
249*de0e0e4dSAntonio Huete Jimenez 
25072c33676SMaxim Ag void
RSA_get0_key(const RSA * r,const BIGNUM ** n,const BIGNUM ** e,const BIGNUM ** d)25172c33676SMaxim Ag RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
25272c33676SMaxim Ag {
25372c33676SMaxim Ag 	if (n != NULL)
25472c33676SMaxim Ag 		*n = r->n;
25572c33676SMaxim Ag 	if (e != NULL)
25672c33676SMaxim Ag 		*e = r->e;
25772c33676SMaxim Ag 	if (d != NULL)
25872c33676SMaxim Ag 		*d = r->d;
25972c33676SMaxim Ag }
26072c33676SMaxim Ag 
26172c33676SMaxim Ag int
RSA_set0_key(RSA * r,BIGNUM * n,BIGNUM * e,BIGNUM * d)26272c33676SMaxim Ag RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
26372c33676SMaxim Ag {
26472c33676SMaxim Ag 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
26572c33676SMaxim Ag 		return 0;
26672c33676SMaxim Ag 
26772c33676SMaxim Ag 	if (n != NULL) {
26872c33676SMaxim Ag 		BN_free(r->n);
26972c33676SMaxim Ag 		r->n = n;
27072c33676SMaxim Ag 	}
27172c33676SMaxim Ag 	if (e != NULL) {
27272c33676SMaxim Ag 		BN_free(r->e);
27372c33676SMaxim Ag 		r->e = e;
27472c33676SMaxim Ag 	}
27572c33676SMaxim Ag 	if (d != NULL) {
27672c33676SMaxim Ag 		BN_free(r->d);
27772c33676SMaxim Ag 		r->d = d;
27872c33676SMaxim Ag 	}
27972c33676SMaxim Ag 
28072c33676SMaxim Ag 	return 1;
28172c33676SMaxim Ag }
28272c33676SMaxim Ag 
28372c33676SMaxim Ag void
RSA_get0_crt_params(const RSA * r,const BIGNUM ** dmp1,const BIGNUM ** dmq1,const BIGNUM ** iqmp)28472c33676SMaxim Ag RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
28572c33676SMaxim Ag     const BIGNUM **iqmp)
28672c33676SMaxim Ag {
28772c33676SMaxim Ag 	if (dmp1 != NULL)
28872c33676SMaxim Ag 		*dmp1 = r->dmp1;
28972c33676SMaxim Ag 	if (dmq1 != NULL)
29072c33676SMaxim Ag 		*dmq1 = r->dmq1;
29172c33676SMaxim Ag 	if (iqmp != NULL)
29272c33676SMaxim Ag 		*iqmp = r->iqmp;
29372c33676SMaxim Ag }
29472c33676SMaxim Ag 
29572c33676SMaxim Ag int
RSA_set0_crt_params(RSA * r,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)29672c33676SMaxim Ag RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
29772c33676SMaxim Ag {
29872c33676SMaxim Ag 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
29972c33676SMaxim Ag 	    (r->dmq1 == NULL && dmq1 == NULL) ||
30072c33676SMaxim Ag 	    (r->iqmp == NULL && iqmp == NULL))
30172c33676SMaxim Ag 	       	return 0;
30272c33676SMaxim Ag 
30372c33676SMaxim Ag 	if (dmp1 != NULL) {
30472c33676SMaxim Ag 		BN_free(r->dmp1);
30572c33676SMaxim Ag 		r->dmp1 = dmp1;
30672c33676SMaxim Ag 	}
30772c33676SMaxim Ag 	if (dmq1 != NULL) {
30872c33676SMaxim Ag 		BN_free(r->dmq1);
30972c33676SMaxim Ag 		r->dmq1 = dmq1;
31072c33676SMaxim Ag 	}
31172c33676SMaxim Ag 	if (iqmp != NULL) {
31272c33676SMaxim Ag 		BN_free(r->iqmp);
31372c33676SMaxim Ag 		r->iqmp = iqmp;
31472c33676SMaxim Ag 	}
31572c33676SMaxim Ag 
31672c33676SMaxim Ag 	return 1;
31772c33676SMaxim Ag }
31872c33676SMaxim Ag 
31972c33676SMaxim Ag void
RSA_get0_factors(const RSA * r,const BIGNUM ** p,const BIGNUM ** q)32072c33676SMaxim Ag RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
32172c33676SMaxim Ag {
32272c33676SMaxim Ag 	if (p != NULL)
32372c33676SMaxim Ag 		*p = r->p;
32472c33676SMaxim Ag 	if (q != NULL)
32572c33676SMaxim Ag 		*q = r->q;
32672c33676SMaxim Ag }
32772c33676SMaxim Ag 
32872c33676SMaxim Ag int
RSA_set0_factors(RSA * r,BIGNUM * p,BIGNUM * q)32972c33676SMaxim Ag RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
33072c33676SMaxim Ag {
33172c33676SMaxim Ag 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
33272c33676SMaxim Ag 		return 0;
33372c33676SMaxim Ag 
33472c33676SMaxim Ag 	if (p != NULL) {
33572c33676SMaxim Ag 		BN_free(r->p);
33672c33676SMaxim Ag 		r->p = p;
33772c33676SMaxim Ag 	}
33872c33676SMaxim Ag 	if (q != NULL) {
33972c33676SMaxim Ag 		BN_free(r->q);
34072c33676SMaxim Ag 		r->q = q;
34172c33676SMaxim Ag 	}
34272c33676SMaxim Ag 
34372c33676SMaxim Ag 	return 1;
34472c33676SMaxim Ag }
34572c33676SMaxim Ag 
346*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_n(const RSA * r)347*de0e0e4dSAntonio Huete Jimenez RSA_get0_n(const RSA *r)
348*de0e0e4dSAntonio Huete Jimenez {
349*de0e0e4dSAntonio Huete Jimenez 	return r->n;
350*de0e0e4dSAntonio Huete Jimenez }
351*de0e0e4dSAntonio Huete Jimenez 
352*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_e(const RSA * r)353*de0e0e4dSAntonio Huete Jimenez RSA_get0_e(const RSA *r)
354*de0e0e4dSAntonio Huete Jimenez {
355*de0e0e4dSAntonio Huete Jimenez 	return r->e;
356*de0e0e4dSAntonio Huete Jimenez }
357*de0e0e4dSAntonio Huete Jimenez 
358*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_d(const RSA * r)359*de0e0e4dSAntonio Huete Jimenez RSA_get0_d(const RSA *r)
360*de0e0e4dSAntonio Huete Jimenez {
361*de0e0e4dSAntonio Huete Jimenez 	return r->d;
362*de0e0e4dSAntonio Huete Jimenez }
363*de0e0e4dSAntonio Huete Jimenez 
364*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_p(const RSA * r)365*de0e0e4dSAntonio Huete Jimenez RSA_get0_p(const RSA *r)
366*de0e0e4dSAntonio Huete Jimenez {
367*de0e0e4dSAntonio Huete Jimenez 	return r->p;
368*de0e0e4dSAntonio Huete Jimenez }
369*de0e0e4dSAntonio Huete Jimenez 
370*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_q(const RSA * r)371*de0e0e4dSAntonio Huete Jimenez RSA_get0_q(const RSA *r)
372*de0e0e4dSAntonio Huete Jimenez {
373*de0e0e4dSAntonio Huete Jimenez 	return r->q;
374*de0e0e4dSAntonio Huete Jimenez }
375*de0e0e4dSAntonio Huete Jimenez 
376*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_dmp1(const RSA * r)377*de0e0e4dSAntonio Huete Jimenez RSA_get0_dmp1(const RSA *r)
378*de0e0e4dSAntonio Huete Jimenez {
379*de0e0e4dSAntonio Huete Jimenez 	return r->dmp1;
380*de0e0e4dSAntonio Huete Jimenez }
381*de0e0e4dSAntonio Huete Jimenez 
382*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_dmq1(const RSA * r)383*de0e0e4dSAntonio Huete Jimenez RSA_get0_dmq1(const RSA *r)
384*de0e0e4dSAntonio Huete Jimenez {
385*de0e0e4dSAntonio Huete Jimenez 	return r->dmq1;
386*de0e0e4dSAntonio Huete Jimenez }
387*de0e0e4dSAntonio Huete Jimenez 
388*de0e0e4dSAntonio Huete Jimenez const BIGNUM *
RSA_get0_iqmp(const RSA * r)389*de0e0e4dSAntonio Huete Jimenez RSA_get0_iqmp(const RSA *r)
390*de0e0e4dSAntonio Huete Jimenez {
391*de0e0e4dSAntonio Huete Jimenez 	return r->iqmp;
392*de0e0e4dSAntonio Huete Jimenez }
393*de0e0e4dSAntonio Huete Jimenez 
394*de0e0e4dSAntonio Huete Jimenez const RSA_PSS_PARAMS *
RSA_get0_pss_params(const RSA * r)395*de0e0e4dSAntonio Huete Jimenez RSA_get0_pss_params(const RSA *r)
396*de0e0e4dSAntonio Huete Jimenez {
397*de0e0e4dSAntonio Huete Jimenez 	return r->pss;
398*de0e0e4dSAntonio Huete Jimenez }
399*de0e0e4dSAntonio Huete Jimenez 
40072c33676SMaxim Ag void
RSA_clear_flags(RSA * r,int flags)40172c33676SMaxim Ag RSA_clear_flags(RSA *r, int flags)
40272c33676SMaxim Ag {
40372c33676SMaxim Ag 	r->flags &= ~flags;
40472c33676SMaxim Ag }
40572c33676SMaxim Ag 
40672c33676SMaxim Ag int
RSA_test_flags(const RSA * r,int flags)40772c33676SMaxim Ag RSA_test_flags(const RSA *r, int flags)
40872c33676SMaxim Ag {
40972c33676SMaxim Ag 	return r->flags & flags;
41072c33676SMaxim Ag }
41172c33676SMaxim Ag 
41272c33676SMaxim Ag void
RSA_set_flags(RSA * r,int flags)41372c33676SMaxim Ag RSA_set_flags(RSA *r, int flags)
41472c33676SMaxim Ag {
41572c33676SMaxim Ag 	r->flags |= flags;
41672c33676SMaxim Ag }
417cca6fc52SDaniel Fojt 
418cca6fc52SDaniel Fojt int
RSA_pkey_ctx_ctrl(EVP_PKEY_CTX * ctx,int optype,int cmd,int p1,void * p2)419cca6fc52SDaniel Fojt RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
420cca6fc52SDaniel Fojt {
421cca6fc52SDaniel Fojt 	/* Return an error if the key type is not RSA or RSA-PSS. */
422cca6fc52SDaniel Fojt 	if (ctx != NULL && ctx->pmeth != NULL &&
423cca6fc52SDaniel Fojt 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA &&
424cca6fc52SDaniel Fojt 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
425cca6fc52SDaniel Fojt 		return -1;
426cca6fc52SDaniel Fojt 
427cca6fc52SDaniel Fojt 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
428cca6fc52SDaniel Fojt }
429