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