diff options
author | beck <> | 2002-05-15 02:29:21 +0000 |
---|---|---|
committer | beck <> | 2002-05-15 02:29:21 +0000 |
commit | b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch) | |
tree | fa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/engine/hw_nuron.c | |
parent | e471e1ea98d673597b182ea85f29e30c97cd08b5 (diff) | |
download | openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2 openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip |
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/engine/hw_nuron.c')
-rw-r--r-- | src/lib/libcrypto/engine/hw_nuron.c | 399 |
1 files changed, 399 insertions, 0 deletions
diff --git a/src/lib/libcrypto/engine/hw_nuron.c b/src/lib/libcrypto/engine/hw_nuron.c new file mode 100644 index 0000000000..2672012154 --- /dev/null +++ b/src/lib/libcrypto/engine/hw_nuron.c | |||
@@ -0,0 +1,399 @@ | |||
1 | /* crypto/engine/hw_nuron.c */ | ||
2 | /* Written by Ben Laurie for the OpenSSL Project, leaning heavily on Geoff | ||
3 | * Thorpe's Atalla implementation. | ||
4 | */ | ||
5 | /* ==================================================================== | ||
6 | * Copyright (c) 2000-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 | |||
66 | #ifndef OPENSSL_NO_HW | ||
67 | #ifndef OPENSSL_NO_HW_NURON | ||
68 | |||
69 | #define NURON_LIB_NAME "nuron engine" | ||
70 | #include "hw_nuron_err.c" | ||
71 | |||
72 | static const char def_NURON_LIBNAME[] = "nuronssl"; | ||
73 | static const char *NURON_LIBNAME = def_NURON_LIBNAME; | ||
74 | static const char *NURON_F1 = "nuron_mod_exp"; | ||
75 | |||
76 | /* The definitions for control commands specific to this engine */ | ||
77 | #define NURON_CMD_SO_PATH ENGINE_CMD_BASE | ||
78 | static const ENGINE_CMD_DEFN nuron_cmd_defns[] = { | ||
79 | {NURON_CMD_SO_PATH, | ||
80 | "SO_PATH", | ||
81 | "Specifies the path to the 'nuronssl' shared library", | ||
82 | ENGINE_CMD_FLAG_STRING}, | ||
83 | {0, NULL, NULL, 0} | ||
84 | }; | ||
85 | |||
86 | typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m); | ||
87 | static tfnModExp *pfnModExp = NULL; | ||
88 | |||
89 | static DSO *pvDSOHandle = NULL; | ||
90 | |||
91 | static int nuron_destroy(ENGINE *e) | ||
92 | { | ||
93 | ERR_unload_NURON_strings(); | ||
94 | return 1; | ||
95 | } | ||
96 | |||
97 | static int nuron_init(ENGINE *e) | ||
98 | { | ||
99 | if(pvDSOHandle != NULL) | ||
100 | { | ||
101 | NURONerr(NURON_F_NURON_INIT,NURON_R_ALREADY_LOADED); | ||
102 | return 0; | ||
103 | } | ||
104 | |||
105 | pvDSOHandle = DSO_load(NULL, NURON_LIBNAME, NULL, | ||
106 | DSO_FLAG_NAME_TRANSLATION_EXT_ONLY); | ||
107 | if(!pvDSOHandle) | ||
108 | { | ||
109 | NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_NOT_FOUND); | ||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | pfnModExp = (tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1); | ||
114 | if(!pfnModExp) | ||
115 | { | ||
116 | NURONerr(NURON_F_NURON_INIT,NURON_R_DSO_FUNCTION_NOT_FOUND); | ||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | return 1; | ||
121 | } | ||
122 | |||
123 | static int nuron_finish(ENGINE *e) | ||
124 | { | ||
125 | if(pvDSOHandle == NULL) | ||
126 | { | ||
127 | NURONerr(NURON_F_NURON_FINISH,NURON_R_NOT_LOADED); | ||
128 | return 0; | ||
129 | } | ||
130 | if(!DSO_free(pvDSOHandle)) | ||
131 | { | ||
132 | NURONerr(NURON_F_NURON_FINISH,NURON_R_DSO_FAILURE); | ||
133 | return 0; | ||
134 | } | ||
135 | pvDSOHandle=NULL; | ||
136 | pfnModExp=NULL; | ||
137 | return 1; | ||
138 | } | ||
139 | |||
140 | static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()) | ||
141 | { | ||
142 | int initialised = ((pvDSOHandle == NULL) ? 0 : 1); | ||
143 | switch(cmd) | ||
144 | { | ||
145 | case NURON_CMD_SO_PATH: | ||
146 | if(p == NULL) | ||
147 | { | ||
148 | NURONerr(NURON_F_NURON_CTRL,ERR_R_PASSED_NULL_PARAMETER); | ||
149 | return 0; | ||
150 | } | ||
151 | if(initialised) | ||
152 | { | ||
153 | NURONerr(NURON_F_NURON_CTRL,NURON_R_ALREADY_LOADED); | ||
154 | return 0; | ||
155 | } | ||
156 | NURON_LIBNAME = (const char *)p; | ||
157 | return 1; | ||
158 | default: | ||
159 | break; | ||
160 | } | ||
161 | NURONerr(NURON_F_NURON_CTRL,NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED); | ||
162 | return 0; | ||
163 | } | ||
164 | |||
165 | static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p, | ||
166 | const BIGNUM *m,BN_CTX *ctx) | ||
167 | { | ||
168 | if(!pvDSOHandle) | ||
169 | { | ||
170 | NURONerr(NURON_F_NURON_MOD_EXP,NURON_R_NOT_LOADED); | ||
171 | return 0; | ||
172 | } | ||
173 | return pfnModExp(r,a,p,m); | ||
174 | } | ||
175 | |||
176 | #ifndef OPENSSL_NO_RSA | ||
177 | static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | ||
178 | { | ||
179 | return nuron_mod_exp(r0,I,rsa->d,rsa->n,NULL); | ||
180 | } | ||
181 | #endif | ||
182 | |||
183 | #ifndef OPENSSL_NO_DSA | ||
184 | /* This code was liberated and adapted from the commented-out code in | ||
185 | * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration | ||
186 | * (it doesn't have a CRT form for RSA), this function means that an | ||
187 | * Atalla system running with a DSA server certificate can handshake | ||
188 | * around 5 or 6 times faster/more than an equivalent system running with | ||
189 | * RSA. Just check out the "signs" statistics from the RSA and DSA parts | ||
190 | * of "openssl speed -engine atalla dsa1024 rsa1024". */ | ||
191 | static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, | ||
192 | BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, | ||
193 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
194 | { | ||
195 | BIGNUM t; | ||
196 | int to_return = 0; | ||
197 | |||
198 | BN_init(&t); | ||
199 | /* let rr = a1 ^ p1 mod m */ | ||
200 | if (!nuron_mod_exp(rr,a1,p1,m,ctx)) | ||
201 | goto end; | ||
202 | /* let t = a2 ^ p2 mod m */ | ||
203 | if (!nuron_mod_exp(&t,a2,p2,m,ctx)) | ||
204 | goto end; | ||
205 | /* let rr = rr * t mod m */ | ||
206 | if (!BN_mod_mul(rr,rr,&t,m,ctx)) | ||
207 | goto end; | ||
208 | to_return = 1; | ||
209 | end: | ||
210 | BN_free(&t); | ||
211 | return to_return; | ||
212 | } | ||
213 | |||
214 | |||
215 | static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, | ||
216 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
217 | BN_MONT_CTX *m_ctx) | ||
218 | { | ||
219 | return nuron_mod_exp(r, a, p, m, ctx); | ||
220 | } | ||
221 | #endif | ||
222 | |||
223 | /* This function is aliased to mod_exp (with the mont stuff dropped). */ | ||
224 | static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
225 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | ||
226 | { | ||
227 | return nuron_mod_exp(r, a, p, m, ctx); | ||
228 | } | ||
229 | |||
230 | #ifndef OPENSSL_NO_DH | ||
231 | /* This function is aliased to mod_exp (with the dh and mont dropped). */ | ||
232 | static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r, | ||
233 | const BIGNUM *a, const BIGNUM *p, | ||
234 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | ||
235 | { | ||
236 | return nuron_mod_exp(r, a, p, m, ctx); | ||
237 | } | ||
238 | #endif | ||
239 | |||
240 | #ifndef OPENSSL_NO_RSA | ||
241 | static RSA_METHOD nuron_rsa = | ||
242 | { | ||
243 | "Nuron RSA method", | ||
244 | NULL, | ||
245 | NULL, | ||
246 | NULL, | ||
247 | NULL, | ||
248 | nuron_rsa_mod_exp, | ||
249 | nuron_mod_exp_mont, | ||
250 | NULL, | ||
251 | NULL, | ||
252 | 0, | ||
253 | NULL, | ||
254 | NULL, | ||
255 | NULL | ||
256 | }; | ||
257 | #endif | ||
258 | |||
259 | #ifndef OPENSSL_NO_DSA | ||
260 | static DSA_METHOD nuron_dsa = | ||
261 | { | ||
262 | "Nuron DSA method", | ||
263 | NULL, /* dsa_do_sign */ | ||
264 | NULL, /* dsa_sign_setup */ | ||
265 | NULL, /* dsa_do_verify */ | ||
266 | nuron_dsa_mod_exp, /* dsa_mod_exp */ | ||
267 | nuron_mod_exp_dsa, /* bn_mod_exp */ | ||
268 | NULL, /* init */ | ||
269 | NULL, /* finish */ | ||
270 | 0, /* flags */ | ||
271 | NULL /* app_data */ | ||
272 | }; | ||
273 | #endif | ||
274 | |||
275 | #ifndef OPENSSL_NO_DH | ||
276 | static DH_METHOD nuron_dh = | ||
277 | { | ||
278 | "Nuron DH method", | ||
279 | NULL, | ||
280 | NULL, | ||
281 | nuron_mod_exp_dh, | ||
282 | NULL, | ||
283 | NULL, | ||
284 | 0, | ||
285 | NULL | ||
286 | }; | ||
287 | #endif | ||
288 | |||
289 | /* Constants used when creating the ENGINE */ | ||
290 | static const char *engine_nuron_id = "nuron"; | ||
291 | static const char *engine_nuron_name = "Nuron hardware engine support"; | ||
292 | |||
293 | /* This internal function is used by ENGINE_nuron() and possibly by the | ||
294 | * "dynamic" ENGINE support too */ | ||
295 | static int bind_helper(ENGINE *e) | ||
296 | { | ||
297 | #ifndef OPENSSL_NO_RSA | ||
298 | const RSA_METHOD *meth1; | ||
299 | #endif | ||
300 | #ifndef OPENSSL_NO_DSA | ||
301 | const DSA_METHOD *meth2; | ||
302 | #endif | ||
303 | #ifndef OPENSSL_NO_DH | ||
304 | const DH_METHOD *meth3; | ||
305 | #endif | ||
306 | if(!ENGINE_set_id(e, engine_nuron_id) || | ||
307 | !ENGINE_set_name(e, engine_nuron_name) || | ||
308 | #ifndef OPENSSL_NO_RSA | ||
309 | !ENGINE_set_RSA(e, &nuron_rsa) || | ||
310 | #endif | ||
311 | #ifndef OPENSSL_NO_DSA | ||
312 | !ENGINE_set_DSA(e, &nuron_dsa) || | ||
313 | #endif | ||
314 | #ifndef OPENSSL_NO_DH | ||
315 | !ENGINE_set_DH(e, &nuron_dh) || | ||
316 | #endif | ||
317 | !ENGINE_set_destroy_function(e, nuron_destroy) || | ||
318 | !ENGINE_set_init_function(e, nuron_init) || | ||
319 | !ENGINE_set_finish_function(e, nuron_finish) || | ||
320 | !ENGINE_set_ctrl_function(e, nuron_ctrl) || | ||
321 | !ENGINE_set_cmd_defns(e, nuron_cmd_defns)) | ||
322 | return 0; | ||
323 | |||
324 | #ifndef OPENSSL_NO_RSA | ||
325 | /* We know that the "PKCS1_SSLeay()" functions hook properly | ||
326 | * to the nuron-specific mod_exp and mod_exp_crt so we use | ||
327 | * those functions. NB: We don't use ENGINE_openssl() or | ||
328 | * anything "more generic" because something like the RSAref | ||
329 | * code may not hook properly, and if you own one of these | ||
330 | * cards then you have the right to do RSA operations on it | ||
331 | * anyway! */ | ||
332 | meth1=RSA_PKCS1_SSLeay(); | ||
333 | nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc; | ||
334 | nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec; | ||
335 | nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc; | ||
336 | nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec; | ||
337 | #endif | ||
338 | |||
339 | #ifndef OPENSSL_NO_DSA | ||
340 | /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish | ||
341 | * bits. */ | ||
342 | meth2=DSA_OpenSSL(); | ||
343 | nuron_dsa.dsa_do_sign=meth2->dsa_do_sign; | ||
344 | nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup; | ||
345 | nuron_dsa.dsa_do_verify=meth2->dsa_do_verify; | ||
346 | #endif | ||
347 | |||
348 | #ifndef OPENSSL_NO_DH | ||
349 | /* Much the same for Diffie-Hellman */ | ||
350 | meth3=DH_OpenSSL(); | ||
351 | nuron_dh.generate_key=meth3->generate_key; | ||
352 | nuron_dh.compute_key=meth3->compute_key; | ||
353 | #endif | ||
354 | |||
355 | /* Ensure the nuron error handling is set up */ | ||
356 | ERR_load_NURON_strings(); | ||
357 | return 1; | ||
358 | } | ||
359 | |||
360 | static ENGINE *engine_nuron(void) | ||
361 | { | ||
362 | ENGINE *ret = ENGINE_new(); | ||
363 | if(!ret) | ||
364 | return NULL; | ||
365 | if(!bind_helper(ret)) | ||
366 | { | ||
367 | ENGINE_free(ret); | ||
368 | return NULL; | ||
369 | } | ||
370 | return ret; | ||
371 | } | ||
372 | |||
373 | void ENGINE_load_nuron(void) | ||
374 | { | ||
375 | /* Copied from eng_[openssl|dyn].c */ | ||
376 | ENGINE *toadd = engine_nuron(); | ||
377 | if(!toadd) return; | ||
378 | ENGINE_add(toadd); | ||
379 | ENGINE_free(toadd); | ||
380 | ERR_clear_error(); | ||
381 | } | ||
382 | |||
383 | /* This stuff is needed if this ENGINE is being compiled into a self-contained | ||
384 | * shared-library. */ | ||
385 | #ifdef ENGINE_DYNAMIC_SUPPORT | ||
386 | static int bind_fn(ENGINE *e, const char *id) | ||
387 | { | ||
388 | if(id && (strcmp(id, engine_nuron_id) != 0)) | ||
389 | return 0; | ||
390 | if(!bind_helper(e)) | ||
391 | return 0; | ||
392 | return 1; | ||
393 | } | ||
394 | IMPLEMENT_DYNAMIC_CHECK_FN() | ||
395 | IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) | ||
396 | #endif /* ENGINE_DYNAMIC_SUPPORT */ | ||
397 | |||
398 | #endif /* !OPENSSL_NO_HW_NURON */ | ||
399 | #endif /* !OPENSSL_NO_HW */ | ||