/src/openssl/engines/e_ubsec.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | # 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 | 0 | # 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 | 19 | { |
208 | 19 | # ifndef OPENSSL_NO_RSA |
209 | 19 | const RSA_METHOD *meth1; |
210 | 19 | # endif |
211 | 19 | # ifndef OPENSSL_NO_DH |
212 | 19 | # ifndef HAVE_UBSEC_DH |
213 | 19 | const DH_METHOD *meth3; |
214 | 19 | # endif /* HAVE_UBSEC_DH */ |
215 | 19 | # endif |
216 | 19 | if (!ENGINE_set_id(e, engine_ubsec_id) || |
217 | 19 | !ENGINE_set_name(e, engine_ubsec_name) || |
218 | 19 | # ifndef OPENSSL_NO_RSA |
219 | 19 | !ENGINE_set_RSA(e, &ubsec_rsa) || |
220 | 19 | # endif |
221 | 19 | # ifndef OPENSSL_NO_DSA |
222 | 19 | !ENGINE_set_DSA(e, &ubsec_dsa) || |
223 | 19 | # endif |
224 | 19 | # ifndef OPENSSL_NO_DH |
225 | 19 | !ENGINE_set_DH(e, &ubsec_dh) || |
226 | 19 | # endif |
227 | 19 | !ENGINE_set_destroy_function(e, ubsec_destroy) || |
228 | 19 | !ENGINE_set_init_function(e, ubsec_init) || |
229 | 19 | !ENGINE_set_finish_function(e, ubsec_finish) || |
230 | 19 | !ENGINE_set_ctrl_function(e, ubsec_ctrl) || |
231 | 19 | !ENGINE_set_cmd_defns(e, ubsec_cmd_defns)) |
232 | 0 | return 0; |
233 | | |
234 | 19 | # ifndef OPENSSL_NO_RSA |
235 | | /* |
236 | | * We know that the "PKCS1_SSLeay()" 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 | 19 | meth1 = RSA_PKCS1_SSLeay(); |
244 | 19 | ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc; |
245 | 19 | ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec; |
246 | 19 | ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc; |
247 | 19 | ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec; |
248 | 19 | # endif |
249 | | |
250 | 19 | # ifndef OPENSSL_NO_DH |
251 | 19 | # ifndef HAVE_UBSEC_DH |
252 | | /* Much the same for Diffie-Hellman */ |
253 | 19 | meth3 = DH_OpenSSL(); |
254 | 19 | ubsec_dh.generate_key = meth3->generate_key; |
255 | 19 | ubsec_dh.compute_key = meth3->compute_key; |
256 | 19 | # endif /* HAVE_UBSEC_DH */ |
257 | 19 | # endif |
258 | | |
259 | | /* Ensure the ubsec error handling is set up */ |
260 | 19 | ERR_load_UBSEC_strings(); |
261 | 19 | return 1; |
262 | 19 | } |
263 | | |
264 | | # ifdef OPENSSL_NO_DYNAMIC_ENGINE |
265 | | static ENGINE *engine_ubsec(void) |
266 | 19 | { |
267 | 19 | ENGINE *ret = ENGINE_new(); |
268 | 19 | if (!ret) |
269 | 0 | return NULL; |
270 | 19 | if (!bind_helper(ret)) { |
271 | 0 | ENGINE_free(ret); |
272 | 0 | return NULL; |
273 | 0 | } |
274 | 19 | return ret; |
275 | 19 | } |
276 | | |
277 | | void ENGINE_load_ubsec(void) |
278 | 19 | { |
279 | | /* Copied from eng_[openssl|dyn].c */ |
280 | 19 | ENGINE *toadd = engine_ubsec(); |
281 | 19 | if (!toadd) |
282 | 0 | return; |
283 | 19 | ENGINE_add(toadd); |
284 | 19 | ENGINE_free(toadd); |
285 | 19 | ERR_clear_error(); |
286 | 19 | } |
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 | 0 | { |
335 | 0 | if (UBSEC_LIBNAME) |
336 | 0 | return UBSEC_LIBNAME; |
337 | 0 | return "ubsec"; |
338 | 0 | } |
339 | | |
340 | | static void free_UBSEC_LIBNAME(void) |
341 | 0 | { |
342 | 0 | if (UBSEC_LIBNAME) |
343 | 0 | OPENSSL_free((void *)UBSEC_LIBNAME); |
344 | 0 | UBSEC_LIBNAME = NULL; |
345 | 0 | } |
346 | | |
347 | | static long set_UBSEC_LIBNAME(const char *name) |
348 | 0 | { |
349 | 0 | free_UBSEC_LIBNAME(); |
350 | 0 | return (((UBSEC_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0); |
351 | 0 | } |
352 | | |
353 | | static const char *UBSEC_F1 = "ubsec_bytes_to_bits"; |
354 | | static const char *UBSEC_F2 = "ubsec_bits_to_bytes"; |
355 | | static const char *UBSEC_F3 = "ubsec_open"; |
356 | | static const char *UBSEC_F4 = "ubsec_close"; |
357 | | # ifndef OPENSSL_NO_DH |
358 | | static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl"; |
359 | | static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl"; |
360 | | # endif |
361 | | /* #ifndef OPENSSL_NO_RSA */ |
362 | | static const char *UBSEC_F7 = "rsa_mod_exp_ioctl"; |
363 | | static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl"; |
364 | | /* #endif */ |
365 | | # ifndef OPENSSL_NO_DSA |
366 | | static const char *UBSEC_F9 = "dsa_sign_ioctl"; |
367 | | static const char *UBSEC_F10 = "dsa_verify_ioctl"; |
368 | | # endif |
369 | | static const char *UBSEC_F11 = "math_accelerate_ioctl"; |
370 | | static const char *UBSEC_F12 = "rng_ioctl"; |
371 | | static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl"; |
372 | | |
373 | | /* Destructor (complements the "ENGINE_ubsec()" constructor) */ |
374 | | static int ubsec_destroy(ENGINE *e) |
375 | 0 | { |
376 | 0 | free_UBSEC_LIBNAME(); |
377 | 0 | ERR_unload_UBSEC_strings(); |
378 | 0 | return 1; |
379 | 0 | } |
380 | | |
381 | | /* (de)initialisation functions. */ |
382 | | static int ubsec_init(ENGINE *e) |
383 | 0 | { |
384 | 0 | t_UBSEC_ubsec_bytes_to_bits *p1; |
385 | 0 | t_UBSEC_ubsec_bits_to_bytes *p2; |
386 | 0 | t_UBSEC_ubsec_open *p3; |
387 | 0 | t_UBSEC_ubsec_close *p4; |
388 | 0 | # ifndef OPENSSL_NO_DH |
389 | 0 | t_UBSEC_diffie_hellman_generate_ioctl *p5; |
390 | 0 | t_UBSEC_diffie_hellman_agree_ioctl *p6; |
391 | 0 | # endif |
392 | | /* #ifndef OPENSSL_NO_RSA */ |
393 | 0 | t_UBSEC_rsa_mod_exp_ioctl *p7; |
394 | 0 | t_UBSEC_rsa_mod_exp_crt_ioctl *p8; |
395 | | /* #endif */ |
396 | 0 | # ifndef OPENSSL_NO_DSA |
397 | 0 | t_UBSEC_dsa_sign_ioctl *p9; |
398 | 0 | t_UBSEC_dsa_verify_ioctl *p10; |
399 | 0 | # endif |
400 | 0 | t_UBSEC_math_accelerate_ioctl *p11; |
401 | 0 | t_UBSEC_rng_ioctl *p12; |
402 | 0 | t_UBSEC_max_key_len_ioctl *p13; |
403 | 0 | int fd = 0; |
404 | |
|
405 | 0 | if (ubsec_dso != NULL) { |
406 | 0 | UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED); |
407 | 0 | goto err; |
408 | 0 | } |
409 | | /* |
410 | | * Attempt to load libubsec.so/ubsec.dll/whatever. |
411 | | */ |
412 | 0 | ubsec_dso = DSO_load(NULL, get_UBSEC_LIBNAME(), NULL, 0); |
413 | 0 | if (ubsec_dso == NULL) { |
414 | 0 | UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE); |
415 | 0 | goto err; |
416 | 0 | } |
417 | | |
418 | 0 | if (!(p1 = (t_UBSEC_ubsec_bytes_to_bits *) |
419 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F1)) |
420 | 0 | || !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) |
421 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F2)) |
422 | 0 | || !(p3 = (t_UBSEC_ubsec_open *) |
423 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F3)) |
424 | 0 | || !(p4 = (t_UBSEC_ubsec_close *) |
425 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F4)) |
426 | 0 | # ifndef OPENSSL_NO_DH |
427 | 0 | || !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) |
428 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F5)) |
429 | 0 | || !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) |
430 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F6)) |
431 | 0 | # endif |
432 | | /* #ifndef OPENSSL_NO_RSA */ |
433 | 0 | || !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) |
434 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F7)) |
435 | 0 | || !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) |
436 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F8)) |
437 | | /* #endif */ |
438 | 0 | # ifndef OPENSSL_NO_DSA |
439 | 0 | || !(p9 = (t_UBSEC_dsa_sign_ioctl *) |
440 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F9)) |
441 | 0 | || !(p10 = (t_UBSEC_dsa_verify_ioctl *) |
442 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F10)) |
443 | 0 | # endif |
444 | 0 | || !(p11 = (t_UBSEC_math_accelerate_ioctl *) |
445 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F11)) |
446 | 0 | || !(p12 = (t_UBSEC_rng_ioctl *) |
447 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F12)) |
448 | 0 | || !(p13 = (t_UBSEC_max_key_len_ioctl *) |
449 | 0 | DSO_bind_func(ubsec_dso, UBSEC_F13))) { |
450 | 0 | UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE); |
451 | 0 | goto err; |
452 | 0 | } |
453 | | |
454 | | /* Copy the pointers */ |
455 | 0 | p_UBSEC_ubsec_bytes_to_bits = p1; |
456 | 0 | p_UBSEC_ubsec_bits_to_bytes = p2; |
457 | 0 | p_UBSEC_ubsec_open = p3; |
458 | 0 | p_UBSEC_ubsec_close = p4; |
459 | 0 | # ifndef OPENSSL_NO_DH |
460 | 0 | p_UBSEC_diffie_hellman_generate_ioctl = p5; |
461 | 0 | p_UBSEC_diffie_hellman_agree_ioctl = p6; |
462 | 0 | # endif |
463 | 0 | # ifndef OPENSSL_NO_RSA |
464 | 0 | p_UBSEC_rsa_mod_exp_ioctl = p7; |
465 | 0 | p_UBSEC_rsa_mod_exp_crt_ioctl = p8; |
466 | 0 | # endif |
467 | 0 | # ifndef OPENSSL_NO_DSA |
468 | 0 | p_UBSEC_dsa_sign_ioctl = p9; |
469 | 0 | p_UBSEC_dsa_verify_ioctl = p10; |
470 | 0 | # endif |
471 | 0 | p_UBSEC_math_accelerate_ioctl = p11; |
472 | 0 | p_UBSEC_rng_ioctl = p12; |
473 | 0 | p_UBSEC_max_key_len_ioctl = p13; |
474 | | |
475 | | /* Perform an open to see if there's actually any unit running. */ |
476 | 0 | if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) |
477 | 0 | && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0)) { |
478 | 0 | p_UBSEC_ubsec_close(fd); |
479 | 0 | return 1; |
480 | 0 | } else { |
481 | 0 | UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE); |
482 | 0 | } |
483 | | |
484 | 0 | err: |
485 | 0 | if (ubsec_dso) |
486 | 0 | DSO_free(ubsec_dso); |
487 | 0 | ubsec_dso = NULL; |
488 | 0 | p_UBSEC_ubsec_bytes_to_bits = NULL; |
489 | 0 | p_UBSEC_ubsec_bits_to_bytes = NULL; |
490 | 0 | p_UBSEC_ubsec_open = NULL; |
491 | 0 | p_UBSEC_ubsec_close = NULL; |
492 | 0 | # ifndef OPENSSL_NO_DH |
493 | 0 | p_UBSEC_diffie_hellman_generate_ioctl = NULL; |
494 | 0 | p_UBSEC_diffie_hellman_agree_ioctl = NULL; |
495 | 0 | # endif |
496 | 0 | # ifndef OPENSSL_NO_RSA |
497 | 0 | p_UBSEC_rsa_mod_exp_ioctl = NULL; |
498 | 0 | p_UBSEC_rsa_mod_exp_crt_ioctl = NULL; |
499 | 0 | # endif |
500 | 0 | # ifndef OPENSSL_NO_DSA |
501 | 0 | p_UBSEC_dsa_sign_ioctl = NULL; |
502 | 0 | p_UBSEC_dsa_verify_ioctl = NULL; |
503 | 0 | # endif |
504 | 0 | p_UBSEC_math_accelerate_ioctl = NULL; |
505 | 0 | p_UBSEC_rng_ioctl = NULL; |
506 | 0 | p_UBSEC_max_key_len_ioctl = NULL; |
507 | |
|
508 | 0 | return 0; |
509 | 0 | } |
510 | | |
511 | | static int ubsec_finish(ENGINE *e) |
512 | 0 | { |
513 | 0 | free_UBSEC_LIBNAME(); |
514 | 0 | if (ubsec_dso == NULL) { |
515 | 0 | UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED); |
516 | 0 | return 0; |
517 | 0 | } |
518 | 0 | if (!DSO_free(ubsec_dso)) { |
519 | 0 | UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE); |
520 | 0 | return 0; |
521 | 0 | } |
522 | 0 | ubsec_dso = NULL; |
523 | 0 | p_UBSEC_ubsec_bytes_to_bits = NULL; |
524 | 0 | p_UBSEC_ubsec_bits_to_bytes = NULL; |
525 | 0 | p_UBSEC_ubsec_open = NULL; |
526 | 0 | p_UBSEC_ubsec_close = NULL; |
527 | 0 | # ifndef OPENSSL_NO_DH |
528 | 0 | p_UBSEC_diffie_hellman_generate_ioctl = NULL; |
529 | 0 | p_UBSEC_diffie_hellman_agree_ioctl = NULL; |
530 | 0 | # endif |
531 | 0 | # ifndef OPENSSL_NO_RSA |
532 | 0 | p_UBSEC_rsa_mod_exp_ioctl = NULL; |
533 | 0 | p_UBSEC_rsa_mod_exp_crt_ioctl = NULL; |
534 | 0 | # endif |
535 | 0 | # ifndef OPENSSL_NO_DSA |
536 | 0 | p_UBSEC_dsa_sign_ioctl = NULL; |
537 | 0 | p_UBSEC_dsa_verify_ioctl = NULL; |
538 | 0 | # endif |
539 | 0 | p_UBSEC_math_accelerate_ioctl = NULL; |
540 | 0 | p_UBSEC_rng_ioctl = NULL; |
541 | 0 | p_UBSEC_max_key_len_ioctl = NULL; |
542 | 0 | return 1; |
543 | 0 | } |
544 | | |
545 | | static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) |
546 | 0 | { |
547 | 0 | int initialised = ((ubsec_dso == NULL) ? 0 : 1); |
548 | 0 | switch (cmd) { |
549 | 0 | case UBSEC_CMD_SO_PATH: |
550 | 0 | if (p == NULL) { |
551 | 0 | UBSECerr(UBSEC_F_UBSEC_CTRL, ERR_R_PASSED_NULL_PARAMETER); |
552 | 0 | return 0; |
553 | 0 | } |
554 | 0 | if (initialised) { |
555 | 0 | UBSECerr(UBSEC_F_UBSEC_CTRL, UBSEC_R_ALREADY_LOADED); |
556 | 0 | return 0; |
557 | 0 | } |
558 | 0 | return set_UBSEC_LIBNAME((const char *)p); |
559 | 0 | default: |
560 | 0 | break; |
561 | 0 | } |
562 | 0 | UBSECerr(UBSEC_F_UBSEC_CTRL, UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED); |
563 | 0 | return 0; |
564 | 0 | } |
565 | | |
566 | | static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
567 | | const BIGNUM *m, BN_CTX *ctx) |
568 | 0 | { |
569 | 0 | int y_len = 0; |
570 | 0 | int fd; |
571 | |
|
572 | 0 | if (ubsec_dso == NULL) { |
573 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED); |
574 | 0 | return 0; |
575 | 0 | } |
576 | | |
577 | | /* Check if hardware can't handle this argument. */ |
578 | 0 | y_len = BN_num_bits(m); |
579 | 0 | if (y_len > max_key_len) { |
580 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL); |
581 | 0 | return BN_mod_exp(r, a, p, m, ctx); |
582 | 0 | } |
583 | | |
584 | 0 | if (!bn_wexpand(r, m->top)) { |
585 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL); |
586 | 0 | return 0; |
587 | 0 | } |
588 | | |
589 | 0 | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
590 | 0 | fd = 0; |
591 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_UNIT_FAILURE); |
592 | 0 | return BN_mod_exp(r, a, p, m, ctx); |
593 | 0 | } |
594 | | |
595 | 0 | if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a), |
596 | 0 | (unsigned char *)m->d, BN_num_bits(m), |
597 | 0 | (unsigned char *)p->d, BN_num_bits(p), |
598 | 0 | (unsigned char *)r->d, &y_len) != 0) { |
599 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED); |
600 | 0 | p_UBSEC_ubsec_close(fd); |
601 | |
|
602 | 0 | return BN_mod_exp(r, a, p, m, ctx); |
603 | 0 | } |
604 | | |
605 | 0 | p_UBSEC_ubsec_close(fd); |
606 | |
|
607 | 0 | r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2; |
608 | 0 | return 1; |
609 | 0 | } |
610 | | |
611 | | # ifndef OPENSSL_NO_RSA |
612 | | static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, |
613 | | BN_CTX *ctx) |
614 | 0 | { |
615 | 0 | int to_return = 0; |
616 | |
|
617 | 0 | if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) { |
618 | 0 | UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS); |
619 | 0 | goto err; |
620 | 0 | } |
621 | | |
622 | 0 | to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1, |
623 | 0 | rsa->dmq1, rsa->iqmp, ctx); |
624 | 0 | if (to_return == FAIL_TO_SOFTWARE) { |
625 | | /* |
626 | | * Do in software as hardware failed. |
627 | | */ |
628 | 0 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); |
629 | 0 | to_return = (*meth->rsa_mod_exp) (r0, I, rsa, ctx); |
630 | 0 | } |
631 | 0 | err: |
632 | 0 | return to_return; |
633 | 0 | } |
634 | | |
635 | | static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
636 | | const BIGNUM *q, const BIGNUM *dp, |
637 | | const BIGNUM *dq, const BIGNUM *qinv, |
638 | | BN_CTX *ctx) |
639 | 0 | { |
640 | 0 | int y_len, fd; |
641 | |
|
642 | 0 | y_len = BN_num_bits(p) + BN_num_bits(q); |
643 | | |
644 | | /* Check if hardware can't handle this argument. */ |
645 | 0 | if (y_len > max_key_len) { |
646 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, |
647 | 0 | UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL); |
648 | 0 | return FAIL_TO_SOFTWARE; |
649 | 0 | } |
650 | | |
651 | 0 | if (!bn_wexpand(r, p->top + q->top + 1)) { |
652 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL); |
653 | 0 | return 0; |
654 | 0 | } |
655 | | |
656 | 0 | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
657 | 0 | fd = 0; |
658 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_UNIT_FAILURE); |
659 | 0 | return FAIL_TO_SOFTWARE; |
660 | 0 | } |
661 | | |
662 | 0 | if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd, |
663 | 0 | (unsigned char *)a->d, BN_num_bits(a), |
664 | 0 | (unsigned char *)qinv->d, |
665 | 0 | BN_num_bits(qinv), |
666 | 0 | (unsigned char *)dp->d, BN_num_bits(dp), |
667 | 0 | (unsigned char *)p->d, BN_num_bits(p), |
668 | 0 | (unsigned char *)dq->d, BN_num_bits(dq), |
669 | 0 | (unsigned char *)q->d, BN_num_bits(q), |
670 | 0 | (unsigned char *)r->d, &y_len) != 0) { |
671 | 0 | UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_REQUEST_FAILED); |
672 | 0 | p_UBSEC_ubsec_close(fd); |
673 | 0 | return FAIL_TO_SOFTWARE; |
674 | 0 | } |
675 | | |
676 | 0 | p_UBSEC_ubsec_close(fd); |
677 | |
|
678 | 0 | r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1) / BN_BITS2; |
679 | 0 | return 1; |
680 | 0 | } |
681 | | # endif |
682 | | |
683 | | # ifndef OPENSSL_NO_DSA |
684 | | # ifdef NOT_USED |
685 | | static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, |
686 | | BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, |
687 | | BN_CTX *ctx, BN_MONT_CTX *in_mont) |
688 | | { |
689 | | BIGNUM t; |
690 | | int to_return = 0; |
691 | | |
692 | | BN_init(&t); |
693 | | /* let rr = a1 ^ p1 mod m */ |
694 | | if (!ubsec_mod_exp(rr, a1, p1, m, ctx)) |
695 | | goto end; |
696 | | /* let t = a2 ^ p2 mod m */ |
697 | | if (!ubsec_mod_exp(&t, a2, p2, m, ctx)) |
698 | | goto end; |
699 | | /* let rr = rr * t mod m */ |
700 | | if (!BN_mod_mul(rr, rr, &t, m, ctx)) |
701 | | goto end; |
702 | | to_return = 1; |
703 | | end: |
704 | | BN_free(&t); |
705 | | return to_return; |
706 | | } |
707 | | |
708 | | static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, |
709 | | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, |
710 | | BN_MONT_CTX *m_ctx) |
711 | | { |
712 | | return ubsec_mod_exp(r, a, p, m, ctx); |
713 | | } |
714 | | # endif |
715 | | # endif |
716 | | |
717 | | # ifndef OPENSSL_NO_RSA |
718 | | |
719 | | /* |
720 | | * This function is aliased to mod_exp (with the mont stuff dropped). |
721 | | */ |
722 | | static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
723 | | const BIGNUM *m, BN_CTX *ctx, |
724 | | BN_MONT_CTX *m_ctx) |
725 | 0 | { |
726 | 0 | int ret = 0; |
727 | | |
728 | | /* Do in software if the key is too large for the hardware. */ |
729 | 0 | if (BN_num_bits(m) > max_key_len) { |
730 | 0 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); |
731 | 0 | ret = (*meth->bn_mod_exp) (r, a, p, m, ctx, m_ctx); |
732 | 0 | } else { |
733 | 0 | ret = ubsec_mod_exp(r, a, p, m, ctx); |
734 | 0 | } |
735 | |
|
736 | 0 | return ret; |
737 | 0 | } |
738 | | # endif |
739 | | |
740 | | # ifndef OPENSSL_NO_DH |
741 | | /* This function is aliased to mod_exp (with the dh and mont dropped). */ |
742 | | static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, |
743 | | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, |
744 | | BN_MONT_CTX *m_ctx) |
745 | 0 | { |
746 | 0 | return ubsec_mod_exp(r, a, p, m, ctx); |
747 | 0 | } |
748 | | # endif |
749 | | |
750 | | # ifndef OPENSSL_NO_DSA |
751 | | static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, |
752 | | DSA *dsa) |
753 | 0 | { |
754 | 0 | DSA_SIG *to_return = NULL; |
755 | 0 | int s_len = 160, r_len = 160, d_len, fd; |
756 | 0 | BIGNUM m, *r = NULL, *s = NULL; |
757 | |
|
758 | 0 | BN_init(&m); |
759 | |
|
760 | 0 | s = BN_new(); |
761 | 0 | r = BN_new(); |
762 | 0 | if ((s == NULL) || (r == NULL)) |
763 | 0 | goto err; |
764 | | |
765 | 0 | d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen); |
766 | |
|
767 | 0 | if (!bn_wexpand(r, (160 + BN_BITS2 - 1) / BN_BITS2) || |
768 | 0 | (!bn_wexpand(s, (160 + BN_BITS2 - 1) / BN_BITS2))) { |
769 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_BN_EXPAND_FAIL); |
770 | 0 | goto err; |
771 | 0 | } |
772 | | |
773 | 0 | if (BN_bin2bn(dgst, dlen, &m) == NULL) { |
774 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_BN_EXPAND_FAIL); |
775 | 0 | goto err; |
776 | 0 | } |
777 | | |
778 | 0 | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
779 | 0 | const DSA_METHOD *meth; |
780 | 0 | fd = 0; |
781 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_UNIT_FAILURE); |
782 | 0 | meth = DSA_OpenSSL(); |
783 | 0 | to_return = meth->dsa_do_sign(dgst, dlen, dsa); |
784 | 0 | goto err; |
785 | 0 | } |
786 | | |
787 | 0 | if (p_UBSEC_dsa_sign_ioctl(fd, |
788 | | /* compute hash before signing */ |
789 | 0 | 0, (unsigned char *)dgst, d_len, NULL, |
790 | | /* compute random value */ |
791 | 0 | 0, |
792 | 0 | (unsigned char *)dsa->p->d, |
793 | 0 | BN_num_bits(dsa->p), |
794 | 0 | (unsigned char *)dsa->q->d, |
795 | 0 | BN_num_bits(dsa->q), |
796 | 0 | (unsigned char *)dsa->g->d, |
797 | 0 | BN_num_bits(dsa->g), |
798 | 0 | (unsigned char *)dsa->priv_key->d, |
799 | 0 | BN_num_bits(dsa->priv_key), |
800 | 0 | (unsigned char *)r->d, &r_len, |
801 | 0 | (unsigned char *)s->d, &s_len) != 0) { |
802 | 0 | const DSA_METHOD *meth; |
803 | |
|
804 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_REQUEST_FAILED); |
805 | 0 | p_UBSEC_ubsec_close(fd); |
806 | 0 | meth = DSA_OpenSSL(); |
807 | 0 | to_return = meth->dsa_do_sign(dgst, dlen, dsa); |
808 | |
|
809 | 0 | goto err; |
810 | 0 | } |
811 | | |
812 | 0 | p_UBSEC_ubsec_close(fd); |
813 | |
|
814 | 0 | r->top = (160 + BN_BITS2 - 1) / BN_BITS2; |
815 | 0 | s->top = (160 + BN_BITS2 - 1) / BN_BITS2; |
816 | |
|
817 | 0 | to_return = DSA_SIG_new(); |
818 | 0 | if (to_return == NULL) { |
819 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_BN_EXPAND_FAIL); |
820 | 0 | goto err; |
821 | 0 | } |
822 | | |
823 | 0 | to_return->r = r; |
824 | 0 | to_return->s = s; |
825 | |
|
826 | 0 | err: |
827 | 0 | if (!to_return) { |
828 | 0 | if (r) |
829 | 0 | BN_free(r); |
830 | 0 | if (s) |
831 | 0 | BN_free(s); |
832 | 0 | } |
833 | 0 | BN_clear_free(&m); |
834 | 0 | return to_return; |
835 | 0 | } |
836 | | |
837 | | static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len, |
838 | | DSA_SIG *sig, DSA *dsa) |
839 | 0 | { |
840 | 0 | int v_len, d_len; |
841 | 0 | int to_return = 0; |
842 | 0 | int fd; |
843 | 0 | BIGNUM v, *pv = &v; |
844 | |
|
845 | 0 | BN_init(&v); |
846 | |
|
847 | 0 | if (!bn_wexpand(pv, dsa->p->top)) { |
848 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY, UBSEC_R_BN_EXPAND_FAIL); |
849 | 0 | goto err; |
850 | 0 | } |
851 | | |
852 | 0 | v_len = BN_num_bits(dsa->p); |
853 | |
|
854 | 0 | d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len); |
855 | |
|
856 | 0 | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
857 | 0 | const DSA_METHOD *meth; |
858 | 0 | fd = 0; |
859 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY, UBSEC_R_UNIT_FAILURE); |
860 | 0 | meth = DSA_OpenSSL(); |
861 | 0 | to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa); |
862 | 0 | goto err; |
863 | 0 | } |
864 | | |
865 | 0 | if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */ |
866 | 0 | (unsigned char *)dgst, d_len, |
867 | 0 | (unsigned char *)dsa->p->d, |
868 | 0 | BN_num_bits(dsa->p), |
869 | 0 | (unsigned char *)dsa->q->d, |
870 | 0 | BN_num_bits(dsa->q), |
871 | 0 | (unsigned char *)dsa->g->d, |
872 | 0 | BN_num_bits(dsa->g), |
873 | 0 | (unsigned char *)dsa->pub_key->d, |
874 | 0 | BN_num_bits(dsa->pub_key), |
875 | 0 | (unsigned char *)sig->r->d, |
876 | 0 | BN_num_bits(sig->r), |
877 | 0 | (unsigned char *)sig->s->d, |
878 | 0 | BN_num_bits(sig->s), (unsigned char *)v.d, |
879 | 0 | &v_len) != 0) { |
880 | 0 | const DSA_METHOD *meth; |
881 | 0 | UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY, UBSEC_R_REQUEST_FAILED); |
882 | 0 | p_UBSEC_ubsec_close(fd); |
883 | |
|
884 | 0 | meth = DSA_OpenSSL(); |
885 | 0 | to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa); |
886 | |
|
887 | 0 | goto err; |
888 | 0 | } |
889 | | |
890 | 0 | p_UBSEC_ubsec_close(fd); |
891 | |
|
892 | 0 | to_return = 1; |
893 | 0 | err: |
894 | 0 | BN_clear_free(&v); |
895 | 0 | return to_return; |
896 | 0 | } |
897 | | # endif |
898 | | |
899 | | # ifndef OPENSSL_NO_DH |
900 | | static int ubsec_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, |
901 | | DH *dh) |
902 | 0 | { |
903 | 0 | int ret = -1, k_len, fd; |
904 | |
|
905 | 0 | k_len = BN_num_bits(dh->p); |
906 | |
|
907 | 0 | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
908 | 0 | const DH_METHOD *meth; |
909 | 0 | UBSECerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_UNIT_FAILURE); |
910 | 0 | meth = DH_OpenSSL(); |
911 | 0 | ret = meth->compute_key(key, pub_key, dh); |
912 | 0 | goto err; |
913 | 0 | } |
914 | | |
915 | 0 | if (p_UBSEC_diffie_hellman_agree_ioctl(fd, |
916 | 0 | (unsigned char *)dh->priv_key->d, |
917 | 0 | BN_num_bits(dh->priv_key), |
918 | 0 | (unsigned char *)pub_key->d, |
919 | 0 | BN_num_bits(pub_key), |
920 | 0 | (unsigned char *)dh->p->d, |
921 | 0 | BN_num_bits(dh->p), key, |
922 | 0 | &k_len) != 0) { |
923 | | /* Hardware's a no go, failover to software */ |
924 | 0 | const DH_METHOD *meth; |
925 | 0 | UBSECerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED); |
926 | 0 | p_UBSEC_ubsec_close(fd); |
927 | |
|
928 | 0 | meth = DH_OpenSSL(); |
929 | 0 | ret = meth->compute_key(key, pub_key, dh); |
930 | |
|
931 | 0 | goto err; |
932 | 0 | } |
933 | | |
934 | 0 | p_UBSEC_ubsec_close(fd); |
935 | |
|
936 | 0 | ret = p_UBSEC_ubsec_bits_to_bytes(k_len); |
937 | 0 | err: |
938 | 0 | return ret; |
939 | 0 | } |
940 | | |
941 | | static int ubsec_dh_generate_key(DH *dh) |
942 | 0 | { |
943 | 0 | int ret = 0, random_bits = 0, pub_key_len = 0, priv_key_len = 0, fd; |
944 | 0 | BIGNUM *pub_key = NULL; |
945 | 0 | BIGNUM *priv_key = NULL; |
946 | | |
947 | | /* |
948 | | * How many bits should Random x be? dh_key.c |
949 | | * sets the range from 0 to num_bits(modulus) ??? |
950 | | */ |
951 | |
|
952 | 0 | if (dh->priv_key == NULL) { |
953 | 0 | priv_key = BN_new(); |
954 | 0 | if (priv_key == NULL) |
955 | 0 | goto err; |
956 | 0 | priv_key_len = BN_num_bits(dh->p); |
957 | 0 | if (bn_wexpand(priv_key, dh->p->top) == NULL) |
958 | 0 | goto err; |
959 | 0 | do |
960 | 0 | if (!BN_rand_range(priv_key, dh->p)) |
961 | 0 | goto err; |
962 | 0 | while (BN_is_zero(priv_key)) ; |
963 | 0 | random_bits = BN_num_bits(priv_key); |
964 | 0 | } else { |
965 | 0 | priv_key = dh->priv_key; |
966 | 0 | } |
967 | | |
968 | 0 | if (dh->pub_key == NULL) { |
969 | 0 | pub_key = BN_new(); |
970 | 0 | if (pub_key == NULL) |
971 | 0 | goto err; |
972 | 0 | pub_key_len = BN_num_bits(dh->p); |
973 | 0 | if (bn_wexpand(pub_key, dh->p->top) == NULL) |
974 | 0 | goto err; |
975 | 0 | } else { |
976 | 0 | pub_key = dh->pub_key; |
977 | 0 | } |
978 | | |
979 | 0 | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
980 | 0 | const DH_METHOD *meth; |
981 | 0 | UBSECerr(UBSEC_F_UBSEC_DH_GENERATE_KEY, UBSEC_R_UNIT_FAILURE); |
982 | 0 | meth = DH_OpenSSL(); |
983 | 0 | ret = meth->generate_key(dh); |
984 | 0 | goto err; |
985 | 0 | } |
986 | | |
987 | 0 | if (p_UBSEC_diffie_hellman_generate_ioctl(fd, |
988 | 0 | (unsigned char *)priv_key->d, |
989 | 0 | &priv_key_len, |
990 | 0 | (unsigned char *)pub_key->d, |
991 | 0 | &pub_key_len, |
992 | 0 | (unsigned char *)dh->g->d, |
993 | 0 | BN_num_bits(dh->g), |
994 | 0 | (unsigned char *)dh->p->d, |
995 | 0 | BN_num_bits(dh->p), 0, 0, |
996 | 0 | random_bits) != 0) { |
997 | | /* Hardware's a no go, failover to software */ |
998 | 0 | const DH_METHOD *meth; |
999 | |
|
1000 | 0 | UBSECerr(UBSEC_F_UBSEC_DH_GENERATE_KEY, UBSEC_R_REQUEST_FAILED); |
1001 | 0 | p_UBSEC_ubsec_close(fd); |
1002 | |
|
1003 | 0 | meth = DH_OpenSSL(); |
1004 | 0 | ret = meth->generate_key(dh); |
1005 | |
|
1006 | 0 | goto err; |
1007 | 0 | } |
1008 | | |
1009 | 0 | p_UBSEC_ubsec_close(fd); |
1010 | |
|
1011 | 0 | dh->pub_key = pub_key; |
1012 | 0 | dh->pub_key->top = (pub_key_len + BN_BITS2 - 1) / BN_BITS2; |
1013 | 0 | dh->priv_key = priv_key; |
1014 | 0 | dh->priv_key->top = (priv_key_len + BN_BITS2 - 1) / BN_BITS2; |
1015 | |
|
1016 | 0 | ret = 1; |
1017 | 0 | err: |
1018 | 0 | return ret; |
1019 | 0 | } |
1020 | | # endif |
1021 | | |
1022 | | # ifdef NOT_USED |
1023 | | static int ubsec_rand_bytes(unsigned char *buf, int num) |
1024 | | { |
1025 | | int ret = 0, fd; |
1026 | | |
1027 | | if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) { |
1028 | | const RAND_METHOD *meth; |
1029 | | UBSECerr(UBSEC_F_UBSEC_RAND_BYTES, UBSEC_R_UNIT_FAILURE); |
1030 | | num = p_UBSEC_ubsec_bits_to_bytes(num); |
1031 | | meth = RAND_SSLeay(); |
1032 | | meth->seed(buf, num); |
1033 | | ret = meth->bytes(buf, num); |
1034 | | goto err; |
1035 | | } |
1036 | | |
1037 | | num *= 8; /* bytes to bits */ |
1038 | | |
1039 | | if (p_UBSEC_rng_ioctl(fd, UBSEC_RNG_DIRECT, buf, &num) != 0) { |
1040 | | /* Hardware's a no go, failover to software */ |
1041 | | const RAND_METHOD *meth; |
1042 | | |
1043 | | UBSECerr(UBSEC_F_UBSEC_RAND_BYTES, UBSEC_R_REQUEST_FAILED); |
1044 | | p_UBSEC_ubsec_close(fd); |
1045 | | |
1046 | | num = p_UBSEC_ubsec_bits_to_bytes(num); |
1047 | | meth = RAND_SSLeay(); |
1048 | | meth->seed(buf, num); |
1049 | | ret = meth->bytes(buf, num); |
1050 | | |
1051 | | goto err; |
1052 | | } |
1053 | | |
1054 | | p_UBSEC_ubsec_close(fd); |
1055 | | |
1056 | | ret = 1; |
1057 | | err: |
1058 | | return (ret); |
1059 | | } |
1060 | | |
1061 | | static int ubsec_rand_status(void) |
1062 | | { |
1063 | | return 0; |
1064 | | } |
1065 | | # endif |
1066 | | |
1067 | | /* |
1068 | | * This stuff is needed if this ENGINE is being compiled into a |
1069 | | * self-contained shared-library. |
1070 | | */ |
1071 | | # ifndef OPENSSL_NO_DYNAMIC_ENGINE |
1072 | | static int bind_fn(ENGINE *e, const char *id) |
1073 | | { |
1074 | | if (id && (strcmp(id, engine_ubsec_id) != 0)) |
1075 | | return 0; |
1076 | | if (!bind_helper(e)) |
1077 | | return 0; |
1078 | | return 1; |
1079 | | } |
1080 | | |
1081 | | IMPLEMENT_DYNAMIC_CHECK_FN() |
1082 | | IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) |
1083 | | # endif /* OPENSSL_NO_DYNAMIC_ENGINE */ |
1084 | | # endif /* !OPENSSL_NO_HW_UBSEC */ |
1085 | | #endif /* !OPENSSL_NO_HW */ |