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