Modify () to (void), since that's what is actually defined in the
[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 "engine_int.h"
64 #include <openssl/engine.h>
65
66
67 #ifndef NO_HW
68 #ifndef NO_HW_NURON
69
70 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
71 static tfnModExp *pfnModExp = NULL;
72
73 static DSO *pvDSOHandle = NULL;
74
75 static int nuron_init(void)
76         {
77         if(pvDSOHandle != NULL)
78                 {
79                 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_ALREADY_LOADED);
80                 return 0;
81                 }
82
83         pvDSOHandle=DSO_load(NULL,"nuronssl",NULL,
84                 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
85         if(!pvDSOHandle)
86                 {
87                 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_NOT_FOUND);
88                 return 0;
89                 }
90
91         pfnModExp=(tfnModExp *)DSO_bind_func(pvDSOHandle,"nuron_mod_exp");
92         if(!pfnModExp)
93                 {
94                 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_FUNCTION_NOT_FOUND);
95                 return 0;
96                 }
97
98         return 1;
99         }
100
101 static int nuron_finish(void)
102         {
103         if(pvDSOHandle == NULL)
104                 {
105                 ENGINEerr(ENGINE_F_NURON_FINISH,ENGINE_R_NOT_LOADED);
106                 return 0;
107                 }
108         if(!DSO_free(pvDSOHandle))
109                 {
110                 ENGINEerr(ENGINE_F_NURON_FINISH,ENGINE_R_DSO_FAILURE);
111                 return 0;
112                 }
113         pvDSOHandle=NULL;
114         pfnModExp=NULL;
115         return 1;
116         }
117
118 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
119                          const BIGNUM *m,BN_CTX *ctx)
120         {
121         if(!pvDSOHandle)
122                 {
123                 ENGINEerr(ENGINE_F_NURON_MOD_EXP,ENGINE_R_NOT_LOADED);
124                 return 0;
125                 }
126         return pfnModExp(r,a,p,m);
127         }
128
129 static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
130         {
131         return nuron_mod_exp(r0,I,rsa->d,rsa->n,NULL);
132         }
133
134 /* This code was liberated and adapted from the commented-out code in
135  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
136  * (it doesn't have a CRT form for RSA), this function means that an
137  * Atalla system running with a DSA server certificate can handshake
138  * around 5 or 6 times faster/more than an equivalent system running with
139  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
140  * of "openssl speed -engine atalla dsa1024 rsa1024". */
141 static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
142                              BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
143                              BN_CTX *ctx, BN_MONT_CTX *in_mont)
144         {
145         BIGNUM t;
146         int to_return = 0;
147  
148         BN_init(&t);
149         /* let rr = a1 ^ p1 mod m */
150         if (!nuron_mod_exp(rr,a1,p1,m,ctx))
151                 goto end;
152         /* let t = a2 ^ p2 mod m */
153         if (!nuron_mod_exp(&t,a2,p2,m,ctx))
154                 goto end;
155         /* let rr = rr * t mod m */
156         if (!BN_mod_mul(rr,rr,&t,m,ctx))
157                 goto end;
158         to_return = 1;
159 end:
160         BN_free(&t);
161         return to_return;
162         }
163
164
165 static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
166                              const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
167                              BN_MONT_CTX *m_ctx)
168         {
169         return nuron_mod_exp(r, a, p, m, ctx);
170         }
171
172 /* This function is aliased to mod_exp (with the mont stuff dropped). */
173 static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
174                               const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
175         {
176         return nuron_mod_exp(r, a, p, m, ctx);
177         }
178
179 /* This function is aliased to mod_exp (with the dh and mont dropped). */
180 static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
181                 const BIGNUM *a, const BIGNUM *p,
182                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
183         {
184         return nuron_mod_exp(r, a, p, m, ctx);
185         }
186
187 static RSA_METHOD nuron_rsa =
188         {
189         "Nuron RSA method",
190         NULL,
191         NULL,
192         NULL,
193         NULL,
194         nuron_rsa_mod_exp,
195         nuron_mod_exp_mont,
196         NULL,
197         NULL,
198         0,
199         NULL,
200         NULL,
201         NULL
202         };
203
204 static DSA_METHOD nuron_dsa =
205         {
206         "Nuron DSA method",
207         NULL, /* dsa_do_sign */
208         NULL, /* dsa_sign_setup */
209         NULL, /* dsa_do_verify */
210         nuron_dsa_mod_exp, /* dsa_mod_exp */
211         nuron_mod_exp_dsa, /* bn_mod_exp */
212         NULL, /* init */
213         NULL, /* finish */
214         0, /* flags */
215         NULL /* app_data */
216         };
217
218 static DH_METHOD nuron_dh =
219         {
220         "Nuron DH method",
221         NULL,
222         NULL,
223         nuron_mod_exp_dh,
224         NULL,
225         NULL,
226         0,
227         NULL
228         };
229
230 static ENGINE engine_nuron =
231         {
232         "nuron",
233         "Nuron hardware engine support",
234         &nuron_rsa,
235         &nuron_dsa,
236         &nuron_dh,
237         NULL,
238         nuron_mod_exp,
239         NULL,
240         nuron_init,
241         nuron_finish,
242         NULL, /* no ctrl() */
243         NULL, /* no load_privkey() */
244         NULL, /* no load_pubkey() */
245         0, /* no flags */
246         0, 0, /* no references */
247         NULL, NULL /* unlinked */
248         };
249
250 /* As this is only ever called once, there's no need for locking
251  * (indeed - the lock will already be held by our caller!!!) */
252 ENGINE *ENGINE_nuron()
253         {
254         const RSA_METHOD *meth1;
255         const DSA_METHOD *meth2;
256         const DH_METHOD *meth3;
257
258         /* We know that the "PKCS1_SSLeay()" functions hook properly
259          * to the nuron-specific mod_exp and mod_exp_crt so we use
260          * those functions. NB: We don't use ENGINE_openssl() or
261          * anything "more generic" because something like the RSAref
262          * code may not hook properly, and if you own one of these
263          * cards then you have the right to do RSA operations on it
264          * anyway! */ 
265         meth1=RSA_PKCS1_SSLeay();
266         nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc;
267         nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec;
268         nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc;
269         nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec;
270
271         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
272          * bits. */
273         meth2=DSA_OpenSSL();
274         nuron_dsa.dsa_do_sign=meth2->dsa_do_sign;
275         nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup;
276         nuron_dsa.dsa_do_verify=meth2->dsa_do_verify;
277
278         /* Much the same for Diffie-Hellman */
279         meth3=DH_OpenSSL();
280         nuron_dh.generate_key=meth3->generate_key;
281         nuron_dh.compute_key=meth3->compute_key;
282         return &engine_nuron;
283         }
284
285 #endif /* !NO_HW_NURON */
286 #endif /* !NO_HW */