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