Some more tweaks to ENGINE code.
[openssl.git] / crypto / engine / hw_cswift.c
index 85cf1036c7260747078f2812434fff74ef1deb96..e9f62f61d3a9a37ec603af5e20a68733c8864b77 100644 (file)
 #include <openssl/crypto.h>
 #include "cryptlib.h"
 #include <openssl/dso.h>
-#include "engine_int.h"
 #include <openssl/engine.h>
 
-#ifndef NO_HW
-#ifndef NO_HW_CSWIFT
+#ifndef OPENSSL_NO_HW
+#ifndef OPENSSL_NO_HW_CSWIFT
 
 /* Attribution notice: Rainbow have generously allowed me to reproduce
  * the necessary definitions here from their API. This means the support
@@ -84,8 +83,8 @@
 #include "vendor_defns/cswift.h"
 #endif
 
-static int cswift_init(void);
-static int cswift_finish(void);
+static int cswift_init(ENGINE *);
+static int cswift_finish(ENGINE *);
 
 /* BIGNUM stuff */
 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
@@ -107,7 +106,8 @@ static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
 
 /* DH stuff */
 /* This function is alised to mod_exp (with the DH and mont dropped). */
-static int cswift_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+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);
 
 
@@ -157,33 +157,32 @@ static DH_METHOD cswift_dh =
        NULL
        };
 
-/* Our ENGINE structure. */
-static ENGINE engine_cswift =
-        {
-       "cswift",
-       "CryptoSwift hardware engine support",
-       &cswift_rsa,
-       &cswift_dsa,
-       &cswift_dh,
-       NULL,
-       cswift_mod_exp,
-       cswift_mod_exp_crt,
-       cswift_init,
-       cswift_finish,
-       NULL, /* no ctrl() */
-       NULL, /* no load_privkey() */
-       NULL, /* no load_pubkey() */
-       0, /* no flags */
-       0, 0, /* no references */
-       NULL, NULL /* unlinked */
-        };
+/* Constants used when creating the ENGINE */
+static const char *engine_cswift_id = "cswift";
+static const char *engine_cswift_name = "CryptoSwift hardware engine support";
 
 /* As this is only ever called once, there's no need for locking
  * (indeed - the lock will already be held by our caller!!!) */
 ENGINE *ENGINE_cswift()
        {
-       RSA_METHOD *meth1;
-       DH_METHOD *meth2;
+       const RSA_METHOD *meth1;
+       const DH_METHOD *meth2;
+       ENGINE *ret = ENGINE_new();
+       if(!ret)
+               return NULL;
+       if(!ENGINE_set_id(ret, engine_cswift_id) ||
+                       !ENGINE_set_name(ret, engine_cswift_name) ||
+                       !ENGINE_set_RSA(ret, &cswift_rsa) ||
+                       !ENGINE_set_DSA(ret, &cswift_dsa) ||
+                       !ENGINE_set_DH(ret, &cswift_dh) ||
+                       !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_free(ret);
+               return NULL;
+               }
 
        /* We know that the "PKCS1_SSLeay()" functions hook properly
         * to the cswift-specific mod_exp and mod_exp_crt so we use
@@ -202,7 +201,7 @@ ENGINE *ENGINE_cswift()
        meth2 = DH_OpenSSL();
        cswift_dh.generate_key = meth2->generate_key;
        cswift_dh.compute_key = meth2->compute_key;
-       return &engine_cswift;
+       return ret;
        }
 
 /* This is a process-global DSO handle used for loading and unloading
@@ -250,7 +249,7 @@ static void release_context(SW_CONTEXT_HANDLE hac)
        }
 
 /* (de)initialisation functions. */
-static int cswift_init()
+static int cswift_init(ENGINE *e)
        {
         SW_CONTEXT_HANDLE hac;
         t_swAcquireAccContext *p1;
@@ -307,7 +306,7 @@ err:
        return 0;
        }
 
-static int cswift_finish()
+static int cswift_finish(ENGINE *e)
        {
        if(cswift_dso == NULL)
                {
@@ -357,11 +356,12 @@ static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                }
        acquired = 1;
        /* Prepare the params */
+       BN_CTX_start(ctx);
        modulus = BN_CTX_get(ctx);
        exponent = BN_CTX_get(ctx);
        argument = BN_CTX_get(ctx);
        result = BN_CTX_get(ctx);
-       if(!modulus || !exponent || !argument || !result)
+       if(!result)
                {
                ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_CTX_FULL);
                goto err;
@@ -420,10 +420,7 @@ static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
 err:
        if(acquired)
                release_context(hac);
-       if(modulus) ctx->tos--;
-       if(exponent) ctx->tos--;
-       if(argument) ctx->tos--;
-       if(result) ctx->tos--;
+       BN_CTX_end(ctx);
        return to_return;
        }
 
@@ -453,6 +450,7 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                }
        acquired = 1;
        /* Prepare the params */
