sl@0: /* crypto/rsa/rsa_eay.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: * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 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: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include 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: #ifndef RSA_NULL sl@0: sl@0: static int RSA_eay_public_encrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa,int padding); sl@0: static int RSA_eay_private_encrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa,int padding); sl@0: static int RSA_eay_public_decrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa,int padding); sl@0: static int RSA_eay_private_decrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa,int padding); sl@0: static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx); sl@0: static int RSA_eay_init(RSA *rsa); sl@0: static int RSA_eay_finish(RSA *rsa); sl@0: sl@0: #ifndef EMULATOR sl@0: static RSA_METHOD rsa_pkcs1_eay_meth={ sl@0: "Eric Young's PKCS#1 RSA", sl@0: RSA_eay_public_encrypt, sl@0: RSA_eay_public_decrypt, /* signature verification */ sl@0: RSA_eay_private_encrypt, /* signing */ sl@0: RSA_eay_private_decrypt, sl@0: RSA_eay_mod_exp, sl@0: BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ sl@0: RSA_eay_init, sl@0: RSA_eay_finish, sl@0: 0, /* flags */ sl@0: NULL, sl@0: 0, /* rsa_sign */ sl@0: 0, /* rsa_verify */ sl@0: NULL /* rsa_keygen */ sl@0: }; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(rsa_pkcs1_eay_meth,rsa_eay,RSA_METHOD ) sl@0: #define rsa_pkcs1_eay_meth (*GET_WSD_VAR_NAME(rsa_pkcs1_eay_meth,rsa_eay,s)()) sl@0: const RSA_METHOD temp_s_rsa_pkcs1_eay_meth={ sl@0: "Eric Young's PKCS#1 RSA", sl@0: RSA_eay_public_encrypt, sl@0: RSA_eay_public_decrypt, /* signature verification */ sl@0: RSA_eay_private_encrypt, /* signing */ sl@0: RSA_eay_private_decrypt, sl@0: RSA_eay_mod_exp, sl@0: BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ sl@0: RSA_eay_init, sl@0: RSA_eay_finish, sl@0: 0, /* flags */ sl@0: NULL, sl@0: 0, /* rsa_sign */ sl@0: 0, /* rsa_verify */ sl@0: NULL /* rsa_keygen */ sl@0: }; sl@0: sl@0: #endif sl@0: EXPORT_C const RSA_METHOD *RSA_PKCS1_SSLeay(void) sl@0: { sl@0: return(&rsa_pkcs1_eay_meth); sl@0: } sl@0: sl@0: /* Usage example; sl@0: * MONT_HELPER(rsa, bn_ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err); sl@0: */ sl@0: #define MONT_HELPER(rsa, ctx, m, pre_cond, err_instr) \ sl@0: if((pre_cond) && ((rsa)->_method_mod_##m == NULL) && \ sl@0: !BN_MONT_CTX_set_locked(&((rsa)->_method_mod_##m), \ sl@0: CRYPTO_LOCK_RSA, \ sl@0: (rsa)->m, (ctx))) \ sl@0: err_instr sl@0: sl@0: static int RSA_eay_public_encrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa, int padding) sl@0: { sl@0: BIGNUM *f,*ret; sl@0: int i,j,k,num=0,r= -1; sl@0: unsigned char *buf=NULL; sl@0: BN_CTX *ctx=NULL; sl@0: if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE); sl@0: return -1; sl@0: } sl@0: sl@0: if (BN_ucmp(rsa->n, rsa->e) <= 0) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); sl@0: return -1; sl@0: } sl@0: sl@0: /* for large moduli, enforce exponent limit */ sl@0: if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) sl@0: { sl@0: if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: sl@0: if ((ctx=BN_CTX_new()) == NULL) goto err; sl@0: BN_CTX_start(ctx); sl@0: f = BN_CTX_get(ctx); sl@0: ret = BN_CTX_get(ctx); sl@0: num=BN_num_bytes(rsa->n); sl@0: buf = OPENSSL_malloc(num); sl@0: if (!f || !ret || !buf) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: switch (padding) sl@0: { sl@0: case RSA_PKCS1_PADDING: sl@0: i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); sl@0: break; sl@0: #ifndef OPENSSL_NO_SHA sl@0: case RSA_PKCS1_OAEP_PADDING: sl@0: i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); sl@0: break; sl@0: #endif sl@0: case RSA_SSLV23_PADDING: sl@0: i=RSA_padding_add_SSLv23(buf,num,from,flen); sl@0: break; sl@0: case RSA_NO_PADDING: sl@0: i=RSA_padding_add_none(buf,num,from,flen); sl@0: break; sl@0: default: sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); sl@0: goto err; sl@0: } sl@0: if (i <= 0) goto err; sl@0: sl@0: if (BN_bin2bn(buf,num,f) == NULL) goto err; sl@0: sl@0: if (BN_ucmp(f, rsa->n) >= 0) sl@0: { sl@0: /* usually the padding functions would catch this */ sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); sl@0: goto err; sl@0: } sl@0: sl@0: MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); sl@0: sl@0: if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, sl@0: rsa->_method_mod_n)) goto err; sl@0: sl@0: /* put in leading 0 bytes if the number is less than the sl@0: * length of the modulus */ sl@0: j=BN_num_bytes(ret); sl@0: i=BN_bn2bin(ret,&(to[num-j])); sl@0: for (k=0; k<(num-i); k++) sl@0: to[k]=0; sl@0: sl@0: r=num; sl@0: err: sl@0: if (ctx != NULL) sl@0: { sl@0: BN_CTX_end(ctx); sl@0: BN_CTX_free(ctx); sl@0: } sl@0: if (buf != NULL) sl@0: { sl@0: OPENSSL_cleanse(buf,num); sl@0: OPENSSL_free(buf); sl@0: } sl@0: return(r); sl@0: } sl@0: sl@0: static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) sl@0: { sl@0: BN_BLINDING *ret; sl@0: int got_write_lock = 0; sl@0: sl@0: CRYPTO_r_lock(CRYPTO_LOCK_RSA); sl@0: sl@0: if (rsa->blinding == NULL) sl@0: { sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_RSA); sl@0: CRYPTO_w_lock(CRYPTO_LOCK_RSA); sl@0: got_write_lock = 1; sl@0: sl@0: if (rsa->blinding == NULL) sl@0: rsa->blinding = RSA_setup_blinding(rsa, ctx); sl@0: } sl@0: sl@0: ret = rsa->blinding; sl@0: if (ret == NULL) sl@0: goto err; sl@0: sl@0: if (BN_BLINDING_get_thread_id(ret) == CRYPTO_thread_id()) sl@0: { sl@0: /* rsa->blinding is ours! */ sl@0: sl@0: *local = 1; sl@0: } sl@0: else sl@0: { sl@0: /* resort to rsa->mt_blinding instead */ sl@0: sl@0: *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert() sl@0: * that the BN_BLINDING is shared, meaning that accesses sl@0: * require locks, and that the blinding factor must be sl@0: * stored outside the BN_BLINDING sl@0: */ sl@0: sl@0: if (rsa->mt_blinding == NULL) sl@0: { sl@0: if (!got_write_lock) sl@0: { sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_RSA); sl@0: CRYPTO_w_lock(CRYPTO_LOCK_RSA); sl@0: got_write_lock = 1; sl@0: } sl@0: sl@0: if (rsa->mt_blinding == NULL) sl@0: rsa->mt_blinding = RSA_setup_blinding(rsa, ctx); sl@0: } sl@0: ret = rsa->mt_blinding; sl@0: } sl@0: sl@0: err: sl@0: if (got_write_lock) sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_RSA); sl@0: else sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_RSA); sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f, sl@0: BIGNUM *r, BN_CTX *ctx) sl@0: { sl@0: if (local) sl@0: return BN_BLINDING_convert_ex(f, NULL, b, ctx); sl@0: else sl@0: { sl@0: int ret; sl@0: CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING); sl@0: ret = BN_BLINDING_convert_ex(f, r, b, ctx); sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING); sl@0: return ret; sl@0: } sl@0: } sl@0: sl@0: static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f, sl@0: BIGNUM *r, BN_CTX *ctx) sl@0: { sl@0: if (local) sl@0: return BN_BLINDING_invert_ex(f, NULL, b, ctx); sl@0: else sl@0: { sl@0: int ret; sl@0: CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING); sl@0: ret = BN_BLINDING_invert_ex(f, r, b, ctx); sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING); sl@0: return ret; sl@0: } sl@0: } sl@0: sl@0: /* signing */ sl@0: static int RSA_eay_private_encrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa, int padding) sl@0: { sl@0: BIGNUM *f, *ret, *br, *res; sl@0: int i,j,k,num=0,r= -1; sl@0: unsigned char *buf=NULL; sl@0: BN_CTX *ctx=NULL; sl@0: int local_blinding = 0; sl@0: BN_BLINDING *blinding = NULL; sl@0: sl@0: if ((ctx=BN_CTX_new()) == NULL) goto err; sl@0: BN_CTX_start(ctx); sl@0: f = BN_CTX_get(ctx); sl@0: br = BN_CTX_get(ctx); sl@0: ret = BN_CTX_get(ctx); sl@0: num = BN_num_bytes(rsa->n); sl@0: buf = OPENSSL_malloc(num); sl@0: if(!f || !ret || !buf) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: switch (padding) sl@0: { sl@0: case RSA_PKCS1_PADDING: sl@0: i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); sl@0: break; sl@0: case RSA_X931_PADDING: sl@0: i=RSA_padding_add_X931(buf,num,from,flen); sl@0: break; sl@0: case RSA_NO_PADDING: sl@0: i=RSA_padding_add_none(buf,num,from,flen); sl@0: break; sl@0: case RSA_SSLV23_PADDING: sl@0: default: sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); sl@0: goto err; sl@0: } sl@0: if (i <= 0) goto err; sl@0: sl@0: if (BN_bin2bn(buf,num,f) == NULL) goto err; sl@0: sl@0: if (BN_ucmp(f, rsa->n) >= 0) sl@0: { sl@0: /* usually the padding functions would catch this */ sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); sl@0: goto err; sl@0: } sl@0: sl@0: if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) sl@0: { sl@0: blinding = rsa_get_blinding(rsa, &local_blinding, ctx); sl@0: sl@0: if (blinding == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (blinding != NULL) sl@0: if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx)) sl@0: goto err; sl@0: sl@0: if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || sl@0: ((rsa->p != NULL) && sl@0: (rsa->q != NULL) && sl@0: (rsa->dmp1 != NULL) && sl@0: (rsa->dmq1 != NULL) && sl@0: (rsa->iqmp != NULL)) ) sl@0: { sl@0: if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; sl@0: } sl@0: else sl@0: { sl@0: BIGNUM local_d; sl@0: BIGNUM *d = NULL; sl@0: sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: BN_init(&local_d); sl@0: d = &local_d; sl@0: BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: d = rsa->d; sl@0: sl@0: MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); sl@0: sl@0: if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, sl@0: rsa->_method_mod_n)) goto err; sl@0: } sl@0: sl@0: if (blinding) sl@0: if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) sl@0: goto err; sl@0: sl@0: if (padding == RSA_X931_PADDING) sl@0: { sl@0: BN_sub(f, rsa->n, ret); sl@0: if (BN_cmp(ret, f)) sl@0: res = f; sl@0: else sl@0: res = ret; sl@0: } sl@0: else sl@0: res = ret; sl@0: sl@0: /* put in leading 0 bytes if the number is less than the sl@0: * length of the modulus */ sl@0: j=BN_num_bytes(res); sl@0: i=BN_bn2bin(res,&(to[num-j])); sl@0: for (k=0; k<(num-i); k++) sl@0: to[k]=0; sl@0: sl@0: r=num; sl@0: err: sl@0: if (ctx != NULL) sl@0: { sl@0: BN_CTX_end(ctx); sl@0: BN_CTX_free(ctx); sl@0: } sl@0: if (buf != NULL) sl@0: { sl@0: OPENSSL_cleanse(buf,num); sl@0: OPENSSL_free(buf); sl@0: } sl@0: return(r); sl@0: } sl@0: sl@0: static int RSA_eay_private_decrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa, int padding) sl@0: { sl@0: BIGNUM *f, *ret, *br; sl@0: int j,num=0,r= -1; sl@0: unsigned char *p; sl@0: unsigned char *buf=NULL; sl@0: BN_CTX *ctx=NULL; sl@0: int local_blinding = 0; sl@0: BN_BLINDING *blinding = NULL; sl@0: sl@0: if((ctx = BN_CTX_new()) == NULL) goto err; sl@0: BN_CTX_start(ctx); sl@0: f = BN_CTX_get(ctx); sl@0: br = BN_CTX_get(ctx); sl@0: ret = BN_CTX_get(ctx); sl@0: num = BN_num_bytes(rsa->n); sl@0: buf = OPENSSL_malloc(num); sl@0: if(!f || !ret || !buf) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: /* This check was for equality but PGP does evil things sl@0: * and chops off the top '0' bytes */ sl@0: if (flen > num) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); sl@0: goto err; sl@0: } sl@0: sl@0: /* make data into a big number */ sl@0: if (BN_bin2bn(from,(int)flen,f) == NULL) goto err; sl@0: sl@0: if (BN_ucmp(f, rsa->n) >= 0) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); sl@0: goto err; sl@0: } sl@0: sl@0: if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) sl@0: { sl@0: blinding = rsa_get_blinding(rsa, &local_blinding, ctx); sl@0: if (blinding == NULL) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (blinding != NULL) sl@0: if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx)) sl@0: goto err; sl@0: sl@0: /* do the decrypt */ sl@0: if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || sl@0: ((rsa->p != NULL) && sl@0: (rsa->q != NULL) && sl@0: (rsa->dmp1 != NULL) && sl@0: (rsa->dmq1 != NULL) && sl@0: (rsa->iqmp != NULL)) ) sl@0: { sl@0: if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; sl@0: } sl@0: else sl@0: { sl@0: BIGNUM local_d; sl@0: BIGNUM *d = NULL; sl@0: sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: d = &local_d; sl@0: BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: d = rsa->d; sl@0: sl@0: MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); sl@0: if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, sl@0: rsa->_method_mod_n)) sl@0: goto err; sl@0: } sl@0: sl@0: if (blinding) sl@0: if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) sl@0: goto err; sl@0: sl@0: p=buf; sl@0: j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */ sl@0: sl@0: switch (padding) sl@0: { sl@0: case RSA_PKCS1_PADDING: sl@0: r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); sl@0: break; sl@0: #ifndef OPENSSL_NO_SHA sl@0: case RSA_PKCS1_OAEP_PADDING: sl@0: r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); sl@0: break; sl@0: #endif sl@0: case RSA_SSLV23_PADDING: sl@0: r=RSA_padding_check_SSLv23(to,num,buf,j,num); sl@0: break; sl@0: case RSA_NO_PADDING: sl@0: r=RSA_padding_check_none(to,num,buf,j,num); sl@0: break; sl@0: default: sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); sl@0: goto err; sl@0: } sl@0: if (r < 0) sl@0: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); sl@0: sl@0: err: sl@0: if (ctx != NULL) sl@0: { sl@0: BN_CTX_end(ctx); sl@0: BN_CTX_free(ctx); sl@0: } sl@0: if (buf != NULL) sl@0: { sl@0: OPENSSL_cleanse(buf,num); sl@0: OPENSSL_free(buf); sl@0: } sl@0: return(r); sl@0: } sl@0: sl@0: /* signature verification */ sl@0: static int RSA_eay_public_decrypt(int flen, const unsigned char *from, sl@0: unsigned char *to, RSA *rsa, int padding) sl@0: { sl@0: BIGNUM *f,*ret; sl@0: int i,num=0,r= -1; sl@0: unsigned char *p; sl@0: unsigned char *buf=NULL; sl@0: BN_CTX *ctx=NULL; sl@0: if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE); sl@0: return -1; sl@0: } sl@0: sl@0: if (BN_ucmp(rsa->n, rsa->e) <= 0) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); sl@0: return -1; sl@0: } sl@0: sl@0: /* for large moduli, enforce exponent limit */ sl@0: if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) sl@0: { sl@0: if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); sl@0: return -1; sl@0: } sl@0: } sl@0: if((ctx = BN_CTX_new()) == NULL) goto err; sl@0: BN_CTX_start(ctx); sl@0: f = BN_CTX_get(ctx); sl@0: ret = BN_CTX_get(ctx); sl@0: num=BN_num_bytes(rsa->n); sl@0: buf = OPENSSL_malloc(num); sl@0: if(!f || !ret || !buf) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: /* This check was for equality but PGP does evil things sl@0: * and chops off the top '0' bytes */ sl@0: if (flen > num) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); sl@0: goto err; sl@0: } sl@0: sl@0: if (BN_bin2bn(from,flen,f) == NULL) goto err; sl@0: sl@0: if (BN_ucmp(f, rsa->n) >= 0) sl@0: { sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); sl@0: goto err; sl@0: } sl@0: sl@0: MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); sl@0: sl@0: if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, sl@0: rsa->_method_mod_n)) goto err; sl@0: sl@0: if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12)) sl@0: BN_sub(ret, rsa->n, ret); sl@0: sl@0: p=buf; sl@0: i=BN_bn2bin(ret,p); sl@0: sl@0: switch (padding) sl@0: { sl@0: case RSA_PKCS1_PADDING: sl@0: r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); sl@0: break; sl@0: case RSA_X931_PADDING: sl@0: r=RSA_padding_check_X931(to,num,buf,i,num); sl@0: break; sl@0: case RSA_NO_PADDING: sl@0: r=RSA_padding_check_none(to,num,buf,i,num); sl@0: break; sl@0: default: sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); sl@0: goto err; sl@0: } sl@0: if (r < 0) sl@0: RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); sl@0: sl@0: err: sl@0: if (ctx != NULL) sl@0: { sl@0: BN_CTX_end(ctx); sl@0: BN_CTX_free(ctx); sl@0: } sl@0: if (buf != NULL) sl@0: { sl@0: OPENSSL_cleanse(buf,num); sl@0: OPENSSL_free(buf); sl@0: } sl@0: return(r); sl@0: } sl@0: sl@0: static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *r1,*m1,*vrfy; sl@0: BIGNUM local_dmp1,local_dmq1,local_c,local_r1; sl@0: BIGNUM *dmp1,*dmq1,*c,*pr1; sl@0: int bn_flags; sl@0: int ret=0; sl@0: sl@0: BN_CTX_start(ctx); sl@0: r1 = BN_CTX_get(ctx); sl@0: m1 = BN_CTX_get(ctx); sl@0: vrfy = BN_CTX_get(ctx); sl@0: sl@0: /* Make sure mod_inverse in montgomerey intialization use correct sl@0: * BN_FLG_CONSTTIME flag. sl@0: */ sl@0: bn_flags = rsa->p->flags; sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: rsa->p->flags |= BN_FLG_CONSTTIME; sl@0: } sl@0: MONT_HELPER(rsa, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err); sl@0: /* We restore bn_flags back */ sl@0: rsa->p->flags = bn_flags; sl@0: sl@0: /* Make sure mod_inverse in montgomerey intialization use correct sl@0: * BN_FLG_CONSTTIME flag. sl@0: */ sl@0: bn_flags = rsa->q->flags; sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: rsa->q->flags |= BN_FLG_CONSTTIME; sl@0: } sl@0: MONT_HELPER(rsa, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err); sl@0: /* We restore bn_flags back */ sl@0: rsa->q->flags = bn_flags; sl@0: MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); sl@0: sl@0: /* compute I mod q */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: c = &local_c; sl@0: BN_with_flags(c, I, BN_FLG_CONSTTIME); sl@0: if (!BN_mod(r1,c,rsa->q,ctx)) goto err; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_mod(r1,I,rsa->q,ctx)) goto err; sl@0: } sl@0: /* compute r1^dmq1 mod q */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: dmq1 = &local_dmq1; sl@0: BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: dmq1 = rsa->dmq1; sl@0: if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx, sl@0: rsa->_method_mod_q)) goto err; sl@0: sl@0: /* compute I mod p */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: c = &local_c; sl@0: BN_with_flags(c, I, BN_FLG_CONSTTIME); sl@0: if (!BN_mod(r1,c,rsa->p,ctx)) goto err; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_mod(r1,I,rsa->p,ctx)) goto err; sl@0: } sl@0: /* compute r1^dmp1 mod p */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: dmp1 = &local_dmp1; sl@0: BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: dmp1 = rsa->dmp1; sl@0: if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx, sl@0: rsa->_method_mod_p)) goto err; sl@0: sl@0: if (!BN_sub(r0,r0,m1)) goto err; sl@0: /* This will help stop the size of r0 increasing, which does sl@0: * affect the multiply if it optimised for a power of 2 size */ sl@0: if (BN_is_negative(r0)) sl@0: if (!BN_add(r0,r0,rsa->p)) goto err; sl@0: sl@0: if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err; sl@0: sl@0: /* Turn BN_FLG_CONSTTIME flag on before division operation */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: pr1 = &local_r1; sl@0: BN_with_flags(pr1, r1, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: pr1 = r1; sl@0: if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err; sl@0: sl@0: /* If p < q it is occasionally possible for the correction of sl@0: * adding 'p' if r0 is negative above to leave the result still sl@0: * negative. This can break the private key operations: the following sl@0: * second correction should *always* correct this rare occurrence. sl@0: * This will *never* happen with OpenSSL generated keys because sl@0: * they ensure p > q [steve] sl@0: */ sl@0: if (BN_is_negative(r0)) sl@0: if (!BN_add(r0,r0,rsa->p)) goto err; sl@0: if (!BN_mul(r1,r0,rsa->q,ctx)) goto err; sl@0: if (!BN_add(r0,r1,m1)) goto err; sl@0: sl@0: if (rsa->e && rsa->n) sl@0: { sl@0: if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err; sl@0: /* If 'I' was greater than (or equal to) rsa->n, the operation sl@0: * will be equivalent to using 'I mod n'. However, the result of sl@0: * the verify will *always* be less than 'n' so we don't check sl@0: * for absolute equality, just congruency. */ sl@0: if (!BN_sub(vrfy, vrfy, I)) goto err; sl@0: if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err; sl@0: if (BN_is_negative(vrfy)) sl@0: if (!BN_add(vrfy, vrfy, rsa->n)) goto err; sl@0: if (!BN_is_zero(vrfy)) sl@0: { sl@0: /* 'I' and 'vrfy' aren't congruent mod n. Don't leak sl@0: * miscalculated CRT output, just do a raw (slower) sl@0: * mod_exp and return that instead. */ sl@0: sl@0: BIGNUM local_d; sl@0: BIGNUM *d = NULL; sl@0: sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: d = &local_d; sl@0: BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: d = rsa->d; sl@0: if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx, sl@0: rsa->_method_mod_n)) goto err; sl@0: } sl@0: } sl@0: ret=1; sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return(ret); sl@0: } sl@0: sl@0: static int RSA_eay_init(RSA *rsa) sl@0: { sl@0: rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; sl@0: return(1); sl@0: } sl@0: sl@0: static int RSA_eay_finish(RSA *rsa) sl@0: { sl@0: if (rsa->_method_mod_n != NULL) sl@0: BN_MONT_CTX_free(rsa->_method_mod_n); sl@0: if (rsa->_method_mod_p != NULL) sl@0: BN_MONT_CTX_free(rsa->_method_mod_p); sl@0: if (rsa->_method_mod_q != NULL) sl@0: BN_MONT_CTX_free(rsa->_method_mod_q); sl@0: return(1); sl@0: } sl@0: sl@0: #endif