xref: /openbsd/lib/libcrypto/rsa/rsa_lib.c (revision d230cbd2)
1 /* $OpenBSD: rsa_lib.c,v 1.50 2024/03/27 01:22:30 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/opensslconf.h>
62 
63 #include <openssl/bn.h>
64 #include <openssl/crypto.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/lhash.h>
68 #include <openssl/rsa.h>
69 
70 #include "bn_local.h"
71 #include "evp_local.h"
72 #include "rsa_local.h"
73 
74 static const RSA_METHOD *default_RSA_meth = NULL;
75 
76 RSA *
RSA_new(void)77 RSA_new(void)
78 {
79 	RSA *r = RSA_new_method(NULL);
80 
81 	return r;
82 }
83 LCRYPTO_ALIAS(RSA_new);
84 
85 void
RSA_set_default_method(const RSA_METHOD * meth)86 RSA_set_default_method(const RSA_METHOD *meth)
87 {
88 	default_RSA_meth = meth;
89 }
90 LCRYPTO_ALIAS(RSA_set_default_method);
91 
92 const RSA_METHOD *
RSA_get_default_method(void)93 RSA_get_default_method(void)
94 {
95 	if (default_RSA_meth == NULL)
96 		default_RSA_meth = RSA_PKCS1_SSLeay();
97 
98 	return default_RSA_meth;
99 }
100 LCRYPTO_ALIAS(RSA_get_default_method);
101 
102 const RSA_METHOD *
RSA_get_method(const RSA * rsa)103 RSA_get_method(const RSA *rsa)
104 {
105 	return rsa->meth;
106 }
107 LCRYPTO_ALIAS(RSA_get_method);
108 
109 int
RSA_set_method(RSA * rsa,const RSA_METHOD * meth)110 RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
111 {
112 	/*
113 	 * NB: The caller is specifically setting a method, so it's not up to us
114 	 * to deal with which ENGINE it comes from.
115 	 */
116 	const RSA_METHOD *mtmp;
117 
118 	mtmp = rsa->meth;
119 	if (mtmp->finish)
120 		mtmp->finish(rsa);
121 	rsa->meth = meth;
122 	if (meth->init)
123 		meth->init(rsa);
124 	return 1;
125 }
126 LCRYPTO_ALIAS(RSA_set_method);
127 
128 RSA *
RSA_new_method(ENGINE * engine)129 RSA_new_method(ENGINE *engine)
130 {
131 	RSA *ret;
132 
133 	if ((ret = calloc(1, sizeof(RSA))) == NULL) {
134 		RSAerror(ERR_R_MALLOC_FAILURE);
135 		return NULL;
136 	}
137 
138 	ret->meth = RSA_get_default_method();
139 
140 	ret->references = 1;
141 	ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
142 
143 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
144 		goto err;
145 
146 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
147 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
148 		goto err;
149 	}
150 
151 	return ret;
152 
153  err:
154 	free(ret);
155 
156 	return NULL;
157 }
158 LCRYPTO_ALIAS(RSA_new_method);
159 
160 void
RSA_free(RSA * r)161 RSA_free(RSA *r)
162 {
163 	int i;
164 
165 	if (r == NULL)
166 		return;
167 
168 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
169 	if (i > 0)
170 		return;
171 
172 	if (r->meth->finish)
173 		r->meth->finish(r);
174 
175 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
176 
177 	BN_free(r->n);
178 	BN_free(r->e);
179 	BN_free(r->d);
180 	BN_free(r->p);
181 	BN_free(r->q);
182 	BN_free(r->dmp1);
183 	BN_free(r->dmq1);
184 	BN_free(r->iqmp);
185 	BN_BLINDING_free(r->blinding);
186 	BN_BLINDING_free(r->mt_blinding);
187 	RSA_PSS_PARAMS_free(r->pss);
188 	free(r);
189 }
190 LCRYPTO_ALIAS(RSA_free);
191 
192 int
RSA_up_ref(RSA * r)193 RSA_up_ref(RSA *r)
194 {
195 	return CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA) > 1;
196 }
197 LCRYPTO_ALIAS(RSA_up_ref);
198 
199 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)200 RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
201     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
202 {
203 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
204 	    new_func, dup_func, free_func);
205 }
206 LCRYPTO_ALIAS(RSA_get_ex_new_index);
207 
208 int
RSA_set_ex_data(RSA * r,int idx,void * arg)209 RSA_set_ex_data(RSA *r, int idx, void *arg)
210 {
211 	return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
212 }
213 LCRYPTO_ALIAS(RSA_set_ex_data);
214 
215 void *
RSA_get_ex_data(const RSA * r,int idx)216 RSA_get_ex_data(const RSA *r, int idx)
217 {
218 	return CRYPTO_get_ex_data(&r->ex_data, idx);
219 }
220 LCRYPTO_ALIAS(RSA_get_ex_data);
221 
222 int
RSA_security_bits(const RSA * rsa)223 RSA_security_bits(const RSA *rsa)
224 {
225 	return BN_security_bits(RSA_bits(rsa), -1);
226 }
227 LCRYPTO_ALIAS(RSA_security_bits);
228 
229 void
RSA_get0_key(const RSA * r,const BIGNUM ** n,const BIGNUM ** e,const BIGNUM ** d)230 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
231 {
232 	if (n != NULL)
233 		*n = r->n;
234 	if (e != NULL)
235 		*e = r->e;
236 	if (d != NULL)
237 		*d = r->d;
238 }
239 LCRYPTO_ALIAS(RSA_get0_key);
240 
241 int
RSA_set0_key(RSA * r,BIGNUM * n,BIGNUM * e,BIGNUM * d)242 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
243 {
244 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
245 		return 0;
246 
247 	if (n != NULL) {
248 		BN_free(r->n);
249 		r->n = n;
250 	}
251 	if (e != NULL) {
252 		BN_free(r->e);
253 		r->e = e;
254 	}
255 	if (d != NULL) {
256 		BN_free(r->d);
257 		r->d = d;
258 	}
259 
260 	return 1;
261 }
262 LCRYPTO_ALIAS(RSA_set0_key);
263 
264 void
RSA_get0_crt_params(const RSA * r,const BIGNUM ** dmp1,const BIGNUM ** dmq1,const BIGNUM ** iqmp)265 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
266     const BIGNUM **iqmp)
267 {
268 	if (dmp1 != NULL)
269 		*dmp1 = r->dmp1;
270 	if (dmq1 != NULL)
271 		*dmq1 = r->dmq1;
272 	if (iqmp != NULL)
273 		*iqmp = r->iqmp;
274 }
275 LCRYPTO_ALIAS(RSA_get0_crt_params);
276 
277 int
RSA_set0_crt_params(RSA * r,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)278 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
279 {
280 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
281 	    (r->dmq1 == NULL && dmq1 == NULL) ||
282 	    (r->iqmp == NULL && iqmp == NULL))
283 		return 0;
284 
285 	if (dmp1 != NULL) {
286 		BN_free(r->dmp1);
287 		r->dmp1 = dmp1;
288 	}
289 	if (dmq1 != NULL) {
290 		BN_free(r->dmq1);
291 		r->dmq1 = dmq1;
292 	}
293 	if (iqmp != NULL) {
294 		BN_free(r->iqmp);
295 		r->iqmp = iqmp;
296 	}
297 
298 	return 1;
299 }
300 LCRYPTO_ALIAS(RSA_set0_crt_params);
301 
302 void
RSA_get0_factors(const RSA * r,const BIGNUM ** p,const BIGNUM ** q)303 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
304 {
305 	if (p != NULL)
306 		*p = r->p;
307 	if (q != NULL)
308 		*q = r->q;
309 }
310 LCRYPTO_ALIAS(RSA_get0_factors);
311 
312 int
RSA_set0_factors(RSA * r,BIGNUM * p,BIGNUM * q)313 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
314 {
315 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
316 		return 0;
317 
318 	if (p != NULL) {
319 		BN_free(r->p);
320 		r->p = p;
321 	}
322 	if (q != NULL) {
323 		BN_free(r->q);
324 		r->q = q;
325 	}
326 
327 	return 1;
328 }
329 LCRYPTO_ALIAS(RSA_set0_factors);
330 
331 const BIGNUM *
RSA_get0_n(const RSA * r)332 RSA_get0_n(const RSA *r)
333 {
334 	return r->n;
335 }
336 LCRYPTO_ALIAS(RSA_get0_n);
337 
338 const BIGNUM *
RSA_get0_e(const RSA * r)339 RSA_get0_e(const RSA *r)
340 {
341 	return r->e;
342 }
343 LCRYPTO_ALIAS(RSA_get0_e);
344 
345 const BIGNUM *
RSA_get0_d(const RSA * r)346 RSA_get0_d(const RSA *r)
347 {
348 	return r->d;
349 }
350 LCRYPTO_ALIAS(RSA_get0_d);
351 
352 const BIGNUM *
RSA_get0_p(const RSA * r)353 RSA_get0_p(const RSA *r)
354 {
355 	return r->p;
356 }
357 LCRYPTO_ALIAS(RSA_get0_p);
358 
359 const BIGNUM *
RSA_get0_q(const RSA * r)360 RSA_get0_q(const RSA *r)
361 {
362 	return r->q;
363 }
364 LCRYPTO_ALIAS(RSA_get0_q);
365 
366 const BIGNUM *
RSA_get0_dmp1(const RSA * r)367 RSA_get0_dmp1(const RSA *r)
368 {
369 	return r->dmp1;
370 }
371 LCRYPTO_ALIAS(RSA_get0_dmp1);
372 
373 const BIGNUM *
RSA_get0_dmq1(const RSA * r)374 RSA_get0_dmq1(const RSA *r)
375 {
376 	return r->dmq1;
377 }
378 LCRYPTO_ALIAS(RSA_get0_dmq1);
379 
380 const BIGNUM *
RSA_get0_iqmp(const RSA * r)381 RSA_get0_iqmp(const RSA *r)
382 {
383 	return r->iqmp;
384 }
385 LCRYPTO_ALIAS(RSA_get0_iqmp);
386 
387 const RSA_PSS_PARAMS *
RSA_get0_pss_params(const RSA * r)388 RSA_get0_pss_params(const RSA *r)
389 {
390 	return r->pss;
391 }
392 LCRYPTO_ALIAS(RSA_get0_pss_params);
393 
394 void
RSA_clear_flags(RSA * r,int flags)395 RSA_clear_flags(RSA *r, int flags)
396 {
397 	r->flags &= ~flags;
398 }
399 LCRYPTO_ALIAS(RSA_clear_flags);
400 
401 int
RSA_test_flags(const RSA * r,int flags)402 RSA_test_flags(const RSA *r, int flags)
403 {
404 	return r->flags & flags;
405 }
406 LCRYPTO_ALIAS(RSA_test_flags);
407 
408 void
RSA_set_flags(RSA * r,int flags)409 RSA_set_flags(RSA *r, int flags)
410 {
411 	r->flags |= flags;
412 }
413 LCRYPTO_ALIAS(RSA_set_flags);
414 
415 int
RSA_pkey_ctx_ctrl(EVP_PKEY_CTX * ctx,int optype,int cmd,int p1,void * p2)416 RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
417 {
418 	/* Return an error if the key type is not RSA or RSA-PSS. */
419 	if (ctx != NULL && ctx->pmeth != NULL &&
420 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA &&
421 	    ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
422 		return -1;
423 
424 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
425 }
426 LCRYPTO_ALIAS(RSA_pkey_ctx_ctrl);
427