more information on 0.9.5
[openssl.git] / rsaref / rsaref.c
index 324460327d46703c2347a62af7c90e4d6bf531d0..ae70feb3e9b0e246d07d0891ef0087cb34c96d61 100644 (file)
  * [including the GNU Public Licence.]
  */
 
+#ifndef NO_RSA
 #include <stdio.h>
 #include "cryptlib.h"
-#include "bn.h"
-#include "rsa.h"
-#include "rsaref.h"
-#include "rand.h"
+#include <openssl/bn.h>
+#include <openssl/rsa.h>
+#include <openssl/rsaref.h>
+#include <openssl/rand.h>
 
-/* 
- * RSAREFerr(RSAREF_F_RSA_REF_BN2BIN,RSAREF_R_CONTENT_ENCODING);
- * RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_DECRYPT,RSAREF_R_DATA);
- * RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT,RSAREF_R_DIGEST_ALGORITHM);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_DECRYPT,RSAREF_R_ENCODING);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_KEY);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_KEY_ENCODING);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_LEN);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_MODULUS_LEN);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_NEED_RANDOM);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_PRIVATE_KEY);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_PUBLIC_KEY);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_SIGNATURE);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_SIGNATURE_ENCODING);
- * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_ENCRYPTION_ALGORITHM);
- * RSAREFerr(RSAREF_F_RSAREF_BN2BIN,ERR_R_BN_LIB);
- */
-
-#ifndef NOPROTO
 static int RSAref_bn2bin(BIGNUM * from, unsigned char* to, int max);
 #ifdef undef
 static BIGNUM* RSAref_bin2bn(unsigned char* from, BIGNUM * to, int max);
@@ -96,26 +78,9 @@ int RSA_ref_public_encrypt(int len, unsigned char *from,
        unsigned char *to, RSA *rsa, int padding);
 int RSA_ref_public_decrypt(int len, unsigned char *from,
        unsigned char *to, RSA *rsa, int padding);
