1 /* crypto/engine/hw_nuron.c */
2 /* Written by Ben Laurie for the OpenSSL Project, leaning heavily on Geoff
3 * Thorpe's Atalla implementation.
5 /* ====================================================================
6 * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #include <openssl/crypto.h>
62 #include <openssl/buffer.h>
63 #include <openssl/dso.h>
64 #include <openssl/engine.h>
65 #include <openssl/rsa.h>
66 #include <openssl/dsa.h>
67 #include <openssl/dh.h>
68 #include <openssl/bn.h>
71 #ifndef OPENSSL_NO_HW_NURON
73 #define NURON_LIB_NAME "nuron engine"
74 #include "e_nuron_err.c"
76 static const char *NURON_LIBNAME = NULL;
77 static const char *get_NURON_LIBNAME(void)
83 static void free_NURON_LIBNAME(void)
86 OPENSSL_free((void*)NURON_LIBNAME);
89 static long set_NURON_LIBNAME(const char *name)
92 return (((NURON_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
94 static const char *NURON_F1 = "nuron_mod_exp";
96 /* The definitions for control commands specific to this engine */
97 #define NURON_CMD_SO_PATH ENGINE_CMD_BASE
98 static const ENGINE_CMD_DEFN nuron_cmd_defns[] = {
101 "Specifies the path to the 'nuronssl' shared library",
102 ENGINE_CMD_FLAG_STRING},
106 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
107 static tfnModExp *pfnModExp = NULL;
109 static DSO *pvDSOHandle = NULL;
111 static int nuron_destroy(ENGINE *e)
113 free_NURON_LIBNAME();
114 ERR_unload_NURON_strings();
118 static int nuron_init(ENGINE *e)
120 if(pvDSOHandle != NULL)
122 NURONerr(NURON_F_NURON_INIT,NURON_R_ALREADY_LOADED);
126 pvDSOHandle = DSO_load(NULL, get_NURON_LIBNAME(), NULL,
127 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
130 NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_NOT_FOUND);
134 pfnModExp = (tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
137 NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_FUNCTION_NOT_FOUND);
144 static int nuron_finish(ENGINE *e)
146 free_NURON_LIBNAME();
147 if(pvDSOHandle == NULL)
149 NURONerr(NURON_F_NURON_FINISH,NURON_R_NOT_LOADED);
152 if(!DSO_free(pvDSOHandle))
154 NURONerr(NURON_F_NURON_FINISH,NURON_R_DSO_FAILURE);
162 static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
164 int initialised = ((pvDSOHandle == NULL) ? 0 : 1);
167 case NURON_CMD_SO_PATH:
170 NURONerr(NURON_F_NURON_CTRL,ERR_R_PASSED_NULL_PARAMETER);
175 NURONerr(NURON_F_NURON_CTRL,NURON_R_ALREADY_LOADED);
178 return set_NURON_LIBNAME((const char *)p);
182 NURONerr(NURON_F_NURON_CTRL,NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED);
186 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
187 const BIGNUM *m,BN_CTX *ctx)
191 NURONerr(NURON_F_NURON_MOD_EXP,NURON_R_NOT_LOADED);
194 return pfnModExp(r,a,p,m);
197 #ifndef OPENSSL_NO_RSA
198 static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
200 return nuron_mod_exp(r0,I,rsa->d,rsa->n,ctx);
204 #ifndef OPENSSL_NO_DSA
205 /* This code was liberated and adapted from the commented-out code in
206 * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
207 * (it doesn't have a CRT form for RSA), this function means that an
208 * Atalla system running with a DSA server certificate can handshake
209 * around 5 or 6 times faster/more than an equivalent system running with
210 * RSA. Just check out the "signs" statistics from the RSA and DSA parts
211 * of "openssl speed -engine atalla dsa1024 rsa1024". */
212 static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
213 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
214 BN_CTX *ctx, BN_MONT_CTX *in_mont)
220 /* let rr = a1 ^ p1 mod m */
221 if (!nuron_mod_exp(rr,a1,p1,m,ctx))
223 /* let t = a2 ^ p2 mod m */
224 if (!nuron_mod_exp(&t,a2,p2,m,ctx))
226 /* let rr = rr * t mod m */
227 if (!BN_mod_mul(rr,rr,&t,m,ctx))
236 static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
237 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
240 return nuron_mod_exp(r, a, p, m, ctx);
244 /* This function is aliased to mod_exp (with the mont stuff dropped). */
245 static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
246 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
248 return nuron_mod_exp(r, a, p, m, ctx);
251 #ifndef OPENSSL_NO_DH
252 /* This function is aliased to mod_exp (with the dh and mont dropped). */
253 static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
254 const BIGNUM *a, const BIGNUM *p,
255 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
257 return nuron_mod_exp(r, a, p, m, ctx);
261 #ifndef OPENSSL_NO_RSA
262 static RSA_METHOD nuron_rsa =
281 #ifndef OPENSSL_NO_DSA
282 static DSA_METHOD nuron_dsa =
285 NULL, /* dsa_do_sign */
286 NULL, /* dsa_sign_setup */
287 NULL, /* dsa_do_verify */
288 nuron_dsa_mod_exp, /* dsa_mod_exp */
289 nuron_mod_exp_dsa, /* bn_mod_exp */
294 NULL, /* dsa_paramgen */
295 NULL /* dsa_keygen */
299 #ifndef OPENSSL_NO_DH
300 static DH_METHOD nuron_dh =
314 /* Constants used when creating the ENGINE */
315 static const char *engine_nuron_id = "nuron";
316 static const char *engine_nuron_name = "Nuron hardware engine support";
318 /* This internal function is used by ENGINE_nuron() and possibly by the
319 * "dynamic" ENGINE support too */
320 static int bind_helper(ENGINE *e)
322 #ifndef OPENSSL_NO_RSA
323 const RSA_METHOD *meth1;
325 #ifndef OPENSSL_NO_DSA
326 const DSA_METHOD *meth2;
328 #ifndef OPENSSL_NO_DH
329 const DH_METHOD *meth3;
331 if(!ENGINE_set_id(e, engine_nuron_id) ||
332 !ENGINE_set_name(e, engine_nuron_name) ||
333 #ifndef OPENSSL_NO_RSA
334 !ENGINE_set_RSA(e, &nuron_rsa) ||
336 #ifndef OPENSSL_NO_DSA
337 !ENGINE_set_DSA(e, &nuron_dsa) ||
339 #ifndef OPENSSL_NO_DH
340 !ENGINE_set_DH(e, &nuron_dh) ||
342 !ENGINE_set_destroy_function(e, nuron_destroy) ||
343 !ENGINE_set_init_function(e, nuron_init) ||
344 !ENGINE_set_finish_function(e, nuron_finish) ||
345 !ENGINE_set_ctrl_function(e, nuron_ctrl) ||
346 !ENGINE_set_cmd_defns(e, nuron_cmd_defns))
349 #ifndef OPENSSL_NO_RSA
350 /* We know that the "PKCS1_SSLeay()" functions hook properly
351 * to the nuron-specific mod_exp and mod_exp_crt so we use
352 * those functions. NB: We don't use ENGINE_openssl() or
353 * anything "more generic" because something like the RSAref
354 * code may not hook properly, and if you own one of these
355 * cards then you have the right to do RSA operations on it
357 meth1=RSA_PKCS1_SSLeay();
358 nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc;
359 nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec;
360 nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc;
361 nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec;
364 #ifndef OPENSSL_NO_DSA
365 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
368 nuron_dsa.dsa_do_sign=meth2->dsa_do_sign;
369 nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup;
370 nuron_dsa.dsa_do_verify=meth2->dsa_do_verify;
373 #ifndef OPENSSL_NO_DH
374 /* Much the same for Diffie-Hellman */
376 nuron_dh.generate_key=meth3->generate_key;
377 nuron_dh.compute_key=meth3->compute_key;
380 /* Ensure the nuron error handling is set up */
381 ERR_load_NURON_strings();
385 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
386 static ENGINE *engine_nuron(void)
388 ENGINE *ret = ENGINE_new();
391 if(!bind_helper(ret))
399 void ENGINE_load_nuron(void)
401 /* Copied from eng_[openssl|dyn].c */
402 ENGINE *toadd = engine_nuron();
410 /* This stuff is needed if this ENGINE is being compiled into a self-contained
412 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
413 static int bind_fn(ENGINE *e, const char *id)
415 if(id && (strcmp(id, engine_nuron_id) != 0))
421 IMPLEMENT_DYNAMIC_CHECK_FN()
422 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
423 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
425 #endif /* !OPENSSL_NO_HW_NURON */
426 #endif /* !OPENSSL_NO_HW */