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 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).
60 #include <openssl/crypto.h>
62 #include <openssl/dso.h>
63 #include "engine_int.h"
64 #include <openssl/engine.h>
68 #ifndef OPENSSL_NO_HW_NURON
70 static const char *NURON_LIBNAME = "nuronssl";
71 static const char *NURON_F1 = "nuron_mod_exp";
73 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
74 static tfnModExp *pfnModExp = NULL;
76 static DSO *pvDSOHandle = NULL;
78 static int nuron_init(ENGINE *e)
80 if(pvDSOHandle != NULL)
82 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_ALREADY_LOADED);
86 pvDSOHandle=DSO_load(NULL, NURON_LIBNAME, NULL,
87 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
90 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_NOT_FOUND);
94 pfnModExp=(tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
97 ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_FUNCTION_NOT_FOUND);
104 static int nuron_finish(ENGINE *e)
106 if(pvDSOHandle == NULL)
108 ENGINEerr(ENGINE_F_NURON_FINISH,ENGINE_R_NOT_LOADED);
111 if(!DSO_free(pvDSOHandle))
113 ENGINEerr(ENGINE_F_NURON_FINISH,ENGINE_R_DSO_FAILURE);
121 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
122 const BIGNUM *m,BN_CTX *ctx)
126 ENGINEerr(ENGINE_F_NURON_MOD_EXP,ENGINE_R_NOT_LOADED);
129 return pfnModExp(r,a,p,m);
132 static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
134 return nuron_mod_exp(r0,I,rsa->d,rsa->n,NULL);
137 /* This code was liberated and adapted from the commented-out code in
138 * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
139 * (it doesn't have a CRT form for RSA), this function means that an
140 * Atalla system running with a DSA server certificate can handshake
141 * around 5 or 6 times faster/more than an equivalent system running with
142 * RSA. Just check out the "signs" statistics from the RSA and DSA parts
143 * of "openssl speed -engine atalla dsa1024 rsa1024". */
144 static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
145 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
146 BN_CTX *ctx, BN_MONT_CTX *in_mont)
152 /* let rr = a1 ^ p1 mod m */
153 if (!nuron_mod_exp(rr,a1,p1,m,ctx))
155 /* let t = a2 ^ p2 mod m */
156 if (!nuron_mod_exp(&t,a2,p2,m,ctx))
158 /* let rr = rr * t mod m */
159 if (!BN_mod_mul(rr,rr,&t,m,ctx))
168 static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
169 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
172 return nuron_mod_exp(r, a, p, m, ctx);
175 /* This function is aliased to mod_exp (with the mont stuff dropped). */
176 static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
177 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
179 return nuron_mod_exp(r, a, p, m, ctx);
182 /* This function is aliased to mod_exp (with the dh and mont dropped). */
183 static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
184 const BIGNUM *a, const BIGNUM *p,
185 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
187 return nuron_mod_exp(r, a, p, m, ctx);
190 static RSA_METHOD nuron_rsa =
207 static DSA_METHOD nuron_dsa =
210 NULL, /* dsa_do_sign */
211 NULL, /* dsa_sign_setup */
212 NULL, /* dsa_do_verify */
213 nuron_dsa_mod_exp, /* dsa_mod_exp */
214 nuron_mod_exp_dsa, /* bn_mod_exp */
221 static DH_METHOD nuron_dh =
233 static ENGINE engine_nuron =
236 "Nuron hardware engine support",
245 NULL, /* no ctrl() */
246 NULL, /* no load_privkey() */
247 NULL, /* no load_pubkey() */
249 0, 0, /* no references */
250 NULL, NULL /* unlinked */
253 /* As this is only ever called once, there's no need for locking
254 * (indeed - the lock will already be held by our caller!!!) */
255 ENGINE *ENGINE_nuron()
257 const RSA_METHOD *meth1;
258 const DSA_METHOD *meth2;
259 const DH_METHOD *meth3;
261 /* We know that the "PKCS1_SSLeay()" functions hook properly
262 * to the nuron-specific mod_exp and mod_exp_crt so we use
263 * those functions. NB: We don't use ENGINE_openssl() or
264 * anything "more generic" because something like the RSAref
265 * code may not hook properly, and if you own one of these
266 * cards then you have the right to do RSA operations on it
268 meth1=RSA_PKCS1_SSLeay();
269 nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc;
270 nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec;
271 nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc;
272 nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec;
274 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
277 nuron_dsa.dsa_do_sign=meth2->dsa_do_sign;
278 nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup;
279 nuron_dsa.dsa_do_verify=meth2->dsa_do_verify;
281 /* Much the same for Diffie-Hellman */
283 nuron_dh.generate_key=meth3->generate_key;
284 nuron_dh.compute_key=meth3->compute_key;
285 return &engine_nuron;
288 #endif /* !OPENSSL_NO_HW_NURON */
289 #endif /* !OPENSSL_NO_HW */