-static int BN_ref_mod_exp(BIGNUM *r,BIGNUM *a,BIGNUM *p,BIGNUM *m, BN_CTX *ctx);
+static int BN_ref_mod_exp(BIGNUM *r,BIGNUM *a,const BIGNUM *p,const BIGNUM *m,
+                         BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 static int RSA_ref_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa);
-#else
-
-static int RSAref_bn2bin();
-#ifdef undef
-static BIGNUM* RSAref_bin2bn();
-#endif
-static int RSAref_Public_eay2ref();
-static int RSAref_Private_eay2ref();
-static int BN_ref_mod_exp();
-static int RSA_ref_mod_exp();
-int RSA_ref_private_decrypt();
-int RSA_ref_private_encrypt();
-int RSA_ref_public_encrypt();
-int RSA_ref_public_decrypt();
-static int BN_ref_mod_exp();
-static int RSA_ref_mod_exp();
-#endif
-
 static RSA_METHOD rsa_pkcs1_ref_meth={
        "RSAref PKCS#1 RSA",
        RSA_ref_public_encrypt,
@@ -130,32 +95,26 @@ static RSA_METHOD rsa_pkcs1_ref_meth={
        NULL,
        };
 
-RSA_METHOD *RSA_PKCS1_RSAref()
+RSA_METHOD *RSA_PKCS1_RSAref(void)
        {
        return(&rsa_pkcs1_ref_meth);
        }
 
-static int RSA_ref_mod_exp(r0, I, rsa)
-BIGNUM *r0;
-BIGNUM *I;
-RSA *rsa;
+static int RSA_ref_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
        {
        RSAREFerr(RSAREF_F_RSA_REF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
        return(0);
        }
 
-static int BN_ref_mod_exp(r,a,p,m,ctx)
-BIGNUM *r,*a,*p,*m;
-BN_CTX *ctx;
+static int BN_ref_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
        {
        RSAREFerr(RSAREF_F_BN_REF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
        return(0);
        }
 
-static int RSAref_bn2bin(from,to,max)
-BIGNUM *from;
-unsigned char *to; /* [max] */
-int max;
+/* unsigned char *to:  [max]    */
+static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
        {
        int i;
 
@@ -173,10 +132,8 @@ int max;
        }
 
 #ifdef undef
-static BIGNUM *RSAref_bin2bn(from,to,max)
-unsigned char *from; /* [max] */
-BIGNUM *to;
-int max;
+/* unsigned char *from:  [max]    */
+static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
        {
        int i;
        BIGNUM *ret;
@@ -188,9 +145,7 @@ int max;
        return(ret);
        }
 
-static int RSAref_Public_ref2eay(from,to)
-RSArefPublicKey *from;
-RSA *to;
+static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
        {
        to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
        to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
@@ -199,9 +154,7 @@ RSA *to;
        }
 #endif
 
-static int RSAref_Public_eay2ref(from,to)
-RSA *from;
-RSArefPublicKey *to;
+static int RSAref_Public_eay2ref(RSA *from, RSArefPublicKey *to)
        {
        to->bits=BN_num_bits(from->n);
        if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
@@ -210,9 +163,7 @@ RSArefPublicKey *to;
        }
 
 #ifdef undef
-static int RSAref_Private_ref2eay(from,to)
-RSArefPrivateKey *from;
-RSA *to;
+static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
        {
        if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
                return(0);
@@ -236,9 +187,7 @@ RSA *to;
        }
 #endif
 
-static int RSAref_Private_eay2ref(from,to)
-RSA *from;
-RSArefPrivateKey *to;
+static int RSAref_Private_eay2ref(RSA *from, RSArefPrivateKey *to)
        {
        to->bits=BN_num_bits(from->n);
        if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
@@ -252,11 +201,8 @@ RSArefPrivateKey *to;
        return(1);
        }
 
-int RSA_ref_private_decrypt(len,from,to,rsa,padding)
-int len;
-unsigned char *from,*to;
-RSA *rsa;
-int padding;
+int RSA_ref_private_decrypt(int len, unsigned char *from, unsigned char *to,
+            RSA *rsa, int padding)
        {
        int i,outlen= -1;
        RSArefPrivateKey RSAkey;
@@ -273,15 +219,17 @@ err:
        return(outlen);
        }
 
-int RSA_ref_private_encrypt(len,from,to,rsa,padding)
-int len;
-unsigned char *from,*to;
-RSA *rsa;
-int padding;
+int RSA_ref_private_encrypt(int len, unsigned char *from, unsigned char *to,
+            RSA *rsa, int padding)
        {
        int i,outlen= -1;
        RSArefPrivateKey RSAkey;
 
+       if (padding != RSA_PKCS1_PADDING)
+               {
+               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
+               goto err;
+       }
        if (!RSAref_Private_eay2ref(rsa,&RSAkey))
                goto err;
        if ((i=RSAPrivateEncrypt(to,&outlen,from,len,&RSAkey)) != 0)
@@ -294,11 +242,8 @@ err:
        return(outlen);
        }
 
-int RSA_ref_public_decrypt(len,from,to,rsa,padding)
-int len;
-unsigned char *from,*to;
-RSA *rsa;
-int padding;
+int RSA_ref_public_decrypt(int len, unsigned char *from, unsigned char *to,
+            RSA *rsa, int padding)
        {
        int i,outlen= -1;
        RSArefPublicKey RSAkey;
@@ -315,11 +260,8 @@ err:
        return(outlen);
        }
 
-int RSA_ref_public_encrypt(len,from,to,rsa,padding)
-int len;
-unsigned char *from,*to;
-RSA *rsa;
-int padding;
+int RSA_ref_public_encrypt(int len, unsigned char *from, unsigned char *to,
+            RSA *rsa, int padding)
        {
        int outlen= -1;
        int i;
@@ -327,11 +269,18 @@ int padding;
        RSARandomState rnd;
        unsigned char buf[16];
 
+       if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) 
+               {
+               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
+               goto err;
+               }
+       
        R_RandomInit(&rnd);
        R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
        while (i > 0)
                {
-               RAND_bytes(buf,16);
+               if (RAND_bytes(buf,16) <= 0)
+                       goto err;
                R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
                i-=16;
                }
@@ -350,4 +299,10 @@ err:
        memset(&rnd,0,sizeof(rnd));
        return(outlen);
        }
+#else /* !NO_RSA */
+
+# if PEDANTIC
+static void *dummy=&dummy;
+# endif
 
+#endif