10Sstevel@tonic-gate /* crypto/rsa/rsa_lib.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <openssl/crypto.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/lhash.h>
630Sstevel@tonic-gate #include <openssl/bn.h>
640Sstevel@tonic-gate #include <openssl/rsa.h>
650Sstevel@tonic-gate #include <openssl/rand.h>
660Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
670Sstevel@tonic-gate #include <openssl/engine.h>
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate
700Sstevel@tonic-gate const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
710Sstevel@tonic-gate
720Sstevel@tonic-gate static const RSA_METHOD *default_RSA_meth=NULL;
730Sstevel@tonic-gate
RSA_new(void)740Sstevel@tonic-gate RSA *RSA_new(void)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate RSA *r=RSA_new_method(NULL);
770Sstevel@tonic-gate
780Sstevel@tonic-gate return r;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
RSA_set_default_method(const RSA_METHOD * meth)810Sstevel@tonic-gate void RSA_set_default_method(const RSA_METHOD *meth)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate default_RSA_meth = meth;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
RSA_get_default_method(void)860Sstevel@tonic-gate const RSA_METHOD *RSA_get_default_method(void)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate if (default_RSA_meth == NULL)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate #ifdef RSA_NULL
910Sstevel@tonic-gate default_RSA_meth=RSA_null_method();
920Sstevel@tonic-gate #else
930Sstevel@tonic-gate #if 0 /* was: #ifdef RSAref */
940Sstevel@tonic-gate default_RSA_meth=RSA_PKCS1_RSAref();
950Sstevel@tonic-gate #else
960Sstevel@tonic-gate default_RSA_meth=RSA_PKCS1_SSLeay();
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate #endif
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate return default_RSA_meth;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
RSA_get_method(const RSA * rsa)1040Sstevel@tonic-gate const RSA_METHOD *RSA_get_method(const RSA *rsa)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate return rsa->meth;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
RSA_set_method(RSA * rsa,const RSA_METHOD * meth)1090Sstevel@tonic-gate int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate /* NB: The caller is specifically setting a method, so it's not up to us
1120Sstevel@tonic-gate * to deal with which ENGINE it comes from. */
1130Sstevel@tonic-gate const RSA_METHOD *mtmp;
1140Sstevel@tonic-gate mtmp = rsa->meth;
1150Sstevel@tonic-gate if (mtmp->finish) mtmp->finish(rsa);
1160Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1170Sstevel@tonic-gate if (rsa->engine)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate ENGINE_finish(rsa->engine);
1200Sstevel@tonic-gate rsa->engine = NULL;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate rsa->meth = meth;
1240Sstevel@tonic-gate if (meth->init) meth->init(rsa);
1250Sstevel@tonic-gate return 1;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
RSA_new_method(ENGINE * engine)1280Sstevel@tonic-gate RSA *RSA_new_method(ENGINE *engine)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate RSA *ret;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
1330Sstevel@tonic-gate if (ret == NULL)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
1360Sstevel@tonic-gate return NULL;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate ret->meth = RSA_get_default_method();
1400Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1410Sstevel@tonic-gate if (engine)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate if (!ENGINE_init(engine))
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
1460Sstevel@tonic-gate OPENSSL_free(ret);
1470Sstevel@tonic-gate return NULL;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate ret->engine = engine;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate else
1520Sstevel@tonic-gate ret->engine = ENGINE_get_default_RSA();
1530Sstevel@tonic-gate if(ret->engine)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate ret->meth = ENGINE_get_RSA(ret->engine);
1560Sstevel@tonic-gate if(!ret->meth)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate RSAerr(RSA_F_RSA_NEW_METHOD,
1590Sstevel@tonic-gate ERR_R_ENGINE_LIB);
1600Sstevel@tonic-gate ENGINE_finish(ret->engine);
1610Sstevel@tonic-gate OPENSSL_free(ret);
1620Sstevel@tonic-gate return NULL;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate ret->pad=0;
1680Sstevel@tonic-gate ret->version=0;
1690Sstevel@tonic-gate ret->n=NULL;
1700Sstevel@tonic-gate ret->e=NULL;
1710Sstevel@tonic-gate ret->d=NULL;
1720Sstevel@tonic-gate ret->p=NULL;
1730Sstevel@tonic-gate ret->q=NULL;
1740Sstevel@tonic-gate ret->dmp1=NULL;
1750Sstevel@tonic-gate ret->dmq1=NULL;
1760Sstevel@tonic-gate ret->iqmp=NULL;
1770Sstevel@tonic-gate ret->references=1;
1780Sstevel@tonic-gate ret->_method_mod_n=NULL;
1790Sstevel@tonic-gate ret->_method_mod_p=NULL;
1800Sstevel@tonic-gate ret->_method_mod_q=NULL;
1810Sstevel@tonic-gate ret->blinding=NULL;
182*2139Sjp161948 ret->mt_blinding=NULL;
1830Sstevel@tonic-gate ret->bignum_data=NULL;
1840Sstevel@tonic-gate ret->flags=ret->meth->flags;
1850Sstevel@tonic-gate CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
1860Sstevel@tonic-gate if ((ret->meth->init != NULL) && !ret->meth->init(ret))
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1890Sstevel@tonic-gate if (ret->engine)
1900Sstevel@tonic-gate ENGINE_finish(ret->engine);
1910Sstevel@tonic-gate #endif
1920Sstevel@tonic-gate CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
1930Sstevel@tonic-gate OPENSSL_free(ret);
1940Sstevel@tonic-gate ret=NULL;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate return(ret);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
RSA_free(RSA * r)1990Sstevel@tonic-gate void RSA_free(RSA *r)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate int i;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (r == NULL) return;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
2060Sstevel@tonic-gate #ifdef REF_PRINT
2070Sstevel@tonic-gate REF_PRINT("RSA",r);
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate if (i > 0) return;
2100Sstevel@tonic-gate #ifdef REF_CHECK
2110Sstevel@tonic-gate if (i < 0)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate fprintf(stderr,"RSA_free, bad reference count\n");
2140Sstevel@tonic-gate abort();
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (r->meth->finish)
2190Sstevel@tonic-gate r->meth->finish(r);
2200Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2210Sstevel@tonic-gate if (r->engine)
2220Sstevel@tonic-gate ENGINE_finish(r->engine);
2230Sstevel@tonic-gate #endif
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (r->n != NULL) BN_clear_free(r->n);
2280Sstevel@tonic-gate if (r->e != NULL) BN_clear_free(r->e);
2290Sstevel@tonic-gate if (r->d != NULL) BN_clear_free(r->d);
2300Sstevel@tonic-gate if (r->p != NULL) BN_clear_free(r->p);
2310Sstevel@tonic-gate if (r->q != NULL) BN_clear_free(r->q);
2320Sstevel@tonic-gate if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
2330Sstevel@tonic-gate if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
2340Sstevel@tonic-gate if (r->iqmp != NULL) BN_clear_free(r->iqmp);
2350Sstevel@tonic-gate if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
236*2139Sjp161948 if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding);
2370Sstevel@tonic-gate if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
2380Sstevel@tonic-gate OPENSSL_free(r);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
RSA_up_ref(RSA * r)2410Sstevel@tonic-gate int RSA_up_ref(RSA *r)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
2440Sstevel@tonic-gate #ifdef REF_PRINT
2450Sstevel@tonic-gate REF_PRINT("RSA",r);
2460Sstevel@tonic-gate #endif
2470Sstevel@tonic-gate #ifdef REF_CHECK
2480Sstevel@tonic-gate if (i < 2)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate fprintf(stderr, "RSA_up_ref, bad reference count\n");
2510Sstevel@tonic-gate abort();
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate #endif
2540Sstevel@tonic-gate return ((i > 1) ? 1 : 0);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
RSA_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)2570Sstevel@tonic-gate int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2580Sstevel@tonic-gate CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
2610Sstevel@tonic-gate new_func, dup_func, free_func);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
RSA_set_ex_data(RSA * r,int idx,void * arg)2640Sstevel@tonic-gate int RSA_set_ex_data(RSA *r, int idx, void *arg)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
RSA_get_ex_data(const RSA * r,int idx)2690Sstevel@tonic-gate void *RSA_get_ex_data(const RSA *r, int idx)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate return(CRYPTO_get_ex_data(&r->ex_data,idx));
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
RSA_size(const RSA * r)2740Sstevel@tonic-gate int RSA_size(const RSA *r)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate return(BN_num_bytes(r->n));
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
RSA_public_encrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)2790Sstevel@tonic-gate int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
2800Sstevel@tonic-gate RSA *rsa, int padding)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate
RSA_private_encrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)2850Sstevel@tonic-gate int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
2860Sstevel@tonic-gate RSA *rsa, int padding)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
RSA_private_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)2910Sstevel@tonic-gate int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
2920Sstevel@tonic-gate RSA *rsa, int padding)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
RSA_public_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)2970Sstevel@tonic-gate int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
2980Sstevel@tonic-gate RSA *rsa, int padding)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
RSA_flags(const RSA * r)3030Sstevel@tonic-gate int RSA_flags(const RSA *r)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate return((r == NULL)?0:r->meth->flags);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
RSA_blinding_off(RSA * rsa)3080Sstevel@tonic-gate void RSA_blinding_off(RSA *rsa)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate if (rsa->blinding != NULL)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate BN_BLINDING_free(rsa->blinding);
3130Sstevel@tonic-gate rsa->blinding=NULL;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate rsa->flags &= ~RSA_FLAG_BLINDING;
3160Sstevel@tonic-gate rsa->flags |= RSA_FLAG_NO_BLINDING;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate
RSA_blinding_on(RSA * rsa,BN_CTX * ctx)319*2139Sjp161948 int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate int ret=0;
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate if (rsa->blinding != NULL)
324*2139Sjp161948 RSA_blinding_off(rsa);
3250Sstevel@tonic-gate
326*2139Sjp161948 rsa->blinding = RSA_setup_blinding(rsa, ctx);
327*2139Sjp161948 if (rsa->blinding == NULL)
328*2139Sjp161948 goto err;
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate rsa->flags |= RSA_FLAG_BLINDING;
3310Sstevel@tonic-gate rsa->flags &= ~RSA_FLAG_NO_BLINDING;
3320Sstevel@tonic-gate ret=1;
3330Sstevel@tonic-gate err:
3340Sstevel@tonic-gate return(ret);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate
rsa_get_public_exp(const BIGNUM * d,const BIGNUM * p,const BIGNUM * q,BN_CTX * ctx)337*2139Sjp161948 static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
338*2139Sjp161948 const BIGNUM *q, BN_CTX *ctx)
339*2139Sjp161948 {
340*2139Sjp161948 BIGNUM *ret = NULL, *r0, *r1, *r2;
341*2139Sjp161948
342*2139Sjp161948 if (d == NULL || p == NULL || q == NULL)
343*2139Sjp161948 return NULL;
344*2139Sjp161948
345*2139Sjp161948 BN_CTX_start(ctx);
346*2139Sjp161948 r0 = BN_CTX_get(ctx);
347*2139Sjp161948 r1 = BN_CTX_get(ctx);
348*2139Sjp161948 r2 = BN_CTX_get(ctx);
349*2139Sjp161948 if (r2 == NULL)
350*2139Sjp161948 goto err;
351*2139Sjp161948
352*2139Sjp161948 if (!BN_sub(r1, p, BN_value_one())) goto err;
353*2139Sjp161948 if (!BN_sub(r2, q, BN_value_one())) goto err;
354*2139Sjp161948 if (!BN_mul(r0, r1, r2, ctx)) goto err;
355*2139Sjp161948
356*2139Sjp161948 ret = BN_mod_inverse(NULL, d, r0, ctx);
357*2139Sjp161948 err:
358*2139Sjp161948 BN_CTX_end(ctx);
359*2139Sjp161948 return ret;
360*2139Sjp161948 }
361*2139Sjp161948
RSA_setup_blinding(RSA * rsa,BN_CTX * in_ctx)362*2139Sjp161948 BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
363*2139Sjp161948 {
364*2139Sjp161948 BIGNUM *e;
365*2139Sjp161948 BN_CTX *ctx;
366*2139Sjp161948 BN_BLINDING *ret = NULL;
367*2139Sjp161948
368*2139Sjp161948 if (in_ctx == NULL)
369*2139Sjp161948 {
370*2139Sjp161948 if ((ctx = BN_CTX_new()) == NULL) return 0;
371*2139Sjp161948 }
372*2139Sjp161948 else
373*2139Sjp161948 ctx = in_ctx;
374*2139Sjp161948
375*2139Sjp161948 BN_CTX_start(ctx);
376*2139Sjp161948 e = BN_CTX_get(ctx);
377*2139Sjp161948 if (e == NULL)
378*2139Sjp161948 {
379*2139Sjp161948 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);
380*2139Sjp161948 goto err;
381*2139Sjp161948 }
382*2139Sjp161948
383*2139Sjp161948 if (rsa->e == NULL)
384*2139Sjp161948 {
385*2139Sjp161948 e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
386*2139Sjp161948 if (e == NULL)
387*2139Sjp161948 {
388*2139Sjp161948 RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT);
389*2139Sjp161948 goto err;
390*2139Sjp161948 }
391*2139Sjp161948 }
392*2139Sjp161948 else
393*2139Sjp161948 e = rsa->e;
394*2139Sjp161948
395*2139Sjp161948
396*2139Sjp161948 if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
397*2139Sjp161948 {
398*2139Sjp161948 /* if PRNG is not properly seeded, resort to secret
399*2139Sjp161948 * exponent as unpredictable seed */
400*2139Sjp161948 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0);
401*2139Sjp161948 }
402*2139Sjp161948
403*2139Sjp161948 ret = BN_BLINDING_create_param(NULL, e, rsa->n, ctx,
404*2139Sjp161948 rsa->meth->bn_mod_exp, rsa->_method_mod_n);
405*2139Sjp161948 if (ret == NULL)
406*2139Sjp161948 {
407*2139Sjp161948 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB);
408*2139Sjp161948 goto err;
409*2139Sjp161948 }
410*2139Sjp161948 BN_BLINDING_set_thread_id(ret, CRYPTO_thread_id());
411*2139Sjp161948 err:
412*2139Sjp161948 BN_CTX_end(ctx);
413*2139Sjp161948 if (in_ctx == NULL)
414*2139Sjp161948 BN_CTX_free(ctx);
415*2139Sjp161948
416*2139Sjp161948 return ret;
417*2139Sjp161948 }
418*2139Sjp161948
RSA_memory_lock(RSA * r)4190Sstevel@tonic-gate int RSA_memory_lock(RSA *r)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate int i,j,k,off;
4220Sstevel@tonic-gate char *p;
4230Sstevel@tonic-gate BIGNUM *bn,**t[6],*b;
4240Sstevel@tonic-gate BN_ULONG *ul;
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate if (r->d == NULL) return(1);
4270Sstevel@tonic-gate t[0]= &r->d;
4280Sstevel@tonic-gate t[1]= &r->p;
4290Sstevel@tonic-gate t[2]= &r->q;
4300Sstevel@tonic-gate t[3]= &r->dmp1;
4310Sstevel@tonic-gate t[4]= &r->dmq1;
4320Sstevel@tonic-gate t[5]= &r->iqmp;
4330Sstevel@tonic-gate k=sizeof(BIGNUM)*6;
4340Sstevel@tonic-gate off=k/sizeof(BN_ULONG)+1;
4350Sstevel@tonic-gate j=1;
4360Sstevel@tonic-gate for (i=0; i<6; i++)
4370Sstevel@tonic-gate j+= (*t[i])->top;
4380Sstevel@tonic-gate if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
4390Sstevel@tonic-gate {
440*2139Sjp161948 RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
4410Sstevel@tonic-gate return(0);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate bn=(BIGNUM *)p;
4440Sstevel@tonic-gate ul=(BN_ULONG *)&(p[off]);
4450Sstevel@tonic-gate for (i=0; i<6; i++)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate b= *(t[i]);
4480Sstevel@tonic-gate *(t[i])= &(bn[i]);
4490Sstevel@tonic-gate memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
4500Sstevel@tonic-gate bn[i].flags=BN_FLG_STATIC_DATA;
4510Sstevel@tonic-gate bn[i].d=ul;
4520Sstevel@tonic-gate memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
4530Sstevel@tonic-gate ul+=b->top;
4540Sstevel@tonic-gate BN_clear_free(b);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /* I should fix this so it can still be done */
4580Sstevel@tonic-gate r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate r->bignum_data=p;
4610Sstevel@tonic-gate return(1);
4620Sstevel@tonic-gate }
463