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).
61 #include <openssl/crypto.h>
62 #include <openssl/buffer.h>
63 #include <openssl/dso.h>
64 #include <openssl/engine.h>
65 #ifndef OPENSSL_NO_RSA
66 #include <openssl/rsa.h>
68 #ifndef OPENSSL_NO_DSA
69 #include <openssl/dsa.h>
72 #include <openssl/dh.h>
74 #include <openssl/bn.h>
77 #ifndef OPENSSL_NO_HW_ATALLA
82 #include "vendor_defns/atalla.h"
85 #define ATALLA_LIB_NAME "atalla engine"
86 #include "e_atalla_err.c"
88 static int atalla_destroy(ENGINE *e);
89 static int atalla_init(ENGINE *e);
90 static int atalla_finish(ENGINE *e);
91 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
94 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
95 const BIGNUM *m, BN_CTX *ctx);
97 #ifndef OPENSSL_NO_RSA
99 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
100 /* This function is aliased to mod_exp (with the mont stuff dropped). */
101 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
102 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
105 #ifndef OPENSSL_NO_DSA
107 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
108 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
109 BN_CTX *ctx, BN_MONT_CTX *in_mont);
110 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
111 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
115 #ifndef OPENSSL_NO_DH
117 /* This function is alised to mod_exp (with the DH and mont dropped). */
118 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
119 const BIGNUM *a, const BIGNUM *p,
120 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
123 /* The definitions for control commands specific to this engine */
124 #define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE
125 static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
128 "Specifies the path to the 'atasi' shared library",
129 ENGINE_CMD_FLAG_STRING},
133 #ifndef OPENSSL_NO_RSA
134 /* Our internal RSA_METHOD that we provide pointers to */
135 static RSA_METHOD atalla_rsa =
154 #ifndef OPENSSL_NO_DSA
155 /* Our internal DSA_METHOD that we provide pointers to */
156 static DSA_METHOD atalla_dsa =
159 NULL, /* dsa_do_sign */
160 NULL, /* dsa_sign_setup */
161 NULL, /* dsa_do_verify */
162 atalla_dsa_mod_exp, /* dsa_mod_exp */
163 atalla_mod_exp_dsa, /* bn_mod_exp */
168 NULL, /* dsa_paramgen */
169 NULL /* dsa_keygen */
173 #ifndef OPENSSL_NO_DH
174 /* Our internal DH_METHOD that we provide pointers to */
175 static DH_METHOD atalla_dh =
189 /* Constants used when creating the ENGINE */
190 static const char *engine_atalla_id = "atalla";
191 static const char *engine_atalla_name = "Atalla hardware engine support";
193 /* This internal function is used by ENGINE_atalla() and possibly by the
194 * "dynamic" ENGINE support too */
195 static int bind_helper(ENGINE *e)
197 #ifndef OPENSSL_NO_RSA
198 const RSA_METHOD *meth1;
200 #ifndef OPENSSL_NO_DSA
201 const DSA_METHOD *meth2;
203 #ifndef OPENSSL_NO_DH
204 const DH_METHOD *meth3;
206 if(!ENGINE_set_id(e, engine_atalla_id) ||
207 !ENGINE_set_name(e, engine_atalla_name) ||
208 #ifndef OPENSSL_NO_RSA
209 !ENGINE_set_RSA(e, &atalla_rsa) ||
211 #ifndef OPENSSL_NO_DSA
212 !ENGINE_set_DSA(e, &atalla_dsa) ||
214 #ifndef OPENSSL_NO_DH
215 !ENGINE_set_DH(e, &atalla_dh) ||
217 !ENGINE_set_destroy_function(e, atalla_destroy) ||
218 !ENGINE_set_init_function(e, atalla_init) ||
219 !ENGINE_set_finish_function(e, atalla_finish) ||
220 !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
221 !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
224 #ifndef OPENSSL_NO_RSA
225 /* We know that the "PKCS1_SSLeay()" functions hook properly
226 * to the atalla-specific mod_exp and mod_exp_crt so we use
227 * those functions. NB: We don't use ENGINE_openssl() or
228 * anything "more generic" because something like the RSAref
229 * code may not hook properly, and if you own one of these
230 * cards then you have the right to do RSA operations on it
232 meth1 = RSA_PKCS1_SSLeay();
233 atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
234 atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
235 atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
236 atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
239 #ifndef OPENSSL_NO_DSA
240 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
242 meth2 = DSA_OpenSSL();
243 atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
244 atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
245 atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
248 #ifndef OPENSSL_NO_DH
249 /* Much the same for Diffie-Hellman */
250 meth3 = DH_OpenSSL();
251 atalla_dh.generate_key = meth3->generate_key;
252 atalla_dh.compute_key = meth3->compute_key;
255 /* Ensure the atalla error handling is set up */
256 ERR_load_ATALLA_strings();
260 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
261 static ENGINE *engine_atalla(void)
263 ENGINE *ret = ENGINE_new();
266 if(!bind_helper(ret))
274 void ENGINE_load_atalla(void)
276 /* Copied from eng_[openssl|dyn].c */
277 ENGINE *toadd = engine_atalla();
285 /* This is a process-global DSO handle used for loading and unloading
286 * the Atalla library. NB: This is only set (or unset) during an
287 * init() or finish() call (reference counts permitting) and they're
288 * operating with global locks, so this should be thread-safe
290 static DSO *atalla_dso = NULL;
292 /* These are the function pointers that are (un)set when the library has
293 * successfully (un)loaded. */
294 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
295 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
296 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
298 /* These are the static string constants for the DSO file name and the function
299 * symbol names to bind to. Regrettably, the DSO name on *nix appears to be
300 * "atasi.so" rather than something more consistent like "libatasi.so". At the
301 * time of writing, I'm not sure what the file name on win32 is but clearly
302 * native name translation is not possible (eg libatasi.so on *nix, and
303 * atasi.dll on win32). For the purposes of testing, I have created a symbollic
304 * link called "libatasi.so" so that we can use native name-translation - a
305 * better solution will be needed. */
306 static const char *ATALLA_LIBNAME = NULL;
307 static const char *get_ATALLA_LIBNAME(void)
310 return ATALLA_LIBNAME;
313 static void free_ATALLA_LIBNAME(void)
316 OPENSSL_free((void*)ATALLA_LIBNAME);
317 ATALLA_LIBNAME = NULL;
319 static long set_ATALLA_LIBNAME(const char *name)
321 free_ATALLA_LIBNAME();
322 return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
324 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
325 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
326 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
328 /* Destructor (complements the "ENGINE_atalla()" constructor) */
329 static int atalla_destroy(ENGINE *e)
331 free_ATALLA_LIBNAME();
332 /* Unload the atalla error strings so any error state including our
333 * functs or reasons won't lead to a segfault (they simply get displayed
334 * without corresponding string data because none will be found). */
335 ERR_unload_ATALLA_strings();
339 /* (de)initialisation functions. */
340 static int atalla_init(ENGINE *e)
342 tfnASI_GetHardwareConfig *p1;
343 tfnASI_RSAPrivateKeyOpFn *p2;
344 tfnASI_GetPerformanceStatistics *p3;
345 /* Not sure of the origin of this magic value, but Ben's code had it
346 * and it seemed to have been working for a few people. :-) */
347 unsigned int config_buf[1024];
349 if(atalla_dso != NULL)
351 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED);
354 /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
355 * changed unfortunately because the Atalla drivers don't have
356 * standard library names that can be platform-translated well. */
357 /* TODO: Work out how to actually map to the names the Atalla
358 * drivers really use - for now a symbollic link needs to be
359 * created on the host system from libatasi.so to atasi.so on
361 atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0);
362 if(atalla_dso == NULL)
364 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
367 if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
368 atalla_dso, ATALLA_F1)) ||
369 !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
370 atalla_dso, ATALLA_F2)) ||
371 !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
372 atalla_dso, ATALLA_F3)))
374 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
377 /* Copy the pointers */
378 p_Atalla_GetHardwareConfig = p1;
379 p_Atalla_RSAPrivateKeyOpFn = p2;
380 p_Atalla_GetPerformanceStatistics = p3;
381 /* Perform a basic test to see if there's actually any unit
383 if(p1(0L, config_buf) != 0)
385 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE);
388 /* Everything's fine. */
392 DSO_free(atalla_dso);
394 p_Atalla_GetHardwareConfig = NULL;
395 p_Atalla_RSAPrivateKeyOpFn = NULL;
396 p_Atalla_GetPerformanceStatistics = NULL;
400 static int atalla_finish(ENGINE *e)
402 free_ATALLA_LIBNAME();
403 if(atalla_dso == NULL)
405 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED);
408 if(!DSO_free(atalla_dso))
410 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE);
414 p_Atalla_GetHardwareConfig = NULL;
415 p_Atalla_RSAPrivateKeyOpFn = NULL;
416 p_Atalla_GetPerformanceStatistics = NULL;
420 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
422 int initialised = ((atalla_dso == NULL) ? 0 : 1);
425 case ATALLA_CMD_SO_PATH:
428 ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER);
433 ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED);
436 return set_ATALLA_LIBNAME((const char *)p);
440 ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
444 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
445 const BIGNUM *m, BN_CTX *ctx)
447 /* I need somewhere to store temporary serialised values for
448 * use with the Atalla API calls. A neat cheat - I'll use
449 * BIGNUMs from the BN_CTX but access their arrays directly as
450 * byte arrays <grin>. This way I don't have to clean anything
456 RSAPrivateKey keydata;
457 int to_return, numbytes;
459 modulus = exponent = argument = result = NULL;
460 to_return = 0; /* expect failure */
464 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);
467 /* Prepare the params */
469 modulus = BN_CTX_get(ctx);
470 exponent = BN_CTX_get(ctx);
471 argument = BN_CTX_get(ctx);
472 result = BN_CTX_get(ctx);
475 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);
478 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
479 !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
481 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);
484 /* Prepare the key-data */
485 memset(&keydata, 0,sizeof keydata);
486 numbytes = BN_num_bytes(m);
487 memset(exponent->d, 0, numbytes);
488 memset(modulus->d, 0, numbytes);
489 BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
490 BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
491 keydata.privateExponent.data = (unsigned char *)exponent->d;
492 keydata.privateExponent.len = numbytes;
493 keydata.modulus.data = (unsigned char *)modulus->d;
494 keydata.modulus.len = numbytes;
495 /* Prepare the argument */
496 memset(argument->d, 0, numbytes);
497 memset(result->d, 0, numbytes);
498 BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
499 /* Perform the operation */
500 if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
501 (unsigned char *)argument->d,
502 keydata.modulus.len) != 0)
504 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);
507 /* Convert the response */
508 BN_bin2bn((unsigned char *)result->d, numbytes, r);
515 #ifndef OPENSSL_NO_RSA
516 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
522 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED);
525 if(!rsa->d || !rsa->n)
527 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS);
530 to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
536 #ifndef OPENSSL_NO_DSA
537 /* This code was liberated and adapted from the commented-out code in
538 * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
539 * (it doesn't have a CRT form for RSA), this function means that an
540 * Atalla system running with a DSA server certificate can handshake
541 * around 5 or 6 times faster/more than an equivalent system running with
542 * RSA. Just check out the "signs" statistics from the RSA and DSA parts
543 * of "openssl speed -engine atalla dsa1024 rsa1024". */
544 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
545 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
546 BN_CTX *ctx, BN_MONT_CTX *in_mont)
552 /* let rr = a1 ^ p1 mod m */
553 if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
554 /* let t = a2 ^ p2 mod m */
555 if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
556 /* let rr = rr * t mod m */
557 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
564 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
565 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
568 return atalla_mod_exp(r, a, p, m, ctx);
572 #ifndef OPENSSL_NO_RSA
573 /* This function is aliased to mod_exp (with the mont stuff dropped). */
574 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
575 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
577 return atalla_mod_exp(r, a, p, m, ctx);
581 #ifndef OPENSSL_NO_DH
582 /* This function is aliased to mod_exp (with the dh and mont dropped). */
583 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
584 const BIGNUM *a, const BIGNUM *p,
585 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
587 return atalla_mod_exp(r, a, p, m, ctx);
591 /* This stuff is needed if this ENGINE is being compiled into a self-contained
593 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
594 static int bind_fn(ENGINE *e, const char *id)
596 if(id && (strcmp(id, engine_atalla_id) != 0))
602 IMPLEMENT_DYNAMIC_CHECK_FN()
603 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
604 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
606 #endif /* !OPENSSL_NO_HW_ATALLA */
607 #endif /* !OPENSSL_NO_HW */