Add missing variable length cipher flag for Blowfish.
[oweals/openssl.git] / crypto / engine / hw_cswift.c
index e9f62f61d3a9a37ec603af5e20a68733c8864b77..5471dbe50112b0c09d43fee4b7536a56da49d366 100644 (file)
@@ -83,8 +83,9 @@
 #include "vendor_defns/cswift.h"
 #endif
 
-static int cswift_init(ENGINE *);
-static int cswift_finish(ENGINE *);
+static int cswift_init(ENGINE *e);
+static int cswift_finish(ENGINE *e);
+static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
 
 /* BIGNUM stuff */
 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
@@ -93,24 +94,40 @@ static int cswift_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);
 
+#ifndef OPENSSL_NO_RSA
 /* RSA stuff */
 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
+#endif
 /* This function is aliased to mod_exp (with the mont stuff dropped). */
 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 
+#ifndef OPENSSL_NO_DSA
 /* DSA stuff */
 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
                                DSA_SIG *sig, DSA *dsa);
+#endif
 
+#ifndef OPENSSL_NO_DH
 /* DH stuff */
 /* This function is alised to mod_exp (with the DH and mont dropped). */
 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
                const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
+#endif
 
+/* The definitions for control commands specific to this engine */
+#define CSWIFT_CMD_SO_PATH             ENGINE_CMD_BASE
+static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
+       {CSWIFT_CMD_SO_PATH,
+               "SO_PATH",
+               "Specifies the path to the 'cswift' shared library",
+               ENGINE_CMD_FLAG_STRING},
+       {0, NULL, NULL, 0}
+       };
 
+#ifndef OPENSSL_NO_RSA
 /* Our internal RSA_METHOD that we provide pointers to */
 static RSA_METHOD cswift_rsa =
        {
@@ -128,7 +145,9 @@ static RSA_METHOD cswift_rsa =
        NULL,
        NULL
        };
+#endif
 
+#ifndef OPENSSL_NO_DSA
 /* Our internal DSA_METHOD that we provide pointers to */
 static DSA_METHOD cswift_dsa =
        {
@@ -143,7 +162,9 @@ static DSA_METHOD cswift_dsa =
        0, /* flags */
        NULL /* app_data */
        };
+#endif
 
+#ifndef OPENSSL_NO_DH
 /* Our internal DH_METHOD that we provide pointers to */
 static DH_METHOD cswift_dh =
        {
@@ -156,6 +177,7 @@ static DH_METHOD cswift_dh =
        0,
        NULL
        };
+#endif
 
 /* Constants used when creating the ENGINE */
 static const char *engine_cswift_id = "cswift";
@@ -165,25 +187,38 @@ static const char *engine_cswift_name = "CryptoSwift hardware engine support";
  * (indeed - the lock will already be held by our caller!!!) */
 ENGINE *ENGINE_cswift()
        {
+#ifndef OPENSSL_NO_RSA
        const RSA_METHOD *meth1;
+#endif
+#ifndef OPENSSL_NO_DH
        const DH_METHOD *meth2;
+#endif
        ENGINE *ret = ENGINE_new();
        if(!ret)
                return NULL;
        if(!ENGINE_set_id(ret, engine_cswift_id) ||
                        !ENGINE_set_name(ret, engine_cswift_name) ||
+#ifndef OPENSSL_NO_RSA
                        !ENGINE_set_RSA(ret, &cswift_rsa) ||
+#endif
+#ifndef OPENSSL_NO_DSA
                        !ENGINE_set_DSA(ret, &cswift_dsa) ||
+#endif
+#ifndef OPENSSL_NO_DH
                        !ENGINE_set_DH(ret, &cswift_dh) ||
+#endif
                        !ENGINE_set_BN_mod_exp(ret, &cswift_mod_exp) ||
                        !ENGINE_set_BN_mod_exp_crt(ret, &cswift_mod_exp_crt) ||
                        !ENGINE_set_init_function(ret, cswift_init) ||
-                       !ENGINE_set_finish_function(ret, cswift_finish))
+                       !ENGINE_set_finish_function(ret, cswift_finish) ||
+                       !ENGINE_set_ctrl_function(ret, cswift_ctrl) ||
+                       !ENGINE_set_cmd_defns(ret, cswift_cmd_defns))
                {
                ENGINE_free(ret);
                return NULL;
                }
 
