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