1 /* crypto/engine/hw_atalla.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-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).
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 #define ATALLA_LIB_NAME "atalla engine"
75 #include "hw_atalla_err.c"
77 static int atalla_destroy(ENGINE *e);
78 static int atalla_init(ENGINE *e);
79 static int atalla_finish(ENGINE *e);
80 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
83 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
84 const BIGNUM *m, BN_CTX *ctx);
86 #ifndef OPENSSL_NO_RSA
88 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
90 /* This function is aliased to mod_exp (with the mont stuff dropped). */
91 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
92 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
94 #ifndef OPENSSL_NO_DSA
96 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
97 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
98 BN_CTX *ctx, BN_MONT_CTX *in_mont);
99 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
100 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
104 #ifndef OPENSSL_NO_DH
106 /* This function is alised to mod_exp (with the DH and mont dropped). */
107 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
108 const BIGNUM *a, const BIGNUM *p,
109 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
112 /* The definitions for control commands specific to this engine */
113 #define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE
114 static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
117 "Specifies the path to the 'atasi' shared library",
118 ENGINE_CMD_FLAG_STRING},
122 #ifndef OPENSSL_NO_RSA
123 /* Our internal RSA_METHOD that we provide pointers to */
124 static RSA_METHOD atalla_rsa =
142 #ifndef OPENSSL_NO_DSA
143 /* Our internal DSA_METHOD that we provide pointers to */
144 static DSA_METHOD atalla_dsa =
147 NULL, /* dsa_do_sign */
148 NULL, /* dsa_sign_setup */
149 NULL, /* dsa_do_verify */
150 atalla_dsa_mod_exp, /* dsa_mod_exp */
151 atalla_mod_exp_dsa, /* bn_mod_exp */
159 #ifndef OPENSSL_NO_DH
160 /* Our internal DH_METHOD that we provide pointers to */
161 static DH_METHOD atalla_dh =
174 /* Constants used when creating the ENGINE */
175 static const char *engine_atalla_id = "atalla";
176 static const char *engine_atalla_name = "Atalla hardware engine support";
178 /* This internal function is used by ENGINE_atalla() and possibly by the
179 * "dynamic" ENGINE support too */
180 static int bind_helper(ENGINE *e)
182 #ifndef OPENSSL_NO_RSA
183 const RSA_METHOD *meth1;
185 #ifndef OPENSSL_NO_DSA
186 const DSA_METHOD *meth2;
188 #ifndef OPENSSL_NO_DH
189 const DH_METHOD *meth3;
191 if(!ENGINE_set_id(e, engine_atalla_id) ||
192 !ENGINE_set_name(e, engine_atalla_name) ||
193 #ifndef OPENSSL_NO_RSA
194 !ENGINE_set_RSA(e, &atalla_rsa) ||
196 #ifndef OPENSSL_NO_DSA
197 !ENGINE_set_DSA(e, &atalla_dsa) ||
199 #ifndef OPENSSL_NO_DH
200 !ENGINE_set_DH(e, &atalla_dh) ||
202 !ENGINE_set_destroy_function(e, atalla_destroy) ||
203 !ENGINE_set_init_function(e, atalla_init) ||
204 !ENGINE_set_finish_function(e, atalla_finish) ||
205 !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
206 !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
209 #ifndef OPENSSL_NO_RSA
210 /* We know that the "PKCS1_SSLeay()" functions hook properly
211 * to the atalla-specific mod_exp and mod_exp_crt so we use
212 * those functions. NB: We don't use ENGINE_openssl() or
213 * anything "more generic" because something like the RSAref
214 * code may not hook properly, and if you own one of these
215 * cards then you have the right to do RSA operations on it
217 meth1 = RSA_PKCS1_SSLeay();
218 atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
219 atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
220 atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
221 atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
224 #ifndef OPENSSL_NO_DSA
225 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
227 meth2 = DSA_OpenSSL();
228 atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
229 atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
230 atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
233 #ifndef OPENSSL_NO_DH
234 /* Much the same for Diffie-Hellman */
235 meth3 = DH_OpenSSL();
236 atalla_dh.generate_key = meth3->generate_key;
237 atalla_dh.compute_key = meth3->compute_key;
240 /* Ensure the atalla error handling is set up */
241 ERR_load_ATALLA_strings();
245 static ENGINE *engine_atalla(void)
247 ENGINE *ret = ENGINE_new();
250 if(!bind_helper(ret))
258 void ENGINE_load_atalla(void)
260 /* Copied from eng_[openssl|dyn].c */
261 ENGINE *toadd = engine_atalla();
268 /* This is a process-global DSO handle used for loading and unloading
269 * the Atalla library. NB: This is only set (or unset) during an
270 * init() or finish() call (reference counts permitting) and they're
271 * operating with global locks, so this should be thread-safe
273 static DSO *atalla_dso = NULL;
275 /* These are the function pointers that are (un)set when the library has
276 * successfully (un)loaded. */
277 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
278 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
279 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
281 /* These are the static string constants for the DSO file name and the function
282 * symbol names to bind to. Regrettably, the DSO name on *nix appears to be
283 * "atasi.so" rather than something more consistent like "libatasi.so". At the
284 * time of writing, I'm not sure what the file name on win32 is but clearly
285 * native name translation is not possible (eg libatasi.so on *nix, and
286 * atasi.dll on win32). For the purposes of testing, I have created a symbollic
287 * link called "libatasi.so" so that we can use native name-translation - a
288 * better solution will be needed. */
289 static const char def_ATALLA_LIBNAME[] = "atasi";
290 static const char *ATALLA_LIBNAME = def_ATALLA_LIBNAME;
291 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
292 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
293 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
295 /* Destructor (complements the "ENGINE_atalla()" constructor) */
296 static int atalla_destroy(ENGINE *e)
298 /* Unload the atalla error strings so any error state including our
299 * functs or reasons won't lead to a segfault (they simply get displayed
300 * without corresponding string data because none will be found). */
301 ERR_unload_ATALLA_strings();
305 /* (de)initialisation functions. */
306 static int atalla_init(ENGINE *e)
308 tfnASI_GetHardwareConfig *p1;
309 tfnASI_RSAPrivateKeyOpFn *p2;
310 tfnASI_GetPerformanceStatistics *p3;
311 /* Not sure of the origin of this magic value, but Ben's code had it
312 * and it seemed to have been working for a few people. :-) */
313 unsigned int config_buf[1024];
315 if(atalla_dso != NULL)
317 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED);
320 /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
321 * changed unfortunately because the Atalla drivers don't have
322 * standard library names that can be platform-translated well. */
323 /* TODO: Work out how to actually map to the names the Atalla
324 * drivers really use - for now a symbollic link needs to be
325 * created on the host system from libatasi.so to atasi.so on
327 atalla_dso = DSO_load(NULL, ATALLA_LIBNAME, NULL, 0);
328 if(atalla_dso == NULL)
330 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
333 if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
334 atalla_dso, ATALLA_F1)) ||
335 !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
336 atalla_dso, ATALLA_F2)) ||
337 !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
338 atalla_dso, ATALLA_F3)))
340 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
343 /* Copy the pointers */
344 p_Atalla_GetHardwareConfig = p1;
345 p_Atalla_RSAPrivateKeyOpFn = p2;
346 p_Atalla_GetPerformanceStatistics = p3;
347 /* Perform a basic test to see if there's actually any unit
349 if(p1(0L, config_buf) != 0)
351 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE);
354 /* Everything's fine. */
358 DSO_free(atalla_dso);
359 p_Atalla_GetHardwareConfig = NULL;
360 p_Atalla_RSAPrivateKeyOpFn = NULL;
361 p_Atalla_GetPerformanceStatistics = NULL;
365 static int atalla_finish(ENGINE *e)
367 if(atalla_dso == NULL)
369 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED);
372 if(!DSO_free(atalla_dso))
374 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE);
378 p_Atalla_GetHardwareConfig = NULL;
379 p_Atalla_RSAPrivateKeyOpFn = NULL;
380 p_Atalla_GetPerformanceStatistics = NULL;
384 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
386 int initialised = ((atalla_dso == NULL) ? 0 : 1);
389 case ATALLA_CMD_SO_PATH:
392 ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER);
397 ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED);
400 ATALLA_LIBNAME = (const char *)p;
405 ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
409 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
410 const BIGNUM *m, BN_CTX *ctx)
412 /* I need somewhere to store temporary serialised values for
413 * use with the Atalla API calls. A neat cheat - I'll use
414 * BIGNUMs from the BN_CTX but access their arrays directly as
415 * byte arrays <grin>. This way I don't have to clean anything
421 RSAPrivateKey keydata;
422 int to_return, numbytes;
424 modulus = exponent = argument = result = NULL;
425 to_return = 0; /* expect failure */
429 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);
432 /* Prepare the params */
434 modulus = BN_CTX_get(ctx);
435 exponent = BN_CTX_get(ctx);
436 argument = BN_CTX_get(ctx);
437 result = BN_CTX_get(ctx);
440 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);
443 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
444 !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
446 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);
449 /* Prepare the key-data */
450 memset(&keydata, 0,sizeof keydata);
451 numbytes = BN_num_bytes(m);
452 memset(exponent->d, 0, numbytes);
453 memset(modulus->d, 0, numbytes);
454 BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
455 BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
456 keydata.privateExponent.data = (unsigned char *)exponent->d;
457 keydata.privateExponent.len = numbytes;
458 keydata.modulus.data = (unsigned char *)modulus->d;
459 keydata.modulus.len = numbytes;
460 /* Prepare the argument */
461 memset(argument->d, 0, numbytes);
462 memset(result->d, 0, numbytes);
463 BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
464 /* Perform the operation */
465 if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
466 (unsigned char *)argument->d,
467 keydata.modulus.len) != 0)
469 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);
472 /* Convert the response */
473 BN_bin2bn((unsigned char *)result->d, numbytes, r);
480 #ifndef OPENSSL_NO_RSA
481 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
488 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED);
491 if((ctx = BN_CTX_new()) == NULL)
493 if(!rsa->d || !rsa->n)
495 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS);
498 to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
506 #ifndef OPENSSL_NO_DSA
507 /* This code was liberated and adapted from the commented-out code in
508 * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
509 * (it doesn't have a CRT form for RSA), this function means that an
510 * Atalla system running with a DSA server certificate can handshake
511 * around 5 or 6 times faster/more than an equivalent system running with
512 * RSA. Just check out the "signs" statistics from the RSA and DSA parts
513 * of "openssl speed -engine atalla dsa1024 rsa1024". */
514 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
515 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
516 BN_CTX *ctx, BN_MONT_CTX *in_mont)
522 /* let rr = a1 ^ p1 mod m */
523 if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
524 /* let t = a2 ^ p2 mod m */
525 if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
526 /* let rr = rr * t mod m */
527 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
534 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
535 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
538 return atalla_mod_exp(r, a, p, m, ctx);
542 /* This function is aliased to mod_exp (with the mont stuff dropped). */
543 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
544 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
546 return atalla_mod_exp(r, a, p, m, ctx);
549 #ifndef OPENSSL_NO_DH
550 /* This function is aliased to mod_exp (with the dh and mont dropped). */
551 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
552 const BIGNUM *a, const BIGNUM *p,
553 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
555 return atalla_mod_exp(r, a, p, m, ctx);
559 /* This stuff is needed if this ENGINE is being compiled into a self-contained
561 #ifdef ENGINE_DYNAMIC_SUPPORT
562 static int bind_fn(ENGINE *e, const char *id)
564 if(id && (strcmp(id, engine_atalla_id) != 0))
570 IMPLEMENT_DYNAMIC_CHECK_FN()
571 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
572 #endif /* ENGINE_DYNAMIC_SUPPORT */
574 #endif /* !OPENSSL_NO_HW_ATALLA */
575 #endif /* !OPENSSL_NO_HW */