Hide BN_CTX structure details.
[openssl.git] / crypto / engine / hw_cswift.c
index 5747973c749678db1b31ca94d8c198eda45e77c7..2ee9040ca06372cb5e6397901e833b384f9c2bdf 100644 (file)
@@ -63,8 +63,8 @@
 #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
@@ -88,16 +88,16 @@ static int cswift_init(void);
 static int cswift_finish(void);
 
 /* BIGNUM stuff */
-static int cswift_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx);
-static int cswift_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+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);
 
 /* RSA stuff */
-static int cswift_rsa_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa);
+static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
 /* This function is aliased to mod_exp (with the mont stuff dropped). */
-static int cswift_mod_exp_mont(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+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);
 
 /* DSA stuff */
@@ -107,7 +107,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);
 
 
@@ -182,8 +183,8 @@ static ENGINE engine_cswift =
  * (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;
 
        /* We know that the "PKCS1_SSLeay()" functions hook properly
         * to the cswift-specific mod_exp and mod_exp_crt so we use
@@ -250,7 +251,7 @@ static void release_context(SW_CONTEXT_HANDLE hac)
        }
 
 /* (de)initialisation functions. */
-static int cswift_init()
+static int cswift_init(void)
        {
         SW_CONTEXT_HANDLE hac;
         t_swAcquireAccContext *p1;
@@ -307,7 +308,7 @@ err:
        return 0;
        }
 
-static int cswift_finish()
+static int cswift_finish(void)
        {
        if(cswift_dso == NULL)
                {
@@ -328,7 +329,7 @@ static int cswift_finish()
        }
 
 /* Un petit mod_exp */
-static int cswift_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                        const BIGNUM *m, BN_CTX *ctx)
        {
        /* I need somewhere to store temporary serialised values for
@@ -357,11 +358,12 @@ static int cswift_mod_exp(BIGNUM *r, 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,15 +422,12 @@ static int cswift_mod_exp(BIGNUM *r, 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;
        }
 
 /* Un petit mod_exp chinois */
-static int cswift_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+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)
        {
@@ -453,6 +452,7 @@ static int cswift_mod_exp_crt(BIGNUM *r, 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 +460,7 @@ static int cswift_mod_exp_crt(BIGNUM *r, 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,17 +530,11 @@ static int cswift_mod_exp_crt(BIGNUM *r, 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;
        }
  
-static int cswift_rsa_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
+static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
        {
        BN_CTX *ctx;
        int to_return = 0;
@@ -562,7 +555,7 @@ err:
        }
 
 /* This function is aliased to mod_exp (with the mont stuff dropped). */
-static int cswift_mod_exp_mont(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+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)
        {
        return cswift_mod_exp(r, a, p, m, ctx);
@@ -593,12 +586,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 +665,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 +699,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 +778,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 */