typo
[openssl.git] / engines / e_aep.c
index 8b8380a582b4d1432a76862a70ef6aff952652ce..a9372268cd4a21b78504e655390d6d579953fb82 100644 (file)
@@ -1,6 +1,3 @@
-/* crypto/engine/hw_aep.c */
-/*
- */
 /* ====================================================================
  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
  *
@@ -60,7 +57,7 @@
 #include <string.h>
 
 #include <openssl/e_os2.h>
-#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
+#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__) || defined(__MINGW32__)
 #include <sys/types.h>
 #include <unistd.h>
 #else
 typedef int pid_t;
 #endif
 
+#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
+#define getpid GetThreadID
+extern int GetThreadID(void);
+#elif defined(_WIN32) && !defined(__WATCOMC__)
+#define getpid _getpid
+#endif
+
 #include <openssl/crypto.h>
 #include <openssl/dso.h>
 #include <openssl/engine.h>
 #include <openssl/buffer.h>
+#ifndef OPENSSL_NO_RSA
+#include <openssl/rsa.h>
+#endif
+#ifndef OPENSSL_NO_DSA
+#include <openssl/dsa.h>
+#endif
+#ifndef OPENSSL_NO_DH
+#include <openssl/dh.h>
+#endif
 
 #ifndef OPENSSL_NO_HW
 #ifndef OPENSSL_NO_HW_AEP
@@ -84,11 +97,11 @@ typedef int pid_t;
 #define AEP_LIB_NAME "aep engine"
 #define FAIL_TO_SW 0x10101010
 
-#include "hw_aep_err.c"
+#include "e_aep_err.c"
 
 static int aep_init(ENGINE *e);
 static int aep_finish(ENGINE *e);
-static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
+static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
 static int aep_destroy(ENGINE *e);
 
 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
@@ -97,21 +110,25 @@ static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
 
 /* BIGNUM stuff */
+#ifndef OPENSSL_NO_RSA
 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        const BIGNUM *m, BN_CTX *ctx);
 
 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const  BIGNUM *a, const BIGNUM *p,
        const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
        const BIGNUM *iqmp, BN_CTX *ctx);
+#endif
 
 /* RSA stuff */
 #ifndef OPENSSL_NO_RSA
-static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
+static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
 #endif
 
 /* This function is aliased to mod_exp (with the mont stuff dropped). */
+#ifndef OPENSSL_NO_RSA
 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
+#endif
 
 /* DSA stuff */
 #ifndef OPENSSL_NO_DSA
@@ -172,7 +189,8 @@ static RSA_METHOD aep_rsa =
        0,                   /*flags*/
        NULL,                /*app_data*/
        NULL,                /*rsa_sign*/
-       NULL                 /*rsa_verify*/
+       NULL,                /*rsa_verify*/
+       NULL                 /*rsa_keygen*/
        };
 #endif
 
@@ -189,7 +207,9 @@ static DSA_METHOD aep_dsa =
        NULL,                /* init */
        NULL,                /* finish */
        0,                   /* flags */
-       NULL                 /* app_data */
+       NULL,                /* app_data */
+       NULL,                /* dsa_paramgen */
+       NULL                 /* dsa_keygen */
        };
 #endif
 
@@ -204,6 +224,7 @@ static DH_METHOD aep_dh =
        NULL,
        NULL,
        0,
+       NULL,
        NULL
        };
 #endif
@@ -318,7 +339,7 @@ static int bind_aep(ENGINE *e)
        return 1;
 }
 
-#ifdef ENGINE_DYNAMIC_SUPPORT
+#ifndef OPENSSL_NO_DYNAMIC_ENGINE
 static int bind_helper(ENGINE *e, const char *id)
        {
        if(id && (strcmp(id, engine_aep_id) != 0))
@@ -474,6 +495,7 @@ static int aep_init(ENGINE *e)
 
        if(aep_dso)
                DSO_free(aep_dso);
+       aep_dso = NULL;
                
        p_AEP_OpenConnection    = NULL;
        p_AEP_ModExp            = NULL;
@@ -550,7 +572,7 @@ static int aep_finish(ENGINE *e)
        return to_return;
        }
 
-static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
+static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
        {
        int initialised = ((aep_dso == NULL) ? 0 : 1);
        switch(cmd)
@@ -615,7 +637,7 @@ static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        rv = aep_return_connection(hConnection);
        if (rv != AEP_R_OK)
                {
-               AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED); 
+               AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_RETURN_CONNECTION_FAILED); 
                goto err;
                }
 
@@ -624,6 +646,7 @@ static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        return to_return;
        }
        
+#ifndef OPENSSL_NO_RSA
 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        const BIGNUM *q, const BIGNUM *dmp1,
        const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
@@ -653,13 +676,14 @@ static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        rv = aep_return_connection(hConnection);
        if (rv != AEP_R_OK)
                {
-               AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED); 
+               AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_RETURN_CONNECTION_FAILED); 
                goto err;
                }
  
  err:
        return rv;
        }
+#endif
        
 
 #ifdef AEPRAND
@@ -741,15 +765,11 @@ static int aep_rand_status(void)
 #endif
 
 #ifndef OPENSSL_NO_RSA
-static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
+static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
        {
-       BN_CTX *ctx = NULL;
        int to_return = 0;
        AEP_RV rv = AEP_R_OK;
 
-       if ((ctx = BN_CTX_new()) == NULL)
-               goto err;
-
        if (!aep_dso)
                {
                AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
@@ -763,7 +783,7 @@ static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
 
                if (rv == FAIL_TO_SW){
                        const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
-                       to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
+                       to_return = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
                        goto err;
                }
                else if (rv != AEP_R_OK)
@@ -786,8 +806,6 @@ static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
        to_return = 1;
 
  err:
-       if(ctx)
-               BN_CTX_free(ctx);
        return to_return;
 }
 #endif
@@ -821,12 +839,14 @@ static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
        }
 #endif
 
