Add functions returning security bits.
[openssl.git] / crypto / dh / dh_lib.c
index 48b9db2ac06996e3d64b0db96ff80b3eef5d59e3..83b3dc50c15a0a288ffcf2e5fa1a18d7dc3f4bb3 100644 (file)
 #include "cryptlib.h"
 #include <openssl/bn.h>
 #include <openssl/dh.h>
+#ifndef OPENSSL_NO_ENGINE
+#include <openssl/engine.h>
+#endif
 
-const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
+const char DH_version[]="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
 
-static DH_METHOD *default_DH_method;
-static int dh_meth_num = 0;
-static STACK *dh_meth = NULL;
+static const DH_METHOD *default_DH_method = NULL;
 
-void DH_set_default_method(DH_METHOD *meth)
-{
+void DH_set_default_method(const DH_METHOD *meth)
+       {
        default_DH_method = meth;
-}
+       }
 
-DH_METHOD *DH_get_default_method(void)
-{
-       if(!default_DH_method) default_DH_method = DH_OpenSSL();
+const DH_METHOD *DH_get_default_method(void)
+       {
+       if(!default_DH_method)
+               default_DH_method = DH_OpenSSL();
        return default_DH_method;
-}
+       }
 
-DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth)
-{
-        DH_METHOD *mtmp;
+int DH_set_method(DH *dh, const DH_METHOD *meth)
+       {
+       /* NB: The caller is specifically setting a method, so it's not up to us
+        * to deal with which ENGINE it comes from. */
+        const DH_METHOD *mtmp;
         mtmp = dh->meth;
         if (mtmp->finish) mtmp->finish(dh);
+#ifndef OPENSSL_NO_ENGINE
+       if (dh->engine)
+               {
+               ENGINE_finish(dh->engine);
+               dh->engine = NULL;
+               }
+#endif
         dh->meth = meth;
         if (meth->init) meth->init(dh);
-        return mtmp;
-}
+        return 1;
+       }
 
 DH *DH_new(void)
-{
+       {
        return DH_new_method(NULL);
-}
+       }
 
-DH *DH_new_method(DH_METHOD *meth)
+DH *DH_new_method(ENGINE *engine)
        {
        DH *ret;
-       ret=(DH *)Malloc(sizeof(DH));
 
+       ret=(DH *)OPENSSL_malloc(sizeof(DH));
        if (ret == NULL)
                {
-               DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
+               DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
-       if(!default_DH_method) default_DH_method = DH_OpenSSL();
-       if(meth) ret->meth = meth;
-       else ret->meth = default_DH_method;
+
+       ret->meth = DH_get_default_method();
+#ifndef OPENSSL_NO_ENGINE
+       if (engine)
+               {
+               if (!ENGINE_init(engine))
+                       {
+                       DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
+                       OPENSSL_free(ret);
+                       return NULL;
+                       }
+               ret->engine = engine;
+               }
+       else
+               ret->engine = ENGINE_get_default_DH();
+       if(ret->engine)
+               {
+               ret->meth = ENGINE_get_DH(ret->engine);
+               if(!ret->meth)
+                       {
+                       DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB);
+                       ENGINE_finish(ret->engine);
+                       OPENSSL_free(ret);
+                       return NULL;
+                       }
+               }
+#endif
+
        ret->pad=0;
        ret->version=0;
        ret->p=NULL;
@@ -113,16 +149,25 @@ DH *DH_new_method(DH_METHOD *meth)
        ret->length=0;
        ret->pub_key=NULL;
        ret->priv_key=NULL;
+       ret->q=NULL;
+       ret->j=NULL;
+       ret->seed = NULL;
+       ret->seedlen = 0;
+       ret->counter = NULL;
        ret->method_mont_p=NULL;
        ret->references = 1;
        ret->flags=ret->meth->flags;
+       CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
        if ((ret->meth->init != NULL) && !ret->meth->init(ret))
                {
-               Free(ret);
+#ifndef OPENSSL_NO_ENGINE
+               if (ret->engine)
+                       ENGINE_finish(ret->engine);
+#endif
+               CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
+               OPENSSL_free(ret);
                ret=NULL;
                }
-       else
-               CRYPTO_new_ex_data(dh_meth,(char *)ret,&ret->ex_data);
        return(ret);
        }
 
@@ -143,36 +188,72 @@ void DH_free(DH *r)
        }
 #endif
 
-       CRYPTO_free_ex_data(dh_meth, (char *)r, &r->ex_data);
+       if (r->meth->finish)
+               r->meth->finish(r);
+#ifndef OPENSSL_NO_ENGINE
+       if (r->engine)
+               ENGINE_finish(r->engine);
+#endif
 
-       if(r->meth->finish) r->meth->finish(r);
+       CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
 
        if (r->p != NULL) BN_clear_free(r->p);
        if (r->g != NULL) BN_clear_free(r->g);
+       if (r->q != NULL) BN_clear_free(r->q);
+       if (r->j != NULL) BN_clear_free(r->j);
+       if (r->seed) OPENSSL_free(r->seed);
+       if (r->counter != NULL) BN_clear_free(r->counter);
        if (r->pub_key != NULL) BN_clear_free(r->pub_key);
        if (r->priv_key != NULL) BN_clear_free(r->priv_key);
-       Free(r);
+       OPENSSL_free(r);
        }
 
-int DH_get_ex_new_index(long argl, char *argp, int (*new_func)(),
-            int (*dup_func)(), void (*free_func)())
+int DH_up_ref(DH *r)
+       {
+       int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
+#ifdef REF_PRINT
+       REF_PRINT("DH",r);
+#endif
+#ifdef REF_CHECK
+       if (i < 2)
+               {
+               fprintf(stderr, "DH_up, bad reference count\n");
+               abort();
+               }
+#endif
+       return ((i > 1) ? 1 : 0);
+       }
+
+int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
+            CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
         {
-       dh_meth_num++;
-       return(CRYPTO_get_ex_new_index(dh_meth_num-1,
-               &dh_meth,argl,argp,new_func,dup_func,free_func));
+       return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp,
+                               new_func, dup_func, free_func);
         }
 
-int DH_set_ex_data(DH *d, int idx, char *arg)
+int DH_set_ex_data(DH *d, int idx, void *arg)
        {
        return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
        }
 
-char *DH_get_ex_data(DH *d, int idx)
+void *DH_get_ex_data(DH *d, int idx)
        {
        return(CRYPTO_get_ex_data(&d->ex_data,idx));
        }
 
-int DH_size(DH *dh)
+int DH_size(const DH *dh)
        {
        return(BN_num_bytes(dh->p));
        }
+
+int DH_security_bits(const DH *dh)
+       {
+       int N;
+       if (dh->q)
+               N = BN_num_bits(dh->q);
+       else if (dh->length)
+               N = dh->length;
+       else
+               N = -1;
+       return BN_security_bits(BN_num_bits(dh->p), N);
+       }