rsa_eay.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include <stdio.h>
#include "cryptlib.h"
#ifndef RSA_NULL
static RSA_METHOD rsa_pkcs1_eay_meth={
"Eric Young's PKCS#1 RSA",
RSA_eay_public_decrypt, /* signature verification */
RSA_eay_private_encrypt, /* signing */
BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
0, /* flags */
NULL,
0, /* rsa_sign */
0 /* rsa_verify */
};
const RSA_METHOD *RSA_PKCS1_SSLeay(void)
{
return(&rsa_pkcs1_eay_meth);
}
{
int i,j,k,num=0,r= -1;
BN_init(&f);
{
goto err;
}
switch (padding)
{
case RSA_PKCS1_PADDING:
break;
#ifndef OPENSSL_NO_SHA
case RSA_PKCS1_OAEP_PADDING:
break;
#endif
case RSA_SSLV23_PADDING:
break;
case RSA_NO_PADDING:
break;
default:
goto err;
}
if (i <= 0) goto err;
{
/* usually the padding functions would catch this */
goto err;
}
{
goto err;
{
goto err;
}
{
{
bn_mont_ctx = NULL;
}
}
if (bn_mont_ctx)
}
/* put in leading 0 bytes if the number is less than the
* length of the modulus */
j=BN_num_bytes(&ret);
for (k=0; k<(num-i); k++)
to[k]=0;
r=num;
err:
BN_clear_free(&f);
BN_clear_free(&ret);
{
}
return(r);
}
{
int ret = 1;
/* Check again inside the lock - the macro's check is racey */
return ret;
}
do { \
} while(0)
{
/* added in OpenSSL 0.9.6j and 0.9.7b */
/* NB: similar code appears in RSA_blinding_on (rsa_lib.c);
* this should be placed in a new function of its own, but for reasons
* of binary compatibility can't */
A = BN_CTX_get(ctx);
{
/* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
}
else
{
}
goto err;
err:
return ret;
}
/* signing */
{
int i,j,k,num=0,r= -1;
int local_blinding = 0;
BN_init(&f);
{
goto err;
}
switch (padding)
{
case RSA_PKCS1_PADDING:
break;
case RSA_NO_PADDING:
break;
case RSA_SSLV23_PADDING:
default:
goto err;
}
if (i <= 0) goto err;
{
/* usually the padding functions would catch this */
goto err;
}
/* Now unless blinding is disabled, 'blinding' is non-NULL.
* But the BN_BLINDING object may be owned by some other thread
* (we don't want to keep it constant and we don't want to use
* lots of locking to avoid race conditions, so only a single
* thread can use it; other threads have to use local blinding
* factors) */
{
{
goto err;
}
}
{
{
/* we need a local one-time blinding factor */
goto err;
local_blinding = 1;
}
}
if (blinding)
else
{
}
if (blinding)
/* put in leading 0 bytes if the number is less than the
* length of the modulus */
j=BN_num_bytes(&ret);
for (k=0; k<(num-i); k++)
to[k]=0;
r=num;
err:
BN_clear_free(&ret);
BN_clear_free(&f);
if (local_blinding)
{
}
return(r);
}
{
int j,num=0,r= -1;
unsigned char *p;
int local_blinding = 0;
BN_init(&f);
ctx=BN_CTX_new();
{
goto err;
}
/* This check was for equality but PGP does evil things
* and chops off the top '0' bytes */
{
goto err;
}
/* make data into a big number */
{
goto err;
}
/* Now unless blinding is disabled, 'blinding' is non-NULL.
* But the BN_BLINDING object may be owned by some other thread
* (we don't want to keep it constant and we don't want to use
* lots of locking to avoid race conditions, so only a single
* thread can use it; other threads have to use local blinding
* factors) */
{
{
goto err;
}
}
{
{
/* we need a local one-time blinding factor */
goto err;
local_blinding = 1;
}
}
if (blinding)
/* do the decrypt */
else
{
goto err;
}
if (blinding)
p=buf;
switch (padding)
{
case RSA_PKCS1_PADDING:
break;
#ifndef OPENSSL_NO_SHA
case RSA_PKCS1_OAEP_PADDING:
break;
#endif
case RSA_SSLV23_PADDING:
break;
case RSA_NO_PADDING:
break;
default:
goto err;
}
if (r < 0)
err:
BN_clear_free(&f);
BN_clear_free(&ret);
if (local_blinding)
{
}
return(r);
}
/* signature verification */
{
int i,num=0,r= -1;
unsigned char *p;
BN_init(&f);
ctx=BN_CTX_new();
{
goto err;
}
/* This check was for equality but PGP does evil things
* and chops off the top '0' bytes */
{
goto err;
}
{
goto err;
}
/* do the decrypt */
{
goto err;
{
goto err;
}
{
{
bn_mont_ctx = NULL;
}
}
if (bn_mont_ctx)
}
p=buf;
switch (padding)
{
case RSA_PKCS1_PADDING:
break;
case RSA_NO_PADDING:
break;
default:
goto err;
}
if (r < 0)
err:
BN_clear_free(&f);
BN_clear_free(&ret);
{
}
return(r);
}
{
int ret=0;
{
{
goto err;
{
goto err;
}
{
{
bn_mont_ctx = NULL;
}
}
if (bn_mont_ctx)
}
{
goto err;
{
goto err;
}
{
{
bn_mont_ctx = NULL;
}
}
if (bn_mont_ctx)
}
}
/* This will help stop the size of r0 increasing, which does
* affect the multiply if it optimised for a power of 2 size */
/* If p < q it is occasionally possible for the correction of
* adding 'p' if r0 is negative above to leave the result still
* negative. This can break the private key operations: the following
* second correction should *always* correct this rare occurrence.
* This will *never* happen with OpenSSL generated keys because
* they ensure p > q [steve]
*/
{
/* If 'I' was greater than (or equal to) rsa->n, the operation
* will be equivalent to using 'I mod n'. However, the result of
* the verify will *always* be less than 'n' so we don't check
* for absolute equality, just congruency. */
if (!BN_is_zero(&vrfy))
/* 'I' and 'vrfy' aren't congruent mod n. Don't leak
* miscalculated CRT output, just do a raw (slower)
* mod_exp and return that instead. */
}
ret=1;
err:
BN_clear_free(&m1);
BN_clear_free(&r1);
return(ret);
}
{
return(1);
}
{
return(1);
}
#endif