+#ifndef OPENSSL_NO_RSA
 /* This function is aliased to mod_exp (with the mont stuff dropped). */
 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
        {
        return aep_mod_exp(r, a, p, m, ctx);
        }
+#endif
 
 #ifndef OPENSSL_NO_DH
 /* This function is aliased to mod_exp (with the dh and mont dropped). */
@@ -1032,13 +1052,10 @@ static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
        /*Cast the ArbBigNum pointer to our BIGNUM struct*/
        bn = (BIGNUM*) ArbBigNum;
 
-#ifdef SIXTY_FOUR_BIT_LONG
-       *BigNumSize = bn->top << 3;
-#else
-       /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
-         words) multiplies by 4*/
-       *BigNumSize = bn->top << 2;
-#endif
+       *BigNumSize = bn->top*BN_BYTES;
+
+       if (BN_BYTES>sizeof(AEP_U32) && (bn->d[bn->top-1]>>BN_BITS4)==0)
+               *BigNumSize -= 4;
 
        return AEP_R_OK;
        }
@@ -1047,31 +1064,42 @@ static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
        unsigned char* AEP_BigNum)
        {
        BIGNUM* bn;
-
-#ifndef SIXTY_FOUR_BIT_LONG
-       unsigned char* buf;
-       int i;
-#endif
+       const union { long one; char little; } is_endian = {1};
+       AEP_U32 i,j;
 
        /*Cast the ArbBigNum pointer to our BIGNUM struct*/
        bn = (BIGNUM*) ArbBigNum;
 
-#ifdef SIXTY_FOUR_BIT_LONG
-       memcpy(AEP_BigNum, bn->d, BigNumSize);
-#else
        /*Must copy data into a (monotone) least significant byte first format
          performing endian conversion if necessary*/
-       for(i=0;i<bn->top;i++)
+       if (is_endian.little && sizeof(bn->d[0])==BN_BYTES)
+               memcpy(AEP_BigNum, bn->d, BigNumSize);
+       else
                {
-               buf = (unsigned char*)&bn->d[i];
+               BN_ULONG di;
 
-               *((AEP_U32*)AEP_BigNum) = (AEP_U32)
-                       ((unsigned) buf[1] << 8 | buf[0]) |
-                       ((unsigned) buf[3] << 8 | buf[2])  << 16;
+               for (i=0; BigNumSize>=BN_BYTES; i++)
+                       {
+                       di = bn->d[i];
+                       for (j=0; j<BN_BYTES; j++)
+                               {
+                               AEP_BigNum[j] = (unsigned char)di;
+                               di>>=8;
+                               }
+                       AEP_BigNum += BN_BYTES;
+                       BigNumSize -= BN_BYTES;
+                       }
 
-               AEP_BigNum += 4;
+               if (BigNumSize)
+                       {
+                       di = bn->d[i];
+                       for (j=0; j<BigNumSize; j++)
+                               {
+                               AEP_BigNum[j] = (unsigned char)di;
+                               di>>=8;
+                               }
+                       }
                }
-#endif
 
        return AEP_R_OK;
        }
@@ -1081,36 +1109,46 @@ static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
        unsigned char* AEP_BigNum)
        {
        BIGNUM* bn;
-#ifndef SIXTY_FOUR_BIT_LONG
-       int i;
-#endif
+       const union { long one; char little; } is_endian = {1};
+       int i,j,top;
 
        bn = (BIGNUM*)ArbBigNum;
 
        /*Expand the result bn so that it can hold our big num.
          Size is in bits*/
-       bn_expand(bn, (int)(BigNumSize << 3));
+       top = (BigNumSize+BN_BYTES-1)/BN_BYTES;
+       bn_expand(bn, top);
+       bn->top = top;
+       bn->d[top-1] = 0;
 
-#ifdef SIXTY_FOUR_BIT_LONG
-       bn->top = BigNumSize >> 3;
-       
-       if((BigNumSize & 7) != 0)
-               bn->top++;
+       if (is_endian.little && sizeof(bn->d[0])==BN_BYTES)
+               memcpy(bn->d, AEP_BigNum, BigNumSize);
+       else
+               {
+               BN_ULONG di;
 
-       memset(bn->d, 0, bn->top << 3); 
+               for (i=0; BigNumSize>=BN_BYTES; i++)
+                       {
+                       for (di=0,j=BN_BYTES; j!=0; )
+                               {
+                               di <<= 8;
+                               di |= AEP_BigNum[--j];
+                               }
+                       bn->d[i] = di;
+                       AEP_BigNum += BN_BYTES;
+                       BigNumSize -= BN_BYTES;
+                       }
 
-       memcpy(bn->d, AEP_BigNum, BigNumSize);
-#else
-       bn->top = BigNumSize >> 2;
-       for(i=0;i<bn->top;i++)
-               {
-               bn->d[i] = (AEP_U32)
-                       ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
-                       ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
-               AEP_BigNum += 4;
+               if (BigNumSize)
+                       {
+                       for (di=0,j=BigNumSize; j!=0; )
+                               {
+                               di <<= 8;
+                               di |= AEP_BigNum[--j];
+                               }
+                       bn->d[i] = di;
+                       }
                }
-#endif
 
        return AEP_R_OK;
 }