Some more tweaks to ENGINE code.
[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 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
84 static tfnModExp *pfnModExp = NULL;
85
86 static DSO *pvDSOHandle = NULL;
87
88 static int nuron_init(ENGINE *e)
89         {
90         if(pvDSOHandle != NULL)
91                 {
92                 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_ALREADY_LOADED);
93                 return 0;
94                 }
95
96         pvDSOHandle = DSO_load(NULL, NURON_LIBNAME, NULL,
97                 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
98         if(!pvDSOHandle)
99                 {
100                 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_NOT_FOUND);
101                 return 0;
102                 }
103
104         pfnModExp = (tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
105         if(!pfnModExp)
106                 {
107                 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_FUNCTION_NOT_FOUND);
108                 return 0;
109                 }
110
111         return 1;
112         }
113
114 static int nuron_finish(ENGINE *e)
115         {
116         if(pvDSOHandle == NULL)
117                 {
118                 ENGINEerr(ENGINE_F_NURON_FINISH,ENGINE_R_NOT_LOADED);
119                 return 0;
120                 }
121         if(!DSO_free(pvDSOHandle))
122                 {
123                 ENGINEerr(ENGINE_F_NURON_FINISH,ENGINE_R_DSO_FAILURE);
124                 return 0;
125                 }
126         pvDSOHandle=NULL;
127         pfnModExp=NULL;
128         return 1;
129         }
130
131 static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
132         {
133         int initialised = ((pvDSOHandle == NULL) ? 0 : 1);
134         switch(cmd)
135                 {
136         case NURON_CMD_SO_PATH:
137                 if(p == NULL)
138                         {
139                         ENGINEerr(ENGINE_F_NURON_CTRL,ERR_R_PASSED_NULL_PARAMETER);
140                         return 0;
141                         }
142                 if(initialised)
143                         {
144                         ENGINEerr(ENGINE_F_NURON_CTRL,ENGINE_R_ALREADY_LOADED);
145                         return 0;
146                         }
147                 NURON_LIBNAME = (const char *)p;
148                 return 1;
149         default:
150                 break;
151                 }
152         ENGINEerr(ENGINE_F_NURON_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
153         return 0;
154 }
155
156 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
157                          const BIGNUM *m,BN_CTX *ctx)
158         {
159         if(!pvDSOHandle)
160                 {
161                 ENGINEerr(ENGINE_F_NURON_MOD_EXP,ENGINE_R_NOT_LOADED);
162                 return 0;
163                 }
164         return pfnModExp(r,a,p,m);
165         }
166
167 static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
168         {
169         return nuron_mod_exp(r0,I,rsa->d,rsa->n,NULL);
170         }
171
172 /* This code was liberated and adapted from the commented-out code in
173  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
174  * (it doesn't have a CRT form for RSA), this function means that an
175  * Atalla system running with a DSA server certificate can handshake
176  * around 5 or 6 times faster/more than an equivalent system running with
177  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
178  * of "openssl speed -engine atalla dsa1024 rsa1024". */
179 static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
180                              BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
181                              BN_CTX *ctx, BN_MONT_CTX *in_mont)
182         {
183         BIGNUM t;
184         int to_return = 0;
185  
186         BN_init(&t);
187         /* let rr = a1 ^ p1 mod m */
188         if (!nuron_mod_exp(rr,a1,p1,m,ctx))
189                 goto end;
190         /* let t = a2 ^ p2 mod m */
191         if (!nuron_mod_exp(&t,a2,p2,m,ctx))
192                 goto end;
193         /* let rr = rr * t mod m */
194         if (!BN_mod_mul(rr,rr,&t,m,ctx))
195                 goto end;
196         to_return = 1;
197 end:
198         BN_free(&t);
199         return to_return;
200         }
201
202
203 static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
204                              const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
205                              BN_MONT_CTX *m_ctx)
206         {
207         return nuron_mod_exp(r, a, p, m, ctx);
208         }
209
210 /* This function is aliased to mod_exp (with the mont stuff dropped). */
211 static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
212                               const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
213         {
214         return nuron_mod_exp(r, a, p, m, ctx);
215         }
216
217 /* This function is aliased to mod_exp (with the dh and mont dropped). */
218 static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
219                 const BIGNUM *a, const BIGNUM *p,
220                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
221         {
222         return nuron_mod_exp(r, a, p, m, ctx);
223         }
224
225 static RSA_METHOD nuron_rsa =
226         {
227         "Nuron RSA method",
228         NULL,
229         NULL,
230         NULL,
231         NULL,
232         nuron_rsa_mod_exp,
233         nuron_mod_exp_mont,
234         NULL,
235         NULL,
236         0,
237         NULL,
238         NULL,
239         NULL
240         };
241
242 static DSA_METHOD nuron_dsa =
243         {
244         "Nuron DSA method",
245         NULL, /* dsa_do_sign */
246         NULL, /* dsa_sign_setup */
247         NULL, /* dsa_do_verify */
248         nuron_dsa_mod_exp, /* dsa_mod_exp */
249         nuron_mod_exp_dsa, /* bn_mod_exp */
250         NULL, /* init */
251         NULL, /* finish */
252         0, /* flags */
253         NULL /* app_data */
254         };
255
256 static DH_METHOD nuron_dh =
257         {
258         "Nuron DH method",
259         NULL,
260         NULL,
261         nuron_mod_exp_dh,
262         NULL,
263         NULL,
264         0,
265         NULL
266         };
267
268 /* Constants used when creating the ENGINE */
269 static const char *engine_nuron_id = "nuron";
270 static const char *engine_nuron_name = "Nuron hardware engine support";
271
272 /* As this is only ever called once, there's no need for locking
273  * (indeed - the lock will already be held by our caller!!!) */
274 ENGINE *ENGINE_nuron()
275         {
276         const RSA_METHOD *meth1;
277         const DSA_METHOD *meth2;
278         const DH_METHOD *meth3;
279         ENGINE *ret = ENGINE_new();
280         if(!ret)
281                 return NULL;
282         if(!ENGINE_set_id(ret, engine_nuron_id) ||
283                         !ENGINE_set_name(ret, engine_nuron_name) ||
284                         !ENGINE_set_RSA(ret, &nuron_rsa) ||
285                         !ENGINE_set_DSA(ret, &nuron_dsa) ||
286                         !ENGINE_set_DH(ret, &nuron_dh) ||
287                         !ENGINE_set_BN_mod_exp(ret, nuron_mod_exp) ||
288                         !ENGINE_set_init_function(ret, nuron_init) ||
289                         !ENGINE_set_finish_function(ret, nuron_finish) ||
290                         !ENGINE_set_ctrl_function(ret, nuron_ctrl) ||
291                         !ENGINE_set_cmd_defns(ret, nuron_cmd_defns))
292                 {
293                 ENGINE_free(ret);
294                 return NULL;
295                 }
296
297         /* We know that the "PKCS1_SSLeay()" functions hook properly
298          * to the nuron-specific mod_exp and mod_exp_crt so we use
299          * those functions. NB: We don't use ENGINE_openssl() or
300          * anything "more generic" because something like the RSAref
301          * code may not hook properly, and if you own one of these
302          * cards then you have the right to do RSA operations on it
303          * anyway! */ 
304         meth1=RSA_PKCS1_SSLeay();
305         nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc;
306         nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec;
307         nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc;
308         nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec;
309
310         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
311          * bits. */
312         meth2=DSA_OpenSSL();
313         nuron_dsa.dsa_do_sign=meth2->dsa_do_sign;
314         nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup;
315         nuron_dsa.dsa_do_verify=meth2->dsa_do_verify;
316
317         /* Much the same for Diffie-Hellman */
318         meth3=DH_OpenSSL();
319         nuron_dh.generate_key=meth3->generate_key;
320         nuron_dh.compute_key=meth3->compute_key;
321         return ret;
322         }
323
324 #endif /* !OPENSSL_NO_HW_NURON */
325 #endif /* !OPENSSL_NO_HW */