When build as dynamic engines, the loading functions should be defined
[openssl.git] / engines / e_nuron.c
1 /* crypto/engine/hw_nuron.c */
2 /* Written by Ben Laurie for the OpenSSL Project, leaning heavily on Geoff
3  * Thorpe's Atalla implementation.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000-2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include <string.h>
61 #include <openssl/crypto.h>
62 #include <openssl/buffer.h>
63 #include <openssl/dso.h>
64 #include <openssl/engine.h>
65
66
67 #ifndef OPENSSL_NO_HW
68 #ifndef OPENSSL_NO_HW_NURON
69
70 #define NURON_LIB_NAME "nuron engine"
71 #include "e_nuron_err.c"
72
73 static const char *NURON_LIBNAME = NULL;
74 static const char *get_NURON_LIBNAME(void)
75         {
76         if(NURON_LIBNAME)
77                 return NURON_LIBNAME;
78         return "nuronssl";
79         }
80 static void free_NURON_LIBNAME(void)
81         {
82         if(NURON_LIBNAME)
83                 OPENSSL_free((void*)NURON_LIBNAME);
84         NURON_LIBNAME = NULL;
85         }
86 static long set_NURON_LIBNAME(const char *name)
87         {
88         free_NURON_LIBNAME();
89         return (((NURON_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
90         }
91 static const char *NURON_F1 = "nuron_mod_exp";
92
93 /* The definitions for control commands specific to this engine */
94 #define NURON_CMD_SO_PATH               ENGINE_CMD_BASE
95 static const ENGINE_CMD_DEFN nuron_cmd_defns[] = {
96         {NURON_CMD_SO_PATH,
97                 "SO_PATH",
98                 "Specifies the path to the 'nuronssl' shared library",
99                 ENGINE_CMD_FLAG_STRING},
100         {0, NULL, NULL, 0}
101         };
102
103 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
104 static tfnModExp *pfnModExp = NULL;
105
106 static DSO *pvDSOHandle = NULL;
107
108 static int nuron_destroy(ENGINE *e)
109         {
110         free_NURON_LIBNAME();
111         ERR_unload_NURON_strings();
112         return 1;
113         }
114
115 static int nuron_init(ENGINE *e)
116         {
117         if(pvDSOHandle != NULL)
118                 {
119                 NURONerr(NURON_F_NURON_INIT,NURON_R_ALREADY_LOADED);
120                 return 0;
121                 }
122
123         pvDSOHandle = DSO_load(NULL, get_NURON_LIBNAME(), NULL,
124                 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
125         if(!pvDSOHandle)
126                 {
127                 NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_NOT_FOUND);
128                 return 0;
129                 }
130
131         pfnModExp = (tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
132         if(!pfnModExp)
133                 {
134                 NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_FUNCTION_NOT_FOUND);
135                 return 0;
136                 }
137
138         return 1;
139         }
140
141 static int nuron_finish(ENGINE *e)
142         {
143         free_NURON_LIBNAME();
144         if(pvDSOHandle == NULL)
145                 {
146                 NURONerr(NURON_F_NURON_FINISH,NURON_R_NOT_LOADED);
147                 return 0;
148                 }
149         if(!DSO_free(pvDSOHandle))
150                 {
151                 NURONerr(NURON_F_NURON_FINISH,NURON_R_DSO_FAILURE);
152                 return 0;
153                 }
154         pvDSOHandle=NULL;
155         pfnModExp=NULL;
156         return 1;
157         }
158
159 static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
160         {
161         int initialised = ((pvDSOHandle == NULL) ? 0 : 1);
162         switch(cmd)
163                 {
164         case NURON_CMD_SO_PATH:
165                 if(p == NULL)
166                         {
167                         NURONerr(NURON_F_NURON_CTRL,ERR_R_PASSED_NULL_PARAMETER);
168                         return 0;
169                         }
170                 if(initialised)
171                         {
172                         NURONerr(NURON_F_NURON_CTRL,NURON_R_ALREADY_LOADED);
173                         return 0;
174                         }
175                 return set_NURON_LIBNAME((const char *)p);
176         default:
177                 break;
178                 }
179         NURONerr(NURON_F_NURON_CTRL,NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED);
180         return 0;
181 }
182
183 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
184                          const BIGNUM *m,BN_CTX *ctx)
185         {
186         if(!pvDSOHandle)
187                 {
188                 NURONerr(NURON_F_NURON_MOD_EXP,NURON_R_NOT_LOADED);
189                 return 0;
190                 }
191         return pfnModExp(r,a,p,m);
192         }
193
194 #ifndef OPENSSL_NO_RSA
195 static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
196         {
197         return nuron_mod_exp(r0,I,rsa->d,rsa->n,NULL);
198         }
199 #endif
200
201 #ifndef OPENSSL_NO_DSA
202 /* This code was liberated and adapted from the commented-out code in
203  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
204  * (it doesn't have a CRT form for RSA), this function means that an
205  * Atalla system running with a DSA server certificate can handshake
206  * around 5 or 6 times faster/more than an equivalent system running with
207  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
208  * of "openssl speed -engine atalla dsa1024 rsa1024". */
209 static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
210                              BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
211                              BN_CTX *ctx, BN_MONT_CTX *in_mont)
212         {
213         BIGNUM t;
214         int to_return = 0;
215  
216         BN_init(&t);
217         /* let rr = a1 ^ p1 mod m */
218         if (!nuron_mod_exp(rr,a1,p1,m,ctx))
219                 goto end;
220         /* let t = a2 ^ p2 mod m */
221         if (!nuron_mod_exp(&t,a2,p2,m,ctx))
222                 goto end;
223         /* let rr = rr * t mod m */
224         if (!BN_mod_mul(rr,rr,&t,m,ctx))
225                 goto end;
226         to_return = 1;
227 end:
228         BN_free(&t);
229         return to_return;
230         }
231
232
233 static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
234                              const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
235                              BN_MONT_CTX *m_ctx)
236         {
237         return nuron_mod_exp(r, a, p, m, ctx);
238         }
239 #endif
240
241 /* This function is aliased to mod_exp (with the mont stuff dropped). */
242 static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
243                               const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
244         {
245         return nuron_mod_exp(r, a, p, m, ctx);
246         }
247
248 #ifndef OPENSSL_NO_DH
249 /* This function is aliased to mod_exp (with the dh and mont dropped). */
250 static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
251                 const BIGNUM *a, const BIGNUM *p,
252                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
253         {
254         return nuron_mod_exp(r, a, p, m, ctx);
255         }
256 #endif
257
258 #ifndef OPENSSL_NO_RSA
259 static RSA_METHOD nuron_rsa =
260         {
261         "Nuron RSA method",
262         NULL,
263         NULL,
264         NULL,
265         NULL,
266         nuron_rsa_mod_exp,
267         nuron_mod_exp_mont,
268         NULL,
269         NULL,
270         0,
271         NULL,
272         NULL,
273         NULL
274         };
275 #endif
276
277 #ifndef OPENSSL_NO_DSA
278 static DSA_METHOD nuron_dsa =
279         {
280         "Nuron DSA method",
281         NULL, /* dsa_do_sign */
282         NULL, /* dsa_sign_setup */
283         NULL, /* dsa_do_verify */
284         nuron_dsa_mod_exp, /* dsa_mod_exp */
285         nuron_mod_exp_dsa, /* bn_mod_exp */
286         NULL, /* init */
287         NULL, /* finish */
288         0, /* flags */
289         NULL /* app_data */
290         };
291 #endif
292
293 #ifndef OPENSSL_NO_DH
294 static DH_METHOD nuron_dh =
295         {
296         "Nuron DH method",
297         NULL,
298         NULL,
299         nuron_mod_exp_dh,
300         NULL,
301         NULL,
302         0,
303         NULL
304         };
305 #endif
306
307 /* Constants used when creating the ENGINE */
308 static const char *engine_nuron_id = "nuron";
309 static const char *engine_nuron_name = "Nuron hardware engine support";
310
311 /* This internal function is used by ENGINE_nuron() and possibly by the
312  * "dynamic" ENGINE support too */
313 static int bind_helper(ENGINE *e)
314         {
315 #ifndef OPENSSL_NO_RSA
316         const RSA_METHOD *meth1;
317 #endif
318 #ifndef OPENSSL_NO_DSA
319         const DSA_METHOD *meth2;
320 #endif
321 #ifndef OPENSSL_NO_DH
322         const DH_METHOD *meth3;
323 #endif
324         if(!ENGINE_set_id(e, engine_nuron_id) ||
325                         !ENGINE_set_name(e, engine_nuron_name) ||
326 #ifndef OPENSSL_NO_RSA
327                         !ENGINE_set_RSA(e, &nuron_rsa) ||
328 #endif
329 #ifndef OPENSSL_NO_DSA
330                         !ENGINE_set_DSA(e, &nuron_dsa) ||
331 #endif
332 #ifndef OPENSSL_NO_DH
333                         !ENGINE_set_DH(e, &nuron_dh) ||
334 #endif
335                         !ENGINE_set_destroy_function(e, nuron_destroy) ||
336                         !ENGINE_set_init_function(e, nuron_init) ||
337                         !ENGINE_set_finish_function(e, nuron_finish) ||
338                         !ENGINE_set_ctrl_function(e, nuron_ctrl) ||
339                         !ENGINE_set_cmd_defns(e, nuron_cmd_defns))
340                 return 0;
341
342 #ifndef OPENSSL_NO_RSA
343         /* We know that the "PKCS1_SSLeay()" functions hook properly
344          * to the nuron-specific mod_exp and mod_exp_crt so we use
345          * those functions. NB: We don't use ENGINE_openssl() or
346          * anything "more generic" because something like the RSAref
347          * code may not hook properly, and if you own one of these
348          * cards then you have the right to do RSA operations on it
349          * anyway! */ 
350         meth1=RSA_PKCS1_SSLeay();
351         nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc;
352         nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec;
353         nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc;
354         nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec;
355 #endif
356
357 #ifndef OPENSSL_NO_DSA
358         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
359          * bits. */
360         meth2=DSA_OpenSSL();
361         nuron_dsa.dsa_do_sign=meth2->dsa_do_sign;
362         nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup;
363         nuron_dsa.dsa_do_verify=meth2->dsa_do_verify;
364 #endif
365
366 #ifndef OPENSSL_NO_DH
367         /* Much the same for Diffie-Hellman */
368         meth3=DH_OpenSSL();
369         nuron_dh.generate_key=meth3->generate_key;
370         nuron_dh.compute_key=meth3->compute_key;
371 #endif
372
373         /* Ensure the nuron error handling is set up */
374         ERR_load_NURON_strings();
375         return 1;
376         }
377
378 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
379 static ENGINE *engine_nuron(void)
380         {
381         ENGINE *ret = ENGINE_new();
382         if(!ret)
383                 return NULL;
384         if(!bind_helper(ret))
385                 {
386                 ENGINE_free(ret);
387                 return NULL;
388                 }
389         return ret;
390         }
391
392 void ENGINE_load_nuron(void)
393         {
394         /* Copied from eng_[openssl|dyn].c */
395         ENGINE *toadd = engine_nuron();
396         if(!toadd) return;
397         ENGINE_add(toadd);
398         ENGINE_free(toadd);
399         ERR_clear_error();
400         }
401 #endif
402
403 /* This stuff is needed if this ENGINE is being compiled into a self-contained
404  * shared-library. */      
405 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
406 static int bind_fn(ENGINE *e, const char *id)
407         {
408         if(id && (strcmp(id, engine_nuron_id) != 0))
409                 return 0;
410         if(!bind_helper(e))
411                 return 0;
412         return 1;
413         }       
414 IMPLEMENT_DYNAMIC_CHECK_FN()
415 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
416 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
417
418 #endif /* !OPENSSL_NO_HW_NURON */
419 #endif /* !OPENSSL_NO_HW */