]>
Commit | Line | Data |
---|---|---|
5572f482 RL |
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> | |
5be1264b | 60 | #include <string.h> |
5572f482 | 61 | #include <openssl/crypto.h> |
665dc392 | 62 | #include <openssl/buffer.h> |
5572f482 RL |
63 | #include <openssl/dso.h> |
64 | #include <openssl/engine.h> | |
65 | ||
66 | #ifndef OPENSSL_NO_HW | |
67 | #ifndef OPENSSL_NO_HW_ATALLA | |
68 | ||
69 | #ifdef FLAT_INC | |
70 | #include "atalla.h" | |
71 | #else | |
72 | #include "vendor_defns/atalla.h" | |
73 | #endif | |
74 | ||
75 | #define ATALLA_LIB_NAME "atalla engine" | |
665dc392 | 76 | #include "e_atalla_err.c" |
5572f482 RL |
77 | |
78 | static int atalla_destroy(ENGINE *e); | |
79 | static int atalla_init(ENGINE *e); | |
80 | static int atalla_finish(ENGINE *e); | |
a99ce1a5 | 81 | static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)); |
5572f482 RL |
82 | |
83 | /* BIGNUM stuff */ | |
84 | static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |
85 | const BIGNUM *m, BN_CTX *ctx); | |
86 | ||
87 | #ifndef OPENSSL_NO_RSA | |
88 | /* RSA stuff */ | |
46ef873f | 89 | static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); |
5572f482 RL |
90 | #endif |
91 | /* This function is aliased to mod_exp (with the mont stuff dropped). */ | |
92 | static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |
93 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | |
94 | ||
95 | #ifndef OPENSSL_NO_DSA | |
96 | /* DSA stuff */ | |
97 | static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, | |
98 | BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, | |
99 | BN_CTX *ctx, BN_MONT_CTX *in_mont); | |
100 | static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, | |
101 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | |
102 | BN_MONT_CTX *m_ctx); | |
103 | #endif | |
104 | ||
105 | #ifndef OPENSSL_NO_DH | |
106 | /* DH stuff */ | |
107 | /* This function is alised to mod_exp (with the DH and mont dropped). */ | |
108 | static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r, | |
109 | const BIGNUM *a, const BIGNUM *p, | |
110 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | |
111 | #endif | |
112 | ||
113 | /* The definitions for control commands specific to this engine */ | |
114 | #define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE | |
115 | static const ENGINE_CMD_DEFN atalla_cmd_defns[] = { | |
116 | {ATALLA_CMD_SO_PATH, | |
117 | "SO_PATH", | |
118 | "Specifies the path to the 'atasi' shared library", | |
119 | ENGINE_CMD_FLAG_STRING}, | |
120 | {0, NULL, NULL, 0} | |
121 | }; | |
122 | ||
123 | #ifndef OPENSSL_NO_RSA | |
124 | /* Our internal RSA_METHOD that we provide pointers to */ | |
125 | static RSA_METHOD atalla_rsa = | |
126 | { | |
127 | "Atalla RSA method", | |
128 | NULL, | |
129 | NULL, | |
130 | NULL, | |
131 | NULL, | |
132 | atalla_rsa_mod_exp, | |
133 | atalla_mod_exp_mont, | |
134 | NULL, | |
135 | NULL, | |
136 | 0, | |
137 | NULL, | |
138 | NULL, | |
4ebb5293 | 139 | NULL, |
5572f482 RL |
140 | NULL |
141 | }; | |
142 | #endif | |
143 | ||
144 | #ifndef OPENSSL_NO_DSA | |
145 | /* Our internal DSA_METHOD that we provide pointers to */ | |
146 | static DSA_METHOD atalla_dsa = | |
147 | { | |
148 | "Atalla DSA method", | |
149 | NULL, /* dsa_do_sign */ | |
150 | NULL, /* dsa_sign_setup */ | |
151 | NULL, /* dsa_do_verify */ | |
152 | atalla_dsa_mod_exp, /* dsa_mod_exp */ | |
153 | atalla_mod_exp_dsa, /* bn_mod_exp */ | |
154 | NULL, /* init */ | |
155 | NULL, /* finish */ | |
156 | 0, /* flags */ | |
0e4aa0d2 GT |
157 | NULL, /* app_data */ |
158 | NULL, /* dsa_paramgen */ | |
159 | NULL /* dsa_keygen */ | |
5572f482 RL |
160 | }; |
161 | #endif | |
162 | ||
163 | #ifndef OPENSSL_NO_DH | |
164 | /* Our internal DH_METHOD that we provide pointers to */ | |
165 | static DH_METHOD atalla_dh = | |
166 | { | |
167 | "Atalla DH method", | |
168 | NULL, | |
169 | NULL, | |
170 | atalla_mod_exp_dh, | |
171 | NULL, | |
172 | NULL, | |
173 | 0, | |
0e4aa0d2 | 174 | NULL, |
5572f482 RL |
175 | NULL |
176 | }; | |
177 | #endif | |
178 | ||
179 | /* Constants used when creating the ENGINE */ | |
180 | static const char *engine_atalla_id = "atalla"; | |
181 | static const char *engine_atalla_name = "Atalla hardware engine support"; | |
182 | ||
183 | /* This internal function is used by ENGINE_atalla() and possibly by the | |
184 | * "dynamic" ENGINE support too */ | |
185 | static int bind_helper(ENGINE *e) | |
186 | { | |
187 | #ifndef OPENSSL_NO_RSA | |
188 | const RSA_METHOD *meth1; | |
189 | #endif | |
190 | #ifndef OPENSSL_NO_DSA | |
191 | const DSA_METHOD *meth2; | |
192 | #endif | |
193 | #ifndef OPENSSL_NO_DH | |
194 | const DH_METHOD *meth3; | |
195 | #endif | |
196 | if(!ENGINE_set_id(e, engine_atalla_id) || | |
197 | !ENGINE_set_name(e, engine_atalla_name) || | |
198 | #ifndef OPENSSL_NO_RSA | |
199 | !ENGINE_set_RSA(e, &atalla_rsa) || | |
200 | #endif | |
201 | #ifndef OPENSSL_NO_DSA | |
202 | !ENGINE_set_DSA(e, &atalla_dsa) || | |
203 | #endif | |
204 | #ifndef OPENSSL_NO_DH | |
205 | !ENGINE_set_DH(e, &atalla_dh) || | |
206 | #endif | |
207 | !ENGINE_set_destroy_function(e, atalla_destroy) || | |
208 | !ENGINE_set_init_function(e, atalla_init) || | |
209 | !ENGINE_set_finish_function(e, atalla_finish) || | |
210 | !ENGINE_set_ctrl_function(e, atalla_ctrl) || | |
211 | !ENGINE_set_cmd_defns(e, atalla_cmd_defns)) | |
212 | return 0; | |
213 | ||
214 | #ifndef OPENSSL_NO_RSA | |
215 | /* We know that the "PKCS1_SSLeay()" functions hook properly | |
216 | * to the atalla-specific mod_exp and mod_exp_crt so we use | |
217 | * those functions. NB: We don't use ENGINE_openssl() or | |
218 | * anything "more generic" because something like the RSAref | |
219 | * code may not hook properly, and if you own one of these | |
220 | * cards then you have the right to do RSA operations on it | |
221 | * anyway! */ | |
222 | meth1 = RSA_PKCS1_SSLeay(); | |
223 | atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc; | |
224 | atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec; | |
225 | atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc; | |
226 | atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec; | |
227 | #endif | |
228 | ||
229 | #ifndef OPENSSL_NO_DSA | |
230 | /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish | |
231 | * bits. */ | |
232 | meth2 = DSA_OpenSSL(); | |
233 | atalla_dsa.dsa_do_sign = meth2->dsa_do_sign; | |
234 | atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup; | |
235 | atalla_dsa.dsa_do_verify = meth2->dsa_do_verify; | |
236 | #endif | |
237 | ||
238 | #ifndef OPENSSL_NO_DH | |
239 | /* Much the same for Diffie-Hellman */ | |
240 | meth3 = DH_OpenSSL(); | |
241 | atalla_dh.generate_key = meth3->generate_key; | |
242 | atalla_dh.compute_key = meth3->compute_key; | |
243 | #endif | |
244 | ||
245 | /* Ensure the atalla error handling is set up */ | |
246 | ERR_load_ATALLA_strings(); | |
247 | return 1; | |
248 | } | |
249 | ||
8f797f14 | 250 | #ifdef OPENSSL_NO_DYNAMIC_ENGINE |
5572f482 RL |
251 | static ENGINE *engine_atalla(void) |
252 | { | |
253 | ENGINE *ret = ENGINE_new(); | |
254 | if(!ret) | |
255 | return NULL; | |
256 | if(!bind_helper(ret)) | |
257 | { | |
258 | ENGINE_free(ret); | |
259 | return NULL; | |
260 | } | |
261 | return ret; | |
262 | } | |
263 | ||
264 | void ENGINE_load_atalla(void) | |
265 | { | |
266 | /* Copied from eng_[openssl|dyn].c */ | |
267 | ENGINE *toadd = engine_atalla(); | |
268 | if(!toadd) return; | |
269 | ENGINE_add(toadd); | |
270 | ENGINE_free(toadd); | |
271 | ERR_clear_error(); | |
272 | } | |
8f797f14 | 273 | #endif |
5572f482 RL |
274 | |
275 | /* This is a process-global DSO handle used for loading and unloading | |
276 | * the Atalla library. NB: This is only set (or unset) during an | |
277 | * init() or finish() call (reference counts permitting) and they're | |
278 | * operating with global locks, so this should be thread-safe | |
279 | * implicitly. */ | |
280 | static DSO *atalla_dso = NULL; | |
281 | ||
282 | /* These are the function pointers that are (un)set when the library has | |
283 | * successfully (un)loaded. */ | |
284 | static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL; | |
285 | static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL; | |
286 | static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL; | |
287 | ||
288 | /* These are the static string constants for the DSO file name and the function | |
289 | * symbol names to bind to. Regrettably, the DSO name on *nix appears to be | |
290 | * "atasi.so" rather than something more consistent like "libatasi.so". At the | |
291 | * time of writing, I'm not sure what the file name on win32 is but clearly | |
292 | * native name translation is not possible (eg libatasi.so on *nix, and | |
293 | * atasi.dll on win32). For the purposes of testing, I have created a symbollic | |
294 | * link called "libatasi.so" so that we can use native name-translation - a | |
295 | * better solution will be needed. */ | |
296 | static const char *ATALLA_LIBNAME = NULL; | |
297 | static const char *get_ATALLA_LIBNAME(void) | |
298 | { | |
299 | if(ATALLA_LIBNAME) | |
300 | return ATALLA_LIBNAME; | |
301 | return "atasi"; | |
302 | } | |
303 | static void free_ATALLA_LIBNAME(void) | |
304 | { | |
305 | if(ATALLA_LIBNAME) | |
306 | OPENSSL_free((void*)ATALLA_LIBNAME); | |
307 | ATALLA_LIBNAME = NULL; | |
308 | } | |
309 | static long set_ATALLA_LIBNAME(const char *name) | |
310 | { | |
311 | free_ATALLA_LIBNAME(); | |
312 | return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0); | |
313 | } | |
314 | static const char *ATALLA_F1 = "ASI_GetHardwareConfig"; | |
315 | static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn"; | |
316 | static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics"; | |
317 | ||
318 | /* Destructor (complements the "ENGINE_atalla()" constructor) */ | |
319 | static int atalla_destroy(ENGINE *e) | |
320 | { | |
321 | free_ATALLA_LIBNAME(); | |
322 | /* Unload the atalla error strings so any error state including our | |
323 | * functs or reasons won't lead to a segfault (they simply get displayed | |
324 | * without corresponding string data because none will be found). */ | |
325 | ERR_unload_ATALLA_strings(); | |
326 | return 1; | |
327 | } | |
328 | ||
329 | /* (de)initialisation functions. */ | |
330 | static int atalla_init(ENGINE *e) | |
331 | { | |
332 | tfnASI_GetHardwareConfig *p1; | |
333 | tfnASI_RSAPrivateKeyOpFn *p2; | |
334 | tfnASI_GetPerformanceStatistics *p3; | |
335 | /* Not sure of the origin of this magic value, but Ben's code had it | |
336 | * and it seemed to have been working for a few people. :-) */ | |
337 | unsigned int config_buf[1024]; | |
338 | ||
339 | if(atalla_dso != NULL) | |
340 | { | |
341 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED); | |
342 | goto err; | |
343 | } | |
344 | /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be | |
345 | * changed unfortunately because the Atalla drivers don't have | |
346 | * standard library names that can be platform-translated well. */ | |
347 | /* TODO: Work out how to actually map to the names the Atalla | |
348 | * drivers really use - for now a symbollic link needs to be | |
349 | * created on the host system from libatasi.so to atasi.so on | |
350 | * unix variants. */ | |
351 | atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0); | |
352 | if(atalla_dso == NULL) | |
353 | { | |
354 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED); | |
355 | goto err; | |
356 | } | |
357 | if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func( | |
358 | atalla_dso, ATALLA_F1)) || | |
359 | !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func( | |
360 | atalla_dso, ATALLA_F2)) || | |
361 | !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func( | |
362 | atalla_dso, ATALLA_F3))) | |
363 | { | |
364 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED); | |
365 | goto err; | |
366 | } | |
367 | /* Copy the pointers */ | |
368 | p_Atalla_GetHardwareConfig = p1; | |
369 | p_Atalla_RSAPrivateKeyOpFn = p2; | |
370 | p_Atalla_GetPerformanceStatistics = p3; | |
371 | /* Perform a basic test to see if there's actually any unit | |
372 | * running. */ | |
373 | if(p1(0L, config_buf) != 0) | |
374 | { | |
375 | ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE); | |
376 | goto err; | |
377 | } | |
378 | /* Everything's fine. */ | |
379 | return 1; | |
380 | err: | |
381 | if(atalla_dso) | |
382 | DSO_free(atalla_dso); | |
383 | p_Atalla_GetHardwareConfig = NULL; | |
384 | p_Atalla_RSAPrivateKeyOpFn = NULL; | |
385 | p_Atalla_GetPerformanceStatistics = NULL; | |
386 | return 0; | |
387 | } | |
388 | ||
389 | static int atalla_finish(ENGINE *e) | |
390 | { | |
391 | free_ATALLA_LIBNAME(); | |
392 | if(atalla_dso == NULL) | |
393 | { | |
394 | ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED); | |
395 | return 0; | |
396 | } | |
397 | if(!DSO_free(atalla_dso)) | |
398 | { | |
399 | ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE); | |
400 | return 0; | |
401 | } | |
402 | atalla_dso = NULL; | |
403 | p_Atalla_GetHardwareConfig = NULL; | |
404 | p_Atalla_RSAPrivateKeyOpFn = NULL; | |
405 | p_Atalla_GetPerformanceStatistics = NULL; | |
406 | return 1; | |
407 | } | |
408 | ||
a99ce1a5 | 409 | static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) |
5572f482 RL |
410 | { |
411 | int initialised = ((atalla_dso == NULL) ? 0 : 1); | |
412 | switch(cmd) | |
413 | { | |
414 | case ATALLA_CMD_SO_PATH: | |
415 | if(p == NULL) | |
416 | { | |
417 | ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER); | |
418 | return 0; | |
419 | } | |
420 | if(initialised) | |
421 | { | |
422 | ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED); | |
423 | return 0; | |
424 | } | |
425 | return set_ATALLA_LIBNAME((const char *)p); | |
426 | default: | |
427 | break; | |
428 | } | |
429 | ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED); | |
430 | return 0; | |
431 | } | |
432 | ||
433 | static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |
434 | const BIGNUM *m, BN_CTX *ctx) | |
435 | { | |
436 | /* I need somewhere to store temporary serialised values for | |
437 | * use with the Atalla API calls. A neat cheat - I'll use | |
438 | * BIGNUMs from the BN_CTX but access their arrays directly as | |
439 | * byte arrays <grin>. This way I don't have to clean anything | |
440 | * up. */ | |
441 | BIGNUM *modulus; | |
442 | BIGNUM *exponent; | |
443 | BIGNUM *argument; | |
444 | BIGNUM *result; | |
445 | RSAPrivateKey keydata; | |
446 | int to_return, numbytes; | |
447 | ||
448 | modulus = exponent = argument = result = NULL; | |
449 | to_return = 0; /* expect failure */ | |
450 | ||
451 | if(!atalla_dso) | |
452 | { | |
453 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED); | |
454 | goto err; | |
455 | } | |
456 | /* Prepare the params */ | |
457 | BN_CTX_start(ctx); | |
458 | modulus = BN_CTX_get(ctx); | |
459 | exponent = BN_CTX_get(ctx); | |
460 | argument = BN_CTX_get(ctx); | |
461 | result = BN_CTX_get(ctx); | |
462 | if (!result) | |
463 | { | |
464 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL); | |
465 | goto err; | |
466 | } | |
467 | if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) || | |
468 | !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top)) | |
469 | { | |
470 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL); | |
471 | goto err; | |
472 | } | |
473 | /* Prepare the key-data */ | |
474 | memset(&keydata, 0,sizeof keydata); | |
475 | numbytes = BN_num_bytes(m); | |
476 | memset(exponent->d, 0, numbytes); | |
477 | memset(modulus->d, 0, numbytes); | |
478 | BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p)); | |
479 | BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m)); | |
480 | keydata.privateExponent.data = (unsigned char *)exponent->d; | |
481 | keydata.privateExponent.len = numbytes; | |
482 | keydata.modulus.data = (unsigned char *)modulus->d; | |
483 | keydata.modulus.len = numbytes; | |
484 | /* Prepare the argument */ | |
485 | memset(argument->d, 0, numbytes); | |
486 | memset(result->d, 0, numbytes); | |
487 | BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a)); | |
488 | /* Perform the operation */ | |
489 | if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d, | |
490 | (unsigned char *)argument->d, | |
491 | keydata.modulus.len) != 0) | |
492 | { | |
493 | ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED); | |
494 | goto err; | |
495 | } | |
496 | /* Convert the response */ | |
497 | BN_bin2bn((unsigned char *)result->d, numbytes, r); | |
498 | to_return = 1; | |
499 | err: | |
500 | BN_CTX_end(ctx); | |
501 | return to_return; | |
502 | } | |
503 | ||
504 | #ifndef OPENSSL_NO_RSA | |
46ef873f | 505 | static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) |
5572f482 | 506 | { |
5572f482 RL |
507 | int to_return = 0; |
508 | ||
509 | if(!atalla_dso) | |
510 | { | |
511 | ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED); | |
512 | goto err; | |
513 | } | |
5572f482 RL |
514 | if(!rsa->d || !rsa->n) |
515 | { | |
516 | ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS); | |
517 | goto err; | |
518 | } | |
519 | to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx); | |
520 | err: | |
5572f482 RL |
521 | return to_return; |
522 | } | |
523 | #endif | |
524 | ||
525 | #ifndef OPENSSL_NO_DSA | |
526 | /* This code was liberated and adapted from the commented-out code in | |
527 | * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration | |
528 | * (it doesn't have a CRT form for RSA), this function means that an | |
529 | * Atalla system running with a DSA server certificate can handshake | |
530 | * around 5 or 6 times faster/more than an equivalent system running with | |
531 | * RSA. Just check out the "signs" statistics from the RSA and DSA parts | |
532 | * of "openssl speed -engine atalla dsa1024 rsa1024". */ | |
533 | static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, | |
534 | BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, | |
535 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | |
536 | { | |
537 | BIGNUM t; | |
538 | int to_return = 0; | |
539 | ||
540 | BN_init(&t); | |
541 | /* let rr = a1 ^ p1 mod m */ | |
542 | if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end; | |
543 | /* let t = a2 ^ p2 mod m */ | |
544 | if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end; | |
545 | /* let rr = rr * t mod m */ | |
546 | if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end; | |
547 | to_return = 1; | |
548 | end: | |
549 | BN_free(&t); | |
550 | return to_return; | |
551 | } | |
552 | ||
553 | static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, | |
554 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | |
555 | BN_MONT_CTX *m_ctx) | |
556 | { | |
557 | return atalla_mod_exp(r, a, p, m, ctx); | |
558 | } | |
559 | #endif | |
560 | ||
561 | /* This function is aliased to mod_exp (with the mont stuff dropped). */ | |
562 | static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |
563 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | |
564 | { | |
565 | return atalla_mod_exp(r, a, p, m, ctx); | |
566 | } | |
567 | ||
568 | #ifndef OPENSSL_NO_DH | |
569 | /* This function is aliased to mod_exp (with the dh and mont dropped). */ | |
570 | static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r, | |
571 | const BIGNUM *a, const BIGNUM *p, | |
572 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | |
573 | { | |
574 | return atalla_mod_exp(r, a, p, m, ctx); | |
575 | } | |
576 | #endif | |
577 | ||
578 | /* This stuff is needed if this ENGINE is being compiled into a self-contained | |
579 | * shared-library. */ | |
ecd45314 | 580 | #ifndef OPENSSL_NO_DYNAMIC_ENGINE |
5572f482 RL |
581 | static int bind_fn(ENGINE *e, const char *id) |
582 | { | |
583 | if(id && (strcmp(id, engine_atalla_id) != 0)) | |
584 | return 0; | |
585 | if(!bind_helper(e)) | |
586 | return 0; | |
587 | return 1; | |
588 | } | |
589 | IMPLEMENT_DYNAMIC_CHECK_FN() | |
590 | IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) | |
ecd45314 | 591 | #endif /* OPENSSL_NO_DYNAMIC_ENGINE */ |
5572f482 RL |
592 | |
593 | #endif /* !OPENSSL_NO_HW_ATALLA */ | |
594 | #endif /* !OPENSSL_NO_HW */ |