The following patch adapts that already in #668 to leave software
fallback off except when explicitly requested, per Geoff Thorpe's
request.

--- openssl-0.9.7b.orig/crypto/engine/hw_ncipher.c      2002-12-12 17:41:34.000000000 
+0000
+++ openssl-0.9.7b/crypto/engine/hw_ncipher.c   2003-09-29 14:15:49.000000000 +0100
@@ -154,6 +154,8 @@
 #define HWCRHK_CMD_THREAD_LOCKING      (ENGINE_CMD_BASE + 2)
 #define HWCRHK_CMD_SET_USER_INTERFACE   (ENGINE_CMD_BASE + 3)
 #define HWCRHK_CMD_SET_CALLBACK_DATA    (ENGINE_CMD_BASE + 4)
+#define HWCRHK_CMD_FALLBACK_RSA         (ENGINE_CMD_BASE + 5)
+#define HWCRHK_CMD_FALLBACK_MODEXP      (ENGINE_CMD_BASE + 6)
 static const ENGINE_CMD_DEFN hwcrhk_cmd_defns[] = {
        {HWCRHK_CMD_SO_PATH,
                "SO_PATH",
@@ -175,6 +177,14 @@
                "SET_CALLBACK_DATA",
                "Set the global user interface extra data (internal)",
                ENGINE_CMD_FLAG_INTERNAL},
+       {HWCRHK_CMD_FALLBACK_RSA,
+               "FALLBACK_RSA",
+               "Fallback to software RSA if hardware fails (boolean)",
+               ENGINE_CMD_FLAG_NUMERIC},
+       {HWCRHK_CMD_FALLBACK_MODEXP,
+               "FALLBACK_MODEXP",
+               "Fallback to software modexp if hardware fails (boolean)",
+               ENGINE_CMD_FLAG_NUMERIC},
        {0, NULL, NULL, 0}
        };
 
@@ -516,6 +526,7 @@
 #endif
        HWCryptoHook_RandomBytes_t *p8;
        HWCryptoHook_ModExpCRT_t *p9;
+       const char *fallback;
 
        if(hwcrhk_dso != NULL)
                {
@@ -595,6 +606,14 @@
                        }
                }
 
+       /* Enable software fallback unless explicitly disabled (may also use
+        * control commands). */
+       fallback = getenv("OPENSSL_NCIPHER_FALLBACK");
+       if (fallback && (0 == strcmp(fallback, "on")))
+               hwcrhk_globals.flags |=
+                       HWCryptoHook_InitFlags_FallbackRSAImmed |
+                       HWCryptoHook_InitFlags_FallbackModExp;
+
        /* Try and get a context - if not, we may have a DSO but no
         * accelerator! */
        if(!get_context(&hwcrhk_context, &password_context))
@@ -728,6 +747,28 @@
                                ~HWCryptoHook_InitFlags_SimpleForkCheck;
                CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
                break;
+       /* These enable or disable initialisation structure flags allowing
+        * fallback to software computation. */
+       case HWCRHK_CMD_FALLBACK_RSA:
+               CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+               if(i)
+                       hwcrhk_globals.flags |=
+                               HWCryptoHook_InitFlags_FallbackRSAImmed;
+               else
+                       hwcrhk_globals.flags &=
+                               ~HWCryptoHook_InitFlags_FallbackRSAImmed;
+               CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+               break;
+       case HWCRHK_CMD_FALLBACK_MODEXP:
+               CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+               if(i)
+                       hwcrhk_globals.flags |=
+                               HWCryptoHook_InitFlags_FallbackModExp;
+               else
+                       hwcrhk_globals.flags &=
+                               ~HWCryptoHook_InitFlags_FallbackModExp;
+               CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+               break;
        /* This will prevent the initialisation function from "installing"
         * the mutex-handling callbacks, even if they are available from
         * within the library (or were provided to the library from the
@@ -938,19 +979,13 @@
        r->top = m_r.size / sizeof(BN_ULONG);
        bn_fix_top(r);
 
+       if ((ret == HWCRYPTOHOOK_ERROR_FALLBACK) &&
+           (hwcrhk_globals.flags & HWCryptoHook_InitFlags_FallbackModExp))
+               return ret;
+
        if (ret < 0)
                {
-               /* FIXME: When this error is returned, HWCryptoHook is
-                  telling us that falling back to software computation
-                  might be a good thing. */
-               if(ret == HWCRYPTOHOOK_ERROR_FALLBACK)
-                       {
-                       HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP,HWCRHK_R_REQUEST_FALLBACK);
-                       }
-               else
-                       {
-                       HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP,HWCRHK_R_REQUEST_FAILED);
-                       }
+               HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP,HWCRHK_R_REQUEST_FAILED);
                ERR_add_error_data(1,rmsg.buf);
                goto err;
                }
