Step 12 of move of engines: Time to make the changes to support
[openssl.git] / crypto / engine / hw_nuron.c
index a7925e8f72e69e0fee1c09d7b085de17340e3b5c..130b6d8b40571e40bdd034d80dc4d16e3974392a 100644 (file)
 #ifndef OPENSSL_NO_HW
 #ifndef OPENSSL_NO_HW_NURON
 
-static const char def_NURON_LIBNAME[] = "nuronssl";
-static const char *NURON_LIBNAME = def_NURON_LIBNAME;
+#define NURON_LIB_NAME "nuron engine"
+#include "hw_nuron_err.c"
+
+static const char *NURON_LIBNAME = NULL;
+static const char *get_NURON_LIBNAME(void)
+       {
+       if(NURON_LIBNAME)
+               return NURON_LIBNAME;
+       return "nuronssl";
+       }
+static void free_NURON_LIBNAME(void)
+       {
+       if(NURON_LIBNAME)
+               OPENSSL_free((void*)NURON_LIBNAME);
+       NURON_LIBNAME = NULL;
+       }
+static long set_NURON_LIBNAME(const char *name)
+       {
+       free_NURON_LIBNAME();
+       return (((NURON_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
+       }
 static const char *NURON_F1 = "nuron_mod_exp";
 
 /* The definitions for control commands specific to this engine */
@@ -80,63 +99,6 @@ static const ENGINE_CMD_DEFN nuron_cmd_defns[] = {
        {0, NULL, NULL, 0}
        };
 
-#ifndef OPENSSL_NO_ERR
-/* Error function codes for use in nuron operation */
-#define NURON_F_NURON_INIT                     100
-#define NURON_F_NURON_FINISH                   101
-#define NURON_F_NURON_CTRL                     102
-#define NURON_F_NURON_MOD_EXP                  103
-/* Error reason codes */
-#define NURON_R_ALREADY_LOADED                 104
-#define NURON_R_DSO_NOT_FOUND                  105
-#define NURON_R_DSO_FUNCTION_NOT_FOUND         106
-#define NURON_R_NOT_LOADED                     107
-#define NURON_R_DSO_FAILURE                    108
-#define NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED   109
-static ERR_STRING_DATA nuron_str_functs[] =
-       {
-       /* This first element is changed to match the dynamic 'lib' number */
-{ERR_PACK(0,0,0),                              "nuron engine code"},
-{ERR_PACK(0,NURON_F_NURON_INIT,0),             "nuron_init"},
-{ERR_PACK(0,NURON_F_NURON_FINISH,0),           "nuron_finish"},
-{ERR_PACK(0,NURON_F_NURON_CTRL,0),             "nuron_ctrl"},
-{ERR_PACK(0,NURON_F_NURON_MOD_EXP,0),          "nuron_mod_exp"},
-/* Error reason codes */
-{NURON_R_ALREADY_LOADED                        ,"already loaded"},
-{NURON_R_DSO_NOT_FOUND                 ,"DSO not found"},
-{NURON_R_DSO_FUNCTION_NOT_FOUND                ,"DSO function not found"},
-{NURON_R_NOT_LOADED                    ,"not loaded"},
-{NURON_R_DSO_FAILURE                   ,"DSO failure"},
-{NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED  ,"ctrl command not implemented"},
-{0,NULL}
-       };
-/* The library number we obtain dynamically from the ERR code */
-static int nuron_err_lib = -1;
-#define NURONerr(f,r) ERR_PUT_error(nuron_err_lib,(f),(r),__FILE__,__LINE__)
-static void nuron_load_error_strings(void)
-       {
-       if(nuron_err_lib < 0)
-               {
-               if((nuron_err_lib = ERR_get_next_error_library()) <= 0)
-                       return;
-               nuron_str_functs[0].error = ERR_PACK(nuron_err_lib,0,0);
-               ERR_load_strings(nuron_err_lib, nuron_str_functs);
-               }
-       }
-static void nuron_unload_error_strings(void)
-       {
-       if(nuron_err_lib >= 0)
-               {
-               ERR_unload_strings(nuron_err_lib, nuron_str_functs);
-               nuron_err_lib = -1;
-               }
-       }
-#else
-#define NURONerr(f,r)                                  /* NOP */
-static void nuron_load_error_strings(void) { }         /* NOP */
-static void nuron_unload_error_strings(void) { }       /* NOP */
-#endif
-
 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
 static tfnModExp *pfnModExp = NULL;
 
@@ -144,7 +106,8 @@ static DSO *pvDSOHandle = NULL;
 
 static int nuron_destroy(ENGINE *e)
        {
-       nuron_unload_error_strings();
+       free_NURON_LIBNAME();
+       ERR_unload_NURON_strings();
        return 1;
        }
 
@@ -156,7 +119,7 @@ static int nuron_init(ENGINE *e)
                return 0;
                }
 
-       pvDSOHandle = DSO_load(NULL, NURON_LIBNAME, NULL,
+       pvDSOHandle = DSO_load(NULL, get_NURON_LIBNAME(), NULL,
                DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
        if(!pvDSOHandle)
                {
@@ -176,6 +139,7 @@ static int nuron_init(ENGINE *e)
 
 static int nuron_finish(ENGINE *e)
        {
+       free_NURON_LIBNAME();
        if(pvDSOHandle == NULL)
                {
                NURONerr(NURON_F_NURON_FINISH,NURON_R_NOT_LOADED);
@@ -207,8 +171,7 @@ static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
                        NURONerr(NURON_F_NURON_CTRL,NURON_R_ALREADY_LOADED);
                        return 0;
                        }
-               NURON_LIBNAME = (const char *)p;
-               return 1;
+               return set_NURON_LIBNAME((const char *)p);
        default:
                break;
                }
@@ -368,7 +331,6 @@ static int bind_helper(ENGINE *e)
 #ifndef OPENSSL_NO_DH
                        !ENGINE_set_DH(e, &nuron_dh) ||
 #endif
-                       !ENGINE_set_BN_mod_exp(e, nuron_mod_exp) ||
                        !ENGINE_set_destroy_function(e, nuron_destroy) ||
                        !ENGINE_set_init_function(e, nuron_init) ||
                        !ENGINE_set_finish_function(e, nuron_finish) ||
@@ -408,13 +370,11 @@ static int bind_helper(ENGINE *e)
 #endif
 
        /* Ensure the nuron error handling is set up */
-       nuron_load_error_strings();
+       ERR_load_NURON_strings();
        return 1;
        }
 
-/* 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_nuron(void)
+static ENGINE *engine_nuron(void)
        {
        ENGINE *ret = ENGINE_new();
        if(!ret)
@@ -427,6 +387,16 @@ ENGINE *ENGINE_nuron(void)
        return ret;
        }
 
+void ENGINE_load_nuron(void)
+       {
+       /* Copied from eng_[openssl|dyn].c */
+       ENGINE *toadd = engine_nuron();
+       if(!toadd) return;
+       ENGINE_add(toadd);
+       ENGINE_free(toadd);
+       ERR_clear_error();
+       }
+
 /* This stuff is needed if this ENGINE is being compiled into a self-contained
  * shared-library. */     
 #ifdef ENGINE_DYNAMIC_SUPPORT