summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/engine/hw_ubsec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/engine/hw_ubsec.c')
-rw-r--r--src/lib/libcrypto/engine/hw_ubsec.c1061
1 files changed, 0 insertions, 1061 deletions
diff --git a/src/lib/libcrypto/engine/hw_ubsec.c b/src/lib/libcrypto/engine/hw_ubsec.c
deleted file mode 100644
index 8fb834af31..0000000000
--- a/src/lib/libcrypto/engine/hw_ubsec.c
+++ /dev/null
@@ -1,1061 +0,0 @@
1/* crypto/engine/hw_ubsec.c */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 *
5 * Cloned shamelessly by Joe Tardo.
6 */
7/* ====================================================================
8 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 *
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * licensing@OpenSSL.org.
31 *
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
35 *
36 * 6. Redistributions of any form whatsoever must retain the following
37 * acknowledgment:
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
58 *
59 */
60
61#include <stdio.h>
62#include <openssl/crypto.h>
63#include "cryptlib.h"
64#include <openssl/dso.h>
65#include <openssl/engine.h>
66
67#ifndef OPENSSL_NO_HW
68#ifndef OPENSSL_NO_HW_UBSEC
69
70#ifdef FLAT_INC
71#include "hw_ubsec.h"
72#else
73#include "vendor_defns/hw_ubsec.h"
74#endif
75
76#define UBSEC_LIB_NAME "ubsec engine"
77#include "hw_ubsec_err.c"
78
79#define FAIL_TO_SOFTWARE -15
80
81static int ubsec_destroy(ENGINE *e);
82static int ubsec_init(ENGINE *e);
83static int ubsec_finish(ENGINE *e);
84static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
85static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
86 const BIGNUM *m, BN_CTX *ctx);
87static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
88 const BIGNUM *q, const BIGNUM *dp,
89 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
90#ifndef OPENSSL_NO_RSA
91static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
92#endif
93static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
94 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
95#ifndef OPENSSL_NO_DSA
96#ifdef NOT_USED
97static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
98 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
99 BN_CTX *ctx, BN_MONT_CTX *in_mont);
100static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
101 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
102 BN_MONT_CTX *m_ctx);
103#endif
104static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
105static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
106 DSA_SIG *sig, DSA *dsa);
107#endif
108#ifndef OPENSSL_NO_DH
109static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
110 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
111 BN_MONT_CTX *m_ctx);
112static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
113static int ubsec_dh_generate_key(DH *dh);
114#endif
115
116#ifdef NOT_USED
117static int ubsec_rand_bytes(unsigned char *buf, int num);
118static int ubsec_rand_status(void);
119#endif
120
121#define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
122static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
123 {UBSEC_CMD_SO_PATH,
124 "SO_PATH",
125 "Specifies the path to the 'ubsec' shared library",
126 ENGINE_CMD_FLAG_STRING},
127 {0, NULL, NULL, 0}
128 };
129
130#ifndef OPENSSL_NO_RSA
131/* Our internal RSA_METHOD that we provide pointers to */
132static RSA_METHOD ubsec_rsa =
133 {
134 "UBSEC RSA method",
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 ubsec_rsa_mod_exp,
140 ubsec_mod_exp_mont,
141 NULL,
142 NULL,
143 0,
144 NULL,
145 NULL,
146 NULL
147 };
148#endif
149
150#ifndef OPENSSL_NO_DSA
151/* Our internal DSA_METHOD that we provide pointers to */
152static DSA_METHOD ubsec_dsa =
153 {
154 "UBSEC DSA method",
155 ubsec_dsa_do_sign, /* dsa_do_sign */
156 NULL, /* dsa_sign_setup */
157 ubsec_dsa_verify, /* dsa_do_verify */
158 NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
159 NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
160 NULL, /* init */
161 NULL, /* finish */
162 0, /* flags */
163 NULL /* app_data */
164 };
165#endif
166
167#ifndef OPENSSL_NO_DH
168/* Our internal DH_METHOD that we provide pointers to */
169static DH_METHOD ubsec_dh =
170 {
171 "UBSEC DH method",
172 ubsec_dh_generate_key,
173 ubsec_dh_compute_key,
174 ubsec_mod_exp_dh,
175 NULL,
176 NULL,
177 0,
178 NULL
179 };
180#endif
181
182/* Constants used when creating the ENGINE */
183static const char *engine_ubsec_id = "ubsec";
184static const char *engine_ubsec_name = "UBSEC hardware engine support";
185
186/* This internal function is used by ENGINE_ubsec() and possibly by the
187 * "dynamic" ENGINE support too */
188static int bind_helper(ENGINE *e)
189 {
190#ifndef OPENSSL_NO_RSA
191 const RSA_METHOD *meth1;
192#endif
193#ifndef OPENSSL_NO_DH
194#ifndef HAVE_UBSEC_DH
195 const DH_METHOD *meth3;
196#endif /* HAVE_UBSEC_DH */
197#endif
198 if(!ENGINE_set_id(e, engine_ubsec_id) ||
199 !ENGINE_set_name(e, engine_ubsec_name) ||
200#ifndef OPENSSL_NO_RSA
201 !ENGINE_set_RSA(e, &ubsec_rsa) ||
202#endif
203#ifndef OPENSSL_NO_DSA
204 !ENGINE_set_DSA(e, &ubsec_dsa) ||
205#endif
206#ifndef OPENSSL_NO_DH
207 !ENGINE_set_DH(e, &ubsec_dh) ||
208#endif
209 !ENGINE_set_destroy_function(e, ubsec_destroy) ||
210 !ENGINE_set_init_function(e, ubsec_init) ||
211 !ENGINE_set_finish_function(e, ubsec_finish) ||
212 !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
213 !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
214 return 0;
215
216#ifndef OPENSSL_NO_RSA
217 /* We know that the "PKCS1_SSLeay()" functions hook properly
218 * to the Broadcom-specific mod_exp and mod_exp_crt so we use
219 * those functions. NB: We don't use ENGINE_openssl() or
220 * anything "more generic" because something like the RSAref
221 * code may not hook properly, and if you own one of these
222 * cards then you have the right to do RSA operations on it
223 * anyway! */
224 meth1 = RSA_PKCS1_SSLeay();
225 ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
226 ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
227 ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
228 ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
229#endif
230
231#ifndef OPENSSL_NO_DH
232#ifndef HAVE_UBSEC_DH
233 /* Much the same for Diffie-Hellman */
234 meth3 = DH_OpenSSL();
235 ubsec_dh.generate_key = meth3->generate_key;
236 ubsec_dh.compute_key = meth3->compute_key;
237#endif /* HAVE_UBSEC_DH */
238#endif
239
240 /* Ensure the ubsec error handling is set up */
241 ERR_load_UBSEC_strings();
242 return 1;
243 }
244
245#ifndef ENGINE_DYNAMIC_SUPPORT
246static ENGINE *engine_ubsec(void)
247 {
248 ENGINE *ret = ENGINE_new();
249 if(!ret)
250 return NULL;
251 if(!bind_helper(ret))
252 {
253 ENGINE_free(ret);
254 return NULL;
255 }
256 return ret;
257 }
258
259void ENGINE_load_ubsec(void)
260 {
261 /* Copied from eng_[openssl|dyn].c */
262 ENGINE *toadd = engine_ubsec();
263 if(!toadd) return;
264 ENGINE_add(toadd);
265 ENGINE_free(toadd);
266 ERR_clear_error();
267 }
268#endif
269
270/* This is a process-global DSO handle used for loading and unloading
271 * the UBSEC library. NB: This is only set (or unset) during an
272 * init() or finish() call (reference counts permitting) and they're
273 * operating with global locks, so this should be thread-safe
274 * implicitly. */
275
276static DSO *ubsec_dso = NULL;
277
278/* These are the function pointers that are (un)set when the library has
279 * successfully (un)loaded. */
280
281static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
282static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
283static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
284static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
285#ifndef OPENSSL_NO_DH
286static t_UBSEC_diffie_hellman_generate_ioctl
287 *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
288static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
289#endif
290/* #ifndef OPENSSL_NO_RSA */
291static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
292static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
293/* #endif */
294#ifndef OPENSSL_NO_DSA
295static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
296static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
297#endif
298static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
299static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
300static t_UBSEC_max_key_len_ioctl *p_UBSEC_max_key_len_ioctl = NULL;
301
302static int max_key_len = 1024; /* ??? */
303
304/*
305 * These are the static string constants for the DSO file name and the function
306 * symbol names to bind to.
307 */
308
309static const char *UBSEC_LIBNAME = NULL;
310static const char *get_UBSEC_LIBNAME(void)
311 {
312 if(UBSEC_LIBNAME)
313 return UBSEC_LIBNAME;
314 return "ubsec";
315 }
316static void free_UBSEC_LIBNAME(void)
317 {
318 if(UBSEC_LIBNAME)
319 OPENSSL_free((void*)UBSEC_LIBNAME);
320 UBSEC_LIBNAME = NULL;
321 }
322static long set_UBSEC_LIBNAME(const char *name)
323 {
324 free_UBSEC_LIBNAME();
325 return (((UBSEC_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
326 }
327static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
328static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
329static const char *UBSEC_F3 = "ubsec_open";
330static const char *UBSEC_F4 = "ubsec_close";
331#ifndef OPENSSL_NO_DH
332static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
333static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
334#endif
335/* #ifndef OPENSSL_NO_RSA */
336static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
337static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
338/* #endif */
339#ifndef OPENSSL_NO_DSA
340static const char *UBSEC_F9 = "dsa_sign_ioctl";
341static const char *UBSEC_F10 = "dsa_verify_ioctl";
342#endif
343static const char *UBSEC_F11 = "math_accelerate_ioctl";
344static const char *UBSEC_F12 = "rng_ioctl";
345static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl";
346
347/* Destructor (complements the "ENGINE_ubsec()" constructor) */
348static int ubsec_destroy(ENGINE *e)
349 {
350 free_UBSEC_LIBNAME();
351 ERR_unload_UBSEC_strings();
352 return 1;
353 }
354
355/* (de)initialisation functions. */
356static int ubsec_init(ENGINE *e)
357 {
358 t_UBSEC_ubsec_bytes_to_bits *p1;
359 t_UBSEC_ubsec_bits_to_bytes *p2;
360 t_UBSEC_ubsec_open *p3;
361 t_UBSEC_ubsec_close *p4;
362#ifndef OPENSSL_NO_DH
363 t_UBSEC_diffie_hellman_generate_ioctl *p5;
364 t_UBSEC_diffie_hellman_agree_ioctl *p6;
365#endif
366/* #ifndef OPENSSL_NO_RSA */
367 t_UBSEC_rsa_mod_exp_ioctl *p7;
368 t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
369/* #endif */
370#ifndef OPENSSL_NO_DSA
371 t_UBSEC_dsa_sign_ioctl *p9;
372 t_UBSEC_dsa_verify_ioctl *p10;
373#endif
374 t_UBSEC_math_accelerate_ioctl *p11;
375 t_UBSEC_rng_ioctl *p12;
376 t_UBSEC_max_key_len_ioctl *p13;
377 int fd = 0;
378
379 if(ubsec_dso != NULL)
380 {
381 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
382 goto err;
383 }
384 /*
385 * Attempt to load libubsec.so/ubsec.dll/whatever.
386 */
387 ubsec_dso = DSO_load(NULL, get_UBSEC_LIBNAME(), NULL, 0);
388 if(ubsec_dso == NULL)
389 {
390 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
391 goto err;
392 }
393
394 if (
395 !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
396 !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
397 !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
398 !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
399#ifndef OPENSSL_NO_DH
400 !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *)
401 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
402 !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *)
403 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
404#endif
405/* #ifndef OPENSSL_NO_RSA */
406 !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
407 !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
408/* #endif */
409#ifndef OPENSSL_NO_DSA
410 !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
411 !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
412#endif
413 !(p11 = (t_UBSEC_math_accelerate_ioctl *)
414 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
415 !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) ||
416 !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13)))
417 {
418 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
419 goto err;
420 }
421
422 /* Copy the pointers */
423 p_UBSEC_ubsec_bytes_to_bits = p1;
424 p_UBSEC_ubsec_bits_to_bytes = p2;
425 p_UBSEC_ubsec_open = p3;
426 p_UBSEC_ubsec_close = p4;
427#ifndef OPENSSL_NO_DH
428 p_UBSEC_diffie_hellman_generate_ioctl = p5;
429 p_UBSEC_diffie_hellman_agree_ioctl = p6;
430#endif
431#ifndef OPENSSL_NO_RSA
432 p_UBSEC_rsa_mod_exp_ioctl = p7;
433 p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
434#endif
435#ifndef OPENSSL_NO_DSA
436 p_UBSEC_dsa_sign_ioctl = p9;
437 p_UBSEC_dsa_verify_ioctl = p10;
438#endif
439 p_UBSEC_math_accelerate_ioctl = p11;
440 p_UBSEC_rng_ioctl = p12;
441 p_UBSEC_max_key_len_ioctl = p13;
442
443 /* Perform an open to see if there's actually any unit running. */
444 if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
445 {
446 p_UBSEC_ubsec_close(fd);
447 return 1;
448 }
449 else
450 {
451 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
452 }
453
454err:
455 if(ubsec_dso)
456 DSO_free(ubsec_dso);
457 ubsec_dso = NULL;
458 p_UBSEC_ubsec_bytes_to_bits = NULL;
459 p_UBSEC_ubsec_bits_to_bytes = NULL;
460 p_UBSEC_ubsec_open = NULL;
461 p_UBSEC_ubsec_close = NULL;
462#ifndef OPENSSL_NO_DH
463 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
464 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
465#endif
466#ifndef OPENSSL_NO_RSA
467 p_UBSEC_rsa_mod_exp_ioctl = NULL;
468 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
469#endif
470#ifndef OPENSSL_NO_DSA
471 p_UBSEC_dsa_sign_ioctl = NULL;
472 p_UBSEC_dsa_verify_ioctl = NULL;
473#endif
474 p_UBSEC_math_accelerate_ioctl = NULL;
475 p_UBSEC_rng_ioctl = NULL;
476 p_UBSEC_max_key_len_ioctl = NULL;
477
478 return 0;
479 }
480
481static int ubsec_finish(ENGINE *e)
482 {
483 free_UBSEC_LIBNAME();
484 if(ubsec_dso == NULL)
485 {
486 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
487 return 0;
488 }
489 if(!DSO_free(ubsec_dso))
490 {
491 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
492 return 0;
493 }
494 ubsec_dso = NULL;
495 p_UBSEC_ubsec_bytes_to_bits = NULL;
496 p_UBSEC_ubsec_bits_to_bytes = NULL;
497 p_UBSEC_ubsec_open = NULL;
498 p_UBSEC_ubsec_close = NULL;
499#ifndef OPENSSL_NO_DH
500 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
501 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
502#endif
503#ifndef OPENSSL_NO_RSA
504 p_UBSEC_rsa_mod_exp_ioctl = NULL;
505 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
506#endif
507#ifndef OPENSSL_NO_DSA
508 p_UBSEC_dsa_sign_ioctl = NULL;
509 p_UBSEC_dsa_verify_ioctl = NULL;
510#endif
511 p_UBSEC_math_accelerate_ioctl = NULL;
512 p_UBSEC_rng_ioctl = NULL;
513 p_UBSEC_max_key_len_ioctl = NULL;
514 return 1;
515 }
516
517static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
518 {
519 int initialised = ((ubsec_dso == NULL) ? 0 : 1);
520 switch(cmd)
521 {
522 case UBSEC_CMD_SO_PATH:
523 if(p == NULL)
524 {
525 UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
526 return 0;
527 }
528 if(initialised)
529 {
530 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
531 return 0;
532 }
533 return set_UBSEC_LIBNAME((const char *)p);
534 default:
535 break;
536 }
537 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
538 return 0;
539 }
540
541static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
542 const BIGNUM *m, BN_CTX *ctx)
543 {
544 int y_len = 0;
545 int fd;
546
547 if(ubsec_dso == NULL)
548 {
549 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
550 return 0;
551 }
552
553 /* Check if hardware can't handle this argument. */
554 y_len = BN_num_bits(m);
555 if (y_len > max_key_len) {
556 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
557 return BN_mod_exp(r, a, p, m, ctx);
558 }
559
560 if(!bn_wexpand(r, m->top))
561 {
562 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
563 return 0;
564 }
565
566 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
567 fd = 0;
568 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
569 return BN_mod_exp(r, a, p, m, ctx);
570 }
571
572 if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
573 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d,
574 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
575 {
576 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
577 p_UBSEC_ubsec_close(fd);
578
579 return BN_mod_exp(r, a, p, m, ctx);
580 }
581
582 p_UBSEC_ubsec_close(fd);
583
584 r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
585 return 1;
586 }
587
588#ifndef OPENSSL_NO_RSA
589static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
590 {
591 BN_CTX *ctx;
592 int to_return = 0;
593
594 if((ctx = BN_CTX_new()) == NULL)
595 goto err;
596
597 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
598 {
599 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
600 goto err;
601 }
602
603 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
604 rsa->dmq1, rsa->iqmp, ctx);
605 if (to_return == FAIL_TO_SOFTWARE)
606 {
607 /*
608 * Do in software as hardware failed.
609 */
610 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
611 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
612 }
613err:
614 if(ctx)
615 BN_CTX_free(ctx);
616 return to_return;
617 }
618#endif
619
620static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
621 const BIGNUM *q, const BIGNUM *dp,
622 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
623 {
624 int y_len,
625 m_len,
626 fd;
627
628 m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
629 y_len = BN_num_bits(p) + BN_num_bits(q);
630
631 /* Check if hardware can't handle this argument. */
632 if (y_len > max_key_len) {
633 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
634 return FAIL_TO_SOFTWARE;
635 }
636
637 if (!bn_wexpand(r, p->top + q->top + 1)) {
638 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
639 return 0;
640 }
641
642 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
643 fd = 0;
644 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
645 return FAIL_TO_SOFTWARE;
646 }
647
648 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
649 (unsigned char *)a->d, BN_num_bits(a),
650 (unsigned char *)qinv->d, BN_num_bits(qinv),
651 (unsigned char *)dp->d, BN_num_bits(dp),
652 (unsigned char *)p->d, BN_num_bits(p),
653 (unsigned char *)dq->d, BN_num_bits(dq),
654 (unsigned char *)q->d, BN_num_bits(q),
655 (unsigned char *)r->d, &y_len) != 0) {
656 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
657 p_UBSEC_ubsec_close(fd);
658 return FAIL_TO_SOFTWARE;
659 }
660
661 p_UBSEC_ubsec_close(fd);
662
663 r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
664 return 1;
665}
666
667#ifndef OPENSSL_NO_DSA
668#ifdef NOT_USED
669static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
670 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
671 BN_CTX *ctx, BN_MONT_CTX *in_mont)
672 {
673 BIGNUM t;
674 int to_return = 0;
675
676 BN_init(&t);
677 /* let rr = a1 ^ p1 mod m */
678 if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
679 /* let t = a2 ^ p2 mod m */
680 if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
681 /* let rr = rr * t mod m */
682 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
683 to_return = 1;
684end:
685 BN_free(&t);
686 return to_return;
687 }
688
689static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
690 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
691 BN_MONT_CTX *m_ctx)
692 {
693 return ubsec_mod_exp(r, a, p, m, ctx);
694 }
695#endif
696#endif
697
698/*
699 * This function is aliased to mod_exp (with the mont stuff dropped).
700 */
701static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
702 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
703 {
704 int ret = 0;
705
706#ifndef OPENSSL_NO_RSA
707 /* Do in software if the key is too large for the hardware. */
708 if (BN_num_bits(m) > max_key_len)
709 {
710 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
711 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
712 }
713 else
714#endif
715 {
716 ret = ubsec_mod_exp(r, a, p, m, ctx);
717 }
718
719 return ret;
720 }
721
722#ifndef OPENSSL_NO_DH
723/* This function is aliased to mod_exp (with the dh and mont dropped). */
724static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
725 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
726 BN_MONT_CTX *m_ctx)
727 {
728 return ubsec_mod_exp(r, a, p, m, ctx);
729 }
730#endif
731
732#ifndef OPENSSL_NO_DSA
733static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
734 {
735 DSA_SIG *to_return = NULL;
736 int s_len = 160, r_len = 160, d_len, fd;
737 BIGNUM m, *r=NULL, *s=NULL;
738
739 BN_init(&m);
740
741 s = BN_new();
742 r = BN_new();
743 if ((s == NULL) || (r==NULL))
744 goto err;
745
746 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
747
748 if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
749 (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
750 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
751 goto err;
752 }
753
754 if (BN_bin2bn(dgst,dlen,&m) == NULL) {
755 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
756 goto err;
757 }
758
759 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
760 const DSA_METHOD *meth;
761 fd = 0;
762 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
763 meth = DSA_OpenSSL();
764 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
765 goto err;
766 }
767
768 if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
769 (unsigned char *)dgst, d_len,
770 NULL, 0, /* compute random value */
771 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
772 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
773 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
774 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
775 (unsigned char *)r->d, &r_len,
776 (unsigned char *)s->d, &s_len ) != 0) {
777 const DSA_METHOD *meth;
778
779 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
780 p_UBSEC_ubsec_close(fd);
781 meth = DSA_OpenSSL();
782 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
783
784 goto err;
785 }
786
787 p_UBSEC_ubsec_close(fd);
788
789 r->top = (160+BN_BITS2-1)/BN_BITS2;
790 s->top = (160+BN_BITS2-1)/BN_BITS2;
791
792 to_return = DSA_SIG_new();
793 if(to_return == NULL) {
794 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
795 goto err;
796 }
797
798 to_return->r = r;
799 to_return->s = s;
800
801err:
802 if (!to_return) {
803 if (r) BN_free(r);
804 if (s) BN_free(s);
805 }
806 BN_clear_free(&m);
807 return to_return;
808}
809
810static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
811 DSA_SIG *sig, DSA *dsa)
812 {
813 int v_len, d_len;
814 int to_return = 0;
815 int fd;
816 BIGNUM v;
817
818 BN_init(&v);
819
820 if(!bn_wexpand(&v, dsa->p->top)) {
821 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
822 goto err;
823 }
824
825 v_len = BN_num_bits(dsa->p);
826
827 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
828
829 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
830 const DSA_METHOD *meth;
831 fd = 0;
832 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
833 meth = DSA_OpenSSL();
834 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
835 goto err;
836 }
837
838 if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
839 (unsigned char *)dgst, d_len,
840 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
841 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
842 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
843 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
844 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
845 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
846 (unsigned char *)v.d, &v_len) != 0) {
847 const DSA_METHOD *meth;
848 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
849 p_UBSEC_ubsec_close(fd);
850
851 meth = DSA_OpenSSL();
852 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
853
854 goto err;
855 }
856
857 p_UBSEC_ubsec_close(fd);
858
859 to_return = 1;
860err:
861 BN_clear_free(&v);
862 return to_return;
863 }
864#endif
865
866#ifndef OPENSSL_NO_DH
867static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
868 {
869 int ret = -1,
870 k_len,
871 fd;
872
873 k_len = BN_num_bits(dh->p);
874
875 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
876 {
877 const DH_METHOD *meth;
878 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
879 meth = DH_OpenSSL();
880 ret = meth->compute_key(key, pub_key, dh);
881 goto err;
882 }
883
884 if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
885 (unsigned char *)dh->priv_key->d, BN_num_bits(dh->priv_key),
886 (unsigned char *)pub_key->d, BN_num_bits(pub_key),
887 (unsigned char *)dh->p->d, BN_num_bits(dh->p),
888 key, &k_len) != 0)
889 {
890 /* Hardware's a no go, failover to software */
891 const DH_METHOD *meth;
892 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
893 p_UBSEC_ubsec_close(fd);
894
895 meth = DH_OpenSSL();
896 ret = meth->compute_key(key, pub_key, dh);
897
898 goto err;
899 }
900
901 p_UBSEC_ubsec_close(fd);
902
903 ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
904err:
905 return ret;
906 }
907
908static int ubsec_dh_generate_key (DH *dh)
909 {
910 int ret = 0,
911 random_bits = 0,
912 pub_key_len = 0,
913 priv_key_len = 0,
914 fd;
915 BIGNUM *pub_key = NULL;
916 BIGNUM *priv_key = NULL;
917
918 /*
919 * How many bits should Random x be? dh_key.c
920 * sets the range from 0 to num_bits(modulus) ???
921 */
922
923 if (dh->priv_key == NULL)
924 {
925 priv_key = BN_new();
926 if (priv_key == NULL) goto err;
927 priv_key_len = BN_num_bits(dh->p);
928 bn_wexpand(priv_key, dh->p->top);
929 do
930 if (!BN_rand_range(priv_key, dh->p)) goto err;
931 while (BN_is_zero(priv_key));
932 random_bits = BN_num_bits(priv_key);
933 }
934 else
935 {
936 priv_key = dh->priv_key;
937 }
938
939 if (dh->pub_key == NULL)
940 {
941 pub_key = BN_new();
942 pub_key_len = BN_num_bits(dh->p);
943 bn_wexpand(pub_key, dh->p->top);
944 if(pub_key == NULL) goto err;
945 }
946 else
947 {
948 pub_key = dh->pub_key;
949 }
950
951 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
952 {
953 const DH_METHOD *meth;
954 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
955 meth = DH_OpenSSL();
956 ret = meth->generate_key(dh);
957 goto err;
958 }
959
960 if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
961 (unsigned char *)priv_key->d, &priv_key_len,
962 (unsigned char *)pub_key->d, &pub_key_len,
963 (unsigned char *)dh->g->d, BN_num_bits(dh->g),
964 (unsigned char *)dh->p->d, BN_num_bits(dh->p),
965 0, 0, random_bits) != 0)
966 {
967 /* Hardware's a no go, failover to software */
968 const DH_METHOD *meth;
969
970 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
971 p_UBSEC_ubsec_close(fd);
972
973 meth = DH_OpenSSL();
974 ret = meth->generate_key(dh);
975
976 goto err;
977 }
978
979 p_UBSEC_ubsec_close(fd);
980
981 dh->pub_key = pub_key;
982 dh->pub_key->top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
983 dh->priv_key = priv_key;
984 dh->priv_key->top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
985
986 ret = 1;
987err:
988 return ret;
989 }
990#endif
991
992#ifdef NOT_USED
993static int ubsec_rand_bytes(unsigned char * buf,
994 int num)
995 {
996 int ret = 0,
997 fd;
998
999 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
1000 {
1001 const RAND_METHOD *meth;
1002 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
1003 num = p_UBSEC_ubsec_bits_to_bytes(num);
1004 meth = RAND_SSLeay();
1005 meth->seed(buf, num);
1006 ret = meth->bytes(buf, num);
1007 goto err;
1008 }
1009
1010 num *= 8; /* bytes to bits */
1011
1012 if (p_UBSEC_rng_ioctl(fd,
1013 UBSEC_RNG_DIRECT,
1014 buf,
1015 &num) != 0)
1016 {
1017 /* Hardware's a no go, failover to software */
1018 const RAND_METHOD *meth;
1019
1020 ENGINEerr(UBSEC_F_UBSEC_RNG_BYTES, UBSEC_R_REQUEST_FAILED);
1021 p_UBSEC_ubsec_close(fd);
1022
1023 num = p_UBSEC_ubsec_bits_to_bytes(num);
1024 meth = RAND_SSLeay();
1025 meth->seed(buf, num);
1026 ret = meth->bytes(buf, num);
1027
1028 goto err;
1029 }
1030
1031 p_UBSEC_ubsec_close(fd);
1032
1033 ret = 1;
1034err:
1035 return(ret);
1036 }
1037
1038
1039static int ubsec_rand_status(void)
1040 {
1041 return 0;
1042 }
1043#endif
1044
1045/* This stuff is needed if this ENGINE is being compiled into a self-contained
1046 * shared-library. */
1047#ifdef ENGINE_DYNAMIC_SUPPORT
1048static int bind_fn(ENGINE *e, const char *id)
1049 {
1050 if(id && (strcmp(id, engine_ubsec_id) != 0))
1051 return 0;
1052 if(!bind_helper(e))
1053 return 0;
1054 return 1;
1055 }
1056IMPLEMENT_DYNAMIC_CHECK_FN()
1057IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1058#endif /* ENGINE_DYNAMIC_SUPPORT */
1059
1060#endif /* !OPENSSL_NO_HW_UBSEC */
1061#endif /* !OPENSSL_NO_HW */