Change #include filenames from <foo.h> to <openssl.h>.
[openssl.git] / crypto / rsa / rsa_eay.c
index b4050506c3680823711030c1b22664907740b9df..bdb533c9dba3c2c9e3db5b6fdc7e78b191cb2db4 100644 (file)
@@ -58,9 +58,9 @@
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "bn.h"
-#include "rsa.h"
-#include "rand.h"
+#include <openssl/bn.h>
+#include <openssl/rsa.h>
+#include <openssl/rand.h>
 
 #ifndef NOPROTO
 static int RSA_eay_public_encrypt(int flen, unsigned char *from,
@@ -98,17 +98,13 @@ static RSA_METHOD rsa_pkcs1_eay_meth={
        NULL,
        };
 
-RSA_METHOD *RSA_PKCS1_SSLeay()
+RSA_METHOD *RSA_PKCS1_SSLeay(void)
        {
        return(&rsa_pkcs1_eay_meth);
        }
 
-static int RSA_eay_public_encrypt(flen, from, to, rsa, padding)
-int flen;
-unsigned char *from;
-unsigned char *to;
-RSA *rsa;
-int padding;
+static int RSA_eay_public_encrypt(int flen, unsigned char *from,
+            unsigned char *to, RSA *rsa, int padding)
        {
        BIGNUM f,ret;
        int i,j,k,num=0,r= -1;
@@ -130,6 +126,9 @@ int padding;
        case RSA_PKCS1_PADDING:
                i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
                break;
+       case RSA_PKCS1_OAEP_PADDING:
+               i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
+               break;
        case RSA_SSLV23_PADDING:
                i=RSA_padding_add_SSLv23(buf,num,from,flen);
                break;
@@ -174,12 +173,8 @@ err:
        return(r);
        }
 
-static int RSA_eay_private_encrypt(flen, from, to, rsa, padding)
-int flen;
-unsigned char *from;
-unsigned char *to;
-RSA *rsa;
-int padding;
+static int RSA_eay_private_encrypt(int flen, unsigned char *from,
+            unsigned char *to, RSA *rsa, int padding)
        {
        BIGNUM f,ret;
        int i,j,k,num=0,r= -1;
@@ -253,12 +248,8 @@ err:
        return(r);
        }
 
-static int RSA_eay_private_decrypt(flen, from, to, rsa,padding)
-int flen;
-unsigned char *from;
-unsigned char *to;
-RSA *rsa;
-int padding;
+static int RSA_eay_private_decrypt(int flen, unsigned char *from,
+            unsigned char *to, RSA *rsa, int padding)
        {
        BIGNUM f,ret;
        int j,num=0,r= -1;
@@ -319,7 +310,10 @@ int padding;
        case RSA_PKCS1_PADDING:
                r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
                break;
-       case RSA_SSLV23_PADDING:
+        case RSA_PKCS1_OAEP_PADDING:
+               r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
+                break;
+       case RSA_SSLV23_PADDING:
                r=RSA_padding_check_SSLv23(to,num,buf,j,num);
                break;
        case RSA_NO_PADDING:
@@ -344,12 +338,8 @@ err:
        return(r);
        }
 
-static int RSA_eay_public_decrypt(flen, from, to, rsa, padding)
-int flen;
-unsigned char *from;
-unsigned char *to;
-RSA *rsa;
-int padding;
+static int RSA_eay_public_decrypt(int flen, unsigned char *from,
+            unsigned char *to, RSA *rsa, int padding)
        {
        BIGNUM f,ret;
        int i,num=0,r= -1;
@@ -420,10 +410,7 @@ err:
        return(r);
        }
 
-static int RSA_eay_mod_exp(r0, I, rsa)
-BIGNUM *r0;
-BIGNUM *I;
-RSA *rsa;
+static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
        {
        BIGNUM r1,m1;
        int ret=0;
@@ -467,6 +454,15 @@ RSA *rsa;
 
        if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
        if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
+       /* 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 (r0->neg)
+               if (!BN_add(r0,r0,rsa->p)) goto err;
        if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
        if (!BN_add(r0,&r1,&m1)) goto err;
 
@@ -478,15 +474,13 @@ err:
        return(ret);
        }
 
-static int RSA_eay_init(rsa)
-RSA *rsa;
+static int RSA_eay_init(RSA *rsa)
        {
        rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
        return(1);
        }
 
-static int RSA_eay_finish(rsa)
-RSA *rsa;
+static int RSA_eay_finish(RSA *rsa)
        {
        if (rsa->_method_mod_n != NULL)
                BN_MONT_CTX_free(rsa->_method_mod_n);