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