+       BN_CTX_start(ctx);
        rsa_p = BN_CTX_get(ctx);
        rsa_q = BN_CTX_get(ctx);
        rsa_dmp1 = BN_CTX_get(ctx);
@@ -460,8 +458,7 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        rsa_iqmp = BN_CTX_get(ctx);
        argument = BN_CTX_get(ctx);
        result = BN_CTX_get(ctx);
-       if(!rsa_p || !rsa_q || !rsa_dmp1 || !rsa_dmq1 || !rsa_iqmp ||
-                       !argument || !result)
+       if(!result)
                {
                ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_CTX_FULL);
                goto err;
@@ -531,13 +528,7 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
 err:
        if(acquired)
                release_context(hac);
-       if(rsa_p) ctx->tos--;
-       if(rsa_q) ctx->tos--;
-       if(rsa_dmp1) ctx->tos--;
-       if(rsa_dmq1) ctx->tos--;
-       if(rsa_iqmp) ctx->tos--;
-       if(argument) ctx->tos--;
-       if(result) ctx->tos--;
+       BN_CTX_end(ctx);
        return to_return;
        }
  
@@ -593,12 +584,13 @@ static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
                }
        acquired = 1;
        /* Prepare the params */
+       BN_CTX_start(ctx);
        dsa_p = BN_CTX_get(ctx);
        dsa_q = BN_CTX_get(ctx);
        dsa_g = BN_CTX_get(ctx);
        dsa_key = BN_CTX_get(ctx);
        result = BN_CTX_get(ctx);
-       if(!dsa_p || !dsa_q || !dsa_g || !dsa_key || !result)
+       if(!result)
                {
                ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_CTX_FULL);
                goto err;
@@ -671,13 +663,11 @@ static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
 err:
        if(acquired)
                release_context(hac);
-       if(dsa_p) ctx->tos--;
-       if(dsa_q) ctx->tos--;
-       if(dsa_g) ctx->tos--;
-       if(dsa_key) ctx->tos--;
-       if(result) ctx->tos--;
        if(ctx)
+               {
+               BN_CTX_end(ctx);
                BN_CTX_free(ctx);
+               }
        return to_return;
        }
 
@@ -707,12 +697,13 @@ static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
                }
        acquired = 1;
        /* Prepare the params */
+       BN_CTX_start(ctx);
        dsa_p = BN_CTX_get(ctx);
        dsa_q = BN_CTX_get(ctx);
        dsa_g = BN_CTX_get(ctx);
        dsa_key = BN_CTX_get(ctx);
        argument = BN_CTX_get(ctx);
-       if(!dsa_p || !dsa_q || !dsa_g || !dsa_key || !argument)
+       if(!argument)
                {
                ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_CTX_FULL);
                goto err;
@@ -785,22 +776,21 @@ static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
 err:
        if(acquired)
                release_context(hac);
-       if(dsa_p) ctx->tos--;
-       if(dsa_q) ctx->tos--;
-       if(dsa_g) ctx->tos--;
-       if(dsa_key) ctx->tos--;
-       if(argument) ctx->tos--;
        if(ctx)
+               {
+               BN_CTX_end(ctx);
                BN_CTX_free(ctx);
+               }
        return to_return;
        }
 
 /* This function is aliased to mod_exp (with the dh and mont dropped). */
-static int cswift_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+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)
        {
        return cswift_mod_exp(r, a, p, m, ctx);
        }
 
-#endif /* !NO_HW_CSWIFT */
-#endif /* !NO_HW */
+#endif /* !OPENSSL_NO_HW_CSWIFT */
+#endif /* !OPENSSL_NO_HW */