1 /* crypto/engine/hw_atalla.c */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/crypto.h>
63 #include <openssl/buffer.h>
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_RSA
67 # include <openssl/rsa.h>
69 #ifndef OPENSSL_NO_DSA
70 # include <openssl/dsa.h>
73 # include <openssl/dh.h>
75 #include <openssl/bn.h>
78 # ifndef OPENSSL_NO_HW_ATALLA
83 # include "vendor_defns/atalla.h"
86 # define ATALLA_LIB_NAME "atalla engine"
87 # include "e_atalla_err.c"
89 static int atalla_destroy(ENGINE
*e
);
90 static int atalla_init(ENGINE
*e
);
91 static int atalla_finish(ENGINE
*e
);
92 static int atalla_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void));
95 static int atalla_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
96 const BIGNUM
*m
, BN_CTX
*ctx
);
98 # ifndef OPENSSL_NO_RSA
100 static int atalla_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
,
102 /* This function is aliased to mod_exp (with the mont stuff dropped). */
103 static int atalla_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
104 const BIGNUM
*m
, BN_CTX
*ctx
,
108 # ifndef OPENSSL_NO_DSA
110 static int atalla_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
111 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
, BIGNUM
*m
,
112 BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
);
113 static int atalla_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
114 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
118 # ifndef OPENSSL_NO_DH
120 /* This function is alised to mod_exp (with the DH and mont dropped). */
121 static int atalla_mod_exp_dh(const DH
*dh
, BIGNUM
*r
,
122 const BIGNUM
*a
, const BIGNUM
*p
,
123 const BIGNUM
*m
, BN_CTX
*ctx
,
127 /* The definitions for control commands specific to this engine */
128 # define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE
129 static const ENGINE_CMD_DEFN atalla_cmd_defns
[] = {
132 "Specifies the path to the 'atasi' shared library",
133 ENGINE_CMD_FLAG_STRING
},
137 # ifndef OPENSSL_NO_RSA
138 /* Our internal RSA_METHOD that we provide pointers to */
139 static RSA_METHOD atalla_rsa
= {
157 # ifndef OPENSSL_NO_DSA
158 /* Our internal DSA_METHOD that we provide pointers to */
159 static DSA_METHOD atalla_dsa
= {
161 NULL
, /* dsa_do_sign */
162 NULL
, /* dsa_sign_setup */
163 NULL
, /* dsa_do_verify */
164 atalla_dsa_mod_exp
, /* dsa_mod_exp */
165 atalla_mod_exp_dsa
, /* bn_mod_exp */
170 NULL
, /* dsa_paramgen */
171 NULL
/* dsa_keygen */
175 # ifndef OPENSSL_NO_DH
176 /* Our internal DH_METHOD that we provide pointers to */
177 static DH_METHOD atalla_dh
= {
190 /* Constants used when creating the ENGINE */
191 static const char *engine_atalla_id
= "atalla";
192 static const char *engine_atalla_name
= "Atalla hardware engine support";
195 * This internal function is used by ENGINE_atalla() and possibly by the
196 * "dynamic" ENGINE support too
198 static int bind_helper(ENGINE
*e
)
200 # ifndef OPENSSL_NO_RSA
201 const RSA_METHOD
*meth1
;
203 # ifndef OPENSSL_NO_DSA
204 const DSA_METHOD
*meth2
;
206 # ifndef OPENSSL_NO_DH
207 const DH_METHOD
*meth3
;
209 if (!ENGINE_set_id(e
, engine_atalla_id
) ||
210 !ENGINE_set_name(e
, engine_atalla_name
) ||
211 # ifndef OPENSSL_NO_RSA
212 !ENGINE_set_RSA(e
, &atalla_rsa
) ||
214 # ifndef OPENSSL_NO_DSA
215 !ENGINE_set_DSA(e
, &atalla_dsa
) ||
217 # ifndef OPENSSL_NO_DH
218 !ENGINE_set_DH(e
, &atalla_dh
) ||
220 !ENGINE_set_destroy_function(e
, atalla_destroy
) ||
221 !ENGINE_set_init_function(e
, atalla_init
) ||
222 !ENGINE_set_finish_function(e
, atalla_finish
) ||
223 !ENGINE_set_ctrl_function(e
, atalla_ctrl
) ||
224 !ENGINE_set_cmd_defns(e
, atalla_cmd_defns
))
227 # ifndef OPENSSL_NO_RSA
229 * We know that the "PKCS1_SSLeay()" functions hook properly to the
230 * atalla-specific mod_exp and mod_exp_crt so we use those functions. NB:
231 * We don't use ENGINE_openssl() or anything "more generic" because
232 * something like the RSAref code may not hook properly, and if you own
233 * one of these cards then you have the right to do RSA operations on it
236 meth1
= RSA_PKCS1_SSLeay();
237 atalla_rsa
.rsa_pub_enc
= meth1
->rsa_pub_enc
;
238 atalla_rsa
.rsa_pub_dec
= meth1
->rsa_pub_dec
;
239 atalla_rsa
.rsa_priv_enc
= meth1
->rsa_priv_enc
;
240 atalla_rsa
.rsa_priv_dec
= meth1
->rsa_priv_dec
;
243 # ifndef OPENSSL_NO_DSA
245 * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
247 meth2
= DSA_OpenSSL();
248 atalla_dsa
.dsa_do_sign
= meth2
->dsa_do_sign
;
249 atalla_dsa
.dsa_sign_setup
= meth2
->dsa_sign_setup
;
250 atalla_dsa
.dsa_do_verify
= meth2
->dsa_do_verify
;
253 # ifndef OPENSSL_NO_DH
254 /* Much the same for Diffie-Hellman */
255 meth3
= DH_OpenSSL();
256 atalla_dh
.generate_key
= meth3
->generate_key
;
257 atalla_dh
.compute_key
= meth3
->compute_key
;
260 /* Ensure the atalla error handling is set up */
261 ERR_load_ATALLA_strings();
265 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
266 static ENGINE
*engine_atalla(void)
268 ENGINE
*ret
= ENGINE_new();
271 if (!bind_helper(ret
)) {
278 void ENGINE_load_atalla(void)
280 /* Copied from eng_[openssl|dyn].c */
281 ENGINE
*toadd
= engine_atalla();
291 * This is a process-global DSO handle used for loading and unloading the
292 * Atalla library. NB: This is only set (or unset) during an init() or
293 * finish() call (reference counts permitting) and they're operating with
294 * global locks, so this should be thread-safe implicitly.
296 static DSO
*atalla_dso
= NULL
;
299 * These are the function pointers that are (un)set when the library has
300 * successfully (un)loaded.
302 static tfnASI_GetHardwareConfig
*p_Atalla_GetHardwareConfig
= NULL
;
303 static tfnASI_RSAPrivateKeyOpFn
*p_Atalla_RSAPrivateKeyOpFn
= NULL
;
304 static tfnASI_GetPerformanceStatistics
*p_Atalla_GetPerformanceStatistics
=
308 * These are the static string constants for the DSO file name and the
309 * function symbol names to bind to. Regrettably, the DSO name on *nix
310 * appears to be "atasi.so" rather than something more consistent like
311 * "libatasi.so". At the time of writing, I'm not sure what the file name on
312 * win32 is but clearly native name translation is not possible (eg
313 * libatasi.so on *nix, and atasi.dll on win32). For the purposes of testing,
314 * I have created a symbollic link called "libatasi.so" so that we can use
315 * native name-translation - a better solution will be needed.
317 static const char *ATALLA_LIBNAME
= NULL
;
318 static const char *get_ATALLA_LIBNAME(void)
321 return ATALLA_LIBNAME
;
325 static void free_ATALLA_LIBNAME(void)
328 OPENSSL_free((void *)ATALLA_LIBNAME
);
329 ATALLA_LIBNAME
= NULL
;
332 static long set_ATALLA_LIBNAME(const char *name
)
334 free_ATALLA_LIBNAME();
335 return (((ATALLA_LIBNAME
= BUF_strdup(name
)) != NULL
) ? 1 : 0);
338 static const char *ATALLA_F1
= "ASI_GetHardwareConfig";
339 static const char *ATALLA_F2
= "ASI_RSAPrivateKeyOpFn";
340 static const char *ATALLA_F3
= "ASI_GetPerformanceStatistics";
342 /* Destructor (complements the "ENGINE_atalla()" constructor) */
343 static int atalla_destroy(ENGINE
*e
)
345 free_ATALLA_LIBNAME();
347 * Unload the atalla error strings so any error state including our
348 * functs or reasons won't lead to a segfault (they simply get displayed
349 * without corresponding string data because none will be found).
351 ERR_unload_ATALLA_strings();
355 /* (de)initialisation functions. */
356 static int atalla_init(ENGINE
*e
)
358 tfnASI_GetHardwareConfig
*p1
;
359 tfnASI_RSAPrivateKeyOpFn
*p2
;
360 tfnASI_GetPerformanceStatistics
*p3
;
362 * Not sure of the origin of this magic value, but Ben's code had it and
363 * it seemed to have been working for a few people. :-)
365 unsigned int config_buf
[1024];
367 if (atalla_dso
!= NULL
) {
368 ATALLAerr(ATALLA_F_ATALLA_INIT
, ATALLA_R_ALREADY_LOADED
);
372 * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed
373 * unfortunately because the Atalla drivers don't have standard library
374 * names that can be platform-translated well.
377 * TODO: Work out how to actually map to the names the Atalla drivers
378 * really use - for now a symbollic link needs to be created on the host
379 * system from libatasi.so to atasi.so on unix variants.
381 atalla_dso
= DSO_load(NULL
, get_ATALLA_LIBNAME(), NULL
, 0);
382 if (atalla_dso
== NULL
) {
383 ATALLAerr(ATALLA_F_ATALLA_INIT
, ATALLA_R_NOT_LOADED
);
388 (tfnASI_GetHardwareConfig
*) DSO_bind_func(atalla_dso
, ATALLA_F1
))
389 || !(p2
= (tfnASI_RSAPrivateKeyOpFn
*) DSO_bind_func(atalla_dso
, ATALLA_F2
))
391 (tfnASI_GetPerformanceStatistics
*) DSO_bind_func(atalla_dso
,
393 ATALLAerr(ATALLA_F_ATALLA_INIT
, ATALLA_R_NOT_LOADED
);
396 /* Copy the pointers */
397 p_Atalla_GetHardwareConfig
= p1
;
398 p_Atalla_RSAPrivateKeyOpFn
= p2
;
399 p_Atalla_GetPerformanceStatistics
= p3
;
401 * Perform a basic test to see if there's actually any unit running.
403 if (p1(0L, config_buf
) != 0) {
404 ATALLAerr(ATALLA_F_ATALLA_INIT
, ATALLA_R_UNIT_FAILURE
);
407 /* Everything's fine. */
411 DSO_free(atalla_dso
);
413 p_Atalla_GetHardwareConfig
= NULL
;
414 p_Atalla_RSAPrivateKeyOpFn
= NULL
;
415 p_Atalla_GetPerformanceStatistics
= NULL
;
419 static int atalla_finish(ENGINE
*e
)
421 free_ATALLA_LIBNAME();
422 if (atalla_dso
== NULL
) {
423 ATALLAerr(ATALLA_F_ATALLA_FINISH
, ATALLA_R_NOT_LOADED
);
426 if (!DSO_free(atalla_dso
)) {
427 ATALLAerr(ATALLA_F_ATALLA_FINISH
, ATALLA_R_UNIT_FAILURE
);
431 p_Atalla_GetHardwareConfig
= NULL
;
432 p_Atalla_RSAPrivateKeyOpFn
= NULL
;
433 p_Atalla_GetPerformanceStatistics
= NULL
;
437 static int atalla_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void))
439 int initialised
= ((atalla_dso
== NULL
) ? 0 : 1);
441 case ATALLA_CMD_SO_PATH
:
443 ATALLAerr(ATALLA_F_ATALLA_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
447 ATALLAerr(ATALLA_F_ATALLA_CTRL
, ATALLA_R_ALREADY_LOADED
);
450 return set_ATALLA_LIBNAME((const char *)p
);
454 ATALLAerr(ATALLA_F_ATALLA_CTRL
, ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
458 static int atalla_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
459 const BIGNUM
*m
, BN_CTX
*ctx
)
462 * I need somewhere to store temporary serialised values for use with the
463 * Atalla API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
464 * access their arrays directly as byte arrays <grin>. This way I don't
465 * have to clean anything up.
471 RSAPrivateKey keydata
;
472 int to_return
, numbytes
;
474 modulus
= exponent
= argument
= result
= NULL
;
475 to_return
= 0; /* expect failure */
478 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP
, ATALLA_R_NOT_LOADED
);
481 /* Prepare the params */
483 modulus
= BN_CTX_get(ctx
);
484 exponent
= BN_CTX_get(ctx
);
485 argument
= BN_CTX_get(ctx
);
486 result
= BN_CTX_get(ctx
);
488 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP
, ATALLA_R_BN_CTX_FULL
);
491 if (!bn_wexpand(modulus
, m
->top
) || !bn_wexpand(exponent
, m
->top
) ||
492 !bn_wexpand(argument
, m
->top
) || !bn_wexpand(result
, m
->top
)) {
493 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP
, ATALLA_R_BN_EXPAND_FAIL
);
496 /* Prepare the key-data */
497 memset(&keydata
, 0, sizeof keydata
);
498 numbytes
= BN_num_bytes(m
);
499 memset(exponent
->d
, 0, numbytes
);
500 memset(modulus
->d
, 0, numbytes
);
501 BN_bn2bin(p
, (unsigned char *)exponent
->d
+ numbytes
- BN_num_bytes(p
));
502 BN_bn2bin(m
, (unsigned char *)modulus
->d
+ numbytes
- BN_num_bytes(m
));
503 keydata
.privateExponent
.data
= (unsigned char *)exponent
->d
;
504 keydata
.privateExponent
.len
= numbytes
;
505 keydata
.modulus
.data
= (unsigned char *)modulus
->d
;
506 keydata
.modulus
.len
= numbytes
;
507 /* Prepare the argument */
508 memset(argument
->d
, 0, numbytes
);
509 memset(result
->d
, 0, numbytes
);
510 BN_bn2bin(a
, (unsigned char *)argument
->d
+ numbytes
- BN_num_bytes(a
));
511 /* Perform the operation */
512 if (p_Atalla_RSAPrivateKeyOpFn(&keydata
, (unsigned char *)result
->d
,
513 (unsigned char *)argument
->d
,
514 keydata
.modulus
.len
) != 0) {
515 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP
, ATALLA_R_REQUEST_FAILED
);
518 /* Convert the response */
519 BN_bin2bn((unsigned char *)result
->d
, numbytes
, r
);
526 # ifndef OPENSSL_NO_RSA
527 static int atalla_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
,
533 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP
, ATALLA_R_NOT_LOADED
);
536 if (!rsa
->d
|| !rsa
->n
) {
537 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP
,
538 ATALLA_R_MISSING_KEY_COMPONENTS
);
541 to_return
= atalla_mod_exp(r0
, I
, rsa
->d
, rsa
->n
, ctx
);
547 # ifndef OPENSSL_NO_DSA
549 * This code was liberated and adapted from the commented-out code in
550 * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration (it
551 * doesn't have a CRT form for RSA), this function means that an Atalla
552 * system running with a DSA server certificate can handshake around 5 or 6
553 * times faster/more than an equivalent system running with RSA. Just check
554 * out the "signs" statistics from the RSA and DSA parts of "openssl speed
555 * -engine atalla dsa1024 rsa1024".
557 static int atalla_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
558 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
, BIGNUM
*m
,
559 BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
565 /* let rr = a1 ^ p1 mod m */
566 if (!atalla_mod_exp(rr
, a1
, p1
, m
, ctx
))
568 /* let t = a2 ^ p2 mod m */
569 if (!atalla_mod_exp(&t
, a2
, p2
, m
, ctx
))
571 /* let rr = rr * t mod m */
572 if (!BN_mod_mul(rr
, rr
, &t
, m
, ctx
))
580 static int atalla_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
581 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
584 return atalla_mod_exp(r
, a
, p
, m
, ctx
);
588 # ifndef OPENSSL_NO_RSA
589 /* This function is aliased to mod_exp (with the mont stuff dropped). */
590 static int atalla_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
591 const BIGNUM
*m
, BN_CTX
*ctx
,
594 return atalla_mod_exp(r
, a
, p
, m
, ctx
);
598 # ifndef OPENSSL_NO_DH
599 /* This function is aliased to mod_exp (with the dh and mont dropped). */
600 static int atalla_mod_exp_dh(const DH
*dh
, BIGNUM
*r
,
601 const BIGNUM
*a
, const BIGNUM
*p
,
602 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
604 return atalla_mod_exp(r
, a
, p
, m
, ctx
);
609 * This stuff is needed if this ENGINE is being compiled into a
610 * self-contained shared-library.
612 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
613 static int bind_fn(ENGINE
*e
, const char *id
)
615 if (id
&& (strcmp(id
, engine_atalla_id
) != 0))
622 IMPLEMENT_DYNAMIC_CHECK_FN()
623 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
624 # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
625 # endif /* !OPENSSL_NO_HW_ATALLA */
626 #endif /* !OPENSSL_NO_HW */