Make return value from EVP_PKEY_cmp() and EVP_PKEY_cmp_parameters() consistent.
[openssl.git] / crypto / ecdh / ech_lib.c
index fd8cb19fddada4e443d076bb5af07f6df3e03bc0..01e75e2a5c0c806d505ac0854bc05a1a5255a900 100644 (file)
@@ -14,7 +14,7 @@
  *
  */
 /* ====================================================================
- * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  *
  */
 
-#include "ecdh.h"
+#include "ech_locl.h"
 #include <string.h>
+#ifndef OPENSSL_NO_ENGINE
 #include <openssl/engine.h>
+#endif
+#include <openssl/err.h>
 
 const char *ECDH_version="ECDH" OPENSSL_VERSION_PTEXT;
 
-static void ecdh_finish(EC_KEY *);
-
 static const ECDH_METHOD *default_ECDH_method = NULL;
 
+static void *ecdh_data_new(void);
+static void *ecdh_data_dup(void *);
+static void  ecdh_data_free(void *);
+
 void ECDH_set_default_method(const ECDH_METHOD *meth)
        {
        default_ECDH_method = meth;
@@ -104,11 +109,13 @@ int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth)
         if (mtmp->finish)
                mtmp->finish(eckey);
 #endif
+#ifndef OPENSSL_NO_ENGINE
        if (ecdh->engine)
                {
                ENGINE_finish(ecdh->engine);
                ecdh->engine = NULL;
                }
+#endif
         ecdh->meth = meth;
 #if 0
         if (meth->init) 
@@ -117,27 +124,22 @@ int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth)
         return 1;
        }
 
-ECDH_DATA *ECDH_DATA_new(void)
-       {
-       return ECDH_DATA_new_method(NULL);
-       }
-
-ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine)
+static ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine)
        {
        ECDH_DATA *ret;
 
        ret=(ECDH_DATA *)OPENSSL_malloc(sizeof(ECDH_DATA));
        if (ret == NULL)
                {
-               ECDHerr(ECDH_F_ECDH_DATA_NEW, ERR_R_MALLOC_FAILURE);
+               ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
 
        ret->init = NULL;
-       ret->finish = ecdh_finish;
 
        ret->meth = ECDH_get_default_method();
        ret->engine = engine;
+#ifndef OPENSSL_NO_ENGINE
        if (!ret->engine)
                ret->engine = ENGINE_get_default_ECDH();
        if (ret->engine)
@@ -145,12 +147,13 @@ ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine)
                ret->meth = ENGINE_get_ECDH(ret->engine);
                if (!ret->meth)
                        {
-                       ECDHerr(ECDH_F_ECDH_DATA_NEW, ERR_R_ENGINE_LIB);
+                       ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_ENGINE_LIB);
                        ENGINE_finish(ret->engine);
                        OPENSSL_free(ret);
                        return NULL;
                        }
                }
+#endif
 
        ret->flags = ret->meth->flags;
        CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data);
@@ -165,50 +168,59 @@ ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine)
        return(ret);
        }
 
-void ECDH_DATA_free(ECDH_DATA *r)
+static void *ecdh_data_new(void)
        {
-#if 0
-       if (r->meth->finish)
-               r->meth->finish(r);
-#endif
+       return (void *)ECDH_DATA_new_method(NULL);
+       }
+
+static void *ecdh_data_dup(void *data)
+{
+       ECDH_DATA *r = (ECDH_DATA *)data;
+
+       /* XXX: dummy operation */
+       if (r == NULL)
+               return NULL;
+
+       return (void *)ecdh_data_new();
+}
+
+void ecdh_data_free(void *data)
+       {
+       ECDH_DATA *r = (ECDH_DATA *)data;
+
+#ifndef OPENSSL_NO_ENGINE
        if (r->engine)
                ENGINE_finish(r->engine);
+#endif
 
        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, r, &r->ex_data);
 
-       memset((void *)r, 0x0, sizeof(ECDH_DATA));
+       OPENSSL_cleanse((void *)r, sizeof(ECDH_DATA));
 
        OPENSSL_free(r);
        }
 
 ECDH_DATA *ecdh_check(EC_KEY *key)
        {
-       if (key->meth_data)
-               {
-               if (key->meth_data->finish != ecdh_finish)
-                       {
-                       key->meth_data->finish(key);
-                       key->meth_data = (EC_KEY_METH_DATA *)ECDH_DATA_new();
-                       }
-               }
-       else
-               key->meth_data = (EC_KEY_METH_DATA *)ECDH_DATA_new();
-       return (ECDH_DATA *)key->meth_data;
-       }
-
-static void ecdh_finish(EC_KEY *key)
+       ECDH_DATA *ecdh_data;
+       void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup,
+                                       ecdh_data_free, ecdh_data_free);
+       if (data == NULL)
        {
-       if (key->meth_data && key->meth_data->finish == ecdh_finish)
-               ECDH_DATA_free((ECDH_DATA *)key->meth_data);
+               ecdh_data = (ECDH_DATA *)ecdh_data_new();
+               if (ecdh_data == NULL)
+                       return NULL;
+               EC_KEY_insert_key_method_data(key, (void *)ecdh_data,
+                       ecdh_data_dup, ecdh_data_free, ecdh_data_free);
        }
+       else
+               ecdh_data = (ECDH_DATA *)data;
+       
 
-
-int ECDH_size(const EC_KEY *ecdh)
-       {
-       return 20;
+       return ecdh_data;
        }
 
-
 int ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
             CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
        {