+#ifndef OPENSSL_NO_RSA
        /* We know that the "PKCS1_SSLeay()" functions hook properly
         * to the cswift-specific mod_exp and mod_exp_crt so we use
         * those functions. NB: We don't use ENGINE_openssl() or
@@ -196,11 +231,14 @@ ENGINE *ENGINE_cswift()
        cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
        cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
        cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
+#endif
 
+#ifndef OPENSSL_NO_DH
        /* Much the same for Diffie-Hellman */
        meth2 = DH_OpenSSL();
        cswift_dh.generate_key = meth2->generate_key;
        cswift_dh.compute_key = meth2->compute_key;
+#endif
        return ret;
        }
 
@@ -219,7 +257,8 @@ t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
 
 /* Used in the DSO operations. */
-static const char *CSWIFT_LIBNAME = "swift";
+static const char def_CSWIFT_LIBNAME[] = "swift";
+static const char *CSWIFT_LIBNAME = def_CSWIFT_LIBNAME;
 static const char *CSWIFT_F1 = "swAcquireAccContext";
 static const char *CSWIFT_F2 = "swAttachKeyParam";
 static const char *CSWIFT_F3 = "swSimpleRequest";
@@ -326,6 +365,33 @@ static int cswift_finish(ENGINE *e)
        return 1;
        }
 
+static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
+       {
+       int initialised = ((cswift_dso == NULL) ? 0 : 1);
+       switch(cmd)
+               {
+       case CSWIFT_CMD_SO_PATH:
+               if(p == NULL)
+                       {
+                       ENGINEerr(ENGINE_F_CSWIFT_CTRL,
+                               ERR_R_PASSED_NULL_PARAMETER);
+                       return 0;
+                       }
+               if(initialised)
+                       {
+                       ENGINEerr(ENGINE_F_CSWIFT_CTRL,
+                               ENGINE_R_ALREADY_LOADED);
+                       return 0;
+                       }
+               CSWIFT_LIBNAME = (const char *)p;
+               return 1;
+       default:
+               break;
+               }
+       ENGINEerr(ENGINE_F_CSWIFT_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
+       return 0;
+       }
+
 /* Un petit mod_exp */
 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                        const BIGNUM *m, BN_CTX *ctx)
@@ -532,6 +598,7 @@ err:
        return to_return;
        }
  
+#ifndef OPENSSL_NO_RSA
 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
        {
        BN_CTX *ctx;
@@ -551,6 +618,7 @@ err:
                BN_CTX_free(ctx);
        return to_return;
        }
+#endif
 
 /* This function is aliased to mod_exp (with the mont stuff dropped). */
 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
@@ -559,6 +627,7 @@ static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        return cswift_mod_exp(r, a, p, m, ctx);
        }
 
+#ifndef OPENSSL_NO_DSA
 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
        {
        SW_CONTEXT_HANDLE hac;
@@ -783,7 +852,9 @@ err:
                }
        return to_return;
        }
+#endif
 
+#ifndef OPENSSL_NO_DH
 /* This function is aliased to mod_exp (with the dh and mont dropped). */
 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
                const BIGNUM *a, const BIGNUM *p,
@@ -791,6 +862,7 @@ static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
        {
        return cswift_mod_exp(r, a, p, m, ctx);
        }
+#endif
 
 #endif /* !OPENSSL_NO_HW_CSWIFT */
 #endif /* !OPENSSL_NO_HW */