diff options
author | djm <> | 2010-10-01 22:59:01 +0000 |
---|---|---|
committer | djm <> | 2010-10-01 22:59:01 +0000 |
commit | fe047d8b632246cb2db3234a0a4f32e5c318857b (patch) | |
tree | 939b752540947d33507b3acc48d76a8bfb7c3dc3 /src/lib/libcrypto/engine/hw_atalla.c | |
parent | 2ea67f4aa254b09ded62e6e14fc893bbe6381579 (diff) | |
download | openbsd-fe047d8b632246cb2db3234a0a4f32e5c318857b.tar.gz openbsd-fe047d8b632246cb2db3234a0a4f32e5c318857b.tar.bz2 openbsd-fe047d8b632246cb2db3234a0a4f32e5c318857b.zip |
resolve conflicts, fix local changes
Diffstat (limited to 'src/lib/libcrypto/engine/hw_atalla.c')
-rw-r--r-- | src/lib/libcrypto/engine/hw_atalla.c | 595 |
1 files changed, 0 insertions, 595 deletions
diff --git a/src/lib/libcrypto/engine/hw_atalla.c b/src/lib/libcrypto/engine/hw_atalla.c deleted file mode 100644 index 2b8342bbdd..0000000000 --- a/src/lib/libcrypto/engine/hw_atalla.c +++ /dev/null | |||
@@ -1,595 +0,0 @@ | |||
1 | /* crypto/engine/hw_atalla.c */ | ||
2 | /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL | ||
3 | * project 2000. | ||
4 | */ | ||
5 | /* ==================================================================== | ||
6 | * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in | ||
17 | * the documentation and/or other materials provided with the | ||
18 | * distribution. | ||
19 | * | ||
20 | * 3. All advertising materials mentioning features or use of this | ||
21 | * software must display the following acknowledgment: | ||
22 | * "This product includes software developed by the OpenSSL Project | ||
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
24 | * | ||
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
26 | * endorse or promote products derived from this software without | ||
27 | * prior written permission. For written permission, please contact | ||
28 | * licensing@OpenSSL.org. | ||
29 | * | ||
30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
31 | * nor may "OpenSSL" appear in their names without prior written | ||
32 | * permission of the OpenSSL Project. | ||
33 | * | ||
34 | * 6. Redistributions of any form whatsoever must retain the following | ||
35 | * acknowledgment: | ||
36 | * "This product includes software developed by the OpenSSL Project | ||
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
38 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
51 | * ==================================================================== | ||
52 | * | ||
53 | * This product includes cryptographic software written by Eric Young | ||
54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
55 | * Hudson (tjh@cryptsoft.com). | ||
56 | * | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include <openssl/crypto.h> | ||
61 | #include "cryptlib.h" | ||
62 | #include <openssl/dso.h> | ||
63 | #include <openssl/engine.h> | ||
64 | |||
65 | #ifndef OPENSSL_NO_HW | ||
66 | #ifndef OPENSSL_NO_HW_ATALLA | ||
67 | |||
68 | #ifdef FLAT_INC | ||
69 | #include "atalla.h" | ||
70 | #else | ||
71 | #include "vendor_defns/atalla.h" | ||
72 | #endif | ||
73 | |||
74 | #define ATALLA_LIB_NAME "atalla engine" | ||
75 | #include "hw_atalla_err.c" | ||
76 | |||
77 | static int atalla_destroy(ENGINE *e); | ||
78 | static int atalla_init(ENGINE *e); | ||
79 | static int atalla_finish(ENGINE *e); | ||
80 | static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); | ||
81 | |||
82 | /* BIGNUM stuff */ | ||
83 | static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
84 | const BIGNUM *m, BN_CTX *ctx); | ||
85 | |||
86 | #ifndef OPENSSL_NO_RSA | ||
87 | /* RSA stuff */ | ||
88 | static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa); | ||
89 | #endif | ||
90 | /* This function is aliased to mod_exp (with the mont stuff dropped). */ | ||
91 | static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
92 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
93 | |||
94 | #ifndef OPENSSL_NO_DSA | ||
95 | /* DSA stuff */ | ||
96 | static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, | ||
97 | BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, | ||
98 | BN_CTX *ctx, BN_MONT_CTX *in_mont); | ||
99 | static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, | ||
100 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
101 | BN_MONT_CTX *m_ctx); | ||
102 | #endif | ||
103 | |||
104 | #ifndef OPENSSL_NO_DH | ||
105 | /* DH stuff */ | ||
106 | /* This function is alised to mod_exp (with the DH and mont dropped). */ | ||
107 | static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r, | ||
108 | const BIGNUM *a, const BIGNUM *p, | ||
109 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
110 | #endif | ||
111 | |||
112 | /* The definitions for control commands specific to this engine */ | ||
113 | #define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE | ||
114 | static const ENGINE_CMD_DEFN atalla_cmd_defns[] = { | ||
115 | {ATALLA_CMD_SO_PATH, | ||
116 | "SO_PATH", | ||
117 | "Specifies the path to the 'atasi' shared library", | ||
118 | ENGINE_CMD_FLAG_STRING}, | ||
119 | {0, NULL, NULL, 0} | ||
120 | }; | ||
121 | |||
122 | #ifndef OPENSSL_NO_RSA | ||
123 | /* Our internal RSA_METHOD that we provide pointers to */ | ||
124 | static RSA_METHOD atalla_rsa = | ||
125 | { | ||
126 | "Atalla RSA method", | ||
127 | NULL, | ||
128 | NULL, | ||
129 | NULL, | ||
130 | NULL, | ||
131 | atalla_rsa_mod_exp, | ||
132 | atalla_mod_exp_mont, | ||
133 | NULL, | ||
134 | NULL, | ||
135 | 0, | ||
136 | NULL, | ||
137 | NULL, | ||
138 | NULL | ||
139 | }; | ||
140 | #endif | ||
141 | |||
142 | #ifndef OPENSSL_NO_DSA | ||
143 | /* Our internal DSA_METHOD that we provide pointers to */ | ||
144 | static DSA_METHOD atalla_dsa = | ||
145 | { | ||
146 | "Atalla DSA method", | ||
147 | NULL, /* dsa_do_sign */ | ||
148 | NULL, /* dsa_sign_setup */ | ||
149 | NULL, /* dsa_do_verify */ | ||
150 | atalla_dsa_mod_exp, /* dsa_mod_exp */ | ||
151 | atalla_mod_exp_dsa, /* bn_mod_exp */ | ||
152 | NULL, /* init */ | ||
153 | NULL, /* finish */ | ||
154 | 0, /* flags */ | ||
155 | NULL /* app_data */ | ||
156 | }; | ||
157 | #endif | ||
158 | |||
159 | #ifndef OPENSSL_NO_DH | ||
160 | /* Our internal DH_METHOD that we provide pointers to */ | ||
161 | static DH_METHOD atalla_dh = | ||
162 | { | ||
163 | "Atalla DH method", | ||
164 | NULL, | ||
165 | NULL, | ||
166 | atalla_mod_exp_dh, | ||
167 | NULL, | ||
168 | NULL, | ||
169 | 0, | ||
170 | NULL | ||
171 | }; | ||
172 | #endif | ||
173 | |||
174 | /* Constants used when creating the ENGINE */ | ||
175 | static const char *engine_atalla_id = "atalla"; | ||
176 | static const char *engine_atalla_name = "Atalla hardware engine support"; | ||
177 | |||
178 | /* This internal function is used by ENGINE_atalla() and possibly by the | ||
179 | * "dynamic" ENGINE support too */ | ||
180 | static int bind_helper(ENGINE *e) | ||
181 | { | ||
182 | #ifndef OPENSSL_NO_RSA | ||
183 | const RSA_METHOD *meth1; | ||
184 | #endif | ||
185 | #ifndef OPENSSL_NO_DSA | ||
186 | const DSA_METHOD *meth2; | ||
187 | #endif | ||
188 | #ifndef OPENSSL_NO_DH | ||
189 | const DH_METHOD *meth3; | ||
190 | #endif | ||
191 | if(!ENGINE_set_id(e, engine_atalla_id) || | ||
192 | !ENGINE_set_name(e, engine_atalla_name) || | ||
193 | #ifndef OPENSSL_NO_RSA | ||
194 | !ENGINE_set_RSA(e, &atalla_rsa) || | ||
195 | #endif | ||
196 | #ifndef OPENSSL_NO_DSA | ||
197 | !ENGINE_set_DSA(e, &atalla_dsa) || | ||
198 | #endif | ||
199 | #ifndef OPENSSL_NO_DH | ||
200 | !ENGINE_set_DH(e, &atalla_dh) || | ||
201 | #endif | ||
202 | !ENGINE_set_destroy_function(e, atalla_destroy) || | ||
203 | !ENGINE_set_init_function(e, atalla_init) || | ||
204 | !ENGINE_set_finish_function(e, atalla_finish) || | ||
205 | !ENGINE_set_ctrl_function(e, atalla_ctrl) || | ||
206 | !ENGINE_set_cmd_defns(e, atalla_cmd_defns)) | ||
207 | return 0; | ||
208 | |||
209 | #ifndef OPENSSL_NO_RSA | ||
210 | /* We know that the "PKCS1_SSLeay()" functions hook properly | ||
211 | * to the atalla-specific mod_exp and mod_exp_crt so we use | ||
212 | * those functions. NB: We don't use ENGINE_openssl() or | ||
213 | * anything "more generic" because something like the RSAref | ||
214 | * code may not hook properly, and if you own one of these | ||
215 | * cards then you have the right to do RSA operations on it | ||
216 | * anyway! */ | ||
217 | meth1 = RSA_PKCS1_SSLeay(); | ||
218 | atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc; | ||
219 | atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec; | ||
220 | atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc; | ||
221 | atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec; | ||
222 | #endif | ||
223 | |||
224 | #ifndef OPENSSL_NO_DSA | ||
225 | /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish | ||
226 | * bits. */ | ||
227 | meth2 = DSA_OpenSSL(); | ||
228 | atalla_dsa.dsa_do_sign = meth2->dsa_do_sign; | ||
229 | atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup; | ||
230 | atalla_dsa.dsa_do_verify = meth2->dsa_do_verify; | ||
231 | #endif | ||
232 | |||
233 | #ifndef OPENSSL_NO_DH | ||
234 | /* Much the same for Diffie-Hellman */ | ||
235 | meth3 = DH_OpenSSL(); | ||
236 | atalla_dh.generate_key = meth3->generate_key; | ||
237 | atalla_dh.compute_key = meth3->compute_key; | ||
238 | #endif | ||
239 | |||
240 | /* Ensure the atalla error handling is set up */ | ||
241 | ERR_load_ATALLA_strings(); | ||
242 | return 1; | ||
243 | } | ||
244 | |||
245 | #ifndef ENGINE_DYNAMIC_SUPPORT | ||
246 | static ENGINE *engine_atalla(void) | ||
247 | { | ||
248 | ENGINE *ret = ENGINE_new(); | ||
249 | if(!ret) | ||
250 | return NULL; | ||
251 | if(!bind_helper(ret)) | ||
252 | { | ||
253 | ENGINE_free(ret); | ||
254 | return NULL; | ||
255 | } | ||
256 | return ret; | ||
257 | } | ||
258 | |||
259 | void ENGINE_load_atalla(void) | ||
260 | { | ||
261 | /* Copied from eng_[openssl|dyn].c */ | ||
262 | ENGINE *toadd = engine_atalla(); | ||
263 | if(!toadd) return; | ||
264 | ENGINE_add(toadd); | ||
265 | ENGINE_free(toadd); | ||
266 | ERR_clear_error(); | ||
267 | } | ||
268 | #endif | ||
269 | |||
270 | /* This is a process-global DSO handle used for loading and unloading | ||
271 | * the Atalla library. NB: This is only set (or unset) during an | ||
272 | * init() or finish() call (reference counts permitting) and they're | ||
273 | * operating with global locks, so this should be thread-safe | ||
274 | * implicitly. */ | ||
275 | static DSO *atalla_dso = NULL; | ||
276 | |||
277 | /* These are the function pointers that are (un)set when the library has | ||
278 | * successfully (un)loaded. */ | ||
279 | static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL; | ||
280 | static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL; | ||
281 | static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL; | ||
282 | |||
283 | /* These are the static string constants for the DSO file name and the function | ||
284 | * symbol names to bind to. Regrettably, the DSO name on *nix appears to be | ||
285 | * "atasi.so" rather than something more consistent like "libatasi.so". At the | ||
286 | * time of writing, I'm not sure what the file name on win32 is but clearly | ||
287 | * native name translation is not possible (eg libatasi.so on *nix, and | ||
288 | * atasi.dll on win32). For the purposes of testing, I have created a symbollic | ||
289 | * link called "libatasi.so" so that we can use native name-translation - a | ||
290 | * better solution will be needed. */ | ||
291 | static const char *ATALLA_LIBNAME = NULL; | ||
292 | static const char *get_ATALLA_LIBNAME(void) | ||
293 | { | ||
294 | if(ATALLA_LIBNAME) | ||
295 | return ATALLA_LIBNAME; | ||
296 | return "atasi"; | ||
297 | } | ||
298 | static void free_ATALLA_LIBNAME(void) | ||
299 | { | ||
300 | if(ATALLA_LIBNAME) | ||
301 | OPENSSL_free((void*)ATALLA_LIBNAME); | ||
302 | ATALLA_LIBNAME = NULL; | ||
303 | } | ||
304 | static long set_ATALLA_LIBNAME(const char *name) | ||
305 | { | ||
306 | free_ATALLA_LIBNAME(); | ||
307 | return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0); | ||
308 | } | ||
309 | static const char *ATALLA_F1 = "ASI_GetHardwareConfig"; | ||
310 | static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn"; | ||
311 | static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics"; | ||
312 | |||
313 | /* Destructor (complements the "ENGINE_atalla()" constructor) */ | ||
314 | static int atalla_destroy(ENGINE *e) | ||
315 | { | ||
316 | free_ATALLA_LIBNAME(); | ||
317 | /* Unload the atalla error strings so any error state including our | ||
318 | * functs or reasons won't lead to a segfault (they simply get displayed | ||
319 | * without corresponding string data because none will be found). */ | ||
320 | ERR_unload_ATALLA_strings(); | ||
321 | return 1; | ||
322 | } | ||
323 | |||
324 | /* (de)initialisation functions. */ | ||
325 | static int atalla_init(ENGINE *e) | ||
326 | { | ||
327 | tfnASI_GetHardwareConfig *p1; | ||
328 | tfnASI_RSAPrivateKeyOpFn *p2; | ||
329 | tfnASI_GetPerformanceStatistics *p3; | ||
330 | /* Not sure of the origin of this magic value, but Ben's code had it | ||
331 | * and it seemed to have been working for a few people. :-) */ | ||
332 | unsigned int config_buf[1024]; | ||
333 | |||
334 | if(atalla_dso != NULL) | ||
335 | { | ||
336 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED); | ||
337 | goto err; | ||
338 | } | ||
339 | /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be | ||
340 | * changed unfortunately because the Atalla drivers don't have | ||
341 | * standard library names that can be platform-translated well. */ | ||
342 | /* TODO: Work out how to actually map to the names the Atalla | ||
343 | * drivers really use - for now a symbollic link needs to be | ||
344 | * created on the host system from libatasi.so to atasi.so on | ||
345 | * unix variants. */ | ||
346 | atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0); | ||
347 | if(atalla_dso == NULL) | ||
348 | { | ||
349 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED); | ||
350 | goto err; | ||
351 | } | ||
352 | if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func( | ||
353 | atalla_dso, ATALLA_F1)) || | ||
354 | !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func( | ||
355 | atalla_dso, ATALLA_F2)) || | ||
356 | !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func( | ||
357 | atalla_dso, ATALLA_F3))) | ||
358 | { | ||
359 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED); | ||
360 | goto err; | ||
361 | } | ||
362 | /* Copy the pointers */ | ||
363 | p_Atalla_GetHardwareConfig = p1; | ||
364 | p_Atalla_RSAPrivateKeyOpFn = p2; | ||
365 | p_Atalla_GetPerformanceStatistics = p3; | ||
366 | /* Perform a basic test to see if there's actually any unit | ||
367 | * running. */ | ||
368 | if(p1(0L, config_buf) != 0) | ||
369 | { | ||
370 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE); | ||
371 | goto err; | ||
372 | } | ||
373 | /* Everything's fine. */ | ||
374 | return 1; | ||
375 | err: | ||
376 | if(atalla_dso) | ||
377 | DSO_free(atalla_dso); | ||
378 | atalla_dso = NULL; | ||
379 | p_Atalla_GetHardwareConfig = NULL; | ||
380 | p_Atalla_RSAPrivateKeyOpFn = NULL; | ||
381 | p_Atalla_GetPerformanceStatistics = NULL; | ||
382 | return 0; | ||
383 | } | ||
384 | |||
385 | static int atalla_finish(ENGINE *e) | ||
386 | { | ||
387 | free_ATALLA_LIBNAME(); | ||
388 | if(atalla_dso == NULL) | ||
389 | { | ||
390 | ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED); | ||
391 | return 0; | ||
392 | } | ||
393 | if(!DSO_free(atalla_dso)) | ||
394 | { | ||
395 | ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE); | ||
396 | return 0; | ||
397 | } | ||
398 | atalla_dso = NULL; | ||
399 | p_Atalla_GetHardwareConfig = NULL; | ||
400 | p_Atalla_RSAPrivateKeyOpFn = NULL; | ||
401 | p_Atalla_GetPerformanceStatistics = NULL; | ||
402 | return 1; | ||
403 | } | ||
404 | |||
405 | static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()) | ||
406 | { | ||
407 | int initialised = ((atalla_dso == NULL) ? 0 : 1); | ||
408 | switch(cmd) | ||
409 | { | ||
410 | case ATALLA_CMD_SO_PATH: | ||
411 | if(p == NULL) | ||
412 | { | ||
413 | ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER); | ||
414 | return 0; | ||
415 | } | ||
416 | if(initialised) | ||
417 | { | ||
418 | ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED); | ||
419 | return 0; | ||
420 | } | ||
421 | return set_ATALLA_LIBNAME((const char *)p); | ||
422 | default: | ||
423 | break; | ||
424 | } | ||
425 | ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED); | ||
426 | return 0; | ||
427 | } | ||
428 | |||
429 | static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
430 | const BIGNUM *m, BN_CTX *ctx) | ||
431 | { | ||
432 | /* I need somewhere to store temporary serialised values for | ||
433 | * use with the Atalla API calls. A neat cheat - I'll use | ||
434 | * BIGNUMs from the BN_CTX but access their arrays directly as | ||
435 | * byte arrays <grin>. This way I don't have to clean anything | ||
436 | * up. */ | ||
437 | BIGNUM *modulus; | ||
438 | BIGNUM *exponent; | ||
439 | BIGNUM *argument; | ||
440 | BIGNUM *result; | ||
441 | RSAPrivateKey keydata; | ||
442 | int to_return, numbytes; | ||
443 | |||
444 | modulus = exponent = argument = result = NULL; | ||
445 | to_return = 0; /* expect failure */ | ||
446 | |||
447 | if(!atalla_dso) | ||
448 | { | ||
449 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED); | ||
450 | goto err; | ||
451 | } | ||
452 | /* Prepare the params */ | ||
453 | BN_CTX_start(ctx); | ||
454 | modulus = BN_CTX_get(ctx); | ||
455 | exponent = BN_CTX_get(ctx); | ||
456 | argument = BN_CTX_get(ctx); | ||
457 | result = BN_CTX_get(ctx); | ||
458 | if (!result) | ||
459 | { | ||
460 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL); | ||
461 | goto err; | ||
462 | } | ||
463 | if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) || | ||
464 | !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top)) | ||
465 | { | ||
466 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL); | ||
467 | goto err; | ||
468 | } | ||
469 | /* Prepare the key-data */ | ||
470 | memset(&keydata, 0,sizeof keydata); | ||
471 | numbytes = BN_num_bytes(m); | ||
472 | memset(exponent->d, 0, numbytes); | ||
473 | memset(modulus->d, 0, numbytes); | ||
474 | BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p)); | ||
475 | BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m)); | ||
476 | keydata.privateExponent.data = (unsigned char *)exponent->d; | ||
477 | keydata.privateExponent.len = numbytes; | ||
478 | keydata.modulus.data = (unsigned char *)modulus->d; | ||
479 | keydata.modulus.len = numbytes; | ||
480 | /* Prepare the argument */ | ||
481 | memset(argument->d, 0, numbytes); | ||
482 | memset(result->d, 0, numbytes); | ||
483 | BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a)); | ||
484 | /* Perform the operation */ | ||
485 | if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d, | ||
486 | (unsigned char *)argument->d, | ||
487 | keydata.modulus.len) != 0) | ||
488 | { | ||
489 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED); | ||
490 | goto err; | ||
491 | } | ||
492 | /* Convert the response */ | ||
493 | BN_bin2bn((unsigned char *)result->d, numbytes, r); | ||
494 | to_return = 1; | ||
495 | err: | ||
496 | BN_CTX_end(ctx); | ||
497 | return to_return; | ||
498 | } | ||
499 | |||
500 | #ifndef OPENSSL_NO_RSA | ||
501 | static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | ||
502 | { | ||
503 | BN_CTX *ctx = NULL; | ||
504 | int to_return = 0; | ||
505 | |||
506 | if(!atalla_dso) | ||
507 | { | ||
508 | ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED); | ||
509 | goto err; | ||
510 | } | ||
511 | if((ctx = BN_CTX_new()) == NULL) | ||
512 | goto err; | ||
513 | if(!rsa->d || !rsa->n) | ||
514 | { | ||
515 | ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS); | ||
516 | goto err; | ||
517 | } | ||
518 | to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx); | ||
519 | err: | ||
520 | if(ctx) | ||
521 | BN_CTX_free(ctx); | ||
522 | return to_return; | ||
523 | } | ||
524 | #endif | ||
525 | |||
526 | #ifndef OPENSSL_NO_DSA | ||
527 | /* This code was liberated and adapted from the commented-out code in | ||
528 | * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration | ||
529 | * (it doesn't have a CRT form for RSA), this function means that an | ||
530 | * Atalla system running with a DSA server certificate can handshake | ||
531 | * around 5 or 6 times faster/more than an equivalent system running with | ||
532 | * RSA. Just check out the "signs" statistics from the RSA and DSA parts | ||
533 | * of "openssl speed -engine atalla dsa1024 rsa1024". */ | ||
534 | static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, | ||
535 | BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, | ||
536 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
537 | { | ||
538 | BIGNUM t; | ||
539 | int to_return = 0; | ||
540 | |||
541 | BN_init(&t); | ||
542 | /* let rr = a1 ^ p1 mod m */ | ||
543 | if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end; | ||
544 | /* let t = a2 ^ p2 mod m */ | ||
545 | if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end; | ||
546 | /* let rr = rr * t mod m */ | ||
547 | if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end; | ||
548 | to_return = 1; | ||
549 | end: | ||
550 | BN_free(&t); | ||
551 | return to_return; | ||
552 | } | ||
553 | |||
554 | static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, | ||
555 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
556 | BN_MONT_CTX *m_ctx) | ||
557 | { | ||
558 | return atalla_mod_exp(r, a, p, m, ctx); | ||
559 | } | ||
560 | #endif | ||
561 | |||
562 | /* This function is aliased to mod_exp (with the mont stuff dropped). */ | ||
563 | static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
564 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | ||
565 | { | ||
566 | return atalla_mod_exp(r, a, p, m, ctx); | ||
567 | } | ||
568 | |||
569 | #ifndef OPENSSL_NO_DH | ||
570 | /* This function is aliased to mod_exp (with the dh and mont dropped). */ | ||
571 | static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r, | ||
572 | const BIGNUM *a, const BIGNUM *p, | ||
573 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | ||
574 | { | ||
575 | return atalla_mod_exp(r, a, p, m, ctx); | ||
576 | } | ||
577 | #endif | ||
578 | |||
579 | /* This stuff is needed if this ENGINE is being compiled into a self-contained | ||
580 | * shared-library. */ | ||
581 | #ifdef ENGINE_DYNAMIC_SUPPORT | ||
582 | static int bind_fn(ENGINE *e, const char *id) | ||
583 | { | ||
584 | if(id && (strcmp(id, engine_atalla_id) != 0)) | ||
585 | return 0; | ||
586 | if(!bind_helper(e)) | ||
587 | return 0; | ||
588 | return 1; | ||
589 | } | ||
590 | IMPLEMENT_DYNAMIC_CHECK_FN() | ||
591 | IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) | ||
592 | #endif /* ENGINE_DYNAMIC_SUPPORT */ | ||
593 | |||
594 | #endif /* !OPENSSL_NO_HW_ATALLA */ | ||
595 | #endif /* !OPENSSL_NO_HW */ | ||