sl@0: /* crypto/rsa/rsa_lib.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: #include sl@0: #endif sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT; sl@0: sl@0: #ifndef EMULATOR sl@0: static const RSA_METHOD *default_RSA_meth=NULL; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(default_RSA_meth,rsa_lib,const RSA_METHOD *) sl@0: #define default_RSA_meth (*GET_WSD_VAR_NAME(default_RSA_meth,rsa_lib, s)()) sl@0: #endif sl@0: sl@0: EXPORT_C RSA *RSA_new(void) sl@0: { sl@0: RSA *r=RSA_new_method(NULL); sl@0: sl@0: return r; sl@0: } sl@0: sl@0: EXPORT_C void RSA_set_default_method(const RSA_METHOD *meth) sl@0: { sl@0: default_RSA_meth = meth; sl@0: } sl@0: sl@0: EXPORT_C const RSA_METHOD *RSA_get_default_method(void) sl@0: { sl@0: if (default_RSA_meth == NULL) sl@0: { sl@0: #ifdef RSA_NULL sl@0: default_RSA_meth=RSA_null_method(); sl@0: #else sl@0: #if 0 /* was: #ifdef RSAref */ sl@0: default_RSA_meth=RSA_PKCS1_RSAref(); sl@0: #else sl@0: default_RSA_meth=RSA_PKCS1_SSLeay(); sl@0: #endif sl@0: #endif sl@0: } sl@0: sl@0: return default_RSA_meth; sl@0: } sl@0: sl@0: EXPORT_C const RSA_METHOD *RSA_get_method(const RSA *rsa) sl@0: { sl@0: return rsa->meth; sl@0: } sl@0: sl@0: EXPORT_C int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) sl@0: { sl@0: /* NB: The caller is specifically setting a method, so it's not up to us sl@0: * to deal with which ENGINE it comes from. */ sl@0: const RSA_METHOD *mtmp; sl@0: mtmp = rsa->meth; sl@0: if (mtmp->finish) mtmp->finish(rsa); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (rsa->engine) sl@0: { sl@0: ENGINE_finish(rsa->engine); sl@0: rsa->engine = NULL; sl@0: } sl@0: #endif sl@0: rsa->meth = meth; sl@0: if (meth->init) meth->init(rsa); sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C RSA *RSA_new_method(ENGINE *engine) sl@0: { sl@0: RSA *ret; sl@0: sl@0: ret=(RSA *)OPENSSL_malloc(sizeof(RSA)); sl@0: if (ret == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); sl@0: return NULL; sl@0: } sl@0: sl@0: ret->meth = RSA_get_default_method(); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (engine) sl@0: { sl@0: if (!ENGINE_init(engine)) sl@0: { sl@0: RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); sl@0: OPENSSL_free(ret); sl@0: return NULL; sl@0: } sl@0: ret->engine = engine; sl@0: } sl@0: else sl@0: ret->engine = ENGINE_get_default_RSA(); sl@0: if(ret->engine) sl@0: { sl@0: ret->meth = ENGINE_get_RSA(ret->engine); sl@0: if(!ret->meth) sl@0: { sl@0: RSAerr(RSA_F_RSA_NEW_METHOD, sl@0: ERR_R_ENGINE_LIB); sl@0: ENGINE_finish(ret->engine); sl@0: OPENSSL_free(ret); sl@0: return NULL; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: ret->pad=0; sl@0: ret->version=0; sl@0: ret->n=NULL; sl@0: ret->e=NULL; sl@0: ret->d=NULL; sl@0: ret->p=NULL; sl@0: ret->q=NULL; sl@0: ret->dmp1=NULL; sl@0: ret->dmq1=NULL; sl@0: ret->iqmp=NULL; sl@0: ret->references=1; sl@0: ret->_method_mod_n=NULL; sl@0: ret->_method_mod_p=NULL; sl@0: ret->_method_mod_q=NULL; sl@0: ret->blinding=NULL; sl@0: ret->mt_blinding=NULL; sl@0: ret->bignum_data=NULL; sl@0: ret->flags=ret->meth->flags; sl@0: CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); sl@0: if ((ret->meth->init != NULL) && !ret->meth->init(ret)) sl@0: { sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (ret->engine) sl@0: ENGINE_finish(ret->engine); sl@0: #endif sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); sl@0: OPENSSL_free(ret); sl@0: ret=NULL; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C void RSA_free(RSA *r) sl@0: { sl@0: int i; sl@0: sl@0: if (r == NULL) return; sl@0: sl@0: i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("RSA",r); sl@0: #endif sl@0: if (i > 0) return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"RSA_free, bad reference count\n"); sl@0: abort(); sl@0: } sl@0: #endif sl@0: sl@0: if (r->meth->finish) sl@0: r->meth->finish(r); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if (r->engine) sl@0: ENGINE_finish(r->engine); sl@0: #endif sl@0: sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); sl@0: sl@0: if (r->n != NULL) BN_clear_free(r->n); sl@0: if (r->e != NULL) BN_clear_free(r->e); sl@0: if (r->d != NULL) BN_clear_free(r->d); sl@0: if (r->p != NULL) BN_clear_free(r->p); sl@0: if (r->q != NULL) BN_clear_free(r->q); sl@0: if (r->dmp1 != NULL) BN_clear_free(r->dmp1); sl@0: if (r->dmq1 != NULL) BN_clear_free(r->dmq1); sl@0: if (r->iqmp != NULL) BN_clear_free(r->iqmp); sl@0: if (r->blinding != NULL) BN_BLINDING_free(r->blinding); sl@0: if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding); sl@0: if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data); sl@0: OPENSSL_free(r); sl@0: } sl@0: sl@0: EXPORT_C int RSA_up_ref(RSA *r) sl@0: { sl@0: int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("RSA",r); sl@0: #endif sl@0: #ifdef REF_CHECK sl@0: if (i < 2) sl@0: { sl@0: fprintf(stderr, "RSA_up_ref, bad reference count\n"); sl@0: abort(); sl@0: } sl@0: #endif sl@0: return ((i > 1) ? 1 : 0); sl@0: } sl@0: sl@0: EXPORT_C int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, sl@0: CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) sl@0: { sl@0: return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, sl@0: new_func, dup_func, free_func); sl@0: } sl@0: sl@0: EXPORT_C int RSA_set_ex_data(RSA *r, int idx, void *arg) sl@0: { sl@0: return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); sl@0: } sl@0: sl@0: EXPORT_C void *RSA_get_ex_data(const RSA *r, int idx) sl@0: { sl@0: return(CRYPTO_get_ex_data(&r->ex_data,idx)); sl@0: } sl@0: sl@0: EXPORT_C int RSA_size(const RSA *r) sl@0: { sl@0: return(BN_num_bytes(r->n)); sl@0: } sl@0: sl@0: EXPORT_C int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, sl@0: RSA *rsa, int padding) sl@0: { sl@0: return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); sl@0: } sl@0: sl@0: EXPORT_C int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, sl@0: RSA *rsa, int padding) sl@0: { sl@0: return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); sl@0: } sl@0: sl@0: EXPORT_C int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, sl@0: RSA *rsa, int padding) sl@0: { sl@0: return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); sl@0: } sl@0: sl@0: EXPORT_C int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, sl@0: RSA *rsa, int padding) sl@0: { sl@0: return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); sl@0: } sl@0: sl@0: EXPORT_C int RSA_flags(const RSA *r) sl@0: { sl@0: return((r == NULL)?0:r->meth->flags); sl@0: } sl@0: sl@0: EXPORT_C void RSA_blinding_off(RSA *rsa) sl@0: { sl@0: if (rsa->blinding != NULL) sl@0: { sl@0: BN_BLINDING_free(rsa->blinding); sl@0: rsa->blinding=NULL; sl@0: } sl@0: rsa->flags &= ~RSA_FLAG_BLINDING; sl@0: rsa->flags |= RSA_FLAG_NO_BLINDING; sl@0: } sl@0: sl@0: EXPORT_C int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) sl@0: { sl@0: int ret=0; sl@0: sl@0: if (rsa->blinding != NULL) sl@0: RSA_blinding_off(rsa); sl@0: sl@0: rsa->blinding = RSA_setup_blinding(rsa, ctx); sl@0: if (rsa->blinding == NULL) sl@0: goto err; sl@0: sl@0: rsa->flags |= RSA_FLAG_BLINDING; sl@0: rsa->flags &= ~RSA_FLAG_NO_BLINDING; sl@0: ret=1; sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, sl@0: const BIGNUM *q, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *ret = NULL, *r0, *r1, *r2; sl@0: sl@0: if (d == NULL || p == NULL || q == NULL) sl@0: return NULL; sl@0: sl@0: BN_CTX_start(ctx); sl@0: r0 = BN_CTX_get(ctx); sl@0: r1 = BN_CTX_get(ctx); sl@0: r2 = BN_CTX_get(ctx); sl@0: if (r2 == NULL) sl@0: goto err; sl@0: sl@0: if (!BN_sub(r1, p, BN_value_one())) goto err; sl@0: if (!BN_sub(r2, q, BN_value_one())) goto err; sl@0: if (!BN_mul(r0, r1, r2, ctx)) goto err; sl@0: sl@0: ret = BN_mod_inverse(NULL, d, r0, ctx); sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx) sl@0: { sl@0: BIGNUM local_n; sl@0: BIGNUM *e,*n; sl@0: BN_CTX *ctx; sl@0: BN_BLINDING *ret = NULL; sl@0: sl@0: if (in_ctx == NULL) sl@0: { sl@0: if ((ctx = BN_CTX_new()) == NULL) return 0; sl@0: } sl@0: else sl@0: ctx = in_ctx; sl@0: sl@0: BN_CTX_start(ctx); sl@0: e = BN_CTX_get(ctx); sl@0: if (e == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: if (rsa->e == NULL) sl@0: { sl@0: e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx); sl@0: if (e == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT); sl@0: goto err; sl@0: } sl@0: } sl@0: else sl@0: e = rsa->e; sl@0: sl@0: sl@0: if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL) sl@0: { sl@0: /* if PRNG is not properly seeded, resort to secret sl@0: * exponent as unpredictable seed */ sl@0: RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0); sl@0: } sl@0: sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: /* Set BN_FLG_CONSTTIME flag */ sl@0: n = &local_n; sl@0: BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: n = rsa->n; sl@0: sl@0: ret = BN_BLINDING_create_param(NULL, e, n, ctx, sl@0: rsa->meth->bn_mod_exp, rsa->_method_mod_n); sl@0: if (ret == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: BN_BLINDING_set_thread_id(ret, CRYPTO_thread_id()); sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: if (in_ctx == NULL) sl@0: BN_CTX_free(ctx); sl@0: if(rsa->e == NULL) sl@0: BN_free(e); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int RSA_memory_lock(RSA *r) sl@0: { sl@0: int i,j,k,off; sl@0: char *p; sl@0: BIGNUM *bn,**t[6],*b; sl@0: BN_ULONG *ul; sl@0: sl@0: if (r->d == NULL) return(1); sl@0: t[0]= &r->d; sl@0: t[1]= &r->p; sl@0: t[2]= &r->q; sl@0: t[3]= &r->dmp1; sl@0: t[4]= &r->dmq1; sl@0: t[5]= &r->iqmp; sl@0: k=sizeof(BIGNUM)*6; sl@0: off=k/sizeof(BN_ULONG)+1; sl@0: j=1; sl@0: for (i=0; i<6; i++) sl@0: j+= (*t[i])->top; sl@0: if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: bn=(BIGNUM *)p; sl@0: ul=(BN_ULONG *)&(p[off]); sl@0: for (i=0; i<6; i++) sl@0: { sl@0: b= *(t[i]); sl@0: *(t[i])= &(bn[i]); sl@0: memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM)); sl@0: bn[i].flags=BN_FLG_STATIC_DATA; sl@0: bn[i].d=ul; sl@0: memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top); sl@0: ul+=b->top; sl@0: BN_clear_free(b); sl@0: } sl@0: sl@0: /* I should fix this so it can still be done */ sl@0: r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC); sl@0: sl@0: r->bignum_data=p; sl@0: return(1); sl@0: }