@@ -1006,13 +1041,13 @@
 
                if (ret < 0)
                        {
-                       /* FIXME: When this error is returned, HWCryptoHook is
-                          telling us that falling back to software computation
-                          might be a good thing. */
-                       if(ret == HWCRYPTOHOOK_ERROR_FALLBACK)
+                       if ((ret == HWCRYPTOHOOK_ERROR_FALLBACK) &&
+                           (hwcrhk_globals.flags &
+                            HWCryptoHook_InitFlags_FallbackRSAImmed))
                                {
-                               HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
-                                       HWCRHK_R_REQUEST_FALLBACK);
+                               const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
+                               to_return = (*meth->rsa_mod_exp)(r, I, rsa);
+                               goto err;
                                }
                        else
                                {
@@ -1054,13 +1089,13 @@
 
                if (ret < 0)
                        {
-                       /* FIXME: When this error is returned, HWCryptoHook is
-                          telling us that falling back to software computation
-                          might be a good thing. */
-                       if(ret == HWCRYPTOHOOK_ERROR_FALLBACK)
+                       if ((ret == HWCRYPTOHOOK_ERROR_FALLBACK) &&
+                           (hwcrhk_globals.flags &
+                            HWCryptoHook_InitFlags_FallbackModExp))
                                {
-                               HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
-                                       HWCRHK_R_REQUEST_FALLBACK);
+                               const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
+                               to_return = (*meth->rsa_mod_exp)(r, I, rsa);
+                               goto err;
                                }
                        else
                                {
@@ -1082,7 +1117,14 @@
 static int hwcrhk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
        {
-       return hwcrhk_mod_exp(r, a, p, m, ctx);
+       int ret = hwcrhk_mod_exp(r, a, p, m, ctx);
+       if ((ret == HWCRYPTOHOOK_ERROR_FALLBACK) &&
+           (hwcrhk_globals.flags & HWCryptoHook_InitFlags_FallbackModExp))
+               {
+               const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
+               ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
+               }
+       return ret;
        }
 
 #ifndef OPENSSL_NO_DH
@@ -1091,7 +1133,14 @@
                const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
        {
-       return hwcrhk_mod_exp(r, a, p, m, ctx);
+       int ret = hwcrhk_mod_exp(r, a, p, m, ctx);
+       if ((ret == HWCRYPTOHOOK_ERROR_FALLBACK) &&
+           (hwcrhk_globals.flags & HWCryptoHook_InitFlags_FallbackModExp))
+               {
+               const DH_METHOD *meth = DH_OpenSSL();
+               ret = (*meth->bn_mod_exp)(dh, r, a, p, m, ctx, m_ctx);
+               }
+       return ret;
        }
 #endif
 
@@ -1113,21 +1162,18 @@
                }
 
        ret = p_hwcrhk_RandomBytes(hwcrhk_context, buf, num, &rmsg);
+
+       if (ret == HWCRYPTOHOOK_ERROR_FALLBACK)
+               {
+               const RAND_METHOD *meth = RAND_SSLeay();
+               meth->seed(buf, num);
+               return meth->bytes(buf, num);
+               }
+
        if (ret < 0)
                {
-               /* FIXME: When this error is returned, HWCryptoHook is
-                  telling us that falling back to software computation
-                  might be a good thing. */
-               if(ret == HWCRYPTOHOOK_ERROR_FALLBACK)
-                       {
-                       HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES,
-                               HWCRHK_R_REQUEST_FALLBACK);
-                       }
-               else
-                       {
-                       HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES,
-                               HWCRHK_R_REQUEST_FAILED);
-                       }
+               HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES,
+                       HWCRHK_R_REQUEST_FAILED);
                ERR_add_error_data(1,rmsg.buf);
                goto err;
                }

-- 
Colin Watson                                      [EMAIL PROTECTED]
Software Engineer                            nCipher Corporation Limited

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to