1 /* crypto/engine/hw_atalla.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 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 <openssl/engine.h>
66 #ifndef OPENSSL_NO_HW_ATALLA
71 #include "vendor_defns/atalla.h"
74 static int atalla_init(ENGINE *e);
75 static int atalla_finish(ENGINE *e);
76 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
79 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
80 const BIGNUM *m, BN_CTX *ctx);
82 #ifndef OPENSSL_NO_RSA
84 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
86 /* This function is aliased to mod_exp (with the mont stuff dropped). */
87 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
88 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
90 #ifndef OPENSSL_NO_DSA
92 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
93 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
94 BN_CTX *ctx, BN_MONT_CTX *in_mont);
95 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
96 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
100 #ifndef OPENSSL_NO_DH
102 /* This function is alised to mod_exp (with the DH and mont dropped). */
103 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
104 const BIGNUM *a, const BIGNUM *p,
105 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
108 /* The definitions for control commands specific to this engine */
109 #define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE
110 static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
113 "Specifies the path to the 'atasi' shared library",
114 ENGINE_CMD_FLAG_STRING},
118 #ifndef OPENSSL_NO_RSA
119 /* Our internal RSA_METHOD that we provide pointers to */
120 static RSA_METHOD atalla_rsa =
138 #ifndef OPENSSL_NO_DSA
139 /* Our internal DSA_METHOD that we provide pointers to */
140 static DSA_METHOD atalla_dsa =
143 NULL, /* dsa_do_sign */
144 NULL, /* dsa_sign_setup */
145 NULL, /* dsa_do_verify */
146 atalla_dsa_mod_exp, /* dsa_mod_exp */
147 atalla_mod_exp_dsa, /* bn_mod_exp */
155 #ifndef OPENSSL_NO_DH
156 /* Our internal DH_METHOD that we provide pointers to */
157 static DH_METHOD atalla_dh =
170 /* Constants used when creating the ENGINE */
171 static const char *engine_atalla_id = "atalla";
172 static const char *engine_atalla_name = "Atalla hardware engine support";
174 /* As this is only ever called once, there's no need for locking
175 * (indeed - the lock will already be held by our caller!!!) */
176 ENGINE *ENGINE_atalla()
178 #ifndef OPENSSL_NO_RSA
179 const RSA_METHOD *meth1;
181 #ifndef OPENSSL_NO_DSA
182 const DSA_METHOD *meth2;
184 #ifndef OPENSSL_NO_DH
185 const DH_METHOD *meth3;
187 ENGINE *ret = ENGINE_new();
190 if(!ENGINE_set_id(ret, engine_atalla_id) ||
191 !ENGINE_set_name(ret, engine_atalla_name) ||
192 #ifndef OPENSSL_NO_RSA
193 !ENGINE_set_RSA(ret, &atalla_rsa) ||
195 #ifndef OPENSSL_NO_DSA
196 !ENGINE_set_DSA(ret, &atalla_dsa) ||
198 #ifndef OPENSSL_NO_DH
199 !ENGINE_set_DH(ret, &atalla_dh) ||
201 !ENGINE_set_BN_mod_exp(ret, atalla_mod_exp) ||
202 !ENGINE_set_init_function(ret, atalla_init) ||
203 !ENGINE_set_finish_function(ret, atalla_finish) ||
204 !ENGINE_set_ctrl_function(ret, atalla_ctrl) ||
205 !ENGINE_set_cmd_defns(ret, atalla_cmd_defns))
211 #ifndef OPENSSL_NO_RSA
212 /* We know that the "PKCS1_SSLeay()" functions hook properly
213 * to the atalla-specific mod_exp and mod_exp_crt so we use
214 * those functions. NB: We don't use ENGINE_openssl() or
215 * anything "more generic" because something like the RSAref
216 * code may not hook properly, and if you own one of these
217 * cards then you have the right to do RSA operations on it
219 meth1 = RSA_PKCS1_SSLeay();
220 atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
221 atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
222 atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
223 atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
226 #ifndef OPENSSL_NO_DSA
227 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
229 meth2 = DSA_OpenSSL();
230 atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
231 atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
232 atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
235 #ifndef OPENSSL_NO_DH
236 /* Much the same for Diffie-Hellman */
237 meth3 = DH_OpenSSL();
238 atalla_dh.generate_key = meth3->generate_key;
239 atalla_dh.compute_key = meth3->compute_key;
244 /* This is a process-global DSO handle used for loading and unloading
245 * the Atalla library. NB: This is only set (or unset) during an
246 * init() or finish() call (reference counts permitting) and they're
247 * operating with global locks, so this should be thread-safe
249 static DSO *atalla_dso = NULL;
251 /* These are the function pointers that are (un)set when the library has
252 * successfully (un)loaded. */
253 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
254 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
255 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
257 /* These are the static string constants for the DSO file name and the function
258 * symbol names to bind to. Regrettably, the DSO name on *nix appears to be
259 * "atasi.so" rather than something more consistent like "libatasi.so". At the
260 * time of writing, I'm not sure what the file name on win32 is but clearly
261 * native name translation is not possible (eg libatasi.so on *nix, and
262 * atasi.dll on win32). For the purposes of testing, I have created a symbollic
263 * link called "libatasi.so" so that we can use native name-translation - a
264 * better solution will be needed. */
265 static const char def_ATALLA_LIBNAME[] = "atasi";
266 static const char *ATALLA_LIBNAME = def_ATALLA_LIBNAME;
267 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
268 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
269 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
271 /* (de)initialisation functions. */
272 static int atalla_init(ENGINE *e)
274 tfnASI_GetHardwareConfig *p1;
275 tfnASI_RSAPrivateKeyOpFn *p2;
276 tfnASI_GetPerformanceStatistics *p3;
277 /* Not sure of the origin of this magic value, but Ben's code had it
278 * and it seemed to have been working for a few people. :-) */
279 unsigned int config_buf[1024];
281 if(atalla_dso != NULL)
283 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_ALREADY_LOADED);
286 /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
287 * changed unfortunately because the Atalla drivers don't have
288 * standard library names that can be platform-translated well. */
289 /* TODO: Work out how to actually map to the names the Atalla
290 * drivers really use - for now a symbollic link needs to be
291 * created on the host system from libatasi.so to atasi.so on
293 atalla_dso = DSO_load(NULL, ATALLA_LIBNAME, NULL, 0);
294 if(atalla_dso == NULL)
296 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_DSO_FAILURE);
299 if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
300 atalla_dso, ATALLA_F1)) ||
301 !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
302 atalla_dso, ATALLA_F2)) ||
303 !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
304 atalla_dso, ATALLA_F3)))
306 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_DSO_FAILURE);
309 /* Copy the pointers */
310 p_Atalla_GetHardwareConfig = p1;
311 p_Atalla_RSAPrivateKeyOpFn = p2;
312 p_Atalla_GetPerformanceStatistics = p3;
313 /* Perform a basic test to see if there's actually any unit
315 if(p1(0L, config_buf) != 0)
317 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_UNIT_FAILURE);
320 /* Everything's fine. */
324 DSO_free(atalla_dso);
325 p_Atalla_GetHardwareConfig = NULL;
326 p_Atalla_RSAPrivateKeyOpFn = NULL;
327 p_Atalla_GetPerformanceStatistics = NULL;
331 static int atalla_finish(ENGINE *e)
333 if(atalla_dso == NULL)
335 ENGINEerr(ENGINE_F_ATALLA_FINISH,ENGINE_R_NOT_LOADED);
338 if(!DSO_free(atalla_dso))
340 ENGINEerr(ENGINE_F_ATALLA_FINISH,ENGINE_R_DSO_FAILURE);
344 p_Atalla_GetHardwareConfig = NULL;
345 p_Atalla_RSAPrivateKeyOpFn = NULL;
346 p_Atalla_GetPerformanceStatistics = NULL;
350 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
352 int initialised = ((atalla_dso == NULL) ? 0 : 1);
355 case ATALLA_CMD_SO_PATH:
358 ENGINEerr(ENGINE_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER);
363 ENGINEerr(ENGINE_F_ATALLA_CTRL,ENGINE_R_ALREADY_LOADED);
366 ATALLA_LIBNAME = (const char *)p;
371 ENGINEerr(ENGINE_F_ATALLA_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
375 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
376 const BIGNUM *m, BN_CTX *ctx)
378 /* I need somewhere to store temporary serialised values for
379 * use with the Atalla API calls. A neat cheat - I'll use
380 * BIGNUMs from the BN_CTX but access their arrays directly as
381 * byte arrays <grin>. This way I don't have to clean anything
387 RSAPrivateKey keydata;
388 int to_return, numbytes;
390 modulus = exponent = argument = result = NULL;
391 to_return = 0; /* expect failure */
395 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_NOT_LOADED);
398 /* Prepare the params */
400 modulus = BN_CTX_get(ctx);
401 exponent = BN_CTX_get(ctx);
402 argument = BN_CTX_get(ctx);
403 result = BN_CTX_get(ctx);
406 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_BN_CTX_FULL);
409 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
410 !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
412 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
415 /* Prepare the key-data */
416 memset(&keydata, 0,sizeof keydata);
417 numbytes = BN_num_bytes(m);
418 memset(exponent->d, 0, numbytes);
419 memset(modulus->d, 0, numbytes);
420 BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
421 BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
422 keydata.privateExponent.data = (unsigned char *)exponent->d;
423 keydata.privateExponent.len = numbytes;
424 keydata.modulus.data = (unsigned char *)modulus->d;
425 keydata.modulus.len = numbytes;
426 /* Prepare the argument */
427 memset(argument->d, 0, numbytes);
428 memset(result->d, 0, numbytes);
429 BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
430 /* Perform the operation */
431 if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
432 (unsigned char *)argument->d,
433 keydata.modulus.len) != 0)
435 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_REQUEST_FAILED);
439 /* Convert the response */
440 BN_bin2bn((unsigned char *)result->d, numbytes, r);
447 #ifndef OPENSSL_NO_RSA
448 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
455 ENGINEerr(ENGINE_F_ATALLA_RSA_MOD_EXP,ENGINE_R_NOT_LOADED);
458 if((ctx = BN_CTX_new()) == NULL)
460 if(!rsa->d || !rsa->n)
462 ENGINEerr(ENGINE_F_ATALLA_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
465 to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
473 #ifndef OPENSSL_NO_DSA
474 /* This code was liberated and adapted from the commented-out code in
475 * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
476 * (it doesn't have a CRT form for RSA), this function means that an
477 * Atalla system running with a DSA server certificate can handshake
478 * around 5 or 6 times faster/more than an equivalent system running with
479 * RSA. Just check out the "signs" statistics from the RSA and DSA parts
480 * of "openssl speed -engine atalla dsa1024 rsa1024". */
481 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
482 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
483 BN_CTX *ctx, BN_MONT_CTX *in_mont)
489 /* let rr = a1 ^ p1 mod m */
490 if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
491 /* let t = a2 ^ p2 mod m */
492 if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
493 /* let rr = rr * t mod m */
494 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
501 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
502 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
505 return atalla_mod_exp(r, a, p, m, ctx);
509 /* This function is aliased to mod_exp (with the mont stuff dropped). */
510 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
511 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
513 return atalla_mod_exp(r, a, p, m, ctx);
516 #ifndef OPENSSL_NO_DH
517 /* This function is aliased to mod_exp (with the dh and mont dropped). */
518 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
519 const BIGNUM *a, const BIGNUM *p,
520 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
522 return atalla_mod_exp(r, a, p, m, ctx);
526 #endif /* !OPENSSL_NO_HW_ATALLA */
527 #endif /* !OPENSSL_NO_HW */