Add needed libraries as per configuration to the list of libraries we
[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 <openssl/crypto.h>
61 #include <openssl/buffer.h>
62 #include <openssl/dso.h>
63 #include <openssl/engine.h>
64
65
66 #ifndef OPENSSL_NO_HW
67 #ifndef OPENSSL_NO_HW_NURON
68
69 #define NURON_LIB_NAME "nuron engine"
70 #include "e_nuron_err.c"
71
72 static const char *NURON_LIBNAME = NULL;
73 static const char *get_NURON_LIBNAME(void)
74         {
75         if(NURON_LIBNAME)
76                 return NURON_LIBNAME;
77         return "nuronssl";
78         }
79 static void free_NURON_LIBNAME(void)
80         {
81         if(NURON_LIBNAME)
82                 OPENSSL_free((void*)NURON_LIBNAME);
83         NURON_LIBNAME = NULL;
84         }
85 static long set_NURON_LIBNAME(const char *name)
86         {
87         free_NURON_LIBNAME();
88         return (((NURON_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
89         }
90 static const char *NURON_F1 = "nuron_mod_exp";
91
92 /* The definitions for control commands specific to this engine */
93 #define NURON_CMD_SO_PATH               ENGINE_CMD_BASE
94 static const ENGINE_CMD_DEFN nuron_cmd_defns[] = {
95         {NURON_CMD_SO_PATH,
96                 "SO_PATH",
97                 "Specifies the path to the 'nuronssl' shared library",
98                 ENGINE_CMD_FLAG_STRING},
99         {0, NULL, NULL, 0}
100         };
101
102 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
103 static tfnModExp *pfnModExp = NULL;
104
105 static DSO *pvDSOHandle = NULL;
106
107 static int nuron_destroy(ENGINE *e)
108         {
109         free_NURON_LIBNAME();
110         ERR_unload_NURON_strings();
111         return 1;
112         }
113
114 static int nuron_init(ENGINE *e)
115         {
116         if(pvDSOHandle != NULL)
117                 {
118                 NURONerr(NURON_F_NURON_INIT,NURON_R_ALREADY_LOADED);
119                 return 0;
120                 }
121
122         pvDSOHandle = DSO_load(NULL, get_NURON_LIBNAME(), NULL,
123                 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
124         if(!pvDSOHandle)
125                 {
126                 NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_NOT_FOUND);
127                 return 0;
128                 }
129
130         pfnModExp = (tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
131         if(!pfnModExp)
132                 {
133                 NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_FUNCTION_NOT_FOUND);
134                 return 0;
135                 }
136
137         return 1;
138         }
139
140 static int nuron_finish(ENGINE *e)
141         {
142         free_NURON_LIBNAME();
143         if(pvDSOHandle == NULL)
144                 {
145                 NURONerr(NURON_F_NURON_FINISH,NURON_R_NOT_LOADED);
146                 return 0;
147                 }
148         if(!DSO_free(pvDSOHandle))
149                 {
150                 NURONerr(NURON_F_NURON_FINISH,NURON_R_DSO_FAILURE);
151                 return 0;
152                 }
153         pvDSOHandle=NULL;
154         pfnModExp=NULL;
155         return 1;
156         }
157
158 static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
159         {
160         int initialised = ((pvDSOHandle == NULL) ? 0 : 1);
161         switch(cmd)
162                 {
163         case NURON_CMD_SO_PATH:
164                 if(p == NULL)
165                         {
166                         NURONerr(NURON_F_NURON_CTRL,ERR_R_PASSED_NULL_PARAMETER);
167                         return 0;
168                         }
169                 if(initialised)
170                         {
171                         NURONerr(NURON_F_NURON_CTRL,NURON_R_ALREADY_LOADED);
172                         return 0;
173                         }
174                 return set_NURON_LIBNAME((const char *)p);
175         default:
176                 break;
177                 }
178         NURONerr(NURON_F_NURON_CTRL,NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED);
179         return 0;
180 }
181
182 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
183                          const BIGNUM *m,BN_CTX *ctx)
184         {
185         if(!pvDSOHandle)
186                 {
187                 NURONerr(NURON_F_NURON_MOD_EXP,NURON_R_NOT_LOADED);
188                 return 0;
189                 }
190         return pfnModExp(r,a,p,m);
191         }
192
193 #ifndef OPENSSL_NO_RSA
194 static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
195         {
196         return nuron_mod_exp(r0,I,rsa->d,rsa->n,NULL);
197         }
198 #endif
199
200 #ifndef OPENSSL_NO_DSA
201 /* This code was liberated and adapted from the commented-out code in
202  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
203  * (it doesn't have a CRT form for RSA), this function means that an
204  * Atalla system running with a DSA server certificate can handshake
205  * around 5 or 6 times faster/more than an equivalent system running with
206  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
207  * of "openssl speed -engine atalla dsa1024 rsa1024". */
208 static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
209                              BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
210                              BN_CTX *ctx, BN_MONT_CTX *in_mont)
211         {
212         BIGNUM t;
213         int to_return = 0;
214  
215         BN_init(&t);
216         /* let rr = a1 ^ p1 mod m */
217         if (!nuron_mod_exp(rr,a1,p1,m,ctx))
218                 goto end;
219         /* let t = a2 ^ p2 mod m */
220         if (!nuron_mod_exp(&t,a2,p2,m,ctx))
221                 goto end;
222         /* let rr = rr * t mod m */
223         if (!BN_mod_mul(rr,rr,&t,m,ctx))
224                 goto end;
225         to_return = 1;
226 end:
227         BN_free(&t);
228         return to_return;
229         }
230
231
232 static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
233                              const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
234                              BN_MONT_CTX *m_ctx)
235         {
236         return nuron_mod_exp(r, a, p, m, ctx);
237         }
238 #endif
239
240 /* This function is aliased to mod_exp (with the mont stuff dropped). */
241 static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
242                               const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
243         {
244         return nuron_mod_exp(r, a, p, m, ctx);
245         }
246
247 #ifndef OPENSSL_NO_DH
248 /* This function is aliased to mod_exp (with the dh and mont dropped). */
249 static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
250                 const BIGNUM *a, const BIGNUM *p,
251                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
252         {
253         return nuron_mod_exp(r, a, p, m, ctx);
254         }
255 #endif
256
257 #ifndef OPENSSL_NO_RSA
258 static RSA_METHOD nuron_rsa =
259         {
260         "Nuron RSA method",
261         NULL,
262         NULL,
263         NULL,
264         NULL,
265         nuron_rsa_mod_exp,
266         nuron_mod_exp_mont,
267         NULL,
268         NULL,
269         0,
270         NULL,
271         NULL,
272         NULL
273         };
274 #endif
275
276 #ifndef OPENSSL_NO_DSA
277 static DSA_METHOD nuron_dsa =
278         {
279         "Nuron DSA method",
280         NULL, /* dsa_do_sign */
281         NULL, /* dsa_sign_setup */
282         NULL, /* dsa_do_verify */
283         nuron_dsa_mod_exp, /* dsa_mod_exp */
284         nuron_mod_exp_dsa, /* bn_mod_exp */
285         NULL, /* init */
286         NULL, /* finish */
287         0, /* flags */
288         NULL /* app_data */
289         };
290 #endif
291
292 #ifndef OPENSSL_NO_DH
293 static DH_METHOD nuron_dh =
294         {
295         "Nuron DH method",
296         NULL,
297         NULL,
298         nuron_mod_exp_dh,
299         NULL,
300         NULL,
301         0,
302         NULL
303         };
304 #endif
305
306 /* Constants used when creating the ENGINE */
307 static const char *engine_nuron_id = "nuron";
308 static const char *engine_nuron_name = "Nuron hardware engine support";
309
310 /* This internal function is used by ENGINE_nuron() and possibly by the
311  * "dynamic" ENGINE support too */
312 static int bind_helper(ENGINE *e)
313         {
314 #ifndef OPENSSL_NO_RSA
315         const RSA_METHOD *meth1;
316 #endif
317 #ifndef OPENSSL_NO_DSA
318         const DSA_METHOD *meth2;
319 #endif
320 #ifndef OPENSSL_NO_DH
321         const DH_METHOD *meth3;
322 #endif
323         if(!ENGINE_set_id(e, engine_nuron_id) ||
324                         !ENGINE_set_name(e, engine_nuron_name) ||
325 #ifndef OPENSSL_NO_RSA
326                         !ENGINE_set_RSA(e, &nuron_rsa) ||
327 #endif
328 #ifndef OPENSSL_NO_DSA
329                         !ENGINE_set_DSA(e, &nuron_dsa) ||
330 #endif
331 #ifndef OPENSSL_NO_DH
332                         !ENGINE_set_DH(e, &nuron_dh) ||
333 #endif
334                         !ENGINE_set_destroy_function(e, nuron_destroy) ||
335                         !ENGINE_set_init_function(e, nuron_init) ||
336                         !ENGINE_set_finish_function(e, nuron_finish) ||
337                         !ENGINE_set_ctrl_function(e, nuron_ctrl) ||
338                         !ENGINE_set_cmd_defns(e, nuron_cmd_defns))
339                 return 0;
340
341 #ifndef OPENSSL_NO_RSA
342         /* We know that the "PKCS1_SSLeay()" functions hook properly
343          * to the nuron-specific mod_exp and mod_exp_crt so we use
344          * those functions. NB: We don't use ENGINE_openssl() or
345          * anything "more generic" because something like the RSAref
346          * code may not hook properly, and if you own one of these
347          * cards then you have the right to do RSA operations on it
348          * anyway! */ 
349         meth1=RSA_PKCS1_SSLeay();
350         nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc;
351         nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec;
352         nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc;
353         nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec;
354 #endif
355
356 #ifndef OPENSSL_NO_DSA
357         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
358          * bits. */
359         meth2=DSA_OpenSSL();
360         nuron_dsa.dsa_do_sign=meth2->dsa_do_sign;
361         nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup;
362         nuron_dsa.dsa_do_verify=meth2->dsa_do_verify;
363 #endif
364
365 #ifndef OPENSSL_NO_DH
366         /* Much the same for Diffie-Hellman */
367         meth3=DH_OpenSSL();
368         nuron_dh.generate_key=meth3->generate_key;
369         nuron_dh.compute_key=meth3->compute_key;
370 #endif
371
372         /* Ensure the nuron error handling is set up */
373         ERR_load_NURON_strings();
374         return 1;
375         }
376
377 static ENGINE *engine_nuron(void)
378         {
379         ENGINE *ret = ENGINE_new();
380         if(!ret)
381                 return NULL;
382         if(!bind_helper(ret))
383                 {
384                 ENGINE_free(ret);
385                 return NULL;
386                 }
387         return ret;
388         }
389
390 void ENGINE_load_nuron(void)
391         {
392         /* Copied from eng_[openssl|dyn].c */
393         ENGINE *toadd = engine_nuron();
394         if(!toadd) return;
395         ENGINE_add(toadd);
396         ENGINE_free(toadd);
397         ERR_clear_error();
398         }
399
400 /* This stuff is needed if this ENGINE is being compiled into a self-contained
401  * shared-library. */      
402 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
403 static int bind_fn(ENGINE *e, const char *id)
404         {
405         if(id && (strcmp(id, engine_nuron_id) != 0))
406                 return 0;
407         if(!bind_helper(e))
408                 return 0;
409         return 1;
410         }       
411 IMPLEMENT_DYNAMIC_CHECK_FN()
412 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
413 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
414
415 #endif /* !OPENSSL_NO_HW_NURON */
416 #endif /* !OPENSSL_NO_HW */