1 /* crypto/engine/hw_ubsec.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 * Cloned shamelessly by Joe Tardo.
7 /* ====================================================================
8 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
62 #include <openssl/crypto.h>
64 #include <openssl/dso.h>
65 #include "engine_int.h"
66 #include <openssl/engine.h>
74 #include "vendor_defns/hw_ubsec.h"
77 static int ubsec_init(void);
78 static int ubsec_finish(void);
79 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
80 const BIGNUM *m, BN_CTX *ctx);
81 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
82 const BIGNUM *q, const BIGNUM *dp,
83 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
84 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
85 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
86 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
87 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
88 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
89 BN_CTX *ctx, BN_MONT_CTX *in_mont);
90 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
91 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
93 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
94 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
95 DSA_SIG *sig, DSA *dsa);
96 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
97 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
99 static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
100 static int ubsec_dh_generate_key(DH *dh);
101 static int ubsec_rand_bytes(unsigned char *buf, int num);
102 static int ubsec_rand_status(void);
104 /* Our internal RSA_METHOD that we provide pointers to */
106 static RSA_METHOD ubsec_rsa =
123 /* Our internal DSA_METHOD that we provide pointers to */
124 static DSA_METHOD ubsec_dsa =
127 ubsec_dsa_do_sign, /* dsa_do_sign */
128 NULL, /* dsa_sign_setup */
129 ubsec_dsa_verify, /* dsa_do_verify */
130 NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
131 NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
138 /* Our internal DH_METHOD that we provide pointers to */
139 static DH_METHOD ubsec_dh =
142 ubsec_dh_generate_key,
143 ubsec_dh_compute_key,
151 /* Our ENGINE structure. */
152 static ENGINE engine_ubsec =
155 "UBSEC hardware engine support",
164 NULL, /* no ctrl() */
165 NULL, /* no load_privkey() */
166 NULL, /* no load_pubkey() */
168 0, 0, /* no references */
169 NULL, NULL /* unlinked */
172 /* As this is only ever called once, there's no need for locking
173 * (indeed - the lock will already be held by our caller!!!) */
174 ENGINE *ENGINE_ubsec()
176 const RSA_METHOD *meth1;
177 #ifndef HAVE_UBSEC_DH
178 const DH_METHOD *meth3;
179 #endif /* HAVE_UBSEC_DH */
181 /* We know that the "PKCS1_SSLeay()" functions hook properly
182 * to the Broadcom-specific mod_exp and mod_exp_crt so we use
183 * those functions. NB: We don't use ENGINE_openssl() or
184 * anything "more generic" because something like the RSAref
185 * code may not hook properly, and if you own one of these
186 * cards then you have the right to do RSA operations on it
188 meth1 = RSA_PKCS1_SSLeay();
189 ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
190 ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
191 ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
192 ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
194 #ifndef HAVE_UBSEC_DH
195 /* Much the same for Diffie-Hellman */
196 meth3 = DH_OpenSSL();
197 ubsec_dh.generate_key = meth3->generate_key;
198 ubsec_dh.compute_key = meth3->compute_key;
199 #endif /* HAVE_UBSEC_DH */
201 return &engine_ubsec;
204 /* This is a process-global DSO handle used for loading and unloading
205 * the UBSEC library. NB: This is only set (or unset) during an
206 * init() or finish() call (reference counts permitting) and they're
207 * operating with global locks, so this should be thread-safe
210 static DSO *ubsec_dso = NULL;
212 /* These are the function pointers that are (un)set when the library has
213 * successfully (un)loaded. */
215 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
216 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
217 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
218 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
219 static t_UBSEC_diffie_hellman_generate_ioctl
220 *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
221 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
222 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
223 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
224 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
225 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
226 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
227 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
229 /* (de)initialisation functions. */
230 static int ubsec_init()
232 t_UBSEC_ubsec_bytes_to_bits *p1;
233 t_UBSEC_ubsec_bits_to_bytes *p2;
234 t_UBSEC_ubsec_open *p3;
235 t_UBSEC_ubsec_close *p4;
236 t_UBSEC_diffie_hellman_generate_ioctl *p5;
237 t_UBSEC_diffie_hellman_agree_ioctl *p6;
238 t_UBSEC_rsa_mod_exp_ioctl *p7;
239 t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
240 t_UBSEC_dsa_sign_ioctl *p9;
241 t_UBSEC_dsa_verify_ioctl *p10;
242 t_UBSEC_math_accelerate_ioctl *p11;
243 t_UBSEC_rng_ioctl *p12;
246 if(ubsec_dso != NULL)
248 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_ALREADY_LOADED);
252 * Attempt to load libubsec.so/ubsec.dll/whatever.
254 ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
255 if(ubsec_dso == NULL)
257 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_DSO_FAILURE);
262 !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
263 !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
264 !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
265 !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
266 !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *)
267 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
268 !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *)
269 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
270 !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
271 !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
272 !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
273 !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
274 !(p11 = (t_UBSEC_math_accelerate_ioctl *)
275 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
276 !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)))
278 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_DSO_FAILURE);
282 /* Copy the pointers */
283 p_UBSEC_ubsec_bytes_to_bits = p1;
284 p_UBSEC_ubsec_bits_to_bytes = p2;
285 p_UBSEC_ubsec_open = p3;
286 p_UBSEC_ubsec_close = p4;
287 p_UBSEC_diffie_hellman_generate_ioctl = p5;
288 p_UBSEC_diffie_hellman_agree_ioctl = p6;
289 p_UBSEC_rsa_mod_exp_ioctl = p7;
290 p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
291 p_UBSEC_dsa_sign_ioctl = p9;
292 p_UBSEC_dsa_verify_ioctl = p10;
293 p_UBSEC_math_accelerate_ioctl = p11;
294 p_UBSEC_rng_ioctl = p12;
296 /* Perform an open to see if there's actually any unit running. */
297 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0)
299 p_UBSEC_ubsec_close(fd);
304 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
310 p_UBSEC_ubsec_bytes_to_bits = NULL;
311 p_UBSEC_ubsec_bits_to_bytes = NULL;
312 p_UBSEC_ubsec_open = NULL;
313 p_UBSEC_ubsec_close = NULL;
314 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
315 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
316 p_UBSEC_rsa_mod_exp_ioctl = NULL;
317 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
318 p_UBSEC_dsa_sign_ioctl = NULL;
319 p_UBSEC_dsa_verify_ioctl = NULL;
320 p_UBSEC_math_accelerate_ioctl = NULL;
321 p_UBSEC_rng_ioctl = NULL;
326 static int ubsec_finish()
328 if(ubsec_dso == NULL)
330 ENGINEerr(ENGINE_F_UBSEC_FINISH, ENGINE_R_NOT_LOADED);
333 if(!DSO_free(ubsec_dso))
335 ENGINEerr(ENGINE_F_UBSEC_FINISH, ENGINE_R_DSO_FAILURE);
339 p_UBSEC_ubsec_bytes_to_bits = NULL;
340 p_UBSEC_ubsec_bits_to_bytes = NULL;
341 p_UBSEC_ubsec_open = NULL;
342 p_UBSEC_ubsec_close = NULL;
343 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
344 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
345 p_UBSEC_rsa_mod_exp_ioctl = NULL;
346 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
347 p_UBSEC_dsa_sign_ioctl = NULL;
348 p_UBSEC_dsa_verify_ioctl = NULL;
349 p_UBSEC_math_accelerate_ioctl = NULL;
350 p_UBSEC_rng_ioctl = NULL;
354 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
355 const BIGNUM *m, BN_CTX *ctx)
360 if(ubsec_dso == NULL)
362 ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_NOT_LOADED);
366 /* Check if hardware can't handle this argument. */
367 y_len = BN_num_bits(m);
369 ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
373 if(!bn_wexpand(r, m->top))
375 ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_BN_EXPAND_FAIL);
378 memset(r->d, 0, BN_num_bytes(m));
380 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
382 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
386 if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
387 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d,
388 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
390 ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_REQUEST_FAILED);
394 p_UBSEC_ubsec_close(fd);
396 r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
400 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
405 if((ctx = BN_CTX_new()) == NULL)
408 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
410 ENGINEerr(ENGINE_F_UBSEC_RSA_MOD_EXP, ENGINE_R_MISSING_KEY_COMPONENTS);
415 * Do in software if argument is too large for hardware.
417 if ((BN_num_bits(rsa->p)+BN_num_bits(rsa->q)) > 1024) {
418 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
419 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
421 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
422 rsa->dmq1, rsa->iqmp, ctx);
430 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
431 const BIGNUM *q, const BIGNUM *dp,
432 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
438 m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
439 y_len = BN_num_bits(p) + BN_num_bits(q);
441 /* Check if hardware can't handle this argument. */
443 ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
447 if (!bn_wexpand(r, p->top + q->top + 1)) {
448 ENGINEerr(ENGINE_F_UBSEC_RSA_MOD_EXP_CRT, ENGINE_R_BN_EXPAND_FAIL);
452 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
454 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
458 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
459 (unsigned char *)a->d, BN_num_bits(a),
460 (unsigned char *)qinv->d, BN_num_bits(qinv),
461 (unsigned char *)dp->d, BN_num_bits(dp),
462 (unsigned char *)p->d, BN_num_bits(p),
463 (unsigned char *)dq->d, BN_num_bits(dq),
464 (unsigned char *)q->d, BN_num_bits(q),
465 (unsigned char *)r->d, &y_len) != 0) {
466 ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_REQUEST_FAILED);
470 p_UBSEC_ubsec_close(fd);
472 r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
476 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
477 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
478 BN_CTX *ctx, BN_MONT_CTX *in_mont)
484 /* let rr = a1 ^ p1 mod m */
485 if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
486 /* let t = a2 ^ p2 mod m */
487 if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
488 /* let rr = rr * t mod m */
489 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
496 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
497 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
500 return ubsec_mod_exp(r, a, p, m, ctx);
504 * This function is aliased to mod_exp (with the mont stuff dropped).
506 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
507 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
511 /* Do in software if the key is too large for the hardware. */
512 if (BN_num_bits(m) > 1024) {
513 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
514 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
516 ret = ubsec_mod_exp(r, a, p, m, ctx);
522 /* This function is aliased to mod_exp (with the dh and mont dropped). */
523 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
524 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
527 return ubsec_mod_exp(r, a, p, m, ctx);
530 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
532 DSA_SIG *to_return = NULL;
533 int s_len = 160, r_len = 160, d_len, fd;
534 BIGNUM m, *r=NULL, *s=NULL;
540 if ((s == NULL) || (r==NULL))
543 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
545 if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
546 (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
547 ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_BN_EXPAND_FAIL);
551 if (BN_bin2bn(dgst,dlen,&m) == NULL) {
552 ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_BN_EXPAND_FAIL);
556 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
558 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
562 if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
563 (unsigned char *)dgst, d_len,
564 NULL, 0, /* compute random value */
565 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
566 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
567 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
568 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
569 (unsigned char *)r->d, &r_len,
570 (unsigned char *)s->d, &s_len ) != 0) {
571 ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_REQUEST_FAILED);
575 p_UBSEC_ubsec_close(fd);
577 r->top = (160+BN_BITS2-1)/BN_BITS2;
578 s->top = (160+BN_BITS2-1)/BN_BITS2;
580 to_return = DSA_SIG_new();
581 if(to_return == NULL) {
582 ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_BN_EXPAND_FAIL);
598 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
599 DSA_SIG *sig, DSA *dsa)
608 if(!bn_wexpand(&v, dsa->p->top)) {
609 ENGINEerr(ENGINE_F_UBSEC_DSA_VERIFY ,ENGINE_R_BN_EXPAND_FAIL);
613 v_len = BN_num_bits(dsa->p);
615 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
617 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
619 ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
623 if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
624 (unsigned char *)dgst, d_len,
625 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
626 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
627 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
628 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
629 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
630 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
631 (unsigned char *)v.d, &v_len) != 0) {
632 ENGINEerr(ENGINE_F_UBSEC_DSA_VERIFY , ENGINE_R_REQUEST_FAILED);
636 p_UBSEC_ubsec_close(fd);
644 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
649 static int ubsec_dh_generate_key (DH *dh)
654 static int ubsec_rand_bytes(unsigned char *buf, int num)
659 static int ubsec_rand_status(void)
664 #endif /* !NO_HW_UBSEC */